blob: bac317c9bae1ff9a0fb082940d52f42277228a00 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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 Friedman3f2af102008-05-22 01:40:10 +000016#include "CGDebugInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/Decl.h"
20#include "clang/AST/DeclObjC.h"
Nate Begeman440b4562008-03-07 20:04:22 +000021#include "llvm/CallingConv.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Function.h"
25#include "llvm/Analysis/Verifier.h"
Devang Pateld9363c32007-09-28 21:49:18 +000026#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28using namespace CodeGen;
29
30CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000031 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
Chris Lattner41110242008-06-17 18:05:57 +000032 CaseRangeBlock(NULL) {
33 LLVMIntTy = ConvertType(getContext().IntTy);
34 LLVMPointerWidth = Target.getPointerWidth(0);
35}
Reid Spencer5f016e22007-07-11 17:01:13 +000036
37ASTContext &CodeGenFunction::getContext() const {
38 return CGM.getContext();
39}
40
41
42llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
43 llvm::BasicBlock *&BB = LabelMap[S];
44 if (BB) return BB;
45
46 // Create, but don't insert, the new block.
Gabor Greif984d0b42008-04-06 20:42:52 +000047 return BB = llvm::BasicBlock::Create(S->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000050llvm::Constant *
Steve Naroff248a7532008-04-15 22:42:06 +000051CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000052 return cast<llvm::Constant>(LocalDeclMap[BVD]);
53}
Reid Spencer5f016e22007-07-11 17:01:13 +000054
55const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
56 return CGM.getTypes().ConvertType(T);
57}
58
Chris Lattner41110242008-06-17 18:05:57 +000059bool CodeGenFunction::isObjCPointerType(QualType T) {
60 // All Objective-C types are pointers.
61 return T->isObjCInterfaceType() ||
62 T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
63}
64
Reid Spencer5f016e22007-07-11 17:01:13 +000065bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Chris Lattner41110242008-06-17 18:05:57 +000066 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
67 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000068}
69
Chris Lattner41110242008-06-17 18:05:57 +000070void CodeGenFunction::GenerateFunction(const Stmt *Body) {
Chris Lattner391d77a2008-03-30 23:03:07 +000071 // Emit the function body.
Chris Lattner41110242008-06-17 18:05:57 +000072 EmitStmt(Body);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +000073
74 // Finish emission of indirect switches.
75 EmitIndirectSwitches();
76
Sanjiv Guptaaf994172008-07-04 11:04:26 +000077 // Emit debug descriptor for function end.
78 CGDebugInfo *DI = CGM.getDebugInfo();
79 if (DI) {
80 const CompoundStmt* s = dyn_cast<CompoundStmt>(Body);
81 if (s && s->getRBracLoc().isValid()) {
82 DI->setLocation(s->getRBracLoc());
83 }
84 DI->EmitRegionEnd(CurFn, Builder);
85 }
86
Chris Lattner391d77a2008-03-30 23:03:07 +000087 // Emit a return for code that falls off the end. If insert point
88 // is a dummy block with no predecessors then remove the block itself.
89 llvm::BasicBlock *BB = Builder.GetInsertBlock();
90 if (isDummyBlock(BB))
91 BB->eraseFromParent();
92 else {
Devang Pateld9363c32007-09-28 21:49:18 +000093 // FIXME: if this is C++ main, this should return 0.
94 if (CurFn->getReturnType() == llvm::Type::VoidTy)
95 Builder.CreateRetVoid();
96 else
97 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
98 }
Chris Lattnerda138702007-07-16 21:28:45 +000099 assert(BreakContinueStack.empty() &&
100 "mismatched push/pop in break/continue stack!");
101
Chris Lattner5a2fa142007-12-02 06:32:24 +0000102 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
103 AllocaInsertPt->eraseFromParent();
104 AllocaInsertPt = 0;
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // Verify that the function is well formed.
Chris Lattner391d77a2008-03-30 23:03:07 +0000107 assert(!verifyFunction(*CurFn) && "Generated function is not well formed.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000110void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
111 llvm::Function *Fn) {
Chris Lattner41110242008-06-17 18:05:57 +0000112 CurFuncDecl = FD;
113 FnRetTy = FD->getResultType();
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000114 CurFn = Fn;
Chris Lattner41110242008-06-17 18:05:57 +0000115 assert(CurFn->isDeclaration() && "Function already has body?");
116
117 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
118
119 // Create a marker to make it easy to insert allocas into the entryblock
120 // later. Don't create this with the builder, because we don't want it
121 // folded.
122 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
123 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
124 EntryBB);
125
126 Builder.SetInsertPoint(EntryBB);
127
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000128 // Emit subprogram debug descriptor.
129 CGDebugInfo *DI = CGM.getDebugInfo();
130 if (DI) {
131 CompoundStmt* body = dyn_cast<CompoundStmt>(FD->getBody());
132 if (body && body->getLBracLoc().isValid()) {
133 DI->setLocation(body->getLBracLoc());
134 }
135 DI->EmitFunctionStart(FD, CurFn, Builder);
136 }
137
Chris Lattner41110242008-06-17 18:05:57 +0000138 // Emit allocs for param decls. Give the LLVM Argument nodes names.
139 llvm::Function::arg_iterator AI = CurFn->arg_begin();
140
141 // Name the struct return argument.
142 if (hasAggregateLLVMType(FD->getResultType())) {
143 AI->setName("agg.result");
144 ++AI;
145 }
146
147 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
148 assert(AI != CurFn->arg_end() && "Argument mismatch!");
149 EmitParmDecl(*FD->getParamDecl(i), AI);
150 }
151 GenerateFunction(FD->getBody());
152}
153
Devang Pateld9363c32007-09-28 21:49:18 +0000154/// isDummyBlock - Return true if BB is an empty basic block
155/// with no predecessors.
156bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
Chris Lattner1e9660e2008-07-25 23:40:10 +0000157 if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName())
Devang Pateld9363c32007-09-28 21:49:18 +0000158 return true;
159 return false;
160}
161
Devang Patel51b09f22007-10-04 23:45:31 +0000162/// StartBlock - Start new block named N. If insert block is a dummy block
163/// then reuse it.
164void CodeGenFunction::StartBlock(const char *N) {
165 llvm::BasicBlock *BB = Builder.GetInsertBlock();
166 if (!isDummyBlock(BB))
Gabor Greif984d0b42008-04-06 20:42:52 +0000167 EmitBlock(llvm::BasicBlock::Create(N));
Devang Patel51b09f22007-10-04 23:45:31 +0000168 else
169 BB->setName(N);
170}
171
Devang Patel88a981b2007-11-01 19:11:01 +0000172/// getCGRecordLayout - Return record layout info.
173const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000174 QualType Ty) {
175 const RecordType *RTy = Ty->getAsRecordType();
176 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000177
Chris Lattneraf319132008-02-05 06:55:31 +0000178 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000179}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000180
181/// WarnUnsupported - Print out a warning that codegen doesn't support the
182/// specified stmt yet.
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000183void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattner2c8569d2007-12-02 07:19:18 +0000184 CGM.WarnUnsupported(S, Type);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000185}
186
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000187unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
188 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
189 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
190}
191
192void CodeGenFunction::EmitIndirectSwitches() {
193 llvm::BasicBlock *Default;
194
Daniel Dunbar76526a52008-08-04 17:24:44 +0000195 if (IndirectSwitches.empty())
196 return;
197
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000198 if (!LabelIDs.empty()) {
199 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
200 } else {
201 // No possible targets for indirect goto, just emit an infinite
202 // loop.
203 Default = llvm::BasicBlock::Create("indirectgoto.loop", CurFn);
204 llvm::BranchInst::Create(Default, Default);
205 }
206
207 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
208 e = IndirectSwitches.end(); i != e; ++i) {
209 llvm::SwitchInst *I = *i;
210
211 I->setSuccessor(0, Default);
212 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
213 LE = LabelIDs.end(); LI != LE; ++LI) {
214 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
215 LI->second),
216 getBasicBlockForLabel(LI->first));
217 }
218 }
219}