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