1 //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// 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 coordinates the per-function state used while generating code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenFunction.h" 15 #include "CodeGenModule.h" 16 #include "CGDebugInfo.h" 17 #include "clang/Basic/TargetInfo.h" 18 #include "clang/AST/AST.h" 19 #include "llvm/CallingConv.h" 20 #include "llvm/Constants.h" 21 #include "llvm/DerivedTypes.h" 22 #include "llvm/Function.h" 23 #include "llvm/Analysis/Verifier.h" 24 #include "llvm/Support/CFG.h" 25 using namespace clang; 26 using namespace CodeGen; 27 28 CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) 29 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), 30 CaseRangeBlock(NULL) { 31 LLVMIntTy = ConvertType(getContext().IntTy); 32 LLVMPointerWidth = Target.getPointerWidth(0); 33 } 34 35 ASTContext &CodeGenFunction::getContext() const { 36 return CGM.getContext(); 37 } 38 39 40 llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { 41 llvm::BasicBlock *&BB = LabelMap[S]; 42 if (BB) return BB; 43 44 // Create, but don't insert, the new block. 45 return BB = llvm::BasicBlock::Create(S->getName()); 46 } 47 48 llvm::Constant * 49 CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { 50 return cast<llvm::Constant>(LocalDeclMap[BVD]); 51 } 52 53 const llvm::Type *CodeGenFunction::ConvertType(QualType T) { 54 return CGM.getTypes().ConvertType(T); 55 } 56 57 bool CodeGenFunction::isObjCPointerType(QualType T) { 58 // All Objective-C types are pointers. 59 return T->isObjCInterfaceType() || 60 T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType(); 61 } 62 63 bool CodeGenFunction::hasAggregateLLVMType(QualType T) { 64 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() && 65 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); 66 } 67 68 void CodeGenFunction::GenerateFunction(const Stmt *Body) { 69 // Emit the function body. 70 EmitStmt(Body); 71 72 // Emit debug descriptor for function end. 73 CGDebugInfo *DI = CGM.getDebugInfo(); 74 if (DI) { 75 const CompoundStmt* s = dyn_cast<CompoundStmt>(Body); 76 if (s && s->getRBracLoc().isValid()) { 77 DI->setLocation(s->getRBracLoc()); 78 } 79 DI->EmitRegionEnd(CurFn, Builder); 80 } 81 82 // Emit a return for code that falls off the end. If insert point 83 // is a dummy block with no predecessors then remove the block itself. 84 llvm::BasicBlock *BB = Builder.GetInsertBlock(); 85 if (isDummyBlock(BB)) 86 BB->eraseFromParent(); 87 else { 88 // FIXME: if this is C++ main, this should return 0. 89 if (CurFn->getReturnType() == llvm::Type::VoidTy) 90 Builder.CreateRetVoid(); 91 else 92 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); 93 } 94 assert(BreakContinueStack.empty() && 95 "mismatched push/pop in break/continue stack!"); 96 97 // Remove the AllocaInsertPt instruction, which is just a convenience for us. 98 AllocaInsertPt->eraseFromParent(); 99 AllocaInsertPt = 0; 100 101 // Verify that the function is well formed. 102 assert(!verifyFunction(*CurFn) && "Generated function is not well formed."); 103 } 104 105 void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { 106 CurFuncDecl = FD; 107 FnRetTy = FD->getResultType(); 108 CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true)); 109 assert(CurFn->isDeclaration() && "Function already has body?"); 110 111 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); 112 113 // Create a marker to make it easy to insert allocas into the entryblock 114 // later. Don't create this with the builder, because we don't want it 115 // folded. 116 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); 117 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", 118 EntryBB); 119 120 Builder.SetInsertPoint(EntryBB); 121 122 // Emit subprogram debug descriptor. 123 CGDebugInfo *DI = CGM.getDebugInfo(); 124 if (DI) { 125 CompoundStmt* body = dyn_cast<CompoundStmt>(FD->getBody()); 126 if (body && body->getLBracLoc().isValid()) { 127 DI->setLocation(body->getLBracLoc()); 128 } 129 DI->EmitFunctionStart(FD, CurFn, Builder); 130 } 131 132 // Emit allocs for param decls. Give the LLVM Argument nodes names. 133 llvm::Function::arg_iterator AI = CurFn->arg_begin(); 134 135 // Name the struct return argument. 136 if (hasAggregateLLVMType(FD->getResultType())) { 137 AI->setName("agg.result"); 138 ++AI; 139 } 140 141 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { 142 assert(AI != CurFn->arg_end() && "Argument mismatch!"); 143 EmitParmDecl(*FD->getParamDecl(i), AI); 144 } 145 GenerateFunction(FD->getBody()); 146 } 147 148 /// isDummyBlock - Return true if BB is an empty basic block 149 /// with no predecessors. 150 bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { 151 if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName()) 152 return true; 153 return false; 154 } 155 156 /// StartBlock - Start new block named N. If insert block is a dummy block 157 /// then reuse it. 158 void CodeGenFunction::StartBlock(const char *N) { 159 llvm::BasicBlock *BB = Builder.GetInsertBlock(); 160 if (!isDummyBlock(BB)) 161 EmitBlock(llvm::BasicBlock::Create(N)); 162 else 163 BB->setName(N); 164 } 165 166 /// getCGRecordLayout - Return record layout info. 167 const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, 168 QualType Ty) { 169 const RecordType *RTy = Ty->getAsRecordType(); 170 assert (RTy && "Unexpected type. RecordType expected here."); 171 172 return CGT.getCGRecordLayout(RTy->getDecl()); 173 } 174 175 /// WarnUnsupported - Print out a warning that codegen doesn't support the 176 /// specified stmt yet. 177 void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) { 178 CGM.WarnUnsupported(S, Type); 179 } 180 181