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