| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1 | //===---- TargetABIInfo.cpp - Encapsulate target ABI details ----*- C++ -*-===// | 
|  | 2 | // | 
|  | 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 |  | 
|  | 15 | #include "ABIInfo.h" | 
|  | 16 | #include "CodeGenFunction.h" | 
| Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" | 
| Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 20 | #include <cstdio> | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | using namespace clang; | 
|  | 23 | using namespace CodeGen; | 
|  | 24 |  | 
|  | 25 | ABIInfo::~ABIInfo() {} | 
|  | 26 |  | 
|  | 27 | void ABIArgInfo::dump() const { | 
|  | 28 | fprintf(stderr, "(ABIArgInfo Kind="); | 
|  | 29 | switch (TheKind) { | 
|  | 30 | case Direct: | 
|  | 31 | fprintf(stderr, "Direct"); | 
|  | 32 | break; | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 33 | case Extend: | 
|  | 34 | fprintf(stderr, "Extend"); | 
|  | 35 | break; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 36 | case Ignore: | 
|  | 37 | fprintf(stderr, "Ignore"); | 
|  | 38 | break; | 
|  | 39 | case Coerce: | 
|  | 40 | fprintf(stderr, "Coerce Type="); | 
|  | 41 | getCoerceToType()->print(llvm::errs()); | 
|  | 42 | break; | 
|  | 43 | case Indirect: | 
|  | 44 | fprintf(stderr, "Indirect Align=%d", getIndirectAlign()); | 
|  | 45 | break; | 
|  | 46 | case Expand: | 
|  | 47 | fprintf(stderr, "Expand"); | 
|  | 48 | break; | 
|  | 49 | } | 
|  | 50 | fprintf(stderr, ")\n"); | 
|  | 51 | } | 
|  | 52 |  | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 53 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 54 |  | 
|  | 55 | /// isEmptyField - Return true iff a the field is "empty", that is it | 
|  | 56 | /// is an unnamed bit-field or an (array of) empty record(s). | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 57 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, | 
|  | 58 | bool AllowArrays) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 59 | if (FD->isUnnamedBitfield()) | 
|  | 60 | return true; | 
|  | 61 |  | 
|  | 62 | QualType FT = FD->getType(); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 63 |  | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 64 | // Constant arrays of empty records count as empty, strip them off. | 
|  | 65 | if (AllowArrays) | 
|  | 66 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) | 
|  | 67 | FT = AT->getElementType(); | 
|  | 68 |  | 
|  | 69 | return isEmptyRecord(Context, FT, AllowArrays); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
|  | 72 | /// isEmptyRecord - Return true iff a structure contains only empty | 
|  | 73 | /// fields. Note that a structure with a flexible array member is not | 
|  | 74 | /// considered empty. | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 75 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 76 | const RecordType *RT = T->getAs<RecordType>(); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 77 | if (!RT) | 
|  | 78 | return 0; | 
|  | 79 | const RecordDecl *RD = RT->getDecl(); | 
|  | 80 | if (RD->hasFlexibleArrayMember()) | 
|  | 81 | return false; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 82 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); | 
|  | 83 | i != e; ++i) | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 84 | if (!isEmptyField(Context, *i, AllowArrays)) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 85 | return false; | 
|  | 86 | return true; | 
|  | 87 | } | 
|  | 88 |  | 
| Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 89 | /// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either | 
|  | 90 | /// a non-trivial destructor or a non-trivial copy constructor. | 
|  | 91 | static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) { | 
|  | 92 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 93 | if (!RD) | 
|  | 94 | return false; | 
|  | 95 |  | 
|  | 96 | return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor(); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | /// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is | 
|  | 100 | /// a record type with either a non-trivial destructor or a non-trivial copy | 
|  | 101 | /// constructor. | 
|  | 102 | static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) { | 
|  | 103 | const RecordType *RT = T->getAs<RecordType>(); | 
|  | 104 | if (!RT) | 
|  | 105 | return false; | 
|  | 106 |  | 
|  | 107 | return hasNonTrivialDestructorOrCopyConstructor(RT); | 
|  | 108 | } | 
|  | 109 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 110 | /// isSingleElementStruct - Determine if a structure is a "single | 
|  | 111 | /// element struct", i.e. it has exactly one non-empty field or | 
|  | 112 | /// exactly one field which is itself a single element | 
|  | 113 | /// struct. Structures with flexible array members are never | 
|  | 114 | /// considered single element structs. | 
|  | 115 | /// | 
|  | 116 | /// \return The field declaration for the single non-empty field, if | 
|  | 117 | /// it exists. | 
|  | 118 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { | 
|  | 119 | const RecordType *RT = T->getAsStructureType(); | 
|  | 120 | if (!RT) | 
|  | 121 | return 0; | 
|  | 122 |  | 
|  | 123 | const RecordDecl *RD = RT->getDecl(); | 
|  | 124 | if (RD->hasFlexibleArrayMember()) | 
|  | 125 | return 0; | 
|  | 126 |  | 
|  | 127 | const Type *Found = 0; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 128 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); | 
|  | 129 | i != e; ++i) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 130 | const FieldDecl *FD = *i; | 
|  | 131 | QualType FT = FD->getType(); | 
|  | 132 |  | 
|  | 133 | // Ignore empty fields. | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 134 | if (isEmptyField(Context, FD, true)) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 135 | continue; | 
|  | 136 |  | 
|  | 137 | // If we already found an element then this isn't a single-element | 
|  | 138 | // struct. | 
|  | 139 | if (Found) | 
|  | 140 | return 0; | 
|  | 141 |  | 
|  | 142 | // Treat single element arrays as the element. | 
|  | 143 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { | 
|  | 144 | if (AT->getSize().getZExtValue() != 1) | 
|  | 145 | break; | 
|  | 146 | FT = AT->getElementType(); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | if (!CodeGenFunction::hasAggregateLLVMType(FT)) { | 
|  | 150 | Found = FT.getTypePtr(); | 
|  | 151 | } else { | 
|  | 152 | Found = isSingleElementStruct(FT, Context); | 
|  | 153 | if (!Found) | 
|  | 154 | return 0; | 
|  | 155 | } | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | return Found; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { | 
| Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 162 | if (!Ty->getAs<BuiltinType>() && !Ty->isAnyPointerType() && | 
|  | 163 | !Ty->isAnyComplexType() && !Ty->isEnumeralType() && | 
|  | 164 | !Ty->isBlockPointerType()) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 165 | return false; | 
|  | 166 |  | 
|  | 167 | uint64_t Size = Context.getTypeSize(Ty); | 
|  | 168 | return Size == 32 || Size == 64; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, | 
|  | 172 | ASTContext &Context) { | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 173 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); | 
|  | 174 | i != e; ++i) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 175 | const FieldDecl *FD = *i; | 
|  | 176 |  | 
|  | 177 | if (!is32Or64BitBasicType(FD->getType(), Context)) | 
|  | 178 | return false; | 
|  | 179 |  | 
|  | 180 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know | 
|  | 181 | // how to expand them yet, and the predicate for telling if a bitfield still | 
|  | 182 | // counts as "basic" is more complicated than what we were doing previously. | 
|  | 183 | if (FD->isBitField()) | 
|  | 184 | return false; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | return true; | 
|  | 188 | } | 
|  | 189 |  | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 190 | static bool typeContainsSSEVector(const RecordDecl *RD, ASTContext &Context) { | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 191 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); | 
|  | 192 | i != e; ++i) { | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 193 | const FieldDecl *FD = *i; | 
|  | 194 |  | 
|  | 195 | if (FD->getType()->isVectorType() && | 
|  | 196 | Context.getTypeSize(FD->getType()) >= 128) | 
|  | 197 | return true; | 
|  | 198 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 199 | if (const RecordType* RT = FD->getType()->getAs<RecordType>()) | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 200 | if (typeContainsSSEVector(RT->getDecl(), Context)) | 
|  | 201 | return true; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | return false; | 
|  | 205 | } | 
|  | 206 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 207 | namespace { | 
|  | 208 | /// DefaultABIInfo - The default implementation for ABI specific | 
|  | 209 | /// details. This implementation provides information which results in | 
|  | 210 | /// self-consistent and sensible LLVM IR generation, but does not | 
|  | 211 | /// conform to any particular ABI. | 
|  | 212 | class DefaultABIInfo : public ABIInfo { | 
|  | 213 | ABIArgInfo classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 214 | ASTContext &Context, | 
|  | 215 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 216 |  | 
|  | 217 | ABIArgInfo classifyArgumentType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 218 | ASTContext &Context, | 
|  | 219 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 220 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 221 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 222 | llvm::LLVMContext &VMContext) const { | 
|  | 223 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context, | 
|  | 224 | VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 225 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); | 
|  | 226 | it != ie; ++it) | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 227 | it->info = classifyArgumentType(it->type, Context, VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
|  | 230 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 231 | CodeGenFunction &CGF) const; | 
|  | 232 | }; | 
|  | 233 |  | 
|  | 234 | /// X86_32ABIInfo - The X86-32 ABI information. | 
|  | 235 | class X86_32ABIInfo : public ABIInfo { | 
|  | 236 | ASTContext &Context; | 
| David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 237 | bool IsDarwinVectorABI; | 
|  | 238 | bool IsSmallStructInRegABI; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 239 |  | 
|  | 240 | static bool isRegisterSize(unsigned Size) { | 
|  | 241 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context); | 
|  | 245 |  | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 246 | static unsigned getIndirectArgumentAlignment(QualType Ty, | 
|  | 247 | ASTContext &Context); | 
|  | 248 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 249 | public: | 
|  | 250 | ABIArgInfo classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 251 | ASTContext &Context, | 
|  | 252 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 253 |  | 
|  | 254 | ABIArgInfo classifyArgumentType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 255 | ASTContext &Context, | 
|  | 256 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 257 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 258 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 259 | llvm::LLVMContext &VMContext) const { | 
|  | 260 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context, | 
|  | 261 | VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 262 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); | 
|  | 263 | it != ie; ++it) | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 264 | it->info = classifyArgumentType(it->type, Context, VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
|  | 267 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 268 | CodeGenFunction &CGF) const; | 
|  | 269 |  | 
| David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 270 | X86_32ABIInfo(ASTContext &Context, bool d, bool p) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | : ABIInfo(), Context(Context), IsDarwinVectorABI(d), | 
| David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 272 | IsSmallStructInRegABI(p) {} | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 273 | }; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 |  | 
|  | 277 | /// shouldReturnTypeInRegister - Determine if the given type should be | 
|  | 278 | /// passed in a register (for the Darwin ABI). | 
|  | 279 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, | 
|  | 280 | ASTContext &Context) { | 
|  | 281 | uint64_t Size = Context.getTypeSize(Ty); | 
|  | 282 |  | 
|  | 283 | // Type must be register sized. | 
|  | 284 | if (!isRegisterSize(Size)) | 
|  | 285 | return false; | 
|  | 286 |  | 
|  | 287 | if (Ty->isVectorType()) { | 
|  | 288 | // 64- and 128- bit vectors inside structures are not returned in | 
|  | 289 | // registers. | 
|  | 290 | if (Size == 64 || Size == 128) | 
|  | 291 | return false; | 
|  | 292 |  | 
|  | 293 | return true; | 
|  | 294 | } | 
|  | 295 |  | 
| Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 296 | // If this is a builtin, pointer, enum, or complex type, it is ok. | 
|  | 297 | if (Ty->getAs<BuiltinType>() || Ty->isAnyPointerType() || | 
|  | 298 | Ty->isAnyComplexType() || Ty->isEnumeralType() || | 
|  | 299 | Ty->isBlockPointerType()) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 300 | return true; | 
|  | 301 |  | 
|  | 302 | // Arrays are treated like records. | 
|  | 303 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) | 
|  | 304 | return shouldReturnTypeInRegister(AT->getElementType(), Context); | 
|  | 305 |  | 
|  | 306 | // Otherwise, it must be a record type. | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 307 | const RecordType *RT = Ty->getAs<RecordType>(); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 308 | if (!RT) return false; | 
|  | 309 |  | 
|  | 310 | // Structure types are passed in register if all fields would be | 
|  | 311 | // passed in a register. | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 312 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), | 
|  | 313 | e = RT->getDecl()->field_end(); i != e; ++i) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 314 | const FieldDecl *FD = *i; | 
|  | 315 |  | 
|  | 316 | // Empty fields are ignored. | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 317 | if (isEmptyField(Context, FD, true)) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 318 | continue; | 
|  | 319 |  | 
|  | 320 | // Check fields recursively. | 
|  | 321 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) | 
|  | 322 | return false; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | return true; | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 329 | ASTContext &Context, | 
|  | 330 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 331 | if (RetTy->isVoidType()) { | 
|  | 332 | return ABIArgInfo::getIgnore(); | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 333 | } else if (const VectorType *VT = RetTy->getAs<VectorType>()) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 334 | // On Darwin, some vectors are returned in registers. | 
| David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 335 | if (IsDarwinVectorABI) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 336 | uint64_t Size = Context.getTypeSize(RetTy); | 
|  | 337 |  | 
|  | 338 | // 128-bit vectors are a special case; they are returned in | 
|  | 339 | // registers and we need to make sure to pick a type the LLVM | 
|  | 340 | // backend will like. | 
|  | 341 | if (Size == 128) | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 342 | return ABIArgInfo::getCoerce(llvm::VectorType::get( | 
|  | 343 | llvm::Type::getInt64Ty(VMContext), 2)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 344 |  | 
|  | 345 | // Always return in register if it fits in a general purpose | 
|  | 346 | // register, or if it is 64 bits and has a single element. | 
|  | 347 | if ((Size == 8 || Size == 16 || Size == 32) || | 
|  | 348 | (Size == 64 && VT->getNumElements() == 1)) | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 349 | return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 350 |  | 
|  | 351 | return ABIArgInfo::getIndirect(0); | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | return ABIArgInfo::getDirect(); | 
|  | 355 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { | 
|  | 356 | // Structures with flexible arrays are always indirect. | 
|  | 357 | if (const RecordType *RT = RetTy->getAsStructureType()) | 
|  | 358 | if (RT->getDecl()->hasFlexibleArrayMember()) | 
|  | 359 | return ABIArgInfo::getIndirect(0); | 
|  | 360 |  | 
| David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 361 | // If specified, structs and unions are always indirect. | 
|  | 362 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 363 | return ABIArgInfo::getIndirect(0); | 
|  | 364 |  | 
|  | 365 | // Classify "single element" structs as their element type. | 
|  | 366 | if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) { | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 367 | if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 368 | if (BT->isIntegerType()) { | 
|  | 369 | // We need to use the size of the structure, padding | 
|  | 370 | // bit-fields can adjust that to be larger than the single | 
|  | 371 | // element type. | 
|  | 372 | uint64_t Size = Context.getTypeSize(RetTy); | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 373 | return ABIArgInfo::getCoerce( | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 374 | llvm::IntegerType::get(VMContext, (unsigned) Size)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 375 | } else if (BT->getKind() == BuiltinType::Float) { | 
|  | 376 | assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) && | 
|  | 377 | "Unexpect single element structure size!"); | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 378 | return ABIArgInfo::getCoerce(llvm::Type::getFloatTy(VMContext)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 379 | } else if (BT->getKind() == BuiltinType::Double) { | 
|  | 380 | assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) && | 
|  | 381 | "Unexpect single element structure size!"); | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 382 | return ABIArgInfo::getCoerce(llvm::Type::getDoubleTy(VMContext)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 383 | } | 
|  | 384 | } else if (SeltTy->isPointerType()) { | 
|  | 385 | // FIXME: It would be really nice if this could come out as the proper | 
|  | 386 | // pointer type. | 
|  | 387 | llvm::Type *PtrTy = | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 388 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 389 | return ABIArgInfo::getCoerce(PtrTy); | 
|  | 390 | } else if (SeltTy->isVectorType()) { | 
|  | 391 | // 64- and 128-bit vectors are never returned in a | 
|  | 392 | // register when inside a structure. | 
|  | 393 | uint64_t Size = Context.getTypeSize(RetTy); | 
|  | 394 | if (Size == 64 || Size == 128) | 
|  | 395 | return ABIArgInfo::getIndirect(0); | 
|  | 396 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 397 | return classifyReturnType(QualType(SeltTy, 0), Context, VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 398 | } | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | // Small structures which are register sized are generally returned | 
|  | 402 | // in a register. | 
|  | 403 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) { | 
|  | 404 | uint64_t Size = Context.getTypeSize(RetTy); | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 405 | return ABIArgInfo::getCoerce(llvm::IntegerType::get(VMContext, Size)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 406 | } | 
|  | 407 |  | 
|  | 408 | return ABIArgInfo::getIndirect(0); | 
|  | 409 | } else { | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 410 | return (RetTy->isPromotableIntegerType() ? | 
|  | 411 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 412 | } | 
|  | 413 | } | 
|  | 414 |  | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 415 | unsigned X86_32ABIInfo::getIndirectArgumentAlignment(QualType Ty, | 
|  | 416 | ASTContext &Context) { | 
|  | 417 | unsigned Align = Context.getTypeAlign(Ty); | 
|  | 418 | if (Align < 128) return 0; | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 419 | if (const RecordType* RT = Ty->getAs<RecordType>()) | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 420 | if (typeContainsSSEVector(RT->getDecl(), Context)) | 
|  | 421 | return 16; | 
|  | 422 | return 0; | 
|  | 423 | } | 
|  | 424 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 425 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 426 | ASTContext &Context, | 
|  | 427 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 428 | // FIXME: Set alignment on indirect arguments. | 
|  | 429 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { | 
|  | 430 | // Structures with flexible arrays are always indirect. | 
|  | 431 | if (const RecordType *RT = Ty->getAsStructureType()) | 
|  | 432 | if (RT->getDecl()->hasFlexibleArrayMember()) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 434 | Context)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 435 |  | 
|  | 436 | // Ignore empty structs. | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 437 | if (Ty->isStructureType() && Context.getTypeSize(Ty) == 0) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 438 | return ABIArgInfo::getIgnore(); | 
|  | 439 |  | 
|  | 440 | // Expand structs with size <= 128-bits which consist only of | 
|  | 441 | // basic types (int, long long, float, double, xxx*). This is | 
|  | 442 | // non-recursive and does not ignore empty fields. | 
|  | 443 | if (const RecordType *RT = Ty->getAsStructureType()) { | 
|  | 444 | if (Context.getTypeSize(Ty) <= 4*32 && | 
|  | 445 | areAllFields32Or64BitBasicType(RT->getDecl(), Context)) | 
|  | 446 | return ABIArgInfo::getExpand(); | 
|  | 447 | } | 
|  | 448 |  | 
| Eli Friedman | a1e6de9 | 2009-06-13 21:37:10 +0000 | [diff] [blame] | 449 | return ABIArgInfo::getIndirect(getIndirectArgumentAlignment(Ty, Context)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 450 | } else { | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 451 | return (Ty->isPromotableIntegerType() ? | 
|  | 452 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 453 | } | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 457 | CodeGenFunction &CGF) const { | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 458 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext())); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 459 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 460 |  | 
|  | 461 | CGBuilderTy &Builder = CGF.Builder; | 
|  | 462 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, | 
|  | 463 | "ap"); | 
|  | 464 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); | 
|  | 465 | llvm::Type *PTy = | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 466 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 467 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); | 
|  | 468 |  | 
|  | 469 | uint64_t Offset = | 
|  | 470 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); | 
|  | 471 | llvm::Value *NextAddr = | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 472 | Builder.CreateGEP(Addr, llvm::ConstantInt::get( | 
|  | 473 | llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset), | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 474 | "ap.next"); | 
|  | 475 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); | 
|  | 476 |  | 
|  | 477 | return AddrTyped; | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | namespace { | 
|  | 481 | /// X86_64ABIInfo - The X86_64 ABI information. | 
|  | 482 | class X86_64ABIInfo : public ABIInfo { | 
|  | 483 | enum Class { | 
|  | 484 | Integer = 0, | 
|  | 485 | SSE, | 
|  | 486 | SSEUp, | 
|  | 487 | X87, | 
|  | 488 | X87Up, | 
|  | 489 | ComplexX87, | 
|  | 490 | NoClass, | 
|  | 491 | Memory | 
|  | 492 | }; | 
|  | 493 |  | 
|  | 494 | /// merge - Implement the X86_64 ABI merging algorithm. | 
|  | 495 | /// | 
|  | 496 | /// Merge an accumulating classification \arg Accum with a field | 
|  | 497 | /// classification \arg Field. | 
|  | 498 | /// | 
|  | 499 | /// \param Accum - The accumulating classification. This should | 
|  | 500 | /// always be either NoClass or the result of a previous merge | 
|  | 501 | /// call. In addition, this should never be Memory (the caller | 
|  | 502 | /// should just return Memory for the aggregate). | 
|  | 503 | Class merge(Class Accum, Class Field) const; | 
|  | 504 |  | 
|  | 505 | /// classify - Determine the x86_64 register classes in which the | 
|  | 506 | /// given type T should be passed. | 
|  | 507 | /// | 
|  | 508 | /// \param Lo - The classification for the parts of the type | 
|  | 509 | /// residing in the low word of the containing object. | 
|  | 510 | /// | 
|  | 511 | /// \param Hi - The classification for the parts of the type | 
|  | 512 | /// residing in the high word of the containing object. | 
|  | 513 | /// | 
|  | 514 | /// \param OffsetBase - The bit offset of this type in the | 
|  | 515 | /// containing object.  Some parameters are classified different | 
|  | 516 | /// depending on whether they straddle an eightbyte boundary. | 
|  | 517 | /// | 
|  | 518 | /// If a word is unused its result will be NoClass; if a type should | 
|  | 519 | /// be passed in Memory then at least the classification of \arg Lo | 
|  | 520 | /// will be Memory. | 
|  | 521 | /// | 
|  | 522 | /// The \arg Lo class will be NoClass iff the argument is ignored. | 
|  | 523 | /// | 
|  | 524 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will | 
|  | 525 | /// also be ComplexX87. | 
|  | 526 | void classify(QualType T, ASTContext &Context, uint64_t OffsetBase, | 
|  | 527 | Class &Lo, Class &Hi) const; | 
|  | 528 |  | 
|  | 529 | /// getCoerceResult - Given a source type \arg Ty and an LLVM type | 
|  | 530 | /// to coerce to, chose the best way to pass Ty in the same place | 
|  | 531 | /// that \arg CoerceTo would be passed, but while keeping the | 
|  | 532 | /// emitted code as simple as possible. | 
|  | 533 | /// | 
|  | 534 | /// FIXME: Note, this should be cleaned up to just take an enumeration of all | 
|  | 535 | /// the ways we might want to pass things, instead of constructing an LLVM | 
|  | 536 | /// type. This makes this code more explicit, and it makes it clearer that we | 
|  | 537 | /// are also doing this for correctness in the case of passing scalar types. | 
|  | 538 | ABIArgInfo getCoerceResult(QualType Ty, | 
|  | 539 | const llvm::Type *CoerceTo, | 
|  | 540 | ASTContext &Context) const; | 
|  | 541 |  | 
|  | 542 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result | 
|  | 543 | /// such that the argument will be passed in memory. | 
|  | 544 | ABIArgInfo getIndirectResult(QualType Ty, | 
|  | 545 | ASTContext &Context) const; | 
|  | 546 |  | 
|  | 547 | ABIArgInfo classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 548 | ASTContext &Context, | 
|  | 549 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 550 |  | 
|  | 551 | ABIArgInfo classifyArgumentType(QualType Ty, | 
|  | 552 | ASTContext &Context, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 553 | llvm::LLVMContext &VMContext, | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 554 | unsigned &neededInt, | 
|  | 555 | unsigned &neededSSE) const; | 
|  | 556 |  | 
|  | 557 | public: | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 558 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 559 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 560 |  | 
|  | 561 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 562 | CodeGenFunction &CGF) const; | 
|  | 563 | }; | 
|  | 564 | } | 
|  | 565 |  | 
|  | 566 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, | 
|  | 567 | Class Field) const { | 
|  | 568 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is | 
|  | 569 | // classified recursively so that always two fields are | 
|  | 570 | // considered. The resulting class is calculated according to | 
|  | 571 | // the classes of the fields in the eightbyte: | 
|  | 572 | // | 
|  | 573 | // (a) If both classes are equal, this is the resulting class. | 
|  | 574 | // | 
|  | 575 | // (b) If one of the classes is NO_CLASS, the resulting class is | 
|  | 576 | // the other class. | 
|  | 577 | // | 
|  | 578 | // (c) If one of the classes is MEMORY, the result is the MEMORY | 
|  | 579 | // class. | 
|  | 580 | // | 
|  | 581 | // (d) If one of the classes is INTEGER, the result is the | 
|  | 582 | // INTEGER. | 
|  | 583 | // | 
|  | 584 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, | 
|  | 585 | // MEMORY is used as class. | 
|  | 586 | // | 
|  | 587 | // (f) Otherwise class SSE is used. | 
|  | 588 |  | 
|  | 589 | // Accum should never be memory (we should have returned) or | 
|  | 590 | // ComplexX87 (because this cannot be passed in a structure). | 
|  | 591 | assert((Accum != Memory && Accum != ComplexX87) && | 
|  | 592 | "Invalid accumulated classification during merge."); | 
|  | 593 | if (Accum == Field || Field == NoClass) | 
|  | 594 | return Accum; | 
|  | 595 | else if (Field == Memory) | 
|  | 596 | return Memory; | 
|  | 597 | else if (Accum == NoClass) | 
|  | 598 | return Field; | 
|  | 599 | else if (Accum == Integer || Field == Integer) | 
|  | 600 | return Integer; | 
|  | 601 | else if (Field == X87 || Field == X87Up || Field == ComplexX87 || | 
|  | 602 | Accum == X87 || Accum == X87Up) | 
|  | 603 | return Memory; | 
|  | 604 | else | 
|  | 605 | return SSE; | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | void X86_64ABIInfo::classify(QualType Ty, | 
|  | 609 | ASTContext &Context, | 
|  | 610 | uint64_t OffsetBase, | 
|  | 611 | Class &Lo, Class &Hi) const { | 
|  | 612 | // FIXME: This code can be simplified by introducing a simple value class for | 
|  | 613 | // Class pairs with appropriate constructor methods for the various | 
|  | 614 | // situations. | 
|  | 615 |  | 
|  | 616 | // FIXME: Some of the split computations are wrong; unaligned vectors | 
|  | 617 | // shouldn't be passed in registers for example, so there is no chance they | 
|  | 618 | // can straddle an eightbyte. Verify & simplify. | 
|  | 619 |  | 
|  | 620 | Lo = Hi = NoClass; | 
|  | 621 |  | 
|  | 622 | Class &Current = OffsetBase < 64 ? Lo : Hi; | 
|  | 623 | Current = Memory; | 
|  | 624 |  | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 625 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 626 | BuiltinType::Kind k = BT->getKind(); | 
|  | 627 |  | 
|  | 628 | if (k == BuiltinType::Void) { | 
|  | 629 | Current = NoClass; | 
|  | 630 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { | 
|  | 631 | Lo = Integer; | 
|  | 632 | Hi = Integer; | 
|  | 633 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { | 
|  | 634 | Current = Integer; | 
|  | 635 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { | 
|  | 636 | Current = SSE; | 
|  | 637 | } else if (k == BuiltinType::LongDouble) { | 
|  | 638 | Lo = X87; | 
|  | 639 | Hi = X87Up; | 
|  | 640 | } | 
|  | 641 | // FIXME: _Decimal32 and _Decimal64 are SSE. | 
|  | 642 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 643 | } else if (const EnumType *ET = Ty->getAs<EnumType>()) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 644 | // Classify the underlying integer type. | 
|  | 645 | classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi); | 
|  | 646 | } else if (Ty->hasPointerRepresentation()) { | 
|  | 647 | Current = Integer; | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 648 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 649 | uint64_t Size = Context.getTypeSize(VT); | 
|  | 650 | if (Size == 32) { | 
|  | 651 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x | 
|  | 652 | // float> as integer. | 
|  | 653 | Current = Integer; | 
|  | 654 |  | 
|  | 655 | // If this type crosses an eightbyte boundary, it should be | 
|  | 656 | // split. | 
|  | 657 | uint64_t EB_Real = (OffsetBase) / 64; | 
|  | 658 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; | 
|  | 659 | if (EB_Real != EB_Imag) | 
|  | 660 | Hi = Lo; | 
|  | 661 | } else if (Size == 64) { | 
|  | 662 | // gcc passes <1 x double> in memory. :( | 
|  | 663 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) | 
|  | 664 | return; | 
|  | 665 |  | 
|  | 666 | // gcc passes <1 x long long> as INTEGER. | 
|  | 667 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong)) | 
|  | 668 | Current = Integer; | 
|  | 669 | else | 
|  | 670 | Current = SSE; | 
|  | 671 |  | 
|  | 672 | // If this type crosses an eightbyte boundary, it should be | 
|  | 673 | // split. | 
|  | 674 | if (OffsetBase && OffsetBase != 64) | 
|  | 675 | Hi = Lo; | 
|  | 676 | } else if (Size == 128) { | 
|  | 677 | Lo = SSE; | 
|  | 678 | Hi = SSEUp; | 
|  | 679 | } | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 680 | } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 681 | QualType ET = Context.getCanonicalType(CT->getElementType()); | 
|  | 682 |  | 
|  | 683 | uint64_t Size = Context.getTypeSize(Ty); | 
|  | 684 | if (ET->isIntegralType()) { | 
|  | 685 | if (Size <= 64) | 
|  | 686 | Current = Integer; | 
|  | 687 | else if (Size <= 128) | 
|  | 688 | Lo = Hi = Integer; | 
|  | 689 | } else if (ET == Context.FloatTy) | 
|  | 690 | Current = SSE; | 
|  | 691 | else if (ET == Context.DoubleTy) | 
|  | 692 | Lo = Hi = SSE; | 
|  | 693 | else if (ET == Context.LongDoubleTy) | 
|  | 694 | Current = ComplexX87; | 
|  | 695 |  | 
|  | 696 | // If this complex type crosses an eightbyte boundary then it | 
|  | 697 | // should be split. | 
|  | 698 | uint64_t EB_Real = (OffsetBase) / 64; | 
|  | 699 | uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64; | 
|  | 700 | if (Hi == NoClass && EB_Real != EB_Imag) | 
|  | 701 | Hi = Lo; | 
|  | 702 | } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { | 
|  | 703 | // Arrays are treated like structures. | 
|  | 704 |  | 
|  | 705 | uint64_t Size = Context.getTypeSize(Ty); | 
|  | 706 |  | 
|  | 707 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger | 
|  | 708 | // than two eightbytes, ..., it has class MEMORY. | 
|  | 709 | if (Size > 128) | 
|  | 710 | return; | 
|  | 711 |  | 
|  | 712 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned | 
|  | 713 | // fields, it has class MEMORY. | 
|  | 714 | // | 
|  | 715 | // Only need to check alignment of array base. | 
|  | 716 | if (OffsetBase % Context.getTypeAlign(AT->getElementType())) | 
|  | 717 | return; | 
|  | 718 |  | 
|  | 719 | // Otherwise implement simplified merge. We could be smarter about | 
|  | 720 | // this, but it isn't worth it and would be harder to verify. | 
|  | 721 | Current = NoClass; | 
|  | 722 | uint64_t EltSize = Context.getTypeSize(AT->getElementType()); | 
|  | 723 | uint64_t ArraySize = AT->getSize().getZExtValue(); | 
|  | 724 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { | 
|  | 725 | Class FieldLo, FieldHi; | 
|  | 726 | classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi); | 
|  | 727 | Lo = merge(Lo, FieldLo); | 
|  | 728 | Hi = merge(Hi, FieldHi); | 
|  | 729 | if (Lo == Memory || Hi == Memory) | 
|  | 730 | break; | 
|  | 731 | } | 
|  | 732 |  | 
|  | 733 | // Do post merger cleanup (see below). Only case we worry about is Memory. | 
|  | 734 | if (Hi == Memory) | 
|  | 735 | Lo = Memory; | 
|  | 736 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 737 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 738 | uint64_t Size = Context.getTypeSize(Ty); | 
|  | 739 |  | 
|  | 740 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger | 
|  | 741 | // than two eightbytes, ..., it has class MEMORY. | 
|  | 742 | if (Size > 128) | 
|  | 743 | return; | 
|  | 744 |  | 
| Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 745 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial | 
|  | 746 | // copy constructor or a non-trivial destructor, it is passed by invisible | 
|  | 747 | // reference. | 
|  | 748 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) | 
|  | 749 | return; | 
|  | 750 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 751 | const RecordDecl *RD = RT->getDecl(); | 
|  | 752 |  | 
|  | 753 | // Assume variable sized types are passed in memory. | 
|  | 754 | if (RD->hasFlexibleArrayMember()) | 
|  | 755 | return; | 
|  | 756 |  | 
|  | 757 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); | 
|  | 758 |  | 
|  | 759 | // Reset Lo class, this will be recomputed. | 
|  | 760 | Current = NoClass; | 
|  | 761 | unsigned idx = 0; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 762 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); | 
|  | 763 | i != e; ++i, ++idx) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 764 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); | 
|  | 765 | bool BitField = i->isBitField(); | 
|  | 766 |  | 
|  | 767 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned | 
|  | 768 | // fields, it has class MEMORY. | 
|  | 769 | // | 
|  | 770 | // Note, skip this test for bit-fields, see below. | 
|  | 771 | if (!BitField && Offset % Context.getTypeAlign(i->getType())) { | 
|  | 772 | Lo = Memory; | 
|  | 773 | return; | 
|  | 774 | } | 
|  | 775 |  | 
|  | 776 | // Classify this field. | 
|  | 777 | // | 
|  | 778 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate | 
|  | 779 | // exceeds a single eightbyte, each is classified | 
|  | 780 | // separately. Each eightbyte gets initialized to class | 
|  | 781 | // NO_CLASS. | 
|  | 782 | Class FieldLo, FieldHi; | 
|  | 783 |  | 
|  | 784 | // Bit-fields require special handling, they do not force the | 
|  | 785 | // structure to be passed in memory even if unaligned, and | 
|  | 786 | // therefore they can straddle an eightbyte. | 
|  | 787 | if (BitField) { | 
|  | 788 | // Ignore padding bit-fields. | 
|  | 789 | if (i->isUnnamedBitfield()) | 
|  | 790 | continue; | 
|  | 791 |  | 
|  | 792 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); | 
|  | 793 | uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue(); | 
|  | 794 |  | 
|  | 795 | uint64_t EB_Lo = Offset / 64; | 
|  | 796 | uint64_t EB_Hi = (Offset + Size - 1) / 64; | 
|  | 797 | FieldLo = FieldHi = NoClass; | 
|  | 798 | if (EB_Lo) { | 
|  | 799 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); | 
|  | 800 | FieldLo = NoClass; | 
|  | 801 | FieldHi = Integer; | 
|  | 802 | } else { | 
|  | 803 | FieldLo = Integer; | 
|  | 804 | FieldHi = EB_Hi ? Integer : NoClass; | 
|  | 805 | } | 
|  | 806 | } else | 
|  | 807 | classify(i->getType(), Context, Offset, FieldLo, FieldHi); | 
|  | 808 | Lo = merge(Lo, FieldLo); | 
|  | 809 | Hi = merge(Hi, FieldHi); | 
|  | 810 | if (Lo == Memory || Hi == Memory) | 
|  | 811 | break; | 
|  | 812 | } | 
|  | 813 |  | 
|  | 814 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: | 
|  | 815 | // | 
|  | 816 | // (a) If one of the classes is MEMORY, the whole argument is | 
|  | 817 | // passed in memory. | 
|  | 818 | // | 
|  | 819 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. | 
|  | 820 |  | 
|  | 821 | // The first of these conditions is guaranteed by how we implement | 
|  | 822 | // the merge (just bail). | 
|  | 823 | // | 
|  | 824 | // The second condition occurs in the case of unions; for example | 
|  | 825 | // union { _Complex double; unsigned; }. | 
|  | 826 | if (Hi == Memory) | 
|  | 827 | Lo = Memory; | 
|  | 828 | if (Hi == SSEUp && Lo != SSE) | 
|  | 829 | Hi = SSE; | 
|  | 830 | } | 
|  | 831 | } | 
|  | 832 |  | 
|  | 833 | ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty, | 
|  | 834 | const llvm::Type *CoerceTo, | 
|  | 835 | ASTContext &Context) const { | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 836 | if (CoerceTo == llvm::Type::getInt64Ty(CoerceTo->getContext())) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 837 | // Integer and pointer types will end up in a general purpose | 
|  | 838 | // register. | 
| Anders Carlsson | 5b3a2fc | 2009-09-26 03:56:53 +0000 | [diff] [blame] | 839 | if (Ty->isIntegralType() || Ty->hasPointerRepresentation()) | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 840 | return (Ty->isPromotableIntegerType() ? | 
|  | 841 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 842 | } else if (CoerceTo == llvm::Type::getDoubleTy(CoerceTo->getContext())) { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 843 | // FIXME: It would probably be better to make CGFunctionInfo only map using | 
|  | 844 | // canonical types than to canonize here. | 
|  | 845 | QualType CTy = Context.getCanonicalType(Ty); | 
|  | 846 |  | 
|  | 847 | // Float and double end up in a single SSE reg. | 
|  | 848 | if (CTy == Context.FloatTy || CTy == Context.DoubleTy) | 
|  | 849 | return ABIArgInfo::getDirect(); | 
|  | 850 |  | 
|  | 851 | } | 
|  | 852 |  | 
|  | 853 | return ABIArgInfo::getCoerce(CoerceTo); | 
|  | 854 | } | 
|  | 855 |  | 
|  | 856 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, | 
|  | 857 | ASTContext &Context) const { | 
|  | 858 | // If this is a scalar LLVM value then assume LLVM will pass it in the right | 
|  | 859 | // place naturally. | 
|  | 860 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 861 | return (Ty->isPromotableIntegerType() ? | 
|  | 862 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 863 |  | 
| Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 864 | bool ByVal = !isRecordWithNonTrivialDestructorOrCopyConstructor(Ty); | 
|  | 865 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 866 | // FIXME: Set alignment correctly. | 
| Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 867 | return ABIArgInfo::getIndirect(0, ByVal); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 868 | } | 
|  | 869 |  | 
|  | 870 | ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 871 | ASTContext &Context, | 
|  | 872 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 873 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the | 
|  | 874 | // classification algorithm. | 
|  | 875 | X86_64ABIInfo::Class Lo, Hi; | 
|  | 876 | classify(RetTy, Context, 0, Lo, Hi); | 
|  | 877 |  | 
|  | 878 | // Check some invariants. | 
|  | 879 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); | 
|  | 880 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); | 
|  | 881 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); | 
|  | 882 |  | 
|  | 883 | const llvm::Type *ResType = 0; | 
|  | 884 | switch (Lo) { | 
|  | 885 | case NoClass: | 
|  | 886 | return ABIArgInfo::getIgnore(); | 
|  | 887 |  | 
|  | 888 | case SSEUp: | 
|  | 889 | case X87Up: | 
|  | 890 | assert(0 && "Invalid classification for lo word."); | 
|  | 891 |  | 
|  | 892 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via | 
|  | 893 | // hidden argument. | 
|  | 894 | case Memory: | 
|  | 895 | return getIndirectResult(RetTy, Context); | 
|  | 896 |  | 
|  | 897 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next | 
|  | 898 | // available register of the sequence %rax, %rdx is used. | 
|  | 899 | case Integer: | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 900 | ResType = llvm::Type::getInt64Ty(VMContext); break; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 901 |  | 
|  | 902 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next | 
|  | 903 | // available SSE register of the sequence %xmm0, %xmm1 is used. | 
|  | 904 | case SSE: | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 905 | ResType = llvm::Type::getDoubleTy(VMContext); break; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 906 |  | 
|  | 907 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is | 
|  | 908 | // returned on the X87 stack in %st0 as 80-bit x87 number. | 
|  | 909 | case X87: | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 910 | ResType = llvm::Type::getX86_FP80Ty(VMContext); break; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 911 |  | 
|  | 912 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real | 
|  | 913 | // part of the value is returned in %st0 and the imaginary part in | 
|  | 914 | // %st1. | 
|  | 915 | case ComplexX87: | 
|  | 916 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 917 | ResType = llvm::StructType::get(VMContext, llvm::Type::getX86_FP80Ty(VMContext), | 
|  | 918 | llvm::Type::getX86_FP80Ty(VMContext), | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 919 | NULL); | 
|  | 920 | break; | 
|  | 921 | } | 
|  | 922 |  | 
|  | 923 | switch (Hi) { | 
|  | 924 | // Memory was handled previously and X87 should | 
|  | 925 | // never occur as a hi class. | 
|  | 926 | case Memory: | 
|  | 927 | case X87: | 
|  | 928 | assert(0 && "Invalid classification for hi word."); | 
|  | 929 |  | 
|  | 930 | case ComplexX87: // Previously handled. | 
|  | 931 | case NoClass: break; | 
|  | 932 |  | 
|  | 933 | case Integer: | 
| Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 934 | ResType = llvm::StructType::get(VMContext, ResType, | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 935 | llvm::Type::getInt64Ty(VMContext), NULL); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 936 | break; | 
|  | 937 | case SSE: | 
| Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 938 | ResType = llvm::StructType::get(VMContext, ResType, | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 939 | llvm::Type::getDoubleTy(VMContext), NULL); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 940 | break; | 
|  | 941 |  | 
|  | 942 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte | 
|  | 943 | // is passed in the upper half of the last used SSE register. | 
|  | 944 | // | 
|  | 945 | // SSEUP should always be preceeded by SSE, just widen. | 
|  | 946 | case SSEUp: | 
|  | 947 | assert(Lo == SSE && "Unexpected SSEUp classification."); | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 948 | ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 949 | break; | 
|  | 950 |  | 
|  | 951 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is | 
|  | 952 | // returned together with the previous X87 value in %st0. | 
|  | 953 | case X87Up: | 
|  | 954 | // If X87Up is preceeded by X87, we don't need to do | 
|  | 955 | // anything. However, in some cases with unions it may not be | 
|  | 956 | // preceeded by X87. In such situations we follow gcc and pass the | 
|  | 957 | // extra bits in an SSE reg. | 
|  | 958 | if (Lo != X87) | 
| Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 959 | ResType = llvm::StructType::get(VMContext, ResType, | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 960 | llvm::Type::getDoubleTy(VMContext), NULL); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 961 | break; | 
|  | 962 | } | 
|  | 963 |  | 
|  | 964 | return getCoerceResult(RetTy, ResType, Context); | 
|  | 965 | } | 
|  | 966 |  | 
|  | 967 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 968 | llvm::LLVMContext &VMContext, | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 969 | unsigned &neededInt, | 
|  | 970 | unsigned &neededSSE) const { | 
|  | 971 | X86_64ABIInfo::Class Lo, Hi; | 
|  | 972 | classify(Ty, Context, 0, Lo, Hi); | 
|  | 973 |  | 
|  | 974 | // Check some invariants. | 
|  | 975 | // FIXME: Enforce these by construction. | 
|  | 976 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); | 
|  | 977 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); | 
|  | 978 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); | 
|  | 979 |  | 
|  | 980 | neededInt = 0; | 
|  | 981 | neededSSE = 0; | 
|  | 982 | const llvm::Type *ResType = 0; | 
|  | 983 | switch (Lo) { | 
|  | 984 | case NoClass: | 
|  | 985 | return ABIArgInfo::getIgnore(); | 
|  | 986 |  | 
|  | 987 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument | 
|  | 988 | // on the stack. | 
|  | 989 | case Memory: | 
|  | 990 |  | 
|  | 991 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or | 
|  | 992 | // COMPLEX_X87, it is passed in memory. | 
|  | 993 | case X87: | 
|  | 994 | case ComplexX87: | 
|  | 995 | return getIndirectResult(Ty, Context); | 
|  | 996 |  | 
|  | 997 | case SSEUp: | 
|  | 998 | case X87Up: | 
|  | 999 | assert(0 && "Invalid classification for lo word."); | 
|  | 1000 |  | 
|  | 1001 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next | 
|  | 1002 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 | 
|  | 1003 | // and %r9 is used. | 
|  | 1004 | case Integer: | 
|  | 1005 | ++neededInt; | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1006 | ResType = llvm::Type::getInt64Ty(VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1007 | break; | 
|  | 1008 |  | 
|  | 1009 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next | 
|  | 1010 | // available SSE register is used, the registers are taken in the | 
|  | 1011 | // order from %xmm0 to %xmm7. | 
|  | 1012 | case SSE: | 
|  | 1013 | ++neededSSE; | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1014 | ResType = llvm::Type::getDoubleTy(VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1015 | break; | 
|  | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | switch (Hi) { | 
|  | 1019 | // Memory was handled previously, ComplexX87 and X87 should | 
|  | 1020 | // never occur as hi classes, and X87Up must be preceed by X87, | 
|  | 1021 | // which is passed in memory. | 
|  | 1022 | case Memory: | 
|  | 1023 | case X87: | 
|  | 1024 | case ComplexX87: | 
|  | 1025 | assert(0 && "Invalid classification for hi word."); | 
|  | 1026 | break; | 
|  | 1027 |  | 
|  | 1028 | case NoClass: break; | 
|  | 1029 | case Integer: | 
| Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1030 | ResType = llvm::StructType::get(VMContext, ResType, | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1031 | llvm::Type::getInt64Ty(VMContext), NULL); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1032 | ++neededInt; | 
|  | 1033 | break; | 
|  | 1034 |  | 
|  | 1035 | // X87Up generally doesn't occur here (long double is passed in | 
|  | 1036 | // memory), except in situations involving unions. | 
|  | 1037 | case X87Up: | 
|  | 1038 | case SSE: | 
| Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1039 | ResType = llvm::StructType::get(VMContext, ResType, | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1040 | llvm::Type::getDoubleTy(VMContext), NULL); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1041 | ++neededSSE; | 
|  | 1042 | break; | 
|  | 1043 |  | 
|  | 1044 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the | 
|  | 1045 | // eightbyte is passed in the upper half of the last used SSE | 
|  | 1046 | // register. | 
|  | 1047 | case SSEUp: | 
|  | 1048 | assert(Lo == SSE && "Unexpected SSEUp classification."); | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1049 | ResType = llvm::VectorType::get(llvm::Type::getDoubleTy(VMContext), 2); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1050 | break; | 
|  | 1051 | } | 
|  | 1052 |  | 
|  | 1053 | return getCoerceResult(Ty, ResType, Context); | 
|  | 1054 | } | 
|  | 1055 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1056 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 1057 | llvm::LLVMContext &VMContext) const { | 
|  | 1058 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), | 
|  | 1059 | Context, VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1060 |  | 
|  | 1061 | // Keep track of the number of assigned registers. | 
|  | 1062 | unsigned freeIntRegs = 6, freeSSERegs = 8; | 
|  | 1063 |  | 
|  | 1064 | // If the return value is indirect, then the hidden argument is consuming one | 
|  | 1065 | // integer register. | 
|  | 1066 | if (FI.getReturnInfo().isIndirect()) | 
|  | 1067 | --freeIntRegs; | 
|  | 1068 |  | 
|  | 1069 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers | 
|  | 1070 | // get assigned (in left-to-right order) for passing as follows... | 
|  | 1071 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); | 
|  | 1072 | it != ie; ++it) { | 
|  | 1073 | unsigned neededInt, neededSSE; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | it->info = classifyArgumentType(it->type, Context, VMContext, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1075 | neededInt, neededSSE); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1076 |  | 
|  | 1077 | // AMD64-ABI 3.2.3p3: If there are no registers available for any | 
|  | 1078 | // eightbyte of an argument, the whole argument is passed on the | 
|  | 1079 | // stack. If registers have already been assigned for some | 
|  | 1080 | // eightbytes of such an argument, the assignments get reverted. | 
|  | 1081 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { | 
|  | 1082 | freeIntRegs -= neededInt; | 
|  | 1083 | freeSSERegs -= neededSSE; | 
|  | 1084 | } else { | 
|  | 1085 | it->info = getIndirectResult(it->type, Context); | 
|  | 1086 | } | 
|  | 1087 | } | 
|  | 1088 | } | 
|  | 1089 |  | 
|  | 1090 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, | 
|  | 1091 | QualType Ty, | 
|  | 1092 | CodeGenFunction &CGF) { | 
|  | 1093 | llvm::Value *overflow_arg_area_p = | 
|  | 1094 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); | 
|  | 1095 | llvm::Value *overflow_arg_area = | 
|  | 1096 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); | 
|  | 1097 |  | 
|  | 1098 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 | 
|  | 1099 | // byte boundary if alignment needed by type exceeds 8 byte boundary. | 
|  | 1100 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; | 
|  | 1101 | if (Align > 8) { | 
|  | 1102 | // Note that we follow the ABI & gcc here, even though the type | 
|  | 1103 | // could in theory have an alignment greater than 16. This case | 
|  | 1104 | // shouldn't ever matter in practice. | 
|  | 1105 |  | 
|  | 1106 | // overflow_arg_area = (overflow_arg_area + 15) & ~15; | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1107 | llvm::Value *Offset = | 
|  | 1108 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 15); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1109 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); | 
|  | 1110 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1111 | llvm::Type::getInt64Ty(CGF.getLLVMContext())); | 
|  | 1112 | llvm::Value *Mask = llvm::ConstantInt::get( | 
|  | 1113 | llvm::Type::getInt64Ty(CGF.getLLVMContext()), ~15LL); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1114 | overflow_arg_area = | 
|  | 1115 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), | 
|  | 1116 | overflow_arg_area->getType(), | 
|  | 1117 | "overflow_arg_area.align"); | 
|  | 1118 | } | 
|  | 1119 |  | 
|  | 1120 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. | 
|  | 1121 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); | 
|  | 1122 | llvm::Value *Res = | 
|  | 1123 | CGF.Builder.CreateBitCast(overflow_arg_area, | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1124 | llvm::PointerType::getUnqual(LTy)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1125 |  | 
|  | 1126 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: | 
|  | 1127 | // l->overflow_arg_area + sizeof(type). | 
|  | 1128 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to | 
|  | 1129 | // an 8 byte boundary. | 
|  | 1130 |  | 
|  | 1131 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1132 | llvm::Value *Offset = | 
|  | 1133 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1134 | (SizeInBytes + 7)  & ~7); | 
|  | 1135 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, | 
|  | 1136 | "overflow_arg_area.next"); | 
|  | 1137 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); | 
|  | 1138 |  | 
|  | 1139 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. | 
|  | 1140 | return Res; | 
|  | 1141 | } | 
|  | 1142 |  | 
|  | 1143 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1144 | CodeGenFunction &CGF) const { | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1145 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1146 | const llvm::Type *i32Ty = llvm::Type::getInt32Ty(VMContext); | 
|  | 1147 | const llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1149 | // Assume that va_list type is correct; should be pointer to LLVM type: | 
|  | 1150 | // struct { | 
|  | 1151 | //   i32 gp_offset; | 
|  | 1152 | //   i32 fp_offset; | 
|  | 1153 | //   i8* overflow_arg_area; | 
|  | 1154 | //   i8* reg_save_area; | 
|  | 1155 | // }; | 
|  | 1156 | unsigned neededInt, neededSSE; | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1157 | ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), VMContext, | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1158 | neededInt, neededSSE); | 
|  | 1159 |  | 
|  | 1160 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed | 
|  | 1161 | // in the registers. If not go to step 7. | 
|  | 1162 | if (!neededInt && !neededSSE) | 
|  | 1163 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); | 
|  | 1164 |  | 
|  | 1165 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of | 
|  | 1166 | // general purpose registers needed to pass type and num_fp to hold | 
|  | 1167 | // the number of floating point registers needed. | 
|  | 1168 |  | 
|  | 1169 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into | 
|  | 1170 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or | 
|  | 1171 | // l->fp_offset > 304 - num_fp * 16 go to step 7. | 
|  | 1172 | // | 
|  | 1173 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of | 
|  | 1174 | // register save space). | 
|  | 1175 |  | 
|  | 1176 | llvm::Value *InRegs = 0; | 
|  | 1177 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; | 
|  | 1178 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; | 
|  | 1179 | if (neededInt) { | 
|  | 1180 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); | 
|  | 1181 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); | 
|  | 1182 | InRegs = | 
|  | 1183 | CGF.Builder.CreateICmpULE(gp_offset, | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1184 | llvm::ConstantInt::get(i32Ty, | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1185 | 48 - neededInt * 8), | 
|  | 1186 | "fits_in_gp"); | 
|  | 1187 | } | 
|  | 1188 |  | 
|  | 1189 | if (neededSSE) { | 
|  | 1190 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); | 
|  | 1191 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); | 
|  | 1192 | llvm::Value *FitsInFP = | 
|  | 1193 | CGF.Builder.CreateICmpULE(fp_offset, | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1194 | llvm::ConstantInt::get(i32Ty, | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1195 | 176 - neededSSE * 16), | 
|  | 1196 | "fits_in_fp"); | 
|  | 1197 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; | 
|  | 1198 | } | 
|  | 1199 |  | 
|  | 1200 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); | 
|  | 1201 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); | 
|  | 1202 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); | 
|  | 1203 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); | 
|  | 1204 |  | 
|  | 1205 | // Emit code to load the value if it was passed in registers. | 
|  | 1206 |  | 
|  | 1207 | CGF.EmitBlock(InRegBlock); | 
|  | 1208 |  | 
|  | 1209 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with | 
|  | 1210 | // an offset of l->gp_offset and/or l->fp_offset. This may require | 
|  | 1211 | // copying to a temporary location in case the parameter is passed | 
|  | 1212 | // in different register classes or requires an alignment greater | 
|  | 1213 | // than 8 for general purpose registers and 16 for XMM registers. | 
|  | 1214 | // | 
|  | 1215 | // FIXME: This really results in shameful code when we end up needing to | 
|  | 1216 | // collect arguments from different places; often what should result in a | 
|  | 1217 | // simple assembling of a structure from scattered addresses has many more | 
|  | 1218 | // loads than necessary. Can we clean this up? | 
|  | 1219 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); | 
|  | 1220 | llvm::Value *RegAddr = | 
|  | 1221 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), | 
|  | 1222 | "reg_save_area"); | 
|  | 1223 | if (neededInt && neededSSE) { | 
|  | 1224 | // FIXME: Cleanup. | 
|  | 1225 | assert(AI.isCoerce() && "Unexpected ABI info for mixed regs"); | 
|  | 1226 | const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); | 
|  | 1227 | llvm::Value *Tmp = CGF.CreateTempAlloca(ST); | 
|  | 1228 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); | 
|  | 1229 | const llvm::Type *TyLo = ST->getElementType(0); | 
|  | 1230 | const llvm::Type *TyHi = ST->getElementType(1); | 
|  | 1231 | assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) && | 
|  | 1232 | "Unexpected ABI info for mixed regs"); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1233 | const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); | 
|  | 1234 | const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1235 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); | 
|  | 1236 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); | 
|  | 1237 | llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr; | 
|  | 1238 | llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr; | 
|  | 1239 | llvm::Value *V = | 
|  | 1240 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); | 
|  | 1241 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); | 
|  | 1242 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); | 
|  | 1243 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); | 
|  | 1244 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1245 | RegAddr = CGF.Builder.CreateBitCast(Tmp, | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1246 | llvm::PointerType::getUnqual(LTy)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1247 | } else if (neededInt) { | 
|  | 1248 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); | 
|  | 1249 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1250 | llvm::PointerType::getUnqual(LTy)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1251 | } else { | 
|  | 1252 | if (neededSSE == 1) { | 
|  | 1253 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); | 
|  | 1254 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1255 | llvm::PointerType::getUnqual(LTy)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1256 | } else { | 
|  | 1257 | assert(neededSSE == 2 && "Invalid number of needed registers!"); | 
|  | 1258 | // SSE registers are spaced 16 bytes apart in the register save | 
|  | 1259 | // area, we need to collect the two eightbytes together. | 
|  | 1260 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); | 
|  | 1261 | llvm::Value *RegAddrHi = | 
|  | 1262 | CGF.Builder.CreateGEP(RegAddrLo, | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1263 | llvm::ConstantInt::get(i32Ty, 16)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1264 | const llvm::Type *DblPtrTy = | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1265 | llvm::PointerType::getUnqual(DoubleTy); | 
|  | 1266 | const llvm::StructType *ST = llvm::StructType::get(VMContext, DoubleTy, | 
|  | 1267 | DoubleTy, NULL); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1268 | llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); | 
|  | 1269 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, | 
|  | 1270 | DblPtrTy)); | 
|  | 1271 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); | 
|  | 1272 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, | 
|  | 1273 | DblPtrTy)); | 
|  | 1274 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); | 
|  | 1275 | RegAddr = CGF.Builder.CreateBitCast(Tmp, | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1276 | llvm::PointerType::getUnqual(LTy)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1277 | } | 
|  | 1278 | } | 
|  | 1279 |  | 
|  | 1280 | // AMD64-ABI 3.5.7p5: Step 5. Set: | 
|  | 1281 | // l->gp_offset = l->gp_offset + num_gp * 8 | 
|  | 1282 | // l->fp_offset = l->fp_offset + num_fp * 16. | 
|  | 1283 | if (neededInt) { | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1284 | llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededInt * 8); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1285 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), | 
|  | 1286 | gp_offset_p); | 
|  | 1287 | } | 
|  | 1288 | if (neededSSE) { | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1289 | llvm::Value *Offset = llvm::ConstantInt::get(i32Ty, neededSSE * 16); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1290 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), | 
|  | 1291 | fp_offset_p); | 
|  | 1292 | } | 
|  | 1293 | CGF.EmitBranch(ContBlock); | 
|  | 1294 |  | 
|  | 1295 | // Emit code to load the value if it was passed in memory. | 
|  | 1296 |  | 
|  | 1297 | CGF.EmitBlock(InMemBlock); | 
|  | 1298 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); | 
|  | 1299 |  | 
|  | 1300 | // Return the appropriate result. | 
|  | 1301 |  | 
|  | 1302 | CGF.EmitBlock(ContBlock); | 
|  | 1303 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), | 
|  | 1304 | "vaarg.addr"); | 
|  | 1305 | ResAddr->reserveOperandSpace(2); | 
|  | 1306 | ResAddr->addIncoming(RegAddr, InRegBlock); | 
|  | 1307 | ResAddr->addIncoming(MemAddr, InMemBlock); | 
|  | 1308 |  | 
|  | 1309 | return ResAddr; | 
|  | 1310 | } | 
|  | 1311 |  | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1312 | // PIC16 ABI Implementation | 
|  | 1313 |  | 
|  | 1314 | namespace { | 
|  | 1315 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1316 | class PIC16ABIInfo : public ABIInfo { | 
|  | 1317 | ABIArgInfo classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1318 | ASTContext &Context, | 
|  | 1319 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1320 |  | 
|  | 1321 | ABIArgInfo classifyArgumentType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1322 | ASTContext &Context, | 
|  | 1323 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1324 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1325 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 1326 | llvm::LLVMContext &VMContext) const { | 
|  | 1327 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context, | 
|  | 1328 | VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1329 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); | 
|  | 1330 | it != ie; ++it) | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1331 | it->info = classifyArgumentType(it->type, Context, VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1332 | } | 
|  | 1333 |  | 
|  | 1334 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1335 | CodeGenFunction &CGF) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1336 | }; | 
|  | 1337 |  | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1338 | } | 
|  | 1339 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1340 | ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1341 | ASTContext &Context, | 
|  | 1342 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1343 | if (RetTy->isVoidType()) { | 
|  | 1344 | return ABIArgInfo::getIgnore(); | 
|  | 1345 | } else { | 
|  | 1346 | return ABIArgInfo::getDirect(); | 
|  | 1347 | } | 
|  | 1348 | } | 
|  | 1349 |  | 
|  | 1350 | ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1351 | ASTContext &Context, | 
|  | 1352 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1353 | return ABIArgInfo::getDirect(); | 
|  | 1354 | } | 
|  | 1355 |  | 
|  | 1356 | llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1357 | CodeGenFunction &CGF) const { | 
|  | 1358 | return 0; | 
|  | 1359 | } | 
|  | 1360 |  | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1361 | // ARM ABI Implementation | 
|  | 1362 |  | 
|  | 1363 | namespace { | 
|  | 1364 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1365 | class ARMABIInfo : public ABIInfo { | 
| Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 1366 | public: | 
|  | 1367 | enum ABIKind { | 
|  | 1368 | APCS = 0, | 
|  | 1369 | AAPCS = 1, | 
|  | 1370 | AAPCS_VFP | 
|  | 1371 | }; | 
|  | 1372 |  | 
|  | 1373 | private: | 
|  | 1374 | ABIKind Kind; | 
|  | 1375 |  | 
|  | 1376 | public: | 
|  | 1377 | ARMABIInfo(ABIKind _Kind) : Kind(_Kind) {} | 
|  | 1378 |  | 
|  | 1379 | private: | 
|  | 1380 | ABIKind getABIKind() const { return Kind; } | 
|  | 1381 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1382 | ABIArgInfo classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1383 | ASTContext &Context, | 
|  | 1384 | llvm::LLVMContext &VMCOntext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1385 |  | 
|  | 1386 | ABIArgInfo classifyArgumentType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1387 | ASTContext &Context, | 
|  | 1388 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1389 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1390 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 1391 | llvm::LLVMContext &VMContext) const; | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1392 |  | 
|  | 1393 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1394 | CodeGenFunction &CGF) const; | 
|  | 1395 | }; | 
|  | 1396 |  | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1397 | } | 
|  | 1398 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1399 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 1400 | llvm::LLVMContext &VMContext) const { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1402 | VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1403 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); | 
|  | 1404 | it != ie; ++it) { | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1405 | it->info = classifyArgumentType(it->type, Context, VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1406 | } | 
| Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 1407 |  | 
|  | 1408 | // ARM always overrides the calling convention. | 
|  | 1409 | switch (getABIKind()) { | 
|  | 1410 | case APCS: | 
|  | 1411 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS); | 
|  | 1412 | break; | 
|  | 1413 |  | 
|  | 1414 | case AAPCS: | 
|  | 1415 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS); | 
|  | 1416 | break; | 
|  | 1417 |  | 
|  | 1418 | case AAPCS_VFP: | 
|  | 1419 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP); | 
|  | 1420 | break; | 
|  | 1421 | } | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1422 | } | 
|  | 1423 |  | 
|  | 1424 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1425 | ASTContext &Context, | 
|  | 1426 | llvm::LLVMContext &VMContext) const { | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1427 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1428 | return (Ty->isPromotableIntegerType() ? | 
|  | 1429 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1430 |  | 
| Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 1431 | // Ignore empty records. | 
|  | 1432 | if (isEmptyRecord(Context, Ty, true)) | 
|  | 1433 | return ABIArgInfo::getIgnore(); | 
|  | 1434 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1435 | // FIXME: This is kind of nasty... but there isn't much choice because the ARM | 
|  | 1436 | // backend doesn't support byval. | 
|  | 1437 | // FIXME: This doesn't handle alignment > 64 bits. | 
|  | 1438 | const llvm::Type* ElemTy; | 
|  | 1439 | unsigned SizeRegs; | 
|  | 1440 | if (Context.getTypeAlign(Ty) > 32) { | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1441 | ElemTy = llvm::Type::getInt64Ty(VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1442 | SizeRegs = (Context.getTypeSize(Ty) + 63) / 64; | 
|  | 1443 | } else { | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1444 | ElemTy = llvm::Type::getInt32Ty(VMContext); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1445 | SizeRegs = (Context.getTypeSize(Ty) + 31) / 32; | 
|  | 1446 | } | 
|  | 1447 | std::vector<const llvm::Type*> LLVMFields; | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1448 | LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs)); | 
| Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1449 | const llvm::Type* STy = llvm::StructType::get(VMContext, LLVMFields, true); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1450 | return ABIArgInfo::getCoerce(STy); | 
|  | 1451 | } | 
|  | 1452 |  | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1453 | static bool isIntegerLikeType(QualType Ty, | 
|  | 1454 | ASTContext &Context, | 
|  | 1455 | llvm::LLVMContext &VMContext) { | 
|  | 1456 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure | 
|  | 1457 | // is called integer-like if its size is less than or equal to one word, and | 
|  | 1458 | // the offset of each of its addressable sub-fields is zero. | 
|  | 1459 |  | 
|  | 1460 | uint64_t Size = Context.getTypeSize(Ty); | 
|  | 1461 |  | 
|  | 1462 | // Check that the type fits in a word. | 
|  | 1463 | if (Size > 32) | 
|  | 1464 | return false; | 
|  | 1465 |  | 
|  | 1466 | // FIXME: Handle vector types! | 
|  | 1467 | if (Ty->isVectorType()) | 
|  | 1468 | return false; | 
|  | 1469 |  | 
| Daniel Dunbar | b0d5819 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 1470 | // Float types are never treated as "integer like". | 
|  | 1471 | if (Ty->isRealFloatingType()) | 
|  | 1472 | return false; | 
|  | 1473 |  | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1474 | // If this is a builtin or pointer type then it is ok. | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1475 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1476 | return true; | 
|  | 1477 |  | 
|  | 1478 | // Complex types "should" be ok by the definition above, but they are not. | 
|  | 1479 | if (Ty->isAnyComplexType()) | 
|  | 1480 | return false; | 
|  | 1481 |  | 
|  | 1482 | // Single element and zero sized arrays should be allowed, by the definition | 
|  | 1483 | // above, but they are not. | 
|  | 1484 |  | 
|  | 1485 | // Otherwise, it must be a record type. | 
|  | 1486 | const RecordType *RT = Ty->getAs<RecordType>(); | 
|  | 1487 | if (!RT) return false; | 
|  | 1488 |  | 
|  | 1489 | // Ignore records with flexible arrays. | 
|  | 1490 | const RecordDecl *RD = RT->getDecl(); | 
|  | 1491 | if (RD->hasFlexibleArrayMember()) | 
|  | 1492 | return false; | 
|  | 1493 |  | 
|  | 1494 | // Check that all sub-fields are at offset 0, and are themselves "integer | 
|  | 1495 | // like". | 
|  | 1496 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); | 
|  | 1497 |  | 
|  | 1498 | bool HadField = false; | 
|  | 1499 | unsigned idx = 0; | 
|  | 1500 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); | 
|  | 1501 | i != e; ++i, ++idx) { | 
|  | 1502 | const FieldDecl *FD = *i; | 
|  | 1503 |  | 
|  | 1504 | // Check if this field is at offset 0. | 
|  | 1505 | uint64_t Offset = Layout.getFieldOffset(idx); | 
|  | 1506 | if (Offset != 0) { | 
|  | 1507 | // Allow padding bit-fields, but only if they are all at the end of the | 
|  | 1508 | // structure (despite the wording above, this matches gcc). | 
|  | 1509 | if (FD->isBitField() && | 
|  | 1510 | !FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) { | 
|  | 1511 | for (; i != e; ++i) | 
|  | 1512 | if (!i->isBitField() || | 
|  | 1513 | i->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) | 
|  | 1514 | return false; | 
|  | 1515 |  | 
|  | 1516 | // All remaining fields are padding, allow this. | 
|  | 1517 | return true; | 
|  | 1518 | } | 
|  | 1519 |  | 
|  | 1520 | return false; | 
|  | 1521 | } | 
|  | 1522 |  | 
|  | 1523 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) | 
|  | 1524 | return false; | 
|  | 1525 |  | 
|  | 1526 | // Only allow at most one field in a structure. Again this doesn't match the | 
|  | 1527 | // wording above, but follows gcc. | 
|  | 1528 | if (!RD->isUnion()) { | 
|  | 1529 | if (HadField) | 
|  | 1530 | return false; | 
|  | 1531 |  | 
|  | 1532 | HadField = true; | 
|  | 1533 | } | 
|  | 1534 | } | 
|  | 1535 |  | 
|  | 1536 | return true; | 
|  | 1537 | } | 
|  | 1538 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1539 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1540 | ASTContext &Context, | 
|  | 1541 | llvm::LLVMContext &VMContext) const { | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1542 | if (RetTy->isVoidType()) | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1543 | return ABIArgInfo::getIgnore(); | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1544 |  | 
|  | 1545 | if (!CodeGenFunction::hasAggregateLLVMType(RetTy)) | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1546 | return (RetTy->isPromotableIntegerType() ? | 
|  | 1547 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1548 |  | 
|  | 1549 | // Are we following APCS? | 
|  | 1550 | if (getABIKind() == APCS) { | 
|  | 1551 | if (isEmptyRecord(Context, RetTy, false)) | 
|  | 1552 | return ABIArgInfo::getIgnore(); | 
|  | 1553 |  | 
|  | 1554 | // Integer like structures are returned in r0. | 
|  | 1555 | if (isIntegerLikeType(RetTy, Context, VMContext)) { | 
|  | 1556 | // Return in the smallest viable integer type. | 
|  | 1557 | uint64_t Size = Context.getTypeSize(RetTy); | 
|  | 1558 | if (Size <= 8) | 
|  | 1559 | return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext)); | 
|  | 1560 | if (Size <= 16) | 
|  | 1561 | return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext)); | 
|  | 1562 | return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext)); | 
|  | 1563 | } | 
|  | 1564 |  | 
|  | 1565 | // Otherwise return in memory. | 
|  | 1566 | return ABIArgInfo::getIndirect(0); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1567 | } | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1568 |  | 
|  | 1569 | // Otherwise this is an AAPCS variant. | 
|  | 1570 |  | 
| Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 1571 | if (isEmptyRecord(Context, RetTy, true)) | 
|  | 1572 | return ABIArgInfo::getIgnore(); | 
|  | 1573 |  | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1574 | // Aggregates <= 4 bytes are returned in r0; other aggregates | 
|  | 1575 | // are returned indirectly. | 
|  | 1576 | uint64_t Size = Context.getTypeSize(RetTy); | 
| Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 1577 | if (Size <= 32) { | 
|  | 1578 | // Return in the smallest viable integer type. | 
|  | 1579 | if (Size <= 8) | 
|  | 1580 | return ABIArgInfo::getCoerce(llvm::Type::getInt8Ty(VMContext)); | 
|  | 1581 | if (Size <= 16) | 
|  | 1582 | return ABIArgInfo::getCoerce(llvm::Type::getInt16Ty(VMContext)); | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1583 | return ABIArgInfo::getCoerce(llvm::Type::getInt32Ty(VMContext)); | 
| Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 1584 | } | 
|  | 1585 |  | 
| Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1586 | return ABIArgInfo::getIndirect(0); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1587 | } | 
|  | 1588 |  | 
|  | 1589 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1590 | CodeGenFunction &CGF) const { | 
|  | 1591 | // FIXME: Need to handle alignment | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | const llvm::Type *BP = | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1593 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext())); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1594 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1595 |  | 
|  | 1596 | CGBuilderTy &Builder = CGF.Builder; | 
|  | 1597 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, | 
|  | 1598 | "ap"); | 
|  | 1599 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); | 
|  | 1600 | llvm::Type *PTy = | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1601 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1602 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); | 
|  | 1603 |  | 
|  | 1604 | uint64_t Offset = | 
|  | 1605 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); | 
|  | 1606 | llvm::Value *NextAddr = | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1607 | Builder.CreateGEP(Addr, llvm::ConstantInt::get( | 
|  | 1608 | llvm::Type::getInt32Ty(CGF.getLLVMContext()), Offset), | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1609 | "ap.next"); | 
|  | 1610 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); | 
|  | 1611 |  | 
|  | 1612 | return AddrTyped; | 
|  | 1613 | } | 
|  | 1614 |  | 
|  | 1615 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1616 | ASTContext &Context, | 
|  | 1617 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1618 | if (RetTy->isVoidType()) { | 
|  | 1619 | return ABIArgInfo::getIgnore(); | 
|  | 1620 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { | 
|  | 1621 | return ABIArgInfo::getIndirect(0); | 
|  | 1622 | } else { | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1623 | return (RetTy->isPromotableIntegerType() ? | 
|  | 1624 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1625 | } | 
|  | 1626 | } | 
|  | 1627 |  | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1628 | // SystemZ ABI Implementation | 
|  | 1629 |  | 
| Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 1630 | namespace { | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1631 |  | 
| Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 1632 | class SystemZABIInfo : public ABIInfo { | 
|  | 1633 | bool isPromotableIntegerType(QualType Ty) const; | 
|  | 1634 |  | 
|  | 1635 | ABIArgInfo classifyReturnType(QualType RetTy, ASTContext &Context, | 
|  | 1636 | llvm::LLVMContext &VMContext) const; | 
|  | 1637 |  | 
|  | 1638 | ABIArgInfo classifyArgumentType(QualType RetTy, ASTContext &Context, | 
|  | 1639 | llvm::LLVMContext &VMContext) const; | 
|  | 1640 |  | 
|  | 1641 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context, | 
|  | 1642 | llvm::LLVMContext &VMContext) const { | 
|  | 1643 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), | 
|  | 1644 | Context, VMContext); | 
|  | 1645 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); | 
|  | 1646 | it != ie; ++it) | 
|  | 1647 | it->info = classifyArgumentType(it->type, Context, VMContext); | 
|  | 1648 | } | 
|  | 1649 |  | 
|  | 1650 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1651 | CodeGenFunction &CGF) const; | 
|  | 1652 | }; | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1653 |  | 
| Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 1654 | } | 
|  | 1655 |  | 
|  | 1656 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { | 
|  | 1657 | // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended. | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1658 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) | 
| Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 1659 | switch (BT->getKind()) { | 
|  | 1660 | case BuiltinType::Bool: | 
|  | 1661 | case BuiltinType::Char_S: | 
|  | 1662 | case BuiltinType::Char_U: | 
|  | 1663 | case BuiltinType::SChar: | 
|  | 1664 | case BuiltinType::UChar: | 
|  | 1665 | case BuiltinType::Short: | 
|  | 1666 | case BuiltinType::UShort: | 
|  | 1667 | case BuiltinType::Int: | 
|  | 1668 | case BuiltinType::UInt: | 
|  | 1669 | return true; | 
|  | 1670 | default: | 
|  | 1671 | return false; | 
|  | 1672 | } | 
|  | 1673 | return false; | 
|  | 1674 | } | 
|  | 1675 |  | 
|  | 1676 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1677 | CodeGenFunction &CGF) const { | 
|  | 1678 | // FIXME: Implement | 
|  | 1679 | return 0; | 
|  | 1680 | } | 
|  | 1681 |  | 
|  | 1682 |  | 
|  | 1683 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy, | 
|  | 1684 | ASTContext &Context, | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1685 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 1686 | if (RetTy->isVoidType()) { | 
|  | 1687 | return ABIArgInfo::getIgnore(); | 
|  | 1688 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { | 
|  | 1689 | return ABIArgInfo::getIndirect(0); | 
|  | 1690 | } else { | 
|  | 1691 | return (isPromotableIntegerType(RetTy) ? | 
|  | 1692 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
|  | 1693 | } | 
|  | 1694 | } | 
|  | 1695 |  | 
|  | 1696 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty, | 
|  | 1697 | ASTContext &Context, | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1698 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 1699 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { | 
|  | 1700 | return ABIArgInfo::getIndirect(0); | 
|  | 1701 | } else { | 
|  | 1702 | return (isPromotableIntegerType(Ty) ? | 
|  | 1703 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
|  | 1704 | } | 
|  | 1705 | } | 
|  | 1706 |  | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1707 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1708 | ASTContext &Context, | 
|  | 1709 | llvm::LLVMContext &VMContext) const { | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1710 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { | 
|  | 1711 | return ABIArgInfo::getIndirect(0); | 
|  | 1712 | } else { | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1713 | return (Ty->isPromotableIntegerType() ? | 
|  | 1714 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1715 | } | 
|  | 1716 | } | 
|  | 1717 |  | 
|  | 1718 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, | 
|  | 1719 | CodeGenFunction &CGF) const { | 
|  | 1720 | return 0; | 
|  | 1721 | } | 
|  | 1722 |  | 
|  | 1723 | const ABIInfo &CodeGenTypes::getABIInfo() const { | 
|  | 1724 | if (TheABIInfo) | 
|  | 1725 | return *TheABIInfo; | 
|  | 1726 |  | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 1727 | // For now we just cache the ABIInfo in CodeGenTypes and don't free it. | 
|  | 1728 |  | 
| Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 1729 | const llvm::Triple &Triple(getContext().Target.getTriple()); | 
|  | 1730 | switch (Triple.getArch()) { | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 1731 | default: | 
|  | 1732 | return *(TheABIInfo = new DefaultABIInfo); | 
|  | 1733 |  | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1734 | case llvm::Triple::arm: | 
|  | 1735 | case llvm::Triple::thumb: | 
| Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 1736 | // FIXME: We want to know the float calling convention as well. | 
| Daniel Dunbar | 018ba5a | 2009-09-14 00:35:03 +0000 | [diff] [blame] | 1737 | if (strcmp(getContext().Target.getABI(), "apcs-gnu") == 0) | 
| Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 1738 | return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::APCS)); | 
|  | 1739 |  | 
|  | 1740 | return *(TheABIInfo = new ARMABIInfo(ARMABIInfo::AAPCS)); | 
| Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 1741 |  | 
|  | 1742 | case llvm::Triple::pic16: | 
|  | 1743 | return *(TheABIInfo = new PIC16ABIInfo()); | 
|  | 1744 |  | 
|  | 1745 | case llvm::Triple::systemz: | 
|  | 1746 | return *(TheABIInfo = new SystemZABIInfo()); | 
|  | 1747 |  | 
| Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 1748 | case llvm::Triple::x86: | 
|  | 1749 | if (Triple.getOS() == llvm::Triple::Darwin) | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 1750 | return *(TheABIInfo = new X86_32ABIInfo(Context, true, true)); | 
|  | 1751 |  | 
| Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 1752 | switch (Triple.getOS()) { | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 1753 | case llvm::Triple::Cygwin: | 
|  | 1754 | case llvm::Triple::DragonFly: | 
|  | 1755 | case llvm::Triple::MinGW32: | 
|  | 1756 | case llvm::Triple::MinGW64: | 
| David Chisnall | 75c135a | 2009-09-03 01:48:05 +0000 | [diff] [blame] | 1757 | case llvm::Triple::FreeBSD: | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 1758 | case llvm::Triple::OpenBSD: | 
|  | 1759 | return *(TheABIInfo = new X86_32ABIInfo(Context, false, true)); | 
|  | 1760 |  | 
|  | 1761 | default: | 
|  | 1762 | return *(TheABIInfo = new X86_32ABIInfo(Context, false, false)); | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1763 | } | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1764 |  | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 1765 | case llvm::Triple::x86_64: | 
|  | 1766 | return *(TheABIInfo = new X86_64ABIInfo()); | 
| Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 1767 | } | 
| Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1768 | } |