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