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