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