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 | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame^] | 62 | CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 64 | |
Chris Lattner | 5c6a42a | 2007-11-27 06:46:51 +0000 | [diff] [blame] | 65 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 66 | // approximation of what we really want. |
| 67 | if (FD->getStorageClass() == FunctionDecl::Static) |
| 68 | CurFn->setLinkage(llvm::Function::InternalLinkage); |
| 69 | else if (FD->isInline()) |
| 70 | CurFn->setLinkage(llvm::Function::WeakLinkage); |
| 71 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); |
| 73 | |
| 74 | Builder.SetInsertPoint(EntryBB); |
| 75 | |
| 76 | // Create a marker to make it easy to insert allocas into the entryblock |
| 77 | // later. |
| 78 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 79 | AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt"); |
| 80 | |
| 81 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 82 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 83 | |
| 84 | // Name the struct return argument. |
| 85 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 86 | AI->setName("agg.result"); |
| 87 | ++AI; |
| 88 | } |
| 89 | |
| 90 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 91 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 92 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 93 | } |
| 94 | |
| 95 | // Emit the function body. |
| 96 | EmitStmt(FD->getBody()); |
| 97 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 98 | // Emit a return for code that falls off the end. If insert point |
| 99 | // is a dummy block with no predecessors then remove the block itself. |
| 100 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 101 | if (isDummyBlock(BB)) |
| 102 | BB->eraseFromParent(); |
| 103 | else { |
| 104 | // FIXME: if this is C++ main, this should return 0. |
| 105 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 106 | Builder.CreateRetVoid(); |
| 107 | else |
| 108 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 109 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 110 | assert(BreakContinueStack.empty() && |
| 111 | "mismatched push/pop in break/continue stack!"); |
| 112 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 113 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 114 | AllocaInsertPt->eraseFromParent(); |
| 115 | AllocaInsertPt = 0; |
| 116 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | // Verify that the function is well formed. |
| 118 | assert(!verifyFunction(*CurFn)); |
| 119 | } |
| 120 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 121 | /// isDummyBlock - Return true if BB is an empty basic block |
| 122 | /// with no predecessors. |
| 123 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
| 124 | if (BB->empty() && pred_begin(BB) == pred_end(BB)) |
| 125 | return true; |
| 126 | return false; |
| 127 | } |
| 128 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 129 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 130 | /// then reuse it. |
| 131 | void CodeGenFunction::StartBlock(const char *N) { |
| 132 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 133 | if (!isDummyBlock(BB)) |
| 134 | EmitBlock(new llvm::BasicBlock(N)); |
| 135 | else |
| 136 | BB->setName(N); |
| 137 | } |
| 138 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 139 | /// getCGRecordLayout - Return record layout info. |
| 140 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
| 141 | QualType RTy) { |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 142 | assert (isa<RecordType>(RTy) |
| 143 | && "Unexpected type. RecordType expected here."); |
| 144 | |
| 145 | const llvm::Type *Ty = ConvertType(RTy); |
| 146 | assert (Ty && "Unable to find llvm::Type"); |
| 147 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 148 | return CGT.getCGRecordLayout(Ty); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 150 | |
| 151 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 152 | /// specified stmt yet. |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 153 | void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) { |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 154 | unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Warning, |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 155 | "cannot codegen this %0 yet"); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 156 | SourceRange Range = S->getSourceRange(); |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 157 | std::string Msg = Type; |
| 158 | CGM.getDiags().Report(S->getLocStart(), DiagID, &Msg, 1, &Range, 1); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 159 | } |
| 160 | |