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