blob: ff38e77e1bdc8061aff20440b8124e4a2cfbc5eb [file] [log] [blame]
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetInfo.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000016#include "ABIInfo.h"
17#include "CodeGenFunction.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000018#include "clang/AST/RecordLayout.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000019#include "llvm/Type.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbar2c0843f2009-08-24 08:52:16 +000021#include "llvm/ADT/Triple.h"
Daniel Dunbar28df7a52009-12-03 09:13:49 +000022#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000023using namespace clang;
24using namespace CodeGen;
25
26ABIInfo::~ABIInfo() {}
27
28void ABIArgInfo::dump() const {
Daniel Dunbar28df7a52009-12-03 09:13:49 +000029 llvm::raw_ostream &OS = llvm::errs();
30 OS << "(ABIArgInfo Kind=";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000031 switch (TheKind) {
32 case Direct:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000033 OS << "Direct";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000034 break;
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000035 case Extend:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000036 OS << "Extend";
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +000037 break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000038 case Ignore:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000039 OS << "Ignore";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000040 break;
41 case Coerce:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000042 OS << "Coerce Type=";
43 getCoerceToType()->print(OS);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000044 break;
45 case Indirect:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000046 OS << "Indirect Align=" << getIndirectAlign();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000047 break;
48 case Expand:
Daniel Dunbar28df7a52009-12-03 09:13:49 +000049 OS << "Expand";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000050 break;
51 }
Daniel Dunbar28df7a52009-12-03 09:13:49 +000052 OS << ")\n";
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000053}
54
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000055TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
56
Daniel Dunbar98303b92009-09-13 08:03:58 +000057static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000058
59/// isEmptyField - Return true iff a the field is "empty", that is it
60/// is an unnamed bit-field or an (array of) empty record(s).
Daniel Dunbar98303b92009-09-13 08:03:58 +000061static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
62 bool AllowArrays) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000063 if (FD->isUnnamedBitfield())
64 return true;
65
66 QualType FT = FD->getType();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000067
Daniel Dunbar98303b92009-09-13 08:03:58 +000068 // Constant arrays of empty records count as empty, strip them off.
69 if (AllowArrays)
70 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT))
71 FT = AT->getElementType();
72
73 return isEmptyRecord(Context, FT, AllowArrays);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000074}
75
76/// isEmptyRecord - Return true iff a structure contains only empty
77/// fields. Note that a structure with a flexible array member is not
78/// considered empty.
Daniel Dunbar98303b92009-09-13 08:03:58 +000079static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
Ted Kremenek6217b802009-07-29 21:53:49 +000080 const RecordType *RT = T->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000081 if (!RT)
82 return 0;
83 const RecordDecl *RD = RT->getDecl();
84 if (RD->hasFlexibleArrayMember())
85 return false;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000086 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
87 i != e; ++i)
Daniel Dunbar98303b92009-09-13 08:03:58 +000088 if (!isEmptyField(Context, *i, AllowArrays))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +000089 return false;
90 return true;
91}
92
Anders Carlsson0a8f8472009-09-16 15:53:40 +000093/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
94/// a non-trivial destructor or a non-trivial copy constructor.
95static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
96 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
97 if (!RD)
98 return false;
99
100 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
101}
102
103/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
104/// a record type with either a non-trivial destructor or a non-trivial copy
105/// constructor.
106static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
107 const RecordType *RT = T->getAs<RecordType>();
108 if (!RT)
109 return false;
110
111 return hasNonTrivialDestructorOrCopyConstructor(RT);
112}
113
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000114/// isSingleElementStruct - Determine if a structure is a "single
115/// element struct", i.e. it has exactly one non-empty field or
116/// exactly one field which is itself a single element
117/// struct. Structures with flexible array members are never
118/// considered single element structs.
119///
120/// \return The field declaration for the single non-empty field, if
121/// it exists.
122static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
123 const RecordType *RT = T->getAsStructureType();
124 if (!RT)
125 return 0;
126
127 const RecordDecl *RD = RT->getDecl();
128 if (RD->hasFlexibleArrayMember())
129 return 0;
130
131 const Type *Found = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000132 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
133 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000134 const FieldDecl *FD = *i;
135 QualType FT = FD->getType();
136
137 // Ignore empty fields.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000138 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000139 continue;
140
141 // If we already found an element then this isn't a single-element
142 // struct.
143 if (Found)
144 return 0;
145
146 // Treat single element arrays as the element.
147 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
148 if (AT->getSize().getZExtValue() != 1)
149 break;
150 FT = AT->getElementType();
151 }
152
153 if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
154 Found = FT.getTypePtr();
155 } else {
156 Found = isSingleElementStruct(FT, Context);
157 if (!Found)
158 return 0;
159 }
160 }
161
162 return Found;
163}
164
165static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000166 if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() &&
167 !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
168 !Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000169 return false;
170
171 uint64_t Size = Context.getTypeSize(Ty);
172 return Size == 32 || Size == 64;
173}
174
Daniel Dunbar53012f42009-11-09 01:33:53 +0000175/// canExpandIndirectArgument - Test whether an argument type which is to be
176/// passed indirectly (on the stack) would have the equivalent layout if it was
177/// expanded into separate arguments. If so, we prefer to do the latter to avoid
178/// inhibiting optimizations.
179///
180// FIXME: This predicate is missing many cases, currently it just follows
181// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
182// should probably make this smarter, or better yet make the LLVM backend
183// capable of handling it.
184static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
185 // We can only expand structure types.
186 const RecordType *RT = Ty->getAs<RecordType>();
187 if (!RT)
188 return false;
189
190 // We can only expand (C) structures.
191 //
192 // FIXME: This needs to be generalized to handle classes as well.
193 const RecordDecl *RD = RT->getDecl();
194 if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
195 return false;
196
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000197 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
198 i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000199 const FieldDecl *FD = *i;
200
201 if (!is32Or64BitBasicType(FD->getType(), Context))
202 return false;
203
204 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
205 // how to expand them yet, and the predicate for telling if a bitfield still
206 // counts as "basic" is more complicated than what we were doing previously.
207 if (FD->isBitField())
208 return false;
209 }
210
211 return true;
212}
213
Eli Friedmana1e6de92009-06-13 21:37:10 +0000214static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000215 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
216 i != e; ++i) {
Eli Friedmana1e6de92009-06-13 21:37:10 +0000217 const FieldDecl *FD = *i;
218
219 if (FD->getType()->isVectorType() &&
220 Context.getTypeSize(FD->getType()) >= 128)
221 return true;
222
Ted Kremenek6217b802009-07-29 21:53:49 +0000223 if (const RecordType* RT = FD->getType()->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000224 if (typeContainsSSEVector(RT->getDecl(), Context))
225 return true;
226 }
227
228 return false;
229}
230
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000231namespace {
232/// DefaultABIInfo - The default implementation for ABI specific
233/// details. This implementation provides information which results in
234/// self-consistent and sensible LLVM IR generation, but does not
235/// conform to any particular ABI.
236class DefaultABIInfo : public ABIInfo {
237 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000238 ASTContext &Context,
239 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000240
241 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000242 ASTContext &Context,
243 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000244
Owen Andersona1cf15f2009-07-14 23:10:40 +0000245 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
246 llvm::LLVMContext &VMContext) const {
247 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
248 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000249 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
250 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000251 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000252 }
253
254 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
255 CodeGenFunction &CGF) const;
256};
257
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000258class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
259public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000260 DefaultTargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000261};
262
263llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
264 CodeGenFunction &CGF) const {
265 return 0;
266}
267
268ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
269 ASTContext &Context,
270 llvm::LLVMContext &VMContext) const {
Chris Lattnera14db752010-03-11 18:19:55 +0000271 if (CodeGenFunction::hasAggregateLLVMType(Ty))
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000272 return ABIArgInfo::getIndirect(0);
Chris Lattnera14db752010-03-11 18:19:55 +0000273
274 // Treat an enum type as its underlying type.
275 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
276 Ty = EnumTy->getDecl()->getIntegerType();
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000277
Chris Lattnera14db752010-03-11 18:19:55 +0000278 return (Ty->isPromotableIntegerType() ?
279 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000280}
281
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000282/// X86_32ABIInfo - The X86-32 ABI information.
283class X86_32ABIInfo : public ABIInfo {
284 ASTContext &Context;
David Chisnall1e4249c2009-08-17 23:08:21 +0000285 bool IsDarwinVectorABI;
286 bool IsSmallStructInRegABI;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000287
288 static bool isRegisterSize(unsigned Size) {
289 return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
290 }
291
292 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
293
Eli Friedmana1e6de92009-06-13 21:37:10 +0000294 static unsigned getIndirectArgumentAlignment(QualType Ty,
295 ASTContext &Context);
296
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000297public:
298 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000299 ASTContext &Context,
300 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000301
302 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000303 ASTContext &Context,
304 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000305
Owen Andersona1cf15f2009-07-14 23:10:40 +0000306 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
307 llvm::LLVMContext &VMContext) const {
308 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
309 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000310 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
311 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +0000312 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000313 }
314
315 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
316 CodeGenFunction &CGF) const;
317
David Chisnall1e4249c2009-08-17 23:08:21 +0000318 X86_32ABIInfo(ASTContext &Context, bool d, bool p)
Mike Stump1eb44332009-09-09 15:08:12 +0000319 : ABIInfo(), Context(Context), IsDarwinVectorABI(d),
David Chisnall1e4249c2009-08-17 23:08:21 +0000320 IsSmallStructInRegABI(p) {}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000321};
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000322
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000323class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
324public:
325 X86_32TargetCodeGenInfo(ASTContext &Context, bool d, bool p)
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000326 :TargetCodeGenInfo(new X86_32ABIInfo(Context, d, p)) {}
Charles Davis74f72932010-02-13 15:54:06 +0000327
328 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
329 CodeGen::CodeGenModule &CGM) const;
John McCall6374c332010-03-06 00:35:14 +0000330
331 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
332 // Darwin uses different dwarf register numbers for EH.
333 if (CGM.isTargetDarwin()) return 5;
334
335 return 4;
336 }
337
338 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
339 llvm::Value *Address) const;
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000340};
341
342}
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000343
344/// shouldReturnTypeInRegister - Determine if the given type should be
345/// passed in a register (for the Darwin ABI).
346bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
347 ASTContext &Context) {
348 uint64_t Size = Context.getTypeSize(Ty);
349
350 // Type must be register sized.
351 if (!isRegisterSize(Size))
352 return false;
353
354 if (Ty->isVectorType()) {
355 // 64- and 128- bit vectors inside structures are not returned in
356 // registers.
357 if (Size == 64 || Size == 128)
358 return false;
359
360 return true;
361 }
362
Daniel Dunbar55e59e12009-09-24 05:12:36 +0000363 // If this is a builtin, pointer, enum, or complex type, it is ok.
364 if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() ||
365 Ty->isAnyComplexType() || Ty->isEnumeralType() ||
366 Ty->isBlockPointerType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000367 return true;
368
369 // Arrays are treated like records.
370 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
371 return shouldReturnTypeInRegister(AT->getElementType(), Context);
372
373 // Otherwise, it must be a record type.
Ted Kremenek6217b802009-07-29 21:53:49 +0000374 const RecordType *RT = Ty->getAs<RecordType>();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000375 if (!RT) return false;
376
Anders Carlssona8874232010-01-27 03:25:19 +0000377 // FIXME: Traverse bases here too.
378
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000379 // Structure types are passed in register if all fields would be
380 // passed in a register.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000381 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
382 e = RT->getDecl()->field_end(); i != e; ++i) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000383 const FieldDecl *FD = *i;
384
385 // Empty fields are ignored.
Daniel Dunbar98303b92009-09-13 08:03:58 +0000386 if (isEmptyField(Context, FD, true))
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000387 continue;
388
389 // Check fields recursively.
390 if (!shouldReturnTypeInRegister(FD->getType(), Context))
391 return false;
392 }
393
394 return true;
395}
396
397ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000398 ASTContext &Context,
399 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000400 if (RetTy->isVoidType()) {
401 return ABIArgInfo::getIgnore();
John McCall183700f2009-09-21 23:43:11 +0000402 } else if (const VectorType *VT = RetTy->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000403 // On Darwin, some vectors are returned in registers.
David Chisnall1e4249c2009-08-17 23:08:21 +0000404 if (IsDarwinVectorABI) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000405 uint64_t Size = Context.getTypeSize(RetTy);
406
407 // 128-bit vectors are a special case; they are returned in
408 // registers and we need to make sure to pick a type the LLVM
409 // backend will like.
410 if (Size == 128)
Owen Anderson0032b272009-08-13 21:57:51 +0000411 return ABIArgInfo::getCoerce(llvm::VectorType::get(
412 llvm::Type::getInt64Ty(VMContext), 2));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000413
414 // Always return in register if it fits in a general purpose
415 // register, or if it is 64 bits and has a single element.
416 if ((Size == 8 || Size == 16 || Size == 32) ||
417 (Size == 64 && VT->getNumElements() == 1))
Owen Anderson0032b272009-08-13 21:57:51 +0000418 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000419
420 return ABIArgInfo::getIndirect(0);
421 }
422
423 return ABIArgInfo::getDirect();
424 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
Anders Carlssona8874232010-01-27 03:25:19 +0000425 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
Anders Carlsson40092972009-10-20 22:07:59 +0000426 // Structures with either a non-trivial destructor or a non-trivial
427 // copy constructor are always indirect.
428 if (hasNonTrivialDestructorOrCopyConstructor(RT))
429 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
430
431 // Structures with flexible arrays are always indirect.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000432 if (RT->getDecl()->hasFlexibleArrayMember())
433 return ABIArgInfo::getIndirect(0);
Anders Carlsson40092972009-10-20 22:07:59 +0000434 }
435
David Chisnall1e4249c2009-08-17 23:08:21 +0000436 // If specified, structs and unions are always indirect.
437 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000438 return ABIArgInfo::getIndirect(0);
439
440 // Classify "single element" structs as their element type.
441 if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) {
John McCall183700f2009-09-21 23:43:11 +0000442 if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000443 if (BT->isIntegerType()) {
444 // We need to use the size of the structure, padding
445 // bit-fields can adjust that to be larger than the single
446 // element type.
447 uint64_t Size = Context.getTypeSize(RetTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000448 return ABIArgInfo::getCoerce(
Owen Anderson0032b272009-08-13 21:57:51 +0000449 llvm::IntegerType::get(VMContext, (unsigned) Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000450 } else if (BT->getKind() == BuiltinType::Float) {
451 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
452 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000453 return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000454 } else if (BT->getKind() == BuiltinType::Double) {
455 assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) &&
456 "Unexpect single element structure size!");
Owen Anderson0032b272009-08-13 21:57:51 +0000457 return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000458 }
459 } else if (SeltTy->isPointerType()) {
460 // FIXME: It would be really nice if this could come out as the proper
461 // pointer type.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000462 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000463 return ABIArgInfo::getCoerce(PtrTy);
464 } else if (SeltTy->isVectorType()) {
465 // 64- and 128-bit vectors are never returned in a
466 // register when inside a structure.
467 uint64_t Size = Context.getTypeSize(RetTy);
468 if (Size == 64 || Size == 128)
469 return ABIArgInfo::getIndirect(0);
470
Owen Andersona1cf15f2009-07-14 23:10:40 +0000471 return classifyReturnType(QualType(SeltTy, 0), Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000472 }
473 }
474
475 // Small structures which are register sized are generally returned
476 // in a register.
477 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) {
478 uint64_t Size = Context.getTypeSize(RetTy);
Owen Anderson0032b272009-08-13 21:57:51 +0000479 return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000480 }
481
482 return ABIArgInfo::getIndirect(0);
483 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000484 // Treat an enum type as its underlying type.
485 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
486 RetTy = EnumTy->getDecl()->getIntegerType();
487
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000488 return (RetTy->isPromotableIntegerType() ?
489 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000490 }
491}
492
Eli Friedmana1e6de92009-06-13 21:37:10 +0000493unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty,
494 ASTContext &Context) {
495 unsigned Align = Context.getTypeAlign(Ty);
496 if (Align < 128) return 0;
Ted Kremenek6217b802009-07-29 21:53:49 +0000497 if (const RecordType* RT = Ty->getAs<RecordType>())
Eli Friedmana1e6de92009-06-13 21:37:10 +0000498 if (typeContainsSSEVector(RT->getDecl(), Context))
499 return 16;
500 return 0;
501}
502
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000503ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000504 ASTContext &Context,
505 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000506 // FIXME: Set alignment on indirect arguments.
507 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
508 // Structures with flexible arrays are always indirect.
Anders Carlssona8874232010-01-27 03:25:19 +0000509 if (const RecordType *RT = Ty->getAs<RecordType>()) {
510 // Structures with either a non-trivial destructor or a non-trivial
511 // copy constructor are always indirect.
512 if (hasNonTrivialDestructorOrCopyConstructor(RT))
513 return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
514
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000515 if (RT->getDecl()->hasFlexibleArrayMember())
Mike Stump1eb44332009-09-09 15:08:12 +0000516 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty,
Eli Friedmana1e6de92009-06-13 21:37:10 +0000517 Context));
Anders Carlssona8874232010-01-27 03:25:19 +0000518 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000519
520 // Ignore empty structs.
Eli Friedmana1e6de92009-06-13 21:37:10 +0000521 if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000522 return ABIArgInfo::getIgnore();
523
Daniel Dunbar53012f42009-11-09 01:33:53 +0000524 // Expand small (<= 128-bit) record types when we know that the stack layout
525 // of those arguments will match the struct. This is important because the
526 // LLVM backend isn't smart enough to remove byval, which inhibits many
527 // optimizations.
528 if (Context.getTypeSize(Ty) <= 4*32 &&
529 canExpandIndirectArgument(Ty, Context))
530 return ABIArgInfo::getExpand();
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000531
Eli Friedmana1e6de92009-06-13 21:37:10 +0000532 return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000533 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +0000534 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
535 Ty = EnumTy->getDecl()->getIntegerType();
536
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +0000537 return (Ty->isPromotableIntegerType() ?
538 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000539 }
540}
541
542llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
543 CodeGenFunction &CGF) const {
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000544 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000545 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000546
547 CGBuilderTy &Builder = CGF.Builder;
548 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
549 "ap");
550 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
551 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000552 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000553 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
554
555 uint64_t Offset =
556 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
557 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +0000558 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
559 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000560 "ap.next");
561 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
562
563 return AddrTyped;
564}
565
Charles Davis74f72932010-02-13 15:54:06 +0000566void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
567 llvm::GlobalValue *GV,
568 CodeGen::CodeGenModule &CGM) const {
569 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
570 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
571 // Get the LLVM function.
572 llvm::Function *Fn = cast<llvm::Function>(GV);
573
574 // Now add the 'alignstack' attribute with a value of 16.
575 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
576 }
577 }
578}
579
John McCall6374c332010-03-06 00:35:14 +0000580bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
581 CodeGen::CodeGenFunction &CGF,
582 llvm::Value *Address) const {
583 CodeGen::CGBuilderTy &Builder = CGF.Builder;
584 llvm::LLVMContext &Context = CGF.getLLVMContext();
585
586 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
587 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
588
589 // 0-7 are the eight integer registers; the order is different
590 // on Darwin (for EH), but the range is the same.
591 // 8 is %eip.
592 for (unsigned I = 0, E = 9; I != E; ++I) {
593 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
594 Builder.CreateStore(Four8, Slot);
595 }
596
597 if (CGF.CGM.isTargetDarwin()) {
598 // 12-16 are st(0..4). Not sure why we stop at 4.
599 // These have size 16, which is sizeof(long double) on
600 // platforms with 8-byte alignment for that type.
601 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
602 for (unsigned I = 12, E = 17; I != E; ++I) {
603 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
604 Builder.CreateStore(Sixteen8, Slot);
605 }
606
607 } else {
608 // 9 is %eflags, which doesn't get a size on Darwin for some
609 // reason.
610 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
611
612 // 11-16 are st(0..5). Not sure why we stop at 5.
613 // These have size 12, which is sizeof(long double) on
614 // platforms with 4-byte alignment for that type.
615 llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12);
616 for (unsigned I = 11, E = 17; I != E; ++I) {
617 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
618 Builder.CreateStore(Twelve8, Slot);
619 }
620 }
621
622 return false;
623}
624
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000625namespace {
626/// X86_64ABIInfo - The X86_64 ABI information.
627class X86_64ABIInfo : public ABIInfo {
628 enum Class {
629 Integer = 0,
630 SSE,
631 SSEUp,
632 X87,
633 X87Up,
634 ComplexX87,
635 NoClass,
636 Memory
637 };
638
639 /// merge - Implement the X86_64 ABI merging algorithm.
640 ///
641 /// Merge an accumulating classification \arg Accum with a field
642 /// classification \arg Field.
643 ///
644 /// \param Accum - The accumulating classification. This should
645 /// always be either NoClass or the result of a previous merge
646 /// call. In addition, this should never be Memory (the caller
647 /// should just return Memory for the aggregate).
648 Class merge(Class Accum, Class Field) const;
649
650 /// classify - Determine the x86_64 register classes in which the
651 /// given type T should be passed.
652 ///
653 /// \param Lo - The classification for the parts of the type
654 /// residing in the low word of the containing object.
655 ///
656 /// \param Hi - The classification for the parts of the type
657 /// residing in the high word of the containing object.
658 ///
659 /// \param OffsetBase - The bit offset of this type in the
660 /// containing object. Some parameters are classified different
661 /// depending on whether they straddle an eightbyte boundary.
662 ///
663 /// If a word is unused its result will be NoClass; if a type should
664 /// be passed in Memory then at least the classification of \arg Lo
665 /// will be Memory.
666 ///
667 /// The \arg Lo class will be NoClass iff the argument is ignored.
668 ///
669 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
670 /// also be ComplexX87.
671 void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
672 Class &Lo, Class &Hi) const;
673
674 /// getCoerceResult - Given a source type \arg Ty and an LLVM type
675 /// to coerce to, chose the best way to pass Ty in the same place
676 /// that \arg CoerceTo would be passed, but while keeping the
677 /// emitted code as simple as possible.
678 ///
679 /// FIXME: Note, this should be cleaned up to just take an enumeration of all
680 /// the ways we might want to pass things, instead of constructing an LLVM
681 /// type. This makes this code more explicit, and it makes it clearer that we
682 /// are also doing this for correctness in the case of passing scalar types.
683 ABIArgInfo getCoerceResult(QualType Ty,
684 const llvm::Type *CoerceTo,
685 ASTContext &Context) const;
686
687 /// getIndirectResult - Give a source type \arg Ty, return a suitable result
688 /// such that the argument will be passed in memory.
689 ABIArgInfo getIndirectResult(QualType Ty,
690 ASTContext &Context) const;
691
692 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000693 ASTContext &Context,
694 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000695
696 ABIArgInfo classifyArgumentType(QualType Ty,
697 ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000698 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000699 unsigned &neededInt,
700 unsigned &neededSSE) const;
701
702public:
Owen Andersona1cf15f2009-07-14 23:10:40 +0000703 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
704 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000705
706 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
707 CodeGenFunction &CGF) const;
708};
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000709
710class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
711public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +0000712 X86_64TargetCodeGenInfo():TargetCodeGenInfo(new X86_64ABIInfo()) {}
John McCall6374c332010-03-06 00:35:14 +0000713
714 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
715 return 7;
716 }
717
718 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
719 llvm::Value *Address) const {
720 CodeGen::CGBuilderTy &Builder = CGF.Builder;
721 llvm::LLVMContext &Context = CGF.getLLVMContext();
722
723 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
724 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
725
726 // 0-16 are the 16 integer registers.
727 // 17 is %rip.
728 for (unsigned I = 0, E = 17; I != E; ++I) {
729 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
730 Builder.CreateStore(Eight8, Slot);
731 }
732
733 return false;
734 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +0000735};
736
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000737}
738
739X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
740 Class Field) const {
741 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
742 // classified recursively so that always two fields are
743 // considered. The resulting class is calculated according to
744 // the classes of the fields in the eightbyte:
745 //
746 // (a) If both classes are equal, this is the resulting class.
747 //
748 // (b) If one of the classes is NO_CLASS, the resulting class is
749 // the other class.
750 //
751 // (c) If one of the classes is MEMORY, the result is the MEMORY
752 // class.
753 //
754 // (d) If one of the classes is INTEGER, the result is the
755 // INTEGER.
756 //
757 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
758 // MEMORY is used as class.
759 //
760 // (f) Otherwise class SSE is used.
761
762 // Accum should never be memory (we should have returned) or
763 // ComplexX87 (because this cannot be passed in a structure).
764 assert((Accum != Memory && Accum != ComplexX87) &&
765 "Invalid accumulated classification during merge.");
766 if (Accum == Field || Field == NoClass)
767 return Accum;
768 else if (Field == Memory)
769 return Memory;
770 else if (Accum == NoClass)
771 return Field;
772 else if (Accum == Integer || Field == Integer)
773 return Integer;
774 else if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
775 Accum == X87 || Accum == X87Up)
776 return Memory;
777 else
778 return SSE;
779}
780
781void X86_64ABIInfo::classify(QualType Ty,
782 ASTContext &Context,
783 uint64_t OffsetBase,
784 Class &Lo, Class &Hi) const {
785 // FIXME: This code can be simplified by introducing a simple value class for
786 // Class pairs with appropriate constructor methods for the various
787 // situations.
788
789 // FIXME: Some of the split computations are wrong; unaligned vectors
790 // shouldn't be passed in registers for example, so there is no chance they
791 // can straddle an eightbyte. Verify & simplify.
792
793 Lo = Hi = NoClass;
794
795 Class &Current = OffsetBase < 64 ? Lo : Hi;
796 Current = Memory;
797
John McCall183700f2009-09-21 23:43:11 +0000798 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000799 BuiltinType::Kind k = BT->getKind();
800
801 if (k == BuiltinType::Void) {
802 Current = NoClass;
803 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
804 Lo = Integer;
805 Hi = Integer;
806 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
807 Current = Integer;
808 } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
809 Current = SSE;
810 } else if (k == BuiltinType::LongDouble) {
811 Lo = X87;
812 Hi = X87Up;
813 }
814 // FIXME: _Decimal32 and _Decimal64 are SSE.
815 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
John McCall183700f2009-09-21 23:43:11 +0000816 } else if (const EnumType *ET = Ty->getAs<EnumType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000817 // Classify the underlying integer type.
818 classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
819 } else if (Ty->hasPointerRepresentation()) {
820 Current = Integer;
John McCall183700f2009-09-21 23:43:11 +0000821 } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000822 uint64_t Size = Context.getTypeSize(VT);
823 if (Size == 32) {
824 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
825 // float> as integer.
826 Current = Integer;
827
828 // If this type crosses an eightbyte boundary, it should be
829 // split.
830 uint64_t EB_Real = (OffsetBase) / 64;
831 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
832 if (EB_Real != EB_Imag)
833 Hi = Lo;
834 } else if (Size == 64) {
835 // gcc passes <1 x double> in memory. :(
836 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
837 return;
838
839 // gcc passes <1 x long long> as INTEGER.
840 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
841 Current = Integer;
842 else
843 Current = SSE;
844
845 // If this type crosses an eightbyte boundary, it should be
846 // split.
847 if (OffsetBase && OffsetBase != 64)
848 Hi = Lo;
849 } else if (Size == 128) {
850 Lo = SSE;
851 Hi = SSEUp;
852 }
John McCall183700f2009-09-21 23:43:11 +0000853 } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000854 QualType ET = Context.getCanonicalType(CT->getElementType());
855
856 uint64_t Size = Context.getTypeSize(Ty);
857 if (ET->isIntegralType()) {
858 if (Size <= 64)
859 Current = Integer;
860 else if (Size <= 128)
861 Lo = Hi = Integer;
862 } else if (ET == Context.FloatTy)
863 Current = SSE;
864 else if (ET == Context.DoubleTy)
865 Lo = Hi = SSE;
866 else if (ET == Context.LongDoubleTy)
867 Current = ComplexX87;
868
869 // If this complex type crosses an eightbyte boundary then it
870 // should be split.
871 uint64_t EB_Real = (OffsetBase) / 64;
872 uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
873 if (Hi == NoClass && EB_Real != EB_Imag)
874 Hi = Lo;
875 } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
876 // Arrays are treated like structures.
877
878 uint64_t Size = Context.getTypeSize(Ty);
879
880 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
881 // than two eightbytes, ..., it has class MEMORY.
882 if (Size > 128)
883 return;
884
885 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
886 // fields, it has class MEMORY.
887 //
888 // Only need to check alignment of array base.
889 if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
890 return;
891
892 // Otherwise implement simplified merge. We could be smarter about
893 // this, but it isn't worth it and would be harder to verify.
894 Current = NoClass;
895 uint64_t EltSize = Context.getTypeSize(AT->getElementType());
896 uint64_t ArraySize = AT->getSize().getZExtValue();
897 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
898 Class FieldLo, FieldHi;
899 classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
900 Lo = merge(Lo, FieldLo);
901 Hi = merge(Hi, FieldHi);
902 if (Lo == Memory || Hi == Memory)
903 break;
904 }
905
906 // Do post merger cleanup (see below). Only case we worry about is Memory.
907 if (Hi == Memory)
908 Lo = Memory;
909 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
Ted Kremenek6217b802009-07-29 21:53:49 +0000910 } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000911 uint64_t Size = Context.getTypeSize(Ty);
912
913 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
914 // than two eightbytes, ..., it has class MEMORY.
915 if (Size > 128)
916 return;
917
Anders Carlsson0a8f8472009-09-16 15:53:40 +0000918 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
919 // copy constructor or a non-trivial destructor, it is passed by invisible
920 // reference.
921 if (hasNonTrivialDestructorOrCopyConstructor(RT))
922 return;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000923
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000924 const RecordDecl *RD = RT->getDecl();
925
926 // Assume variable sized types are passed in memory.
927 if (RD->hasFlexibleArrayMember())
928 return;
929
930 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
931
932 // Reset Lo class, this will be recomputed.
933 Current = NoClass;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000934
935 // If this is a C++ record, classify the bases first.
936 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
937 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
938 e = CXXRD->bases_end(); i != e; ++i) {
939 assert(!i->isVirtual() && !i->getType()->isDependentType() &&
940 "Unexpected base class!");
941 const CXXRecordDecl *Base =
942 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
943
944 // Classify this field.
945 //
946 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
947 // single eightbyte, each is classified separately. Each eightbyte gets
948 // initialized to class NO_CLASS.
949 Class FieldLo, FieldHi;
950 uint64_t Offset = OffsetBase + Layout.getBaseClassOffset(Base);
951 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
952 Lo = merge(Lo, FieldLo);
953 Hi = merge(Hi, FieldHi);
954 if (Lo == Memory || Hi == Memory)
955 break;
956 }
Daniel Dunbar4971ff82009-12-22 01:19:25 +0000957
958 // If this record has no fields but isn't empty, classify as INTEGER.
959 if (RD->field_empty() && Size)
960 Current = Integer;
Daniel Dunbarce9f4232009-11-22 23:01:23 +0000961 }
962
963 // Classify the fields one at a time, merging the results.
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000964 unsigned idx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000965 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
966 i != e; ++i, ++idx) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +0000967 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
968 bool BitField = i->isBitField();
969
970 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
971 // fields, it has class MEMORY.
972 //
973 // Note, skip this test for bit-fields, see below.
974 if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
975 Lo = Memory;
976 return;
977 }
978
979 // Classify this field.
980 //
981 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
982 // exceeds a single eightbyte, each is classified
983 // separately. Each eightbyte gets initialized to class
984 // NO_CLASS.
985 Class FieldLo, FieldHi;
986
987 // Bit-fields require special handling, they do not force the
988 // structure to be passed in memory even if unaligned, and
989 // therefore they can straddle an eightbyte.
990 if (BitField) {
991 // Ignore padding bit-fields.
992 if (i->isUnnamedBitfield())
993 continue;
994
995 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
996 uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
997
998 uint64_t EB_Lo = Offset / 64;
999 uint64_t EB_Hi = (Offset + Size - 1) / 64;
1000 FieldLo = FieldHi = NoClass;
1001 if (EB_Lo) {
1002 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1003 FieldLo = NoClass;
1004 FieldHi = Integer;
1005 } else {
1006 FieldLo = Integer;
1007 FieldHi = EB_Hi ? Integer : NoClass;
1008 }
1009 } else
1010 classify(i->getType(), Context, Offset, FieldLo, FieldHi);
1011 Lo = merge(Lo, FieldLo);
1012 Hi = merge(Hi, FieldHi);
1013 if (Lo == Memory || Hi == Memory)
1014 break;
1015 }
1016
1017 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1018 //
1019 // (a) If one of the classes is MEMORY, the whole argument is
1020 // passed in memory.
1021 //
1022 // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
1023
1024 // The first of these conditions is guaranteed by how we implement
1025 // the merge (just bail).
1026 //
1027 // The second condition occurs in the case of unions; for example
1028 // union { _Complex double; unsigned; }.
1029 if (Hi == Memory)
1030 Lo = Memory;
1031 if (Hi == SSEUp && Lo != SSE)
1032 Hi = SSE;
1033 }
1034}
1035
1036ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
1037 const llvm::Type *CoerceTo,
1038 ASTContext &Context) const {
Owen Anderson0032b272009-08-13 21:57:51 +00001039 if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001040 // Integer and pointer types will end up in a general purpose
1041 // register.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001042
1043 // Treat an enum type as its underlying type.
1044 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1045 Ty = EnumTy->getDecl()->getIntegerType();
1046
Anders Carlsson5b3a2fc2009-09-26 03:56:53 +00001047 if (Ty->isIntegralType() || Ty->hasPointerRepresentation())
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001048 return (Ty->isPromotableIntegerType() ?
1049 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Owen Anderson0032b272009-08-13 21:57:51 +00001050 } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) {
John McCall0b0ef0a2010-02-24 07:14:12 +00001051 assert(Ty.isCanonical() && "should always have a canonical type here");
1052 assert(!Ty.hasQualifiers() && "should never have a qualified type here");
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001053
1054 // Float and double end up in a single SSE reg.
John McCall0b0ef0a2010-02-24 07:14:12 +00001055 if (Ty == Context.FloatTy || Ty == Context.DoubleTy)
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001056 return ABIArgInfo::getDirect();
1057
1058 }
1059
1060 return ABIArgInfo::getCoerce(CoerceTo);
1061}
1062
1063ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1064 ASTContext &Context) const {
1065 // If this is a scalar LLVM value then assume LLVM will pass it in the right
1066 // place naturally.
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001067 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1068 // Treat an enum type as its underlying type.
1069 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1070 Ty = EnumTy->getDecl()->getIntegerType();
1071
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001072 return (Ty->isPromotableIntegerType() ?
1073 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001074 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001075
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001076 bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty);
1077
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001078 // FIXME: Set alignment correctly.
Anders Carlsson0a8f8472009-09-16 15:53:40 +00001079 return ABIArgInfo::getIndirect(0, ByVal);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001080}
1081
1082ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001083 ASTContext &Context,
1084 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001085 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1086 // classification algorithm.
1087 X86_64ABIInfo::Class Lo, Hi;
1088 classify(RetTy, Context, 0, Lo, Hi);
1089
1090 // Check some invariants.
1091 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1092 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1093 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1094
1095 const llvm::Type *ResType = 0;
1096 switch (Lo) {
1097 case NoClass:
1098 return ABIArgInfo::getIgnore();
1099
1100 case SSEUp:
1101 case X87Up:
1102 assert(0 && "Invalid classification for lo word.");
1103
1104 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1105 // hidden argument.
1106 case Memory:
1107 return getIndirectResult(RetTy, Context);
1108
1109 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1110 // available register of the sequence %rax, %rdx is used.
1111 case Integer:
Owen Anderson0032b272009-08-13 21:57:51 +00001112 ResType = llvm::Type::getInt64Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001113
1114 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1115 // available SSE register of the sequence %xmm0, %xmm1 is used.
1116 case SSE:
Owen Anderson0032b272009-08-13 21:57:51 +00001117 ResType = llvm::Type::getDoubleTy(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001118
1119 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1120 // returned on the X87 stack in %st0 as 80-bit x87 number.
1121 case X87:
Owen Anderson0032b272009-08-13 21:57:51 +00001122 ResType = llvm::Type::getX86_FP80Ty(VMContext); break;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001123
1124 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1125 // part of the value is returned in %st0 and the imaginary part in
1126 // %st1.
1127 case ComplexX87:
1128 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
Chris Lattner52d9ae32010-04-06 17:29:22 +00001129 ResType = llvm::StructType::get(VMContext,
1130 llvm::Type::getX86_FP80Ty(VMContext),
Owen Anderson0032b272009-08-13 21:57:51 +00001131 llvm::Type::getX86_FP80Ty(VMContext),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001132 NULL);
1133 break;
1134 }
1135
1136 switch (Hi) {
1137 // Memory was handled previously and X87 should
1138 // never occur as a hi class.
1139 case Memory:
1140 case X87:
1141 assert(0 && "Invalid classification for hi word.");
1142
1143 case ComplexX87: // Previously handled.
1144 case NoClass: break;
1145
1146 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001147 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001148 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001149 break;
1150 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001151 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001152 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001153 break;
1154
1155 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
1156 // is passed in the upper half of the last used SSE register.
1157 //
1158 // SSEUP should always be preceeded by SSE, just widen.
1159 case SSEUp:
1160 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001161 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001162 break;
1163
1164 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1165 // returned together with the previous X87 value in %st0.
1166 case X87Up:
1167 // If X87Up is preceeded by X87, we don't need to do
1168 // anything. However, in some cases with unions it may not be
1169 // preceeded by X87. In such situations we follow gcc and pass the
1170 // extra bits in an SSE reg.
1171 if (Lo != X87)
Owen Anderson47a434f2009-08-05 23:18:46 +00001172 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001173 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001174 break;
1175 }
1176
1177 return getCoerceResult(RetTy, ResType, Context);
1178}
1179
1180ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001181 llvm::LLVMContext &VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001182 unsigned &neededInt,
1183 unsigned &neededSSE) const {
1184 X86_64ABIInfo::Class Lo, Hi;
1185 classify(Ty, Context, 0, Lo, Hi);
1186
1187 // Check some invariants.
1188 // FIXME: Enforce these by construction.
1189 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1190 assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
1191 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1192
1193 neededInt = 0;
1194 neededSSE = 0;
1195 const llvm::Type *ResType = 0;
1196 switch (Lo) {
1197 case NoClass:
1198 return ABIArgInfo::getIgnore();
1199
1200 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1201 // on the stack.
1202 case Memory:
1203
1204 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1205 // COMPLEX_X87, it is passed in memory.
1206 case X87:
1207 case ComplexX87:
1208 return getIndirectResult(Ty, Context);
1209
1210 case SSEUp:
1211 case X87Up:
1212 assert(0 && "Invalid classification for lo word.");
1213
1214 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1215 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1216 // and %r9 is used.
1217 case Integer:
1218 ++neededInt;
Owen Anderson0032b272009-08-13 21:57:51 +00001219 ResType = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001220 break;
1221
1222 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1223 // available SSE register is used, the registers are taken in the
1224 // order from %xmm0 to %xmm7.
1225 case SSE:
1226 ++neededSSE;
Owen Anderson0032b272009-08-13 21:57:51 +00001227 ResType = llvm::Type::getDoubleTy(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001228 break;
1229 }
1230
1231 switch (Hi) {
1232 // Memory was handled previously, ComplexX87 and X87 should
1233 // never occur as hi classes, and X87Up must be preceed by X87,
1234 // which is passed in memory.
1235 case Memory:
1236 case X87:
1237 case ComplexX87:
1238 assert(0 && "Invalid classification for hi word.");
1239 break;
1240
1241 case NoClass: break;
1242 case Integer:
Owen Anderson47a434f2009-08-05 23:18:46 +00001243 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001244 llvm::Type::getInt64Ty(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001245 ++neededInt;
1246 break;
1247
1248 // X87Up generally doesn't occur here (long double is passed in
1249 // memory), except in situations involving unions.
1250 case X87Up:
1251 case SSE:
Owen Anderson47a434f2009-08-05 23:18:46 +00001252 ResType = llvm::StructType::get(VMContext, ResType,
Owen Anderson0032b272009-08-13 21:57:51 +00001253 llvm::Type::getDoubleTy(VMContext), NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001254 ++neededSSE;
1255 break;
1256
1257 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
1258 // eightbyte is passed in the upper half of the last used SSE
1259 // register.
1260 case SSEUp:
1261 assert(Lo == SSE && "Unexpected SSEUp classification.");
Owen Anderson0032b272009-08-13 21:57:51 +00001262 ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001263 break;
1264 }
1265
1266 return getCoerceResult(Ty, ResType, Context);
1267}
1268
Owen Andersona1cf15f2009-07-14 23:10:40 +00001269void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1270 llvm::LLVMContext &VMContext) const {
1271 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1272 Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001273
1274 // Keep track of the number of assigned registers.
1275 unsigned freeIntRegs = 6, freeSSERegs = 8;
1276
1277 // If the return value is indirect, then the hidden argument is consuming one
1278 // integer register.
1279 if (FI.getReturnInfo().isIndirect())
1280 --freeIntRegs;
1281
1282 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
1283 // get assigned (in left-to-right order) for passing as follows...
1284 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1285 it != ie; ++it) {
1286 unsigned neededInt, neededSSE;
Mike Stump1eb44332009-09-09 15:08:12 +00001287 it->info = classifyArgumentType(it->type, Context, VMContext,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001288 neededInt, neededSSE);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001289
1290 // AMD64-ABI 3.2.3p3: If there are no registers available for any
1291 // eightbyte of an argument, the whole argument is passed on the
1292 // stack. If registers have already been assigned for some
1293 // eightbytes of such an argument, the assignments get reverted.
1294 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
1295 freeIntRegs -= neededInt;
1296 freeSSERegs -= neededSSE;
1297 } else {
1298 it->info = getIndirectResult(it->type, Context);
1299 }
1300 }
1301}
1302
1303static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
1304 QualType Ty,
1305 CodeGenFunction &CGF) {
1306 llvm::Value *overflow_arg_area_p =
1307 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
1308 llvm::Value *overflow_arg_area =
1309 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
1310
1311 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
1312 // byte boundary if alignment needed by type exceeds 8 byte boundary.
1313 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
1314 if (Align > 8) {
1315 // Note that we follow the ABI & gcc here, even though the type
1316 // could in theory have an alignment greater than 16. This case
1317 // shouldn't ever matter in practice.
1318
1319 // overflow_arg_area = (overflow_arg_area + 15) & ~15;
Owen Anderson0032b272009-08-13 21:57:51 +00001320 llvm::Value *Offset =
1321 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001322 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
1323 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
Owen Anderson0032b272009-08-13 21:57:51 +00001324 llvm::Type::getInt64Ty(CGF.getLLVMContext()));
1325 llvm::Value *Mask = llvm::ConstantInt::get(
1326 llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001327 overflow_arg_area =
1328 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1329 overflow_arg_area->getType(),
1330 "overflow_arg_area.align");
1331 }
1332
1333 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
1334 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1335 llvm::Value *Res =
1336 CGF.Builder.CreateBitCast(overflow_arg_area,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001337 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001338
1339 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
1340 // l->overflow_arg_area + sizeof(type).
1341 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
1342 // an 8 byte boundary.
1343
1344 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
Owen Anderson0032b272009-08-13 21:57:51 +00001345 llvm::Value *Offset =
1346 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001347 (SizeInBytes + 7) & ~7);
1348 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
1349 "overflow_arg_area.next");
1350 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
1351
1352 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
1353 return Res;
1354}
1355
1356llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1357 CodeGenFunction &CGF) const {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001358 llvm::LLVMContext &VMContext = CGF.getLLVMContext();
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001359 const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext);
1360 const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext);
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001362 // Assume that va_list type is correct; should be pointer to LLVM type:
1363 // struct {
1364 // i32 gp_offset;
1365 // i32 fp_offset;
1366 // i8* overflow_arg_area;
1367 // i8* reg_save_area;
1368 // };
1369 unsigned neededInt, neededSSE;
Chris Lattnera14db752010-03-11 18:19:55 +00001370
1371 Ty = CGF.getContext().getCanonicalType(Ty);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001372 ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001373 neededInt, neededSSE);
1374
1375 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
1376 // in the registers. If not go to step 7.
1377 if (!neededInt && !neededSSE)
1378 return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1379
1380 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1381 // general purpose registers needed to pass type and num_fp to hold
1382 // the number of floating point registers needed.
1383
1384 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1385 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1386 // l->fp_offset > 304 - num_fp * 16 go to step 7.
1387 //
1388 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1389 // register save space).
1390
1391 llvm::Value *InRegs = 0;
1392 llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1393 llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1394 if (neededInt) {
1395 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1396 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1397 InRegs =
1398 CGF.Builder.CreateICmpULE(gp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001399 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001400 48 - neededInt * 8),
1401 "fits_in_gp");
1402 }
1403
1404 if (neededSSE) {
1405 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1406 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1407 llvm::Value *FitsInFP =
1408 CGF.Builder.CreateICmpULE(fp_offset,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001409 llvm::ConstantInt::get(i32Ty,
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001410 176 - neededSSE * 16),
1411 "fits_in_fp");
1412 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1413 }
1414
1415 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1416 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1417 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1418 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1419
1420 // Emit code to load the value if it was passed in registers.
1421
1422 CGF.EmitBlock(InRegBlock);
1423
1424 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1425 // an offset of l->gp_offset and/or l->fp_offset. This may require
1426 // copying to a temporary location in case the parameter is passed
1427 // in different register classes or requires an alignment greater
1428 // than 8 for general purpose registers and 16 for XMM registers.
1429 //
1430 // FIXME: This really results in shameful code when we end up needing to
1431 // collect arguments from different places; often what should result in a
1432 // simple assembling of a structure from scattered addresses has many more
1433 // loads than necessary. Can we clean this up?
1434 const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1435 llvm::Value *RegAddr =
1436 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1437 "reg_save_area");
1438 if (neededInt && neededSSE) {
1439 // FIXME: Cleanup.
1440 assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1441 const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1442 llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1443 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1444 const llvm::Type *TyLo = ST->getElementType(0);
1445 const llvm::Type *TyHi = ST->getElementType(1);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001446 assert((TyLo->isFloatingPointTy() ^ TyHi->isFloatingPointTy()) &&
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001447 "Unexpected ABI info for mixed regs");
Owen Anderson96e0fc72009-07-29 22:16:19 +00001448 const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1449 const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001450 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1451 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
Duncan Sandsf177d9d2010-02-15 16:14:01 +00001452 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
1453 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001454 llvm::Value *V =
1455 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1456 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1457 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1458 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1459
Owen Andersona1cf15f2009-07-14 23:10:40 +00001460 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001461 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001462 } else if (neededInt) {
1463 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1464 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001465 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001466 } else {
1467 if (neededSSE == 1) {
1468 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1469 RegAddr = CGF.Builder.CreateBitCast(RegAddr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001470 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001471 } else {
1472 assert(neededSSE == 2 && "Invalid number of needed registers!");
1473 // SSE registers are spaced 16 bytes apart in the register save
1474 // area, we need to collect the two eightbytes together.
1475 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1476 llvm::Value *RegAddrHi =
1477 CGF.Builder.CreateGEP(RegAddrLo,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001478 llvm::ConstantInt::get(i32Ty, 16));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001479 const llvm::Type *DblPtrTy =
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001480 llvm::PointerType::getUnqual(DoubleTy);
1481 const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy,
1482 DoubleTy, NULL);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001483 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1484 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1485 DblPtrTy));
1486 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1487 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1488 DblPtrTy));
1489 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1490 RegAddr = CGF.Builder.CreateBitCast(Tmp,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001491 llvm::PointerType::getUnqual(LTy));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001492 }
1493 }
1494
1495 // AMD64-ABI 3.5.7p5: Step 5. Set:
1496 // l->gp_offset = l->gp_offset + num_gp * 8
1497 // l->fp_offset = l->fp_offset + num_fp * 16.
1498 if (neededInt) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001499 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001500 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1501 gp_offset_p);
1502 }
1503 if (neededSSE) {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001504 llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001505 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1506 fp_offset_p);
1507 }
1508 CGF.EmitBranch(ContBlock);
1509
1510 // Emit code to load the value if it was passed in memory.
1511
1512 CGF.EmitBlock(InMemBlock);
1513 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1514
1515 // Return the appropriate result.
1516
1517 CGF.EmitBlock(ContBlock);
1518 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1519 "vaarg.addr");
1520 ResAddr->reserveOperandSpace(2);
1521 ResAddr->addIncoming(RegAddr, InRegBlock);
1522 ResAddr->addIncoming(MemAddr, InMemBlock);
1523
1524 return ResAddr;
1525}
1526
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001527// PIC16 ABI Implementation
1528
1529namespace {
1530
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001531class PIC16ABIInfo : public ABIInfo {
1532 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001533 ASTContext &Context,
1534 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001535
1536 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001537 ASTContext &Context,
1538 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001539
Owen Andersona1cf15f2009-07-14 23:10:40 +00001540 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1541 llvm::LLVMContext &VMContext) const {
1542 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
1543 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001544 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1545 it != ie; ++it)
Owen Andersona1cf15f2009-07-14 23:10:40 +00001546 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001547 }
1548
1549 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1550 CodeGenFunction &CGF) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001551};
1552
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001553class PIC16TargetCodeGenInfo : public TargetCodeGenInfo {
1554public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001555 PIC16TargetCodeGenInfo():TargetCodeGenInfo(new PIC16ABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001556};
1557
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001558}
1559
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001560ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001561 ASTContext &Context,
1562 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001563 if (RetTy->isVoidType()) {
1564 return ABIArgInfo::getIgnore();
1565 } else {
1566 return ABIArgInfo::getDirect();
1567 }
1568}
1569
1570ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001571 ASTContext &Context,
1572 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001573 return ABIArgInfo::getDirect();
1574}
1575
1576llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1577 CodeGenFunction &CGF) const {
Chris Lattner52d9ae32010-04-06 17:29:22 +00001578 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001579 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
1580
1581 CGBuilderTy &Builder = CGF.Builder;
1582 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1583 "ap");
1584 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1585 llvm::Type *PTy =
1586 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1587 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1588
1589 uint64_t Offset = CGF.getContext().getTypeSize(Ty) / 8;
1590
1591 llvm::Value *NextAddr =
1592 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1593 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
1594 "ap.next");
1595 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1596
1597 return AddrTyped;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001598}
1599
Sanjiv Guptaa446ecd2010-02-17 02:25:52 +00001600
John McCallec853ba2010-03-11 00:10:12 +00001601// PowerPC-32
1602
1603namespace {
1604class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
1605public:
1606 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1607 // This is recovered from gcc output.
1608 return 1; // r1 is the dedicated stack pointer
1609 }
1610
1611 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1612 llvm::Value *Address) const;
1613};
1614
1615}
1616
1617bool
1618PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1619 llvm::Value *Address) const {
1620 // This is calculated from the LLVM and GCC tables and verified
1621 // against gcc output. AFAIK all ABIs use the same encoding.
1622
1623 CodeGen::CGBuilderTy &Builder = CGF.Builder;
1624 llvm::LLVMContext &Context = CGF.getLLVMContext();
1625
1626 const llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context);
1627 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
1628 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
1629 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
1630
1631 // 0-31: r0-31, the 4-byte general-purpose registers
1632 for (unsigned I = 0, E = 32; I != E; ++I) {
1633 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1634 Builder.CreateStore(Four8, Slot);
1635 }
1636
1637 // 32-63: fp0-31, the 8-byte floating-point registers
1638 for (unsigned I = 32, E = 64; I != E; ++I) {
1639 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1640 Builder.CreateStore(Eight8, Slot);
1641 }
1642
1643 // 64-76 are various 4-byte special-purpose registers:
1644 // 64: mq
1645 // 65: lr
1646 // 66: ctr
1647 // 67: ap
1648 // 68-75 cr0-7
1649 // 76: xer
1650 for (unsigned I = 64, E = 77; I != E; ++I) {
1651 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1652 Builder.CreateStore(Four8, Slot);
1653 }
1654
1655 // 77-108: v0-31, the 16-byte vector registers
1656 for (unsigned I = 77, E = 109; I != E; ++I) {
1657 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1658 Builder.CreateStore(Sixteen8, Slot);
1659 }
1660
1661 // 109: vrsave
1662 // 110: vscr
1663 // 111: spe_acc
1664 // 112: spefscr
1665 // 113: sfp
1666 for (unsigned I = 109, E = 114; I != E; ++I) {
1667 llvm::Value *Slot = Builder.CreateConstInBoundsGEP1_32(Address, I);
1668 Builder.CreateStore(Four8, Slot);
1669 }
1670
1671 return false;
1672}
1673
1674
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001675// ARM ABI Implementation
1676
1677namespace {
1678
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001679class ARMABIInfo : public ABIInfo {
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001680public:
1681 enum ABIKind {
1682 APCS = 0,
1683 AAPCS = 1,
1684 AAPCS_VFP
1685 };
1686
1687private:
1688 ABIKind Kind;
1689
1690public:
1691 ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {}
1692
1693private:
1694 ABIKind getABIKind() const { return Kind; }
1695
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001696 ABIArgInfo classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001697 ASTContext &Context,
1698 llvm::LLVMContext &VMCOntext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001699
1700 ABIArgInfo classifyArgumentType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001701 ASTContext &Context,
1702 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001703
Owen Andersona1cf15f2009-07-14 23:10:40 +00001704 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1705 llvm::LLVMContext &VMContext) const;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001706
1707 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1708 CodeGenFunction &CGF) const;
1709};
1710
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001711class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
1712public:
1713 ARMTargetCodeGenInfo(ARMABIInfo::ABIKind K)
Douglas Gregor568bb2d2010-01-22 15:41:14 +00001714 :TargetCodeGenInfo(new ARMABIInfo(K)) {}
John McCall6374c332010-03-06 00:35:14 +00001715
1716 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
1717 return 13;
1718 }
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001719};
1720
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001721}
1722
Owen Andersona1cf15f2009-07-14 23:10:40 +00001723void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1724 llvm::LLVMContext &VMContext) const {
Mike Stump1eb44332009-09-09 15:08:12 +00001725 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001726 VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001727 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1728 it != ie; ++it) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001729 it->info = classifyArgumentType(it->type, Context, VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001730 }
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00001731
1732 // ARM always overrides the calling convention.
1733 switch (getABIKind()) {
1734 case APCS:
1735 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
1736 break;
1737
1738 case AAPCS:
1739 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
1740 break;
1741
1742 case AAPCS_VFP:
1743 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
1744 break;
1745 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001746}
1747
1748ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001749 ASTContext &Context,
1750 llvm::LLVMContext &VMContext) const {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001751 if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1752 // Treat an enum type as its underlying type.
1753 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1754 Ty = EnumTy->getDecl()->getIntegerType();
1755
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001756 return (Ty->isPromotableIntegerType() ?
1757 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001758 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001759
Daniel Dunbar42025572009-09-14 21:54:03 +00001760 // Ignore empty records.
1761 if (isEmptyRecord(Context, Ty, true))
1762 return ABIArgInfo::getIgnore();
1763
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001764 // FIXME: This is kind of nasty... but there isn't much choice because the ARM
1765 // backend doesn't support byval.
1766 // FIXME: This doesn't handle alignment > 64 bits.
1767 const llvm::Type* ElemTy;
1768 unsigned SizeRegs;
1769 if (Context.getTypeAlign(Ty) > 32) {
Owen Anderson0032b272009-08-13 21:57:51 +00001770 ElemTy = llvm::Type::getInt64Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001771 SizeRegs = (Context.getTypeSize(Ty) + 63) / 64;
1772 } else {
Owen Anderson0032b272009-08-13 21:57:51 +00001773 ElemTy = llvm::Type::getInt32Ty(VMContext);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001774 SizeRegs = (Context.getTypeSize(Ty) + 31) / 32;
1775 }
1776 std::vector<const llvm::Type*> LLVMFields;
Owen Anderson96e0fc72009-07-29 22:16:19 +00001777 LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs));
Owen Anderson47a434f2009-08-05 23:18:46 +00001778 const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001779 return ABIArgInfo::getCoerce(STy);
1780}
1781
Daniel Dunbar98303b92009-09-13 08:03:58 +00001782static bool isIntegerLikeType(QualType Ty,
1783 ASTContext &Context,
1784 llvm::LLVMContext &VMContext) {
1785 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
1786 // is called integer-like if its size is less than or equal to one word, and
1787 // the offset of each of its addressable sub-fields is zero.
1788
1789 uint64_t Size = Context.getTypeSize(Ty);
1790
1791 // Check that the type fits in a word.
1792 if (Size > 32)
1793 return false;
1794
1795 // FIXME: Handle vector types!
1796 if (Ty->isVectorType())
1797 return false;
1798
Daniel Dunbarb0d58192009-09-14 02:20:34 +00001799 // Float types are never treated as "integer like".
1800 if (Ty->isRealFloatingType())
1801 return false;
1802
Daniel Dunbar98303b92009-09-13 08:03:58 +00001803 // If this is a builtin or pointer type then it is ok.
John McCall183700f2009-09-21 23:43:11 +00001804 if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
Daniel Dunbar98303b92009-09-13 08:03:58 +00001805 return true;
1806
Daniel Dunbar45815812010-02-01 23:31:26 +00001807 // Small complex integer types are "integer like".
1808 if (const ComplexType *CT = Ty->getAs<ComplexType>())
1809 return isIntegerLikeType(CT->getElementType(), Context, VMContext);
Daniel Dunbar98303b92009-09-13 08:03:58 +00001810
1811 // Single element and zero sized arrays should be allowed, by the definition
1812 // above, but they are not.
1813
1814 // Otherwise, it must be a record type.
1815 const RecordType *RT = Ty->getAs<RecordType>();
1816 if (!RT) return false;
1817
1818 // Ignore records with flexible arrays.
1819 const RecordDecl *RD = RT->getDecl();
1820 if (RD->hasFlexibleArrayMember())
1821 return false;
1822
1823 // Check that all sub-fields are at offset 0, and are themselves "integer
1824 // like".
1825 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1826
1827 bool HadField = false;
1828 unsigned idx = 0;
1829 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1830 i != e; ++i, ++idx) {
1831 const FieldDecl *FD = *i;
1832
Daniel Dunbar679855a2010-01-29 03:22:29 +00001833 // Bit-fields are not addressable, we only need to verify they are "integer
1834 // like". We still have to disallow a subsequent non-bitfield, for example:
1835 // struct { int : 0; int x }
1836 // is non-integer like according to gcc.
1837 if (FD->isBitField()) {
1838 if (!RD->isUnion())
1839 HadField = true;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001840
Daniel Dunbar679855a2010-01-29 03:22:29 +00001841 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1842 return false;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001843
Daniel Dunbar679855a2010-01-29 03:22:29 +00001844 continue;
Daniel Dunbar98303b92009-09-13 08:03:58 +00001845 }
1846
Daniel Dunbar679855a2010-01-29 03:22:29 +00001847 // Check if this field is at offset 0.
1848 if (Layout.getFieldOffset(idx) != 0)
1849 return false;
1850
Daniel Dunbar98303b92009-09-13 08:03:58 +00001851 if (!isIntegerLikeType(FD->getType(), Context, VMContext))
1852 return false;
1853
Daniel Dunbar679855a2010-01-29 03:22:29 +00001854 // Only allow at most one field in a structure. This doesn't match the
1855 // wording above, but follows gcc in situations with a field following an
1856 // empty structure.
Daniel Dunbar98303b92009-09-13 08:03:58 +00001857 if (!RD->isUnion()) {
1858 if (HadField)
1859 return false;
1860
1861 HadField = true;
1862 }
1863 }
1864
1865 return true;
1866}
1867
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001868ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001869 ASTContext &Context,
1870 llvm::LLVMContext &VMContext) const {
Daniel Dunbar98303b92009-09-13 08:03:58 +00001871 if (RetTy->isVoidType())
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001872 return ABIArgInfo::getIgnore();
Daniel Dunbar98303b92009-09-13 08:03:58 +00001873
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001874 if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1875 // Treat an enum type as its underlying type.
1876 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1877 RetTy = EnumTy->getDecl()->getIntegerType();
1878
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001879 return (RetTy->isPromotableIntegerType() ?
1880 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001881 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001882
1883 // Are we following APCS?
1884 if (getABIKind() == APCS) {
1885 if (isEmptyRecord(Context, RetTy, false))
1886 return ABIArgInfo::getIgnore();
1887
Daniel Dunbar4cc753f2010-02-01 23:31:19 +00001888 // Complex types are all returned as packed integers.
1889 //
1890 // FIXME: Consider using 2 x vector types if the back end handles them
1891 // correctly.
1892 if (RetTy->isAnyComplexType())
1893 return ABIArgInfo::getCoerce(llvm::IntegerType::get(
1894 VMContext, Context.getTypeSize(RetTy)));
1895
Daniel Dunbar98303b92009-09-13 08:03:58 +00001896 // Integer like structures are returned in r0.
1897 if (isIntegerLikeType(RetTy, Context, VMContext)) {
1898 // Return in the smallest viable integer type.
1899 uint64_t Size = Context.getTypeSize(RetTy);
1900 if (Size <= 8)
1901 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1902 if (Size <= 16)
1903 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
1904 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
1905 }
1906
1907 // Otherwise return in memory.
1908 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001909 }
Daniel Dunbar98303b92009-09-13 08:03:58 +00001910
1911 // Otherwise this is an AAPCS variant.
1912
Daniel Dunbar16a08082009-09-14 00:56:55 +00001913 if (isEmptyRecord(Context, RetTy, true))
1914 return ABIArgInfo::getIgnore();
1915
Daniel Dunbar98303b92009-09-13 08:03:58 +00001916 // Aggregates <= 4 bytes are returned in r0; other aggregates
1917 // are returned indirectly.
1918 uint64_t Size = Context.getTypeSize(RetTy);
Daniel Dunbar16a08082009-09-14 00:56:55 +00001919 if (Size <= 32) {
1920 // Return in the smallest viable integer type.
1921 if (Size <= 8)
1922 return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext));
1923 if (Size <= 16)
1924 return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext));
Daniel Dunbar98303b92009-09-13 08:03:58 +00001925 return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext));
Daniel Dunbar16a08082009-09-14 00:56:55 +00001926 }
1927
Daniel Dunbar98303b92009-09-13 08:03:58 +00001928 return ABIArgInfo::getIndirect(0);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001929}
1930
1931llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1932 CodeGenFunction &CGF) const {
1933 // FIXME: Need to handle alignment
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001934 const llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001935 const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001936
1937 CGBuilderTy &Builder = CGF.Builder;
1938 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
1939 "ap");
1940 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
1941 llvm::Type *PTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001942 llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001943 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1944
1945 uint64_t Offset =
1946 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
1947 llvm::Value *NextAddr =
Owen Anderson0032b272009-08-13 21:57:51 +00001948 Builder.CreateGEP(Addr, llvm::ConstantInt::get(
1949 llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset),
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001950 "ap.next");
1951 Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1952
1953 return AddrTyped;
1954}
1955
1956ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001957 ASTContext &Context,
1958 llvm::LLVMContext &VMContext) const {
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001959 if (RetTy->isVoidType()) {
1960 return ABIArgInfo::getIgnore();
1961 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1962 return ABIArgInfo::getIndirect(0);
1963 } else {
Douglas Gregoraa74a1e2010-02-02 20:10:50 +00001964 // Treat an enum type as its underlying type.
1965 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1966 RetTy = EnumTy->getDecl()->getIntegerType();
1967
Anton Korobeynikovcc6fa882009-06-06 09:36:29 +00001968 return (RetTy->isPromotableIntegerType() ?
1969 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00001970 }
1971}
1972
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001973// SystemZ ABI Implementation
1974
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001975namespace {
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001976
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00001977class SystemZABIInfo : public ABIInfo {
1978 bool isPromotableIntegerType(QualType Ty) const;
1979
1980 ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context,
1981 llvm::LLVMContext &VMContext) const;
1982
1983 ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context,
1984 llvm::LLVMContext &VMContext) const;
1985
1986 virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context,
1987 llvm::LLVMContext &VMContext) const {
1988 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
1989 Context, VMContext);
1990 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
1991 it != ie; ++it)
1992 it->info = classifyArgumentType(it->type, Context, VMContext);
1993 }
1994
1995 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1996 CodeGenFunction &CGF) const;
1997};
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00001998
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001999class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
2000public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002001 SystemZTargetCodeGenInfo():TargetCodeGenInfo(new SystemZABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002002};
2003
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002004}
2005
2006bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
2007 // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended.
John McCall183700f2009-09-21 23:43:11 +00002008 if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002009 switch (BT->getKind()) {
2010 case BuiltinType::Bool:
2011 case BuiltinType::Char_S:
2012 case BuiltinType::Char_U:
2013 case BuiltinType::SChar:
2014 case BuiltinType::UChar:
2015 case BuiltinType::Short:
2016 case BuiltinType::UShort:
2017 case BuiltinType::Int:
2018 case BuiltinType::UInt:
2019 return true;
2020 default:
2021 return false;
2022 }
2023 return false;
2024}
2025
2026llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2027 CodeGenFunction &CGF) const {
2028 // FIXME: Implement
2029 return 0;
2030}
2031
2032
2033ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy,
2034 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002035 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002036 if (RetTy->isVoidType()) {
2037 return ABIArgInfo::getIgnore();
2038 } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2039 return ABIArgInfo::getIndirect(0);
2040 } else {
2041 return (isPromotableIntegerType(RetTy) ?
2042 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2043 }
2044}
2045
2046ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty,
2047 ASTContext &Context,
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002048 llvm::LLVMContext &VMContext) const {
Anton Korobeynikov89e887f2009-07-16 20:09:57 +00002049 if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
2050 return ABIArgInfo::getIndirect(0);
2051 } else {
2052 return (isPromotableIntegerType(Ty) ?
2053 ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2054 }
2055}
2056
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002057// MSP430 ABI Implementation
2058
2059namespace {
2060
2061class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
2062public:
Douglas Gregor568bb2d2010-01-22 15:41:14 +00002063 MSP430TargetCodeGenInfo():TargetCodeGenInfo(new DefaultABIInfo()) {}
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002064 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2065 CodeGen::CodeGenModule &M) const;
2066};
2067
2068}
2069
2070void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2071 llvm::GlobalValue *GV,
2072 CodeGen::CodeGenModule &M) const {
2073 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2074 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
2075 // Handle 'interrupt' attribute:
2076 llvm::Function *F = cast<llvm::Function>(GV);
2077
2078 // Step 1: Set ISR calling convention.
2079 F->setCallingConv(llvm::CallingConv::MSP430_INTR);
2080
2081 // Step 2: Add attributes goodness.
2082 F->addFnAttr(llvm::Attribute::NoInline);
2083
2084 // Step 3: Emit ISR vector alias.
2085 unsigned Num = attr->getNumber() + 0xffe0;
2086 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
2087 "vector_" +
2088 llvm::LowercaseString(llvm::utohexstr(Num)),
2089 GV, &M.getModule());
2090 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002091 }
2092}
2093
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002094const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() const {
2095 if (TheTargetCodeGenInfo)
2096 return *TheTargetCodeGenInfo;
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002097
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002098 // For now we just cache the TargetCodeGenInfo in CodeGenModule and don't
2099 // free it.
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002100
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002101 const llvm::Triple &Triple(getContext().Target.getTriple());
2102 switch (Triple.getArch()) {
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002103 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002104 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo);
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002105
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002106 case llvm::Triple::arm:
2107 case llvm::Triple::thumb:
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002108 // FIXME: We want to know the float calling convention as well.
Daniel Dunbar018ba5a2009-09-14 00:35:03 +00002109 if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0)
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002110 return *(TheTargetCodeGenInfo =
2111 new ARMTargetCodeGenInfo(ARMABIInfo::APCS));
Daniel Dunbar5e7bace2009-09-12 01:00:39 +00002112
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002113 return *(TheTargetCodeGenInfo =
2114 new ARMTargetCodeGenInfo(ARMABIInfo::AAPCS));
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002115
2116 case llvm::Triple::pic16:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002117 return *(TheTargetCodeGenInfo = new PIC16TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002118
John McCallec853ba2010-03-11 00:10:12 +00002119 case llvm::Triple::ppc:
2120 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo());
2121
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002122 case llvm::Triple::systemz:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002123 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo());
2124
2125 case llvm::Triple::msp430:
2126 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo());
Daniel Dunbar34d91fd2009-09-12 00:59:49 +00002127
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002128 case llvm::Triple::x86:
Daniel Dunbar1752ee42009-08-24 09:10:05 +00002129 switch (Triple.getOS()) {
Edward O'Callaghan7ee68bd2009-10-20 17:22:50 +00002130 case llvm::Triple::Darwin:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002131 return *(TheTargetCodeGenInfo =
2132 new X86_32TargetCodeGenInfo(Context, true, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002133 case llvm::Triple::Cygwin:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002134 case llvm::Triple::MinGW32:
2135 case llvm::Triple::MinGW64:
Edward O'Callaghan727e2682009-10-21 11:58:24 +00002136 case llvm::Triple::AuroraUX:
2137 case llvm::Triple::DragonFly:
David Chisnall75c135a2009-09-03 01:48:05 +00002138 case llvm::Triple::FreeBSD:
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002139 case llvm::Triple::OpenBSD:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002140 return *(TheTargetCodeGenInfo =
2141 new X86_32TargetCodeGenInfo(Context, false, true));
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002142
2143 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002144 return *(TheTargetCodeGenInfo =
2145 new X86_32TargetCodeGenInfo(Context, false, false));
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002146 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002147
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002148 case llvm::Triple::x86_64:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002149 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo());
Daniel Dunbar2c0843f2009-08-24 08:52:16 +00002150 }
Anton Korobeynikovc4a59eb2009-06-05 22:08:42 +00002151}