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