blob: c6cc31b7b0a19b888a549576b514fb95fdb6ff74 [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
Eli Friedman3192cc82009-06-13 21:37:10 +0000295 static unsigned getIndirectArgumentAlignment(QualType Ty,
296 ASTContext &Context);
297
Daniel Dunbar557893d2010-04-21 19:10:51 +0000298 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
299 /// such that the argument will be passed in memory.
300 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context,
301 bool ByVal = true) const;
302
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000303public:
304 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000305 ASTContext &Context,
306 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000307
308 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000309 ASTContext &Context,
310 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000311
Owen Anderson170229f2009-07-14 23:10:40 +0000312 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
313 llvm::LLVMContext &VMContext) const {
314 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
315 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000316 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
317 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +0000318 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000319 }
320
321 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
322 CodeGenFunction &CGF) const;
323
David Chisnallde3a0692009-08-17 23:08:21 +0000324 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump11289f42009-09-09 15:08:12 +0000325 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnallde3a0692009-08-17 23:08:21 +0000326 IsSmallStructInRegABI(p) {}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000327};
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000328
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000329class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
330public:
331 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor0599df12010-01-22 15:41:14 +0000332 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis4ea31ab2010-02-13 15:54:06 +0000333
334 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
335 CodeGen::CodeGenModule &CGM) const;
John McCallbeec5a02010-03-06 00:35:14 +0000336
337 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
338 // Darwin uses different dwarf register numbers for EH.
339 if (CGM.isTargetDarwin()) return 5;
340
341 return 4;
342 }
343
344 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
345 llvm::Value *Address) const;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000346};
347
348}
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000349
350/// shouldReturnTypeInRegister - Determine if the given type should be
351/// passed in a register (for the Darwin ABI).
352bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
353 ASTContext &Context) {
354 uint64_t Size = Context.getTypeSize(Ty);
355
356 // Type must be register sized.
357 if (!isRegisterSize(Size))
358 return false;
359
360 if (Ty->isVectorType()) {
361 // 64- and 128- bit vectors inside structures are not returned in
362 // registers.
363 if (Size == 64 || Size == 128)
364 return false;
365
366 return true;
367 }
368
Daniel Dunbarb3b1e532009-09-24 05:12:36 +0000369 // If this is a builtin, pointer, enum, or complex type, it is ok.
370 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
371 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
372 Ty->isBlockPointerType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000373 return true;
374
375 // Arrays are treated like records.
376 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
377 return shouldReturnTypeInRegister(AT->getElementType(), Context);
378
379 // Otherwise, it must be a record type.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000380 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000381 if (!RT) return false;
382
Anders Carlsson40446e82010-01-27 03:25:19 +0000383 // FIXME: Traverse bases here too.
384
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000385 // Structure types are passed in register if all fields would be
386 // passed in a register.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000387 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
388 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000389 const FieldDecl *FD = *i;
390
391 // Empty fields are ignored.
Daniel Dunbar626f1d82009-09-13 08:03:58 +0000392 if (isEmptyField(Context, FD, true))
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000393 continue;
394
395 // Check fields recursively.
396 if (!shouldReturnTypeInRegister(FD->getType(), Context))
397 return false;
398 }
399
400 return true;
401}
402
403ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000404 ASTContext &Context,
405 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000406 if (RetTy->isVoidType()) {
407 return ABIArgInfo::getIgnore();
John McCall9dd450b2009-09-21 23:43:11 +0000408 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000409 // On Darwin, some vectors are returned in registers.
David Chisnallde3a0692009-08-17 23:08:21 +0000410 if (IsDarwinVectorABI) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000411 uint64_t Size = Context.getTypeSize(RetTy);
412
413 // 128-bit vectors are a special case; they are returned in
414 // registers and we need to make sure to pick a type the LLVM
415 // backend will like.
416 if (Size == 128)
Owen Anderson41a75022009-08-13 21:57:51 +0000417 return ABIArgInfo::getCoerce(llvm::VectorType::get(
418 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000419
420 // Always return in register if it fits in a general purpose
421 // register, or if it is 64 bits and has a single element.
422 if ((Size == 8 || Size == 16 || Size == 32) ||
423 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson41a75022009-08-13 21:57:51 +0000424 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000425
426 return ABIArgInfo::getIndirect(0);
427 }
428
429 return ABIArgInfo::getDirect();
430 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlsson40446e82010-01-27 03:25:19 +0000431 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson5789c492009-10-20 22:07:59 +0000432 // Structures with either a non-trivial destructor or a non-trivial
433 // copy constructor are always indirect.
434 if (hasNonTrivialDestructorOrCopyConstructor(RT))
435 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
436
437 // Structures with flexible arrays are always indirect.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000438 if (RT->getDecl()->hasFlexibleArrayMember())
439 return ABIArgInfo::getIndirect(0);
Anders Carlsson5789c492009-10-20 22:07:59 +0000440 }
441
David Chisnallde3a0692009-08-17 23:08:21 +0000442 // If specified, structs and unions are always indirect.
443 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000444 return ABIArgInfo::getIndirect(0);
445
446 // Classify "single element" structs as their element type.
447 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall9dd450b2009-09-21 23:43:11 +0000448 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000449 if (BT->isIntegerType()) {
450 // We need to use the size of the structure, padding
451 // bit-fields can adjust that to be larger than the single
452 // element type.
453 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000454 return ABIArgInfo::getCoerce(
Owen Anderson41a75022009-08-13 21:57:51 +0000455 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000456 } else if (BT->getKind() == BuiltinType::Float) {
457 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
458 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000459 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000460 } else if (BT->getKind() == BuiltinType::Double) {
461 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
462 "Unexpect single element structure size!");
Owen Anderson41a75022009-08-13 21:57:51 +0000463 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000464 }
465 } else if (SeltTy->isPointerType()) {
466 // FIXME: It would be really nice if this could come out as the proper
467 // pointer type.
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000468 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000469 return ABIArgInfo::getCoerce(PtrTy);
470 } else if (SeltTy->isVectorType()) {
471 // 64- and 128-bit vectors are never returned in a
472 // register when inside a structure.
473 uint64_t Size = Context.getTypeSize(RetTy);
474 if (Size == 64 || Size == 128)
475 return ABIArgInfo::getIndirect(0);
476
Owen Anderson170229f2009-07-14 23:10:40 +0000477 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000478 }
479 }
480
481 // Small structures which are register sized are generally returned
482 // in a register.
483 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
484 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson41a75022009-08-13 21:57:51 +0000485 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000486 }
487
488 return ABIArgInfo::getIndirect(0);
489 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000490 // Treat an enum type as its underlying type.
491 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
492 RetTy = EnumTy->getDecl()->getIntegerType();
493
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000494 return (RetTy->isPromotableIntegerType() ?
495 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000496 }
497}
498
Eli Friedman3192cc82009-06-13 21:37:10 +0000499unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
500 ASTContext &Context) {
501 unsigned Align = Context.getTypeAlign(Ty);
502 if (Align < 128) return 0;
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000503 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedman3192cc82009-06-13 21:37:10 +0000504 if (typeContainsSSEVector(RT->getDecl(), Context))
505 return 16;
506 return 0;
507}
508
Daniel Dunbar557893d2010-04-21 19:10:51 +0000509ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty,
510 ASTContext &Context,
511 bool ByVal) const {
512 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context),
513 ByVal);
514}
515
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000516ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +0000517 ASTContext &Context,
518 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000519 // FIXME: Set alignment on indirect arguments.
520 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
521 // Structures with flexible arrays are always indirect.
Anders Carlsson40446e82010-01-27 03:25:19 +0000522 if (const RecordType *RT = Ty->getAs<RecordType>()) {
523 // Structures with either a non-trivial destructor or a non-trivial
524 // copy constructor are always indirect.
525 if (hasNonTrivialDestructorOrCopyConstructor(RT))
Daniel Dunbar557893d2010-04-21 19:10:51 +0000526 return getIndirectResult(Ty, Context, /*ByVal=*/false);
527
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000528 if (RT->getDecl()->hasFlexibleArrayMember())
Daniel Dunbar557893d2010-04-21 19:10:51 +0000529 return getIndirectResult(Ty, Context);
Anders Carlsson40446e82010-01-27 03:25:19 +0000530 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000531
532 // Ignore empty structs.
Eli Friedman3192cc82009-06-13 21:37:10 +0000533 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000534 return ABIArgInfo::getIgnore();
535
Daniel Dunbar11c08c82009-11-09 01:33:53 +0000536 // Expand small (<= 128-bit) record types when we know that the stack layout
537 // of those arguments will match the struct. This is important because the
538 // LLVM backend isn't smart enough to remove byval, which inhibits many
539 // optimizations.
540 if (Context.getTypeSize(Ty) <= 4*32 &&
541 canExpandIndirectArgument(Ty, Context))
542 return ABIArgInfo::getExpand();
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000543
Daniel Dunbar557893d2010-04-21 19:10:51 +0000544 return getIndirectResult(Ty, Context);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000545 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +0000546 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
547 Ty = EnumTy->getDecl()->getIntegerType();
548
Anton Korobeynikov18adbf52009-06-06 09:36:29 +0000549 return (Ty->isPromotableIntegerType() ?
550 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000551 }
552}
553
554llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
555 CodeGenFunction &CGF) const {
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000556 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +0000557 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000558
559 CGBuilderTy &Builder = CGF.Builder;
560 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
561 "ap");
562 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
563 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000564 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000565 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
566
567 uint64_t Offset =
568 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
569 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +0000570 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
571 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000572 "ap.next");
573 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
574
575 return AddrTyped;
576}
577
Charles Davis4ea31ab2010-02-13 15:54:06 +0000578void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
579 llvm::GlobalValue *GV,
580 CodeGen::CodeGenModule &CGM) const {
581 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
582 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
583 // Get the LLVM function.
584 llvm::Function *Fn = cast<llvm::Function>(GV);
585
586 // Now add the 'alignstack' attribute with a value of 16.
587 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
588 }
589 }
590}
591
John McCallbeec5a02010-03-06 00:35:14 +0000592bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
593 CodeGen::CodeGenFunction &CGF,
594 llvm::Value *Address) const {
595 CodeGen::CGBuilderTy &Builder = CGF.Builder;
596 llvm::LLVMContext &Context = CGF.getLLVMContext();
597
598 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
599 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
600
601 // 0-7 are the eight integer registers; the order is different
602 // on Darwin (for EH), but the range is the same.
603 // 8 is %eip.
604 for (unsigned I = 0, E = 9; I != E; ++I) {
605 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
606 Builder.CreateStore(Four8, Slot);
607 }
608
609 if (CGF.CGM.isTargetDarwin()) {
610 // 12-16 are st(0..4). Not sure why we stop at 4.
611 // These have size 16, which is sizeof(long double) on
612 // platforms with 8-byte alignment for that type.
613 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
614 for (unsigned I = 12, E = 17; I != E; ++I) {
615 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
616 Builder.CreateStore(Sixteen8, Slot);
617 }
618
619 } else {
620 // 9 is %eflags, which doesn't get a size on Darwin for some
621 // reason.
622 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
623
624 // 11-16 are st(0..5). Not sure why we stop at 5.
625 // These have size 12, which is sizeof(long double) on
626 // platforms with 4-byte alignment for that type.
627 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
628 for (unsigned I = 11, E = 17; I != E; ++I) {
629 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
630 Builder.CreateStore(Twelve8, Slot);
631 }
632 }
633
634 return false;
635}
636
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000637namespace {
638/// X86_64ABIInfo - The X86_64 ABI information.
639class X86_64ABIInfo : public ABIInfo {
640 enum Class {
641 Integer = 0,
642 SSE,
643 SSEUp,
644 X87,
645 X87Up,
646 ComplexX87,
647 NoClass,
648 Memory
649 };
650
651 /// merge - Implement the X86_64 ABI merging algorithm.
652 ///
653 /// Merge an accumulating classification \arg Accum with a field
654 /// classification \arg Field.
655 ///
656 /// \param Accum - The accumulating classification. This should
657 /// always be either NoClass or the result of a previous merge
658 /// call. In addition, this should never be Memory (the caller
659 /// should just return Memory for the aggregate).
660 Class merge(Class Accum, Class Field) const;
661
662 /// classify - Determine the x86_64 register classes in which the
663 /// given type T should be passed.
664 ///
665 /// \param Lo - The classification for the parts of the type
666 /// residing in the low word of the containing object.
667 ///
668 /// \param Hi - The classification for the parts of the type
669 /// residing in the high word of the containing object.
670 ///
671 /// \param OffsetBase - The bit offset of this type in the
672 /// containing object. Some parameters are classified different
673 /// depending on whether they straddle an eightbyte boundary.
674 ///
675 /// If a word is unused its result will be NoClass; if a type should
676 /// be passed in Memory then at least the classification of \arg Lo
677 /// will be Memory.
678 ///
679 /// The \arg Lo class will be NoClass iff the argument is ignored.
680 ///
681 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
682 /// also be ComplexX87.
683 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
684 Class &Lo, Class &Hi) const;
685
686 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
687 /// to coerce to, chose the best way to pass Ty in the same place
688 /// that \arg CoerceTo would be passed, but while keeping the
689 /// emitted code as simple as possible.
690 ///
691 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
692 /// the ways we might want to pass things, instead of constructing an LLVM
693 /// type. This makes this code more explicit, and it makes it clearer that we
694 /// are also doing this for correctness in the case of passing scalar types.
695 ABIArgInfo getCoerceResult(QualType Ty,
696 const llvm::Type *CoerceTo,
697 ASTContext &Context) const;
698
699 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
700 /// such that the argument will be passed in memory.
Daniel Dunbar557893d2010-04-21 19:10:51 +0000701 ABIArgInfo getIndirectResult(QualType Ty, ASTContext &Context) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000702
703 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +0000704 ASTContext &Context,
705 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000706
707 ABIArgInfo classifyArgumentType(QualType Ty,
708 ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +0000709 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000710 unsigned &neededInt,
711 unsigned &neededSSE) const;
712
713public:
Owen Anderson170229f2009-07-14 23:10:40 +0000714 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
715 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000716
717 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
718 CodeGenFunction &CGF) const;
719};
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000720
721class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
722public:
Douglas Gregor0599df12010-01-22 15:41:14 +0000723 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCallbeec5a02010-03-06 00:35:14 +0000724
725 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
726 return 7;
727 }
728
729 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
730 llvm::Value *Address) const {
731 CodeGen::CGBuilderTy &Builder = CGF.Builder;
732 llvm::LLVMContext &Context = CGF.getLLVMContext();
733
734 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
735 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
736
737 // 0-16 are the 16 integer registers.
738 // 17 is %rip.
739 for (unsigned I = 0, E = 17; I != E; ++I) {
740 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
741 Builder.CreateStore(Eight8, Slot);
742 }
743
744 return false;
745 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000746};
747
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000748}
749
750X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
751 Class Field) const {
752 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
753 // classified recursively so that always two fields are
754 // considered. The resulting class is calculated according to
755 // the classes of the fields in the eightbyte:
756 //
757 // (a) If both classes are equal, this is the resulting class.
758 //
759 // (b) If one of the classes is NO_CLASS, the resulting class is
760 // the other class.
761 //
762 // (c) If one of the classes is MEMORY, the result is the MEMORY
763 // class.
764 //
765 // (d) If one of the classes is INTEGER, the result is the
766 // INTEGER.
767 //
768 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
769 // MEMORY is used as class.
770 //
771 // (f) Otherwise class SSE is used.
772
773 // Accum should never be memory (we should have returned) or
774 // ComplexX87 (because this cannot be passed in a structure).
775 assert((Accum != Memory && Accum != ComplexX87) &&
776 "Invalid accumulated classification during merge.");
777 if (Accum == Field || Field == NoClass)
778 return Accum;
779 else if (Field == Memory)
780 return Memory;
781 else if (Accum == NoClass)
782 return Field;
783 else if (Accum == Integer || Field == Integer)
784 return Integer;
785 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
786 Accum == X87 || Accum == X87Up)
787 return Memory;
788 else
789 return SSE;
790}
791
792void X86_64ABIInfo::classify(QualType Ty,
793 ASTContext &Context,
794 uint64_t OffsetBase,
795 Class &Lo, Class &Hi) const {
796 // FIXME: This code can be simplified by introducing a simple value class for
797 // Class pairs with appropriate constructor methods for the various
798 // situations.
799
800 // FIXME: Some of the split computations are wrong; unaligned vectors
801 // shouldn't be passed in registers for example, so there is no chance they
802 // can straddle an eightbyte. Verify & simplify.
803
804 Lo = Hi = NoClass;
805
806 Class &Current = OffsetBase < 64 ? Lo : Hi;
807 Current = Memory;
808
John McCall9dd450b2009-09-21 23:43:11 +0000809 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000810 BuiltinType::Kind k = BT->getKind();
811
812 if (k == BuiltinType::Void) {
813 Current = NoClass;
814 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
815 Lo = Integer;
816 Hi = Integer;
817 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
818 Current = Integer;
819 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
820 Current = SSE;
821 } else if (k == BuiltinType::LongDouble) {
822 Lo = X87;
823 Hi = X87Up;
824 }
825 // FIXME: _Decimal32 and _Decimal64 are SSE.
826 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall9dd450b2009-09-21 23:43:11 +0000827 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000828 // Classify the underlying integer type.
829 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
830 } else if (Ty->hasPointerRepresentation()) {
831 Current = Integer;
John McCall9dd450b2009-09-21 23:43:11 +0000832 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000833 uint64_t Size = Context.getTypeSize(VT);
834 if (Size == 32) {
835 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
836 // float> as integer.
837 Current = Integer;
838
839 // If this type crosses an eightbyte boundary, it should be
840 // split.
841 uint64_t EB_Real = (OffsetBase) / 64;
842 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
843 if (EB_Real != EB_Imag)
844 Hi = Lo;
845 } else if (Size == 64) {
846 // gcc passes <1 x double> in memory. :(
847 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
848 return;
849
850 // gcc passes <1 x long long> as INTEGER.
851 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
852 Current = Integer;
853 else
854 Current = SSE;
855
856 // If this type crosses an eightbyte boundary, it should be
857 // split.
858 if (OffsetBase && OffsetBase != 64)
859 Hi = Lo;
860 } else if (Size == 128) {
861 Lo = SSE;
862 Hi = SSEUp;
863 }
John McCall9dd450b2009-09-21 23:43:11 +0000864 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000865 QualType ET = Context.getCanonicalType(CT->getElementType());
866
867 uint64_t Size = Context.getTypeSize(Ty);
868 if (ET->isIntegralType()) {
869 if (Size <= 64)
870 Current = Integer;
871 else if (Size <= 128)
872 Lo = Hi = Integer;
873 } else if (ET == Context.FloatTy)
874 Current = SSE;
875 else if (ET == Context.DoubleTy)
876 Lo = Hi = SSE;
877 else if (ET == Context.LongDoubleTy)
878 Current = ComplexX87;
879
880 // If this complex type crosses an eightbyte boundary then it
881 // should be split.
882 uint64_t EB_Real = (OffsetBase) / 64;
883 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
884 if (Hi == NoClass && EB_Real != EB_Imag)
885 Hi = Lo;
886 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
887 // Arrays are treated like structures.
888
889 uint64_t Size = Context.getTypeSize(Ty);
890
891 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
892 // than two eightbytes, ..., it has class MEMORY.
893 if (Size > 128)
894 return;
895
896 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
897 // fields, it has class MEMORY.
898 //
899 // Only need to check alignment of array base.
900 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
901 return;
902
903 // Otherwise implement simplified merge. We could be smarter about
904 // this, but it isn't worth it and would be harder to verify.
905 Current = NoClass;
906 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
907 uint64_t ArraySize = AT->getSize().getZExtValue();
908 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
909 Class FieldLo, FieldHi;
910 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
911 Lo = merge(Lo, FieldLo);
912 Hi = merge(Hi, FieldHi);
913 if (Lo == Memory || Hi == Memory)
914 break;
915 }
916
917 // Do post merger cleanup (see below). Only case we worry about is Memory.
918 if (Hi == Memory)
919 Lo = Memory;
920 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000921 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000922 uint64_t Size = Context.getTypeSize(Ty);
923
924 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
925 // than two eightbytes, ..., it has class MEMORY.
926 if (Size > 128)
927 return;
928
Anders Carlsson20759ad2009-09-16 15:53:40 +0000929 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
930 // copy constructor or a non-trivial destructor, it is passed by invisible
931 // reference.
932 if (hasNonTrivialDestructorOrCopyConstructor(RT))
933 return;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000934
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000935 const RecordDecl *RD = RT->getDecl();
936
937 // Assume variable sized types are passed in memory.
938 if (RD->hasFlexibleArrayMember())
939 return;
940
941 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
942
943 // Reset Lo class, this will be recomputed.
944 Current = NoClass;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000945
946 // If this is a C++ record, classify the bases first.
947 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
948 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
949 e = CXXRD->bases_end(); i != e; ++i) {
950 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
951 "Unexpected base class!");
952 const CXXRecordDecl *Base =
953 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
954
955 // Classify this field.
956 //
957 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
958 // single eightbyte, each is classified separately. Each eightbyte gets
959 // initialized to class NO_CLASS.
960 Class FieldLo, FieldHi;
961 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
962 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
963 Lo = merge(Lo, FieldLo);
964 Hi = merge(Hi, FieldHi);
965 if (Lo == Memory || Hi == Memory)
966 break;
967 }
Daniel Dunbar3780f0b2009-12-22 01:19:25 +0000968
969 // If this record has no fields but isn't empty, classify as INTEGER.
970 if (RD->field_empty() && Size)
971 Current = Integer;
Daniel Dunbare1cd0152009-11-22 23:01:23 +0000972 }
973
974 // Classify the fields one at a time, merging the results.
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000975 unsigned idx = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000976 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
977 i != e; ++i, ++idx) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +0000978 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
979 bool BitField = i->isBitField();
980
981 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
982 // fields, it has class MEMORY.
983 //
984 // Note, skip this test for bit-fields, see below.
985 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
986 Lo = Memory;
987 return;
988 }
989
990 // Classify this field.
991 //
992 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
993 // exceeds a single eightbyte, each is classified
994 // separately. Each eightbyte gets initialized to class
995 // NO_CLASS.
996 Class FieldLo, FieldHi;
997
998 // Bit-fields require special handling, they do not force the
999 // structure to be passed in memory even if unaligned, and
1000 // therefore they can straddle an eightbyte.
1001 if (BitField) {
1002 // Ignore padding bit-fields.
1003 if (i->isUnnamedBitfield())
1004 continue;
1005
1006 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1007 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1008
1009 uint64_t EB_Lo = Offset / 64;
1010 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1011 FieldLo = FieldHi = NoClass;
1012 if (EB_Lo) {
1013 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1014 FieldLo = NoClass;
1015 FieldHi = Integer;
1016 } else {
1017 FieldLo = Integer;
1018 FieldHi = EB_Hi ? Integer : NoClass;
1019 }
1020 } else
1021 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1022 Lo = merge(Lo, FieldLo);
1023 Hi = merge(Hi, FieldHi);
1024 if (Lo == Memory || Hi == Memory)
1025 break;
1026 }
1027
1028 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1029 //
1030 // (a) If one of the classes is MEMORY, the whole argument is
1031 // passed in memory.
1032 //
1033 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1034
1035 // The first of these conditions is guaranteed by how we implement
1036 // the merge (just bail).
1037 //
1038 // The second condition occurs in the case of unions; for example
1039 // union { _Complex double; unsigned; }.
1040 if (Hi == Memory)
1041 Lo = Memory;
1042 if (Hi == SSEUp && Lo != SSE)
1043 Hi = SSE;
1044 }
1045}
1046
1047ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1048 const llvm::Type *CoerceTo,
1049 ASTContext &Context) const {
Owen Anderson41a75022009-08-13 21:57:51 +00001050 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001051 // Integer and pointer types will end up in a general purpose
1052 // register.
Douglas Gregora71cc152010-02-02 20:10:50 +00001053
1054 // Treat an enum type as its underlying type.
1055 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1056 Ty = EnumTy->getDecl()->getIntegerType();
1057
Anders Carlsson03747422009-09-26 03:56:53 +00001058 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001059 return (Ty->isPromotableIntegerType() ?
1060 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson41a75022009-08-13 21:57:51 +00001061 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
John McCall8ee376f2010-02-24 07:14:12 +00001062 assert(Ty.isCanonical() && "should always have a canonical type here");
1063 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001064
1065 // Float and double end up in a single SSE reg.
John McCall8ee376f2010-02-24 07:14:12 +00001066 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001067 return ABIArgInfo::getDirect();
1068
1069 }
1070
1071 return ABIArgInfo::getCoerce(CoerceTo);
1072}
1073
1074ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1075 ASTContext &Context) const {
1076 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1077 // place naturally.
Douglas Gregora71cc152010-02-02 20:10:50 +00001078 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1079 // Treat an enum type as its underlying type.
1080 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1081 Ty = EnumTy->getDecl()->getIntegerType();
1082
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001083 return (Ty->isPromotableIntegerType() ?
1084 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001085 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001086
Anders Carlsson20759ad2009-09-16 15:53:40 +00001087 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
1088
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001089 // FIXME: Set alignment correctly.
Anders Carlsson20759ad2009-09-16 15:53:40 +00001090 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001091}
1092
1093ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001094 ASTContext &Context,
1095 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001096 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1097 // classification algorithm.
1098 X86_64ABIInfo::Class Lo, Hi;
1099 classify(RetTy, Context, 0, Lo, Hi);
1100
1101 // Check some invariants.
1102 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1103 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1104 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1105
1106 const llvm::Type *ResType = 0;
1107 switch (Lo) {
1108 case NoClass:
1109 return ABIArgInfo::getIgnore();
1110
1111 case SSEUp:
1112 case X87Up:
1113 assert(0 && "Invalid classification for lo word.");
1114
1115 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1116 // hidden argument.
1117 case Memory:
1118 return getIndirectResult(RetTy, Context);
1119
1120 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1121 // available register of the sequence %rax, %rdx is used.
1122 case Integer:
Owen Anderson41a75022009-08-13 21:57:51 +00001123 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001124
1125 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1126 // available SSE register of the sequence %xmm0, %xmm1 is used.
1127 case SSE:
Owen Anderson41a75022009-08-13 21:57:51 +00001128 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001129
1130 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1131 // returned on the X87 stack in %st0 as 80-bit x87 number.
1132 case X87:
Owen Anderson41a75022009-08-13 21:57:51 +00001133 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001134
1135 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1136 // part of the value is returned in %st0 and the imaginary part in
1137 // %st1.
1138 case ComplexX87:
1139 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001140 ResType = llvm::StructType::get(VMContext,
1141 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson41a75022009-08-13 21:57:51 +00001142 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001143 NULL);
1144 break;
1145 }
1146
1147 switch (Hi) {
1148 // Memory was handled previously and X87 should
1149 // never occur as a hi class.
1150 case Memory:
1151 case X87:
1152 assert(0 && "Invalid classification for hi word.");
1153
1154 case ComplexX87: // Previously handled.
1155 case NoClass: break;
1156
1157 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001158 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001159 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001160 break;
1161 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001162 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001163 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001164 break;
1165
1166 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1167 // is passed in the upper half of the last used SSE register.
1168 //
1169 // SSEUP should always be preceeded by SSE, just widen.
1170 case SSEUp:
1171 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001172 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001173 break;
1174
1175 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1176 // returned together with the previous X87 value in %st0.
1177 case X87Up:
1178 // If X87Up is preceeded by X87, we don't need to do
1179 // anything. However, in some cases with unions it may not be
1180 // preceeded by X87. In such situations we follow gcc and pass the
1181 // extra bits in an SSE reg.
1182 if (Lo != X87)
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
1188 return getCoerceResult(RetTy, ResType, Context);
1189}
1190
1191ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001192 llvm::LLVMContext &VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001193 unsigned &neededInt,
1194 unsigned &neededSSE) const {
1195 X86_64ABIInfo::Class Lo, Hi;
1196 classify(Ty, Context, 0, Lo, Hi);
1197
1198 // Check some invariants.
1199 // FIXME: Enforce these by construction.
1200 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1201 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1202 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1203
1204 neededInt = 0;
1205 neededSSE = 0;
1206 const llvm::Type *ResType = 0;
1207 switch (Lo) {
1208 case NoClass:
1209 return ABIArgInfo::getIgnore();
1210
1211 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1212 // on the stack.
1213 case Memory:
1214
1215 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1216 // COMPLEX_X87, it is passed in memory.
1217 case X87:
1218 case ComplexX87:
1219 return getIndirectResult(Ty, Context);
1220
1221 case SSEUp:
1222 case X87Up:
1223 assert(0 && "Invalid classification for lo word.");
1224
1225 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1226 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1227 // and %r9 is used.
1228 case Integer:
1229 ++neededInt;
Owen Anderson41a75022009-08-13 21:57:51 +00001230 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001231 break;
1232
1233 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1234 // available SSE register is used, the registers are taken in the
1235 // order from %xmm0 to %xmm7.
1236 case SSE:
1237 ++neededSSE;
Owen Anderson41a75022009-08-13 21:57:51 +00001238 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001239 break;
1240 }
1241
1242 switch (Hi) {
1243 // Memory was handled previously, ComplexX87 and X87 should
1244 // never occur as hi classes, and X87Up must be preceed by X87,
1245 // which is passed in memory.
1246 case Memory:
1247 case X87:
1248 case ComplexX87:
1249 assert(0 && "Invalid classification for hi word.");
1250 break;
1251
1252 case NoClass: break;
1253 case Integer:
Owen Anderson758428f2009-08-05 23:18:46 +00001254 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001255 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001256 ++neededInt;
1257 break;
1258
1259 // X87Up generally doesn't occur here (long double is passed in
1260 // memory), except in situations involving unions.
1261 case X87Up:
1262 case SSE:
Owen Anderson758428f2009-08-05 23:18:46 +00001263 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson41a75022009-08-13 21:57:51 +00001264 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001265 ++neededSSE;
1266 break;
1267
1268 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1269 // eightbyte is passed in the upper half of the last used SSE
1270 // register.
1271 case SSEUp:
1272 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson41a75022009-08-13 21:57:51 +00001273 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001274 break;
1275 }
1276
1277 return getCoerceResult(Ty, ResType, Context);
1278}
1279
Owen Anderson170229f2009-07-14 23:10:40 +00001280void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1281 llvm::LLVMContext &VMContext) const {
1282 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1283 Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001284
1285 // Keep track of the number of assigned registers.
1286 unsigned freeIntRegs = 6, freeSSERegs = 8;
1287
1288 // If the return value is indirect, then the hidden argument is consuming one
1289 // integer register.
1290 if (FI.getReturnInfo().isIndirect())
1291 --freeIntRegs;
1292
1293 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1294 // get assigned (in left-to-right order) for passing as follows...
1295 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1296 it != ie; ++it) {
1297 unsigned neededInt, neededSSE;
Mike Stump11289f42009-09-09 15:08:12 +00001298 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Anderson170229f2009-07-14 23:10:40 +00001299 neededInt, neededSSE);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001300
1301 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1302 // eightbyte of an argument, the whole argument is passed on the
1303 // stack. If registers have already been assigned for some
1304 // eightbytes of such an argument, the assignments get reverted.
1305 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1306 freeIntRegs -= neededInt;
1307 freeSSERegs -= neededSSE;
1308 } else {
1309 it->info = getIndirectResult(it->type, Context);
1310 }
1311 }
1312}
1313
1314static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1315 QualType Ty,
1316 CodeGenFunction &CGF) {
1317 llvm::Value *overflow_arg_area_p =
1318 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1319 llvm::Value *overflow_arg_area =
1320 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1321
1322 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1323 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1324 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1325 if (Align > 8) {
1326 // Note that we follow the ABI & gcc here, even though the type
1327 // could in theory have an alignment greater than 16. This case
1328 // shouldn't ever matter in practice.
1329
1330 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson41a75022009-08-13 21:57:51 +00001331 llvm::Value *Offset =
1332 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001333 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1334 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson41a75022009-08-13 21:57:51 +00001335 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1336 llvm::Value *Mask = llvm::ConstantInt::get(
1337 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001338 overflow_arg_area =
1339 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1340 overflow_arg_area->getType(),
1341 "overflow_arg_area.align");
1342 }
1343
1344 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1345 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1346 llvm::Value *Res =
1347 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001348 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001349
1350 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1351 // l->overflow_arg_area + sizeof(type).
1352 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1353 // an 8 byte boundary.
1354
1355 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson41a75022009-08-13 21:57:51 +00001356 llvm::Value *Offset =
1357 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001358 (SizeInBytes + 7) & ~7);
1359 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1360 "overflow_arg_area.next");
1361 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1362
1363 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1364 return Res;
1365}
1366
1367llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1368 CodeGenFunction &CGF) const {
Owen Anderson170229f2009-07-14 23:10:40 +00001369 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbard59655c2009-09-12 00:59:49 +00001370 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1371 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump11289f42009-09-09 15:08:12 +00001372
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001373 // Assume that va_list type is correct; should be pointer to LLVM type:
1374 // struct {
1375 // i32 gp_offset;
1376 // i32 fp_offset;
1377 // i8* overflow_arg_area;
1378 // i8* reg_save_area;
1379 // };
1380 unsigned neededInt, neededSSE;
Chris Lattner9723d6c2010-03-11 18:19:55 +00001381
1382 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Anderson170229f2009-07-14 23:10:40 +00001383 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001384 neededInt, neededSSE);
1385
1386 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1387 // in the registers. If not go to step 7.
1388 if (!neededInt && !neededSSE)
1389 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1390
1391 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1392 // general purpose registers needed to pass type and num_fp to hold
1393 // the number of floating point registers needed.
1394
1395 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1396 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1397 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1398 //
1399 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1400 // register save space).
1401
1402 llvm::Value *InRegs = 0;
1403 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1404 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1405 if (neededInt) {
1406 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1407 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1408 InRegs =
1409 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001410 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001411 48 - neededInt * 8),
1412 "fits_in_gp");
1413 }
1414
1415 if (neededSSE) {
1416 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1417 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1418 llvm::Value *FitsInFP =
1419 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001420 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001421 176 - neededSSE * 16),
1422 "fits_in_fp");
1423 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1424 }
1425
1426 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1427 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1428 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1429 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1430
1431 // Emit code to load the value if it was passed in registers.
1432
1433 CGF.EmitBlock(InRegBlock);
1434
1435 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1436 // an offset of l->gp_offset and/or l->fp_offset. This may require
1437 // copying to a temporary location in case the parameter is passed
1438 // in different register classes or requires an alignment greater
1439 // than 8 for general purpose registers and 16 for XMM registers.
1440 //
1441 // FIXME: This really results in shameful code when we end up needing to
1442 // collect arguments from different places; often what should result in a
1443 // simple assembling of a structure from scattered addresses has many more
1444 // loads than necessary. Can we clean this up?
1445 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1446 llvm::Value *RegAddr =
1447 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1448 "reg_save_area");
1449 if (neededInt && neededSSE) {
1450 // FIXME: Cleanup.
1451 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1452 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1453 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1454 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1455 const llvm::Type *TyLo = ST->getElementType(0);
1456 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sands998f9d92010-02-15 16:14:01 +00001457 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001458 "Unexpected ABI info for mixed regs");
Owen Anderson9793f0e2009-07-29 22:16:19 +00001459 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1460 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001461 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1462 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sands998f9d92010-02-15 16:14:01 +00001463 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1464 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001465 llvm::Value *V =
1466 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1467 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1468 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1469 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1470
Owen Anderson170229f2009-07-14 23:10:40 +00001471 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001472 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001473 } else if (neededInt) {
1474 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1475 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001476 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001477 } else {
1478 if (neededSSE == 1) {
1479 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1480 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001481 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001482 } else {
1483 assert(neededSSE == 2 && "Invalid number of needed registers!");
1484 // SSE registers are spaced 16 bytes apart in the register save
1485 // area, we need to collect the two eightbytes together.
1486 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1487 llvm::Value *RegAddrHi =
1488 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbard59655c2009-09-12 00:59:49 +00001489 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001490 const llvm::Type *DblPtrTy =
Daniel Dunbard59655c2009-09-12 00:59:49 +00001491 llvm::PointerType::getUnqual(DoubleTy);
1492 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1493 DoubleTy, NULL);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001494 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1495 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1496 DblPtrTy));
1497 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1498 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1499 DblPtrTy));
1500 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1501 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001502 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001503 }
1504 }
1505
1506 // AMD64-ABI 3.5.7p5: Step 5. Set:
1507 // l->gp_offset = l->gp_offset + num_gp * 8
1508 // l->fp_offset = l->fp_offset + num_fp * 16.
1509 if (neededInt) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001510 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001511 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1512 gp_offset_p);
1513 }
1514 if (neededSSE) {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001515 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001516 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1517 fp_offset_p);
1518 }
1519 CGF.EmitBranch(ContBlock);
1520
1521 // Emit code to load the value if it was passed in memory.
1522
1523 CGF.EmitBlock(InMemBlock);
1524 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1525
1526 // Return the appropriate result.
1527
1528 CGF.EmitBlock(ContBlock);
1529 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1530 "vaarg.addr");
1531 ResAddr->reserveOperandSpace(2);
1532 ResAddr->addIncoming(RegAddr, InRegBlock);
1533 ResAddr->addIncoming(MemAddr, InMemBlock);
1534
1535 return ResAddr;
1536}
1537
Daniel Dunbard59655c2009-09-12 00:59:49 +00001538// PIC16 ABI Implementation
1539
1540namespace {
1541
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001542class PIC16ABIInfo : public ABIInfo {
1543 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001544 ASTContext &Context,
1545 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001546
1547 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001548 ASTContext &Context,
1549 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001550
Owen Anderson170229f2009-07-14 23:10:40 +00001551 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1552 llvm::LLVMContext &VMContext) const {
1553 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1554 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001555 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1556 it != ie; ++it)
Owen Anderson170229f2009-07-14 23:10:40 +00001557 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001558 }
1559
1560 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1561 CodeGenFunction &CGF) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001562};
1563
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001564class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1565public:
Douglas Gregor0599df12010-01-22 15:41:14 +00001566 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001567};
1568
Daniel Dunbard59655c2009-09-12 00:59:49 +00001569}
1570
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001571ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001572 ASTContext &Context,
1573 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001574 if (RetTy->isVoidType()) {
1575 return ABIArgInfo::getIgnore();
1576 } else {
1577 return ABIArgInfo::getDirect();
1578 }
1579}
1580
1581ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001582 ASTContext &Context,
1583 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001584 return ABIArgInfo::getDirect();
1585}
1586
1587llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1588 CodeGenFunction &CGF) const {
Chris Lattnerc0e8a592010-04-06 17:29:22 +00001589 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001590 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1591
1592 CGBuilderTy &Builder = CGF.Builder;
1593 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1594 "ap");
1595 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1596 llvm::Type *PTy =
1597 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1598 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1599
1600 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1601
1602 llvm::Value *NextAddr =
1603 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1604 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1605 "ap.next");
1606 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1607
1608 return AddrTyped;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001609}
1610
Sanjiv Guptaba1e2672010-02-17 02:25:52 +00001611
John McCallea8d8bb2010-03-11 00:10:12 +00001612// PowerPC-32
1613
1614namespace {
1615class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1616public:
1617 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1618 // This is recovered from gcc output.
1619 return 1; // r1 is the dedicated stack pointer
1620 }
1621
1622 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1623 llvm::Value *Address) const;
1624};
1625
1626}
1627
1628bool
1629PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1630 llvm::Value *Address) const {
1631 // This is calculated from the LLVM and GCC tables and verified
1632 // against gcc output. AFAIK all ABIs use the same encoding.
1633
1634 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1635 llvm::LLVMContext &Context = CGF.getLLVMContext();
1636
1637 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1638 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1639 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1640 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1641
1642 // 0-31: r0-31, the 4-byte general-purpose registers
1643 for (unsigned I = 0, E = 32; I != E; ++I) {
1644 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1645 Builder.CreateStore(Four8, Slot);
1646 }
1647
1648 // 32-63: fp0-31, the 8-byte floating-point registers
1649 for (unsigned I = 32, E = 64; I != E; ++I) {
1650 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1651 Builder.CreateStore(Eight8, Slot);
1652 }
1653
1654 // 64-76 are various 4-byte special-purpose registers:
1655 // 64: mq
1656 // 65: lr
1657 // 66: ctr
1658 // 67: ap
1659 // 68-75 cr0-7
1660 // 76: xer
1661 for (unsigned I = 64, E = 77; I != E; ++I) {
1662 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1663 Builder.CreateStore(Four8, Slot);
1664 }
1665
1666 // 77-108: v0-31, the 16-byte vector registers
1667 for (unsigned I = 77, E = 109; I != E; ++I) {
1668 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1669 Builder.CreateStore(Sixteen8, Slot);
1670 }
1671
1672 // 109: vrsave
1673 // 110: vscr
1674 // 111: spe_acc
1675 // 112: spefscr
1676 // 113: sfp
1677 for (unsigned I = 109, E = 114; I != E; ++I) {
1678 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1679 Builder.CreateStore(Four8, Slot);
1680 }
1681
1682 return false;
1683}
1684
1685
Daniel Dunbard59655c2009-09-12 00:59:49 +00001686// ARM ABI Implementation
1687
1688namespace {
1689
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001690class ARMABIInfo : public ABIInfo {
Daniel Dunbar020daa92009-09-12 01:00:39 +00001691public:
1692 enum ABIKind {
1693 APCS = 0,
1694 AAPCS = 1,
1695 AAPCS_VFP
1696 };
1697
1698private:
1699 ABIKind Kind;
1700
1701public:
1702 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1703
1704private:
1705 ABIKind getABIKind() const { return Kind; }
1706
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001707 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001708 ASTContext &Context,
1709 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001710
1711 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001712 ASTContext &Context,
1713 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001714
Owen Anderson170229f2009-07-14 23:10:40 +00001715 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1716 llvm::LLVMContext &VMContext) const;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001717
1718 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1719 CodeGenFunction &CGF) const;
1720};
1721
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001722class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1723public:
1724 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor0599df12010-01-22 15:41:14 +00001725 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCallbeec5a02010-03-06 00:35:14 +00001726
1727 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1728 return 13;
1729 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001730};
1731
Daniel Dunbard59655c2009-09-12 00:59:49 +00001732}
1733
Owen Anderson170229f2009-07-14 23:10:40 +00001734void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1735 llvm::LLVMContext &VMContext) const {
Mike Stump11289f42009-09-09 15:08:12 +00001736 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Anderson170229f2009-07-14 23:10:40 +00001737 VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001738 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1739 it != ie; ++it) {
Owen Anderson170229f2009-07-14 23:10:40 +00001740 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001741 }
Daniel Dunbar020daa92009-09-12 01:00:39 +00001742
1743 // ARM always overrides the calling convention.
1744 switch (getABIKind()) {
1745 case APCS:
1746 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1747 break;
1748
1749 case AAPCS:
1750 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1751 break;
1752
1753 case AAPCS_VFP:
1754 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1755 break;
1756 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001757}
1758
1759ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Anderson170229f2009-07-14 23:10:40 +00001760 ASTContext &Context,
1761 llvm::LLVMContext &VMContext) const {
Douglas Gregora71cc152010-02-02 20:10:50 +00001762 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1763 // Treat an enum type as its underlying type.
1764 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1765 Ty = EnumTy->getDecl()->getIntegerType();
1766
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001767 return (Ty->isPromotableIntegerType() ?
1768 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001769 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001770
Daniel Dunbar09d33622009-09-14 21:54:03 +00001771 // Ignore empty records.
1772 if (isEmptyRecord(Context, Ty, true))
1773 return ABIArgInfo::getIgnore();
1774
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001775 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1776 // backend doesn't support byval.
1777 // FIXME: This doesn't handle alignment > 64 bits.
1778 const llvm::Type* ElemTy;
1779 unsigned SizeRegs;
1780 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson41a75022009-08-13 21:57:51 +00001781 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001782 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1783 } else {
Owen Anderson41a75022009-08-13 21:57:51 +00001784 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001785 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1786 }
1787 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson9793f0e2009-07-29 22:16:19 +00001788 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson758428f2009-08-05 23:18:46 +00001789 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001790 return ABIArgInfo::getCoerce(STy);
1791}
1792
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001793static bool isIntegerLikeType(QualType Ty,
1794 ASTContext &Context,
1795 llvm::LLVMContext &VMContext) {
1796 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1797 // is called integer-like if its size is less than or equal to one word, and
1798 // the offset of each of its addressable sub-fields is zero.
1799
1800 uint64_t Size = Context.getTypeSize(Ty);
1801
1802 // Check that the type fits in a word.
1803 if (Size > 32)
1804 return false;
1805
1806 // FIXME: Handle vector types!
1807 if (Ty->isVectorType())
1808 return false;
1809
Daniel Dunbard53bac72009-09-14 02:20:34 +00001810 // Float types are never treated as "integer like".
1811 if (Ty->isRealFloatingType())
1812 return false;
1813
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001814 // If this is a builtin or pointer type then it is ok.
John McCall9dd450b2009-09-21 23:43:11 +00001815 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001816 return true;
1817
Daniel Dunbar96ebba52010-02-01 23:31:26 +00001818 // Small complex integer types are "integer like".
1819 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1820 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001821
1822 // Single element and zero sized arrays should be allowed, by the definition
1823 // above, but they are not.
1824
1825 // Otherwise, it must be a record type.
1826 const RecordType *RT = Ty->getAs<RecordType>();
1827 if (!RT) return false;
1828
1829 // Ignore records with flexible arrays.
1830 const RecordDecl *RD = RT->getDecl();
1831 if (RD->hasFlexibleArrayMember())
1832 return false;
1833
1834 // Check that all sub-fields are at offset 0, and are themselves "integer
1835 // like".
1836 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1837
1838 bool HadField = false;
1839 unsigned idx = 0;
1840 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1841 i != e; ++i, ++idx) {
1842 const FieldDecl *FD = *i;
1843
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001844 // Bit-fields are not addressable, we only need to verify they are "integer
1845 // like". We still have to disallow a subsequent non-bitfield, for example:
1846 // struct { int : 0; int x }
1847 // is non-integer like according to gcc.
1848 if (FD->isBitField()) {
1849 if (!RD->isUnion())
1850 HadField = true;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001851
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001852 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1853 return false;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001854
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001855 continue;
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001856 }
1857
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001858 // Check if this field is at offset 0.
1859 if (Layout.getFieldOffset(idx) != 0)
1860 return false;
1861
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001862 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1863 return false;
1864
Daniel Dunbar45c7ff12010-01-29 03:22:29 +00001865 // Only allow at most one field in a structure. This doesn't match the
1866 // wording above, but follows gcc in situations with a field following an
1867 // empty structure.
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001868 if (!RD->isUnion()) {
1869 if (HadField)
1870 return false;
1871
1872 HadField = true;
1873 }
1874 }
1875
1876 return true;
1877}
1878
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001879ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001880 ASTContext &Context,
1881 llvm::LLVMContext &VMContext) const {
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001882 if (RetTy->isVoidType())
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001883 return ABIArgInfo::getIgnore();
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001884
Douglas Gregora71cc152010-02-02 20:10:50 +00001885 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1886 // Treat an enum type as its underlying type.
1887 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1888 RetTy = EnumTy->getDecl()->getIntegerType();
1889
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001890 return (RetTy->isPromotableIntegerType() ?
1891 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregora71cc152010-02-02 20:10:50 +00001892 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001893
1894 // Are we following APCS?
1895 if (getABIKind() == APCS) {
1896 if (isEmptyRecord(Context, RetTy, false))
1897 return ABIArgInfo::getIgnore();
1898
Daniel Dunbareedf1512010-02-01 23:31:19 +00001899 // Complex types are all returned as packed integers.
1900 //
1901 // FIXME: Consider using 2 x vector types if the back end handles them
1902 // correctly.
1903 if (RetTy->isAnyComplexType())
1904 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1905 VMContext, Context.getTypeSize(RetTy)));
1906
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001907 // Integer like structures are returned in r0.
1908 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1909 // Return in the smallest viable integer type.
1910 uint64_t Size = Context.getTypeSize(RetTy);
1911 if (Size <= 8)
1912 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1913 if (Size <= 16)
1914 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1915 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1916 }
1917
1918 // Otherwise return in memory.
1919 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001920 }
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001921
1922 // Otherwise this is an AAPCS variant.
1923
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001924 if (isEmptyRecord(Context, RetTy, true))
1925 return ABIArgInfo::getIgnore();
1926
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001927 // Aggregates <= 4 bytes are returned in r0; other aggregates
1928 // are returned indirectly.
1929 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001930 if (Size <= 32) {
1931 // Return in the smallest viable integer type.
1932 if (Size <= 8)
1933 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1934 if (Size <= 16)
1935 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001936 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar1ce72512009-09-14 00:56:55 +00001937 }
1938
Daniel Dunbar626f1d82009-09-13 08:03:58 +00001939 return ABIArgInfo::getIndirect(0);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001940}
1941
1942llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1943 CodeGenFunction &CGF) const {
1944 // FIXME: Need to handle alignment
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001945 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson9793f0e2009-07-29 22:16:19 +00001946 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001947
1948 CGBuilderTy &Builder = CGF.Builder;
1949 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1950 "ap");
1951 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1952 llvm::Type *PTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00001953 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001954 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1955
1956 uint64_t Offset =
1957 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1958 llvm::Value *NextAddr =
Owen Anderson41a75022009-08-13 21:57:51 +00001959 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1960 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001961 "ap.next");
1962 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1963
1964 return AddrTyped;
1965}
1966
1967ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Anderson170229f2009-07-14 23:10:40 +00001968 ASTContext &Context,
1969 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001970 if (RetTy->isVoidType()) {
1971 return ABIArgInfo::getIgnore();
1972 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1973 return ABIArgInfo::getIndirect(0);
1974 } else {
Douglas Gregora71cc152010-02-02 20:10:50 +00001975 // Treat an enum type as its underlying type.
1976 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1977 RetTy = EnumTy->getDecl()->getIntegerType();
1978
Anton Korobeynikov18adbf52009-06-06 09:36:29 +00001979 return (RetTy->isPromotableIntegerType() ?
1980 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov244360d2009-06-05 22:08:42 +00001981 }
1982}
1983
Daniel Dunbard59655c2009-09-12 00:59:49 +00001984// SystemZ ABI Implementation
1985
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001986namespace {
Daniel Dunbard59655c2009-09-12 00:59:49 +00001987
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00001988class SystemZABIInfo : public ABIInfo {
1989 bool isPromotableIntegerType(QualType Ty) const;
1990
1991 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1992 llvm::LLVMContext &VMContext) const;
1993
1994 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1995 llvm::LLVMContext &VMContext) const;
1996
1997 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1998 llvm::LLVMContext &VMContext) const {
1999 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
2000 Context, VMContext);
2001 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2002 it != ie; ++it)
2003 it->info = classifyArgumentType(it->type, Context, VMContext);
2004 }
2005
2006 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2007 CodeGenFunction &CGF) const;
2008};
Daniel Dunbard59655c2009-09-12 00:59:49 +00002009
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002010class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2011public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002012 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002013};
2014
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002015}
2016
2017bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2018 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall9dd450b2009-09-21 23:43:11 +00002019 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002020 switch (BT->getKind()) {
2021 case BuiltinType::Bool:
2022 case BuiltinType::Char_S:
2023 case BuiltinType::Char_U:
2024 case BuiltinType::SChar:
2025 case BuiltinType::UChar:
2026 case BuiltinType::Short:
2027 case BuiltinType::UShort:
2028 case BuiltinType::Int:
2029 case BuiltinType::UInt:
2030 return true;
2031 default:
2032 return false;
2033 }
2034 return false;
2035}
2036
2037llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2038 CodeGenFunction &CGF) const {
2039 // FIXME: Implement
2040 return 0;
2041}
2042
2043
2044ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2045 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002046 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002047 if (RetTy->isVoidType()) {
2048 return ABIArgInfo::getIgnore();
2049 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2050 return ABIArgInfo::getIndirect(0);
2051 } else {
2052 return (isPromotableIntegerType(RetTy) ?
2053 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2054 }
2055}
2056
2057ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2058 ASTContext &Context,
Daniel Dunbard59655c2009-09-12 00:59:49 +00002059 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovb5b703b2009-07-16 20:09:57 +00002060 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2061 return ABIArgInfo::getIndirect(0);
2062 } else {
2063 return (isPromotableIntegerType(Ty) ?
2064 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2065 }
2066}
2067
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002068// MSP430 ABI Implementation
2069
2070namespace {
2071
2072class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2073public:
Douglas Gregor0599df12010-01-22 15:41:14 +00002074 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002075 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2076 CodeGen::CodeGenModule &M) const;
2077};
2078
2079}
2080
2081void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2082 llvm::GlobalValue *GV,
2083 CodeGen::CodeGenModule &M) const {
2084 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2085 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2086 // Handle 'interrupt' attribute:
2087 llvm::Function *F = cast<llvm::Function>(GV);
2088
2089 // Step 1: Set ISR calling convention.
2090 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2091
2092 // Step 2: Add attributes goodness.
2093 F->addFnAttr(llvm::Attribute::NoInline);
2094
2095 // Step 3: Emit ISR vector alias.
2096 unsigned Num = attr->getNumber() + 0xffe0;
2097 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2098 "vector_" +
2099 llvm::LowercaseString(llvm::utohexstr(Num)),
2100 GV, &M.getModule());
2101 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002102 }
2103}
2104
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002105const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2106 if (TheTargetCodeGenInfo)
2107 return *TheTargetCodeGenInfo;
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002108
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002109 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2110 // free it.
Daniel Dunbare3532f82009-08-24 08:52:16 +00002111
Daniel Dunbar40165182009-08-24 09:10:05 +00002112 const llvm::Triple &Triple(getContext().Target.getTriple());
2113 switch (Triple.getArch()) {
Daniel Dunbare3532f82009-08-24 08:52:16 +00002114 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002115 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbare3532f82009-08-24 08:52:16 +00002116
Daniel Dunbard59655c2009-09-12 00:59:49 +00002117 case llvm::Triple::arm:
2118 case llvm::Triple::thumb:
Daniel Dunbar020daa92009-09-12 01:00:39 +00002119 // FIXME: We want to know the float calling convention as well.
Daniel Dunbarb4091a92009-09-14 00:35:03 +00002120 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002121 return *(TheTargetCodeGenInfo =
2122 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar020daa92009-09-12 01:00:39 +00002123
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002124 return *(TheTargetCodeGenInfo =
2125 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbard59655c2009-09-12 00:59:49 +00002126
2127 case llvm::Triple::pic16:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002128 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002129
John McCallea8d8bb2010-03-11 00:10:12 +00002130 case llvm::Triple::ppc:
2131 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2132
Daniel Dunbard59655c2009-09-12 00:59:49 +00002133 case llvm::Triple::systemz:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002134 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2135
2136 case llvm::Triple::msp430:
2137 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbard59655c2009-09-12 00:59:49 +00002138
Daniel Dunbar40165182009-08-24 09:10:05 +00002139 case llvm::Triple::x86:
Daniel Dunbar40165182009-08-24 09:10:05 +00002140 switch (Triple.getOS()) {
Edward O'Callaghan462e4ab2009-10-20 17:22:50 +00002141 case llvm::Triple::Darwin:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002142 return *(TheTargetCodeGenInfo =
2143 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002144 case llvm::Triple::Cygwin:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002145 case llvm::Triple::MinGW32:
2146 case llvm::Triple::MinGW64:
Edward O'Callaghan437ec1e2009-10-21 11:58:24 +00002147 case llvm::Triple::AuroraUX:
2148 case llvm::Triple::DragonFly:
David Chisnall2c5bef22009-09-03 01:48:05 +00002149 case llvm::Triple::FreeBSD:
Daniel Dunbare3532f82009-08-24 08:52:16 +00002150 case llvm::Triple::OpenBSD:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002151 return *(TheTargetCodeGenInfo =
2152 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbare3532f82009-08-24 08:52:16 +00002153
2154 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002155 return *(TheTargetCodeGenInfo =
2156 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002157 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002158
Daniel Dunbare3532f82009-08-24 08:52:16 +00002159 case llvm::Triple::x86_64:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002160 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbare3532f82009-08-24 08:52:16 +00002161 }
Anton Korobeynikov244360d2009-06-05 22:08:42 +00002162}