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