CodeGenFunction.h revision b14095aa98c6fedd3625920c4ce834bcaf24d9f7
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 "CGBlocks.h" 26#include "CGBuilder.h" 27#include "CGCall.h" 28#include "CGCXX.h" 29#include "CGValue.h" 30 31namespace llvm { 32 class BasicBlock; 33 class Module; 34 class SwitchInst; 35 class Value; 36} 37 38namespace clang { 39 class ASTContext; 40 class Decl; 41 class EnumConstantDecl; 42 class FunctionDecl; 43 class FunctionProtoType; 44 class LabelStmt; 45 class ObjCContainerDecl; 46 class ObjCInterfaceDecl; 47 class ObjCIvarDecl; 48 class ObjCMethodDecl; 49 class ObjCImplementationDecl; 50 class ObjCPropertyImplDecl; 51 class TargetInfo; 52 class VarDecl; 53 54namespace CodeGen { 55 class CodeGenModule; 56 class CodeGenTypes; 57 class CGDebugInfo; 58 class CGFunctionInfo; 59 class CGRecordLayout; 60 61/// CodeGenFunction - This class organizes the per-function state that is used 62/// while generating LLVM code. 63class CodeGenFunction : public BlockFunction { 64 CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT 65 void operator=(const CodeGenFunction&); // DO NOT IMPLEMENT 66public: 67 CodeGenModule &CGM; // Per-module state. 68 TargetInfo &Target; 69 70 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; 71 CGBuilderTy Builder; 72 73 /// CurFuncDecl - Holds the Decl for the current function or method. This 74 /// excludes BlockDecls. 75 const Decl *CurFuncDecl; 76 const CGFunctionInfo *CurFnInfo; 77 QualType FnRetTy; 78 llvm::Function *CurFn; 79 80 /// ReturnBlock - Unified return block. 81 llvm::BasicBlock *ReturnBlock; 82 /// ReturnValue - The temporary alloca to hold the return value. This is null 83 /// iff the function has no return value. 84 llvm::Instruction *ReturnValue; 85 86 /// AllocaInsertPoint - This is an instruction in the entry block before which 87 /// we prefer to insert allocas. 88 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt; 89 90 const llvm::Type *LLVMIntTy; 91 uint32_t LLVMPointerWidth; 92 93public: 94 /// ObjCEHValueStack - Stack of Objective-C exception values, used for 95 /// rethrows. 96 llvm::SmallVector<llvm::Value*, 8> ObjCEHValueStack; 97 98 /// PushCleanupBlock - Push a new cleanup entry on the stack and set the 99 /// passed in block as the cleanup block. 100 void PushCleanupBlock(llvm::BasicBlock *CleanupBlock); 101 102 /// CleanupBlockInfo - A struct representing a popped cleanup block. 103 struct CleanupBlockInfo { 104 /// CleanupBlock - the cleanup block 105 llvm::BasicBlock *CleanupBlock; 106 107 /// SwitchBlock - the block (if any) containing the switch instruction used 108 /// for jumping to the final destination. 109 llvm::BasicBlock *SwitchBlock; 110 111 /// EndBlock - the default destination for the switch instruction. 112 llvm::BasicBlock *EndBlock; 113 114 CleanupBlockInfo(llvm::BasicBlock *cb, llvm::BasicBlock *sb, 115 llvm::BasicBlock *eb) 116 : CleanupBlock(cb), SwitchBlock(sb), EndBlock(eb) {} 117 }; 118 119 /// PopCleanupBlock - Will pop the cleanup entry on the stack, process all 120 /// branch fixups and return a block info struct with the switch block and end 121 /// block. 122 CleanupBlockInfo PopCleanupBlock(); 123 124 /// CleanupScope - RAII object that will create a cleanup block and set the 125 /// insert point to that block. When destructed, it sets the insert point to 126 /// the previous block and pushes a new cleanup entry on the stack. 127 class CleanupScope { 128 CodeGenFunction& CGF; 129 llvm::BasicBlock *CurBB; 130 llvm::BasicBlock *CleanupBB; 131 132 public: 133 CleanupScope(CodeGenFunction &cgf) 134 : CGF(cgf), CurBB(CGF.Builder.GetInsertBlock()) { 135 CleanupBB = CGF.createBasicBlock("cleanup"); 136 CGF.Builder.SetInsertPoint(CleanupBB); 137 } 138 139 ~CleanupScope() { 140 CGF.PushCleanupBlock(CleanupBB); 141 CGF.Builder.SetInsertPoint(CurBB); 142 } 143 }; 144 145 /// EmitCleanupBlocks - Takes the old cleanup stack size and emits the cleanup 146 /// blocks that have been added. 147 void EmitCleanupBlocks(size_t OldCleanupStackSize); 148 149 /// EmitBranchThroughCleanup - Emit a branch from the current insert block 150 /// through the cleanup handling code (if any) and then on to \arg Dest. 151 /// 152 /// FIXME: Maybe this should really be in EmitBranch? Don't we always want 153 /// this behavior for branches? 154 void EmitBranchThroughCleanup(llvm::BasicBlock *Dest); 155 156private: 157 CGDebugInfo* DebugInfo; 158 159 /// LabelIDs - Track arbitrary ids assigned to labels for use in implementing 160 /// the GCC address-of-label extension and indirect goto. IDs are assigned to 161 /// labels inside getIDForAddrOfLabel(). 162 std::map<const LabelStmt*, unsigned> LabelIDs; 163 164 /// IndirectSwitches - Record the list of switches for indirect 165 /// gotos. Emission of the actual switching code needs to be delayed until all 166 /// AddrLabelExprs have been seen. 167 std::vector<llvm::SwitchInst*> IndirectSwitches; 168 169 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C 170 /// decls. 171 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 172 173 /// LabelMap - This keeps track of the LLVM basic block for each C label. 174 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap; 175 176 // BreakContinueStack - This keeps track of where break and continue 177 // statements should jump to. 178 struct BreakContinue { 179 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb) 180 : BreakBlock(bb), ContinueBlock(cb) {} 181 182 llvm::BasicBlock *BreakBlock; 183 llvm::BasicBlock *ContinueBlock; 184 }; 185 llvm::SmallVector<BreakContinue, 8> BreakContinueStack; 186 187 /// SwitchInsn - This is nearest current switch instruction. It is null if if 188 /// current context is not in a switch. 189 llvm::SwitchInst *SwitchInsn; 190 191 /// CaseRangeBlock - This block holds if condition check for last case 192 /// statement range in current switch instruction. 193 llvm::BasicBlock *CaseRangeBlock; 194 195 /// InvokeDest - This is the nearest exception target for calls 196 /// which can unwind, when exceptions are being used. 197 llvm::BasicBlock *InvokeDest; 198 199 // VLASizeMap - This keeps track of the associated size for each VLA type. 200 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we 201 // enter/leave scopes. 202 llvm::DenseMap<const VariableArrayType*, llvm::Value*> VLASizeMap; 203 204 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid 205 /// calling llvm.stacksave for multiple VLAs in the same scope. 206 bool DidCallStackSave; 207 208 struct CleanupEntry { 209 /// CleanupBlock - The block of code that does the actual cleanup. 210 llvm::BasicBlock *CleanupBlock; 211 212 /// Blocks - Basic blocks that were emitted in the current cleanup scope. 213 std::vector<llvm::BasicBlock *> Blocks; 214 215 /// BranchFixups - Branch instructions to basic blocks that haven't been 216 /// inserted into the current function yet. 217 std::vector<llvm::BranchInst *> BranchFixups; 218 219 explicit CleanupEntry(llvm::BasicBlock *cb) 220 : CleanupBlock(cb) {} 221 }; 222 223 /// CleanupEntries - Stack of cleanup entries. 224 llvm::SmallVector<CleanupEntry, 8> CleanupEntries; 225 226 typedef llvm::DenseMap<llvm::BasicBlock*, size_t> BlockScopeMap; 227 228 /// BlockScopes - Map of which "cleanup scope" scope basic blocks have. 229 BlockScopeMap BlockScopes; 230 231 /// CXXThisDecl - When parsing an C++ function, this will hold the implicit 232 /// 'this' declaration. 233 ImplicitParamDecl *CXXThisDecl; 234 235public: 236 CodeGenFunction(CodeGenModule &cgm); 237 238 ASTContext &getContext() const; 239 CGDebugInfo *getDebugInfo() { return DebugInfo; } 240 241 llvm::BasicBlock *getInvokeDest() { return InvokeDest; } 242 void setInvokeDest(llvm::BasicBlock *B) { InvokeDest = B; } 243 244 //===--------------------------------------------------------------------===// 245 // Objective-C 246 //===--------------------------------------------------------------------===// 247 248 void GenerateObjCMethod(const ObjCMethodDecl *OMD); 249 250 void StartObjCMethod(const ObjCMethodDecl *MD, 251 const ObjCContainerDecl *CD); 252 253 /// GenerateObjCGetter - Synthesize an Objective-C property getter function. 254 void GenerateObjCGetter(ObjCImplementationDecl *IMP, 255 const ObjCPropertyImplDecl *PID); 256 257 /// GenerateObjCSetter - Synthesize an Objective-C property setter function 258 /// for the given property. 259 void GenerateObjCSetter(ObjCImplementationDecl *IMP, 260 const ObjCPropertyImplDecl *PID); 261 262 //===--------------------------------------------------------------------===// 263 // Block Bits 264 //===--------------------------------------------------------------------===// 265 266 llvm::Value *BuildBlockLiteralTmp(const BlockExpr *); 267 llvm::Constant *BuildDescriptorBlockDecl(bool BlockHasCopyDispose, 268 uint64_t Size, 269 const llvm::StructType *, 270 std::vector<HelperInfo> *); 271 272 llvm::Function *GenerateBlockFunction(const BlockExpr *BExpr, 273 const BlockInfo& Info, 274 const Decl *OuterFuncDecl, 275 llvm::DenseMap<const Decl*, llvm::Value*> ldm, 276 uint64_t &Size, uint64_t &Align, 277 llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls, 278 bool &subBlockHasCopyDispose); 279 280 void BlockForwardSelf(); 281 llvm::Value *LoadBlockStruct(); 282 283 llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E); 284 285 const llvm::Type *BuildByRefType(QualType Ty, uint64_t Align); 286 287 void GenerateCode(const FunctionDecl *FD, 288 llvm::Function *Fn); 289 void StartFunction(const Decl *D, QualType RetTy, 290 llvm::Function *Fn, 291 const FunctionArgList &Args, 292 SourceLocation StartLoc); 293 294 /// EmitReturnBlock - Emit the unified return block, trying to avoid its 295 /// emission when possible. 296 void EmitReturnBlock(); 297 298 /// FinishFunction - Complete IR generation of the current function. It is 299 /// legal to call this function even if there is no current insertion point. 300 void FinishFunction(SourceLocation EndLoc=SourceLocation()); 301 302 /// EmitFunctionProlog - Emit the target specific LLVM code to load the 303 /// arguments for the given function. This is also responsible for naming the 304 /// LLVM function arguments. 305 void EmitFunctionProlog(const CGFunctionInfo &FI, 306 llvm::Function *Fn, 307 const FunctionArgList &Args); 308 309 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the 310 /// given temporary. 311 void EmitFunctionEpilog(const CGFunctionInfo &FI, llvm::Value *ReturnValue); 312 313 const llvm::Type *ConvertTypeForMem(QualType T); 314 const llvm::Type *ConvertType(QualType T); 315 316 /// LoadObjCSelf - Load the value of self. This function is only valid while 317 /// generating code for an Objective-C method. 318 llvm::Value *LoadObjCSelf(); 319 320 /// TypeOfSelfObject - Return type of object that this self represents. 321 QualType TypeOfSelfObject(); 322 323 /// hasAggregateLLVMType - Return true if the specified AST type will map into 324 /// an aggregate LLVM type or is void. 325 static bool hasAggregateLLVMType(QualType T); 326 327 /// createBasicBlock - Create an LLVM basic block. 328 llvm::BasicBlock *createBasicBlock(const char *Name="", 329 llvm::Function *Parent=0, 330 llvm::BasicBlock *InsertBefore=0) { 331#ifdef NDEBUG 332 return llvm::BasicBlock::Create("", Parent, InsertBefore); 333#else 334 return llvm::BasicBlock::Create(Name, Parent, InsertBefore); 335#endif 336 } 337 338 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified 339 /// label maps to. 340 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S); 341 342 /// SimplifyForwardingBlocks - If the given basic block is only a 343 /// branch to another basic block, simplify it. This assumes that no 344 /// other code could potentially reference the basic block. 345 void SimplifyForwardingBlocks(llvm::BasicBlock *BB); 346 347 /// EmitBlock - Emit the given block \arg BB and set it as the insert point, 348 /// adding a fall-through branch from the current insert block if 349 /// necessary. It is legal to call this function even if there is no current 350 /// insertion point. 351 /// 352 /// IsFinished - If true, indicates that the caller has finished emitting 353 /// branches to the given block and does not expect to emit code into it. This 354 /// means the block can be ignored if it is unreachable. 355 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false); 356 357 /// EmitBranch - Emit a branch to the specified basic block from the current 358 /// insert block, taking care to avoid creation of branches from dummy 359 /// blocks. It is legal to call this function even if there is no current 360 /// insertion point. 361 /// 362 /// This function clears the current insertion point. The caller should follow 363 /// calls to this function with calls to Emit*Block prior to generation new 364 /// code. 365 void EmitBranch(llvm::BasicBlock *Block); 366 367 /// HaveInsertPoint - True if an insertion point is defined. If not, this 368 /// indicates that the current code being emitted is unreachable. 369 bool HaveInsertPoint() const { 370 return Builder.GetInsertBlock() != 0; 371 } 372 373 /// EnsureInsertPoint - Ensure that an insertion point is defined so that 374 /// emitted IR has a place to go. Note that by definition, if this function 375 /// creates a block then that block is unreachable; callers may do better to 376 /// detect when no insertion point is defined and simply skip IR generation. 377 void EnsureInsertPoint() { 378 if (!HaveInsertPoint()) 379 EmitBlock(createBasicBlock()); 380 } 381 382 /// ErrorUnsupported - Print out an error that codegen doesn't support the 383 /// specified stmt yet. 384 void ErrorUnsupported(const Stmt *S, const char *Type, 385 bool OmitOnError=false); 386 387 //===--------------------------------------------------------------------===// 388 // Helpers 389 //===--------------------------------------------------------------------===// 390 391 /// CreateTempAlloca - This creates a alloca and inserts it into the entry 392 /// block. 393 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty, 394 const char *Name = "tmp"); 395 396 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified 397 /// expression and compare the result against zero, returning an Int1Ty value. 398 llvm::Value *EvaluateExprAsBool(const Expr *E); 399 400 /// EmitAnyExpr - Emit code to compute the specified expression which can have 401 /// any type. The result is returned as an RValue struct. If this is an 402 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where 403 /// the result should be returned. 404 RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0, 405 bool isAggLocVolatile = false); 406 407 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address 408 // or the value of the expression, depending on how va_list is defined. 409 llvm::Value *EmitVAListRef(const Expr *E); 410 411 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will 412 /// always be accessible even if no aggregate location is provided. 413 RValue EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc = 0, 414 bool isAggLocVolatile = false); 415 416 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, 417 QualType EltTy); 418 419 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty); 420 421 /// StartBlock - Start new block named N. If insert block is a dummy block 422 /// then reuse it. 423 void StartBlock(const char *N); 424 425 /// getCGRecordLayout - Return record layout info. 426 const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy); 427 428 /// GetAddrOfStaticLocalVar - Return the address of a static local variable. 429 llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD); 430 431 /// GetAddrOfLocalVar - Return the address of a local variable. 432 llvm::Value *GetAddrOfLocalVar(const VarDecl *VD); 433 434 /// getAccessedFieldNo - Given an encoded value and a result number, return 435 /// the input field number being accessed. 436 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); 437 438 unsigned GetIDForAddrOfLabel(const LabelStmt *L); 439 440 /// EmitMemSetToZero - Generate code to memset a value of the given type to 0. 441 void EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty); 442 443 // EmitVAArg - Generate code to get an argument from the passed in pointer 444 // and update it accordingly. The return value is a pointer to the argument. 445 // FIXME: We should be able to get rid of this method and use the va_arg 446 // instruction in LLVM instead once it works well enough. 447 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty); 448 449 // EmitVLASize - Generate code for any VLA size expressions that might occur 450 // in a variably modified type. If Ty is a VLA, will return the value that 451 // corresponds to the size in bytes of the VLA type. Will return 0 otherwise. 452 llvm::Value *EmitVLASize(QualType Ty); 453 454 // GetVLASize - Returns an LLVM value that corresponds to the size in bytes 455 // of a variable length array type. 456 llvm::Value *GetVLASize(const VariableArrayType *); 457 458 /// LoadCXXThis - Load the value of 'this'. This function is only valid while 459 /// generating code for an C++ member function. 460 llvm::Value *LoadCXXThis(); 461 462 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, 463 llvm::Value *This, 464 CallExpr::const_arg_iterator ArgBeg, 465 CallExpr::const_arg_iterator ArgEnd); 466 467 //===--------------------------------------------------------------------===// 468 // Declaration Emission 469 //===--------------------------------------------------------------------===// 470 471 void EmitDecl(const Decl &D); 472 void EmitBlockVarDecl(const VarDecl &D); 473 void EmitLocalBlockVarDecl(const VarDecl &D); 474 void EmitStaticBlockVarDecl(const VarDecl &D); 475 476 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl. 477 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg); 478 479 //===--------------------------------------------------------------------===// 480 // Statement Emission 481 //===--------------------------------------------------------------------===// 482 483 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info. 484 void EmitStopPoint(const Stmt *S); 485 486 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call 487 /// this function even if there is no current insertion point. 488 /// 489 /// This function may clear the current insertion point; callers should use 490 /// EnsureInsertPoint if they wish to subsequently generate code without first 491 /// calling EmitBlock, EmitBranch, or EmitStmt. 492 void EmitStmt(const Stmt *S); 493 494 /// EmitSimpleStmt - Try to emit a "simple" statement which does not 495 /// necessarily require an insertion point or debug information; typically 496 /// because the statement amounts to a jump or a container of other 497 /// statements. 498 /// 499 /// \return True if the statement was handled. 500 bool EmitSimpleStmt(const Stmt *S); 501 502 RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, 503 llvm::Value *AggLoc = 0, bool isAggVol = false); 504 505 /// EmitLabel - Emit the block for the given label. It is legal to call this 506 /// function even if there is no current insertion point. 507 void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt. 508 509 void EmitLabelStmt(const LabelStmt &S); 510 void EmitGotoStmt(const GotoStmt &S); 511 void EmitIndirectGotoStmt(const IndirectGotoStmt &S); 512 void EmitIfStmt(const IfStmt &S); 513 void EmitWhileStmt(const WhileStmt &S); 514 void EmitDoStmt(const DoStmt &S); 515 void EmitForStmt(const ForStmt &S); 516 void EmitReturnStmt(const ReturnStmt &S); 517 void EmitDeclStmt(const DeclStmt &S); 518 void EmitBreakStmt(const BreakStmt &S); 519 void EmitContinueStmt(const ContinueStmt &S); 520 void EmitSwitchStmt(const SwitchStmt &S); 521 void EmitDefaultStmt(const DefaultStmt &S); 522 void EmitCaseStmt(const CaseStmt &S); 523 void EmitCaseStmtRange(const CaseStmt &S); 524 void EmitAsmStmt(const AsmStmt &S); 525 526 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S); 527 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S); 528 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S); 529 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S); 530 531 //===--------------------------------------------------------------------===// 532 // LValue Expression Emission 533 //===--------------------------------------------------------------------===// 534 535 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type. 536 RValue GetUndefRValue(QualType Ty); 537 538 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E 539 /// and issue an ErrorUnsupported style diagnostic (using the 540 /// provided Name). 541 RValue EmitUnsupportedRValue(const Expr *E, 542 const char *Name); 543 544 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue 545 /// an ErrorUnsupported style diagnostic (using the provided Name). 546 LValue EmitUnsupportedLValue(const Expr *E, 547 const char *Name); 548 549 /// EmitLValue - Emit code to compute a designator that specifies the location 550 /// of the expression. 551 /// 552 /// This can return one of two things: a simple address or a bitfield 553 /// reference. In either case, the LLVM Value* in the LValue structure is 554 /// guaranteed to be an LLVM pointer type. 555 /// 556 /// If this returns a bitfield reference, nothing about the pointee type of 557 /// the LLVM value is known: For example, it may not be a pointer to an 558 /// integer. 559 /// 560 /// If this returns a normal address, and if the lvalue's C type is fixed 561 /// size, this method guarantees that the returned pointer type will point to 562 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a 563 /// variable length type, this is not possible. 564 /// 565 LValue EmitLValue(const Expr *E); 566 567 /// EmitLoadOfScalar - Load a scalar value from an address, taking 568 /// care to appropriately convert from the memory representation to 569 /// the LLVM value representation. 570 llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, 571 QualType Ty); 572 573 /// EmitStoreOfScalar - Store a scalar value to an address, taking 574 /// care to appropriately convert from the memory representation to 575 /// the LLVM value representation. 576 void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, 577 bool Volatile); 578 579 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, 580 /// this method emits the address of the lvalue, then loads the result as an 581 /// rvalue, returning the rvalue. 582 RValue EmitLoadOfLValue(LValue V, QualType LVType); 583 RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType); 584 RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType); 585 RValue EmitLoadOfPropertyRefLValue(LValue LV, QualType ExprType); 586 RValue EmitLoadOfKVCRefLValue(LValue LV, QualType ExprType); 587 588 589 /// EmitStoreThroughLValue - Store the specified rvalue into the specified 590 /// lvalue, where both are guaranteed to the have the same type, and that type 591 /// is 'Ty'. 592 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty); 593 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst, 594 QualType Ty); 595 void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst, QualType Ty); 596 void EmitStoreThroughKVCRefLValue(RValue Src, LValue Dst, QualType Ty); 597 598 /// EmitStoreThroughLValue - Store Src into Dst with same constraints as 599 /// EmitStoreThroughLValue. 600 /// 601 /// \param Result [out] - If non-null, this will be set to a Value* for the 602 /// bit-field contents after the store, appropriate for use as the result of 603 /// an assignment to the bit-field. 604 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty, 605 llvm::Value **Result=0); 606 607 // Note: only availabe for agg return types 608 LValue EmitBinaryOperatorLValue(const BinaryOperator *E); 609 // Note: only available for agg return types 610 LValue EmitCallExprLValue(const CallExpr *E); 611 // Note: only available for agg return types 612 LValue EmitVAArgExprLValue(const VAArgExpr *E); 613 LValue EmitDeclRefLValue(const DeclRefExpr *E); 614 LValue EmitStringLiteralLValue(const StringLiteral *E); 615 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E); 616 LValue EmitPredefinedFunctionName(unsigned Type); 617 LValue EmitPredefinedLValue(const PredefinedExpr *E); 618 LValue EmitUnaryOpLValue(const UnaryOperator *E); 619 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); 620 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); 621 LValue EmitMemberExpr(const MemberExpr *E); 622 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); 623 LValue EmitConditionalOperator(const ConditionalOperator *E); 624 LValue EmitCastLValue(const CastExpr *E); 625 626 llvm::Value *EmitIvarOffset(ObjCInterfaceDecl *Interface, 627 const ObjCIvarDecl *Ivar); 628 LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field, 629 bool isUnion, unsigned CVRQualifiers); 630 LValue EmitLValueForIvar(QualType ObjectTy, 631 llvm::Value* Base, const ObjCIvarDecl *Ivar, 632 const FieldDecl *Field, 633 unsigned CVRQualifiers); 634 635 LValue EmitLValueForBitfield(llvm::Value* Base, FieldDecl* Field, 636 unsigned CVRQualifiers); 637 638 LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E); 639 640 LValue EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E); 641 LValue EmitCXXTemporaryObjectExprLValue(const CXXTemporaryObjectExpr *E); 642 643 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); 644 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); 645 LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E); 646 LValue EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E); 647 LValue EmitObjCSuperExpr(const ObjCSuperExpr *E); 648 649 //===--------------------------------------------------------------------===// 650 // Scalar Expression Emission 651 //===--------------------------------------------------------------------===// 652 653 /// EmitCall - Generate a call of the given function, expecting the given 654 /// result type, and using the given argument list which specifies both the 655 /// LLVM arguments and the types they were derived from. 656 /// 657 /// \param TargetDecl - If given, the decl of the function in a 658 /// direct call; used to set attributes on the call (noreturn, 659 /// etc.). 660 RValue EmitCall(const CGFunctionInfo &FnInfo, 661 llvm::Value *Callee, 662 const CallArgList &Args, 663 const Decl *TargetDecl = 0); 664 665 RValue EmitCallExpr(const CallExpr *E); 666 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E); 667 668 RValue EmitCallExpr(llvm::Value *Callee, QualType FnType, 669 CallExpr::const_arg_iterator ArgBeg, 670 CallExpr::const_arg_iterator ArgEnd, 671 const Decl *TargetDecl = 0); 672 673 RValue EmitBuiltinExpr(const FunctionDecl *FD, 674 unsigned BuiltinID, const CallExpr *E); 675 676 RValue EmitBlockCallExpr(const CallExpr *E); 677 678 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call 679 /// is unhandled by the current target. 680 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 681 682 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); 683 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 684 685 llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...); 686 llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals, 687 bool isSplat = false); 688 689 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); 690 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); 691 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); 692 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E); 693 RValue EmitObjCPropertyGet(const Expr *E); 694 RValue EmitObjCSuperPropertyGet(const Expr *Exp, const Selector &S); 695 void EmitObjCPropertySet(const Expr *E, RValue Src); 696 void EmitObjCSuperPropertySet(const Expr *E, const Selector &S, RValue Src); 697 698 699 //===--------------------------------------------------------------------===// 700 // Expression Emission 701 //===--------------------------------------------------------------------===// 702 703 // Expressions are broken into three classes: scalar, complex, aggregate. 704 705 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM 706 /// scalar type, returning the result. 707 llvm::Value *EmitScalarExpr(const Expr *E); 708 709 /// EmitScalarConversion - Emit a conversion from the specified type to the 710 /// specified destination type, both of which are LLVM scalar types. 711 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, 712 QualType DstTy); 713 714 /// EmitComplexToScalarConversion - Emit a conversion from the specified 715 /// complex type to the specified destination type, where the destination type 716 /// is an LLVM scalar type. 717 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, 718 QualType DstTy); 719 720 721 /// EmitAggExpr - Emit the computation of the specified expression of 722 /// aggregate type. The result is computed into DestPtr. Note that if 723 /// DestPtr is null, the value of the aggregate expression is not needed. 724 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest); 725 726 /// EmitComplexExpr - Emit the computation of the specified expression of 727 /// complex type, returning the result. 728 ComplexPairTy EmitComplexExpr(const Expr *E); 729 730 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression 731 /// of complex type, storing into the specified Value*. 732 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr, 733 bool DestIsVolatile); 734 735 /// StoreComplexToAddr - Store a complex number into the specified address. 736 void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr, 737 bool DestIsVolatile); 738 /// LoadComplexFromAddr - Load a complex number from the specified address. 739 ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile); 740 741 /// CreateStaticBlockVarDecl - Create a zero-initialized LLVM global 742 /// for a static block var decl. 743 llvm::GlobalVariable * CreateStaticBlockVarDecl(const VarDecl &D, 744 const char *Separator, 745 llvm::GlobalValue::LinkageTypes 746 Linkage); 747 748 /// GenerateStaticCXXBlockVarDecl - Create the initializer for a C++ 749 /// runtime initialized static block var decl. 750 void GenerateStaticCXXBlockVarDeclInit(const VarDecl &D, 751 llvm::GlobalVariable *GV); 752 753 void EmitCXXTemporaryObjectExpr(llvm::Value *Dest, 754 const CXXTemporaryObjectExpr *E); 755 756 //===--------------------------------------------------------------------===// 757 // Internal Helpers 758 //===--------------------------------------------------------------------===// 759 760 /// ContainsLabel - Return true if the statement contains a label in it. If 761 /// this statement is not executed normally, it not containing a label means 762 /// that we can just remove the code. 763 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false); 764 765 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold 766 /// to a constant, or if it does but contains a label, return 0. If it 767 /// constant folds to 'true' and does not contain a label, return 1, if it 768 /// constant folds to 'false' and does not contain a label, return -1. 769 int ConstantFoldsToSimpleInteger(const Expr *Cond); 770 771 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an 772 /// if statement) to the specified blocks. Based on the condition, this might 773 /// try to simplify the codegen of the conditional based on the branch. 774 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, 775 llvm::BasicBlock *FalseBlock); 776private: 777 778 /// EmitIndirectSwitches - Emit code for all of the switch 779 /// instructions in IndirectSwitches. 780 void EmitIndirectSwitches(); 781 782 void EmitReturnOfRValue(RValue RV, QualType Ty); 783 784 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty 785 /// from function arguments into \arg Dst. See ABIArgInfo::Expand. 786 /// 787 /// \param AI - The first function argument of the expansion. 788 /// \return The argument following the last expanded function 789 /// argument. 790 llvm::Function::arg_iterator 791 ExpandTypeFromArgs(QualType Ty, LValue Dst, 792 llvm::Function::arg_iterator AI); 793 794 /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg 795 /// Ty, into individual arguments on the provided vector \arg Args. See 796 /// ABIArgInfo::Expand. 797 void ExpandTypeToArgs(QualType Ty, RValue Src, 798 llvm::SmallVector<llvm::Value*, 16> &Args); 799 800 llvm::Value* EmitAsmInput(const AsmStmt &S, TargetInfo::ConstraintInfo Info, 801 const Expr *InputExpr, std::string &ConstraintStr); 802 803 /// EmitCleanupBlock - emits a single cleanup block. 804 void EmitCleanupBlock(); 805 806 /// AddBranchFixup - adds a branch instruction to the list of fixups for the 807 /// current cleanup scope. 808 void AddBranchFixup(llvm::BranchInst *BI); 809 810 /// EmitCallArg - Emit a single call argument. 811 RValue EmitCallArg(const Expr *E, QualType ArgType); 812 813 /// EmitCallArgs - Emit call arguments for a function. 814 /// FIXME: It should be possible to generalize this and pass a generic 815 /// "argument type container" type instead of the FunctionProtoType. This way 816 /// it can work on Objective-C methods as well. 817 void EmitCallArgs(CallArgList& args, const FunctionProtoType *FPT, 818 CallExpr::const_arg_iterator ArgBeg, 819 CallExpr::const_arg_iterator ArgEnd); 820 821}; 822} // end namespace CodeGen 823} // end namespace clang 824 825#endif 826