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" |
Eli Friedman | 9bc7c8d | 2008-05-22 01:40:10 +0000 | [diff] [blame^] | 16 | #include "CGDebugInfo.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "clang/AST/AST.h" |
Nate Begeman | d75d28b | 2008-03-07 20:04:22 +0000 | [diff] [blame] | 19 | #include "llvm/CallingConv.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CFG.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
| 28 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 29 | : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), |
Chris Lattner | c61e9f8 | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 30 | CaseRangeBlock(NULL) {} |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | |
| 32 | ASTContext &CodeGenFunction::getContext() const { |
| 33 | return CGM.getContext(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 38 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 39 | if (BB) return BB; |
| 40 | |
| 41 | // Create, but don't insert, the new block. |
Gabor Greif | 815e2c1 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 42 | return BB = llvm::BasicBlock::Create(S->getName()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Lauro Ramos Venancio | 934fb02 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 45 | llvm::Constant * |
Steve Naroff | 72a6ebc | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 46 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
Lauro Ramos Venancio | 934fb02 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 47 | return cast<llvm::Constant>(LocalDeclMap[BVD]); |
| 48 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | |
| 50 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 51 | return CGM.getTypes().ConvertType(T); |
| 52 | } |
| 53 | |
| 54 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Chris Lattner | a05f7d2 | 2008-04-02 17:45:06 +0000 | [diff] [blame] | 55 | return !T->isRealType() && !T->isPointerLikeType() && |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 56 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 59 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 60 | /// its pointer, name, and types registered in the class struture. |
| 61 | // FIXME: This method contains a lot of code copied and pasted from |
| 62 | // GenerateCode. This should be factored out. |
| 63 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
| 64 | llvm::SmallVector<const llvm::Type *, 16> ParamTypes; |
| 65 | for (unsigned i=0 ; i<OMD->param_size() ; i++) { |
| 66 | ParamTypes.push_back(ConvertType(OMD->getParamDecl(i)->getType())); |
| 67 | } |
Chris Lattner | c61e9f8 | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 68 | CurFn =CGM.getObjCRuntime()->MethodPreamble(ConvertType(OMD->getResultType()), |
| 69 | llvm::PointerType::getUnqual(llvm::Type::Int32Ty), |
| 70 | ParamTypes.begin(), |
| 71 | OMD->param_size(), |
| 72 | OMD->isVariadic()); |
Gabor Greif | 815e2c1 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 73 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 74 | |
| 75 | // Create a marker to make it easy to insert allocas into the entryblock |
| 76 | // later. Don't create this with the builder, because we don't want it |
| 77 | // folded. |
| 78 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 79 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 80 | EntryBB); |
| 81 | |
| 82 | FnRetTy = OMD->getResultType(); |
| 83 | |
| 84 | Builder.SetInsertPoint(EntryBB); |
| 85 | |
| 86 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 87 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 88 | |
| 89 | // Name the struct return argument. |
| 90 | // FIXME: Probably should be in the runtime, or it will trample the other |
| 91 | // hidden arguments. |
| 92 | if (hasAggregateLLVMType(OMD->getResultType())) { |
| 93 | AI->setName("agg.result"); |
| 94 | ++AI; |
| 95 | } |
| 96 | |
| 97 | // Add implicit parameters to the decl map. |
| 98 | // TODO: Add something to AST to let the runtime specify the names and types |
| 99 | // of these. |
| 100 | llvm::Value *&DMEntry = LocalDeclMap[&(*OMD->getSelfDecl())]; |
Chris Lattner | 6e6a597 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 101 | const llvm::Type *SelfTy = AI->getType(); |
| 102 | llvm::Value *DeclPtr = new llvm::AllocaInst(SelfTy, 0, "self.addr", |
| 103 | AllocaInsertPt); |
| 104 | |
| 105 | // Store the initial value into the alloca. |
| 106 | Builder.CreateStore(AI, DeclPtr); |
| 107 | DMEntry = DeclPtr; |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 108 | ++AI; ++AI; |
| 109 | |
| 110 | |
| 111 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 112 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 113 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 114 | } |
| 115 | |
| 116 | // Emit the function body. |
| 117 | EmitStmt(OMD->getBody()); |
| 118 | |
| 119 | // Emit a return for code that falls off the end. If insert point |
| 120 | // is a dummy block with no predecessors then remove the block itself. |
| 121 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 122 | if (isDummyBlock(BB)) |
| 123 | BB->eraseFromParent(); |
| 124 | else { |
| 125 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 126 | Builder.CreateRetVoid(); |
| 127 | else |
| 128 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 129 | } |
| 130 | assert(BreakContinueStack.empty() && |
| 131 | "mismatched push/pop in break/continue stack!"); |
| 132 | |
| 133 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 134 | AllocaInsertPt->eraseFromParent(); |
| 135 | AllocaInsertPt = 0; |
| 136 | // Verify that the function is well formed. |
| 137 | assert(!verifyFunction(*CurFn) && "Generated method is not well formed."); |
| 138 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 6e6a597 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 140 | llvm::Value *CodeGenFunction::LoadObjCSelf(void) |
| 141 | { |
| 142 | if(const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) { |
| 143 | llvm::Value *SelfPtr = LocalDeclMap[&(*OMD->getSelfDecl())]; |
| 144 | return Builder.CreateLoad(SelfPtr, "self"); |
| 145 | } |
| 146 | return NULL; |
| 147 | } |
| 148 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 149 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD) { |
| 150 | LLVMIntTy = ConvertType(getContext().IntTy); |
Chris Lattner | a96e0d8 | 2007-09-04 02:34:27 +0000 | [diff] [blame] | 151 | LLVMPointerWidth = static_cast<unsigned>( |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 152 | getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 154 | CurFuncDecl = FD; |
Chris Lattner | 6e6a597 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 155 | FnRetTy = FD->getType()->getAsFunctionType()->getResultType(); |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 1a3c1e2 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 157 | CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 158 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 159 | |
Chris Lattner | 23d9e71 | 2007-11-27 06:46:51 +0000 | [diff] [blame] | 160 | // TODO: Set up linkage and many other things. Note, this is a simple |
| 161 | // approximation of what we really want. |
Chris Lattner | 25094a4 | 2008-05-04 01:44:26 +0000 | [diff] [blame] | 162 | if (FD->getStorageClass() == FunctionDecl::Static) |
| 163 | CurFn->setLinkage(llvm::Function::InternalLinkage); |
| 164 | else if (FD->getAttr<DLLImportAttr>()) |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 165 | CurFn->setLinkage(llvm::Function::DLLImportLinkage); |
| 166 | else if (FD->getAttr<DLLExportAttr>()) |
| 167 | CurFn->setLinkage(llvm::Function::DLLExportLinkage); |
| 168 | else if (FD->getAttr<WeakAttr>() || FD->isInline()) |
Chris Lattner | 23d9e71 | 2007-11-27 06:46:51 +0000 | [diff] [blame] | 169 | CurFn->setLinkage(llvm::Function::WeakLinkage); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 170 | |
Nate Begeman | d75d28b | 2008-03-07 20:04:22 +0000 | [diff] [blame] | 171 | if (FD->getAttr<FastCallAttr>()) |
| 172 | CurFn->setCallingConv(llvm::CallingConv::Fast); |
| 173 | |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 174 | if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>()) |
Dan Gohman | 4751a3a | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 175 | CodeGenModule::setVisibility(CurFn, attr->getVisibility()); |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 176 | // FIXME: else handle -fvisibility |
| 177 | |
| 178 | |
Chris Lattner | 0b3357e | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 179 | unsigned FuncAttrs = 0; |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 180 | if (FD->getAttr<NoThrowAttr>()) |
Chris Lattner | 0b3357e | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 181 | FuncAttrs |= llvm::ParamAttr::NoUnwind; |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 182 | if (FD->getAttr<NoReturnAttr>()) |
Chris Lattner | 0b3357e | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 183 | FuncAttrs |= llvm::ParamAttr::NoReturn; |
| 184 | |
| 185 | if (FuncAttrs) { |
Chris Lattner | 8c83f08 | 2008-03-12 17:46:07 +0000 | [diff] [blame] | 186 | llvm::ParamAttrsWithIndex PAWI = |
| 187 | llvm::ParamAttrsWithIndex::get(0, FuncAttrs); |
| 188 | CurFn->setParamAttrs(llvm::PAListPtr::get(&PAWI, 1)); |
Chris Lattner | 0b3357e | 2008-03-03 03:45:26 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 402b337 | 2008-03-03 03:28:21 +0000 | [diff] [blame] | 190 | |
Gabor Greif | 815e2c1 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 191 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 193 | // 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] | 194 | // later. Don't create this with the builder, because we don't want it |
| 195 | // folded. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 196 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
Chris Lattner | 40715f0 | 2007-12-17 20:50:59 +0000 | [diff] [blame] | 197 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 198 | EntryBB); |
| 199 | |
| 200 | Builder.SetInsertPoint(EntryBB); |
Eli Friedman | 9bc7c8d | 2008-05-22 01:40:10 +0000 | [diff] [blame^] | 201 | |
| 202 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 203 | if (DI) { |
| 204 | CompoundStmt* body = cast<CompoundStmt>(CurFuncDecl->getBody()); |
| 205 | if (body->getLBracLoc().isValid()) { |
| 206 | DI->setLocation(body->getLBracLoc()); |
| 207 | } |
| 208 | DI->EmitFunctionStart(CurFn, Builder); |
| 209 | } |
| 210 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 211 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 212 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 213 | |
| 214 | // Name the struct return argument. |
| 215 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 216 | AI->setName("agg.result"); |
| 217 | ++AI; |
| 218 | } |
| 219 | |
| 220 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 221 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 222 | EmitParmDecl(*FD->getParamDecl(i), AI); |
| 223 | } |
| 224 | |
| 225 | // Emit the function body. |
| 226 | EmitStmt(FD->getBody()); |
| 227 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 228 | // Emit a return for code that falls off the end. If insert point |
| 229 | // is a dummy block with no predecessors then remove the block itself. |
| 230 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 231 | if (isDummyBlock(BB)) |
| 232 | BB->eraseFromParent(); |
| 233 | else { |
Eli Friedman | 9bc7c8d | 2008-05-22 01:40:10 +0000 | [diff] [blame^] | 234 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 235 | if (DI) { |
| 236 | CompoundStmt* body = cast<CompoundStmt>(CurFuncDecl->getBody()); |
| 237 | if (body->getRBracLoc().isValid()) { |
| 238 | DI->setLocation(body->getRBracLoc()); |
| 239 | } |
| 240 | DI->EmitFunctionEnd(CurFn, Builder); |
| 241 | } |
| 242 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 243 | // FIXME: if this is C++ main, this should return 0. |
| 244 | if (CurFn->getReturnType() == llvm::Type::VoidTy) |
| 245 | Builder.CreateRetVoid(); |
| 246 | else |
| 247 | Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType())); |
| 248 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 249 | assert(BreakContinueStack.empty() && |
| 250 | "mismatched push/pop in break/continue stack!"); |
| 251 | |
Chris Lattner | ca92981 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 252 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 253 | AllocaInsertPt->eraseFromParent(); |
| 254 | AllocaInsertPt = 0; |
| 255 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 256 | // Verify that the function is well formed. |
Chris Lattner | b326b17 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 257 | assert(!verifyFunction(*CurFn) && "Generated function is not well formed."); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 260 | /// isDummyBlock - Return true if BB is an empty basic block |
| 261 | /// with no predecessors. |
| 262 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
| 263 | if (BB->empty() && pred_begin(BB) == pred_end(BB)) |
| 264 | return true; |
| 265 | return false; |
| 266 | } |
| 267 | |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 268 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 269 | /// then reuse it. |
| 270 | void CodeGenFunction::StartBlock(const char *N) { |
| 271 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 272 | if (!isDummyBlock(BB)) |
Gabor Greif | 815e2c1 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 273 | EmitBlock(llvm::BasicBlock::Create(N)); |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 274 | else |
| 275 | BB->setName(N); |
| 276 | } |
| 277 | |
Devang Patel | 7a78e43 | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 278 | /// getCGRecordLayout - Return record layout info. |
| 279 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 280 | QualType Ty) { |
| 281 | const RecordType *RTy = Ty->getAsRecordType(); |
| 282 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 7b2543e | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 284 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | aebd83f | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 286 | |
| 287 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 288 | /// specified stmt yet. |
Chris Lattner | e8f4963 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 289 | void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) { |
Chris Lattner | cf9c9d0 | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 290 | CGM.WarnUnsupported(S, Type); |
Chris Lattner | 9d4e620 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 291 | } |
| 292 | |