CodeGenFunction.h revision 5d4d946ec2d88696fd8422aeb64dc29688e6a2c1
1//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- 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// This is the internal per-function state used for llvm translation. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef CLANG_CODEGEN_CODEGENFUNCTION_H 15#define CLANG_CODEGEN_CODEGENFUNCTION_H 16 17#include "clang/AST/Type.h" 18#include "clang/AST/ExprCXX.h" 19#include "clang/AST/ExprObjC.h" 20#include "clang/Basic/TargetInfo.h" 21#include "llvm/ADT/DenseMap.h" 22#include "llvm/ADT/SmallVector.h" 23#include "llvm/Support/ValueHandle.h" 24#include <map> 25#include "CodeGenModule.h" 26#include "CGBlocks.h" 27#include "CGBuilder.h" 28#include "CGCall.h" 29#include "CGCXX.h" 30#include "CGValue.h" 31 32namespace llvm { 33 class BasicBlock; 34 class LLVMContext; 35 class Module; 36 class SwitchInst; 37 class Twine; 38 class Value; 39} 40 41namespace clang { 42 class ASTContext; 43 class CXXDestructorDecl; 44 class CXXTryStmt; 45 class Decl; 46 class EnumConstantDecl; 47 class FunctionDecl; 48 class FunctionProtoType; 49 class LabelStmt; 50 class ObjCContainerDecl; 51 class ObjCInterfaceDecl; 52 class ObjCIvarDecl; 53 class ObjCMethodDecl; 54 class ObjCImplementationDecl; 55 class ObjCPropertyImplDecl; 56 class TargetInfo; 57 class VarDecl; 58 class ObjCForCollectionStmt; 59 class ObjCAtTryStmt; 60 class ObjCAtThrowStmt; 61 class ObjCAtSynchronizedStmt; 62 63namespace CodeGen { 64 class CodeGenModule; 65 class CodeGenTypes; 66 class CGDebugInfo; 67 class CGFunctionInfo; 68 class CGRecordLayout; 69 70/// CodeGenFunction - This class organizes the per-function state that is used 71/// while generating LLVM code. 72class CodeGenFunction : public BlockFunction { 73 CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT 74 void operator=(const CodeGenFunction&); // DO NOT IMPLEMENT 75public: 76 CodeGenModule &CGM; // Per-module state. 77 const TargetInfo &Target; 78 79 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; 80 CGBuilderTy Builder; 81 82 /// CurFuncDecl - Holds the Decl for the current function or ObjC method. 83 /// This excludes BlockDecls. 84 const Decl *CurFuncDecl; 85 /// CurCodeDecl - This is the inner-most code context, which includes blocks. 86 const Decl *CurCodeDecl; 87 const CGFunctionInfo *CurFnInfo; 88 QualType FnRetTy; 89 llvm::Function *CurFn; 90 91 /// ReturnBlock - Unified return block. 92 llvm::BasicBlock *ReturnBlock; 93 /// ReturnValue - The temporary alloca to hold the return value. This is null 94 /// iff the function has no return value. 95 llvm::Instruction *ReturnValue; 96 97 /// AllocaInsertPoint - This is an instruction in the entry block before which 98 /// we prefer to insert allocas. 99 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt; 100 101 const llvm::Type *LLVMIntTy; 102 uint32_t LLVMPointerWidth; 103 104public: 105 /// ObjCEHValueStack - Stack of Objective-C exception values, used for 106 /// rethrows. 107 llvm::SmallVector<llvm::Value*, 8> ObjCEHValueStack; 108 109 /// PushCleanupBlock - Push a new cleanup entry on the stack and set the 110 /// passed in block as the cleanup block. 111 void PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock, 112 llvm::BasicBlock *CleanupExitBlock = 0); 113 114 /// CleanupBlockInfo - A struct representing a popped cleanup block. 115 struct CleanupBlockInfo { 116 /// CleanupEntryBlock - the cleanup entry block 117 llvm::BasicBlock *CleanupBlock; 118 119 /// SwitchBlock - the block (if any) containing the switch instruction used 120 /// for jumping to the final destination. 121 llvm::BasicBlock *SwitchBlock; 122 123 /// EndBlock - the default destination for the switch instruction. 124 llvm::BasicBlock *EndBlock; 125 126 CleanupBlockInfo(llvm::BasicBlock *cb, llvm::BasicBlock *sb, 127 llvm::BasicBlock *eb) 128 : CleanupBlock(cb), SwitchBlock(sb), EndBlock(eb) {} 129 }; 130 131 /// PopCleanupBlock - Will pop the cleanup entry on the stack, process all 132 /// branch fixups and return a block info struct with the switch block and end 133 /// block. 134 CleanupBlockInfo PopCleanupBlock(); 135 136 /// DelayedCleanupBlock - RAII object that will create a cleanup block and set the 137 /// insert point to that block. When destructed, it sets the insert point to 138 /// the previous block and pushes a new cleanup entry on the stack. 139 class DelayedCleanupBlock { 140 CodeGenFunction& CGF; 141 llvm::BasicBlock *CurBB; 142 llvm::BasicBlock *CleanupEntryBB; 143 llvm::BasicBlock *CleanupExitBB; 144 145 public: 146 DelayedCleanupBlock(CodeGenFunction &cgf) 147 : CGF(cgf), CurBB(CGF.Builder.GetInsertBlock()), 148 CleanupEntryBB(CGF.createBasicBlock("cleanup")), CleanupExitBB(0) { 149 CGF.Builder.SetInsertPoint(CleanupEntryBB); 150 } 151 152 llvm::BasicBlock *getCleanupExitBlock() { 153 if (!CleanupExitBB) 154 CleanupExitBB = CGF.createBasicBlock("cleanup.exit"); 155 return CleanupExitBB; 156 } 157 158 ~DelayedCleanupBlock() { 159 CGF.PushCleanupBlock(CleanupEntryBB, CleanupExitBB); 160 // FIXME: This is silly, move this into the builder. 161 if (CurBB) 162 CGF.Builder.SetInsertPoint(CurBB); 163 else 164 CGF.Builder.ClearInsertionPoint(); 165 } 166 }; 167 168 /// \brief Enters a new scope for capturing cleanups, all of which will be 169 /// executed once the scope is exited. 170 class CleanupScope { 171 CodeGenFunction& CGF; 172 size_t CleanupStackDepth; 173 bool OldDidCallStackSave; 174 175 CleanupScope(const CleanupScope &); // DO NOT IMPLEMENT 176 CleanupScope &operator=(const CleanupScope &); // DO NOT IMPLEMENT 177 178 public: 179 /// \brief Enter a new cleanup scope. 180 explicit CleanupScope(CodeGenFunction &CGF) : CGF(CGF) { 181 CleanupStackDepth = CGF.CleanupEntries.size(); 182 OldDidCallStackSave = CGF.DidCallStackSave; 183 } 184 185 /// \brief Exit this cleanup scope, emitting any accumulated 186 /// cleanups. 187 ~CleanupScope() { 188 CGF.DidCallStackSave = OldDidCallStackSave; 189 CGF.EmitCleanupBlocks(CleanupStackDepth); 190 } 191 }; 192 193 /// EmitCleanupBlocks - Takes the old cleanup stack size and emits the cleanup 194 /// blocks that have been added. 195 void EmitCleanupBlocks(size_t OldCleanupStackSize); 196 197 /// EmitBranchThroughCleanup - Emit a branch from the current insert block 198 /// through the cleanup handling code (if any) and then on to \arg Dest. 199 /// 200 /// FIXME: Maybe this should really be in EmitBranch? Don't we always want 201 /// this behavior for branches? 202 void EmitBranchThroughCleanup(llvm::BasicBlock *Dest); 203 204 /// StartConditionalBranch - Should be called before a conditional part of an 205 /// expression is emitted. For example, before the RHS of the expression below 206 /// is emitted: 207 /// 208 /// b && f(T()); 209 /// 210 /// This is used to make sure that any temporaries created in the conditional 211 /// branch are only destroyed if the branch is taken. 212 void StartConditionalBranch() { 213 ++ConditionalBranchLevel; 214 } 215 216 /// FinishConditionalBranch - Should be called after a conditional part of an 217 /// expression has been emitted. 218 void FinishConditionalBranch() { 219 --ConditionalBranchLevel; 220 } 221 222private: 223 CGDebugInfo *DebugInfo; 224 225 /// IndirectBranch - The first time an indirect goto is seen we create a 226 /// block with an indirect branch. Every time we see the address of a label 227 /// taken, we add the label to the indirect goto. Every subsequent indirect 228 /// goto is codegen'd as a jump to the IndirectBranch's basic block. 229 llvm::IndirectBrInst *IndirectBranch; 230 231 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C 232 /// decls. 233 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 234 235 /// LabelMap - This keeps track of the LLVM basic block for each C label. 236 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap; 237 238 // BreakContinueStack - This keeps track of where break and continue 239 // statements should jump to. 240 struct BreakContinue { 241 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb) 242 : BreakBlock(bb), ContinueBlock(cb) {} 243 244 llvm::BasicBlock *BreakBlock; 245 llvm::BasicBlock *ContinueBlock; 246 }; 247 llvm::SmallVector<BreakContinue, 8> BreakContinueStack; 248 249 /// SwitchInsn - This is nearest current switch instruction. It is null if if 250 /// current context is not in a switch. 251 llvm::SwitchInst *SwitchInsn; 252 253 /// CaseRangeBlock - This block holds if condition check for last case 254 /// statement range in current switch instruction. 255 llvm::BasicBlock *CaseRangeBlock; 256 257 /// InvokeDest - This is the nearest exception target for calls 258 /// which can unwind, when exceptions are being used. 259 llvm::BasicBlock *InvokeDest; 260 261 // VLASizeMap - This keeps track of the associated size for each VLA type. 262 // We track this by the size expression rather than the type itself because 263 // in certain situations, like a const qualifier applied to an VLA typedef, 264 // multiple VLA types can share the same size expression. 265 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we 266 // enter/leave scopes. 267 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap; 268 269 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid 270 /// calling llvm.stacksave for multiple VLAs in the same scope. 271 bool DidCallStackSave; 272 273 struct CleanupEntry { 274 /// CleanupEntryBlock - The block of code that does the actual cleanup. 275 llvm::BasicBlock *CleanupEntryBlock; 276 277 /// CleanupExitBlock - The cleanup exit block. 278 llvm::BasicBlock *CleanupExitBlock; 279 280 /// Blocks - Basic blocks that were emitted in the current cleanup scope. 281 std::vector<llvm::BasicBlock *> Blocks; 282 283 /// BranchFixups - Branch instructions to basic blocks that haven't been 284 /// inserted into the current function yet. 285 std::vector<llvm::BranchInst *> BranchFixups; 286 287 explicit CleanupEntry(llvm::BasicBlock *CleanupEntryBlock, 288 llvm::BasicBlock *CleanupExitBlock) 289 : CleanupEntryBlock(CleanupEntryBlock), 290 CleanupExitBlock(CleanupExitBlock) {} 291 }; 292 293 /// CleanupEntries - Stack of cleanup entries. 294 llvm::SmallVector<CleanupEntry, 8> CleanupEntries; 295 296 typedef llvm::DenseMap<llvm::BasicBlock*, size_t> BlockScopeMap; 297 298 /// BlockScopes - Map of which "cleanup scope" scope basic blocks have. 299 BlockScopeMap BlockScopes; 300 301 /// CXXThisDecl - When parsing an C++ function, this will hold the implicit 302 /// 'this' declaration. 303 ImplicitParamDecl *CXXThisDecl; 304 305 /// CXXLiveTemporaryInfo - Holds information about a live C++ temporary. 306 struct CXXLiveTemporaryInfo { 307 /// Temporary - The live temporary. 308 const CXXTemporary *Temporary; 309 310 /// ThisPtr - The pointer to the temporary. 311 llvm::Value *ThisPtr; 312 313 /// DtorBlock - The destructor block. 314 llvm::BasicBlock *DtorBlock; 315 316 /// CondPtr - If this is a conditional temporary, this is the pointer to 317 /// the condition variable that states whether the destructor should be 318 /// called or not. 319 llvm::Value *CondPtr; 320 321 CXXLiveTemporaryInfo(const CXXTemporary *temporary, 322 llvm::Value *thisptr, llvm::BasicBlock *dtorblock, 323 llvm::Value *condptr) 324 : Temporary(temporary), ThisPtr(thisptr), DtorBlock(dtorblock), 325 CondPtr(condptr) { } 326 }; 327 328 llvm::SmallVector<CXXLiveTemporaryInfo, 4> LiveTemporaries; 329 330 /// ConditionalBranchLevel - Contains the nesting level of the current 331 /// conditional branch. This is used so that we know if a temporary should be 332 /// destroyed conditionally. 333 unsigned ConditionalBranchLevel; 334 335 336 /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM 337 /// type as well as the field number that contains the actual data. 338 llvm::DenseMap<const ValueDecl *, std::pair<const llvm::Type *, 339 unsigned> > ByRefValueInfo; 340 341 /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field 342 /// number that holds the value. 343 unsigned getByRefValueLLVMField(const ValueDecl *VD) const; 344 345public: 346 CodeGenFunction(CodeGenModule &cgm); 347 348 ASTContext &getContext() const; 349 CGDebugInfo *getDebugInfo() { return DebugInfo; } 350 351 llvm::BasicBlock *getInvokeDest() { return InvokeDest; } 352 void setInvokeDest(llvm::BasicBlock *B) { InvokeDest = B; } 353 354 llvm::LLVMContext &getLLVMContext() { return VMContext; } 355 356 //===--------------------------------------------------------------------===// 357 // Objective-C 358 //===--------------------------------------------------------------------===// 359 360 void GenerateObjCMethod(const ObjCMethodDecl *OMD); 361 362 void StartObjCMethod(const ObjCMethodDecl *MD, 363 const ObjCContainerDecl *CD); 364 365 /// GenerateObjCGetter - Synthesize an Objective-C property getter function. 366 void GenerateObjCGetter(ObjCImplementationDecl *IMP, 367 const ObjCPropertyImplDecl *PID); 368 369 /// GenerateObjCSetter - Synthesize an Objective-C property setter function 370 /// for the given property. 371 void GenerateObjCSetter(ObjCImplementationDecl *IMP, 372 const ObjCPropertyImplDecl *PID); 373 374 //===--------------------------------------------------------------------===// 375 // Block Bits 376 //===--------------------------------------------------------------------===// 377 378 llvm::Value *BuildBlockLiteralTmp(const BlockExpr *); 379 llvm::Constant *BuildDescriptorBlockDecl(bool BlockHasCopyDispose, 380 uint64_t Size, 381 const llvm::StructType *, 382 std::vector<HelperInfo> *); 383 384 llvm::Function *GenerateBlockFunction(const BlockExpr *BExpr, 385 const BlockInfo& Info, 386 const Decl *OuterFuncDecl, 387 llvm::DenseMap<const Decl*, llvm::Value*> ldm, 388 uint64_t &Size, uint64_t &Align, 389 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 390 bool &subBlockHasCopyDispose); 391 392 void BlockForwardSelf(); 393 llvm::Value *LoadBlockStruct(); 394 395 uint64_t AllocateBlockDecl(const BlockDeclRefExpr *E); 396 llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E); 397 const llvm::Type *BuildByRefType(const ValueDecl *D); 398 399 void GenerateCode(GlobalDecl GD, llvm::Function *Fn); 400 void StartFunction(GlobalDecl GD, QualType RetTy, 401 llvm::Function *Fn, 402 const FunctionArgList &Args, 403 SourceLocation StartLoc); 404 405 /// EmitReturnBlock - Emit the unified return block, trying to avoid its 406 /// emission when possible. 407 void EmitReturnBlock(); 408 409 /// FinishFunction - Complete IR generation of the current function. It is 410 /// legal to call this function even if there is no current insertion point. 411 void FinishFunction(SourceLocation EndLoc=SourceLocation()); 412 413 /// DynamicTypeAdjust - Do the non-virtual and virtual adjustments on an 414 /// object pointer to alter the dynamic type of the pointer. Used by 415 /// GenerateCovariantThunk for building thunks. 416 llvm::Value *DynamicTypeAdjust(llvm::Value *V, int64_t nv, int64_t v); 417 418 /// GenerateThunk - Generate a thunk for the given method 419 llvm::Constant *GenerateThunk(llvm::Function *Fn, const CXXMethodDecl *MD, 420 bool Extern, int64_t nv, int64_t v); 421 llvm::Constant *GenerateCovariantThunk(llvm::Function *Fn, 422 const CXXMethodDecl *MD, bool Extern, 423 int64_t nv_t, int64_t v_t, 424 int64_t nv_r, int64_t v_r); 425 426 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type); 427 428 void SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor, 429 CXXCtorType Type, 430 llvm::Function *Fn, 431 const FunctionArgList &Args); 432 433 void SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, 434 llvm::Function *Fn, 435 const FunctionArgList &Args); 436 437 void SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor, 438 CXXCtorType Type, 439 llvm::Function *Fn, 440 const FunctionArgList &Args); 441 442 void SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor, 443 CXXDtorType Type, 444 llvm::Function *Fn, 445 const FunctionArgList &Args); 446 447 /// EmitDtorEpilogue - Emit all code that comes at the end of class's 448 /// destructor. This is to call destructors on members and base classes 449 /// in reverse order of their construction. 450 void EmitDtorEpilogue(const CXXDestructorDecl *Dtor, 451 CXXDtorType Type); 452 453 /// EmitFunctionProlog - Emit the target specific LLVM code to load the 454 /// arguments for the given function. This is also responsible for naming the 455 /// LLVM function arguments. 456 void EmitFunctionProlog(const CGFunctionInfo &FI, 457 llvm::Function *Fn, 458 const FunctionArgList &Args); 459 460 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the 461 /// given temporary. 462 void EmitFunctionEpilog(const CGFunctionInfo &FI, llvm::Value *ReturnValue); 463 464 const llvm::Type *ConvertTypeForMem(QualType T); 465 const llvm::Type *ConvertType(QualType T); 466 467 /// LoadObjCSelf - Load the value of self. This function is only valid while 468 /// generating code for an Objective-C method. 469 llvm::Value *LoadObjCSelf(); 470 471 /// TypeOfSelfObject - Return type of object that this self represents. 472 QualType TypeOfSelfObject(); 473 474 /// hasAggregateLLVMType - Return true if the specified AST type will map into 475 /// an aggregate LLVM type or is void. 476 static bool hasAggregateLLVMType(QualType T); 477 478 /// createBasicBlock - Create an LLVM basic block. 479 llvm::BasicBlock *createBasicBlock(const char *Name="", 480 llvm::Function *Parent=0, 481 llvm::BasicBlock *InsertBefore=0) { 482#ifdef NDEBUG 483 return llvm::BasicBlock::Create(VMContext, "", Parent, InsertBefore); 484#else 485 return llvm::BasicBlock::Create(VMContext, Name, Parent, InsertBefore); 486#endif 487 } 488 489 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified 490 /// label maps to. 491 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S); 492 493 /// SimplifyForwardingBlocks - If the given basic block is only a 494 /// branch to another basic block, simplify it. This assumes that no 495 /// other code could potentially reference the basic block. 496 void SimplifyForwardingBlocks(llvm::BasicBlock *BB); 497 498 /// EmitBlock - Emit the given block \arg BB and set it as the insert point, 499 /// adding a fall-through branch from the current insert block if 500 /// necessary. It is legal to call this function even if there is no current 501 /// insertion point. 502 /// 503 /// IsFinished - If true, indicates that the caller has finished emitting 504 /// branches to the given block and does not expect to emit code into it. This 505 /// means the block can be ignored if it is unreachable. 506 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false); 507 508 /// EmitBranch - Emit a branch to the specified basic block from the current 509 /// insert block, taking care to avoid creation of branches from dummy 510 /// blocks. It is legal to call this function even if there is no current 511 /// insertion point. 512 /// 513 /// This function clears the current insertion point. The caller should follow 514 /// calls to this function with calls to Emit*Block prior to generation new 515 /// code. 516 void EmitBranch(llvm::BasicBlock *Block); 517 518 /// HaveInsertPoint - True if an insertion point is defined. If not, this 519 /// indicates that the current code being emitted is unreachable. 520 bool HaveInsertPoint() const { 521 return Builder.GetInsertBlock() != 0; 522 } 523 524 /// EnsureInsertPoint - Ensure that an insertion point is defined so that 525 /// emitted IR has a place to go. Note that by definition, if this function 526 /// creates a block then that block is unreachable; callers may do better to 527 /// detect when no insertion point is defined and simply skip IR generation. 528 void EnsureInsertPoint() { 529 if (!HaveInsertPoint()) 530 EmitBlock(createBasicBlock()); 531 } 532 533 /// ErrorUnsupported - Print out an error that codegen doesn't support the 534 /// specified stmt yet. 535 void ErrorUnsupported(const Stmt *S, const char *Type, 536 bool OmitOnError=false); 537 538 //===--------------------------------------------------------------------===// 539 // Helpers 540 //===--------------------------------------------------------------------===// 541 542 Qualifiers MakeQualifiers(QualType T) { 543 Qualifiers Quals = getContext().getCanonicalType(T).getQualifiers(); 544 Quals.setObjCGCAttr(getContext().getObjCGCAttrKind(T)); 545 return Quals; 546 } 547 548 /// CreateTempAlloca - This creates a alloca and inserts it into the entry 549 /// block. 550 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty, 551 const llvm::Twine &Name = "tmp"); 552 553 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified 554 /// expression and compare the result against zero, returning an Int1Ty value. 555 llvm::Value *EvaluateExprAsBool(const Expr *E); 556 557 /// EmitAnyExpr - Emit code to compute the specified expression which can have 558 /// any type. The result is returned as an RValue struct. If this is an 559 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where 560 /// the result should be returned. 561 /// 562 /// \param IgnoreResult - True if the resulting value isn't used. 563 RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0, 564 bool IsAggLocVolatile = false, bool IgnoreResult = false, 565 bool IsInitializer = false); 566 567 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address 568 // or the value of the expression, depending on how va_list is defined. 569 llvm::Value *EmitVAListRef(const Expr *E); 570 571 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will 572 /// always be accessible even if no aggregate location is provided. 573 RValue EmitAnyExprToTemp(const Expr *E, bool IsAggLocVolatile = false, 574 bool IsInitializer = false); 575 576 /// EmitAggregateCopy - Emit an aggrate copy. 577 /// 578 /// \param isVolatile - True iff either the source or the destination is 579 /// volatile. 580 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, 581 QualType EltTy, bool isVolatile=false); 582 583 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty); 584 585 /// StartBlock - Start new block named N. If insert block is a dummy block 586 /// then reuse it. 587 void StartBlock(const char *N); 588 589 /// GetAddrOfStaticLocalVar - Return the address of a static local variable. 590 llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD); 591 592 /// GetAddrOfLocalVar - Return the address of a local variable. 593 llvm::Value *GetAddrOfLocalVar(const VarDecl *VD); 594 595 /// getAccessedFieldNo - Given an encoded value and a result number, return 596 /// the input field number being accessed. 597 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); 598 599 llvm::BlockAddress *GetAddrOfLabel(const LabelStmt *L); 600 llvm::BasicBlock *GetIndirectGotoBlock(); 601 602 /// EmitMemSetToZero - Generate code to memset a value of the given type to 0. 603 void EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty); 604 605 // EmitVAArg - Generate code to get an argument from the passed in pointer 606 // and update it accordingly. The return value is a pointer to the argument. 607 // FIXME: We should be able to get rid of this method and use the va_arg 608 // instruction in LLVM instead once it works well enough. 609 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty); 610 611 // EmitVLASize - Generate code for any VLA size expressions that might occur 612 // in a variably modified type. If Ty is a VLA, will return the value that 613 // corresponds to the size in bytes of the VLA type. Will return 0 otherwise. 614 /// 615 /// This function can be called with a null (unreachable) insert point. 616 llvm::Value *EmitVLASize(QualType Ty); 617 618 // GetVLASize - Returns an LLVM value that corresponds to the size in bytes 619 // of a variable length array type. 620 llvm::Value *GetVLASize(const VariableArrayType *); 621 622 /// LoadCXXThis - Load the value of 'this'. This function is only valid while 623 /// generating code for an C++ member function. 624 llvm::Value *LoadCXXThis(); 625 626 /// GetAddressOfBaseClass - This function will add the necessary delta 627 /// to the load of 'this' and returns address of the base class. 628 // FIXME. This currently only does a derived to non-virtual base conversion. 629 // Other kinds of conversions will come later. 630 llvm::Value *GetAddressOfBaseClass(llvm::Value *Value, 631 const CXXRecordDecl *ClassDecl, 632 const CXXRecordDecl *BaseClassDecl, 633 bool NullCheckValue); 634 635 llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value, 636 const CXXRecordDecl *ClassDecl, 637 const CXXRecordDecl *DerivedClassDecl, 638 bool NullCheckValue); 639 640 llvm::Value * 641 GetVirtualCXXBaseClassOffset(llvm::Value *This, 642 const CXXRecordDecl *ClassDecl, 643 const CXXRecordDecl *BaseClassDecl); 644 645 void EmitClassAggrMemberwiseCopy(llvm::Value *DestValue, 646 llvm::Value *SrcValue, 647 const ArrayType *Array, 648 const CXXRecordDecl *BaseClassDecl, 649 QualType Ty); 650 651 void EmitClassAggrCopyAssignment(llvm::Value *DestValue, 652 llvm::Value *SrcValue, 653 const ArrayType *Array, 654 const CXXRecordDecl *BaseClassDecl, 655 QualType Ty); 656 657 void EmitClassMemberwiseCopy(llvm::Value *DestValue, llvm::Value *SrcValue, 658 const CXXRecordDecl *ClassDecl, 659 const CXXRecordDecl *BaseClassDecl, 660 QualType Ty); 661 662 void EmitClassCopyAssignment(llvm::Value *DestValue, llvm::Value *SrcValue, 663 const CXXRecordDecl *ClassDecl, 664 const CXXRecordDecl *BaseClassDecl, 665 QualType Ty); 666 667 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, 668 llvm::Value *This, 669 CallExpr::const_arg_iterator ArgBeg, 670 CallExpr::const_arg_iterator ArgEnd); 671 672 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 673 const ConstantArrayType *ArrayTy, 674 llvm::Value *ArrayPtr, 675 CallExpr::const_arg_iterator ArgBeg, 676 CallExpr::const_arg_iterator ArgEnd); 677 678 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 679 llvm::Value *NumElements, 680 llvm::Value *ArrayPtr, 681 CallExpr::const_arg_iterator ArgBeg, 682 CallExpr::const_arg_iterator ArgEnd); 683 684 void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, 685 const ArrayType *Array, 686 llvm::Value *This); 687 688 void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, 689 llvm::Value *NumElements, 690 llvm::Value *This); 691 692 llvm::Constant * GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, 693 const ArrayType *Array, 694 llvm::Value *This); 695 696 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, 697 llvm::Value *This); 698 699 void PushCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr); 700 void PopCXXTemporary(); 701 702 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E); 703 void EmitCXXDeleteExpr(const CXXDeleteExpr *E); 704 705 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, 706 QualType DeleteTy); 707 708 llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E); 709 llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE); 710 711 //===--------------------------------------------------------------------===// 712 // Declaration Emission 713 //===--------------------------------------------------------------------===// 714 715 /// EmitDecl - Emit a declaration. 716 /// 717 /// This function can be called with a null (unreachable) insert point. 718 void EmitDecl(const Decl &D); 719 720 /// EmitBlockVarDecl - Emit a block variable declaration. 721 /// 722 /// This function can be called with a null (unreachable) insert point. 723 void EmitBlockVarDecl(const VarDecl &D); 724 725 /// EmitLocalBlockVarDecl - Emit a local block variable declaration. 726 /// 727 /// This function can be called with a null (unreachable) insert point. 728 void EmitLocalBlockVarDecl(const VarDecl &D); 729 730 void EmitStaticBlockVarDecl(const VarDecl &D); 731 732 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl. 733 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg); 734 735 //===--------------------------------------------------------------------===// 736 // Statement Emission 737 //===--------------------------------------------------------------------===// 738 739 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info. 740 void EmitStopPoint(const Stmt *S); 741 742 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call 743 /// this function even if there is no current insertion point. 744 /// 745 /// This function may clear the current insertion point; callers should use 746 /// EnsureInsertPoint if they wish to subsequently generate code without first 747 /// calling EmitBlock, EmitBranch, or EmitStmt. 748 void EmitStmt(const Stmt *S); 749 750 /// EmitSimpleStmt - Try to emit a "simple" statement which does not 751 /// necessarily require an insertion point or debug information; typically 752 /// because the statement amounts to a jump or a container of other 753 /// statements. 754 /// 755 /// \return True if the statement was handled. 756 bool EmitSimpleStmt(const Stmt *S); 757 758 RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, 759 llvm::Value *AggLoc = 0, bool isAggVol = false); 760 761 /// EmitLabel - Emit the block for the given label. It is legal to call this 762 /// function even if there is no current insertion point. 763 void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt. 764 765 void EmitLabelStmt(const LabelStmt &S); 766 void EmitGotoStmt(const GotoStmt &S); 767 void EmitIndirectGotoStmt(const IndirectGotoStmt &S); 768 void EmitIfStmt(const IfStmt &S); 769 void EmitWhileStmt(const WhileStmt &S); 770 void EmitDoStmt(const DoStmt &S); 771 void EmitForStmt(const ForStmt &S); 772 void EmitReturnStmt(const ReturnStmt &S); 773 void EmitDeclStmt(const DeclStmt &S); 774 void EmitBreakStmt(const BreakStmt &S); 775 void EmitContinueStmt(const ContinueStmt &S); 776 void EmitSwitchStmt(const SwitchStmt &S); 777 void EmitDefaultStmt(const DefaultStmt &S); 778 void EmitCaseStmt(const CaseStmt &S); 779 void EmitCaseStmtRange(const CaseStmt &S); 780 void EmitAsmStmt(const AsmStmt &S); 781 782 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S); 783 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S); 784 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S); 785 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S); 786 787 void EmitCXXTryStmt(const CXXTryStmt &S); 788 789 //===--------------------------------------------------------------------===// 790 // LValue Expression Emission 791 //===--------------------------------------------------------------------===// 792 793 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type. 794 RValue GetUndefRValue(QualType Ty); 795 796 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E 797 /// and issue an ErrorUnsupported style diagnostic (using the 798 /// provided Name). 799 RValue EmitUnsupportedRValue(const Expr *E, 800 const char *Name); 801 802 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue 803 /// an ErrorUnsupported style diagnostic (using the provided Name). 804 LValue EmitUnsupportedLValue(const Expr *E, 805 const char *Name); 806 807 /// EmitLValue - Emit code to compute a designator that specifies the location 808 /// of the expression. 809 /// 810 /// This can return one of two things: a simple address or a bitfield 811 /// reference. In either case, the LLVM Value* in the LValue structure is 812 /// guaranteed to be an LLVM pointer type. 813 /// 814 /// If this returns a bitfield reference, nothing about the pointee type of 815 /// the LLVM value is known: For example, it may not be a pointer to an 816 /// integer. 817 /// 818 /// If this returns a normal address, and if the lvalue's C type is fixed 819 /// size, this method guarantees that the returned pointer type will point to 820 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a 821 /// variable length type, this is not possible. 822 /// 823 LValue EmitLValue(const Expr *E); 824 825 /// EmitLoadOfScalar - Load a scalar value from an address, taking 826 /// care to appropriately convert from the memory representation to 827 /// the LLVM value representation. 828 llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, 829 QualType Ty); 830 831 /// EmitStoreOfScalar - Store a scalar value to an address, taking 832 /// care to appropriately convert from the memory representation to 833 /// the LLVM value representation. 834 void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, 835 bool Volatile, QualType Ty); 836 837 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, 838 /// this method emits the address of the lvalue, then loads the result as an 839 /// rvalue, returning the rvalue. 840 RValue EmitLoadOfLValue(LValue V, QualType LVType); 841 RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType); 842 RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType); 843 RValue EmitLoadOfPropertyRefLValue(LValue LV, QualType ExprType); 844 RValue EmitLoadOfKVCRefLValue(LValue LV, QualType ExprType); 845 846 847 /// EmitStoreThroughLValue - Store the specified rvalue into the specified 848 /// lvalue, where both are guaranteed to the have the same type, and that type 849 /// is 'Ty'. 850 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty); 851 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst, 852 QualType Ty); 853 void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst, QualType Ty); 854 void EmitStoreThroughKVCRefLValue(RValue Src, LValue Dst, QualType Ty); 855 856 /// EmitStoreThroughLValue - Store Src into Dst with same constraints as 857 /// EmitStoreThroughLValue. 858 /// 859 /// \param Result [out] - If non-null, this will be set to a Value* for the 860 /// bit-field contents after the store, appropriate for use as the result of 861 /// an assignment to the bit-field. 862 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty, 863 llvm::Value **Result=0); 864 865 // Note: only availabe for agg return types 866 LValue EmitBinaryOperatorLValue(const BinaryOperator *E); 867 // Note: only available for agg return types 868 LValue EmitCallExprLValue(const CallExpr *E); 869 // Note: only available for agg return types 870 LValue EmitVAArgExprLValue(const VAArgExpr *E); 871 LValue EmitDeclRefLValue(const DeclRefExpr *E); 872 LValue EmitStringLiteralLValue(const StringLiteral *E); 873 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E); 874 LValue EmitPredefinedFunctionName(unsigned Type); 875 LValue EmitPredefinedLValue(const PredefinedExpr *E); 876 LValue EmitUnaryOpLValue(const UnaryOperator *E); 877 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); 878 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); 879 LValue EmitMemberExpr(const MemberExpr *E); 880 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); 881 LValue EmitConditionalOperatorLValue(const ConditionalOperator *E); 882 LValue EmitCastLValue(const CastExpr *E); 883 LValue EmitNullInitializationLValue(const CXXZeroInitValueExpr *E); 884 885 LValue EmitPointerToDataMemberLValue(const FieldDecl *Field); 886 887 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface, 888 const ObjCIvarDecl *Ivar); 889 LValue EmitLValueForField(llvm::Value* Base, const FieldDecl* Field, 890 bool isUnion, unsigned CVRQualifiers); 891 LValue EmitLValueForIvar(QualType ObjectTy, 892 llvm::Value* Base, const ObjCIvarDecl *Ivar, 893 unsigned CVRQualifiers); 894 895 LValue EmitLValueForBitfield(llvm::Value* Base, const FieldDecl* Field, 896 unsigned CVRQualifiers); 897 898 LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E); 899 900 LValue EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E); 901 LValue EmitCXXConstructLValue(const CXXConstructExpr *E); 902 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E); 903 LValue EmitCXXExprWithTemporariesLValue(const CXXExprWithTemporaries *E); 904 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E); 905 906 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); 907 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); 908 LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E); 909 LValue EmitObjCKVCRefLValue(const ObjCImplicitSetterGetterRefExpr *E); 910 LValue EmitObjCSuperExprLValue(const ObjCSuperExpr *E); 911 LValue EmitStmtExprLValue(const StmtExpr *E); 912 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E); 913 914 //===--------------------------------------------------------------------===// 915 // Scalar Expression Emission 916 //===--------------------------------------------------------------------===// 917 918 /// EmitCall - Generate a call of the given function, expecting the given 919 /// result type, and using the given argument list which specifies both the 920 /// LLVM arguments and the types they were derived from. 921 /// 922 /// \param TargetDecl - If given, the decl of the function in a 923 /// direct call; used to set attributes on the call (noreturn, 924 /// etc.). 925 RValue EmitCall(const CGFunctionInfo &FnInfo, 926 llvm::Value *Callee, 927 const CallArgList &Args, 928 const Decl *TargetDecl = 0); 929 930 RValue EmitCall(llvm::Value *Callee, QualType FnType, 931 CallExpr::const_arg_iterator ArgBeg, 932 CallExpr::const_arg_iterator ArgEnd, 933 const Decl *TargetDecl = 0); 934 RValue EmitCallExpr(const CallExpr *E); 935 936 llvm::Value *BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, 937 const llvm::Type *Ty); 938 llvm::Value *BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, 939 llvm::Value *&This, const llvm::Type *Ty); 940 941 RValue EmitCXXMemberCall(const CXXMethodDecl *MD, 942 llvm::Value *Callee, 943 llvm::Value *This, 944 CallExpr::const_arg_iterator ArgBeg, 945 CallExpr::const_arg_iterator ArgEnd); 946 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E); 947 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E); 948 949 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 950 const CXXMethodDecl *MD); 951 952 953 RValue EmitBuiltinExpr(const FunctionDecl *FD, 954 unsigned BuiltinID, const CallExpr *E); 955 956 RValue EmitBlockCallExpr(const CallExpr *E); 957 958 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call 959 /// is unhandled by the current target. 960 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 961 962 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); 963 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 964 965 llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...); 966 llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals, 967 bool isSplat = false); 968 969 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); 970 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); 971 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); 972 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E); 973 RValue EmitObjCPropertyGet(const Expr *E); 974 RValue EmitObjCSuperPropertyGet(const Expr *Exp, const Selector &S); 975 void EmitObjCPropertySet(const Expr *E, RValue Src); 976 void EmitObjCSuperPropertySet(const Expr *E, const Selector &S, RValue Src); 977 978 979 /// EmitReferenceBindingToExpr - Emits a reference binding to the passed in 980 /// expression. Will emit a temporary variable if E is not an LValue. 981 RValue EmitReferenceBindingToExpr(const Expr* E, QualType DestType, 982 bool IsInitializer = false); 983 984 //===--------------------------------------------------------------------===// 985 // Expression Emission 986 //===--------------------------------------------------------------------===// 987 988 // Expressions are broken into three classes: scalar, complex, aggregate. 989 990 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM 991 /// scalar type, returning the result. 992 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false); 993 994 /// EmitScalarConversion - Emit a conversion from the specified type to the 995 /// specified destination type, both of which are LLVM scalar types. 996 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, 997 QualType DstTy); 998 999 /// EmitComplexToScalarConversion - Emit a conversion from the specified 1000 /// complex type to the specified destination type, where the destination type 1001 /// is an LLVM scalar type. 1002 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, 1003 QualType DstTy); 1004 1005 1006 /// EmitAggExpr - Emit the computation of the specified expression of 1007 /// aggregate type. The result is computed into DestPtr. Note that if 1008 /// DestPtr is null, the value of the aggregate expression is not needed. 1009 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest, 1010 bool IgnoreResult = false, bool IsInitializer = false, 1011 bool RequiresGCollection = false); 1012 1013 /// EmitGCMemmoveCollectable - Emit special API for structs with object 1014 /// pointers. 1015 void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr, 1016 QualType Ty); 1017 1018 /// EmitComplexExpr - Emit the computation of the specified expression of 1019 /// complex type, returning the result. 1020 ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal = false, 1021 bool IgnoreImag = false, 1022 bool IgnoreRealAssign = false, 1023 bool IgnoreImagAssign = false); 1024 1025 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression 1026 /// of complex type, storing into the specified Value*. 1027 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr, 1028 bool DestIsVolatile); 1029 1030 /// StoreComplexToAddr - Store a complex number into the specified address. 1031 void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr, 1032 bool DestIsVolatile); 1033 /// LoadComplexFromAddr - Load a complex number from the specified address. 1034 ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile); 1035 1036 /// CreateStaticBlockVarDecl - Create a zero-initialized LLVM global 1037 /// for a static block var decl. 1038 llvm::GlobalVariable * CreateStaticBlockVarDecl(const VarDecl &D, 1039 const char *Separator, 1040 llvm::GlobalValue::LinkageTypes 1041 Linkage); 1042 1043 /// EmitStaticCXXBlockVarDeclInit - Create the initializer for a C++ 1044 /// runtime initialized static block var decl. 1045 void EmitStaticCXXBlockVarDeclInit(const VarDecl &D, 1046 llvm::GlobalVariable *GV); 1047 1048 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++ 1049 /// variable with global storage. 1050 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr); 1051 1052 /// EmitCXXGlobalDtorRegistration - Emits a call to register the global ptr 1053 /// with the C++ runtime so that its destructor will be called at exit. 1054 void EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn, 1055 llvm::Constant *DeclPtr); 1056 1057 /// GenerateCXXGlobalInitFunc - Generates code for initializing global 1058 /// variables. 1059 void GenerateCXXGlobalInitFunc(llvm::Function *Fn, 1060 const VarDecl **Decls, 1061 unsigned NumDecls); 1062 1063 void EmitCXXConstructExpr(llvm::Value *Dest, const CXXConstructExpr *E); 1064 1065 RValue EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E, 1066 llvm::Value *AggLoc = 0, 1067 bool IsAggLocVolatile = false, 1068 bool IsInitializer = false); 1069 1070 void EmitCXXThrowExpr(const CXXThrowExpr *E); 1071 1072 //===--------------------------------------------------------------------===// 1073 // Internal Helpers 1074 //===--------------------------------------------------------------------===// 1075 1076 /// ContainsLabel - Return true if the statement contains a label in it. If 1077 /// this statement is not executed normally, it not containing a label means 1078 /// that we can just remove the code. 1079 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false); 1080 1081 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold 1082 /// to a constant, or if it does but contains a label, return 0. If it 1083 /// constant folds to 'true' and does not contain a label, return 1, if it 1084 /// constant folds to 'false' and does not contain a label, return -1. 1085 int ConstantFoldsToSimpleInteger(const Expr *Cond); 1086 1087 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an 1088 /// if statement) to the specified blocks. Based on the condition, this might 1089 /// try to simplify the codegen of the conditional based on the branch. 1090 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, 1091 llvm::BasicBlock *FalseBlock); 1092private: 1093 1094 void EmitReturnOfRValue(RValue RV, QualType Ty); 1095 1096 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty 1097 /// from function arguments into \arg Dst. See ABIArgInfo::Expand. 1098 /// 1099 /// \param AI - The first function argument of the expansion. 1100 /// \return The argument following the last expanded function 1101 /// argument. 1102 llvm::Function::arg_iterator 1103 ExpandTypeFromArgs(QualType Ty, LValue Dst, 1104 llvm::Function::arg_iterator AI); 1105 1106 /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg 1107 /// Ty, into individual arguments on the provided vector \arg Args. See 1108 /// ABIArgInfo::Expand. 1109 void ExpandTypeToArgs(QualType Ty, RValue Src, 1110 llvm::SmallVector<llvm::Value*, 16> &Args); 1111 1112 llvm::Value* EmitAsmInput(const AsmStmt &S, 1113 const TargetInfo::ConstraintInfo &Info, 1114 const Expr *InputExpr, std::string &ConstraintStr); 1115 1116 /// EmitCleanupBlock - emits a single cleanup block. 1117 void EmitCleanupBlock(); 1118 1119 /// AddBranchFixup - adds a branch instruction to the list of fixups for the 1120 /// current cleanup scope. 1121 void AddBranchFixup(llvm::BranchInst *BI); 1122 1123 /// EmitCallArg - Emit a single call argument. 1124 RValue EmitCallArg(const Expr *E, QualType ArgType); 1125 1126 /// EmitCallArgs - Emit call arguments for a function. 1127 /// The CallArgTypeInfo parameter is used for iterating over the known 1128 /// argument types of the function being called. 1129 template<typename T> 1130 void EmitCallArgs(CallArgList& Args, const T* CallArgTypeInfo, 1131 CallExpr::const_arg_iterator ArgBeg, 1132 CallExpr::const_arg_iterator ArgEnd) { 1133 CallExpr::const_arg_iterator Arg = ArgBeg; 1134 1135 // First, use the argument types that the type info knows about 1136 if (CallArgTypeInfo) { 1137 for (typename T::arg_type_iterator I = CallArgTypeInfo->arg_type_begin(), 1138 E = CallArgTypeInfo->arg_type_end(); I != E; ++I, ++Arg) { 1139 assert(Arg != ArgEnd && "Running over edge of argument list!"); 1140 QualType ArgType = *I; 1141 1142 assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). 1143 getTypePtr() == 1144 getContext().getCanonicalType(Arg->getType()).getTypePtr() && 1145 "type mismatch in call argument!"); 1146 1147 Args.push_back(std::make_pair(EmitCallArg(*Arg, ArgType), 1148 ArgType)); 1149 } 1150 1151 // Either we've emitted all the call args, or we have a call to a 1152 // variadic function. 1153 assert((Arg == ArgEnd || CallArgTypeInfo->isVariadic()) && 1154 "Extra arguments in non-variadic function!"); 1155 1156 } 1157 1158 // If we still have any arguments, emit them using the type of the argument. 1159 for (; Arg != ArgEnd; ++Arg) { 1160 QualType ArgType = Arg->getType(); 1161 Args.push_back(std::make_pair(EmitCallArg(*Arg, ArgType), 1162 ArgType)); 1163 } 1164 } 1165}; 1166 1167 1168} // end namespace CodeGen 1169} // end namespace clang 1170 1171#endif 1172