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 | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 139 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 140 | uint64_t Size = Context.getTypeSize(RetTy); |
| 141 | if (Size == 8) { |
| 142 | return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); |
| 143 | } else if (Size == 16) { |
| 144 | return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); |
| 145 | } else if (Size == 32) { |
| 146 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 147 | } else if (Size == 64) { |
| 148 | return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); |
| 149 | } else { |
| 150 | return ABIArgInfo::getStructRet(); |
| 151 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 152 | } else { |
| 153 | return ABIArgInfo::getDefault(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /***/ |
| 158 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 159 | const llvm::FunctionType * |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 160 | CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) { |
| 161 | return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic); |
| 162 | } |
| 163 | |
| 164 | const llvm::FunctionType * |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 165 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 166 | return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic()); |
| 167 | } |
| 168 | |
| 169 | const llvm::FunctionType * |
| 170 | CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end, |
| 171 | bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 172 | std::vector<const llvm::Type*> ArgTys; |
| 173 | |
| 174 | const llvm::Type *ResultType = 0; |
| 175 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 176 | QualType RetTy = *begin; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 177 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 178 | switch (RetAI.getKind()) { |
| 179 | case ABIArgInfo::Default: |
| 180 | if (RetTy->isVoidType()) { |
| 181 | ResultType = llvm::Type::VoidTy; |
| 182 | } else { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 183 | ResultType = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 184 | } |
| 185 | break; |
| 186 | |
| 187 | case ABIArgInfo::StructRet: { |
| 188 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 189 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 190 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 195 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 196 | break; |
| 197 | } |
| 198 | |
| 199 | for (++begin; begin != end; ++begin) { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 200 | const llvm::Type *Ty = ConvertType(*begin); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 201 | if (Ty->isSingleValueType()) |
| 202 | ArgTys.push_back(Ty); |
| 203 | else |
| 204 | // byval arguments are always on the stack, which is addr space #0. |
| 205 | ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); |
| 206 | } |
| 207 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 208 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 211 | bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 212 | return classifyReturnType(RetTy, getContext()).isStructRet(); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 215 | void CodeGenModule::ConstructParamAttrList(const Decl *TargetDecl, |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 216 | ArgTypeIterator begin, |
| 217 | ArgTypeIterator end, |
| 218 | ParamAttrListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 219 | unsigned FuncAttrs = 0; |
| 220 | |
| 221 | if (TargetDecl) { |
| 222 | if (TargetDecl->getAttr<NoThrowAttr>()) |
| 223 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
| 224 | if (TargetDecl->getAttr<NoReturnAttr>()) |
| 225 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 226 | } |
| 227 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 228 | QualType RetTy = *begin; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 229 | unsigned Index = 1; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 230 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 231 | switch (RetAI.getKind()) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 232 | case ABIArgInfo::Default: |
| 233 | if (RetTy->isPromotableIntegerType()) { |
| 234 | if (RetTy->isSignedIntegerType()) { |
| 235 | FuncAttrs |= llvm::ParamAttr::SExt; |
| 236 | } else if (RetTy->isUnsignedIntegerType()) { |
| 237 | FuncAttrs |= llvm::ParamAttr::ZExt; |
| 238 | } |
| 239 | } |
| 240 | break; |
| 241 | |
| 242 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 243 | PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, |
| 244 | llvm::ParamAttr::StructRet)); |
| 245 | ++Index; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 249 | break; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 250 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 252 | if (FuncAttrs) |
| 253 | PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs)); |
| 254 | for (++begin; begin != end; ++begin, ++Index) { |
| 255 | QualType ParamType = *begin; |
| 256 | unsigned ParamAttrs = 0; |
| 257 | if (ParamType->isRecordType()) |
| 258 | ParamAttrs |= llvm::ParamAttr::ByVal; |
| 259 | if (ParamType->isPromotableIntegerType()) { |
| 260 | if (ParamType->isSignedIntegerType()) { |
| 261 | ParamAttrs |= llvm::ParamAttr::SExt; |
| 262 | } else if (ParamType->isUnsignedIntegerType()) { |
| 263 | ParamAttrs |= llvm::ParamAttr::ZExt; |
| 264 | } |
| 265 | } |
| 266 | if (ParamAttrs) |
| 267 | PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs)); |
| 268 | } |
| 269 | } |
| 270 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 271 | void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn, |
| 272 | QualType RetTy, |
| 273 | const FunctionArgList &Args) { |
| 274 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 275 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 276 | |
| 277 | // Name the struct return argument. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 278 | if (CGM.ReturnTypeUsesSret(RetTy)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 279 | AI->setName("agg.result"); |
| 280 | ++AI; |
| 281 | } |
| 282 | |
| 283 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 284 | i != e; ++i, ++AI) { |
| 285 | const VarDecl *Arg = i->first; |
| 286 | QualType T = i->second; |
| 287 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 288 | llvm::Value* V = AI; |
| 289 | if (!getContext().typesAreCompatible(T, Arg->getType())) { |
| 290 | // This must be a promotion, for something like |
| 291 | // "void a(x) short x; {..." |
| 292 | V = EmitScalarConversion(V, T, Arg->getType()); |
| 293 | } |
| 294 | EmitParmDecl(*Arg, V); |
| 295 | } |
| 296 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 297 | } |
| 298 | |
| 299 | void CodeGenFunction::EmitFunctionEpilog(QualType RetTy, |
| 300 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 301 | llvm::Value *RV = 0; |
| 302 | |
| 303 | // Functions with no result always return void. |
| 304 | if (ReturnValue) { |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 305 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 306 | |
| 307 | switch (RetAI.getKind()) { |
| 308 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 309 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 310 | break; |
| 311 | |
| 312 | case ABIArgInfo::Default: |
| 313 | RV = Builder.CreateLoad(ReturnValue); |
| 314 | break; |
| 315 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 316 | case ABIArgInfo::Coerce: { |
| 317 | const llvm::Type *CoerceToPTy = |
| 318 | llvm::PointerType::getUnqual(RetAI.getCoerceToType()); |
| 319 | RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy)); |
| 320 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 323 | |
| 324 | if (RV) { |
| 325 | Builder.CreateRet(RV); |
| 326 | } else { |
| 327 | Builder.CreateRetVoid(); |
| 328 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | RValue CodeGenFunction::EmitCall(llvm::Value *Callee, |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 332 | QualType RetTy, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 333 | const CallArgList &CallArgs) { |
| 334 | // FIXME: Factor out code to load from args into locals into target. |
| 335 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 336 | llvm::Value *TempArg0 = 0; |
| 337 | |
| 338 | // Handle struct-return functions by passing a pointer to the |
| 339 | // location that we would like to return into. |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 340 | ABIArgInfo RetAI = classifyReturnType(RetTy, getContext()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 341 | switch (RetAI.getKind()) { |
| 342 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 343 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 344 | TempArg0 = CreateTempAlloca(ConvertType(RetTy)); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 345 | Args.push_back(TempArg0); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 346 | break; |
| 347 | |
| 348 | case ABIArgInfo::Default: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 349 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 350 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
| 354 | I != E; ++I) { |
| 355 | RValue RV = I->first; |
| 356 | if (RV.isScalar()) { |
| 357 | Args.push_back(RV.getScalarVal()); |
| 358 | } else if (RV.isComplex()) { |
| 359 | // Make a temporary alloca to pass the argument. |
| 360 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 361 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 362 | } else { |
| 363 | Args.push_back(RV.getAggregateAddr()); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 368 | CGCallInfo CallInfo(RetTy, CallArgs); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 369 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 370 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 371 | CodeGen::ParamAttrListType ParamAttrList; |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 372 | CGM.ConstructParamAttrList(0, |
| 373 | CallInfo.argtypes_begin(), CallInfo.argtypes_end(), |
| 374 | ParamAttrList); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 375 | CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(), |
| 376 | ParamAttrList.size())); |
| 377 | |
| 378 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 379 | CI->setCallingConv(F->getCallingConv()); |
| 380 | if (CI->getType() != llvm::Type::VoidTy) |
| 381 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 382 | |
| 383 | switch (RetAI.getKind()) { |
| 384 | case ABIArgInfo::StructRet: |
| 385 | if (RetTy->isAnyComplexType()) |
| 386 | return RValue::getComplex(LoadComplexFromAddr(TempArg0, false)); |
| 387 | else |
| 388 | // Struct return. |
| 389 | return RValue::getAggregate(TempArg0); |
| 390 | |
| 391 | case ABIArgInfo::Default: |
| 392 | return RValue::get(RetTy->isVoidType() ? 0 : CI); |
| 393 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame^] | 394 | case ABIArgInfo::Coerce: { |
| 395 | const llvm::Type *CoerceToPTy = |
| 396 | llvm::PointerType::getUnqual(RetAI.getCoerceToType()); |
| 397 | llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp"); |
| 398 | Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy)); |
| 399 | return RValue::getAggregate(V); |
| 400 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 401 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 402 | |
| 403 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 404 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 405 | } |