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