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