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