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