CGCall.cpp revision 834af456588587194337bb32671fb9329e73a7f7
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 "CodeGenModule.h" 18#include "clang/AST/ASTContext.h" 19#include "clang/AST/Decl.h" 20#include "clang/AST/DeclObjC.h" 21#include "llvm/ADT/StringExtras.h" 22#include "llvm/ParameterAttributes.h" 23using namespace clang; 24using namespace CodeGen; 25 26/***/ 27 28// FIXME: Use iterator and sidestep silly type array creation. 29 30CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP) 31 : IsVariadic(true) 32{ 33 ArgTypes.push_back(FTNP->getResultType()); 34} 35 36CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP) 37 : IsVariadic(FTP->isVariadic()) 38{ 39 ArgTypes.push_back(FTP->getResultType()); 40 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 41 ArgTypes.push_back(FTP->getArgType(i)); 42} 43 44// FIXME: Is there really any reason to have this still? 45CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD) 46{ 47 const FunctionType *FTy = FD->getType()->getAsFunctionType(); 48 const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy); 49 50 ArgTypes.push_back(FTy->getResultType()); 51 if (FTP) { 52 IsVariadic = FTP->isVariadic(); 53 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 54 ArgTypes.push_back(FTP->getArgType(i)); 55 } else { 56 IsVariadic = true; 57 } 58} 59 60CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD, 61 const ASTContext &Context) 62 : IsVariadic(MD->isVariadic()) 63{ 64 ArgTypes.push_back(MD->getResultType()); 65 ArgTypes.push_back(MD->getSelfDecl()->getType()); 66 ArgTypes.push_back(Context.getObjCSelType()); 67 for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), 68 e = MD->param_end(); i != e; ++i) 69 ArgTypes.push_back((*i)->getType()); 70} 71 72ArgTypeIterator CGFunctionInfo::argtypes_begin() const { 73 return ArgTypes.begin(); 74} 75 76ArgTypeIterator CGFunctionInfo::argtypes_end() const { 77 return ArgTypes.end(); 78} 79 80/***/ 81 82CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) { 83 ArgTypes.push_back(_ResultType); 84 for (CallArgList::const_iterator i = _Args.begin(), e = _Args.end(); i!=e; ++i) 85 ArgTypes.push_back(i->second); 86} 87 88ArgTypeIterator CGCallInfo::argtypes_begin() const { 89 return ArgTypes.begin(); 90} 91 92ArgTypeIterator CGCallInfo::argtypes_end() const { 93 return ArgTypes.end(); 94} 95 96/***/ 97 98/// ABIArgInfo - Helper class to encapsulate information about how a 99/// specific C type should be passed to or returned from a function. 100class ABIArgInfo { 101public: 102 enum Kind { 103 Default, 104 StructRet, /// Only valid for aggregate return types. 105 106 Coerce, /// Only valid for aggregate return types, the argument 107 /// should be accessed by coercion to a provided type. 108 109 ByVal, /// Only valid for aggregate argument types. The 110 /// structure should be passed "byval" with the 111 /// specified alignment (0 indicates default 112 /// alignment). 113 114 Expand, /// Only valid for aggregate argument types. The 115 /// structure should be expanded into consecutive 116 /// arguments for its constituent fields. Currently 117 /// expand is only allowed on structures whose fields 118 /// are all scalar types or are themselves expandable 119 /// types. 120 121 KindFirst=Default, KindLast=Expand 122 }; 123 124private: 125 Kind TheKind; 126 const llvm::Type *TypeData; 127 unsigned UIntData; 128 129 ABIArgInfo(Kind K, const llvm::Type *TD=0, 130 unsigned UI=0) : TheKind(K), 131 TypeData(TD), 132 UIntData(0) {} 133public: 134 static ABIArgInfo getDefault() { 135 return ABIArgInfo(Default); 136 } 137 static ABIArgInfo getStructRet() { 138 return ABIArgInfo(StructRet); 139 } 140 static ABIArgInfo getCoerce(const llvm::Type *T) { 141 assert(T->isSingleValueType() && "Can only coerce to simple types"); 142 return ABIArgInfo(Coerce, T); 143 } 144 static ABIArgInfo getByVal(unsigned Alignment) { 145 return ABIArgInfo(ByVal, 0, Alignment); 146 } 147 static ABIArgInfo getExpand() { 148 return ABIArgInfo(Expand); 149 } 150 151 Kind getKind() const { return TheKind; } 152 bool isDefault() const { return TheKind == Default; } 153 bool isStructRet() const { return TheKind == StructRet; } 154 bool isCoerce() const { return TheKind == Coerce; } 155 bool isByVal() const { return TheKind == ByVal; } 156 bool isExpand() const { return TheKind == Expand; } 157 158 // Coerce accessors 159 const llvm::Type *getCoerceToType() const { 160 assert(TheKind == Coerce && "Invalid kind!"); 161 return TypeData; 162 } 163 164 // ByVal accessors 165 unsigned getByValAlignment() const { 166 assert(TheKind == ByVal && "Invalid kind!"); 167 return UIntData; 168 } 169}; 170 171/***/ 172 173/// isEmptyStruct - Return true iff a structure has no non-empty 174/// members. Note that a structure with a flexible array member is not 175/// considered empty. 176static bool isEmptyStruct(QualType T) { 177 const RecordType *RT = T->getAsStructureType(); 178 if (!RT) 179 return 0; 180 const RecordDecl *RD = RT->getDecl(); 181 if (RD->hasFlexibleArrayMember()) 182 return false; 183 for (RecordDecl::field_const_iterator i = RD->field_begin(), 184 e = RD->field_end(); i != e; ++i) { 185 const FieldDecl *FD = *i; 186 if (!isEmptyStruct(FD->getType())) 187 return false; 188 } 189 return true; 190} 191 192/// isSingleElementStruct - Determine if a structure is a "single 193/// element struct", i.e. it has exactly one non-empty field or 194/// exactly one field which is itself a single element 195/// struct. Structures with flexible array members are never 196/// considered single element structs. 197/// 198/// \return The field declaration for the single non-empty field, if 199/// it exists. 200static const FieldDecl *isSingleElementStruct(QualType T) { 201 const RecordType *RT = T->getAsStructureType(); 202 if (!RT) 203 return 0; 204 205 const RecordDecl *RD = RT->getDecl(); 206 if (RD->hasFlexibleArrayMember()) 207 return 0; 208 209 const FieldDecl *Found = 0; 210 for (RecordDecl::field_const_iterator i = RD->field_begin(), 211 e = RD->field_end(); i != e; ++i) { 212 const FieldDecl *FD = *i; 213 QualType FT = FD->getType(); 214 215 if (isEmptyStruct(FT)) { 216 // Ignore 217 } else if (Found) { 218 return 0; 219 } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) { 220 Found = FD; 221 } else { 222 Found = isSingleElementStruct(FT); 223 if (!Found) 224 return 0; 225 } 226 } 227 228 return Found; 229} 230 231static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { 232 if (!Ty->getAsBuiltinType() && !Ty->isPointerType()) 233 return false; 234 235 uint64_t Size = Context.getTypeSize(Ty); 236 return Size == 32 || Size == 64; 237} 238 239static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, 240 ASTContext &Context) { 241 for (RecordDecl::field_const_iterator i = RD->field_begin(), 242 e = RD->field_end(); i != e; ++i) { 243 const FieldDecl *FD = *i; 244 245 if (!is32Or64BitBasicType(FD->getType(), Context)) 246 return false; 247 248 // If this is a bit-field we need to make sure it is still a 249 // 32-bit or 64-bit type. 250 if (Expr *BW = FD->getBitWidth()) { 251 unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue(); 252 if (Width <= 16) 253 return false; 254 } 255 } 256 return true; 257} 258 259static ABIArgInfo classifyReturnType(QualType RetTy, 260 ASTContext &Context) { 261 assert(!RetTy->isArrayType() && 262 "Array types cannot be passed directly."); 263 if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 264 // Classify "single element" structs as their element type. 265 const FieldDecl *SeltFD = isSingleElementStruct(RetTy); 266 if (SeltFD) { 267 QualType SeltTy = SeltFD->getType()->getDesugaredType(); 268 if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) { 269 // FIXME: This is gross, it would be nice if we could just 270 // pass back SeltTy and have clients deal with it. Is it worth 271 // supporting coerce to both LLVM and clang Types? 272 if (BT->isIntegerType()) { 273 uint64_t Size = Context.getTypeSize(SeltTy); 274 return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size)); 275 } else if (BT->getKind() == BuiltinType::Float) { 276 return ABIArgInfo::getCoerce(llvm::Type::FloatTy); 277 } else if (BT->getKind() == BuiltinType::Double) { 278 return ABIArgInfo::getCoerce(llvm::Type::DoubleTy); 279 } 280 } else if (SeltTy->isPointerType()) { 281 // FIXME: It would be really nice if this could come out as 282 // the proper pointer type. 283 llvm::Type *PtrTy = 284 llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 285 return ABIArgInfo::getCoerce(PtrTy); 286 } 287 } 288 289 uint64_t Size = Context.getTypeSize(RetTy); 290 if (Size == 8) { 291 return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); 292 } else if (Size == 16) { 293 return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); 294 } else if (Size == 32) { 295 return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); 296 } else if (Size == 64) { 297 return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); 298 } else { 299 return ABIArgInfo::getStructRet(); 300 } 301 } else { 302 return ABIArgInfo::getDefault(); 303 } 304} 305 306static ABIArgInfo classifyArgumentType(QualType Ty, 307 ASTContext &Context) { 308 assert(!Ty->isArrayType() && "Array types cannot be passed directly."); 309 if (CodeGenFunction::hasAggregateLLVMType(Ty)) { 310 // Structures with flexible arrays are always byval. 311 if (const RecordType *RT = Ty->getAsStructureType()) 312 if (RT->getDecl()->hasFlexibleArrayMember()) 313 return ABIArgInfo::getByVal(0); 314 315 // Expand empty structs (i.e. ignore) 316 uint64_t Size = Context.getTypeSize(Ty); 317 if (Ty->isStructureType() && Size == 0) 318 return ABIArgInfo::getExpand(); 319 320 // Expand structs with size <= 128-bits which consist only of 321 // basic types (int, long long, float, double, xxx*). This is 322 // non-recursive and does not ignore empty fields. 323 if (const RecordType *RT = Ty->getAsStructureType()) { 324 if (Context.getTypeSize(Ty) <= 4*32 && 325 areAllFields32Or64BitBasicType(RT->getDecl(), Context)) 326 return ABIArgInfo::getExpand(); 327 } 328 329 return ABIArgInfo::getByVal(0); 330 } else { 331 return ABIArgInfo::getDefault(); 332 } 333} 334 335static ABIArgInfo getABIReturnInfo(QualType Ty, 336 ASTContext &Context) { 337 ABIArgInfo Info = classifyReturnType(Ty, Context); 338 // Ensure default on aggregate types is StructRet. 339 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) 340 return ABIArgInfo::getStructRet(); 341 return Info; 342} 343 344static ABIArgInfo getABIArgumentInfo(QualType Ty, 345 ASTContext &Context) { 346 ABIArgInfo Info = classifyArgumentType(Ty, Context); 347 // Ensure default on aggregate types is ByVal. 348 if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) 349 return ABIArgInfo::getByVal(0); 350 return Info; 351} 352 353/***/ 354 355void CodeGenTypes::GetExpandedTypes(QualType Ty, 356 std::vector<const llvm::Type*> &ArgTys) { 357 const RecordType *RT = Ty->getAsStructureType(); 358 assert(RT && "Can only expand structure types."); 359 const RecordDecl *RD = RT->getDecl(); 360 assert(!RD->hasFlexibleArrayMember() && 361 "Cannot expand structure with flexible array."); 362 363 for (RecordDecl::field_const_iterator i = RD->field_begin(), 364 e = RD->field_end(); i != e; ++i) { 365 const FieldDecl *FD = *i; 366 assert(!FD->isBitField() && 367 "Cannot expand structure with bit-field members."); 368 369 QualType FT = FD->getType(); 370 if (CodeGenFunction::hasAggregateLLVMType(FT)) { 371 GetExpandedTypes(FT, ArgTys); 372 } else { 373 ArgTys.push_back(ConvertType(FT)); 374 } 375 } 376} 377 378llvm::Function::arg_iterator 379CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, 380 llvm::Function::arg_iterator AI) { 381 const RecordType *RT = Ty->getAsStructureType(); 382 assert(RT && "Can only expand structure types."); 383 384 RecordDecl *RD = RT->getDecl(); 385 assert(LV.isSimple() && 386 "Unexpected non-simple lvalue during struct expansion."); 387 llvm::Value *Addr = LV.getAddress(); 388 for (RecordDecl::field_iterator i = RD->field_begin(), 389 e = RD->field_end(); i != e; ++i) { 390 FieldDecl *FD = *i; 391 QualType FT = FD->getType(); 392 393 // FIXME: What are the right qualifiers here? 394 LValue LV = EmitLValueForField(Addr, FD, false, 0); 395 if (CodeGenFunction::hasAggregateLLVMType(FT)) { 396 AI = ExpandTypeFromArgs(FT, LV, AI); 397 } else { 398 EmitStoreThroughLValue(RValue::get(AI), LV, FT); 399 ++AI; 400 } 401 } 402 403 return AI; 404} 405 406void 407CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, 408 llvm::SmallVector<llvm::Value*, 16> &Args) { 409 const RecordType *RT = Ty->getAsStructureType(); 410 assert(RT && "Can only expand structure types."); 411 412 RecordDecl *RD = RT->getDecl(); 413 assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); 414 llvm::Value *Addr = RV.getAggregateAddr(); 415 for (RecordDecl::field_iterator i = RD->field_begin(), 416 e = RD->field_end(); i != e; ++i) { 417 FieldDecl *FD = *i; 418 QualType FT = FD->getType(); 419 420 // FIXME: What are the right qualifiers here? 421 LValue LV = EmitLValueForField(Addr, FD, false, 0); 422 if (CodeGenFunction::hasAggregateLLVMType(FT)) { 423 ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); 424 } else { 425 RValue RV = EmitLoadOfLValue(LV, FT); 426 assert(RV.isScalar() && 427 "Unexpected non-scalar rvalue during struct expansion."); 428 Args.push_back(RV.getScalarVal()); 429 } 430 } 431} 432 433/***/ 434 435const llvm::FunctionType * 436CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) { 437 return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic); 438} 439 440const llvm::FunctionType * 441CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { 442 return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic()); 443} 444 445const llvm::FunctionType * 446CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end, 447 bool IsVariadic) { 448 std::vector<const llvm::Type*> ArgTys; 449 450 const llvm::Type *ResultType = 0; 451 452 QualType RetTy = *begin; 453 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext()); 454 switch (RetAI.getKind()) { 455 case ABIArgInfo::ByVal: 456 case ABIArgInfo::Expand: 457 assert(0 && "Invalid ABI kind for return argument"); 458 459 case ABIArgInfo::Default: 460 if (RetTy->isVoidType()) { 461 ResultType = llvm::Type::VoidTy; 462 } else { 463 ResultType = ConvertType(RetTy); 464 } 465 break; 466 467 case ABIArgInfo::StructRet: { 468 ResultType = llvm::Type::VoidTy; 469 const llvm::Type *STy = ConvertType(RetTy); 470 ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); 471 break; 472 } 473 474 case ABIArgInfo::Coerce: 475 ResultType = RetAI.getCoerceToType(); 476 break; 477 } 478 479 for (++begin; begin != end; ++begin) { 480 ABIArgInfo AI = getABIArgumentInfo(*begin, getContext()); 481 const llvm::Type *Ty = ConvertType(*begin); 482 483 switch (AI.getKind()) { 484 case ABIArgInfo::Coerce: 485 case ABIArgInfo::StructRet: 486 assert(0 && "Invalid ABI kind for non-return argument"); 487 488 case ABIArgInfo::ByVal: 489 // byval arguments are always on the stack, which is addr space #0. 490 ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); 491 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); 492 break; 493 494 case ABIArgInfo::Default: 495 ArgTys.push_back(Ty); 496 break; 497 498 case ABIArgInfo::Expand: 499 GetExpandedTypes(*begin, ArgTys); 500 break; 501 } 502 } 503 504 return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); 505} 506 507bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) { 508 return getABIReturnInfo(RetTy, getContext()).isStructRet(); 509} 510 511void CodeGenModule::ConstructParamAttrList(const Decl *TargetDecl, 512 ArgTypeIterator begin, 513 ArgTypeIterator end, 514 ParamAttrListType &PAL) { 515 unsigned FuncAttrs = 0; 516 517 if (TargetDecl) { 518 if (TargetDecl->getAttr<NoThrowAttr>()) 519 FuncAttrs |= llvm::ParamAttr::NoUnwind; 520 if (TargetDecl->getAttr<NoReturnAttr>()) 521 FuncAttrs |= llvm::ParamAttr::NoReturn; 522 } 523 524 QualType RetTy = *begin; 525 unsigned Index = 1; 526 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext()); 527 switch (RetAI.getKind()) { 528 case ABIArgInfo::Default: 529 if (RetTy->isPromotableIntegerType()) { 530 if (RetTy->isSignedIntegerType()) { 531 FuncAttrs |= llvm::ParamAttr::SExt; 532 } else if (RetTy->isUnsignedIntegerType()) { 533 FuncAttrs |= llvm::ParamAttr::ZExt; 534 } 535 } 536 break; 537 538 case ABIArgInfo::StructRet: 539 PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, 540 llvm::ParamAttr::StructRet| 541 llvm::ParamAttr::NoAlias)); 542 ++Index; 543 break; 544 545 case ABIArgInfo::Coerce: 546 break; 547 548 case ABIArgInfo::ByVal: 549 case ABIArgInfo::Expand: 550 assert(0 && "Invalid ABI kind for return argument"); 551 } 552 553 if (FuncAttrs) 554 PAL.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs)); 555 for (++begin; begin != end; ++begin) { 556 QualType ParamType = *begin; 557 unsigned ParamAttrs = 0; 558 ABIArgInfo AI = getABIArgumentInfo(ParamType, getContext()); 559 560 switch (AI.getKind()) { 561 case ABIArgInfo::StructRet: 562 case ABIArgInfo::Coerce: 563 assert(0 && "Invalid ABI kind for non-return argument"); 564 565 case ABIArgInfo::ByVal: 566 ParamAttrs |= llvm::ParamAttr::ByVal; 567 assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); 568 break; 569 570 case ABIArgInfo::Default: 571 if (ParamType->isPromotableIntegerType()) { 572 if (ParamType->isSignedIntegerType()) { 573 ParamAttrs |= llvm::ParamAttr::SExt; 574 } else if (ParamType->isUnsignedIntegerType()) { 575 ParamAttrs |= llvm::ParamAttr::ZExt; 576 } 577 } 578 break; 579 580 case ABIArgInfo::Expand: { 581 std::vector<const llvm::Type*> Tys; 582 // FIXME: This is rather inefficient. Do we ever actually need 583 // to do anything here? The result should be just reconstructed 584 // on the other side, so extension should be a non-issue. 585 getTypes().GetExpandedTypes(ParamType, Tys); 586 Index += Tys.size(); 587 continue; 588 } 589 } 590 591 if (ParamAttrs) 592 PAL.push_back(llvm::ParamAttrsWithIndex::get(Index, ParamAttrs)); 593 ++Index; 594 } 595} 596 597void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn, 598 QualType RetTy, 599 const FunctionArgList &Args) { 600 // Emit allocs for param decls. Give the LLVM Argument nodes names. 601 llvm::Function::arg_iterator AI = Fn->arg_begin(); 602 603 // Name the struct return argument. 604 if (CGM.ReturnTypeUsesSret(RetTy)) { 605 AI->setName("agg.result"); 606 ++AI; 607 } 608 609 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 610 i != e; ++i) { 611 const VarDecl *Arg = i->first; 612 QualType Ty = i->second; 613 ABIArgInfo ArgI = getABIArgumentInfo(Ty, getContext()); 614 615 switch (ArgI.getKind()) { 616 case ABIArgInfo::ByVal: 617 case ABIArgInfo::Default: { 618 assert(AI != Fn->arg_end() && "Argument mismatch!"); 619 llvm::Value* V = AI; 620 if (!getContext().typesAreCompatible(Ty, Arg->getType())) { 621 // This must be a promotion, for something like 622 // "void a(x) short x; {..." 623 V = EmitScalarConversion(V, Ty, Arg->getType()); 624 } 625 EmitParmDecl(*Arg, V); 626 break; 627 } 628 629 case ABIArgInfo::Expand: { 630 // If this was structure was expand into multiple arguments then 631 // we need to create a temporary and reconstruct it from the 632 // arguments. 633 std::string Name(Arg->getName()); 634 llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty), 635 (Name + ".addr").c_str()); 636 // FIXME: What are the right qualifiers here? 637 llvm::Function::arg_iterator End = 638 ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); 639 EmitParmDecl(*Arg, Temp); 640 641 // Name the arguments used in expansion and increment AI. 642 unsigned Index = 0; 643 for (; AI != End; ++AI, ++Index) 644 AI->setName(Name + "." + llvm::utostr(Index)); 645 continue; 646 } 647 648 case ABIArgInfo::Coerce: 649 case ABIArgInfo::StructRet: 650 assert(0 && "Invalid ABI kind for non-return argument"); 651 } 652 653 ++AI; 654 } 655 assert(AI == Fn->arg_end() && "Argument mismatch!"); 656} 657 658void CodeGenFunction::EmitFunctionEpilog(QualType RetTy, 659 llvm::Value *ReturnValue) { 660 llvm::Value *RV = 0; 661 662 // Functions with no result always return void. 663 if (ReturnValue) { 664 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext()); 665 666 switch (RetAI.getKind()) { 667 case ABIArgInfo::StructRet: 668 EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); 669 break; 670 671 case ABIArgInfo::Default: 672 RV = Builder.CreateLoad(ReturnValue); 673 break; 674 675 case ABIArgInfo::Coerce: { 676 const llvm::Type *CoerceToPTy = 677 llvm::PointerType::getUnqual(RetAI.getCoerceToType()); 678 RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy)); 679 break; 680 } 681 682 case ABIArgInfo::ByVal: 683 case ABIArgInfo::Expand: 684 assert(0 && "Invalid ABI kind for return argument"); 685 } 686 } 687 688 if (RV) { 689 Builder.CreateRet(RV); 690 } else { 691 Builder.CreateRetVoid(); 692 } 693} 694 695RValue CodeGenFunction::EmitCall(llvm::Value *Callee, 696 QualType RetTy, 697 const CallArgList &CallArgs) { 698 llvm::SmallVector<llvm::Value*, 16> Args; 699 700 // Handle struct-return functions by passing a pointer to the 701 // location that we would like to return into. 702 ABIArgInfo RetAI = getABIReturnInfo(RetTy, getContext()); 703 switch (RetAI.getKind()) { 704 case ABIArgInfo::StructRet: 705 // Create a temporary alloca to hold the result of the call. :( 706 Args.push_back(CreateTempAlloca(ConvertType(RetTy))); 707 break; 708 709 case ABIArgInfo::Default: 710 case ABIArgInfo::Coerce: 711 break; 712 713 case ABIArgInfo::ByVal: 714 case ABIArgInfo::Expand: 715 assert(0 && "Invalid ABI kind for return argument"); 716 } 717 718 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 719 I != E; ++I) { 720 ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, getContext()); 721 RValue RV = I->first; 722 723 switch (ArgInfo.getKind()) { 724 case ABIArgInfo::ByVal: // Default is byval 725 case ABIArgInfo::Default: 726 if (RV.isScalar()) { 727 Args.push_back(RV.getScalarVal()); 728 } else if (RV.isComplex()) { 729 // Make a temporary alloca to pass the argument. 730 Args.push_back(CreateTempAlloca(ConvertType(I->second))); 731 StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); 732 } else { 733 Args.push_back(RV.getAggregateAddr()); 734 } 735 break; 736 737 case ABIArgInfo::StructRet: 738 case ABIArgInfo::Coerce: 739 assert(0 && "Invalid ABI kind for non-return argument"); 740 break; 741 742 case ABIArgInfo::Expand: 743 ExpandTypeToArgs(I->second, RV, Args); 744 break; 745 } 746 } 747 748 llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); 749 CGCallInfo CallInfo(RetTy, CallArgs); 750 751 // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. 752 CodeGen::ParamAttrListType ParamAttrList; 753 CGM.ConstructParamAttrList(0, 754 CallInfo.argtypes_begin(), CallInfo.argtypes_end(), 755 ParamAttrList); 756 CI->setParamAttrs(llvm::PAListPtr::get(ParamAttrList.begin(), 757 ParamAttrList.size())); 758 759 if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) 760 CI->setCallingConv(F->getCallingConv()); 761 if (CI->getType() != llvm::Type::VoidTy) 762 CI->setName("call"); 763 764 switch (RetAI.getKind()) { 765 case ABIArgInfo::StructRet: 766 if (RetTy->isAnyComplexType()) 767 return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); 768 else 769 // Struct return. 770 return RValue::getAggregate(Args[0]); 771 772 case ABIArgInfo::Default: 773 return RValue::get(RetTy->isVoidType() ? 0 : CI); 774 775 case ABIArgInfo::Coerce: { 776 const llvm::Type *CoerceToPTy = 777 llvm::PointerType::getUnqual(RetAI.getCoerceToType()); 778 llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp"); 779 Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy)); 780 return RValue::getAggregate(V); 781 } 782 783 case ABIArgInfo::ByVal: 784 case ABIArgInfo::Expand: 785 assert(0 && "Invalid ABI kind for return argument"); 786 } 787 788 assert(0 && "Unhandled ABIArgInfo::Kind"); 789 return RValue::get(0); 790} 791