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" |
| 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "llvm/ParameterAttributes.h" |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
| 24 | /***/ |
| 25 | |
| 26 | static void |
| 27 | constructParamAttrListInternal(const Decl *TargetDecl, |
| 28 | const llvm::SmallVector<QualType, 16> &ArgTypes, |
| 29 | ParamAttrListType &PAL) { |
| 30 | unsigned FuncAttrs = 0; |
| 31 | |
| 32 | if (TargetDecl) { |
| 33 | if (TargetDecl->getAttr<NoThrowAttr>()) |
| 34 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
| 35 | if (TargetDecl->getAttr<NoReturnAttr>()) |
| 36 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 37 | } |
| 38 | |
| 39 | unsigned Index = 1; |
| 40 | if (CodeGenFunction::hasAggregateLLVMType(ArgTypes[0])) { |
| 41 | PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, |
| 42 | llvm::ParamAttr::StructRet)); |
| 43 | ++Index; |
| 44 | } else if (ArgTypes[0]->isPromotableIntegerType()) { |
| 45 | if (ArgTypes[0]->isSignedIntegerType()) { |
| 46 | FuncAttrs |= llvm::ParamAttr::SExt; |
| 47 | } else if (ArgTypes[0]->isUnsignedIntegerType()) { |
| 48 | FuncAttrs |= llvm::ParamAttr::ZExt; |
| 49 | } |
| 50 | } |
| 51 | if (FuncAttrs) |
| 52 | PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs)); |
| 53 | for (llvm::SmallVector<QualType, 8>::const_iterator i = ArgTypes.begin() + 1, |
| 54 | e = ArgTypes.end(); i != e; ++i, ++Index) { |
| 55 | QualType ParamType = *i; |
| 56 | unsigned ParamAttrs = 0; |
| 57 | if (ParamType->isRecordType()) |
| 58 | ParamAttrs |= llvm::ParamAttr::ByVal; |
| 59 | if (ParamType->isPromotableIntegerType()) { |
| 60 | if (ParamType->isSignedIntegerType()) { |
| 61 | ParamAttrs |= llvm::ParamAttr::SExt; |
| 62 | } else if (ParamType->isUnsignedIntegerType()) { |
| 63 | ParamAttrs |= llvm::ParamAttr::ZExt; |
| 64 | } |
| 65 | } |
| 66 | if (ParamAttrs) |
| 67 | PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /***/ |
| 72 | |
| 73 | // FIXME: Use iterator and sidestep silly type array creation. |
| 74 | |
| 75 | CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD) |
| 76 | : TheDecl(FD) |
| 77 | { |
| 78 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
| 79 | const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy); |
| 80 | |
| 81 | ArgTypes.push_back(FTy->getResultType()); |
| 82 | if (FTP) |
| 83 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 84 | ArgTypes.push_back(FTP->getArgType(i)); |
| 85 | } |
| 86 | |
| 87 | CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD, |
| 88 | const ASTContext &Context) |
| 89 | : TheDecl(MD) |
| 90 | { |
| 91 | ArgTypes.push_back(MD->getResultType()); |
| 92 | ArgTypes.push_back(MD->getSelfDecl()->getType()); |
| 93 | ArgTypes.push_back(Context.getObjCSelType()); |
| 94 | for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), |
| 95 | e = MD->param_end(); i != e; ++i) |
| 96 | ArgTypes.push_back((*i)->getType()); |
| 97 | } |
| 98 | |
| 99 | void CGFunctionInfo::constructParamAttrList(ParamAttrListType &PAL) const { |
| 100 | constructParamAttrListInternal(TheDecl, ArgTypes, PAL); |
| 101 | } |
| 102 | |
| 103 | /***/ |
| 104 | |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 105 | CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 106 | : ResultType(_ResultType), |
| 107 | Args(_Args) { |
| 108 | ArgTypes.push_back(ResultType); |
| 109 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); i!=e; ++i) |
| 110 | ArgTypes.push_back(i->second); |
| 111 | } |
| 112 | |
| 113 | void CGCallInfo::constructParamAttrList(ParamAttrListType &PAL) const { |
| 114 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
| 115 | constructParamAttrListInternal(0, ArgTypes, PAL); |
| 116 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame^] | 117 | |
| 118 | /***/ |
| 119 | |
| 120 | void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn, |
| 121 | QualType RetTy, |
| 122 | const FunctionArgList &Args) { |
| 123 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 124 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 125 | |
| 126 | // Name the struct return argument. |
| 127 | if (hasAggregateLLVMType(RetTy)) { |
| 128 | AI->setName("agg.result"); |
| 129 | ++AI; |
| 130 | } |
| 131 | |
| 132 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 133 | i != e; ++i, ++AI) { |
| 134 | const VarDecl *Arg = i->first; |
| 135 | QualType T = i->second; |
| 136 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 137 | llvm::Value* V = AI; |
| 138 | if (!getContext().typesAreCompatible(T, Arg->getType())) { |
| 139 | // This must be a promotion, for something like |
| 140 | // "void a(x) short x; {..." |
| 141 | V = EmitScalarConversion(V, T, Arg->getType()); |
| 142 | } |
| 143 | EmitParmDecl(*Arg, V); |
| 144 | } |
| 145 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 146 | } |
| 147 | |
| 148 | void CodeGenFunction::EmitFunctionEpilog(QualType RetTy, |
| 149 | llvm::Value *ReturnValue) { |
| 150 | if (!ReturnValue) { |
| 151 | Builder.CreateRetVoid(); |
| 152 | } else { |
| 153 | if (!hasAggregateLLVMType(RetTy)) { |
| 154 | Builder.CreateRet(Builder.CreateLoad(ReturnValue)); |
| 155 | } else if (RetTy->isAnyComplexType()) { |
| 156 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 157 | Builder.CreateRetVoid(); |
| 158 | } else { |
| 159 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 160 | Builder.CreateRetVoid(); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | RValue CodeGenFunction::EmitCall(llvm::Value *Callee, |
| 166 | QualType ResultType, |
| 167 | const CallArgList &CallArgs) { |
| 168 | // FIXME: Factor out code to load from args into locals into target. |
| 169 | llvm::SmallVector<llvm::Value*, 16> Args; |
| 170 | llvm::Value *TempArg0 = 0; |
| 171 | |
| 172 | // Handle struct-return functions by passing a pointer to the |
| 173 | // location that we would like to return into. |
| 174 | if (hasAggregateLLVMType(ResultType)) { |
| 175 | // Create a temporary alloca to hold the result of the call. :( |
| 176 | TempArg0 = CreateTempAlloca(ConvertType(ResultType)); |
| 177 | Args.push_back(TempArg0); |
| 178 | } |
| 179 | |
| 180 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
| 181 | I != E; ++I) { |
| 182 | RValue RV = I->first; |
| 183 | if (RV.isScalar()) { |
| 184 | Args.push_back(RV.getScalarVal()); |
| 185 | } else if (RV.isComplex()) { |
| 186 | // Make a temporary alloca to pass the argument. |
| 187 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 188 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 189 | } else { |
| 190 | Args.push_back(RV.getAggregateAddr()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
| 195 | CGCallInfo CallInfo(ResultType, CallArgs); |
| 196 | |
| 197 | CodeGen::ParamAttrListType ParamAttrList; |
| 198 | CallInfo.constructParamAttrList(ParamAttrList); |
| 199 | CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(), |
| 200 | ParamAttrList.size())); |
| 201 | |
| 202 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 203 | CI->setCallingConv(F->getCallingConv()); |
| 204 | if (CI->getType() != llvm::Type::VoidTy) |
| 205 | CI->setName("call"); |
| 206 | else if (ResultType->isAnyComplexType()) |
| 207 | return RValue::getComplex(LoadComplexFromAddr(TempArg0, false)); |
| 208 | else if (hasAggregateLLVMType(ResultType)) |
| 209 | // Struct return. |
| 210 | return RValue::getAggregate(TempArg0); |
| 211 | else { |
| 212 | // void return. |
| 213 | assert(ResultType->isVoidType() && "Should only have a void expr here"); |
| 214 | CI = 0; |
| 215 | } |
| 216 | |
| 217 | return RValue::get(CI); |
| 218 | } |