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