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