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