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