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" |
| 16 | #include "clang/Basic/TargetInfo.h" |
| 17 | #include "clang/AST/AST.h" |
Nate Begeman | 440b456 | 2008-03-07 20:04:22 +0000 | [diff] [blame] | 18 | #include "llvm/CallingConv.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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), |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 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 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 44 | llvm::Constant * |
| 45 | CodeGenFunction::GetAddrOfStaticLocalVar(const BlockVarDecl *BVD) { |
| 46 | return cast<llvm::Constant>(LocalDeclMap[BVD]); |
| 47 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | |
| 49 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 50 | return CGM.getTypes().ConvertType(T); |
| 51 | } |
| 52 | |
| 53 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Chris Lattner | 423a3c9 | 2008-04-02 17:45:06 +0000 | [diff] [blame^] | 54 | return !T->isRealType() && !T->isPointerLikeType() && |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 55 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 58 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 59 | /// its pointer, name, and types registered in the class struture. |
| 60 | // FIXME: This method contains a lot of code copied and pasted from |
| 61 | // GenerateCode. This should be factored out. |
| 62 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
| 63 | llvm::SmallVector<const llvm::Type *, 16> ParamTypes; |
| 64 | for (unsigned i=0 ; i<OMD->param_size() ; i++) { |
| 65 | ParamTypes.push_back(ConvertType(OMD->getParamDecl(i)->getType())); |
| 66 | } |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 67 | CurFn =CGM.getObjCRuntime()->MethodPreamble(ConvertType(OMD->getResultType()), |
| 68 | llvm::PointerType::getUnqual(llvm::Type::Int32Ty), |
| 69 | ParamTypes.begin(), |
| 70 | OMD->param_size(), |
| 71 | OMD->isVariadic()); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 72 | llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); |
| 73 | |
| 74 | // Create a marker to make it easy to insert allocas into the entryblock |
| 75 | // later. Don't create this with the builder, because we don't want it |
| 76 | // folded. |
| 77 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 78 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 79 | EntryBB); |
| 80 | |
| 81 | FnRetTy = OMD->getResultType(); |
| 82 | |
| 83 | Builder.SetInsertPoint(EntryBB); |
| 84 | |
| 85 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 86 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 87 | |
| 88 | // Name the struct return argument. |
| 89 | // FIXME: Probably should be in the runtime, or it will trample the other |
| 90 | // hidden arguments. |
| 91 | if (hasAggregateLLVMType(OMD->getResultType())) { |
| 92 | AI->setName("agg.result"); |
| 93 | ++AI; |
| 94 | } |
| 95 | |
| 96 | // Add implicit parameters to the decl map. |
| 97 | // TODO: Add something to AST to let the runtime specify the names and types |
| 98 | // of these. |
| 99 | llvm::Value *&DMEntry = LocalDeclMap[&(*OMD->getSelfDecl())]; |
| 100 | DMEntry = AI; |
| 101 | ++AI; ++AI; |
| 102 | |
| 103 | |
| 104 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 105 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 106 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 107 | } |
| 108 | |
| 109 | // Emit the function body. |
| 110 | EmitStmt(OMD->getBody()); |
| 111 | |
| 112 | // Emit a return for code that falls off the end. If insert point |
| 113 | // is a dummy block with no predecessors then remove the block itself. |
| 114 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 115 | if (isDummyBlock(BB)) |
| 116 | BB->eraseFromParent(); |
| 117 | else { |
| 118 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 119 | Builder.CreateRetVoid(); |
| 120 | else |
| 121 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 122 | } |
| 123 | assert(BreakContinueStack.empty() && |
| 124 | "mismatched push/pop in break/continue stack!"); |
| 125 | |
| 126 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 127 | AllocaInsertPt->eraseFromParent(); |
| 128 | AllocaInsertPt = 0; |
| 129 | // Verify that the function is well formed. |
| 130 | assert(!verifyFunction(*CurFn) && "Generated method is not well formed."); |
| 131 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | |
| 133 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { |
| 134 | LLVMIntTy = ConvertType(getContext().IntTy); |
Chris Lattner | 47f7dbf | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 135 | LLVMPointerWidth = static_cast<unsigned>( |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 136 | getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | CurFuncDecl = FD; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 139 | FnRetTy = CurFuncDecl->getType()->getAsFunctionType()->getResultType(); |
| 140 | |
| 141 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 142 | CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 144 | |
Chris Lattner | 5c6a42a | 2007-11-27 06:46:51 +0000 | [diff] [blame] | 145 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 146 | // approximation of what we really want. |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 147 | if (FD->getAttr<DLLImportAttr>()) |
| 148 | CurFn->setLinkage(llvm::Function::DLLImportLinkage); |
| 149 | else if (FD->getAttr<DLLExportAttr>()) |
| 150 | CurFn->setLinkage(llvm::Function::DLLExportLinkage); |
| 151 | else if (FD->getAttr<WeakAttr>() || FD->isInline()) |
Chris Lattner | 5c6a42a | 2007-11-27 06:46:51 +0000 | [diff] [blame] | 152 | CurFn->setLinkage(llvm::Function::WeakLinkage); |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 153 | else if (FD->getStorageClass() == FunctionDecl::Static) |
| 154 | CurFn->setLinkage(llvm::Function::InternalLinkage); |
| 155 | |
Nate Begeman | 440b456 | 2008-03-07 20:04:22 +0000 | [diff] [blame] | 156 | if (FD->getAttr<FastCallAttr>()) |
| 157 | CurFn->setCallingConv(llvm::CallingConv::Fast); |
| 158 | |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 159 | if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>()) |
| 160 | CurFn->setVisibility(attr->getVisibility()); |
| 161 | // FIXME: else handle -fvisibility |
| 162 | |
| 163 | |
Chris Lattner | f89e88d | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 164 | unsigned FuncAttrs = 0; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 165 | if (FD->getAttr<NoThrowAttr>()) |
Chris Lattner | f89e88d | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 166 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 167 | if (FD->getAttr<NoReturnAttr>()) |
Chris Lattner | f89e88d | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 168 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 169 | |
| 170 | if (FuncAttrs) { |
Chris Lattner | e0e0c94 | 2008-03-12 17:46:07 +0000 | [diff] [blame] | 171 | llvm::ParamAttrsWithIndex PAWI = |
| 172 | llvm::ParamAttrsWithIndex::get(0, FuncAttrs); |
| 173 | CurFn->setParamAttrs(llvm::PAListPtr::get(&PAWI, 1)); |
Chris Lattner | f89e88d | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | ddee423 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn); |
| 177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | // Create a marker to make it easy to insert allocas into the entryblock |
Chris Lattner | 55352a2 | 2007-12-17 20:50:59 +0000 | [diff] [blame] | 179 | // later. Don't create this with the builder, because we don't want it |
| 180 | // folded. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
Chris Lattner | 55352a2 | 2007-12-17 20:50:59 +0000 | [diff] [blame] | 182 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 183 | EntryBB); |
| 184 | |
| 185 | Builder.SetInsertPoint(EntryBB); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | |
| 187 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 188 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 189 | |
| 190 | // Name the struct return argument. |
| 191 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 192 | AI->setName("agg.result"); |
| 193 | ++AI; |
| 194 | } |
| 195 | |
| 196 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 197 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 198 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 199 | } |
| 200 | |
| 201 | // Emit the function body. |
| 202 | EmitStmt(FD->getBody()); |
| 203 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 204 | // Emit a return for code that falls off the end. If insert point |
| 205 | // is a dummy block with no predecessors then remove the block itself. |
| 206 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 207 | if (isDummyBlock(BB)) |
| 208 | BB->eraseFromParent(); |
| 209 | else { |
| 210 | // FIXME: if this is C++ main, this should return 0. |
| 211 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 212 | Builder.CreateRetVoid(); |
| 213 | else |
| 214 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 215 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 216 | assert(BreakContinueStack.empty() && |
| 217 | "mismatched push/pop in break/continue stack!"); |
| 218 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 219 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 220 | AllocaInsertPt->eraseFromParent(); |
| 221 | AllocaInsertPt = 0; |
| 222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | // Verify that the function is well formed. |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 224 | assert(!verifyFunction(*CurFn) && "Generated function is not well formed."); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 227 | /// isDummyBlock - Return true if BB is an empty basic block |
| 228 | /// with no predecessors. |
| 229 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
| 230 | if (BB->empty() && pred_begin(BB) == pred_end(BB)) |
| 231 | return true; |
| 232 | return false; |
| 233 | } |
| 234 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 235 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 236 | /// then reuse it. |
| 237 | void CodeGenFunction::StartBlock(const char *N) { |
| 238 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 239 | if (!isDummyBlock(BB)) |
| 240 | EmitBlock(new llvm::BasicBlock(N)); |
| 241 | else |
| 242 | BB->setName(N); |
| 243 | } |
| 244 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 245 | /// getCGRecordLayout - Return record layout info. |
| 246 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 247 | QualType Ty) { |
| 248 | const RecordType *RTy = Ty->getAsRecordType(); |
| 249 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 250 | |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 251 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 253 | |
| 254 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 255 | /// specified stmt yet. |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 256 | void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) { |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 257 | CGM.WarnUnsupported(S, Type); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 258 | } |
| 259 | |