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