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 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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) |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 27 | : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), |
| 28 | CaseRangeBlock(NULL) {} |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 29 | |
| 30 | ASTContext &CodeGenFunction::getContext() const { |
| 31 | return CGM.getContext(); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 36 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 37 | if (BB) return BB; |
| 38 | |
| 39 | // Create, but don't insert, the new block. |
| 40 | return BB = new llvm::BasicBlock(S->getName()); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 45 | return CGM.getTypes().ConvertType(T); |
| 46 | } |
| 47 | |
| 48 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 49 | return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() && |
| 50 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
| 54 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { |
| 55 | LLVMIntTy = ConvertType(getContext().IntTy); |
Chris Lattner | a96e0d8 | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 56 | LLVMPointerWidth = static_cast<unsigned>( |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 57 | getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy), |
Chris Lattner | a96e0d8 | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 58 | SourceLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 60 | CurFuncDecl = FD; |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 61 | CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 62 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 63 | |
Chris Lattner | 23d9e71 | 2007-11-27 06:46:51 +0000 | [diff] [blame] | 64 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 65 | // approximation of what we really want. |
| 66 | if (FD->getStorageClass() == FunctionDecl::Static) |
| 67 | CurFn->setLinkage(llvm::Function::InternalLinkage); |
| 68 | else if (FD->isInline()) |
| 69 | CurFn->setLinkage(llvm::Function::WeakLinkage); |
| 70 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 71 | llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); |
| 72 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 73 | // Create a marker to make it easy to insert allocas into the entryblock |
Chris Lattner | 40715f0 | 2007-12-17 20:50:59 +0000 | [diff] [blame] | 74 | // later. Don't create this with the builder, because we don't want it |
| 75 | // folded. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 76 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
Chris Lattner | 40715f0 | 2007-12-17 20:50:59 +0000 | [diff] [blame] | 77 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 78 | EntryBB); |
| 79 | |
| 80 | Builder.SetInsertPoint(EntryBB); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 81 | |
| 82 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 83 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 84 | |
| 85 | // Name the struct return argument. |
| 86 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 87 | AI->setName("agg.result"); |
| 88 | ++AI; |
| 89 | } |
| 90 | |
| 91 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 92 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 93 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 94 | } |
| 95 | |
| 96 | // Emit the function body. |
| 97 | EmitStmt(FD->getBody()); |
| 98 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 99 | // Emit a return for code that falls off the end. If insert point |
| 100 | // is a dummy block with no predecessors then remove the block itself. |
| 101 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 102 | if (isDummyBlock(BB)) |
| 103 | BB->eraseFromParent(); |
| 104 | else { |
| 105 | // FIXME: if this is C++ main, this should return 0. |
| 106 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 107 | Builder.CreateRetVoid(); |
| 108 | else |
| 109 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 110 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 111 | assert(BreakContinueStack.empty() && |
| 112 | "mismatched push/pop in break/continue stack!"); |
| 113 | |
Chris Lattner | ca92981 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 114 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 115 | AllocaInsertPt->eraseFromParent(); |
| 116 | AllocaInsertPt = 0; |
| 117 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 118 | // Verify that the function is well formed. |
| 119 | assert(!verifyFunction(*CurFn)); |
| 120 | } |
| 121 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 122 | /// isDummyBlock - Return true if BB is an empty basic block |
| 123 | /// with no predecessors. |
| 124 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
| 125 | if (BB->empty() && pred_begin(BB) == pred_end(BB)) |
| 126 | return true; |
| 127 | return false; |
| 128 | } |
| 129 | |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 130 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 131 | /// then reuse it. |
| 132 | void CodeGenFunction::StartBlock(const char *N) { |
| 133 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 134 | if (!isDummyBlock(BB)) |
| 135 | EmitBlock(new llvm::BasicBlock(N)); |
| 136 | else |
| 137 | BB->setName(N); |
| 138 | } |
| 139 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 140 | /// getCGRecordLayout - Return record layout info. |
| 141 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 142 | QualType Ty) { |
| 143 | const RecordType *RTy = Ty->getAsRecordType(); |
| 144 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 146 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 148 | |
| 149 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 150 | /// specified stmt yet. |
Chris Lattner | e8f4963 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 151 | void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) { |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 152 | CGM.WarnUnsupported(S, Type); |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 153 | } |
| 154 | |