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 | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclObjC.h" |
| 21 | #include "llvm/ParameterAttributes.h" |
| 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | /***/ |
| 26 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 27 | // FIXME: Use iterator and sidestep silly type array creation. |
| 28 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 29 | CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP) |
| 30 | : IsVariadic(true) |
| 31 | { |
| 32 | ArgTypes.push_back(FTNP->getResultType()); |
| 33 | } |
| 34 | |
| 35 | CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP) |
| 36 | : IsVariadic(FTP->isVariadic()) |
| 37 | { |
| 38 | ArgTypes.push_back(FTP->getResultType()); |
| 39 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 40 | ArgTypes.push_back(FTP->getArgType(i)); |
| 41 | } |
| 42 | |
| 43 | // FIXME: Is there really any reason to have this still? |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 44 | CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD) |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 45 | { |
| 46 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
| 47 | const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 48 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 49 | ArgTypes.push_back(FTy->getResultType()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 50 | if (FTP) { |
| 51 | IsVariadic = FTP->isVariadic(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 52 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 53 | ArgTypes.push_back(FTP->getArgType(i)); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 54 | } else { |
| 55 | IsVariadic = true; |
| 56 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD, |
| 60 | const ASTContext &Context) |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 61 | : IsVariadic(MD->isVariadic()) |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 62 | { |
| 63 | ArgTypes.push_back(MD->getResultType()); |
| 64 | ArgTypes.push_back(MD->getSelfDecl()->getType()); |
| 65 | ArgTypes.push_back(Context.getObjCSelType()); |
| 66 | for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), |
| 67 | e = MD->param_end(); i != e; ++i) |
| 68 | ArgTypes.push_back((*i)->getType()); |
| 69 | } |
| 70 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 71 | ArgTypeIterator CGFunctionInfo::argtypes_begin() const { |
| 72 | return ArgTypes.begin(); |
| 73 | } |
| 74 | |
| 75 | ArgTypeIterator CGFunctionInfo::argtypes_end() const { |
| 76 | return ArgTypes.end(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /***/ |
| 80 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 81 | CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) { |
| 82 | ArgTypes.push_back(_ResultType); |
| 83 | 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] | 84 | ArgTypes.push_back(i->second); |
| 85 | } |
| 86 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 87 | ArgTypeIterator CGCallInfo::argtypes_begin() const { |
| 88 | return ArgTypes.begin(); |
| 89 | } |
| 90 | |
| 91 | ArgTypeIterator CGCallInfo::argtypes_end() const { |
| 92 | return ArgTypes.end(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 93 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 94 | |
| 95 | /***/ |
| 96 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 97 | class ABIArgInfo { |
| 98 | public: |
| 99 | enum Kind { |
| 100 | Default, |
| 101 | StructRet, // Only valid for struct return types |
| 102 | Coerce // Only valid for return types |
| 103 | }; |
| 104 | |
| 105 | private: |
| 106 | Kind TheKind; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 107 | const llvm::Type *TypeData; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 108 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 109 | ABIArgInfo(Kind K, const llvm::Type *TD) : TheKind(K), |
| 110 | TypeData(TD) {} |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 111 | public: |
| 112 | static ABIArgInfo getDefault() { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 113 | return ABIArgInfo(Default, 0); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 114 | } |
| 115 | static ABIArgInfo getStructRet() { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 116 | return ABIArgInfo(StructRet, 0); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 117 | } |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 118 | static ABIArgInfo getCoerce(const llvm::Type *T) { |
| 119 | assert(T->isSingleValueType() && "Can only coerce to simple types"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 120 | return ABIArgInfo(Coerce, T); |
| 121 | } |
| 122 | |
| 123 | Kind getKind() const { return TheKind; } |
| 124 | bool isDefault() const { return TheKind == Default; } |
| 125 | bool isStructRet() const { return TheKind == StructRet; } |
| 126 | bool isCoerce() const { return TheKind == Coerce; } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 127 | |
| 128 | // Coerce accessors |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 129 | const llvm::Type *getCoerceToType() const { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 130 | assert(TheKind == Coerce && "Invalid kind!"); |
| 131 | return TypeData; |
| 132 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /***/ |
| 136 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 137 | static ABIArgInfo classifyReturnType(QualType RetTy, |
| 138 | ASTContext &Context) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame^] | 139 | assert(!RetTy->isArrayType() && |
| 140 | "Array types cannot be passed directly."); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 141 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 142 | uint64_t Size = Context.getTypeSize(RetTy); |
| 143 | if (Size == 8) { |
| 144 | return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); |
| 145 | } else if (Size == 16) { |
| 146 | return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); |
| 147 | } else if (Size == 32) { |
| 148 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 149 | } else if (Size == 64) { |
| 150 | return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); |
| 151 | } else { |
| 152 | return ABIArgInfo::getStructRet(); |
| 153 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 154 | } else { |
| 155 | return ABIArgInfo::getDefault(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /***/ |
| 160 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 161 | const llvm::FunctionType * |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 162 | CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) { |
| 163 | return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic); |
| 164 | } |
| 165 | |
| 166 | const llvm::FunctionType * |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 167 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 168 | return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic()); |
| 169 | } |
| 170 | |
| 171 | const llvm::FunctionType * |
| 172 | CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end, |
| 173 | bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 174 | std::vector<const llvm::Type*> ArgTys; |
| 175 | |
| 176 | const llvm::Type *ResultType = 0; |
| 177 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 178 | QualType RetTy = *begin; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 179 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 180 | switch (RetAI.getKind()) { |
| 181 | case ABIArgInfo::Default: |
| 182 | if (RetTy->isVoidType()) { |
| 183 | ResultType = llvm::Type::VoidTy; |
| 184 | } else { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 185 | ResultType = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 186 | } |
| 187 | break; |
| 188 | |
| 189 | case ABIArgInfo::StructRet: { |
| 190 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 191 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 192 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 197 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | |
| 201 | for (++begin; begin != end; ++begin) { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 202 | const llvm::Type *Ty = ConvertType(*begin); |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame^] | 203 | assert(!(*begin)->isArrayType() && |
| 204 | "Array types cannot be passed directly."); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 205 | if (Ty->isSingleValueType()) |
| 206 | ArgTys.push_back(Ty); |
| 207 | else |
| 208 | // byval arguments are always on the stack, which is addr space #0. |
| 209 | ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); |
| 210 | } |
| 211 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 212 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 215 | bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 216 | return classifyReturnType(RetTy, getContext()).isStructRet(); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 219 | void CodeGenModule::ConstructParamAttrList(const Decl *TargetDecl, |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 220 | ArgTypeIterator begin, |
| 221 | ArgTypeIterator end, |
| 222 | ParamAttrListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 223 | unsigned FuncAttrs = 0; |
| 224 | |
| 225 | if (TargetDecl) { |
| 226 | if (TargetDecl->getAttr<NoThrowAttr>()) |
| 227 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
| 228 | if (TargetDecl->getAttr<NoReturnAttr>()) |
| 229 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 230 | } |
| 231 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 232 | QualType RetTy = *begin; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 233 | unsigned Index = 1; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 234 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 235 | switch (RetAI.getKind()) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 236 | case ABIArgInfo::Default: |
| 237 | if (RetTy->isPromotableIntegerType()) { |
| 238 | if (RetTy->isSignedIntegerType()) { |
| 239 | FuncAttrs |= llvm::ParamAttr::SExt; |
| 240 | } else if (RetTy->isUnsignedIntegerType()) { |
| 241 | FuncAttrs |= llvm::ParamAttr::ZExt; |
| 242 | } |
| 243 | } |
| 244 | break; |
| 245 | |
| 246 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 247 | PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, |
| 248 | llvm::ParamAttr::StructRet)); |
| 249 | ++Index; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 250 | break; |
| 251 | |
| 252 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 253 | break; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 254 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 255 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 256 | if (FuncAttrs) |
| 257 | PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs)); |
| 258 | for (++begin; begin != end; ++begin, ++Index) { |
| 259 | QualType ParamType = *begin; |
| 260 | unsigned ParamAttrs = 0; |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame^] | 261 | assert(!ParamType->isArrayType() && |
| 262 | "Array types cannot be passed directly."); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 263 | if (ParamType->isRecordType()) |
| 264 | ParamAttrs |= llvm::ParamAttr::ByVal; |
| 265 | if (ParamType->isPromotableIntegerType()) { |
| 266 | if (ParamType->isSignedIntegerType()) { |
| 267 | ParamAttrs |= llvm::ParamAttr::SExt; |
| 268 | } else if (ParamType->isUnsignedIntegerType()) { |
| 269 | ParamAttrs |= llvm::ParamAttr::ZExt; |
| 270 | } |
| 271 | } |
| 272 | if (ParamAttrs) |
| 273 | PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs)); |
| 274 | } |
| 275 | } |
| 276 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 277 | void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn, |
| 278 | QualType RetTy, |
| 279 | const FunctionArgList &Args) { |
| 280 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 281 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 282 | |
| 283 | // Name the struct return argument. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 284 | if (CGM.ReturnTypeUsesSret(RetTy)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 285 | AI->setName("agg.result"); |
| 286 | ++AI; |
| 287 | } |
| 288 | |
| 289 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 290 | i != e; ++i, ++AI) { |
| 291 | const VarDecl *Arg = i->first; |
| 292 | QualType T = i->second; |
| 293 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 294 | llvm::Value* V = AI; |
| 295 | if (!getContext().typesAreCompatible(T, Arg->getType())) { |
| 296 | // This must be a promotion, for something like |
| 297 | // "void a(x) short x; {..." |
| 298 | V = EmitScalarConversion(V, T, Arg->getType()); |
| 299 | } |
| 300 | EmitParmDecl(*Arg, V); |
| 301 | } |
| 302 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 303 | } |
| 304 | |
| 305 | void CodeGenFunction::EmitFunctionEpilog(QualType RetTy, |
| 306 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 307 | llvm::Value *RV = 0; |
| 308 | |
| 309 | // Functions with no result always return void. |
| 310 | if (ReturnValue) { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 311 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 312 | |
| 313 | switch (RetAI.getKind()) { |
| 314 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 315 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 316 | break; |
| 317 | |
| 318 | case ABIArgInfo::Default: |
| 319 | RV = Builder.CreateLoad(ReturnValue); |
| 320 | break; |
| 321 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 322 | case ABIArgInfo::Coerce: { |
| 323 | const llvm::Type *CoerceToPTy = |
| 324 | llvm::PointerType::getUnqual(RetAI.getCoerceToType()); |
| 325 | RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy)); |
| 326 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 329 | |
| 330 | if (RV) { |
| 331 | Builder.CreateRet(RV); |
| 332 | } else { |
| 333 | Builder.CreateRetVoid(); |
| 334 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | RValue CodeGenFunction::EmitCall(llvm::Value *Callee, |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 338 | QualType RetTy, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 339 | const CallArgList &CallArgs) { |
| 340 | // FIXME: Factor out code to load from args into locals into target. |
| 341 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 342 | llvm::Value *TempArg0 = 0; |
| 343 | |
| 344 | // Handle struct-return functions by passing a pointer to the |
| 345 | // location that we would like to return into. |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 346 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 347 | switch (RetAI.getKind()) { |
| 348 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 349 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 350 | TempArg0 = CreateTempAlloca(ConvertType(RetTy)); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 351 | Args.push_back(TempArg0); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 352 | break; |
| 353 | |
| 354 | case ABIArgInfo::Default: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 355 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 356 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
| 360 | I != E; ++I) { |
| 361 | RValue RV = I->first; |
| 362 | if (RV.isScalar()) { |
| 363 | Args.push_back(RV.getScalarVal()); |
| 364 | } else if (RV.isComplex()) { |
| 365 | // Make a temporary alloca to pass the argument. |
| 366 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 367 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 368 | } else { |
| 369 | Args.push_back(RV.getAggregateAddr()); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 374 | CGCallInfo CallInfo(RetTy, CallArgs); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 376 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 377 | CodeGen::ParamAttrListType ParamAttrList; |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 378 | CGM.ConstructParamAttrList(0, |
| 379 | CallInfo.argtypes_begin(), CallInfo.argtypes_end(), |
| 380 | ParamAttrList); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 381 | CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(), |
| 382 | ParamAttrList.size())); |
| 383 | |
| 384 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 385 | CI->setCallingConv(F->getCallingConv()); |
| 386 | if (CI->getType() != llvm::Type::VoidTy) |
| 387 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 388 | |
| 389 | switch (RetAI.getKind()) { |
| 390 | case ABIArgInfo::StructRet: |
| 391 | if (RetTy->isAnyComplexType()) |
| 392 | return RValue::getComplex(LoadComplexFromAddr(TempArg0, false)); |
| 393 | else |
| 394 | // Struct return. |
| 395 | return RValue::getAggregate(TempArg0); |
| 396 | |
| 397 | case ABIArgInfo::Default: |
| 398 | return RValue::get(RetTy->isVoidType() ? 0 : CI); |
| 399 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 400 | case ABIArgInfo::Coerce: { |
| 401 | const llvm::Type *CoerceToPTy = |
| 402 | llvm::PointerType::getUnqual(RetAI.getCoerceToType()); |
| 403 | llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp"); |
| 404 | Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy)); |
| 405 | return RValue::getAggregate(V); |
| 406 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 407 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 408 | |
| 409 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 410 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 411 | } |