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" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Verifier.h" |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CFG.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 26 | : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 27 | CaseRangeBlock(NULL) { |
| 28 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 29 | LLVMPointerWidth = Target.getPointerWidth(0); |
| 30 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 42 | return BB = llvm::BasicBlock::Create(S->getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 45 | llvm::Constant * |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 46 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 47 | return cast<llvm::Constant>(LocalDeclMap[BVD]); |
| 48 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 50 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) |
| 51 | { |
| 52 | return LocalDeclMap[VD]; |
| 53 | } |
| 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 56 | return CGM.getTypes().ConvertType(T); |
| 57 | } |
| 58 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 59 | bool CodeGenFunction::isObjCPointerType(QualType T) { |
| 60 | // All Objective-C types are pointers. |
| 61 | return T->isObjCInterfaceType() || |
| 62 | T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType(); |
| 63 | } |
| 64 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 66 | return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() && |
| 67 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 70 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 71 | // Finish emission of indirect switches. |
| 72 | EmitIndirectSwitches(); |
| 73 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 74 | // Emit debug descriptor for function end. |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 75 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 76 | DI->setLocation(EndLoc); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 77 | DI->EmitRegionEnd(CurFn, Builder); |
| 78 | } |
| 79 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 80 | assert(BreakContinueStack.empty() && |
| 81 | "mismatched push/pop in break/continue stack!"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | b01d191 | 2008-09-27 07:15:59 +0000 | [diff] [blame] | 83 | // Emit function epilog (to return). This has the nice side effect |
| 84 | // of also automatically handling code that falls off the end. |
| 85 | EmitBlock(ReturnBlock); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 86 | EmitFunctionEpilog(FnRetTy, ReturnValue); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 88 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 89 | AllocaInsertPt->eraseFromParent(); |
| 90 | AllocaInsertPt = 0; |
| 91 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | // Verify that the function is well formed. |
Daniel Dunbar | b43f392 | 2008-09-17 21:13:22 +0000 | [diff] [blame] | 93 | if (verifyFunction(*CurFn, llvm::PrintMessageAction)) { |
| 94 | CurFn->dump(); |
| 95 | assert(0 && "Function failed verification!"); |
| 96 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 99 | void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy, |
| 100 | llvm::Function *Fn, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame^] | 101 | const FunctionArgList &Args, |
| 102 | SourceLocation StartLoc) { |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 103 | CurFuncDecl = D; |
| 104 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 105 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 106 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 107 | |
| 108 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 110 | // Create a marker to make it easy to insert allocas into the entryblock |
| 111 | // later. Don't create this with the builder, because we don't want it |
| 112 | // folded. |
| 113 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 114 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 115 | EntryBB); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | b01d191 | 2008-09-27 07:15:59 +0000 | [diff] [blame] | 117 | ReturnBlock = llvm::BasicBlock::Create("return"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 118 | ReturnValue = 0; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 119 | if (!RetTy->isVoidType()) |
| 120 | ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 122 | Builder.SetInsertPoint(EntryBB); |
| 123 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 124 | // Emit subprogram debug descriptor. |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 125 | // FIXME: The cast here is a huge hack. |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame^] | 126 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
| 127 | DI->setLocation(StartLoc); |
| 128 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 129 | DI->EmitFunctionStart(FD->getName(), RetTy, CurFn, Builder); |
| 130 | } else { |
| 131 | // Just use LLVM function name. |
| 132 | DI->EmitFunctionStart(Fn->getName().c_str(), |
| 133 | RetTy, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 134 | } |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 137 | EmitFunctionProlog(CurFn, FnRetTy, Args); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 138 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 139 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 140 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD, |
| 141 | llvm::Function *Fn) { |
| 142 | FunctionArgList Args; |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 143 | if (FD->getNumParams()) { |
| 144 | const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto(); |
| 145 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 146 | |
| 147 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
| 148 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
| 149 | FProto->getArgType(i))); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 150 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame^] | 152 | StartFunction(FD, FD->getResultType(), Fn, Args, |
| 153 | cast<CompoundStmt>(FD->getBody())->getLBracLoc()); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 154 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 155 | EmitStmt(FD->getBody()); |
| 156 | |
| 157 | const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()); |
| 158 | if (S) { |
| 159 | FinishFunction(S->getRBracLoc()); |
| 160 | } else { |
| 161 | FinishFunction(); |
| 162 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 165 | /// isDummyBlock - Return true if BB is an empty basic block |
| 166 | /// with no predecessors. |
| 167 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
Chris Lattner | 1e9660e | 2008-07-25 23:40:10 +0000 | [diff] [blame] | 168 | if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName()) |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 169 | return true; |
| 170 | return false; |
| 171 | } |
| 172 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 173 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 174 | /// then reuse it. |
| 175 | void CodeGenFunction::StartBlock(const char *N) { |
| 176 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 177 | if (!isDummyBlock(BB)) |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 178 | EmitBlock(llvm::BasicBlock::Create(N)); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 179 | else |
| 180 | BB->setName(N); |
| 181 | } |
| 182 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 183 | /// getCGRecordLayout - Return record layout info. |
| 184 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 185 | QualType Ty) { |
| 186 | const RecordType *RTy = Ty->getAsRecordType(); |
| 187 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 188 | |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 189 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 192 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 193 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 194 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 195 | bool OmitOnError) { |
| 196 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 199 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 200 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 201 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 202 | } |
| 203 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 204 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) |
| 205 | { |
| 206 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 207 | if (DestPtr->getType() != BP) |
| 208 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 209 | |
| 210 | // Get size and alignment info for this aggregate. |
| 211 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 212 | |
| 213 | // FIXME: Handle variable sized types. |
| 214 | const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); |
| 215 | |
| 216 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
| 217 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 218 | // TypeInfo.first describes size in bits. |
| 219 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 220 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 221 | TypeInfo.second/8)); |
| 222 | } |
| 223 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 224 | void CodeGenFunction::EmitIndirectSwitches() { |
| 225 | llvm::BasicBlock *Default; |
| 226 | |
Daniel Dunbar | 76526a5 | 2008-08-04 17:24:44 +0000 | [diff] [blame] | 227 | if (IndirectSwitches.empty()) |
| 228 | return; |
| 229 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 230 | if (!LabelIDs.empty()) { |
| 231 | Default = getBasicBlockForLabel(LabelIDs.begin()->first); |
| 232 | } else { |
| 233 | // No possible targets for indirect goto, just emit an infinite |
| 234 | // loop. |
| 235 | Default = llvm::BasicBlock::Create("indirectgoto.loop", CurFn); |
| 236 | llvm::BranchInst::Create(Default, Default); |
| 237 | } |
| 238 | |
| 239 | for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), |
| 240 | e = IndirectSwitches.end(); i != e; ++i) { |
| 241 | llvm::SwitchInst *I = *i; |
| 242 | |
| 243 | I->setSuccessor(0, Default); |
| 244 | for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), |
| 245 | LE = LabelIDs.end(); LI != LE; ++LI) { |
| 246 | I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 247 | LI->second), |
| 248 | getBasicBlockForLabel(LI->first)); |
| 249 | } |
| 250 | } |
| 251 | } |