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