| 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/Decl.h" | 
| Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclCXX.h" | 
| Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" | 
| Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompileOptions.h" | 
| Devang Patel | d0646bd | 2008-09-24 01:01:36 +0000 | [diff] [blame] | 23 | #include "llvm/Attributes.h" | 
| Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CallSite.h" | 
| Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" | 
| Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 26 |  | 
 | 27 | #include "ABIInfo.h" | 
 | 28 |  | 
| Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 29 | using namespace clang; | 
 | 30 | using namespace CodeGen; | 
 | 31 |  | 
 | 32 | /***/ | 
 | 33 |  | 
| Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 34 | // FIXME: Use iterator and sidestep silly type array creation. | 
 | 35 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 36 | const  | 
| Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 37 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) { | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 38 |   return getFunctionInfo(FTNP->getResultType(),  | 
 | 39 |                          llvm::SmallVector<QualType, 16>()); | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 40 | } | 
 | 41 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 42 | const  | 
| Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 43 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) { | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 44 |   llvm::SmallVector<QualType, 16> ArgTys; | 
 | 45 |   // FIXME: Kill copy. | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 46 |   for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 47 |     ArgTys.push_back(FTP->getArgType(i)); | 
 | 48 |   return getFunctionInfo(FTP->getResultType(), ArgTys); | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 49 | } | 
 | 50 |  | 
| Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 51 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) { | 
 | 52 |   llvm::SmallVector<QualType, 16> ArgTys; | 
| Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 53 |   // Add the 'this' pointer unless this is a static method. | 
 | 54 |   if (MD->isInstance()) | 
 | 55 |     ArgTys.push_back(MD->getThisType(Context)); | 
| Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 56 |    | 
 | 57 |   const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType(); | 
 | 58 |   for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) | 
 | 59 |     ArgTys.push_back(FTP->getArgType(i)); | 
 | 60 |   return getFunctionInfo(FTP->getResultType(), ArgTys); | 
 | 61 | } | 
 | 62 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 63 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { | 
| Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 64 |   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) | 
| Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 65 |     if (MD->isInstance()) | 
 | 66 |       return getFunctionInfo(MD); | 
| Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 67 |    | 
| Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 68 |   const FunctionType *FTy = FD->getType()->getAsFunctionType(); | 
| Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 69 |   if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy)) | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 70 |     return getFunctionInfo(FTP); | 
| Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 71 |   return getFunctionInfo(cast<FunctionNoProtoType>(FTy)); | 
| Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 72 | } | 
 | 73 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 74 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { | 
 | 75 |   llvm::SmallVector<QualType, 16> ArgTys; | 
 | 76 |   ArgTys.push_back(MD->getSelfDecl()->getType()); | 
 | 77 |   ArgTys.push_back(Context.getObjCSelType()); | 
 | 78 |   // FIXME: Kill copy? | 
| Chris Lattner | 2073216 | 2009-02-20 06:23:21 +0000 | [diff] [blame] | 79 |   for (ObjCMethodDecl::param_iterator i = MD->param_begin(), | 
| Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 80 |          e = MD->param_end(); i != e; ++i) | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 81 |     ArgTys.push_back((*i)->getType()); | 
 | 82 |   return getFunctionInfo(MD->getResultType(), ArgTys); | 
| Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 83 | } | 
 | 84 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 85 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,  | 
 | 86 |                                                     const CallArgList &Args) { | 
 | 87 |   // FIXME: Kill copy. | 
 | 88 |   llvm::SmallVector<QualType, 16> ArgTys; | 
| Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 89 |   for (CallArgList::const_iterator i = Args.begin(), e = Args.end();  | 
 | 90 |        i != e; ++i) | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 91 |     ArgTys.push_back(i->second); | 
 | 92 |   return getFunctionInfo(ResTy, ArgTys); | 
| Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 93 | } | 
 | 94 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 95 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,  | 
 | 96 |                                                   const FunctionArgList &Args) { | 
 | 97 |   // FIXME: Kill copy. | 
 | 98 |   llvm::SmallVector<QualType, 16> ArgTys; | 
| Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 99 |   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();  | 
 | 100 |        i != e; ++i) | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 101 |     ArgTys.push_back(i->second); | 
 | 102 |   return getFunctionInfo(ResTy, ArgTys); | 
 | 103 | } | 
 | 104 |  | 
 | 105 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, | 
 | 106 |                                const llvm::SmallVector<QualType, 16> &ArgTys) { | 
| Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 107 |   // Lookup or create unique function info. | 
 | 108 |   llvm::FoldingSetNodeID ID; | 
 | 109 |   CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end()); | 
 | 110 |  | 
 | 111 |   void *InsertPos = 0; | 
 | 112 |   CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); | 
 | 113 |   if (FI) | 
 | 114 |     return *FI; | 
 | 115 |  | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 116 |   // Construct the function info. | 
| Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 117 |   FI = new CGFunctionInfo(ResTy, ArgTys); | 
| Daniel Dunbar | 35e67d4 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 118 |   FunctionInfos.InsertNode(FI, InsertPos); | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 119 |  | 
 | 120 |   // Compute ABI information. | 
| Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 121 |   getABIInfo().computeInfo(*FI, getContext()); | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 122 |  | 
| Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 123 |   return *FI; | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 124 | } | 
 | 125 |  | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 126 | CGFunctionInfo::CGFunctionInfo(QualType ResTy,  | 
 | 127 |                                const llvm::SmallVector<QualType, 16> &ArgTys) { | 
 | 128 |   NumArgs = ArgTys.size(); | 
 | 129 |   Args = new ArgInfo[1 + NumArgs]; | 
 | 130 |   Args[0].type = ResTy; | 
 | 131 |   for (unsigned i = 0; i < NumArgs; ++i) | 
 | 132 |     Args[1 + i].type = ArgTys[i]; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | /***/ | 
 | 136 |  | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 137 | void CodeGenTypes::GetExpandedTypes(QualType Ty,  | 
 | 138 |                                     std::vector<const llvm::Type*> &ArgTys) { | 
 | 139 |   const RecordType *RT = Ty->getAsStructureType(); | 
 | 140 |   assert(RT && "Can only expand structure types."); | 
 | 141 |   const RecordDecl *RD = RT->getDecl(); | 
 | 142 |   assert(!RD->hasFlexibleArrayMember() &&  | 
 | 143 |          "Cannot expand structure with flexible array."); | 
 | 144 |    | 
| Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 145 |   for (RecordDecl::field_iterator i = RD->field_begin(Context),  | 
 | 146 |          e = RD->field_end(Context); i != e; ++i) { | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 147 |     const FieldDecl *FD = *i; | 
 | 148 |     assert(!FD->isBitField() &&  | 
 | 149 |            "Cannot expand structure with bit-field members."); | 
 | 150 |      | 
 | 151 |     QualType FT = FD->getType(); | 
 | 152 |     if (CodeGenFunction::hasAggregateLLVMType(FT)) { | 
 | 153 |       GetExpandedTypes(FT, ArgTys); | 
 | 154 |     } else { | 
 | 155 |       ArgTys.push_back(ConvertType(FT)); | 
 | 156 |     } | 
 | 157 |   } | 
 | 158 | } | 
 | 159 |  | 
 | 160 | llvm::Function::arg_iterator  | 
 | 161 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, | 
 | 162 |                                     llvm::Function::arg_iterator AI) { | 
 | 163 |   const RecordType *RT = Ty->getAsStructureType(); | 
 | 164 |   assert(RT && "Can only expand structure types."); | 
 | 165 |  | 
 | 166 |   RecordDecl *RD = RT->getDecl(); | 
 | 167 |   assert(LV.isSimple() &&  | 
 | 168 |          "Unexpected non-simple lvalue during struct expansion.");   | 
 | 169 |   llvm::Value *Addr = LV.getAddress(); | 
| Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 170 |   for (RecordDecl::field_iterator i = RD->field_begin(getContext()),  | 
 | 171 |          e = RD->field_end(getContext()); i != e; ++i) { | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 172 |     FieldDecl *FD = *i;     | 
 | 173 |     QualType FT = FD->getType(); | 
 | 174 |  | 
 | 175 |     // FIXME: What are the right qualifiers here? | 
 | 176 |     LValue LV = EmitLValueForField(Addr, FD, false, 0); | 
 | 177 |     if (CodeGenFunction::hasAggregateLLVMType(FT)) { | 
 | 178 |       AI = ExpandTypeFromArgs(FT, LV, AI); | 
 | 179 |     } else { | 
 | 180 |       EmitStoreThroughLValue(RValue::get(AI), LV, FT); | 
 | 181 |       ++AI; | 
 | 182 |     } | 
 | 183 |   } | 
 | 184 |  | 
 | 185 |   return AI; | 
 | 186 | } | 
 | 187 |  | 
 | 188 | void  | 
 | 189 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,  | 
 | 190 |                                   llvm::SmallVector<llvm::Value*, 16> &Args) { | 
 | 191 |   const RecordType *RT = Ty->getAsStructureType(); | 
 | 192 |   assert(RT && "Can only expand structure types."); | 
 | 193 |  | 
 | 194 |   RecordDecl *RD = RT->getDecl(); | 
 | 195 |   assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); | 
 | 196 |   llvm::Value *Addr = RV.getAggregateAddr(); | 
| Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 197 |   for (RecordDecl::field_iterator i = RD->field_begin(getContext()),  | 
 | 198 |          e = RD->field_end(getContext()); i != e; ++i) { | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 199 |     FieldDecl *FD = *i;     | 
 | 200 |     QualType FT = FD->getType(); | 
 | 201 |      | 
 | 202 |     // FIXME: What are the right qualifiers here? | 
 | 203 |     LValue LV = EmitLValueForField(Addr, FD, false, 0); | 
 | 204 |     if (CodeGenFunction::hasAggregateLLVMType(FT)) { | 
 | 205 |       ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); | 
 | 206 |     } else { | 
 | 207 |       RValue RV = EmitLoadOfLValue(LV, FT); | 
 | 208 |       assert(RV.isScalar() &&  | 
 | 209 |              "Unexpected non-scalar rvalue during struct expansion."); | 
 | 210 |       Args.push_back(RV.getScalarVal()); | 
 | 211 |     } | 
 | 212 |   } | 
 | 213 | } | 
 | 214 |  | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 215 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as | 
 | 216 | /// a pointer to an object of type \arg Ty. | 
 | 217 | /// | 
 | 218 | /// This safely handles the case when the src type is smaller than the | 
 | 219 | /// destination type; in this situation the values of bits which not | 
 | 220 | /// present in the src are undefined. | 
 | 221 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, | 
 | 222 |                                       const llvm::Type *Ty, | 
 | 223 |                                       CodeGenFunction &CGF) { | 
 | 224 |   const llvm::Type *SrcTy =  | 
 | 225 |     cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 226 |   uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); | 
 | 227 |   uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 228 |  | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 229 |   // If load is legal, just bitcast the src pointer. | 
| Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 230 |   if (SrcSize >= DstSize) { | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 231 |     // Generally SrcSize is never greater than DstSize, since this means we are | 
 | 232 |     // losing bits. However, this can happen in cases where the structure has | 
 | 233 |     // additional padding, for example due to a user specified alignment. | 
| Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 234 |     // | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 235 |     // FIXME: Assert that we aren't truncating non-padding bits when have access | 
 | 236 |     // to that information. | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 237 |     llvm::Value *Casted = | 
 | 238 |       CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); | 
| Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 239 |     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); | 
 | 240 |     // FIXME: Use better alignment / avoid requiring aligned load. | 
 | 241 |     Load->setAlignment(1); | 
 | 242 |     return Load; | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 243 |   } else { | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 244 |     // Otherwise do coercion through memory. This is stupid, but | 
 | 245 |     // simple. | 
 | 246 |     llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); | 
 | 247 |     llvm::Value *Casted =  | 
 | 248 |       CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); | 
| Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 249 |     llvm::StoreInst *Store =  | 
 | 250 |       CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); | 
 | 251 |     // FIXME: Use better alignment / avoid requiring aligned store. | 
 | 252 |     Store->setAlignment(1); | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 253 |     return CGF.Builder.CreateLoad(Tmp); | 
 | 254 |   } | 
 | 255 | } | 
 | 256 |  | 
 | 257 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, | 
 | 258 | /// where the source and destination may have different types. | 
 | 259 | /// | 
 | 260 | /// This safely handles the case when the src type is larger than the | 
 | 261 | /// destination type; the upper bits of the src will be lost. | 
 | 262 | static void CreateCoercedStore(llvm::Value *Src, | 
 | 263 |                                llvm::Value *DstPtr, | 
 | 264 |                                CodeGenFunction &CGF) { | 
 | 265 |   const llvm::Type *SrcTy = Src->getType(); | 
 | 266 |   const llvm::Type *DstTy =  | 
 | 267 |     cast<llvm::PointerType>(DstPtr->getType())->getElementType(); | 
 | 268 |  | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 269 |   uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); | 
 | 270 |   uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 271 |  | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 272 |   // If store is legal, just bitcast the src pointer. | 
| Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 273 |   if (SrcSize <= DstSize) { | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 274 |     llvm::Value *Casted = | 
 | 275 |       CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); | 
| Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 276 |     // FIXME: Use better alignment / avoid requiring aligned store. | 
 | 277 |     CGF.Builder.CreateStore(Src, Casted)->setAlignment(1); | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 278 |   } else { | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 279 |     // Otherwise do coercion through memory. This is stupid, but | 
 | 280 |     // simple. | 
| Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 281 |  | 
 | 282 |     // Generally SrcSize is never greater than DstSize, since this means we are | 
 | 283 |     // losing bits. However, this can happen in cases where the structure has | 
 | 284 |     // additional padding, for example due to a user specified alignment. | 
 | 285 |     // | 
 | 286 |     // FIXME: Assert that we aren't truncating non-padding bits when have access | 
 | 287 |     // to that information. | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 288 |     llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); | 
 | 289 |     CGF.Builder.CreateStore(Src, Tmp); | 
 | 290 |     llvm::Value *Casted =  | 
 | 291 |       CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); | 
| Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 292 |     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); | 
 | 293 |     // FIXME: Use better alignment / avoid requiring aligned load. | 
 | 294 |     Load->setAlignment(1); | 
 | 295 |     CGF.Builder.CreateStore(Load, DstPtr); | 
| Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 296 |   } | 
 | 297 | } | 
 | 298 |  | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 299 | /***/ | 
 | 300 |  | 
| Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 301 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 302 |   return FI.getReturnInfo().isIndirect(); | 
| Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 303 | } | 
 | 304 |  | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 305 | const llvm::FunctionType * | 
| Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 306 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 307 |   std::vector<const llvm::Type*> ArgTys; | 
 | 308 |  | 
 | 309 |   const llvm::Type *ResultType = 0; | 
 | 310 |  | 
| Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 311 |   QualType RetTy = FI.getReturnType(); | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 312 |   const ABIArgInfo &RetAI = FI.getReturnInfo(); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 313 |   switch (RetAI.getKind()) { | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 314 |   case ABIArgInfo::Expand: | 
 | 315 |     assert(0 && "Invalid ABI kind for return argument"); | 
 | 316 |  | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 317 |   case ABIArgInfo::Extend: | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 318 |   case ABIArgInfo::Direct: | 
 | 319 |     ResultType = ConvertType(RetTy); | 
 | 320 |     break; | 
 | 321 |  | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 322 |   case ABIArgInfo::Indirect: { | 
 | 323 |     assert(!RetAI.getIndirectAlign() && "Align unused on indirect return."); | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 324 |     ResultType = llvm::Type::VoidTy; | 
| Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 325 |     const llvm::Type *STy = ConvertType(RetTy); | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 326 |     ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); | 
 | 327 |     break; | 
 | 328 |   } | 
 | 329 |  | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 330 |   case ABIArgInfo::Ignore: | 
 | 331 |     ResultType = llvm::Type::VoidTy; | 
 | 332 |     break; | 
 | 333 |  | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 334 |   case ABIArgInfo::Coerce: | 
| Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 335 |     ResultType = RetAI.getCoerceToType(); | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 336 |     break; | 
 | 337 |   } | 
 | 338 |    | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 339 |   for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),  | 
 | 340 |          ie = FI.arg_end(); it != ie; ++it) { | 
 | 341 |     const ABIArgInfo &AI = it->info; | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 342 |      | 
 | 343 |     switch (AI.getKind()) { | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 344 |     case ABIArgInfo::Ignore: | 
 | 345 |       break; | 
 | 346 |  | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 347 |     case ABIArgInfo::Coerce: | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 348 |       ArgTys.push_back(AI.getCoerceToType()); | 
 | 349 |       break; | 
 | 350 |  | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 351 |     case ABIArgInfo::Indirect: { | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 352 |       // indirect arguments are always on the stack, which is addr space #0. | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 353 |       const llvm::Type *LTy = ConvertTypeForMem(it->type); | 
 | 354 |       ArgTys.push_back(llvm::PointerType::getUnqual(LTy)); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 355 |       break; | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 356 |     } | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 357 |  | 
 | 358 |     case ABIArgInfo::Extend: | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 359 |     case ABIArgInfo::Direct: | 
| Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 360 |       ArgTys.push_back(ConvertType(it->type)); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 361 |       break; | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 362 |       | 
 | 363 |     case ABIArgInfo::Expand: | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 364 |       GetExpandedTypes(it->type, ArgTys); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 365 |       break; | 
 | 366 |     } | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 367 |   } | 
 | 368 |  | 
| Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 369 |   return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); | 
| Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 370 | } | 
 | 371 |  | 
| Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 372 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, | 
| Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 373 |                                            const Decl *TargetDecl, | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 374 |                                            AttributeListType &PAL) { | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 375 |   unsigned FuncAttrs = 0; | 
| Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 376 |   unsigned RetAttrs = 0; | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 377 |  | 
| Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 378 |   // FIXME: handle sseregparm someday... | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 379 |   if (TargetDecl) { | 
| Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 380 |     if (TargetDecl->hasAttr<NoThrowAttr>()) | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 381 |       FuncAttrs |= llvm::Attribute::NoUnwind; | 
| Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 382 |     if (TargetDecl->hasAttr<NoReturnAttr>()) | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 383 |       FuncAttrs |= llvm::Attribute::NoReturn; | 
| Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 384 |     if (TargetDecl->hasAttr<ConstAttr>()) | 
| Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 385 |       FuncAttrs |= llvm::Attribute::ReadNone; | 
| Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 386 |     else if (TargetDecl->hasAttr<PureAttr>()) | 
| Daniel Dunbar | 64c2e07 | 2009-04-10 22:14:52 +0000 | [diff] [blame] | 387 |       FuncAttrs |= llvm::Attribute::ReadOnly; | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 388 |   } | 
 | 389 |  | 
| Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 390 |   if (CompileOpts.DisableRedZone) | 
 | 391 |     FuncAttrs |= llvm::Attribute::NoRedZone; | 
| Devang Patel | acebb39 | 2009-06-05 22:05:48 +0000 | [diff] [blame] | 392 |   if (CompileOpts.NoImplicitFloat) | 
 | 393 |     FuncAttrs |= llvm::Attribute::NoImplicitFloat; | 
| Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 394 |  | 
| Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 395 |   QualType RetTy = FI.getReturnType(); | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 396 |   unsigned Index = 1; | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 397 |   const ABIArgInfo &RetAI = FI.getReturnInfo(); | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 398 |   switch (RetAI.getKind()) { | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 399 |   case ABIArgInfo::Extend: | 
 | 400 |    if (RetTy->isSignedIntegerType()) { | 
 | 401 |      RetAttrs |= llvm::Attribute::SExt; | 
 | 402 |    } else if (RetTy->isUnsignedIntegerType()) { | 
 | 403 |      RetAttrs |= llvm::Attribute::ZExt; | 
 | 404 |    } | 
 | 405 |    // FALLTHROUGH | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 406 |   case ABIArgInfo::Direct: | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 407 |     break; | 
 | 408 |  | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 409 |   case ABIArgInfo::Indirect: | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 410 |     PAL.push_back(llvm::AttributeWithIndex::get(Index,  | 
| Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 411 |                                                 llvm::Attribute::StructRet | | 
 | 412 |                                                 llvm::Attribute::NoAlias)); | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 413 |     ++Index; | 
| Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 414 |     // sret disables readnone and readonly | 
 | 415 |     FuncAttrs &= ~(llvm::Attribute::ReadOnly | | 
 | 416 |                    llvm::Attribute::ReadNone); | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 417 |     break; | 
 | 418 |  | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 419 |   case ABIArgInfo::Ignore: | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 420 |   case ABIArgInfo::Coerce: | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 421 |     break; | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 422 |  | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 423 |   case ABIArgInfo::Expand: | 
 | 424 |     assert(0 && "Invalid ABI kind for return argument");     | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 425 |   } | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 426 |  | 
| Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 427 |   if (RetAttrs) | 
 | 428 |     PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); | 
| Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 429 |  | 
 | 430 |   // FIXME: we need to honour command line settings also... | 
 | 431 |   // FIXME: RegParm should be reduced in case of nested functions and/or global | 
 | 432 |   // register variable. | 
 | 433 |   signed RegParm = 0; | 
 | 434 |   if (TargetDecl) | 
 | 435 |     if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>()) | 
 | 436 |       RegParm = RegParmAttr->getNumParams(); | 
 | 437 |  | 
 | 438 |   unsigned PointerWidth = getContext().Target.getPointerWidth(0); | 
| Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 439 |   for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),  | 
 | 440 |          ie = FI.arg_end(); it != ie; ++it) { | 
 | 441 |     QualType ParamType = it->type; | 
 | 442 |     const ABIArgInfo &AI = it->info; | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 443 |     unsigned Attributes = 0; | 
| Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 444 |  | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 445 |     switch (AI.getKind()) { | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 446 |     case ABIArgInfo::Coerce: | 
 | 447 |       break; | 
 | 448 |  | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 449 |     case ABIArgInfo::Indirect: | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 450 |       Attributes |= llvm::Attribute::ByVal; | 
| Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 451 |       Attributes |= | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 452 |         llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); | 
| Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 453 |       // byval disables readnone and readonly. | 
 | 454 |       FuncAttrs &= ~(llvm::Attribute::ReadOnly | | 
 | 455 |                      llvm::Attribute::ReadNone); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 456 |       break; | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 457 |  | 
 | 458 |     case ABIArgInfo::Extend: | 
 | 459 |      if (ParamType->isSignedIntegerType()) { | 
 | 460 |        Attributes |= llvm::Attribute::SExt; | 
 | 461 |      } else if (ParamType->isUnsignedIntegerType()) { | 
 | 462 |        Attributes |= llvm::Attribute::ZExt; | 
 | 463 |      } | 
 | 464 |      // FALLS THROUGH | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 465 |     case ABIArgInfo::Direct: | 
| Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 466 |       if (RegParm > 0 && | 
 | 467 |           (ParamType->isIntegerType() || ParamType->isPointerType())) { | 
 | 468 |         RegParm -= | 
 | 469 |           (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth; | 
 | 470 |         if (RegParm >= 0) | 
 | 471 |           Attributes |= llvm::Attribute::InReg; | 
 | 472 |       } | 
 | 473 |       // FIXME: handle sseregparm someday... | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 474 |       break; | 
| Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 475 |  | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 476 |     case ABIArgInfo::Ignore: | 
 | 477 |       // Skip increment, no matching LLVM parameter. | 
 | 478 |       continue;  | 
 | 479 |  | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 480 |     case ABIArgInfo::Expand: { | 
 | 481 |       std::vector<const llvm::Type*> Tys;   | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 482 |       // FIXME: This is rather inefficient. Do we ever actually need to do | 
 | 483 |       // anything here? The result should be just reconstructed on the other | 
 | 484 |       // side, so extension should be a non-issue. | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 485 |       getTypes().GetExpandedTypes(ParamType, Tys); | 
 | 486 |       Index += Tys.size(); | 
 | 487 |       continue; | 
 | 488 |     } | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 489 |     } | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 490 |        | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 491 |     if (Attributes) | 
 | 492 |       PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 493 |     ++Index; | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 494 |   } | 
| Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 495 |   if (FuncAttrs) | 
 | 496 |     PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); | 
| Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 497 | } | 
 | 498 |  | 
| Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 499 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, | 
 | 500 |                                          llvm::Function *Fn, | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 501 |                                          const FunctionArgList &Args) { | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 502 |   // FIXME: We no longer need the types from FunctionArgList; lift up and | 
 | 503 |   // simplify. | 
| Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 504 |  | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 505 |   // Emit allocs for param decls.  Give the LLVM Argument nodes names. | 
 | 506 |   llvm::Function::arg_iterator AI = Fn->arg_begin(); | 
 | 507 |    | 
 | 508 |   // Name the struct return argument. | 
| Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 509 |   if (CGM.ReturnTypeUsesSret(FI)) { | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 510 |     AI->setName("agg.result"); | 
 | 511 |     ++AI; | 
 | 512 |   } | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 513 |      | 
| Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 514 |   assert(FI.arg_size() == Args.size() && | 
 | 515 |          "Mismatch between function signature & arguments."); | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 516 |   CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 517 |   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 518 |        i != e; ++i, ++info_it) { | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 519 |     const VarDecl *Arg = i->first; | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 520 |     QualType Ty = info_it->type; | 
 | 521 |     const ABIArgInfo &ArgI = info_it->info; | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 522 |  | 
 | 523 |     switch (ArgI.getKind()) { | 
| Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 524 |     case ABIArgInfo::Indirect: { | 
 | 525 |       llvm::Value* V = AI; | 
 | 526 |       if (hasAggregateLLVMType(Ty)) { | 
 | 527 |         // Do nothing, aggregates and complex variables are accessed by | 
 | 528 |         // reference. | 
 | 529 |       } else { | 
 | 530 |         // Load scalar value from indirect argument. | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 531 |         V = EmitLoadOfScalar(V, false, Ty); | 
| Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 532 |         if (!getContext().typesAreCompatible(Ty, Arg->getType())) { | 
 | 533 |           // This must be a promotion, for something like | 
 | 534 |           // "void a(x) short x; {..." | 
 | 535 |           V = EmitScalarConversion(V, Ty, Arg->getType()); | 
 | 536 |         } | 
 | 537 |       } | 
 | 538 |       EmitParmDecl(*Arg, V);       | 
 | 539 |       break; | 
 | 540 |     } | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 541 |  | 
 | 542 |     case ABIArgInfo::Extend: | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 543 |     case ABIArgInfo::Direct: { | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 544 |       assert(AI != Fn->arg_end() && "Argument mismatch!"); | 
 | 545 |       llvm::Value* V = AI; | 
| Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 546 |       if (hasAggregateLLVMType(Ty)) { | 
 | 547 |         // Create a temporary alloca to hold the argument; the rest of | 
 | 548 |         // codegen expects to access aggregates & complex values by | 
 | 549 |         // reference. | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 550 |         V = CreateTempAlloca(ConvertTypeForMem(Ty)); | 
| Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 551 |         Builder.CreateStore(AI, V); | 
 | 552 |       } else { | 
 | 553 |         if (!getContext().typesAreCompatible(Ty, Arg->getType())) { | 
 | 554 |           // This must be a promotion, for something like | 
 | 555 |           // "void a(x) short x; {..." | 
 | 556 |           V = EmitScalarConversion(V, Ty, Arg->getType()); | 
 | 557 |         } | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 558 |       } | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 559 |       EmitParmDecl(*Arg, V); | 
 | 560 |       break; | 
 | 561 |     } | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 562 |        | 
 | 563 |     case ABIArgInfo::Expand: { | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 564 |       // If this structure was expanded into multiple arguments then | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 565 |       // we need to create a temporary and reconstruct it from the | 
 | 566 |       // arguments. | 
| Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 567 |       std::string Name = Arg->getNameAsString(); | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 568 |       llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),  | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 569 |                                            (Name + ".addr").c_str()); | 
 | 570 |       // FIXME: What are the right qualifiers here? | 
 | 571 |       llvm::Function::arg_iterator End =  | 
 | 572 |         ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);       | 
 | 573 |       EmitParmDecl(*Arg, Temp); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 574 |  | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 575 |       // Name the arguments used in expansion and increment AI. | 
 | 576 |       unsigned Index = 0; | 
 | 577 |       for (; AI != End; ++AI, ++Index) | 
 | 578 |         AI->setName(Name + "." + llvm::utostr(Index)); | 
 | 579 |       continue; | 
 | 580 |     } | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 581 |  | 
 | 582 |     case ABIArgInfo::Ignore: | 
| Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 583 |       // Initialize the local variable appropriately. | 
 | 584 |       if (hasAggregateLLVMType(Ty)) {  | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 585 |         EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty))); | 
| Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 586 |       } else { | 
 | 587 |         EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType()))); | 
 | 588 |       } | 
 | 589 |        | 
| Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 590 |       // Skip increment, no matching LLVM parameter. | 
 | 591 |       continue;  | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 592 |  | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 593 |     case ABIArgInfo::Coerce: { | 
 | 594 |       assert(AI != Fn->arg_end() && "Argument mismatch!"); | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 595 |       // FIXME: This is very wasteful; EmitParmDecl is just going to drop the | 
 | 596 |       // result in a new alloca anyway, so we could just store into that | 
 | 597 |       // directly if we broke the abstraction down more. | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 598 |       llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce"); | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 599 |       CreateCoercedStore(AI, V, *this); | 
 | 600 |       // Match to what EmitParmDecl is expecting for this type. | 
| Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 601 |       if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 602 |         V = EmitLoadOfScalar(V, false, Ty); | 
| Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 603 |         if (!getContext().typesAreCompatible(Ty, Arg->getType())) { | 
 | 604 |           // This must be a promotion, for something like | 
 | 605 |           // "void a(x) short x; {..." | 
 | 606 |           V = EmitScalarConversion(V, Ty, Arg->getType()); | 
 | 607 |         } | 
 | 608 |       } | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 609 |       EmitParmDecl(*Arg, V); | 
 | 610 |       break; | 
 | 611 |     } | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 612 |     } | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 613 |  | 
 | 614 |     ++AI; | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 615 |   } | 
 | 616 |   assert(AI == Fn->arg_end() && "Argument mismatch!"); | 
 | 617 | } | 
 | 618 |  | 
| Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 619 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 620 |                                          llvm::Value *ReturnValue) { | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 621 |   llvm::Value *RV = 0; | 
 | 622 |  | 
 | 623 |   // Functions with no result always return void. | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 624 |   if (ReturnValue) { | 
| Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 625 |     QualType RetTy = FI.getReturnType(); | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 626 |     const ABIArgInfo &RetAI = FI.getReturnInfo(); | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 627 |  | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 628 |     switch (RetAI.getKind()) { | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 629 |     case ABIArgInfo::Indirect: | 
| Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 630 |       if (RetTy->isAnyComplexType()) { | 
| Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 631 |         ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); | 
 | 632 |         StoreComplexToAddr(RT, CurFn->arg_begin(), false); | 
 | 633 |       } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { | 
 | 634 |         EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); | 
 | 635 |       } else { | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 636 |         EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), | 
| Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 637 |                           false, RetTy); | 
| Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 638 |       } | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 639 |       break; | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 640 |  | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 641 |     case ABIArgInfo::Extend: | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 642 |     case ABIArgInfo::Direct: | 
| Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 643 |       // The internal return value temp always will have | 
 | 644 |       // pointer-to-return-type type. | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 645 |       RV = Builder.CreateLoad(ReturnValue); | 
 | 646 |       break; | 
 | 647 |  | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 648 |     case ABIArgInfo::Ignore: | 
 | 649 |       break; | 
 | 650 |        | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 651 |     case ABIArgInfo::Coerce: | 
| Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 652 |       RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 653 |       break; | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 654 |  | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 655 |     case ABIArgInfo::Expand: | 
 | 656 |       assert(0 && "Invalid ABI kind for return argument");     | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 657 |     } | 
 | 658 |   } | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 659 |    | 
 | 660 |   if (RV) { | 
 | 661 |     Builder.CreateRet(RV); | 
 | 662 |   } else { | 
 | 663 |     Builder.CreateRetVoid(); | 
 | 664 |   } | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 665 | } | 
 | 666 |  | 
| Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 667 | RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) { | 
| Anders Carlsson | 4029ca7 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 668 |   if (ArgType->isReferenceType()) | 
 | 669 |     return EmitReferenceBindingToExpr(E, ArgType); | 
 | 670 |    | 
| Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 671 |   return EmitAnyExprToTemp(E); | 
 | 672 | } | 
 | 673 |  | 
| Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 674 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, | 
 | 675 |                                  llvm::Value *Callee,  | 
| Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 676 |                                  const CallArgList &CallArgs, | 
 | 677 |                                  const Decl *TargetDecl) { | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 678 |   // FIXME: We no longer need the types from CallArgs; lift up and simplify. | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 679 |   llvm::SmallVector<llvm::Value*, 16> Args; | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 680 |  | 
 | 681 |   // Handle struct-return functions by passing a pointer to the | 
 | 682 |   // location that we would like to return into. | 
| Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 683 |   QualType RetTy = CallInfo.getReturnType(); | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 684 |   const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); | 
| Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame^] | 685 |    | 
 | 686 |    | 
 | 687 |   // If the call returns a temporary with struct return, create a temporary | 
 | 688 |   // alloca to hold the result. | 
 | 689 |   if (CGM.ReturnTypeUsesSret(CallInfo)) | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 690 |     Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy))); | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 691 |    | 
| Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 692 |   assert(CallInfo.arg_size() == CallArgs.size() && | 
 | 693 |          "Mismatch between function signature & arguments."); | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 694 |   CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 695 |   for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();  | 
| Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 696 |        I != E; ++I, ++info_it) { | 
 | 697 |     const ABIArgInfo &ArgInfo = info_it->info; | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 698 |     RValue RV = I->first; | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 699 |  | 
 | 700 |     switch (ArgInfo.getKind()) { | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 701 |     case ABIArgInfo::Indirect: | 
| Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 702 |       if (RV.isScalar() || RV.isComplex()) { | 
 | 703 |         // Make a temporary alloca to pass the argument. | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 704 |         Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second))); | 
| Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 705 |         if (RV.isScalar()) | 
| Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 706 |           EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second); | 
| Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 707 |         else | 
 | 708 |           StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);  | 
 | 709 |       } else { | 
 | 710 |         Args.push_back(RV.getAggregateAddr()); | 
 | 711 |       } | 
 | 712 |       break; | 
 | 713 |  | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 714 |     case ABIArgInfo::Extend: | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 715 |     case ABIArgInfo::Direct: | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 716 |       if (RV.isScalar()) { | 
 | 717 |         Args.push_back(RV.getScalarVal()); | 
 | 718 |       } else if (RV.isComplex()) { | 
| Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 719 |         llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second)); | 
 | 720 |         Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0); | 
 | 721 |         Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1); | 
 | 722 |         Args.push_back(Tmp); | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 723 |       } else { | 
| Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 724 |         Args.push_back(Builder.CreateLoad(RV.getAggregateAddr())); | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 725 |       } | 
 | 726 |       break; | 
 | 727 |       | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 728 |     case ABIArgInfo::Ignore: | 
 | 729 |       break; | 
 | 730 |  | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 731 |     case ABIArgInfo::Coerce: { | 
 | 732 |       // FIXME: Avoid the conversion through memory if possible. | 
 | 733 |       llvm::Value *SrcPtr; | 
 | 734 |       if (RV.isScalar()) { | 
| Daniel Dunbar | 5a1be6e | 2009-02-03 23:04:57 +0000 | [diff] [blame] | 735 |         SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); | 
| Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 736 |         EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second); | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 737 |       } else if (RV.isComplex()) { | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 738 |         SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 739 |         StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); | 
 | 740 |       } else  | 
 | 741 |         SrcPtr = RV.getAggregateAddr(); | 
 | 742 |       Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),  | 
 | 743 |                                        *this)); | 
 | 744 |       break; | 
 | 745 |     } | 
 | 746 |  | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 747 |     case ABIArgInfo::Expand: | 
 | 748 |       ExpandTypeToArgs(I->second, RV, Args); | 
 | 749 |       break; | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 750 |     } | 
 | 751 |   } | 
| Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame^] | 752 |    | 
 | 753 |   // If the callee is a bitcast of a function to a varargs pointer to function | 
 | 754 |   // type, check to see if we can remove the bitcast.  This handles some cases | 
 | 755 |   // with unprototyped functions. | 
 | 756 |   if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) | 
 | 757 |     if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { | 
 | 758 |       const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); | 
 | 759 |       const llvm::FunctionType *CurFT = | 
 | 760 |         cast<llvm::FunctionType>(CurPT->getElementType()); | 
 | 761 |       const llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); | 
 | 762 |        | 
 | 763 |       if (CE->getOpcode() == llvm::Instruction::BitCast && | 
 | 764 |           ActualFT->getReturnType() == CurFT->getReturnType() && | 
 | 765 |           ActualFT->getNumParams() == CurFT->getNumParams()) { | 
 | 766 |         bool ArgsMatch = true; | 
 | 767 |         for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) | 
 | 768 |           if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { | 
 | 769 |             ArgsMatch = false; | 
 | 770 |             break; | 
 | 771 |           } | 
 | 772 |         | 
 | 773 |         // Strip the cast if we can get away with it.  This is a nice cleanup, | 
 | 774 |         // but also allows us to inline the function at -O0 if it is marked | 
 | 775 |         // always_inline. | 
 | 776 |         if (ArgsMatch) | 
 | 777 |           Callee = CalleeF; | 
 | 778 |       } | 
 | 779 |     } | 
 | 780 |    | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 781 |  | 
| Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 782 |   llvm::BasicBlock *InvokeDest = getInvokeDest(); | 
| Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 783 |   CodeGen::AttributeListType AttributeList; | 
| Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 784 |   CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList); | 
| Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 785 |   llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), | 
 | 786 |                                                    AttributeList.end()); | 
| Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 787 |    | 
| Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 788 |   llvm::CallSite CS; | 
 | 789 |   if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) { | 
| Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 790 |     CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size()); | 
| Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 791 |   } else { | 
 | 792 |     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); | 
| Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 793 |     CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,  | 
| Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 794 |                               Args.data(), Args.data()+Args.size()); | 
| Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 795 |     EmitBlock(Cont); | 
| Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 796 |   } | 
 | 797 |  | 
| Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 798 |   CS.setAttributes(Attrs); | 
| Chris Lattner | 5db7ae5 | 2009-06-13 00:26:38 +0000 | [diff] [blame^] | 799 |   if (const llvm::Function *F = | 
 | 800 |         dyn_cast<llvm::Function>(Callee->stripPointerCasts())) | 
| Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 801 |     CS.setCallingConv(F->getCallingConv()); | 
 | 802 |  | 
 | 803 |   // If the call doesn't return, finish the basic block and clear the | 
 | 804 |   // insertion point; this allows the rest of IRgen to discard | 
 | 805 |   // unreachable code. | 
 | 806 |   if (CS.doesNotReturn()) { | 
 | 807 |     Builder.CreateUnreachable(); | 
 | 808 |     Builder.ClearInsertionPoint(); | 
 | 809 |      | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 810 |     // FIXME: For now, emit a dummy basic block because expr emitters in | 
 | 811 |     // generally are not ready to handle emitting expressions at unreachable | 
 | 812 |     // points. | 
| Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 813 |     EnsureInsertPoint(); | 
 | 814 |      | 
 | 815 |     // Return a reasonable RValue. | 
 | 816 |     return GetUndefRValue(RetTy); | 
 | 817 |   }     | 
 | 818 |  | 
 | 819 |   llvm::Instruction *CI = CS.getInstruction(); | 
| Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 820 |   if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy) | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 821 |     CI->setName("call"); | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 822 |  | 
 | 823 |   switch (RetAI.getKind()) { | 
| Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 824 |   case ABIArgInfo::Indirect: | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 825 |     if (RetTy->isAnyComplexType()) | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 826 |       return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); | 
| Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 827 |     if (CodeGenFunction::hasAggregateLLVMType(RetTy)) | 
| Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 828 |       return RValue::getAggregate(Args[0]); | 
| Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 829 |     return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy)); | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 830 |  | 
| Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 831 |   case ABIArgInfo::Extend: | 
| Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 832 |   case ABIArgInfo::Direct: | 
| Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 833 |     if (RetTy->isAnyComplexType()) { | 
 | 834 |       llvm::Value *Real = Builder.CreateExtractValue(CI, 0); | 
 | 835 |       llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); | 
 | 836 |       return RValue::getComplex(std::make_pair(Real, Imag)); | 
| Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 837 |     } | 
 | 838 |     if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 839 |       llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp"); | 
| Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 840 |       Builder.CreateStore(CI, V); | 
 | 841 |       return RValue::getAggregate(V); | 
| Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 842 |     } | 
 | 843 |     return RValue::get(CI); | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 844 |  | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 845 |   case ABIArgInfo::Ignore: | 
| Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 846 |     // If we are ignoring an argument that had a result, make sure to | 
 | 847 |     // construct the appropriate return value for our caller. | 
| Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 848 |     return GetUndefRValue(RetTy); | 
| Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 849 |  | 
| Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 850 |   case ABIArgInfo::Coerce: { | 
| Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 851 |     // FIXME: Avoid the conversion through memory if possible. | 
| Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 852 |     llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce"); | 
| Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 853 |     CreateCoercedStore(CI, V, *this); | 
| Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 854 |     if (RetTy->isAnyComplexType()) | 
 | 855 |       return RValue::getComplex(LoadComplexFromAddr(V, false)); | 
| Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 856 |     if (CodeGenFunction::hasAggregateLLVMType(RetTy)) | 
| Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 857 |       return RValue::getAggregate(V); | 
| Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 858 |     return RValue::get(EmitLoadOfScalar(V, false, RetTy)); | 
| Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 859 |   } | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 860 |  | 
| Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 861 |   case ABIArgInfo::Expand: | 
 | 862 |     assert(0 && "Invalid ABI kind for return argument");     | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 863 |   } | 
| Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 864 |  | 
 | 865 |   assert(0 && "Unhandled ABIArgInfo::Kind"); | 
 | 866 |   return RValue::get(0); | 
| Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 867 | } | 
| Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 868 |  | 
 | 869 | /* VarArg handling */ | 
 | 870 |  | 
 | 871 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { | 
 | 872 |   return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); | 
 | 873 | } |