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 | |
| 50 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 51 | return CGM.getTypes().ConvertType(T); |
| 52 | } |
| 53 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 54 | bool CodeGenFunction::isObjCPointerType(QualType T) { |
| 55 | // All Objective-C types are pointers. |
| 56 | return T->isObjCInterfaceType() || |
| 57 | T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType(); |
| 58 | } |
| 59 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 61 | return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() && |
| 62 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 65 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 66 | // Finish emission of indirect switches. |
| 67 | EmitIndirectSwitches(); |
| 68 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 69 | // Emit debug descriptor for function end. |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame^] | 70 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 71 | if (EndLoc.isValid()) { |
| 72 | DI->setLocation(EndLoc); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 73 | } |
| 74 | DI->EmitRegionEnd(CurFn, Builder); |
| 75 | } |
| 76 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 77 | // Emit a return for code that falls off the end. If insert point |
| 78 | // is a dummy block with no predecessors then remove the block itself. |
| 79 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame^] | 80 | if (isDummyBlock(BB)) { |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 81 | BB->eraseFromParent(); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame^] | 82 | } else { |
| 83 | // Just transfer to return |
| 84 | Builder.CreateBr(ReturnBlock); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 86 | assert(BreakContinueStack.empty() && |
| 87 | "mismatched push/pop in break/continue stack!"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame^] | 88 | |
| 89 | // Emit code to actually return. |
| 90 | Builder.SetInsertPoint(ReturnBlock); |
| 91 | if (!ReturnValue) { |
| 92 | Builder.CreateRetVoid(); |
| 93 | } else { |
| 94 | if (!hasAggregateLLVMType(FnRetTy)) { |
| 95 | Builder.CreateRet(Builder.CreateLoad(ReturnValue)); |
| 96 | } else if (FnRetTy->isAnyComplexType()) { |
| 97 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, FnRetTy); |
| 98 | Builder.CreateRetVoid(); |
| 99 | } else { |
| 100 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, FnRetTy); |
| 101 | Builder.CreateRetVoid(); |
| 102 | } |
| 103 | } |
| 104 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 105 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 106 | AllocaInsertPt->eraseFromParent(); |
| 107 | AllocaInsertPt = 0; |
| 108 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | // Verify that the function is well formed. |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 110 | assert(!verifyFunction(*CurFn) && "Generated function is not well formed."); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame^] | 113 | // FIXME: There is parallel code in StartObjCMethod. |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 114 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD, |
| 115 | llvm::Function *Fn) { |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 116 | CurFuncDecl = FD; |
| 117 | FnRetTy = FD->getResultType(); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 118 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 119 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 120 | |
| 121 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame^] | 122 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 123 | // Create a marker to make it easy to insert allocas into the entryblock |
| 124 | // later. Don't create this with the builder, because we don't want it |
| 125 | // folded. |
| 126 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 127 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 128 | EntryBB); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame^] | 129 | |
| 130 | ReturnBlock = llvm::BasicBlock::Create("return", CurFn); |
| 131 | ReturnValue = 0; |
| 132 | if (!FnRetTy->isVoidType()) |
| 133 | ReturnValue = CreateTempAlloca(ConvertType(FnRetTy), "retval"); |
| 134 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 135 | Builder.SetInsertPoint(EntryBB); |
| 136 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 137 | // Emit subprogram debug descriptor. |
| 138 | CGDebugInfo *DI = CGM.getDebugInfo(); |
| 139 | if (DI) { |
| 140 | CompoundStmt* body = dyn_cast<CompoundStmt>(FD->getBody()); |
| 141 | if (body && body->getLBracLoc().isValid()) { |
| 142 | DI->setLocation(body->getLBracLoc()); |
| 143 | } |
| 144 | DI->EmitFunctionStart(FD, CurFn, Builder); |
| 145 | } |
| 146 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 147 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 148 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 149 | |
| 150 | // Name the struct return argument. |
| 151 | if (hasAggregateLLVMType(FD->getResultType())) { |
| 152 | AI->setName("agg.result"); |
| 153 | ++AI; |
| 154 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 155 | |
| 156 | if (FD->getNumParams()) { |
| 157 | const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto(); |
| 158 | assert(FProto && "Function def must have prototype!"); |
| 159 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) { |
| 160 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 161 | const ParmVarDecl* CurParam = FD->getParamDecl(i); |
| 162 | llvm::Value* V = AI; |
| 163 | if (!getContext().typesAreCompatible(FProto->getArgType(i), |
| 164 | CurParam->getType())) { |
| 165 | // This must be a promotion, for something like |
| 166 | // "void a(x) short x; {..." |
| 167 | V = EmitScalarConversion(V, FProto->getArgType(i), |
| 168 | CurParam->getType()); |
| 169 | } |
| 170 | EmitParmDecl(*CurParam, V); |
| 171 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 172 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 173 | |
| 174 | EmitStmt(FD->getBody()); |
| 175 | |
| 176 | const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()); |
| 177 | if (S) { |
| 178 | FinishFunction(S->getRBracLoc()); |
| 179 | } else { |
| 180 | FinishFunction(); |
| 181 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 184 | /// isDummyBlock - Return true if BB is an empty basic block |
| 185 | /// with no predecessors. |
| 186 | bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) { |
Chris Lattner | 1e9660e | 2008-07-25 23:40:10 +0000 | [diff] [blame] | 187 | if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName()) |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 188 | return true; |
| 189 | return false; |
| 190 | } |
| 191 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 192 | /// StartBlock - Start new block named N. If insert block is a dummy block |
| 193 | /// then reuse it. |
| 194 | void CodeGenFunction::StartBlock(const char *N) { |
| 195 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 196 | if (!isDummyBlock(BB)) |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 197 | EmitBlock(llvm::BasicBlock::Create(N)); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 198 | else |
| 199 | BB->setName(N); |
| 200 | } |
| 201 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 202 | /// getCGRecordLayout - Return record layout info. |
| 203 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 204 | QualType Ty) { |
| 205 | const RecordType *RTy = Ty->getAsRecordType(); |
| 206 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 207 | |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 208 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 209 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 211 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 212 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 213 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 214 | bool OmitOnError) { |
| 215 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 218 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 219 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 220 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 221 | } |
| 222 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 223 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) |
| 224 | { |
| 225 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 226 | if (DestPtr->getType() != BP) |
| 227 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 228 | |
| 229 | // Get size and alignment info for this aggregate. |
| 230 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 231 | |
| 232 | // FIXME: Handle variable sized types. |
| 233 | const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); |
| 234 | |
| 235 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
| 236 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 237 | // TypeInfo.first describes size in bits. |
| 238 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 239 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 240 | TypeInfo.second/8)); |
| 241 | } |
| 242 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 243 | void CodeGenFunction::EmitIndirectSwitches() { |
| 244 | llvm::BasicBlock *Default; |
| 245 | |
Daniel Dunbar | 76526a5 | 2008-08-04 17:24:44 +0000 | [diff] [blame] | 246 | if (IndirectSwitches.empty()) |
| 247 | return; |
| 248 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 249 | if (!LabelIDs.empty()) { |
| 250 | Default = getBasicBlockForLabel(LabelIDs.begin()->first); |
| 251 | } else { |
| 252 | // No possible targets for indirect goto, just emit an infinite |
| 253 | // loop. |
| 254 | Default = llvm::BasicBlock::Create("indirectgoto.loop", CurFn); |
| 255 | llvm::BranchInst::Create(Default, Default); |
| 256 | } |
| 257 | |
| 258 | for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), |
| 259 | e = IndirectSwitches.end(); i != e; ++i) { |
| 260 | llvm::SwitchInst *I = *i; |
| 261 | |
| 262 | I->setSuccessor(0, Default); |
| 263 | for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), |
| 264 | LE = LabelIDs.end(); LI != LE; ++LI) { |
| 265 | I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 266 | LI->second), |
| 267 | getBasicBlockForLabel(LI->first)); |
| 268 | } |
| 269 | } |
| 270 | } |