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 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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" |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "clang/AST/AST.h" |
Nate Begeman | 440b456 | 2008-03-07 20:04:22 +0000 | [diff] [blame] | 19 | #include "llvm/CallingConv.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/Analysis/Verifier.h" |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CFG.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
| 28 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 29 | : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 30 | CaseRangeBlock(NULL) { |
| 31 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 32 | LLVMPointerWidth = Target.getPointerWidth(0); |
| 33 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | |
| 35 | ASTContext &CodeGenFunction::getContext() const { |
| 36 | return CGM.getContext(); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 41 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 42 | if (BB) return BB; |
| 43 | |
| 44 | // Create, but don't insert, the new block. |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 45 | return BB = llvm::BasicBlock::Create(S->getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 48 | llvm::Constant * |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 49 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 50 | return cast<llvm::Constant>(LocalDeclMap[BVD]); |
| 51 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | |
| 53 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 54 | return CGM.getTypes().ConvertType(T); |
| 55 | } |
| 56 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 57 | bool CodeGenFunction::isObjCPointerType(QualType T) { |
| 58 | // All Objective-C types are pointers. |
| 59 | return T->isObjCInterfaceType() || |
| 60 | T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType(); |
| 61 | } |
| 62 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 64 | return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() && |
| 65 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 68 | void CodeGenFunction::GenerateFunction(const Stmt *Body) { |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 69 | // Emit the function body. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 70 | EmitStmt(Body); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 71 | |
| 72 | // Finish emission of indirect switches. |
| 73 | EmitIndirectSwitches(); |
| 74 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 75 | // Emit debug descriptor for function end. |
| 76 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 77 | if (DI) { |
| 78 | const CompoundStmt* s = dyn_cast<CompoundStmt>(Body); |
| 79 | if (s && s->getRBracLoc().isValid()) { |
| 80 | DI->setLocation(s->getRBracLoc()); |
| 81 | } |
| 82 | DI->EmitRegionEnd(CurFn, Builder); |
| 83 | } |
| 84 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 85 | // Emit a return for code that falls off the end. If insert point |
| 86 | // is a dummy block with no predecessors then remove the block itself. |
| 87 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 88 | if (isDummyBlock(BB)) |
| 89 | BB->eraseFromParent(); |
| 90 | else { |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 91 | // FIXME: if this is C++ main, this should return 0. |
| 92 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 93 | Builder.CreateRetVoid(); |
| 94 | else |
| 95 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 96 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 97 | assert(BreakContinueStack.empty() && |
| 98 | "mismatched push/pop in break/continue stack!"); |
| 99 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 100 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 101 | AllocaInsertPt->eraseFromParent(); |
| 102 | AllocaInsertPt = 0; |
| 103 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | // Verify that the function is well formed. |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 105 | assert(!verifyFunction(*CurFn) && "Generated function is not well formed."); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 108 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD, |
| 109 | llvm::Function *Fn) { |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 110 | CurFuncDecl = FD; |
| 111 | FnRetTy = FD->getResultType(); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 112 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 113 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 114 | |
| 115 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
| 116 | |
| 117 | // Create a marker to make it easy to insert allocas into the entryblock |
| 118 | // later. Don't create this with the builder, because we don't want it |
| 119 | // folded. |
| 120 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 121 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 122 | EntryBB); |
| 123 | |
| 124 | Builder.SetInsertPoint(EntryBB); |
| 125 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 126 | // Emit subprogram debug descriptor. |
| 127 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 128 | if (DI) { |
| 129 | CompoundStmt* body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 130 | if (body && body->getLBracLoc().isValid()) { |
| 131 | DI->setLocation(body->getLBracLoc()); |
| 132 | } |
| 133 | DI->EmitFunctionStart(FD, CurFn, Builder); |
| 134 | } |
| 135 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 136 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 137 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 138 | |
| 139 | // Name the struct return argument. |
| 140 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 141 | AI->setName("agg.result"); |
| 142 | ++AI; |
| 143 | } |
| 144 | |
| 145 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 146 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 147 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 148 | } |
| 149 | GenerateFunction(FD->getBody()); |
| 150 | } |
| 151 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 152 | /// isDummyBlock - Return true if BB is an empty basic block |
| 153 | /// with no predecessors. |
| 154 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
Chris Lattner | 1e9660e | 2008-07-25 23:40:10 +0000 | [diff] [blame] | 155 | if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName()) |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 156 | return true; |
| 157 | return false; |
| 158 | } |
| 159 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 160 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 161 | /// then reuse it. |
| 162 | void CodeGenFunction::StartBlock(const char *N) { |
| 163 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 164 | if (!isDummyBlock(BB)) |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 165 | EmitBlock(llvm::BasicBlock::Create(N)); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 166 | else |
| 167 | BB->setName(N); |
| 168 | } |
| 169 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 170 | /// getCGRecordLayout - Return record layout info. |
| 171 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 172 | QualType Ty) { |
| 173 | const RecordType *RTy = Ty->getAsRecordType(); |
| 174 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 175 | |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 176 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 178 | |
| 179 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 180 | /// specified stmt yet. |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 181 | void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) { |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 182 | CGM.WarnUnsupported(S, Type); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame^] | 185 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 186 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 187 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 188 | } |
| 189 | |
| 190 | void CodeGenFunction::EmitIndirectSwitches() { |
| 191 | llvm::BasicBlock *Default; |
| 192 | |
| 193 | if (!LabelIDs.empty()) { |
| 194 | Default = getBasicBlockForLabel(LabelIDs.begin()->first); |
| 195 | } else { |
| 196 | // No possible targets for indirect goto, just emit an infinite |
| 197 | // loop. |
| 198 | Default = llvm::BasicBlock::Create("indirectgoto.loop", CurFn); |
| 199 | llvm::BranchInst::Create(Default, Default); |
| 200 | } |
| 201 | |
| 202 | for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), |
| 203 | e = IndirectSwitches.end(); i != e; ++i) { |
| 204 | llvm::SwitchInst *I = *i; |
| 205 | |
| 206 | I->setSuccessor(0, Default); |
| 207 | for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), |
| 208 | LE = LabelIDs.end(); LI != LE; ++LI) { |
| 209 | I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 210 | LI->second), |
| 211 | getBasicBlockForLabel(LI->first)); |
| 212 | } |
| 213 | } |
| 214 | } |