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) |
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 | |
| 60 | CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD)); |
| 61 | CurFuncDecl = FD; |
| 62 | |
| 63 | // TODO: Set up linkage and many other things. |
| 64 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 65 | |
| 66 | llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); |
| 67 | |
| 68 | Builder.SetInsertPoint(EntryBB); |
| 69 | |
| 70 | // Create a marker to make it easy to insert allocas into the entryblock |
| 71 | // later. |
| 72 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 73 | AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt"); |
| 74 | |
| 75 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 76 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 77 | |
| 78 | // Name the struct return argument. |
| 79 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 80 | AI->setName("agg.result"); |
| 81 | ++AI; |
| 82 | } |
| 83 | |
| 84 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 85 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 86 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 87 | } |
| 88 | |
| 89 | // Emit the function body. |
| 90 | EmitStmt(FD->getBody()); |
| 91 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 92 | // Emit a return for code that falls off the end. If insert point |
| 93 | // is a dummy block with no predecessors then remove the block itself. |
| 94 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 95 | if (isDummyBlock(BB)) |
| 96 | BB->eraseFromParent(); |
| 97 | else { |
| 98 | // FIXME: if this is C++ main, this should return 0. |
| 99 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 100 | Builder.CreateRetVoid(); |
| 101 | else |
| 102 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 103 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 104 | assert(BreakContinueStack.empty() && |
| 105 | "mismatched push/pop in break/continue stack!"); |
| 106 | |
| 107 | // Verify that the function is well formed. |
| 108 | assert(!verifyFunction(*CurFn)); |
| 109 | } |
| 110 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 111 | /// isDummyBlock - Return true if BB is an empty basic block |
| 112 | /// with no predecessors. |
| 113 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
| 114 | if (BB->empty() && pred_begin(BB) == pred_end(BB)) |
| 115 | return true; |
| 116 | return false; |
| 117 | } |
| 118 | |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 119 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 120 | /// then reuse it. |
| 121 | void CodeGenFunction::StartBlock(const char *N) { |
| 122 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 123 | if (!isDummyBlock(BB)) |
| 124 | EmitBlock(new llvm::BasicBlock(N)); |
| 125 | else |
| 126 | BB->setName(N); |
| 127 | } |
| 128 | |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 129 | /// getRecordLayoutInfo - Return record layout info. |
| 130 | RecordLayoutInfo *CodeGenFunction::getRecordLayoutInfo(CodeGenTypes &CGT, |
| 131 | QualType RTy) { |
| 132 | assert (isa<RecordType>(RTy) |
| 133 | && "Unexpected type. RecordType expected here."); |
| 134 | |
| 135 | const llvm::Type *Ty = ConvertType(RTy); |
| 136 | assert (Ty && "Unable to find llvm::Type"); |
| 137 | |
| 138 | return CGT.getRecordLayoutInfo(Ty); |
| 139 | } |