blob: d02c860a102fe0ba1353fef7082a63f8afc84faa [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/Analysis/Verifier.h"
Devang Pateld9363c32007-09-28 21:49:18 +000021#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23using namespace CodeGen;
24
25CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000026 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
Chris Lattner41110242008-06-17 18:05:57 +000027 CaseRangeBlock(NULL) {
28 LLVMIntTy = ConvertType(getContext().IntTy);
29 LLVMPointerWidth = Target.getPointerWidth(0);
30}
Reid Spencer5f016e22007-07-11 17:01:13 +000031
32ASTContext &CodeGenFunction::getContext() const {
33 return CGM.getContext();
34}
35
36
37llvm::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 Greif984d0b42008-04-06 20:42:52 +000042 return BB = llvm::BasicBlock::Create(S->getName());
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000045llvm::Constant *
Steve Naroff248a7532008-04-15 22:42:06 +000046CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000047 return cast<llvm::Constant>(LocalDeclMap[BVD]);
48}
Reid Spencer5f016e22007-07-11 17:01:13 +000049
50const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
51 return CGM.getTypes().ConvertType(T);
52}
53
Chris Lattner41110242008-06-17 18:05:57 +000054bool CodeGenFunction::isObjCPointerType(QualType T) {
55 // All Objective-C types are pointers.
56 return T->isObjCInterfaceType() ||
57 T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType();
58}
59
Reid Spencer5f016e22007-07-11 17:01:13 +000060bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Chris Lattner41110242008-06-17 18:05:57 +000061 return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() &&
62 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000063}
64
Chris Lattner41110242008-06-17 18:05:57 +000065void CodeGenFunction::GenerateFunction(const Stmt *Body) {
Chris Lattner391d77a2008-03-30 23:03:07 +000066 // Emit the function body.
Chris Lattner41110242008-06-17 18:05:57 +000067 EmitStmt(Body);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +000068
69 // Finish emission of indirect switches.
70 EmitIndirectSwitches();
71
Sanjiv Guptaaf994172008-07-04 11:04:26 +000072 // Emit debug descriptor for function end.
73 CGDebugInfo *DI = CGM.getDebugInfo();
74 if (DI) {
75 const CompoundStmt* s = dyn_cast<CompoundStmt>(Body);
76 if (s && s->getRBracLoc().isValid()) {
77 DI->setLocation(s->getRBracLoc());
78 }
79 DI->EmitRegionEnd(CurFn, Builder);
80 }
81
Chris Lattner391d77a2008-03-30 23:03:07 +000082 // Emit a return for code that falls off the end. If insert point
83 // is a dummy block with no predecessors then remove the block itself.
84 llvm::BasicBlock *BB = Builder.GetInsertBlock();
85 if (isDummyBlock(BB))
86 BB->eraseFromParent();
87 else {
Devang Pateld9363c32007-09-28 21:49:18 +000088 // FIXME: if this is C++ main, this should return 0.
89 if (CurFn->getReturnType() == llvm::Type::VoidTy)
90 Builder.CreateRetVoid();
91 else
92 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
93 }
Chris Lattnerda138702007-07-16 21:28:45 +000094 assert(BreakContinueStack.empty() &&
95 "mismatched push/pop in break/continue stack!");
96
Chris Lattner5a2fa142007-12-02 06:32:24 +000097 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
98 AllocaInsertPt->eraseFromParent();
99 AllocaInsertPt = 0;
100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 // Verify that the function is well formed.
Chris Lattner391d77a2008-03-30 23:03:07 +0000102 assert(!verifyFunction(*CurFn) && "Generated function is not well formed.");
Reid Spencer5f016e22007-07-11 17:01:13 +0000103}
104
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000105void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
106 llvm::Function *Fn) {
Chris Lattner41110242008-06-17 18:05:57 +0000107 CurFuncDecl = FD;
108 FnRetTy = FD->getResultType();
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000109 CurFn = Fn;
Chris Lattner41110242008-06-17 18:05:57 +0000110 assert(CurFn->isDeclaration() && "Function already has body?");
111
112 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
113
114 // Create a marker to make it easy to insert allocas into the entryblock
115 // later. Don't create this with the builder, because we don't want it
116 // folded.
117 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
118 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
119 EntryBB);
120
121 Builder.SetInsertPoint(EntryBB);
122
Sanjiv Guptaaf994172008-07-04 11:04:26 +0000123 // Emit subprogram debug descriptor.
124 CGDebugInfo *DI = CGM.getDebugInfo();
125 if (DI) {
126 CompoundStmt* body = dyn_cast<CompoundStmt>(FD->getBody());
127 if (body && body->getLBracLoc().isValid()) {
128 DI->setLocation(body->getLBracLoc());
129 }
130 DI->EmitFunctionStart(FD, CurFn, Builder);
131 }
132
Chris Lattner41110242008-06-17 18:05:57 +0000133 // Emit allocs for param decls. Give the LLVM Argument nodes names.
134 llvm::Function::arg_iterator AI = CurFn->arg_begin();
135
136 // Name the struct return argument.
137 if (hasAggregateLLVMType(FD->getResultType())) {
138 AI->setName("agg.result");
139 ++AI;
140 }
141
142 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
143 assert(AI != CurFn->arg_end() && "Argument mismatch!");
144 EmitParmDecl(*FD->getParamDecl(i), AI);
145 }
146 GenerateFunction(FD->getBody());
147}
148
Devang Pateld9363c32007-09-28 21:49:18 +0000149/// isDummyBlock - Return true if BB is an empty basic block
150/// with no predecessors.
151bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
Chris Lattner1e9660e2008-07-25 23:40:10 +0000152 if (BB->empty() && pred_begin(BB) == pred_end(BB) && !BB->hasName())
Devang Pateld9363c32007-09-28 21:49:18 +0000153 return true;
154 return false;
155}
156
Devang Patel51b09f22007-10-04 23:45:31 +0000157/// StartBlock - Start new block named N. If insert block is a dummy block
158/// then reuse it.
159void CodeGenFunction::StartBlock(const char *N) {
160 llvm::BasicBlock *BB = Builder.GetInsertBlock();
161 if (!isDummyBlock(BB))
Gabor Greif984d0b42008-04-06 20:42:52 +0000162 EmitBlock(llvm::BasicBlock::Create(N));
Devang Patel51b09f22007-10-04 23:45:31 +0000163 else
164 BB->setName(N);
165}
166
Devang Patel88a981b2007-11-01 19:11:01 +0000167/// getCGRecordLayout - Return record layout info.
168const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000169 QualType Ty) {
170 const RecordType *RTy = Ty->getAsRecordType();
171 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000172
Chris Lattneraf319132008-02-05 06:55:31 +0000173 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000174}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000175
176/// WarnUnsupported - Print out a warning that codegen doesn't support the
177/// specified stmt yet.
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000178void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattner2c8569d2007-12-02 07:19:18 +0000179 CGM.WarnUnsupported(S, Type);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000180}
181
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000182unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) {
183 // Use LabelIDs.size() as the new ID if one hasn't been assigned.
184 return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second;
185}
186
187void CodeGenFunction::EmitIndirectSwitches() {
188 llvm::BasicBlock *Default;
189
Daniel Dunbar76526a52008-08-04 17:24:44 +0000190 if (IndirectSwitches.empty())
191 return;
192
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000193 if (!LabelIDs.empty()) {
194 Default = getBasicBlockForLabel(LabelIDs.begin()->first);
195 } else {
196 // No possible targets for indirect goto, just emit an infinite
197 // loop.
198 Default = llvm::BasicBlock::Create("indirectgoto.loop", CurFn);
199 llvm::BranchInst::Create(Default, Default);
200 }
201
202 for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(),
203 e = IndirectSwitches.end(); i != e; ++i) {
204 llvm::SwitchInst *I = *i;
205
206 I->setSuccessor(0, Default);
207 for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(),
208 LE = LabelIDs.end(); LI != LE; ++LI) {
209 I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty,
210 LI->second),
211 getBasicBlockForLabel(LI->first));
212 }
213 }
214}