Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/AST.h" |
| 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Analysis/Verifier.h" |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CFG.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 28 | : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), |
| 29 | CaseRangeBlock(NULL) {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | |
| 45 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 46 | return CGM.getTypes().ConvertType(T); |
| 47 | } |
| 48 | |
| 49 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 50 | return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() && |
| 51 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { |
| 56 | LLVMIntTy = ConvertType(getContext().IntTy); |
Chris Lattner | 47f7dbf | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 57 | LLVMPointerWidth = static_cast<unsigned>( |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 58 | getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy), |
Chris Lattner | 47f7dbf | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 59 | SourceLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | CurFuncDecl = FD; |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 62 | llvm::Constant *CurFnC = CGM.GetAddrOfGlobalDecl(FD); |
| 63 | if (!(CurFn = dyn_cast<llvm::Function>(CurFnC))) { |
| 64 | // If CurFnC is not a constant, it must be a bitcast of another function. |
| 65 | llvm::ConstantExpr *CurFnCE = cast<llvm::ConstantExpr>(CurFnC); |
| 66 | assert(CurFnCE->getOpcode() == llvm::Instruction::BitCast && |
| 67 | "Unexpected name collision"); |
| 68 | llvm::Function *OtherFunc = cast<llvm::Function>(CurFnCE->getOperand(0)); |
| 69 | |
| 70 | // This happens if there is a prototype for a function (e.g. "int f()") and |
| 71 | // then a definition of a different type (e.g. "int f(int x)"). Start by |
| 72 | // making a new function of the correct type, RAUW, then steal the name. |
| 73 | const llvm::PointerType *PTy = cast<llvm::PointerType>(CurFnC->getType()); |
| 74 | const llvm::FunctionType *FTy = |
| 75 | cast<llvm::FunctionType>(PTy->getElementType()); |
| 76 | CurFn = new llvm::Function(FTy, llvm::Function::ExternalLinkage, "", |
| 77 | &CGM.getModule()); |
| 78 | CurFn->takeName(OtherFunc); |
| 79 | |
| 80 | // Replace uses of OtherFunc with the Function we will endow with a body. |
| 81 | llvm::Constant *NewPtrForOldDecl = |
| 82 | llvm::ConstantExpr::getBitCast(CurFn, OtherFunc->getType()); |
| 83 | OtherFunc->replaceAllUsesWith(NewPtrForOldDecl); |
| 84 | |
| 85 | // Make sure the GlobalDecl map for FD is up-to-date. |
| 86 | CGM.ChangeGlobalDeclMap(FD, CurFn); |
| 87 | |
| 88 | // FIXME: Update the globaldeclmap for the previous decl of this name. We |
| 89 | // really want a way to walk all of these, but we don't have it yet. This |
| 90 | // is incredibly slow! |
| 91 | CGM.ReplaceMapValuesWith(OtherFunc, NewPtrForOldDecl); |
| 92 | |
| 93 | // Ok, delete the old function now, which is dead. |
| 94 | assert(OtherFunc->isDeclaration() && "Shouldn't replace non-declaration"); |
| 95 | OtherFunc->eraseFromParent(); |
| 96 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 99 | |
Chris Lattner | 5c6a42a | 2007-11-27 06:46:51 +0000 | [diff] [blame] | 100 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 101 | // approximation of what we really want. |
| 102 | if (FD->getStorageClass() == FunctionDecl::Static) |
| 103 | CurFn->setLinkage(llvm::Function::InternalLinkage); |
| 104 | else if (FD->isInline()) |
| 105 | CurFn->setLinkage(llvm::Function::WeakLinkage); |
| 106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); |
| 108 | |
| 109 | Builder.SetInsertPoint(EntryBB); |
| 110 | |
| 111 | // Create a marker to make it easy to insert allocas into the entryblock |
| 112 | // later. |
| 113 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 114 | AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt"); |
| 115 | |
| 116 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 117 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 118 | |
| 119 | // Name the struct return argument. |
| 120 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 121 | AI->setName("agg.result"); |
| 122 | ++AI; |
| 123 | } |
| 124 | |
| 125 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 126 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 127 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 128 | } |
| 129 | |
| 130 | // Emit the function body. |
| 131 | EmitStmt(FD->getBody()); |
| 132 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 133 | // Emit a return for code that falls off the end. If insert point |
| 134 | // is a dummy block with no predecessors then remove the block itself. |
| 135 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 136 | if (isDummyBlock(BB)) |
| 137 | BB->eraseFromParent(); |
| 138 | else { |
| 139 | // FIXME: if this is C++ main, this should return 0. |
| 140 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 141 | Builder.CreateRetVoid(); |
| 142 | else |
| 143 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 144 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 145 | assert(BreakContinueStack.empty() && |
| 146 | "mismatched push/pop in break/continue stack!"); |
| 147 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame^] | 148 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 149 | AllocaInsertPt->eraseFromParent(); |
| 150 | AllocaInsertPt = 0; |
| 151 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | // Verify that the function is well formed. |
| 153 | assert(!verifyFunction(*CurFn)); |
| 154 | } |
| 155 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 156 | /// isDummyBlock - Return true if BB is an empty basic block |
| 157 | /// with no predecessors. |
| 158 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
| 159 | if (BB->empty() && pred_begin(BB) == pred_end(BB)) |
| 160 | return true; |
| 161 | return false; |
| 162 | } |
| 163 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 164 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 165 | /// then reuse it. |
| 166 | void CodeGenFunction::StartBlock(const char *N) { |
| 167 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 168 | if (!isDummyBlock(BB)) |
| 169 | EmitBlock(new llvm::BasicBlock(N)); |
| 170 | else |
| 171 | BB->setName(N); |
| 172 | } |
| 173 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 174 | /// getCGRecordLayout - Return record layout info. |
| 175 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
| 176 | QualType RTy) { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 177 | assert (isa<RecordType>(RTy) |
| 178 | && "Unexpected type. RecordType expected here."); |
| 179 | |
| 180 | const llvm::Type *Ty = ConvertType(RTy); |
| 181 | assert (Ty && "Unable to find llvm::Type"); |
| 182 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 183 | return CGT.getCGRecordLayout(Ty); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 185 | |
| 186 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 187 | /// specified stmt yet. |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 188 | void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) { |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 189 | unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Warning, |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 190 | "cannot codegen this %0 yet"); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 191 | SourceRange Range = S->getSourceRange(); |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 192 | std::string Msg = Type; |
| 193 | CGM.getDiags().Report(S->getLocStart(), DiagID, &Msg, 1, &Range, 1); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 194 | } |
| 195 | |