Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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" |
| 17 | #include "clang/AST/AST.h" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/Analysis/Verifier.h" |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CFG.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
| 27 | : CGM(cgm), Target(CGM.getContext().Target) {} |
| 28 | |
| 29 | ASTContext &CodeGenFunction::getContext() const { |
| 30 | return CGM.getContext(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 35 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 36 | if (BB) return BB; |
| 37 | |
| 38 | // Create, but don't insert, the new block. |
| 39 | return BB = new llvm::BasicBlock(S->getName()); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 44 | return CGM.getTypes().ConvertType(T); |
| 45 | } |
| 46 | |
| 47 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
| 48 | return !T->isRealType() && !T->isPointerType() && !T->isVoidType() && |
Chris Lattner | 5980204 | 2007-08-10 17:02:28 +0000 | [diff] [blame] | 49 | !T->isVectorType() && !T->isFunctionType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { |
| 54 | LLVMIntTy = ConvertType(getContext().IntTy); |
Chris Lattner | a96e0d8 | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 55 | LLVMPointerWidth = static_cast<unsigned>( |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 56 | getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy), |
Chris Lattner | a96e0d8 | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 57 | SourceLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 58 | |
| 59 | CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD)); |
| 60 | CurFuncDecl = FD; |
| 61 | |
| 62 | // TODO: Set up linkage and many other things. |
| 63 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 64 | |
| 65 | llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); |
| 66 | |
| 67 | Builder.SetInsertPoint(EntryBB); |
| 68 | |
| 69 | // Create a marker to make it easy to insert allocas into the entryblock |
| 70 | // later. |
| 71 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 72 | AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt"); |
| 73 | |
| 74 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 75 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 76 | |
| 77 | // Name the struct return argument. |
| 78 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 79 | AI->setName("agg.result"); |
| 80 | ++AI; |
| 81 | } |
| 82 | |
| 83 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 84 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 85 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 86 | } |
| 87 | |
| 88 | // Emit the function body. |
| 89 | EmitStmt(FD->getBody()); |
| 90 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 91 | // Emit a return for code that falls off the end. If insert point |
| 92 | // is a dummy block with no predecessors then remove the block itself. |
| 93 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 94 | if (isDummyBlock(BB)) |
| 95 | BB->eraseFromParent(); |
| 96 | else { |
| 97 | // FIXME: if this is C++ main, this should return 0. |
| 98 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 99 | Builder.CreateRetVoid(); |
| 100 | else |
| 101 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 102 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 103 | assert(BreakContinueStack.empty() && |
| 104 | "mismatched push/pop in break/continue stack!"); |
| 105 | |
| 106 | // Verify that the function is well formed. |
| 107 | assert(!verifyFunction(*CurFn)); |
| 108 | } |
| 109 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 110 | /// isDummyBlock - Return true if BB is an empty basic block |
| 111 | /// with no predecessors. |
| 112 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
| 113 | if (BB->empty() && pred_begin(BB) == pred_end(BB)) |
| 114 | return true; |
| 115 | return false; |
| 116 | } |
| 117 | |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 118 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 119 | /// then reuse it. |
| 120 | void CodeGenFunction::StartBlock(const char *N) { |
| 121 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 122 | if (!isDummyBlock(BB)) |
| 123 | EmitBlock(new llvm::BasicBlock(N)); |
| 124 | else |
| 125 | BB->setName(N); |
| 126 | } |
| 127 | |