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