blob: e1fdf86eb0267acee2ed5de70f276fa6cb281e2b [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;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000133 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
134 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000135 const FieldDecl *FD = *i;
136 QualType FT = FD->getType();
137
138 // Ignore empty fields.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000139 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000140 continue;
141
142 // If we already found an element then this isn't a single-element
143 // struct.
144 if (Found)
145 return 0;
146
147 // Treat single element arrays as the element.
148 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
149 if (AT->getSize().getZExtValue() != 1)
150 break;
151 FT = AT->getElementType();
152 }
153
154 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
155 Found = FT.getTypePtr();
156 } else {
157 Found = isSingleElementStruct(FT, Context);
158 if (!Found)
159 return 0;
160 }
161 }
162
163 return Found;
164}
165
166static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000167 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
168 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
169 !Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000170 return false;
171
172 uint64_t Size = Context.getTypeSize(Ty);
173 return Size == 32 || Size == 64;
174}
175
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000176/// canExpandIndirectArgument - Test whether an argument type which is to be
177/// passed indirectly (on the stack) would have the equivalent layout if it was
178/// expanded into separate arguments. If so, we prefer to do the latter to avoid
179/// inhibiting optimizations.
180///
181// FIXME: This predicate is missing many cases, currently it just follows
182// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
183// should probably make this smarter, or better yet make the LLVM backend
184// capable of handling it.
185static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
186 // We can only expand structure types.
187 const RecordType *RT = Ty->getAs<RecordType>();
188 if (!RT)
189 return false;
190
191 // We can only expand (C) structures.
192 //
193 // FIXME: This needs to be generalized to handle classes as well.
194 const RecordDecl *RD = RT->getDecl();
195 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
196 return false;
197
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000198 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
199 i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000200 const FieldDecl *FD = *i;
201
202 if (!is32Or64BitBasicType(FD->getType(), Context))
203 return false;
204
205 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
206 // how to expand them yet, and the predicate for telling if a bitfield still
207 // counts as "basic" is more complicated than what we were doing previously.
208 if (FD->isBitField())
209 return false;
210 }
211
212 return true;
213}
214
Eli Friedman3192cc82009-06-13 21:37:10 +0000215static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000216 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
217 i != e; ++i) {
Eli Friedman3192cc82009-06-13 21:37:10 +0000218 const FieldDecl *FD = *i;
219
220 if (FD->getType()->isVectorType() &&
221 Context.getTypeSize(FD->getType()) >= 128)
222 return true;
223
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000224 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000225 if (typeContainsSSEVector(RT->getDecl(), Context))
226 return true;
227 }
228
229 return false;
230}
231
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000232namespace {
233/// DefaultABIInfo - The default implementation for ABI specific
234/// details. This implementation provides information which results in
235/// self-consistent and sensible LLVM IR generation, but does not
236/// conform to any particular ABI.
237class DefaultABIInfo : public ABIInfo {
238 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000239 ASTContext &Context,
240 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000241
242 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000243 ASTContext &Context,
244 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000245
Owen Anderson170229f2009-07-14 23:10:40 +0000246 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
247 llvm::LLVMContext &VMContext) const {
248 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
249 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000250 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
251 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000252 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000253 }
254
255 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
256 CodeGenFunction &CGF) const;
257};
258
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000259class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
260public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000261 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000262};
263
264llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
265 CodeGenFunction &CGF) const {
266 return 0;
267}
268
269ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
270 ASTContext &Context,
271 llvm::LLVMContext &VMContext) const {
Chris Lattner9723d6c2010-03-11 18:19:55 +0000272 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000273 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000274
Chris Lattner9723d6c2010-03-11 18:19:55 +0000275 // Treat an enum type as its underlying type.
276 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
277 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregora71cc152010-02-02 20:10:50 +0000278
Chris Lattner9723d6c2010-03-11 18:19:55 +0000279 return (Ty->isPromotableIntegerType() ?
280 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000281}
282
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000283/// X86_32ABIInfo - The X86-32 ABI information.
284class X86_32ABIInfo : public ABIInfo {
285 ASTContext &Context;
David Chisnallde3a0692009-08-17 23:08:21 +0000286 bool IsDarwinVectorABI;
287 bool IsSmallStructInRegABI;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000288
289 static bool isRegisterSize(unsigned Size) {
290 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
291 }
292
293 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
294
Daniel Dunbar557893d2010-04-21 19:10:51 +0000295 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
296 /// such that the argument will be passed in memory.
297 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
298 bool ByVal = true) const;
299
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000300public:
301 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000302 ASTContext &Context,
303 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000304
305 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000306 ASTContext &Context,
307 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000308
Owen Anderson170229f2009-07-14 23:10:40 +0000309 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
310 llvm::LLVMContext &VMContext) const {
311 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
312 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000313 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
314 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000315 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000316 }
317
318 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
319 CodeGenFunction &CGF) const;
320
David Chisnallde3a0692009-08-17 23:08:21 +0000321 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000322 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000323 IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000324};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000325
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000326class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
327public:
328 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor0599df12010-01-22 15:41:14 +0000329 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000330
331 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
332 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000333
334 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
335 // Darwin uses different dwarf register numbers for EH.
336 if (CGM.isTargetDarwin()) return 5;
337
338 return 4;
339 }
340
341 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
342 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000343};
344
345}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000346
347/// shouldReturnTypeInRegister - Determine if the given type should be
348/// passed in a register (for the Darwin ABI).
349bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
350 ASTContext &Context) {
351 uint64_t Size = Context.getTypeSize(Ty);
352
353 // Type must be register sized.
354 if (!isRegisterSize(Size))
355 return false;
356
357 if (Ty->isVectorType()) {
358 // 64- and 128- bit vectors inside structures are not returned in
359 // registers.
360 if (Size == 64 || Size == 128)
361 return false;
362
363 return true;
364 }
365
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000366 // If this is a builtin, pointer, enum, or complex type, it is ok.
367 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
368 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
369 Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000370 return true;
371
372 // Arrays are treated like records.
373 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
374 return shouldReturnTypeInRegister(AT->getElementType(), Context);
375
376 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000377 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000378 if (!RT) return false;
379
Anders Carlsson40446e82010-01-27 03:25:19 +0000380 // FIXME: Traverse bases here too.
381
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000382 // Structure types are passed in register if all fields would be
383 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000384 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
385 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000386 const FieldDecl *FD = *i;
387
388 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000389 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000390 continue;
391
392 // Check fields recursively.
393 if (!shouldReturnTypeInRegister(FD->getType(), Context))
394 return false;
395 }
396
397 return true;
398}
399
400ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000401 ASTContext &Context,
402 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000403 if (RetTy->isVoidType()) {
404 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000405 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000406 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000407 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000408 uint64_t Size = Context.getTypeSize(RetTy);
409
410 // 128-bit vectors are a special case; they are returned in
411 // registers and we need to make sure to pick a type the LLVM
412 // backend will like.
413 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000414 return ABIArgInfo::getCoerce(llvm::VectorType::get(
415 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000416
417 // Always return in register if it fits in a general purpose
418 // register, or if it is 64 bits and has a single element.
419 if ((Size == 8 || Size == 16 || Size == 32) ||
420 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000421 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000422
423 return ABIArgInfo::getIndirect(0);
424 }
425
426 return ABIArgInfo::getDirect();
427 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000428 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000429 // Structures with either a non-trivial destructor or a non-trivial
430 // copy constructor are always indirect.
431 if (hasNonTrivialDestructorOrCopyConstructor(RT))
432 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
433
434 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000435 if (RT->getDecl()->hasFlexibleArrayMember())
436 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000437 }
438
David Chisnallde3a0692009-08-17 23:08:21 +0000439 // If specified, structs and unions are always indirect.
440 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000441 return ABIArgInfo::getIndirect(0);
442
443 // Classify "single element" structs as their element type.
444 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall9dd450b2009-09-21 23:43:11 +0000445 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000446 if (BT->isIntegerType()) {
447 // We need to use the size of the structure, padding
448 // bit-fields can adjust that to be larger than the single
449 // element type.
450 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000451 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000452 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000453 } else if (BT->getKind() == BuiltinType::Float) {
454 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
455 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000456 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000457 } else if (BT->getKind() == BuiltinType::Double) {
458 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
459 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000460 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000461 }
462 } else if (SeltTy->isPointerType()) {
463 // FIXME: It would be really nice if this could come out as the proper
464 // pointer type.
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000465 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000466 return ABIArgInfo::getCoerce(PtrTy);
467 } else if (SeltTy->isVectorType()) {
468 // 64- and 128-bit vectors are never returned in a
469 // register when inside a structure.
470 uint64_t Size = Context.getTypeSize(RetTy);
471 if (Size == 64 || Size == 128)
472 return ABIArgInfo::getIndirect(0);
473
Owen Anderson170229f2009-07-14 23:10:40 +0000474 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000475 }
476 }
477
478 // Small structures which are register sized are generally returned
479 // in a register.
480 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
481 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000482 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000483 }
484
485 return ABIArgInfo::getIndirect(0);
486 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000487 // Treat an enum type as its underlying type.
488 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
489 RetTy = EnumTy->getDecl()->getIntegerType();
490
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000491 return (RetTy->isPromotableIntegerType() ?
492 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000493 }
494}
495
Daniel Dunbar557893d2010-04-21 19:10:51 +0000496ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
497 ASTContext &Context,
498 bool ByVal) const {
Daniel Dunbar53fac692010-04-21 19:49:55 +0000499 if (!ByVal)
500 return ABIArgInfo::getIndirect(0, false);
501
502 // Compute the byval alignment. We trust the back-end to honor the
503 // minimum ABI alignment for byval, to make cleaner IR.
504 const unsigned MinABIAlign = 4;
505 unsigned Align = Context.getTypeAlign(Ty) / 8;
506 if (Align > MinABIAlign)
507 return ABIArgInfo::getIndirect(Align);
508 return ABIArgInfo::getIndirect(0);
Daniel Dunbar557893d2010-04-21 19:10:51 +0000509}
510
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000511ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000512 ASTContext &Context,
513 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000514 // FIXME: Set alignment on indirect arguments.
515 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
516 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000517 if (const RecordType *RT = Ty->getAs<RecordType>()) {
518 // Structures with either a non-trivial destructor or a non-trivial
519 // copy constructor are always indirect.
520 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbar557893d2010-04-21 19:10:51 +0000521 return getIndirectResult(Ty, Context, /*ByVal=*/false);
522
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000523 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar557893d2010-04-21 19:10:51 +0000524 return getIndirectResult(Ty, Context);
Anders Carlsson40446e82010-01-27 03:25:19 +0000525 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000526
527 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000528 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000529 return ABIArgInfo::getIgnore();
530
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000531 // Expand small (<= 128-bit) record types when we know that the stack layout
532 // of those arguments will match the struct. This is important because the
533 // LLVM backend isn't smart enough to remove byval, which inhibits many
534 // optimizations.
535 if (Context.getTypeSize(Ty) <= 4*32 &&
536 canExpandIndirectArgument(Ty, Context))
537 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000538
Daniel Dunbar557893d2010-04-21 19:10:51 +0000539 return getIndirectResult(Ty, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000540 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000541 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
542 Ty = EnumTy->getDecl()->getIntegerType();
543
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000544 return (Ty->isPromotableIntegerType() ?
545 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000546 }
547}
548
549llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
550 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000551 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000552 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000553
554 CGBuilderTy &Builder = CGF.Builder;
555 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
556 "ap");
557 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
558 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000559 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000560 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
561
562 uint64_t Offset =
563 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
564 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +0000565 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
566 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000567 "ap.next");
568 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
569
570 return AddrTyped;
571}
572
Charles Davis4ea31ab2010-02-13 15:54:06 +0000573void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
574 llvm::GlobalValue *GV,
575 CodeGen::CodeGenModule &CGM) const {
576 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
577 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
578 // Get the LLVM function.
579 llvm::Function *Fn = cast<llvm::Function>(GV);
580
581 // Now add the 'alignstack' attribute with a value of 16.
582 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
583 }
584 }
585}
586
John McCallbeec5a02010-03-06 00:35:14 +0000587bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
588 CodeGen::CodeGenFunction &CGF,
589 llvm::Value *Address) const {
590 CodeGen::CGBuilderTy &Builder = CGF.Builder;
591 llvm::LLVMContext &Context = CGF.getLLVMContext();
592
593 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
594 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
595
596 // 0-7 are the eight integer registers; the order is different
597 // on Darwin (for EH), but the range is the same.
598 // 8 is %eip.
599 for (unsigned I = 0, E = 9; I != E; ++I) {
600 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
601 Builder.CreateStore(Four8, Slot);
602 }
603
604 if (CGF.CGM.isTargetDarwin()) {
605 // 12-16 are st(0..4). Not sure why we stop at 4.
606 // These have size 16, which is sizeof(long double) on
607 // platforms with 8-byte alignment for that type.
608 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
609 for (unsigned I = 12, E = 17; I != E; ++I) {
610 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
611 Builder.CreateStore(Sixteen8, Slot);
612 }
613
614 } else {
615 // 9 is %eflags, which doesn't get a size on Darwin for some
616 // reason.
617 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
618
619 // 11-16 are st(0..5). Not sure why we stop at 5.
620 // These have size 12, which is sizeof(long double) on
621 // platforms with 4-byte alignment for that type.
622 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
623 for (unsigned I = 11, E = 17; I != E; ++I) {
624 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
625 Builder.CreateStore(Twelve8, Slot);
626 }
627 }
628
629 return false;
630}
631
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000632namespace {
633/// X86_64ABIInfo - The X86_64 ABI information.
634class X86_64ABIInfo : public ABIInfo {
635 enum Class {
636 Integer = 0,
637 SSE,
638 SSEUp,
639 X87,
640 X87Up,
641 ComplexX87,
642 NoClass,
643 Memory
644 };
645
646 /// merge - Implement the X86_64 ABI merging algorithm.
647 ///
648 /// Merge an accumulating classification \arg Accum with a field
649 /// classification \arg Field.
650 ///
651 /// \param Accum - The accumulating classification. This should
652 /// always be either NoClass or the result of a previous merge
653 /// call. In addition, this should never be Memory (the caller
654 /// should just return Memory for the aggregate).
655 Class merge(Class Accum, Class Field) const;
656
657 /// classify - Determine the x86_64 register classes in which the
658 /// given type T should be passed.
659 ///
660 /// \param Lo - The classification for the parts of the type
661 /// residing in the low word of the containing object.
662 ///
663 /// \param Hi - The classification for the parts of the type
664 /// residing in the high word of the containing object.
665 ///
666 /// \param OffsetBase - The bit offset of this type in the
667 /// containing object. Some parameters are classified different
668 /// depending on whether they straddle an eightbyte boundary.
669 ///
670 /// If a word is unused its result will be NoClass; if a type should
671 /// be passed in Memory then at least the classification of \arg Lo
672 /// will be Memory.
673 ///
674 /// The \arg Lo class will be NoClass iff the argument is ignored.
675 ///
676 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
677 /// also be ComplexX87.
678 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
679 Class &Lo, Class &Hi) const;
680
681 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
682 /// to coerce to, chose the best way to pass Ty in the same place
683 /// that \arg CoerceTo would be passed, but while keeping the
684 /// emitted code as simple as possible.
685 ///
686 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
687 /// the ways we might want to pass things, instead of constructing an LLVM
688 /// type. This makes this code more explicit, and it makes it clearer that we
689 /// are also doing this for correctness in the case of passing scalar types.
690 ABIArgInfo getCoerceResult(QualType Ty,
691 const llvm::Type *CoerceTo,
692 ASTContext &Context) const;
693
694 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Daniel Dunbar53fac692010-04-21 19:49:55 +0000695 /// such that the argument will be returned in memory.
696 ABIArgInfo getIndirectReturnResult(QualType Ty, ASTContext &Context) const;
697
698 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000699 /// such that the argument will be passed in memory.
Daniel Dunbar557893d2010-04-21 19:10:51 +0000700 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000701
702 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000703 ASTContext &Context,
704 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000705
706 ABIArgInfo classifyArgumentType(QualType Ty,
707 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000708 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000709 unsigned &neededInt,
710 unsigned &neededSSE) const;
711
712public:
Owen Anderson170229f2009-07-14 23:10:40 +0000713 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
714 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000715
716 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
717 CodeGenFunction &CGF) const;
718};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000719
720class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
721public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000722 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCallbeec5a02010-03-06 00:35:14 +0000723
724 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
725 return 7;
726 }
727
728 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
729 llvm::Value *Address) const {
730 CodeGen::CGBuilderTy &Builder = CGF.Builder;
731 llvm::LLVMContext &Context = CGF.getLLVMContext();
732
733 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
734 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
735
736 // 0-16 are the 16 integer registers.
737 // 17 is %rip.
738 for (unsigned I = 0, E = 17; I != E; ++I) {
739 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
740 Builder.CreateStore(Eight8, Slot);
741 }
742
743 return false;
744 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000745};
746
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000747}
748
749X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
750 Class Field) const {
751 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
752 // classified recursively so that always two fields are
753 // considered. The resulting class is calculated according to
754 // the classes of the fields in the eightbyte:
755 //
756 // (a) If both classes are equal, this is the resulting class.
757 //
758 // (b) If one of the classes is NO_CLASS, the resulting class is
759 // the other class.
760 //
761 // (c) If one of the classes is MEMORY, the result is the MEMORY
762 // class.
763 //
764 // (d) If one of the classes is INTEGER, the result is the
765 // INTEGER.
766 //
767 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
768 // MEMORY is used as class.
769 //
770 // (f) Otherwise class SSE is used.
771
772 // Accum should never be memory (we should have returned) or
773 // ComplexX87 (because this cannot be passed in a structure).
774 assert((Accum != Memory && Accum != ComplexX87) &&
775 "Invalid accumulated classification during merge.");
776 if (Accum == Field || Field == NoClass)
777 return Accum;
778 else if (Field == Memory)
779 return Memory;
780 else if (Accum == NoClass)
781 return Field;
782 else if (Accum == Integer || Field == Integer)
783 return Integer;
784 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
785 Accum == X87 || Accum == X87Up)
786 return Memory;
787 else
788 return SSE;
789}
790
791void X86_64ABIInfo::classify(QualType Ty,
792 ASTContext &Context,
793 uint64_t OffsetBase,
794 Class &Lo, Class &Hi) const {
795 // FIXME: This code can be simplified by introducing a simple value class for
796 // Class pairs with appropriate constructor methods for the various
797 // situations.
798
799 // FIXME: Some of the split computations are wrong; unaligned vectors
800 // shouldn't be passed in registers for example, so there is no chance they
801 // can straddle an eightbyte. Verify & simplify.
802
803 Lo = Hi = NoClass;
804
805 Class &Current = OffsetBase < 64 ? Lo : Hi;
806 Current = Memory;
807
John McCall9dd450b2009-09-21 23:43:11 +0000808 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000809 BuiltinType::Kind k = BT->getKind();
810
811 if (k == BuiltinType::Void) {
812 Current = NoClass;
813 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
814 Lo = Integer;
815 Hi = Integer;
816 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
817 Current = Integer;
818 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
819 Current = SSE;
820 } else if (k == BuiltinType::LongDouble) {
821 Lo = X87;
822 Hi = X87Up;
823 }
824 // FIXME: _Decimal32 and _Decimal64 are SSE.
825 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall9dd450b2009-09-21 23:43:11 +0000826 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000827 // Classify the underlying integer type.
828 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
829 } else if (Ty->hasPointerRepresentation()) {
830 Current = Integer;
John McCall9dd450b2009-09-21 23:43:11 +0000831 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000832 uint64_t Size = Context.getTypeSize(VT);
833 if (Size == 32) {
834 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
835 // float> as integer.
836 Current = Integer;
837
838 // If this type crosses an eightbyte boundary, it should be
839 // split.
840 uint64_t EB_Real = (OffsetBase) / 64;
841 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
842 if (EB_Real != EB_Imag)
843 Hi = Lo;
844 } else if (Size == 64) {
845 // gcc passes <1 x double> in memory. :(
846 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
847 return;
848
849 // gcc passes <1 x long long> as INTEGER.
850 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
851 Current = Integer;
852 else
853 Current = SSE;
854
855 // If this type crosses an eightbyte boundary, it should be
856 // split.
857 if (OffsetBase && OffsetBase != 64)
858 Hi = Lo;
859 } else if (Size == 128) {
860 Lo = SSE;
861 Hi = SSEUp;
862 }
John McCall9dd450b2009-09-21 23:43:11 +0000863 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000864 QualType ET = Context.getCanonicalType(CT->getElementType());
865
866 uint64_t Size = Context.getTypeSize(Ty);
867 if (ET->isIntegralType()) {
868 if (Size <= 64)
869 Current = Integer;
870 else if (Size <= 128)
871 Lo = Hi = Integer;
872 } else if (ET == Context.FloatTy)
873 Current = SSE;
874 else if (ET == Context.DoubleTy)
875 Lo = Hi = SSE;
876 else if (ET == Context.LongDoubleTy)
877 Current = ComplexX87;
878
879 // If this complex type crosses an eightbyte boundary then it
880 // should be split.
881 uint64_t EB_Real = (OffsetBase) / 64;
882 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
883 if (Hi == NoClass && EB_Real != EB_Imag)
884 Hi = Lo;
885 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
886 // Arrays are treated like structures.
887
888 uint64_t Size = Context.getTypeSize(Ty);
889
890 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
891 // than two eightbytes, ..., it has class MEMORY.
892 if (Size > 128)
893 return;
894
895 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
896 // fields, it has class MEMORY.
897 //
898 // Only need to check alignment of array base.
899 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
900 return;
901
902 // Otherwise implement simplified merge. We could be smarter about
903 // this, but it isn't worth it and would be harder to verify.
904 Current = NoClass;
905 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
906 uint64_t ArraySize = AT->getSize().getZExtValue();
907 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
908 Class FieldLo, FieldHi;
909 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
910 Lo = merge(Lo, FieldLo);
911 Hi = merge(Hi, FieldHi);
912 if (Lo == Memory || Hi == Memory)
913 break;
914 }
915
916 // Do post merger cleanup (see below). Only case we worry about is Memory.
917 if (Hi == Memory)
918 Lo = Memory;
919 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000920 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000921 uint64_t Size = Context.getTypeSize(Ty);
922
923 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
924 // than two eightbytes, ..., it has class MEMORY.
925 if (Size > 128)
926 return;
927
Anders Carlsson20759ad2009-09-16 15:53:40 +0000928 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
929 // copy constructor or a non-trivial destructor, it is passed by invisible
930 // reference.
931 if (hasNonTrivialDestructorOrCopyConstructor(RT))
932 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000933
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000934 const RecordDecl *RD = RT->getDecl();
935
936 // Assume variable sized types are passed in memory.
937 if (RD->hasFlexibleArrayMember())
938 return;
939
940 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
941
942 // Reset Lo class, this will be recomputed.
943 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000944
945 // If this is a C++ record, classify the bases first.
946 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
947 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
948 e = CXXRD->bases_end(); i != e; ++i) {
949 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
950 "Unexpected base class!");
951 const CXXRecordDecl *Base =
952 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
953
954 // Classify this field.
955 //
956 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
957 // single eightbyte, each is classified separately. Each eightbyte gets
958 // initialized to class NO_CLASS.
959 Class FieldLo, FieldHi;
960 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
961 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
962 Lo = merge(Lo, FieldLo);
963 Hi = merge(Hi, FieldHi);
964 if (Lo == Memory || Hi == Memory)
965 break;
966 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +0000967
968 // If this record has no fields but isn't empty, classify as INTEGER.
969 if (RD->field_empty() && Size)
970 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000971 }
972
973 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000974 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000975 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
976 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000977 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
978 bool BitField = i->isBitField();
979
980 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
981 // fields, it has class MEMORY.
982 //
983 // Note, skip this test for bit-fields, see below.
984 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
985 Lo = Memory;
986 return;
987 }
988
989 // Classify this field.
990 //
991 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
992 // exceeds a single eightbyte, each is classified
993 // separately. Each eightbyte gets initialized to class
994 // NO_CLASS.
995 Class FieldLo, FieldHi;
996
997 // Bit-fields require special handling, they do not force the
998 // structure to be passed in memory even if unaligned, and
999 // therefore they can straddle an eightbyte.
1000 if (BitField) {
1001 // Ignore padding bit-fields.
1002 if (i->isUnnamedBitfield())
1003 continue;
1004
1005 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1006 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1007
1008 uint64_t EB_Lo = Offset / 64;
1009 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1010 FieldLo = FieldHi = NoClass;
1011 if (EB_Lo) {
1012 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1013 FieldLo = NoClass;
1014 FieldHi = Integer;
1015 } else {
1016 FieldLo = Integer;
1017 FieldHi = EB_Hi ? Integer : NoClass;
1018 }
1019 } else
1020 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1021 Lo = merge(Lo, FieldLo);
1022 Hi = merge(Hi, FieldHi);
1023 if (Lo == Memory || Hi == Memory)
1024 break;
1025 }
1026
1027 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1028 //
1029 // (a) If one of the classes is MEMORY, the whole argument is
1030 // passed in memory.
1031 //
1032 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1033
1034 // The first of these conditions is guaranteed by how we implement
1035 // the merge (just bail).
1036 //
1037 // The second condition occurs in the case of unions; for example
1038 // union { _Complex double; unsigned; }.
1039 if (Hi == Memory)
1040 Lo = Memory;
1041 if (Hi == SSEUp && Lo != SSE)
1042 Hi = SSE;
1043 }
1044}
1045
1046ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1047 const llvm::Type *CoerceTo,
1048 ASTContext &Context) const {
Owen Anderson41a75022009-08-13 21:57:51 +00001049 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001050 // Integer and pointer types will end up in a general purpose
1051 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001052
1053 // Treat an enum type as its underlying type.
1054 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1055 Ty = EnumTy->getDecl()->getIntegerType();
1056
Anders Carlsson03747422009-09-26 03:56:53 +00001057 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001058 return (Ty->isPromotableIntegerType() ?
1059 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson41a75022009-08-13 21:57:51 +00001060 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
John McCall8ee376f2010-02-24 07:14:12 +00001061 assert(Ty.isCanonical() && "should always have a canonical type here");
1062 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001063
1064 // Float and double end up in a single SSE reg.
John McCall8ee376f2010-02-24 07:14:12 +00001065 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001066 return ABIArgInfo::getDirect();
1067
1068 }
1069
1070 return ABIArgInfo::getCoerce(CoerceTo);
1071}
1072
Daniel Dunbar53fac692010-04-21 19:49:55 +00001073ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty,
1074 ASTContext &Context) const {
1075 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1076 // place naturally.
1077 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1078 // Treat an enum type as its underlying type.
1079 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1080 Ty = EnumTy->getDecl()->getIntegerType();
1081
1082 return (Ty->isPromotableIntegerType() ?
1083 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1084 }
1085
1086 return ABIArgInfo::getIndirect(0);
1087}
1088
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001089ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1090 ASTContext &Context) const {
1091 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1092 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001093 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1094 // Treat an enum type as its underlying type.
1095 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1096 Ty = EnumTy->getDecl()->getIntegerType();
1097
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001098 return (Ty->isPromotableIntegerType() ?
1099 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001100 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001101
Daniel Dunbar53fac692010-04-21 19:49:55 +00001102 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1103 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
Anders Carlsson20759ad2009-09-16 15:53:40 +00001104
Daniel Dunbar53fac692010-04-21 19:49:55 +00001105 // Compute the byval alignment. We trust the back-end to honor the
1106 // minimum ABI alignment for byval, to make cleaner IR.
1107 const unsigned MinABIAlign = 8;
1108 unsigned Align = Context.getTypeAlign(Ty) / 8;
1109 if (Align > MinABIAlign)
1110 return ABIArgInfo::getIndirect(Align);
1111 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001112}
1113
1114ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001115 ASTContext &Context,
1116 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001117 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1118 // classification algorithm.
1119 X86_64ABIInfo::Class Lo, Hi;
1120 classify(RetTy, Context, 0, Lo, Hi);
1121
1122 // Check some invariants.
1123 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1124 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1125 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1126
1127 const llvm::Type *ResType = 0;
1128 switch (Lo) {
1129 case NoClass:
1130 return ABIArgInfo::getIgnore();
1131
1132 case SSEUp:
1133 case X87Up:
1134 assert(0 && "Invalid classification for lo word.");
1135
1136 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1137 // hidden argument.
1138 case Memory:
Daniel Dunbar53fac692010-04-21 19:49:55 +00001139 return getIndirectReturnResult(RetTy, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001140
1141 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1142 // available register of the sequence %rax, %rdx is used.
1143 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +00001144 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001145
1146 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1147 // available SSE register of the sequence %xmm0, %xmm1 is used.
1148 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +00001149 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001150
1151 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1152 // returned on the X87 stack in %st0 as 80-bit x87 number.
1153 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +00001154 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001155
1156 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1157 // part of the value is returned in %st0 and the imaginary part in
1158 // %st1.
1159 case ComplexX87:
1160 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001161 ResType = llvm::StructType::get(VMContext,
1162 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson41a75022009-08-13 21:57:51 +00001163 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001164 NULL);
1165 break;
1166 }
1167
1168 switch (Hi) {
1169 // Memory was handled previously and X87 should
1170 // never occur as a hi class.
1171 case Memory:
1172 case X87:
1173 assert(0 && "Invalid classification for hi word.");
1174
1175 case ComplexX87: // Previously handled.
1176 case NoClass: break;
1177
1178 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001179 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001180 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001181 break;
1182 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001183 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001184 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001185 break;
1186
1187 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1188 // is passed in the upper half of the last used SSE register.
1189 //
1190 // SSEUP should always be preceeded by SSE, just widen.
1191 case SSEUp:
1192 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001193 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001194 break;
1195
1196 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1197 // returned together with the previous X87 value in %st0.
1198 case X87Up:
1199 // If X87Up is preceeded by X87, we don't need to do
1200 // anything. However, in some cases with unions it may not be
1201 // preceeded by X87. In such situations we follow gcc and pass the
1202 // extra bits in an SSE reg.
1203 if (Lo != X87)
Owen Anderson758428f2009-08-05 23:18:46 +00001204 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001205 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001206 break;
1207 }
1208
1209 return getCoerceResult(RetTy, ResType, Context);
1210}
1211
1212ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001213 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001214 unsigned &neededInt,
1215 unsigned &neededSSE) const {
1216 X86_64ABIInfo::Class Lo, Hi;
1217 classify(Ty, Context, 0, Lo, Hi);
1218
1219 // Check some invariants.
1220 // FIXME: Enforce these by construction.
1221 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1222 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1223 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1224
1225 neededInt = 0;
1226 neededSSE = 0;
1227 const llvm::Type *ResType = 0;
1228 switch (Lo) {
1229 case NoClass:
1230 return ABIArgInfo::getIgnore();
1231
1232 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1233 // on the stack.
1234 case Memory:
1235
1236 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1237 // COMPLEX_X87, it is passed in memory.
1238 case X87:
1239 case ComplexX87:
1240 return getIndirectResult(Ty, Context);
1241
1242 case SSEUp:
1243 case X87Up:
1244 assert(0 && "Invalid classification for lo word.");
1245
1246 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1247 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1248 // and %r9 is used.
1249 case Integer:
1250 ++neededInt;
Owen Anderson41a75022009-08-13 21:57:51 +00001251 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001252 break;
1253
1254 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1255 // available SSE register is used, the registers are taken in the
1256 // order from %xmm0 to %xmm7.
1257 case SSE:
1258 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001259 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001260 break;
1261 }
1262
1263 switch (Hi) {
1264 // Memory was handled previously, ComplexX87 and X87 should
1265 // never occur as hi classes, and X87Up must be preceed by X87,
1266 // which is passed in memory.
1267 case Memory:
1268 case X87:
1269 case ComplexX87:
1270 assert(0 && "Invalid classification for hi word.");
1271 break;
1272
1273 case NoClass: break;
1274 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001275 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001276 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001277 ++neededInt;
1278 break;
1279
1280 // X87Up generally doesn't occur here (long double is passed in
1281 // memory), except in situations involving unions.
1282 case X87Up:
1283 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001284 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001285 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001286 ++neededSSE;
1287 break;
1288
1289 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1290 // eightbyte is passed in the upper half of the last used SSE
1291 // register.
1292 case SSEUp:
1293 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001294 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001295 break;
1296 }
1297
1298 return getCoerceResult(Ty, ResType, Context);
1299}
1300
Owen Anderson170229f2009-07-14 23:10:40 +00001301void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1302 llvm::LLVMContext &VMContext) const {
1303 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1304 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001305
1306 // Keep track of the number of assigned registers.
1307 unsigned freeIntRegs = 6, freeSSERegs = 8;
1308
1309 // If the return value is indirect, then the hidden argument is consuming one
1310 // integer register.
1311 if (FI.getReturnInfo().isIndirect())
1312 --freeIntRegs;
1313
1314 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1315 // get assigned (in left-to-right order) for passing as follows...
1316 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1317 it != ie; ++it) {
1318 unsigned neededInt, neededSSE;
Mike Stump11289f42009-09-09 15:08:12 +00001319 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Anderson170229f2009-07-14 23:10:40 +00001320 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001321
1322 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1323 // eightbyte of an argument, the whole argument is passed on the
1324 // stack. If registers have already been assigned for some
1325 // eightbytes of such an argument, the assignments get reverted.
1326 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1327 freeIntRegs -= neededInt;
1328 freeSSERegs -= neededSSE;
1329 } else {
1330 it->info = getIndirectResult(it->type, Context);
1331 }
1332 }
1333}
1334
1335static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1336 QualType Ty,
1337 CodeGenFunction &CGF) {
1338 llvm::Value *overflow_arg_area_p =
1339 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1340 llvm::Value *overflow_arg_area =
1341 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1342
1343 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1344 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1345 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1346 if (Align > 8) {
1347 // Note that we follow the ABI & gcc here, even though the type
1348 // could in theory have an alignment greater than 16. This case
1349 // shouldn't ever matter in practice.
1350
1351 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001352 llvm::Value *Offset =
1353 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001354 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1355 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson41a75022009-08-13 21:57:51 +00001356 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1357 llvm::Value *Mask = llvm::ConstantInt::get(
1358 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001359 overflow_arg_area =
1360 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1361 overflow_arg_area->getType(),
1362 "overflow_arg_area.align");
1363 }
1364
1365 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1366 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1367 llvm::Value *Res =
1368 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001369 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001370
1371 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1372 // l->overflow_arg_area + sizeof(type).
1373 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1374 // an 8 byte boundary.
1375
1376 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001377 llvm::Value *Offset =
1378 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001379 (SizeInBytes + 7) & ~7);
1380 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1381 "overflow_arg_area.next");
1382 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1383
1384 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1385 return Res;
1386}
1387
1388llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1389 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001390 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbard59655c2009-09-12 00:59:49 +00001391 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1392 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump11289f42009-09-09 15:08:12 +00001393
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001394 // Assume that va_list type is correct; should be pointer to LLVM type:
1395 // struct {
1396 // i32 gp_offset;
1397 // i32 fp_offset;
1398 // i8* overflow_arg_area;
1399 // i8* reg_save_area;
1400 // };
1401 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001402
1403 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Anderson170229f2009-07-14 23:10:40 +00001404 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001405 neededInt, neededSSE);
1406
1407 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1408 // in the registers. If not go to step 7.
1409 if (!neededInt && !neededSSE)
1410 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1411
1412 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1413 // general purpose registers needed to pass type and num_fp to hold
1414 // the number of floating point registers needed.
1415
1416 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1417 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1418 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1419 //
1420 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1421 // register save space).
1422
1423 llvm::Value *InRegs = 0;
1424 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1425 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1426 if (neededInt) {
1427 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1428 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1429 InRegs =
1430 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001431 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001432 48 - neededInt * 8),
1433 "fits_in_gp");
1434 }
1435
1436 if (neededSSE) {
1437 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1438 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1439 llvm::Value *FitsInFP =
1440 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001441 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001442 176 - neededSSE * 16),
1443 "fits_in_fp");
1444 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1445 }
1446
1447 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1448 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1449 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1450 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1451
1452 // Emit code to load the value if it was passed in registers.
1453
1454 CGF.EmitBlock(InRegBlock);
1455
1456 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1457 // an offset of l->gp_offset and/or l->fp_offset. This may require
1458 // copying to a temporary location in case the parameter is passed
1459 // in different register classes or requires an alignment greater
1460 // than 8 for general purpose registers and 16 for XMM registers.
1461 //
1462 // FIXME: This really results in shameful code when we end up needing to
1463 // collect arguments from different places; often what should result in a
1464 // simple assembling of a structure from scattered addresses has many more
1465 // loads than necessary. Can we clean this up?
1466 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1467 llvm::Value *RegAddr =
1468 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1469 "reg_save_area");
1470 if (neededInt && neededSSE) {
1471 // FIXME: Cleanup.
1472 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1473 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1474 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1475 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1476 const llvm::Type *TyLo = ST->getElementType(0);
1477 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001478 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001479 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001480 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1481 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001482 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1483 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001484 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1485 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001486 llvm::Value *V =
1487 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1488 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1489 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1490 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1491
Owen Anderson170229f2009-07-14 23:10:40 +00001492 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001493 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001494 } else if (neededInt) {
1495 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1496 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001497 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001498 } else {
1499 if (neededSSE == 1) {
1500 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1501 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001502 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001503 } else {
1504 assert(neededSSE == 2 && "Invalid number of needed registers!");
1505 // SSE registers are spaced 16 bytes apart in the register save
1506 // area, we need to collect the two eightbytes together.
1507 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1508 llvm::Value *RegAddrHi =
1509 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001510 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001511 const llvm::Type *DblPtrTy =
Daniel Dunbard59655c2009-09-12 00:59:49 +00001512 llvm::PointerType::getUnqual(DoubleTy);
1513 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1514 DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001515 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1516 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1517 DblPtrTy));
1518 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1519 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1520 DblPtrTy));
1521 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1522 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001523 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001524 }
1525 }
1526
1527 // AMD64-ABI 3.5.7p5: Step 5. Set:
1528 // l->gp_offset = l->gp_offset + num_gp * 8
1529 // l->fp_offset = l->fp_offset + num_fp * 16.
1530 if (neededInt) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001531 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001532 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1533 gp_offset_p);
1534 }
1535 if (neededSSE) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001536 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001537 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1538 fp_offset_p);
1539 }
1540 CGF.EmitBranch(ContBlock);
1541
1542 // Emit code to load the value if it was passed in memory.
1543
1544 CGF.EmitBlock(InMemBlock);
1545 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1546
1547 // Return the appropriate result.
1548
1549 CGF.EmitBlock(ContBlock);
1550 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1551 "vaarg.addr");
1552 ResAddr->reserveOperandSpace(2);
1553 ResAddr->addIncoming(RegAddr, InRegBlock);
1554 ResAddr->addIncoming(MemAddr, InMemBlock);
1555
1556 return ResAddr;
1557}
1558
Daniel Dunbard59655c2009-09-12 00:59:49 +00001559// PIC16 ABI Implementation
1560
1561namespace {
1562
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001563class PIC16ABIInfo : public ABIInfo {
1564 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001565 ASTContext &Context,
1566 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001567
1568 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001569 ASTContext &Context,
1570 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001571
Owen Anderson170229f2009-07-14 23:10:40 +00001572 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1573 llvm::LLVMContext &VMContext) const {
1574 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1575 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001576 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1577 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001578 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001579 }
1580
1581 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1582 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001583};
1584
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001585class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1586public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001587 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001588};
1589
Daniel Dunbard59655c2009-09-12 00:59:49 +00001590}
1591
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001592ABIArgInfo PIC16ABIInfo::classifyReturnType(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 if (RetTy->isVoidType()) {
1596 return ABIArgInfo::getIgnore();
1597 } else {
1598 return ABIArgInfo::getDirect();
1599 }
1600}
1601
1602ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001603 ASTContext &Context,
1604 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001605 return ABIArgInfo::getDirect();
1606}
1607
1608llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1609 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001610 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001611 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1612
1613 CGBuilderTy &Builder = CGF.Builder;
1614 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1615 "ap");
1616 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1617 llvm::Type *PTy =
1618 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1619 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1620
1621 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1622
1623 llvm::Value *NextAddr =
1624 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1625 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1626 "ap.next");
1627 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1628
1629 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001630}
1631
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001632
John McCallea8d8bb2010-03-11 00:10:12 +00001633// PowerPC-32
1634
1635namespace {
1636class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1637public:
1638 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1639 // This is recovered from gcc output.
1640 return 1; // r1 is the dedicated stack pointer
1641 }
1642
1643 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1644 llvm::Value *Address) const;
1645};
1646
1647}
1648
1649bool
1650PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1651 llvm::Value *Address) const {
1652 // This is calculated from the LLVM and GCC tables and verified
1653 // against gcc output. AFAIK all ABIs use the same encoding.
1654
1655 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1656 llvm::LLVMContext &Context = CGF.getLLVMContext();
1657
1658 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1659 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1660 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1661 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1662
1663 // 0-31: r0-31, the 4-byte general-purpose registers
1664 for (unsigned I = 0, E = 32; I != E; ++I) {
1665 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1666 Builder.CreateStore(Four8, Slot);
1667 }
1668
1669 // 32-63: fp0-31, the 8-byte floating-point registers
1670 for (unsigned I = 32, E = 64; I != E; ++I) {
1671 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1672 Builder.CreateStore(Eight8, Slot);
1673 }
1674
1675 // 64-76 are various 4-byte special-purpose registers:
1676 // 64: mq
1677 // 65: lr
1678 // 66: ctr
1679 // 67: ap
1680 // 68-75 cr0-7
1681 // 76: xer
1682 for (unsigned I = 64, E = 77; I != E; ++I) {
1683 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1684 Builder.CreateStore(Four8, Slot);
1685 }
1686
1687 // 77-108: v0-31, the 16-byte vector registers
1688 for (unsigned I = 77, E = 109; I != E; ++I) {
1689 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1690 Builder.CreateStore(Sixteen8, Slot);
1691 }
1692
1693 // 109: vrsave
1694 // 110: vscr
1695 // 111: spe_acc
1696 // 112: spefscr
1697 // 113: sfp
1698 for (unsigned I = 109, E = 114; I != E; ++I) {
1699 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1700 Builder.CreateStore(Four8, Slot);
1701 }
1702
1703 return false;
1704}
1705
1706
Daniel Dunbard59655c2009-09-12 00:59:49 +00001707// ARM ABI Implementation
1708
1709namespace {
1710
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001711class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001712public:
1713 enum ABIKind {
1714 APCS = 0,
1715 AAPCS = 1,
1716 AAPCS_VFP
1717 };
1718
1719private:
1720 ABIKind Kind;
1721
1722public:
1723 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1724
1725private:
1726 ABIKind getABIKind() const { return Kind; }
1727
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001728 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001729 ASTContext &Context,
1730 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001731
1732 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001733 ASTContext &Context,
1734 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001735
Owen Anderson170229f2009-07-14 23:10:40 +00001736 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1737 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001738
1739 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1740 CodeGenFunction &CGF) const;
1741};
1742
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001743class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1744public:
1745 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor0599df12010-01-22 15:41:14 +00001746 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001747
1748 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1749 return 13;
1750 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001751};
1752
Daniel Dunbard59655c2009-09-12 00:59:49 +00001753}
1754
Owen Anderson170229f2009-07-14 23:10:40 +00001755void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1756 llvm::LLVMContext &VMContext) const {
Mike Stump11289f42009-09-09 15:08:12 +00001757 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001758 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001759 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1760 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001761 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001762 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001763
1764 // ARM always overrides the calling convention.
1765 switch (getABIKind()) {
1766 case APCS:
1767 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1768 break;
1769
1770 case AAPCS:
1771 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1772 break;
1773
1774 case AAPCS_VFP:
1775 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1776 break;
1777 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001778}
1779
1780ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001781 ASTContext &Context,
1782 llvm::LLVMContext &VMContext) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001783 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1784 // Treat an enum type as its underlying type.
1785 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1786 Ty = EnumTy->getDecl()->getIntegerType();
1787
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001788 return (Ty->isPromotableIntegerType() ?
1789 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001790 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001791
Daniel Dunbar09d33622009-09-14 21:54:03 +00001792 // Ignore empty records.
1793 if (isEmptyRecord(Context, Ty, true))
1794 return ABIArgInfo::getIgnore();
1795
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001796 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1797 // backend doesn't support byval.
1798 // FIXME: This doesn't handle alignment > 64 bits.
1799 const llvm::Type* ElemTy;
1800 unsigned SizeRegs;
1801 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001802 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001803 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1804 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001805 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001806 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1807 }
1808 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001809 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001810 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001811 return ABIArgInfo::getCoerce(STy);
1812}
1813
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001814static bool isIntegerLikeType(QualType Ty,
1815 ASTContext &Context,
1816 llvm::LLVMContext &VMContext) {
1817 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1818 // is called integer-like if its size is less than or equal to one word, and
1819 // the offset of each of its addressable sub-fields is zero.
1820
1821 uint64_t Size = Context.getTypeSize(Ty);
1822
1823 // Check that the type fits in a word.
1824 if (Size > 32)
1825 return false;
1826
1827 // FIXME: Handle vector types!
1828 if (Ty->isVectorType())
1829 return false;
1830
Daniel Dunbard53bac72009-09-14 02:20:34 +00001831 // Float types are never treated as "integer like".
1832 if (Ty->isRealFloatingType())
1833 return false;
1834
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001835 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001836 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001837 return true;
1838
Daniel Dunbar96ebba52010-02-01 23:31:26 +00001839 // Small complex integer types are "integer like".
1840 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1841 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001842
1843 // Single element and zero sized arrays should be allowed, by the definition
1844 // above, but they are not.
1845
1846 // Otherwise, it must be a record type.
1847 const RecordType *RT = Ty->getAs<RecordType>();
1848 if (!RT) return false;
1849
1850 // Ignore records with flexible arrays.
1851 const RecordDecl *RD = RT->getDecl();
1852 if (RD->hasFlexibleArrayMember())
1853 return false;
1854
1855 // Check that all sub-fields are at offset 0, and are themselves "integer
1856 // like".
1857 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1858
1859 bool HadField = false;
1860 unsigned idx = 0;
1861 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1862 i != e; ++i, ++idx) {
1863 const FieldDecl *FD = *i;
1864
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001865 // Bit-fields are not addressable, we only need to verify they are "integer
1866 // like". We still have to disallow a subsequent non-bitfield, for example:
1867 // struct { int : 0; int x }
1868 // is non-integer like according to gcc.
1869 if (FD->isBitField()) {
1870 if (!RD->isUnion())
1871 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001872
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001873 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1874 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001875
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001876 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001877 }
1878
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001879 // Check if this field is at offset 0.
1880 if (Layout.getFieldOffset(idx) != 0)
1881 return false;
1882
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001883 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1884 return false;
1885
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001886 // Only allow at most one field in a structure. This doesn't match the
1887 // wording above, but follows gcc in situations with a field following an
1888 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001889 if (!RD->isUnion()) {
1890 if (HadField)
1891 return false;
1892
1893 HadField = true;
1894 }
1895 }
1896
1897 return true;
1898}
1899
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001900ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001901 ASTContext &Context,
1902 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001903 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001904 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001905
Douglas Gregora71cc152010-02-02 20:10:50 +00001906 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1907 // Treat an enum type as its underlying type.
1908 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1909 RetTy = EnumTy->getDecl()->getIntegerType();
1910
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001911 return (RetTy->isPromotableIntegerType() ?
1912 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001913 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001914
1915 // Are we following APCS?
1916 if (getABIKind() == APCS) {
1917 if (isEmptyRecord(Context, RetTy, false))
1918 return ABIArgInfo::getIgnore();
1919
Daniel Dunbareedf1512010-02-01 23:31:19 +00001920 // Complex types are all returned as packed integers.
1921 //
1922 // FIXME: Consider using 2 x vector types if the back end handles them
1923 // correctly.
1924 if (RetTy->isAnyComplexType())
1925 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1926 VMContext, Context.getTypeSize(RetTy)));
1927
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001928 // Integer like structures are returned in r0.
1929 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1930 // Return in the smallest viable integer type.
1931 uint64_t Size = Context.getTypeSize(RetTy);
1932 if (Size <= 8)
1933 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1934 if (Size <= 16)
1935 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1936 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1937 }
1938
1939 // Otherwise return in memory.
1940 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001941 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001942
1943 // Otherwise this is an AAPCS variant.
1944
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001945 if (isEmptyRecord(Context, RetTy, true))
1946 return ABIArgInfo::getIgnore();
1947
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001948 // Aggregates <= 4 bytes are returned in r0; other aggregates
1949 // are returned indirectly.
1950 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001951 if (Size <= 32) {
1952 // Return in the smallest viable integer type.
1953 if (Size <= 8)
1954 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1955 if (Size <= 16)
1956 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001957 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001958 }
1959
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001960 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001961}
1962
1963llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1964 CodeGenFunction &CGF) const {
1965 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001966 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00001967 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001968
1969 CGBuilderTy &Builder = CGF.Builder;
1970 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1971 "ap");
1972 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1973 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001974 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001975 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1976
1977 uint64_t Offset =
1978 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1979 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +00001980 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1981 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001982 "ap.next");
1983 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1984
1985 return AddrTyped;
1986}
1987
1988ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001989 ASTContext &Context,
1990 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001991 if (RetTy->isVoidType()) {
1992 return ABIArgInfo::getIgnore();
1993 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1994 return ABIArgInfo::getIndirect(0);
1995 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +00001996 // Treat an enum type as its underlying type.
1997 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1998 RetTy = EnumTy->getDecl()->getIntegerType();
1999
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00002000 return (RetTy->isPromotableIntegerType() ?
2001 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002002 }
2003}
2004
Daniel Dunbard59655c2009-09-12 00:59:49 +00002005// SystemZ ABI Implementation
2006
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002007namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00002008
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002009class SystemZABIInfo : public ABIInfo {
2010 bool isPromotableIntegerType(QualType Ty) const;
2011
2012 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
2013 llvm::LLVMContext &VMContext) const;
2014
2015 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
2016 llvm::LLVMContext &VMContext) const;
2017
2018 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
2019 llvm::LLVMContext &VMContext) const {
2020 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2021 Context, VMContext);
2022 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2023 it != ie; ++it)
2024 it->info = classifyArgumentType(it->type, Context, VMContext);
2025 }
2026
2027 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2028 CodeGenFunction &CGF) const;
2029};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002030
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002031class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2032public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002033 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002034};
2035
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002036}
2037
2038bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2039 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002040 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002041 switch (BT->getKind()) {
2042 case BuiltinType::Bool:
2043 case BuiltinType::Char_S:
2044 case BuiltinType::Char_U:
2045 case BuiltinType::SChar:
2046 case BuiltinType::UChar:
2047 case BuiltinType::Short:
2048 case BuiltinType::UShort:
2049 case BuiltinType::Int:
2050 case BuiltinType::UInt:
2051 return true;
2052 default:
2053 return false;
2054 }
2055 return false;
2056}
2057
2058llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2059 CodeGenFunction &CGF) const {
2060 // FIXME: Implement
2061 return 0;
2062}
2063
2064
2065ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2066 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002067 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002068 if (RetTy->isVoidType()) {
2069 return ABIArgInfo::getIgnore();
2070 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2071 return ABIArgInfo::getIndirect(0);
2072 } else {
2073 return (isPromotableIntegerType(RetTy) ?
2074 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2075 }
2076}
2077
2078ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2079 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002080 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002081 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2082 return ABIArgInfo::getIndirect(0);
2083 } else {
2084 return (isPromotableIntegerType(Ty) ?
2085 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2086 }
2087}
2088
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002089// MSP430 ABI Implementation
2090
2091namespace {
2092
2093class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2094public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002095 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002096 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2097 CodeGen::CodeGenModule &M) const;
2098};
2099
2100}
2101
2102void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2103 llvm::GlobalValue *GV,
2104 CodeGen::CodeGenModule &M) const {
2105 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2106 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2107 // Handle 'interrupt' attribute:
2108 llvm::Function *F = cast<llvm::Function>(GV);
2109
2110 // Step 1: Set ISR calling convention.
2111 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2112
2113 // Step 2: Add attributes goodness.
2114 F->addFnAttr(llvm::Attribute::NoInline);
2115
2116 // Step 3: Emit ISR vector alias.
2117 unsigned Num = attr->getNumber() + 0xffe0;
2118 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2119 "vector_" +
2120 llvm::LowercaseString(llvm::utohexstr(Num)),
2121 GV, &M.getModule());
2122 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002123 }
2124}
2125
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002126const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2127 if (TheTargetCodeGenInfo)
2128 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002129
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002130 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2131 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002132
Daniel Dunbar40165182009-08-24 09:10:05 +00002133 const llvm::Triple &Triple(getContext().Target.getTriple());
2134 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002135 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002136 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbare3532f82009-08-24 08:52:16 +00002137
Daniel Dunbard59655c2009-09-12 00:59:49 +00002138 case llvm::Triple::arm:
2139 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002140 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002141 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002142 return *(TheTargetCodeGenInfo =
2143 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002144
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002145 return *(TheTargetCodeGenInfo =
2146 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002147
2148 case llvm::Triple::pic16:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002149 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002150
John McCallea8d8bb2010-03-11 00:10:12 +00002151 case llvm::Triple::ppc:
2152 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2153
Daniel Dunbard59655c2009-09-12 00:59:49 +00002154 case llvm::Triple::systemz:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002155 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2156
2157 case llvm::Triple::msp430:
2158 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002159
Daniel Dunbar40165182009-08-24 09:10:05 +00002160 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002161 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002162 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002163 return *(TheTargetCodeGenInfo =
2164 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002165 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002166 case llvm::Triple::MinGW32:
2167 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002168 case llvm::Triple::AuroraUX:
2169 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002170 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002171 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002172 return *(TheTargetCodeGenInfo =
2173 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002174
2175 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002176 return *(TheTargetCodeGenInfo =
2177 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002178 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002179
Daniel Dunbare3532f82009-08-24 08:52:16 +00002180 case llvm::Triple::x86_64:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002181 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00002182 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002183}