CodeGenFunction.h revision 7c086516f3cc9fba2733b1919973206c6ba4b171
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 "llvm/Support/IRBuilder.h" 21#include "clang/AST/Expr.h" 22#include "clang/AST/ExprObjC.h" 23 24#include <vector> 25#include <map> 26 27#include "CGCall.h" 28#include "CGValue.h" 29 30namespace llvm { 31 class BasicBlock; 32 class Module; 33} 34 35namespace clang { 36 class ASTContext; 37 class Decl; 38 class EnumConstantDecl; 39 class FunctionDecl; 40 class FunctionTypeProto; 41 class LabelStmt; 42 class ObjCMethodDecl; 43 class ObjCPropertyImplDecl; 44 class TargetInfo; 45 class VarDecl; 46 47namespace CodeGen { 48 class CodeGenModule; 49 class CodeGenTypes; 50 class CGRecordLayout; 51 52/// CodeGenFunction - This class organizes the per-function state that is used 53/// while generating LLVM code. 54class CodeGenFunction { 55public: 56 CodeGenModule &CGM; // Per-module state. 57 TargetInfo &Target; 58 59 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy; 60 llvm::IRBuilder<> Builder; 61 62 // Holds the Decl for the current function or method 63 const Decl *CurFuncDecl; 64 QualType FnRetTy; 65 llvm::Function *CurFn; 66 67 /// ReturnBlock - Unified return block. 68 llvm::BasicBlock *ReturnBlock; 69 /// ReturnValue - The temporary alloca to hold the return value. This 70 /// is null iff the function has no return value. 71 llvm::Instruction *ReturnValue; 72 73 /// AllocaInsertPoint - This is an instruction in the entry block before which 74 /// we prefer to insert allocas. 75 llvm::Instruction *AllocaInsertPt; 76 77 const llvm::Type *LLVMIntTy; 78 uint32_t LLVMPointerWidth; 79 80private: 81 /// LabelIDs - Track arbitrary ids assigned to labels for use in 82 /// implementing the GCC address-of-label extension and indirect 83 /// goto. IDs are assigned to labels inside getIDForAddrOfLabel(). 84 std::map<const LabelStmt*, unsigned> LabelIDs; 85 86 /// IndirectSwitches - Record the list of switches for indirect 87 /// gotos. Emission of the actual switching code needs to be delayed 88 /// until all AddrLabelExprs have been seen. 89 std::vector<llvm::SwitchInst*> IndirectSwitches; 90 91 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C 92 /// decls. 93 llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap; 94 95 /// LabelMap - This keeps track of the LLVM basic block for each C label. 96 llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap; 97 98 // BreakContinueStack - This keeps track of where break and continue 99 // statements should jump to. 100 struct BreakContinue { 101 BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb) 102 : BreakBlock(bb), ContinueBlock(cb) {} 103 104 llvm::BasicBlock *BreakBlock; 105 llvm::BasicBlock *ContinueBlock; 106 }; 107 llvm::SmallVector<BreakContinue, 8> BreakContinueStack; 108 109 /// SwitchInsn - This is nearest current switch instruction. It is null if 110 /// if current context is not in a switch. 111 llvm::SwitchInst *SwitchInsn; 112 113 /// CaseRangeBlock - This block holds if condition check for last case 114 /// statement range in current switch instruction. 115 llvm::BasicBlock *CaseRangeBlock; 116 117public: 118 CodeGenFunction(CodeGenModule &cgm); 119 120 ASTContext &getContext() const; 121 122 void GenerateObjCMethod(const ObjCMethodDecl *OMD); 123 124 void StartObjCMethod(const ObjCMethodDecl *MD); 125 126 /// GenerateObjCGetter - Synthesize an Objective-C property getter 127 /// function. 128 void GenerateObjCGetter(const ObjCPropertyImplDecl *PID); 129 130 /// GenerateObjCSetter - Synthesize an Objective-C property setter 131 /// function for the given property. 132 void GenerateObjCSetter(const ObjCPropertyImplDecl *PID); 133 134 void GenerateCode(const FunctionDecl *FD, 135 llvm::Function *Fn); 136 void StartFunction(const Decl *D, QualType RetTy, 137 llvm::Function *Fn, 138 const FunctionArgList &Args); 139 void FinishFunction(SourceLocation EndLoc=SourceLocation()); 140 141 const llvm::Type *ConvertType(QualType T); 142 143 /// LoadObjCSelf - Load the value of self. This function is only 144 /// valid while generating code for an Objective-C method. 145 llvm::Value *LoadObjCSelf(); 146 147 /// isObjCPointerType - Return true if the specificed AST type will map onto 148 /// some Objective-C pointer type. 149 static bool isObjCPointerType(QualType T); 150 151 /// hasAggregateLLVMType - Return true if the specified AST type will map into 152 /// an aggregate LLVM type or is void. 153 static bool hasAggregateLLVMType(QualType T); 154 155 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified 156 /// label maps to. 157 llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S); 158 159 160 void EmitBlock(llvm::BasicBlock *BB); 161 162 /// ErrorUnsupported - Print out an error that codegen doesn't support the 163 /// specified stmt yet. 164 void ErrorUnsupported(const Stmt *S, const char *Type, 165 bool OmitOnError=false); 166 167 //===--------------------------------------------------------------------===// 168 // Helpers 169 //===--------------------------------------------------------------------===// 170 171 /// CreateTempAlloca - This creates a alloca and inserts it into the entry 172 /// block. 173 llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty, 174 const char *Name = "tmp"); 175 176 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified 177 /// expression and compare the result against zero, returning an Int1Ty value. 178 llvm::Value *EvaluateExprAsBool(const Expr *E); 179 180 /// EmitAnyExpr - Emit code to compute the specified expression which can have 181 /// any type. The result is returned as an RValue struct. If this is an 182 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where 183 /// the result should be returned. 184 RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0, 185 bool isAggLocVolatile = false); 186 187 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result 188 /// will always be accessible even if no aggregate location is 189 /// provided. 190 RValue EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc = 0, 191 bool isAggLocVolatile = false); 192 193 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, 194 QualType EltTy); 195 196 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty); 197 198 /// isDummyBlock - Return true if BB is an empty basic block 199 /// with no predecessors. 200 static bool isDummyBlock(const llvm::BasicBlock *BB); 201 202 /// StartBlock - Start new block named N. If insert block is a dummy block 203 /// then reuse it. 204 void StartBlock(const char *N); 205 206 /// getCGRecordLayout - Return record layout info. 207 const CGRecordLayout *getCGRecordLayout(CodeGenTypes &CGT, QualType RTy); 208 209 /// GetAddrOfStaticLocalVar - Return the address of a static local variable. 210 llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD); 211 212 /// getAccessedFieldNo - Given an encoded value and a result number, return 213 /// the input field number being accessed. 214 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts); 215 216 unsigned GetIDForAddrOfLabel(const LabelStmt *L); 217 218 /// EmitMemSetToZero - Generate code to memset a value of the given type to 0; 219 void EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty); 220 221 //===--------------------------------------------------------------------===// 222 // Declaration Emission 223 //===--------------------------------------------------------------------===// 224 225 void EmitDecl(const Decl &D); 226 void EmitBlockVarDecl(const VarDecl &D); 227 void EmitLocalBlockVarDecl(const VarDecl &D); 228 void EmitStaticBlockVarDecl(const VarDecl &D); 229 230 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl. 231 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg); 232 233 //===--------------------------------------------------------------------===// 234 // Statement Emission 235 //===--------------------------------------------------------------------===// 236 237 void EmitStmt(const Stmt *S); 238 RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false, 239 llvm::Value *AggLoc = 0, bool isAggVol = false); 240 void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt. 241 void EmitLabelStmt(const LabelStmt &S); 242 void EmitGotoStmt(const GotoStmt &S); 243 void EmitIndirectGotoStmt(const IndirectGotoStmt &S); 244 void EmitIfStmt(const IfStmt &S); 245 void EmitWhileStmt(const WhileStmt &S); 246 void EmitDoStmt(const DoStmt &S); 247 void EmitForStmt(const ForStmt &S); 248 void EmitReturnStmt(const ReturnStmt &S); 249 void EmitDeclStmt(const DeclStmt &S); 250 void EmitBreakStmt(); 251 void EmitContinueStmt(); 252 void EmitSwitchStmt(const SwitchStmt &S); 253 void EmitDefaultStmt(const DefaultStmt &S); 254 void EmitCaseStmt(const CaseStmt &S); 255 void EmitCaseStmtRange(const CaseStmt &S); 256 void EmitAsmStmt(const AsmStmt &S); 257 258 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S); 259 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S); 260 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S); 261 262 //===--------------------------------------------------------------------===// 263 // LValue Expression Emission 264 //===--------------------------------------------------------------------===// 265 266 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E 267 /// and issue an ErrorUnsupported style diagnostic (using the 268 /// provided Name). 269 LValue EmitUnsupportedLValue(const Expr *E, 270 const char *Name); 271 272 /// EmitLValue - Emit code to compute a designator that specifies the location 273 /// of the expression. 274 /// 275 /// This can return one of two things: a simple address or a bitfield 276 /// reference. In either case, the LLVM Value* in the LValue structure is 277 /// guaranteed to be an LLVM pointer type. 278 /// 279 /// If this returns a bitfield reference, nothing about the pointee type of 280 /// the LLVM value is known: For example, it may not be a pointer to an 281 /// integer. 282 /// 283 /// If this returns a normal address, and if the lvalue's C type is fixed 284 /// size, this method guarantees that the returned pointer type will point to 285 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a 286 /// variable length type, this is not possible. 287 /// 288 LValue EmitLValue(const Expr *E); 289 290 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, 291 /// this method emits the address of the lvalue, then loads the result as an 292 /// rvalue, returning the rvalue. 293 RValue EmitLoadOfLValue(LValue V, QualType LVType); 294 RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType); 295 RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType); 296 RValue EmitLoadOfPropertyRefLValue(LValue LV, QualType ExprType); 297 298 299 /// EmitStoreThroughLValue - Store the specified rvalue into the specified 300 /// lvalue, where both are guaranteed to the have the same type, and that type 301 /// is 'Ty'. 302 void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty); 303 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst, 304 QualType Ty); 305 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty); 306 void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst, QualType Ty); 307 308 // Note: only availabe for agg return types 309 LValue EmitBinaryOperatorLValue(const BinaryOperator *E); 310 // Note: only availabe for agg return types 311 LValue EmitCallExprLValue(const CallExpr *E); 312 313 LValue EmitDeclRefLValue(const DeclRefExpr *E); 314 LValue EmitStringLiteralLValue(const StringLiteral *E); 315 LValue EmitPredefinedLValue(const PredefinedExpr *E); 316 LValue EmitUnaryOpLValue(const UnaryOperator *E); 317 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); 318 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); 319 LValue EmitMemberExpr(const MemberExpr *E); 320 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); 321 322 LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field, 323 bool isUnion, unsigned CVRQualifiers); 324 325 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E); 326 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E); 327 LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E); 328 329 //===--------------------------------------------------------------------===// 330 // Scalar Expression Emission 331 //===--------------------------------------------------------------------===// 332 333 /// EmitCall - Generate a call of the given function, expecting the 334 /// given result type, and using the given argument list which 335 /// specifies both the LLVM arguments and the types they were 336 /// derived from. 337 RValue EmitCall(llvm::Value *Callee, 338 QualType ResultType, 339 const CallArgList &Args); 340 341 RValue EmitCallExpr(const CallExpr *E); 342 343 RValue EmitCallExpr(Expr *FnExpr, CallExpr::const_arg_iterator ArgBeg, 344 CallExpr::const_arg_iterator ArgEnd); 345 346 RValue EmitCallExpr(llvm::Value *Callee, QualType FnType, 347 CallExpr::const_arg_iterator ArgBeg, 348 CallExpr::const_arg_iterator ArgEnd); 349 350 RValue EmitBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 351 352 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E); 353 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E); 354 355 llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...); 356 llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals, 357 bool isSplat = false); 358 359 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E); 360 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E); 361 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E); 362 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E); 363 RValue EmitObjCPropertyGet(const ObjCPropertyRefExpr *E); 364 void EmitObjCPropertySet(const ObjCPropertyRefExpr *E, RValue Src); 365 366 367 //===--------------------------------------------------------------------===// 368 // Expression Emission 369 //===--------------------------------------------------------------------===// 370 371 // Expressions are broken into three classes: scalar, complex, aggregate. 372 373 /// EmitScalarExpr - Emit the computation of the specified expression of 374 /// LLVM scalar type, returning the result. 375 llvm::Value *EmitScalarExpr(const Expr *E); 376 377 /// EmitScalarConversion - Emit a conversion from the specified type to the 378 /// specified destination type, both of which are LLVM scalar types. 379 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy, 380 QualType DstTy); 381 382 /// EmitComplexToScalarConversion - Emit a conversion from the specified 383 /// complex type to the specified destination type, where the destination 384 /// type is an LLVM scalar type. 385 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, 386 QualType DstTy); 387 388 389 /// EmitAggExpr - Emit the computation of the specified expression of 390 /// aggregate type. The result is computed into DestPtr. Note that if 391 /// DestPtr is null, the value of the aggregate expression is not needed. 392 void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest); 393 394 /// EmitComplexExpr - Emit the computation of the specified expression of 395 /// complex type, returning the result. 396 ComplexPairTy EmitComplexExpr(const Expr *E); 397 398 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression 399 /// of complex type, storing into the specified Value*. 400 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr, 401 bool DestIsVolatile); 402 403 /// StoreComplexToAddr - Store a complex number into the specified address. 404 void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr, 405 bool DestIsVolatile); 406 /// LoadComplexFromAddr - Load a complex number from the specified address. 407 ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile); 408 409 /// GenerateStaticBlockVarDecl - return the the static 410 /// declaration of local variable. 411 llvm::GlobalValue *GenerateStaticBlockVarDecl(const VarDecl &D, 412 bool NoInit, 413 const char *Separator); 414 415 // GenerateStaticBlockVarDecl - return the static declaration of 416 // a local variable. Performs initialization of the variable if necessary. 417 llvm::GlobalValue *GenerateStaticCXXBlockVarDecl(const VarDecl &D); 418 419 //===--------------------------------------------------------------------===// 420 // Internal Helpers 421 //===--------------------------------------------------------------------===// 422 423private: 424 /// EmitIndirectSwitches - Emit code for all of the switch 425 /// instructions in IndirectSwitches. 426 void EmitIndirectSwitches(); 427}; 428} // end namespace CodeGen 429} // end namespace clang 430 431#endif 432