Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 1 | //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===// |
| 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 | |
| 15 | #include "CGCall.h" |
| 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Devang Patel | d0646bd | 2008-09-24 01:01:36 +0000 | [diff] [blame] | 24 | #include "llvm/Attributes.h" |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
| 30 | /***/ |
| 31 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 32 | // FIXME: Use iterator and sidestep silly type array creation. |
| 33 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 34 | CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 35 | ArgTypes.push_back(FTNP->getResultType()); |
| 36 | } |
| 37 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 38 | CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 39 | ArgTypes.push_back(FTP->getResultType()); |
| 40 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 41 | ArgTypes.push_back(FTP->getArgType(i)); |
| 42 | } |
| 43 | |
| 44 | // FIXME: Is there really any reason to have this still? |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 45 | CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD) { |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 46 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
| 47 | const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 48 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 49 | ArgTypes.push_back(FTy->getResultType()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 50 | if (FTP) { |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 51 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 52 | ArgTypes.push_back(FTP->getArgType(i)); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 53 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 57 | const ASTContext &Context) { |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 58 | ArgTypes.push_back(MD->getResultType()); |
| 59 | ArgTypes.push_back(MD->getSelfDecl()->getType()); |
| 60 | ArgTypes.push_back(Context.getObjCSelType()); |
| 61 | for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), |
| 62 | e = MD->param_end(); i != e; ++i) |
| 63 | ArgTypes.push_back((*i)->getType()); |
| 64 | } |
| 65 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 66 | CGFunctionInfo::CGFunctionInfo(QualType ResTy, const CallArgList &Args) { |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 67 | ArgTypes.push_back(ResTy); |
| 68 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 69 | i != e; ++i) |
| 70 | ArgTypes.push_back(i->second); |
| 71 | } |
| 72 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 73 | CGFunctionInfo::CGFunctionInfo(QualType ResTy, const FunctionArgList &Args) { |
| 74 | ArgTypes.push_back(ResTy); |
| 75 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 76 | i != e; ++i) |
| 77 | ArgTypes.push_back(i->second); |
| 78 | } |
| 79 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 80 | ArgTypeIterator CGFunctionInfo::argtypes_begin() const { |
| 81 | return ArgTypes.begin(); |
| 82 | } |
| 83 | |
| 84 | ArgTypeIterator CGFunctionInfo::argtypes_end() const { |
| 85 | return ArgTypes.end(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /***/ |
| 89 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 90 | /// ABIArgInfo - Helper class to encapsulate information about how a |
| 91 | /// specific C type should be passed to or returned from a function. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 92 | class ABIArgInfo { |
| 93 | public: |
| 94 | enum Kind { |
| 95 | Default, |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 96 | StructRet, /// Only valid for return values. The return value |
| 97 | /// should be passed through a pointer to a caller |
| 98 | /// allocated location passed as an implicit first |
| 99 | /// argument to the function. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 100 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 101 | Ignore, /// Ignore the argument (treat as void). Useful for |
| 102 | /// void and empty structs. |
| 103 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 104 | Coerce, /// Only valid for aggregate return types, the argument |
| 105 | /// should be accessed by coercion to a provided type. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 106 | |
| 107 | ByVal, /// Only valid for aggregate argument types. The |
| 108 | /// structure should be passed "byval" with the |
| 109 | /// specified alignment (0 indicates default |
| 110 | /// alignment). |
| 111 | |
| 112 | Expand, /// Only valid for aggregate argument types. The |
| 113 | /// structure should be expanded into consecutive |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 114 | /// arguments for its constituent fields. Currently |
| 115 | /// expand is only allowed on structures whose fields |
| 116 | /// are all scalar types or are themselves expandable |
| 117 | /// types. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 118 | |
| 119 | KindFirst=Default, KindLast=Expand |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | private: |
| 123 | Kind TheKind; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 124 | const llvm::Type *TypeData; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 125 | unsigned UIntData; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 126 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 127 | ABIArgInfo(Kind K, const llvm::Type *TD=0, |
| 128 | unsigned UI=0) : TheKind(K), |
| 129 | TypeData(TD), |
| 130 | UIntData(0) {} |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 131 | public: |
| 132 | static ABIArgInfo getDefault() { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 133 | return ABIArgInfo(Default); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 134 | } |
| 135 | static ABIArgInfo getStructRet() { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 136 | return ABIArgInfo(StructRet); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 137 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 138 | static ABIArgInfo getIgnore() { |
| 139 | return ABIArgInfo(Ignore); |
| 140 | } |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 141 | static ABIArgInfo getCoerce(const llvm::Type *T) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 142 | return ABIArgInfo(Coerce, T); |
| 143 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 144 | static ABIArgInfo getByVal(unsigned Alignment) { |
| 145 | return ABIArgInfo(ByVal, 0, Alignment); |
| 146 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 147 | static ABIArgInfo getExpand() { |
| 148 | return ABIArgInfo(Expand); |
| 149 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 150 | |
| 151 | Kind getKind() const { return TheKind; } |
| 152 | bool isDefault() const { return TheKind == Default; } |
| 153 | bool isStructRet() const { return TheKind == StructRet; } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 154 | bool isIgnore() const { return TheKind == Ignore; } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 155 | bool isCoerce() const { return TheKind == Coerce; } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 156 | bool isByVal() const { return TheKind == ByVal; } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 157 | bool isExpand() const { return TheKind == Expand; } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 158 | |
| 159 | // Coerce accessors |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 160 | const llvm::Type *getCoerceToType() const { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 161 | assert(TheKind == Coerce && "Invalid kind!"); |
| 162 | return TypeData; |
| 163 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 164 | |
| 165 | // ByVal accessors |
| 166 | unsigned getByValAlignment() const { |
| 167 | assert(TheKind == ByVal && "Invalid kind!"); |
| 168 | return UIntData; |
| 169 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /***/ |
| 173 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 174 | /* FIXME: All of this stuff should be part of the target interface |
| 175 | somehow. It is currently here because it is not clear how to factor |
| 176 | the targets to support this, since the Targets currently live in a |
| 177 | layer below types n'stuff. |
| 178 | */ |
| 179 | |
| 180 | /// ABIInfo - Target specific hooks for defining how a type should be |
| 181 | /// passed or returned from functions. |
| 182 | class clang::ABIInfo { |
| 183 | public: |
| 184 | virtual ~ABIInfo(); |
| 185 | |
| 186 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 187 | ASTContext &Context) const = 0; |
| 188 | |
| 189 | virtual ABIArgInfo classifyArgumentType(QualType Ty, |
| 190 | ASTContext &Context) const = 0; |
| 191 | }; |
| 192 | |
| 193 | ABIInfo::~ABIInfo() {} |
| 194 | |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 195 | /// isEmptyStruct - Return true iff a structure has no non-empty |
| 196 | /// members. Note that a structure with a flexible array member is not |
| 197 | /// considered empty. |
| 198 | static bool isEmptyStruct(QualType T) { |
| 199 | const RecordType *RT = T->getAsStructureType(); |
| 200 | if (!RT) |
| 201 | return 0; |
| 202 | const RecordDecl *RD = RT->getDecl(); |
| 203 | if (RD->hasFlexibleArrayMember()) |
| 204 | return false; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 205 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 206 | e = RD->field_end(); i != e; ++i) { |
| 207 | const FieldDecl *FD = *i; |
| 208 | if (!isEmptyStruct(FD->getType())) |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | /// isSingleElementStruct - Determine if a structure is a "single |
| 215 | /// element struct", i.e. it has exactly one non-empty field or |
| 216 | /// exactly one field which is itself a single element |
| 217 | /// struct. Structures with flexible array members are never |
| 218 | /// considered single element structs. |
| 219 | /// |
| 220 | /// \return The field declaration for the single non-empty field, if |
| 221 | /// it exists. |
| 222 | static const FieldDecl *isSingleElementStruct(QualType T) { |
| 223 | const RecordType *RT = T->getAsStructureType(); |
| 224 | if (!RT) |
| 225 | return 0; |
| 226 | |
| 227 | const RecordDecl *RD = RT->getDecl(); |
| 228 | if (RD->hasFlexibleArrayMember()) |
| 229 | return 0; |
| 230 | |
| 231 | const FieldDecl *Found = 0; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 232 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 233 | e = RD->field_end(); i != e; ++i) { |
| 234 | const FieldDecl *FD = *i; |
| 235 | QualType FT = FD->getType(); |
| 236 | |
| 237 | if (isEmptyStruct(FT)) { |
| 238 | // Ignore |
| 239 | } else if (Found) { |
| 240 | return 0; |
| 241 | } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 242 | Found = FD; |
| 243 | } else { |
| 244 | Found = isSingleElementStruct(FT); |
| 245 | if (!Found) |
| 246 | return 0; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return Found; |
| 251 | } |
| 252 | |
| 253 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 254 | if (!Ty->getAsBuiltinType() && !Ty->isPointerType()) |
| 255 | return false; |
| 256 | |
| 257 | uint64_t Size = Context.getTypeSize(Ty); |
| 258 | return Size == 32 || Size == 64; |
| 259 | } |
| 260 | |
| 261 | static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, |
| 262 | ASTContext &Context) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 263 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 264 | e = RD->field_end(); i != e; ++i) { |
| 265 | const FieldDecl *FD = *i; |
| 266 | |
| 267 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 268 | return false; |
| 269 | |
| 270 | // If this is a bit-field we need to make sure it is still a |
| 271 | // 32-bit or 64-bit type. |
| 272 | if (Expr *BW = FD->getBitWidth()) { |
| 273 | unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue(); |
| 274 | if (Width <= 16) |
| 275 | return false; |
| 276 | } |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 281 | namespace { |
| 282 | /// DefaultABIInfo - The default implementation for ABI specific |
| 283 | /// details. This implementation provides information which results in |
| 284 | /// sensible LLVM IR generation, but does not conform to any |
| 285 | /// particular ABI. |
| 286 | class DefaultABIInfo : public ABIInfo { |
| 287 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 288 | ASTContext &Context) const; |
| 289 | |
| 290 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 291 | ASTContext &Context) const; |
| 292 | }; |
| 293 | |
| 294 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 295 | class X86_32ABIInfo : public ABIInfo { |
| 296 | public: |
| 297 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 298 | ASTContext &Context) const; |
| 299 | |
| 300 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 301 | ASTContext &Context) const; |
| 302 | }; |
| 303 | } |
| 304 | |
| 305 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 306 | ASTContext &Context) const { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 307 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 308 | // Classify "single element" structs as their element type. |
| 309 | const FieldDecl *SeltFD = isSingleElementStruct(RetTy); |
| 310 | if (SeltFD) { |
| 311 | QualType SeltTy = SeltFD->getType()->getDesugaredType(); |
| 312 | if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) { |
| 313 | // FIXME: This is gross, it would be nice if we could just |
| 314 | // pass back SeltTy and have clients deal with it. Is it worth |
| 315 | // supporting coerce to both LLVM and clang Types? |
| 316 | if (BT->isIntegerType()) { |
| 317 | uint64_t Size = Context.getTypeSize(SeltTy); |
| 318 | return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size)); |
| 319 | } else if (BT->getKind() == BuiltinType::Float) { |
| 320 | return ABIArgInfo::getCoerce(llvm::Type::FloatTy); |
| 321 | } else if (BT->getKind() == BuiltinType::Double) { |
| 322 | return ABIArgInfo::getCoerce(llvm::Type::DoubleTy); |
| 323 | } |
| 324 | } else if (SeltTy->isPointerType()) { |
| 325 | // FIXME: It would be really nice if this could come out as |
| 326 | // the proper pointer type. |
| 327 | llvm::Type *PtrTy = |
| 328 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 329 | return ABIArgInfo::getCoerce(PtrTy); |
| 330 | } |
| 331 | } |
| 332 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 333 | uint64_t Size = Context.getTypeSize(RetTy); |
| 334 | if (Size == 8) { |
| 335 | return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); |
| 336 | } else if (Size == 16) { |
| 337 | return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); |
| 338 | } else if (Size == 32) { |
| 339 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 340 | } else if (Size == 64) { |
| 341 | return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); |
| 342 | } else { |
| 343 | return ABIArgInfo::getStructRet(); |
| 344 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 345 | } else { |
| 346 | return ABIArgInfo::getDefault(); |
| 347 | } |
| 348 | } |
| 349 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 350 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 351 | ASTContext &Context) const { |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 352 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 353 | // Structures with flexible arrays are always byval. |
| 354 | if (const RecordType *RT = Ty->getAsStructureType()) |
| 355 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 356 | return ABIArgInfo::getByVal(0); |
| 357 | |
| 358 | // Expand empty structs (i.e. ignore) |
| 359 | uint64_t Size = Context.getTypeSize(Ty); |
| 360 | if (Ty->isStructureType() && Size == 0) |
| 361 | return ABIArgInfo::getExpand(); |
| 362 | |
| 363 | // Expand structs with size <= 128-bits which consist only of |
| 364 | // basic types (int, long long, float, double, xxx*). This is |
| 365 | // non-recursive and does not ignore empty fields. |
| 366 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 367 | if (Context.getTypeSize(Ty) <= 4*32 && |
| 368 | areAllFields32Or64BitBasicType(RT->getDecl(), Context)) |
| 369 | return ABIArgInfo::getExpand(); |
| 370 | } |
| 371 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 372 | return ABIArgInfo::getByVal(0); |
| 373 | } else { |
| 374 | return ABIArgInfo::getDefault(); |
| 375 | } |
| 376 | } |
| 377 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 378 | namespace { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 379 | /// X86_64ABIInfo - The X86_64 ABI information. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 380 | class X86_64ABIInfo : public ABIInfo { |
| 381 | enum Class { |
| 382 | Integer = 0, |
| 383 | SSE, |
| 384 | SSEUp, |
| 385 | X87, |
| 386 | X87Up, |
| 387 | ComplexX87, |
| 388 | NoClass, |
| 389 | Memory |
| 390 | }; |
| 391 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 392 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 393 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 394 | /// Merge an accumulating classification \arg Accum with a field |
| 395 | /// classification \arg Field. |
| 396 | /// |
| 397 | /// \param Accum - The accumulating classification. This should |
| 398 | /// always be either NoClass or the result of a previous merge |
| 399 | /// call. In addition, this should never be Memory (the caller |
| 400 | /// should just return Memory for the aggregate). |
| 401 | Class merge(Class Accum, Class Field) const; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 402 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 403 | /// classify - Determine the x86_64 register classes in which the |
| 404 | /// given type T should be passed. |
| 405 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 406 | /// \param Lo - The classification for the parts of the type |
| 407 | /// residing in the low word of the containing object. |
| 408 | /// |
| 409 | /// \param Hi - The classification for the parts of the type |
| 410 | /// residing in the high word of the containing object. |
| 411 | /// |
| 412 | /// \param OffsetBase - The bit offset of this type in the |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 413 | /// containing object. Some parameters are classified different |
| 414 | /// depending on whether they straddle an eightbyte boundary. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 415 | /// |
| 416 | /// If a word is unused its result will be NoClass; if a type should |
| 417 | /// be passed in Memory then at least the classification of \arg Lo |
| 418 | /// will be Memory. |
| 419 | /// |
| 420 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 421 | /// |
| 422 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 423 | /// be NoClass. |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 424 | void classify(QualType T, ASTContext &Context, uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 425 | Class &Lo, Class &Hi) const; |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 426 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 427 | public: |
| 428 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 429 | ASTContext &Context) const; |
| 430 | |
| 431 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 432 | ASTContext &Context) const; |
| 433 | }; |
| 434 | } |
| 435 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 436 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, |
| 437 | Class Field) const { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 438 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 439 | // classified recursively so that always two fields are |
| 440 | // considered. The resulting class is calculated according to |
| 441 | // the classes of the fields in the eightbyte: |
| 442 | // |
| 443 | // (a) If both classes are equal, this is the resulting class. |
| 444 | // |
| 445 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 446 | // the other class. |
| 447 | // |
| 448 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 449 | // class. |
| 450 | // |
| 451 | // (d) If one of the classes is INTEGER, the result is the |
| 452 | // INTEGER. |
| 453 | // |
| 454 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 455 | // MEMORY is used as class. |
| 456 | // |
| 457 | // (f) Otherwise class SSE is used. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 458 | assert((Accum == NoClass || Accum == Integer || |
| 459 | Accum == SSE || Accum == SSEUp) && |
| 460 | "Invalid accumulated classification during merge."); |
| 461 | if (Accum == Field || Field == NoClass) |
| 462 | return Accum; |
| 463 | else if (Field == Memory) |
| 464 | return Memory; |
| 465 | else if (Accum == NoClass) |
| 466 | return Field; |
| 467 | else if (Accum == Integer || Field == Integer) |
| 468 | return Integer; |
| 469 | else if (Field == X87 || Field == X87Up || Field == ComplexX87) |
| 470 | return Memory; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 471 | else |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 472 | return SSE; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 475 | void X86_64ABIInfo::classify(QualType Ty, |
| 476 | ASTContext &Context, |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 477 | uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 478 | Class &Lo, Class &Hi) const { |
Daniel Dunbar | 9a82b52 | 2009-02-02 18:06:39 +0000 | [diff] [blame] | 479 | // FIXME: This code can be simplified by introducing a simple value |
| 480 | // class for Class pairs with appropriate constructor methods for |
| 481 | // the various situations. |
| 482 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 483 | Lo = Hi = NoClass; |
| 484 | |
| 485 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 486 | Current = Memory; |
| 487 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 488 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) { |
| 489 | BuiltinType::Kind k = BT->getKind(); |
| 490 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 491 | if (k == BuiltinType::Void) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 492 | Current = NoClass; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 493 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 494 | Current = Integer; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 495 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 496 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 497 | } else if (k == BuiltinType::LongDouble) { |
| 498 | Lo = X87; |
| 499 | Hi = X87Up; |
| 500 | } |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 501 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 502 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 503 | // FIXME: __int128 is (Integer, Integer). |
| 504 | } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() || |
| 505 | Ty->isObjCQualifiedInterfaceType()) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 506 | Current = Integer; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 507 | } else if (const VectorType *VT = Ty->getAsVectorType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 508 | uint64_t Size = Context.getTypeSize(VT); |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 509 | if (Size == 64) { |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 510 | // gcc passes <1 x double> in memory. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 511 | if (VT->getElementType() == Context.DoubleTy) |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 512 | return; |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 513 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 514 | Current = SSE; |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 515 | |
| 516 | // If this type crosses an eightbyte boundary, it should be |
| 517 | // split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 518 | if (OffsetBase && OffsetBase != 64) |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 519 | Hi = Lo; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 520 | } else if (Size == 128) { |
| 521 | Lo = SSE; |
| 522 | Hi = SSEUp; |
| 523 | } |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 524 | } else if (const ComplexType *CT = Ty->getAsComplexType()) { |
| 525 | QualType ET = CT->getElementType(); |
| 526 | |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 527 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 528 | if (ET->isIntegerType()) { |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 529 | if (Size <= 64) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 530 | Current = Integer; |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 531 | else if (Size <= 128) |
| 532 | Lo = Hi = Integer; |
| 533 | } else if (ET == Context.FloatTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 534 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 535 | else if (ET == Context.DoubleTy) |
| 536 | Lo = Hi = SSE; |
| 537 | else if (ET == Context.LongDoubleTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 538 | Current = ComplexX87; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 539 | |
| 540 | // If this complex type crosses an eightbyte boundary then it |
| 541 | // should be split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 542 | uint64_t EB_Real = (OffsetBase) / 64; |
| 543 | uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 544 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 545 | Hi = Lo; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 546 | } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 547 | // Arrays are treated like structures. |
| 548 | |
| 549 | uint64_t Size = Context.getTypeSize(Ty); |
| 550 | |
| 551 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 552 | // than two eightbytes, ..., it has class MEMORY. |
| 553 | if (Size > 128) |
| 554 | return; |
| 555 | |
| 556 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 557 | // fields, it has class MEMORY. |
| 558 | // |
| 559 | // Only need to check alignment of array base. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 560 | if (OffsetBase % Context.getTypeAlign(AT->getElementType())) |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 561 | return; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 562 | |
| 563 | // Otherwise implement simplified merge. We could be smarter about |
| 564 | // this, but it isn't worth it and would be harder to verify. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 565 | Current = NoClass; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 566 | uint64_t EltSize = Context.getTypeSize(AT->getElementType()); |
| 567 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
| 568 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 569 | Class FieldLo, FieldHi; |
| 570 | classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 571 | Lo = merge(Lo, FieldLo); |
| 572 | Hi = merge(Hi, FieldHi); |
| 573 | if (Lo == Memory || Hi == Memory) |
| 574 | break; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 575 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 576 | |
| 577 | // Do post merger cleanup (see below). Only case we worry about is Memory. |
| 578 | if (Hi == Memory) |
| 579 | Lo = Memory; |
| 580 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 581 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 582 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 583 | |
| 584 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 585 | // than two eightbytes, ..., it has class MEMORY. |
| 586 | if (Size > 128) |
| 587 | return; |
| 588 | |
| 589 | const RecordDecl *RD = RT->getDecl(); |
| 590 | |
| 591 | // Assume variable sized types are passed in memory. |
| 592 | if (RD->hasFlexibleArrayMember()) |
| 593 | return; |
| 594 | |
| 595 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 596 | |
| 597 | // Reset Lo class, this will be recomputed. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 598 | Current = NoClass; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 599 | unsigned idx = 0; |
| 600 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 601 | e = RD->field_end(); i != e; ++i, ++idx) { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 602 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 603 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 604 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 605 | // fields, it has class MEMORY. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 606 | if (Offset % Context.getTypeAlign(i->getType())) { |
| 607 | Lo = Memory; |
| 608 | return; |
| 609 | } |
| 610 | |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 611 | // Classify this field. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 612 | // |
| 613 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 614 | // exceeds a single eightbyte, each is classified |
| 615 | // separately. Each eightbyte gets initialized to class |
| 616 | // NO_CLASS. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 617 | Class FieldLo, FieldHi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 618 | classify(i->getType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 619 | Lo = merge(Lo, FieldLo); |
| 620 | Hi = merge(Hi, FieldHi); |
| 621 | if (Lo == Memory || Hi == Memory) |
| 622 | break; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 626 | // |
| 627 | // (a) If one of the classes is MEMORY, the whole argument is |
| 628 | // passed in memory. |
| 629 | // |
| 630 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. |
| 631 | |
| 632 | // The first of these conditions is guaranteed by how we implement |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 633 | // the merge (just bail). |
| 634 | // |
| 635 | // The second condition occurs in the case of unions; for example |
| 636 | // union { _Complex double; unsigned; }. |
| 637 | if (Hi == Memory) |
| 638 | Lo = Memory; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 639 | if (Hi == SSEUp && Lo != SSE) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 640 | Hi = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 644 | |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 645 | ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy, |
| 646 | ASTContext &Context) const { |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 647 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 648 | // classification algorithm. |
| 649 | X86_64ABIInfo::Class Lo, Hi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 650 | classify(RetTy, Context, 0, Lo, Hi); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 651 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 652 | // Check some invariants. |
| 653 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 654 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 655 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 656 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 657 | const llvm::Type *ResType = 0; |
| 658 | switch (Lo) { |
| 659 | case NoClass: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 660 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 661 | |
| 662 | case SSEUp: |
| 663 | case X87Up: |
| 664 | assert(0 && "Invalid classification for lo word."); |
| 665 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 666 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 667 | // hidden argument, i.e. structret. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 668 | case Memory: |
| 669 | return ABIArgInfo::getStructRet(); |
| 670 | |
| 671 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 672 | // available register of the sequence %rax, %rdx is used. |
| 673 | case Integer: |
| 674 | ResType = llvm::Type::Int64Ty; break; |
| 675 | |
| 676 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 677 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 678 | case SSE: |
| 679 | ResType = llvm::Type::DoubleTy; break; |
| 680 | |
| 681 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 682 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 683 | case X87: |
| 684 | ResType = llvm::Type::X86_FP80Ty; break; |
| 685 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 686 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 687 | // part of the value is returned in %st0 and the imaginary part in |
| 688 | // %st1. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 689 | case ComplexX87: |
| 690 | assert(Hi == NoClass && "Unexpected ComplexX87 classification."); |
| 691 | ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2); |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | switch (Hi) { |
| 696 | // Memory was handled previously, and ComplexX87 and X87 should |
| 697 | // never occur as hi classes. |
| 698 | case Memory: |
| 699 | case X87: |
| 700 | case ComplexX87: |
| 701 | assert(0 && "Invalid classification for hi word."); |
| 702 | |
| 703 | case NoClass: break; |
| 704 | case Integer: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 705 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 706 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 707 | case SSE: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 708 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 709 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 710 | |
| 711 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| 712 | // is passed in the upper half of the last used SSE register. |
| 713 | // |
| 714 | // SSEUP should always be preceeded by SSE, just widen. |
| 715 | case SSEUp: |
| 716 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 717 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 718 | break; |
| 719 | |
| 720 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 721 | // returned together with the previous X87 value in %st0. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 722 | // |
| 723 | // X87UP should always be preceeded by X87, so we don't need to do |
| 724 | // anything here. |
| 725 | case X87Up: |
| 726 | assert(Lo == X87 && "Unexpected X87Up classification."); |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | return ABIArgInfo::getCoerce(ResType); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, |
| 734 | ASTContext &Context) const { |
| 735 | return ABIArgInfo::getDefault(); |
| 736 | } |
| 737 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 738 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, |
| 739 | ASTContext &Context) const { |
| 740 | return ABIArgInfo::getDefault(); |
| 741 | } |
| 742 | |
| 743 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, |
| 744 | ASTContext &Context) const { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 745 | return ABIArgInfo::getDefault(); |
| 746 | } |
| 747 | |
| 748 | const ABIInfo &CodeGenTypes::getABIInfo() const { |
| 749 | if (TheABIInfo) |
| 750 | return *TheABIInfo; |
| 751 | |
| 752 | // For now we just cache this in the CodeGenTypes and don't bother |
| 753 | // to free it. |
| 754 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 755 | if (strcmp(TargetPrefix, "x86") == 0) { |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 756 | switch (getContext().Target.getPointerWidth(0)) { |
| 757 | case 32: |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 758 | return *(TheABIInfo = new X86_32ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 759 | case 64: |
Daniel Dunbar | 11a76ed | 2009-01-30 18:47:53 +0000 | [diff] [blame] | 760 | return *(TheABIInfo = new X86_64ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 761 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | return *(TheABIInfo = new DefaultABIInfo); |
| 765 | } |
| 766 | |
| 767 | // getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering |
| 768 | // "default" types to StructRet when appropriate for simplicity. |
| 769 | static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) { |
| 770 | assert(!Ty->isArrayType() && |
| 771 | "Array types cannot be passed directly."); |
| 772 | ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext()); |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 773 | // Ensure default on aggregate types is StructRet. |
| 774 | if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 775 | return ABIArgInfo::getStructRet(); |
| 776 | return Info; |
| 777 | } |
| 778 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 779 | // getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering |
| 780 | // "default" types to ByVal when appropriate for simplicity. |
| 781 | static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) { |
| 782 | assert(!Ty->isArrayType() && |
| 783 | "Array types cannot be passed directly."); |
| 784 | ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext()); |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 785 | // Ensure default on aggregate types is ByVal. |
| 786 | if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 787 | return ABIArgInfo::getByVal(0); |
| 788 | return Info; |
| 789 | } |
| 790 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 791 | /***/ |
| 792 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 793 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
| 794 | std::vector<const llvm::Type*> &ArgTys) { |
| 795 | const RecordType *RT = Ty->getAsStructureType(); |
| 796 | assert(RT && "Can only expand structure types."); |
| 797 | const RecordDecl *RD = RT->getDecl(); |
| 798 | assert(!RD->hasFlexibleArrayMember() && |
| 799 | "Cannot expand structure with flexible array."); |
| 800 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 801 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 802 | e = RD->field_end(); i != e; ++i) { |
| 803 | const FieldDecl *FD = *i; |
| 804 | assert(!FD->isBitField() && |
| 805 | "Cannot expand structure with bit-field members."); |
| 806 | |
| 807 | QualType FT = FD->getType(); |
| 808 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 809 | GetExpandedTypes(FT, ArgTys); |
| 810 | } else { |
| 811 | ArgTys.push_back(ConvertType(FT)); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | llvm::Function::arg_iterator |
| 817 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 818 | llvm::Function::arg_iterator AI) { |
| 819 | const RecordType *RT = Ty->getAsStructureType(); |
| 820 | assert(RT && "Can only expand structure types."); |
| 821 | |
| 822 | RecordDecl *RD = RT->getDecl(); |
| 823 | assert(LV.isSimple() && |
| 824 | "Unexpected non-simple lvalue during struct expansion."); |
| 825 | llvm::Value *Addr = LV.getAddress(); |
| 826 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 827 | e = RD->field_end(); i != e; ++i) { |
| 828 | FieldDecl *FD = *i; |
| 829 | QualType FT = FD->getType(); |
| 830 | |
| 831 | // FIXME: What are the right qualifiers here? |
| 832 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 833 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 834 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 835 | } else { |
| 836 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 837 | ++AI; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | return AI; |
| 842 | } |
| 843 | |
| 844 | void |
| 845 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
| 846 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 847 | const RecordType *RT = Ty->getAsStructureType(); |
| 848 | assert(RT && "Can only expand structure types."); |
| 849 | |
| 850 | RecordDecl *RD = RT->getDecl(); |
| 851 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 852 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 853 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 854 | e = RD->field_end(); i != e; ++i) { |
| 855 | FieldDecl *FD = *i; |
| 856 | QualType FT = FD->getType(); |
| 857 | |
| 858 | // FIXME: What are the right qualifiers here? |
| 859 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 860 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 861 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 862 | } else { |
| 863 | RValue RV = EmitLoadOfLValue(LV, FT); |
| 864 | assert(RV.isScalar() && |
| 865 | "Unexpected non-scalar rvalue during struct expansion."); |
| 866 | Args.push_back(RV.getScalarVal()); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 871 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 872 | /// a pointer to an object of type \arg Ty. |
| 873 | /// |
| 874 | /// This safely handles the case when the src type is smaller than the |
| 875 | /// destination type; in this situation the values of bits which not |
| 876 | /// present in the src are undefined. |
| 877 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 878 | const llvm::Type *Ty, |
| 879 | CodeGenFunction &CGF) { |
| 880 | const llvm::Type *SrcTy = |
| 881 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 882 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 883 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty); |
| 884 | |
| 885 | // If load is legal, just bitcase the src pointer. |
| 886 | if (SrcSize == DstSize) { |
| 887 | llvm::Value *Casted = |
| 888 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
| 889 | return CGF.Builder.CreateLoad(Casted); |
| 890 | } else { |
| 891 | assert(SrcSize < DstSize && "Coercion is losing source bits!"); |
| 892 | |
| 893 | // Otherwise do coercion through memory. This is stupid, but |
| 894 | // simple. |
| 895 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 896 | llvm::Value *Casted = |
| 897 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
| 898 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 899 | return CGF.Builder.CreateLoad(Tmp); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 904 | /// where the source and destination may have different types. |
| 905 | /// |
| 906 | /// This safely handles the case when the src type is larger than the |
| 907 | /// destination type; the upper bits of the src will be lost. |
| 908 | static void CreateCoercedStore(llvm::Value *Src, |
| 909 | llvm::Value *DstPtr, |
| 910 | CodeGenFunction &CGF) { |
| 911 | const llvm::Type *SrcTy = Src->getType(); |
| 912 | const llvm::Type *DstTy = |
| 913 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 914 | |
| 915 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 916 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy); |
| 917 | |
| 918 | // If store is legal, just bitcase the src pointer. |
| 919 | if (SrcSize == DstSize) { |
| 920 | llvm::Value *Casted = |
| 921 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
| 922 | CGF.Builder.CreateStore(Src, Casted); |
| 923 | } else { |
| 924 | assert(SrcSize > DstSize && "Coercion is missing bits!"); |
| 925 | |
| 926 | // Otherwise do coercion through memory. This is stupid, but |
| 927 | // simple. |
| 928 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 929 | CGF.Builder.CreateStore(Src, Tmp); |
| 930 | llvm::Value *Casted = |
| 931 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
| 932 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr); |
| 933 | } |
| 934 | } |
| 935 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 936 | /***/ |
| 937 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 938 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { |
| 939 | return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 942 | const llvm::FunctionType * |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 943 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 944 | std::vector<const llvm::Type*> ArgTys; |
| 945 | |
| 946 | const llvm::Type *ResultType = 0; |
| 947 | |
Daniel Dunbar | 36b5f5e | 2009-01-31 03:05:44 +0000 | [diff] [blame] | 948 | ArgTypeIterator begin = FI.argtypes_begin(), end = FI.argtypes_end(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 949 | QualType RetTy = *begin; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 950 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 951 | switch (RetAI.getKind()) { |
| 952 | case ABIArgInfo::ByVal: |
| 953 | case ABIArgInfo::Expand: |
| 954 | assert(0 && "Invalid ABI kind for return argument"); |
| 955 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 956 | case ABIArgInfo::Default: |
| 957 | if (RetTy->isVoidType()) { |
| 958 | ResultType = llvm::Type::VoidTy; |
| 959 | } else { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 960 | ResultType = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 961 | } |
| 962 | break; |
| 963 | |
| 964 | case ABIArgInfo::StructRet: { |
| 965 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 966 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 967 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 968 | break; |
| 969 | } |
| 970 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 971 | case ABIArgInfo::Ignore: |
| 972 | ResultType = llvm::Type::VoidTy; |
| 973 | break; |
| 974 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 975 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 976 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 977 | break; |
| 978 | } |
| 979 | |
| 980 | for (++begin; begin != end; ++begin) { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 981 | ABIArgInfo AI = getABIArgumentInfo(*begin, *this); |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 982 | const llvm::Type *Ty = ConvertType(*begin); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 983 | |
| 984 | switch (AI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 985 | case ABIArgInfo::Ignore: |
| 986 | break; |
| 987 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 988 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 989 | case ABIArgInfo::StructRet: |
| 990 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 991 | |
| 992 | case ABIArgInfo::ByVal: |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 993 | // byval arguments are always on the stack, which is addr space #0. |
| 994 | ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 995 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 996 | break; |
| 997 | |
| 998 | case ABIArgInfo::Default: |
| 999 | ArgTys.push_back(Ty); |
| 1000 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1001 | |
| 1002 | case ABIArgInfo::Expand: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1003 | GetExpandedTypes(*begin, ArgTys); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1008 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 1011 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &Info, |
| 1012 | const Decl *TargetDecl, |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1013 | AttributeListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1014 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1015 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1016 | |
| 1017 | if (TargetDecl) { |
| 1018 | if (TargetDecl->getAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1019 | FuncAttrs |= llvm::Attribute::NoUnwind; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1020 | if (TargetDecl->getAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1021 | FuncAttrs |= llvm::Attribute::NoReturn; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1022 | if (TargetDecl->getAttr<PureAttr>()) |
| 1023 | FuncAttrs |= llvm::Attribute::ReadOnly; |
| 1024 | if (TargetDecl->getAttr<ConstAttr>()) |
| 1025 | FuncAttrs |= llvm::Attribute::ReadNone; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1028 | ArgTypeIterator begin = Info.argtypes_begin(), end = Info.argtypes_end(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1029 | QualType RetTy = *begin; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1030 | unsigned Index = 1; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1031 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1032 | switch (RetAI.getKind()) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1033 | case ABIArgInfo::Default: |
| 1034 | if (RetTy->isPromotableIntegerType()) { |
| 1035 | if (RetTy->isSignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1036 | RetAttrs |= llvm::Attribute::SExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1037 | } else if (RetTy->isUnsignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1038 | RetAttrs |= llvm::Attribute::ZExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | break; |
| 1042 | |
| 1043 | case ABIArgInfo::StructRet: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1044 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1045 | llvm::Attribute::StructRet | |
| 1046 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1047 | ++Index; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1048 | break; |
| 1049 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1050 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1051 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1052 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1053 | |
| 1054 | case ABIArgInfo::ByVal: |
| 1055 | case ABIArgInfo::Expand: |
| 1056 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1057 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1058 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1059 | if (RetAttrs) |
| 1060 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1061 | for (++begin; begin != end; ++begin) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1062 | QualType ParamType = *begin; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1063 | unsigned Attributes = 0; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1064 | ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1065 | |
| 1066 | switch (AI.getKind()) { |
| 1067 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1068 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1069 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1070 | |
| 1071 | case ABIArgInfo::ByVal: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1072 | Attributes |= llvm::Attribute::ByVal; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1073 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 1074 | break; |
| 1075 | |
| 1076 | case ABIArgInfo::Default: |
| 1077 | if (ParamType->isPromotableIntegerType()) { |
| 1078 | if (ParamType->isSignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1079 | Attributes |= llvm::Attribute::SExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1080 | } else if (ParamType->isUnsignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1081 | Attributes |= llvm::Attribute::ZExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1082 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1083 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1084 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1085 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1086 | case ABIArgInfo::Ignore: |
| 1087 | // Skip increment, no matching LLVM parameter. |
| 1088 | continue; |
| 1089 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1090 | case ABIArgInfo::Expand: { |
| 1091 | std::vector<const llvm::Type*> Tys; |
| 1092 | // FIXME: This is rather inefficient. Do we ever actually need |
| 1093 | // to do anything here? The result should be just reconstructed |
| 1094 | // on the other side, so extension should be a non-issue. |
| 1095 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 1096 | Index += Tys.size(); |
| 1097 | continue; |
| 1098 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1099 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1100 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1101 | if (Attributes) |
| 1102 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1103 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1104 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1105 | if (FuncAttrs) |
| 1106 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
| 1107 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 1110 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1111 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1112 | const FunctionArgList &Args) { |
| 1113 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 1114 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 1115 | |
| 1116 | // Name the struct return argument. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 1117 | if (CGM.ReturnTypeUsesSret(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1118 | AI->setName("agg.result"); |
| 1119 | ++AI; |
| 1120 | } |
| 1121 | |
| 1122 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1123 | i != e; ++i) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1124 | const VarDecl *Arg = i->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1125 | QualType Ty = i->second; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1126 | ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1127 | |
| 1128 | switch (ArgI.getKind()) { |
| 1129 | case ABIArgInfo::ByVal: |
| 1130 | case ABIArgInfo::Default: { |
| 1131 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1132 | llvm::Value* V = AI; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1133 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1134 | // This must be a promotion, for something like |
| 1135 | // "void a(x) short x; {..." |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1136 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1137 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1138 | EmitParmDecl(*Arg, V); |
| 1139 | break; |
| 1140 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1141 | |
| 1142 | case ABIArgInfo::Expand: { |
| 1143 | // If this was structure was expand into multiple arguments then |
| 1144 | // we need to create a temporary and reconstruct it from the |
| 1145 | // arguments. |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1146 | std::string Name = Arg->getNameAsString(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1147 | llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty), |
| 1148 | (Name + ".addr").c_str()); |
| 1149 | // FIXME: What are the right qualifiers here? |
| 1150 | llvm::Function::arg_iterator End = |
| 1151 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); |
| 1152 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1153 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1154 | // Name the arguments used in expansion and increment AI. |
| 1155 | unsigned Index = 0; |
| 1156 | for (; AI != End; ++AI, ++Index) |
| 1157 | AI->setName(Name + "." + llvm::utostr(Index)); |
| 1158 | continue; |
| 1159 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1160 | |
| 1161 | case ABIArgInfo::Ignore: |
| 1162 | break; |
| 1163 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1164 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1165 | case ABIArgInfo::StructRet: |
| 1166 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1167 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1168 | |
| 1169 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1170 | } |
| 1171 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 1172 | } |
| 1173 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 1174 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1175 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1176 | llvm::Value *RV = 0; |
| 1177 | |
| 1178 | // Functions with no result always return void. |
| 1179 | if (ReturnValue) { |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 1180 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1181 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1182 | |
| 1183 | switch (RetAI.getKind()) { |
| 1184 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1185 | if (RetTy->isAnyComplexType()) { |
| 1186 | // FIXME: Volatile |
| 1187 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 1188 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 1189 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1190 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 1191 | } else { |
| 1192 | Builder.CreateStore(Builder.CreateLoad(ReturnValue), |
| 1193 | CurFn->arg_begin()); |
| 1194 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1195 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1196 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1197 | case ABIArgInfo::Default: |
| 1198 | RV = Builder.CreateLoad(ReturnValue); |
| 1199 | break; |
| 1200 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1201 | case ABIArgInfo::Ignore: |
| 1202 | break; |
| 1203 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1204 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1205 | RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1206 | break; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1207 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1208 | |
| 1209 | case ABIArgInfo::ByVal: |
| 1210 | case ABIArgInfo::Expand: |
| 1211 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1214 | |
| 1215 | if (RV) { |
| 1216 | Builder.CreateRet(RV); |
| 1217 | } else { |
| 1218 | Builder.CreateRetVoid(); |
| 1219 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 1222 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
| 1223 | llvm::Value *Callee, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1224 | const CallArgList &CallArgs) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1225 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1226 | |
| 1227 | // Handle struct-return functions by passing a pointer to the |
| 1228 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1229 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1230 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1231 | switch (RetAI.getKind()) { |
| 1232 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1233 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1234 | Args.push_back(CreateTempAlloca(ConvertType(RetTy))); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | |
| 1237 | case ABIArgInfo::Default: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1238 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1239 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1240 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1241 | |
| 1242 | case ABIArgInfo::ByVal: |
| 1243 | case ABIArgInfo::Expand: |
| 1244 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
| 1248 | I != E; ++I) { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1249 | ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1250 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1251 | |
| 1252 | switch (ArgInfo.getKind()) { |
| 1253 | case ABIArgInfo::ByVal: // Default is byval |
| 1254 | case ABIArgInfo::Default: |
| 1255 | if (RV.isScalar()) { |
| 1256 | Args.push_back(RV.getScalarVal()); |
| 1257 | } else if (RV.isComplex()) { |
| 1258 | // Make a temporary alloca to pass the argument. |
| 1259 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 1260 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 1261 | } else { |
| 1262 | Args.push_back(RV.getAggregateAddr()); |
| 1263 | } |
| 1264 | break; |
| 1265 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1266 | case ABIArgInfo::Ignore: |
| 1267 | break; |
| 1268 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1269 | case ABIArgInfo::StructRet: |
| 1270 | case ABIArgInfo::Coerce: |
| 1271 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1272 | break; |
| 1273 | |
| 1274 | case ABIArgInfo::Expand: |
| 1275 | ExpandTypeToArgs(I->second, RV, Args); |
| 1276 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1281 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1282 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1283 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame^] | 1284 | CGM.ConstructAttributeList(CallInfo, 0, AttributeList); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1285 | CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1286 | AttributeList.size())); |
| 1287 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1288 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 1289 | CI->setCallingConv(F->getCallingConv()); |
| 1290 | if (CI->getType() != llvm::Type::VoidTy) |
| 1291 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1292 | |
| 1293 | switch (RetAI.getKind()) { |
| 1294 | case ABIArgInfo::StructRet: |
| 1295 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1296 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1297 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1298 | return RValue::getAggregate(Args[0]); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1299 | else |
| 1300 | return RValue::get(Builder.CreateLoad(Args[0])); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1301 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1302 | case ABIArgInfo::Default: |
| 1303 | return RValue::get(RetTy->isVoidType() ? 0 : CI); |
| 1304 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1305 | case ABIArgInfo::Ignore: |
Daniel Dunbar | cc039fe | 2009-01-29 08:24:57 +0000 | [diff] [blame] | 1306 | if (RetTy->isVoidType()) |
| 1307 | return RValue::get(0); |
| 1308 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1309 | llvm::Value *Res = |
| 1310 | llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy))); |
| 1311 | return RValue::getAggregate(Res); |
| 1312 | } |
| 1313 | return RValue::get(llvm::UndefValue::get(ConvertType(RetTy))); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1314 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1315 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1316 | llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce"); |
| 1317 | CreateCoercedStore(CI, V, *this); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1318 | if (RetTy->isAnyComplexType()) |
| 1319 | return RValue::getComplex(LoadComplexFromAddr(V, false)); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1320 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1321 | return RValue::getAggregate(V); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1322 | else |
| 1323 | return RValue::get(Builder.CreateLoad(V)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1324 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1325 | |
| 1326 | case ABIArgInfo::ByVal: |
| 1327 | case ABIArgInfo::Expand: |
| 1328 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1329 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1330 | |
| 1331 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 1332 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1333 | } |