blob: 1b94c595616d9167c4a8392c60fb08d2a7c8e3b3 [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerbed31442007-05-28 01:07:47 +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"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000016#include "clang/Basic/TargetInfo.h"
17#include "clang/AST/AST.h"
Nate Begeman0a6192c2008-03-07 20:04:22 +000018#include "llvm/CallingConv.h"
Chris Lattner208ae962007-05-30 17:57:17 +000019#include "llvm/Constants.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000020#include "llvm/DerivedTypes.h"
Chris Lattner308f4312007-05-29 23:50:05 +000021#include "llvm/Function.h"
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +000022#include "llvm/Analysis/Verifier.h"
Devang Patel8ec4f832007-09-28 21:49:18 +000023#include "llvm/Support/CFG.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000024using namespace clang;
25using namespace CodeGen;
26
Chris Lattnerd1af2d22007-05-29 23:17:50 +000027CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patel11663122007-10-08 20:57:48 +000028 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
Chris Lattnerc00c35a2008-03-30 23:25:33 +000029 CaseRangeBlock(NULL) {}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000030
Chris Lattner6db1fb82007-06-02 22:49:07 +000031ASTContext &CodeGenFunction::getContext() const {
32 return CGM.getContext();
33}
34
Chris Lattnerd1af2d22007-05-29 23:17:50 +000035
Chris Lattnerac248202007-05-30 00:13:02 +000036llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000037 llvm::BasicBlock *&BB = LabelMap[S];
Chris Lattnerac248202007-05-30 00:13:02 +000038 if (BB) return BB;
39
40 // Create, but don't insert, the new block.
Gabor Greifd36afd72008-04-06 20:42:52 +000041 return BB = llvm::BasicBlock::Create(S->getName());
Chris Lattnerac248202007-05-30 00:13:02 +000042}
43
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000044llvm::Constant *
Steve Naroff08899ff2008-04-15 22:42:06 +000045CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) {
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000046 return cast<llvm::Constant>(LocalDeclMap[BVD]);
47}
Chris Lattnerac248202007-05-30 00:13:02 +000048
Chris Lattnerf033c142007-06-22 19:05:19 +000049const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
50 return CGM.getTypes().ConvertType(T);
Chris Lattner45bb9142007-06-09 02:28:57 +000051}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000052
Chris Lattner54fb19e2007-06-22 22:02:34 +000053bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Chris Lattner182f6602008-04-02 17:45:06 +000054 return !T->isRealType() && !T->isPointerLikeType() &&
Anders Carlsson24ebce62007-10-12 23:56:29 +000055 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Chris Lattner54fb19e2007-06-22 22:02:34 +000056}
57
Chris Lattner4bd55962008-03-30 23:03:07 +000058/// Generate an Objective-C method. An Objective-C method is a C function with
59/// its pointer, name, and types registered in the class struture.
60// FIXME: This method contains a lot of code copied and pasted from
61// GenerateCode. This should be factored out.
62void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
63 llvm::SmallVector<const llvm::Type *, 16> ParamTypes;
64 for (unsigned i=0 ; i<OMD->param_size() ; i++) {
65 ParamTypes.push_back(ConvertType(OMD->getParamDecl(i)->getType()));
66 }
Chris Lattnerc00c35a2008-03-30 23:25:33 +000067 CurFn =CGM.getObjCRuntime()->MethodPreamble(ConvertType(OMD->getResultType()),
68 llvm::PointerType::getUnqual(llvm::Type::Int32Ty),
69 ParamTypes.begin(),
70 OMD->param_size(),
71 OMD->isVariadic());
Gabor Greifd36afd72008-04-06 20:42:52 +000072 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
Chris Lattner4bd55962008-03-30 23:03:07 +000073
74 // Create a marker to make it easy to insert allocas into the entryblock
75 // later. Don't create this with the builder, because we don't want it
76 // folded.
77 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
78 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
79 EntryBB);
80
81 FnRetTy = OMD->getResultType();
82
83 Builder.SetInsertPoint(EntryBB);
84
85 // Emit allocs for param decls. Give the LLVM Argument nodes names.
86 llvm::Function::arg_iterator AI = CurFn->arg_begin();
87
88 // Name the struct return argument.
89 // FIXME: Probably should be in the runtime, or it will trample the other
90 // hidden arguments.
91 if (hasAggregateLLVMType(OMD->getResultType())) {
92 AI->setName("agg.result");
93 ++AI;
94 }
95
96 // Add implicit parameters to the decl map.
97 // TODO: Add something to AST to let the runtime specify the names and types
98 // of these.
99 llvm::Value *&DMEntry = LocalDeclMap[&(*OMD->getSelfDecl())];
Chris Lattner5506f8c2008-04-04 04:07:35 +0000100 const llvm::Type *SelfTy = AI->getType();
101 llvm::Value *DeclPtr = new llvm::AllocaInst(SelfTy, 0, "self.addr",
102 AllocaInsertPt);
103
104 // Store the initial value into the alloca.
105 Builder.CreateStore(AI, DeclPtr);
106 DMEntry = DeclPtr;
Chris Lattner4bd55962008-03-30 23:03:07 +0000107 ++AI; ++AI;
108
109
110 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
111 assert(AI != CurFn->arg_end() && "Argument mismatch!");
112 EmitParmDecl(*OMD->getParamDecl(i), AI);
113 }
114
115 // Emit the function body.
116 EmitStmt(OMD->getBody());
117
118 // Emit a return for code that falls off the end. If insert point
119 // is a dummy block with no predecessors then remove the block itself.
120 llvm::BasicBlock *BB = Builder.GetInsertBlock();
121 if (isDummyBlock(BB))
122 BB->eraseFromParent();
123 else {
124 if (CurFn->getReturnType() == llvm::Type::VoidTy)
125 Builder.CreateRetVoid();
126 else
127 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
128 }
129 assert(BreakContinueStack.empty() &&
130 "mismatched push/pop in break/continue stack!");
131
132 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
133 AllocaInsertPt->eraseFromParent();
134 AllocaInsertPt = 0;
135 // Verify that the function is well formed.
136 assert(!verifyFunction(*CurFn) && "Generated method is not well formed.");
137}
Chris Lattner54fb19e2007-06-22 22:02:34 +0000138
Chris Lattner5506f8c2008-04-04 04:07:35 +0000139llvm::Value *CodeGenFunction::LoadObjCSelf(void)
140{
141 if(const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) {
142 llvm::Value *SelfPtr = LocalDeclMap[&(*OMD->getSelfDecl())];
143 return Builder.CreateLoad(SelfPtr, "self");
144 }
145 return NULL;
146}
147
Chris Lattner308f4312007-05-29 23:50:05 +0000148void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
Chris Lattnerf033c142007-06-22 19:05:19 +0000149 LLVMIntTy = ConvertType(getContext().IntTy);
Chris Lattnerfb2eb692007-09-04 02:34:27 +0000150 LLVMPointerWidth = static_cast<unsigned>(
Chris Lattner37e05872008-03-05 18:54:05 +0000151 getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy)));
Chris Lattner6db1fb82007-06-02 22:49:07 +0000152
Chris Lattner3f3dbee2007-06-02 03:19:07 +0000153 CurFuncDecl = FD;
Chris Lattner5506f8c2008-04-04 04:07:35 +0000154 FnRetTy = FD->getType()->getAsFunctionType()->getResultType();
Chris Lattner4bd55962008-03-30 23:03:07 +0000155
156
Chris Lattner5bcdf242007-12-02 07:09:19 +0000157 CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
Chris Lattnerb6984c42007-06-20 04:44:43 +0000158 assert(CurFn->isDeclaration() && "Function already has body?");
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000159
Chris Lattnerf04b69b2007-11-27 06:46:51 +0000160 // TODO: Set up linkage and many other things. Note, this is a simple
161 // approximation of what we really want.
Chris Lattner84966392008-03-03 03:28:21 +0000162 if (FD->getAttr<DLLImportAttr>())
163 CurFn->setLinkage(llvm::Function::DLLImportLinkage);
164 else if (FD->getAttr<DLLExportAttr>())
165 CurFn->setLinkage(llvm::Function::DLLExportLinkage);
166 else if (FD->getAttr<WeakAttr>() || FD->isInline())
Chris Lattnerf04b69b2007-11-27 06:46:51 +0000167 CurFn->setLinkage(llvm::Function::WeakLinkage);
Chris Lattner84966392008-03-03 03:28:21 +0000168 else if (FD->getStorageClass() == FunctionDecl::Static)
169 CurFn->setLinkage(llvm::Function::InternalLinkage);
170
Nate Begeman0a6192c2008-03-07 20:04:22 +0000171 if (FD->getAttr<FastCallAttr>())
172 CurFn->setCallingConv(llvm::CallingConv::Fast);
173
Chris Lattner84966392008-03-03 03:28:21 +0000174 if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>())
175 CurFn->setVisibility(attr->getVisibility());
176 // FIXME: else handle -fvisibility
177
178
Chris Lattner65079bd2008-03-03 03:45:26 +0000179 unsigned FuncAttrs = 0;
Chris Lattner84966392008-03-03 03:28:21 +0000180 if (FD->getAttr<NoThrowAttr>())
Chris Lattner65079bd2008-03-03 03:45:26 +0000181 FuncAttrs |= llvm::ParamAttr::NoUnwind;
Chris Lattner84966392008-03-03 03:28:21 +0000182 if (FD->getAttr<NoReturnAttr>())
Chris Lattner65079bd2008-03-03 03:45:26 +0000183 FuncAttrs |= llvm::ParamAttr::NoReturn;
184
185 if (FuncAttrs) {
Chris Lattner28842b72008-03-12 17:46:07 +0000186 llvm::ParamAttrsWithIndex PAWI =
187 llvm::ParamAttrsWithIndex::get(0, FuncAttrs);
188 CurFn->setParamAttrs(llvm::PAListPtr::get(&PAWI, 1));
Chris Lattner65079bd2008-03-03 03:45:26 +0000189 }
Chris Lattner84966392008-03-03 03:28:21 +0000190
Gabor Greifd36afd72008-04-06 20:42:52 +0000191 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
Chris Lattner308f4312007-05-29 23:50:05 +0000192
Chris Lattner03df1222007-06-02 04:53:11 +0000193 // Create a marker to make it easy to insert allocas into the entryblock
Chris Lattnere6a76da2007-12-17 20:50:59 +0000194 // later. Don't create this with the builder, because we don't want it
195 // folded.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000196 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
Chris Lattnere6a76da2007-12-17 20:50:59 +0000197 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
198 EntryBB);
199
200 Builder.SetInsertPoint(EntryBB);
Chris Lattner308f4312007-05-29 23:50:05 +0000201
Chris Lattner54fb19e2007-06-22 22:02:34 +0000202 // Emit allocs for param decls. Give the LLVM Argument nodes names.
Chris Lattner53621a52007-06-13 20:44:40 +0000203 llvm::Function::arg_iterator AI = CurFn->arg_begin();
Chris Lattner54fb19e2007-06-22 22:02:34 +0000204
205 // Name the struct return argument.
206 if (hasAggregateLLVMType(FD->getResultType())) {
207 AI->setName("agg.result");
208 ++AI;
209 }
210
Chris Lattner53621a52007-06-13 20:44:40 +0000211 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
212 assert(AI != CurFn->arg_end() && "Argument mismatch!");
213 EmitParmDecl(*FD->getParamDecl(i), AI);
214 }
Chris Lattner84915fa2007-06-02 04:16:21 +0000215
216 // Emit the function body.
Chris Lattner308f4312007-05-29 23:50:05 +0000217 EmitStmt(FD->getBody());
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +0000218
Devang Patel8ec4f832007-09-28 21:49:18 +0000219 // Emit a return for code that falls off the end. If insert point
220 // is a dummy block with no predecessors then remove the block itself.
221 llvm::BasicBlock *BB = Builder.GetInsertBlock();
222 if (isDummyBlock(BB))
223 BB->eraseFromParent();
224 else {
225 // FIXME: if this is C++ main, this should return 0.
226 if (CurFn->getReturnType() == llvm::Type::VoidTy)
227 Builder.CreateRetVoid();
228 else
229 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
230 }
Chris Lattnere73e4322007-07-16 21:28:45 +0000231 assert(BreakContinueStack.empty() &&
232 "mismatched push/pop in break/continue stack!");
233
Chris Lattnerc48023b2007-12-02 06:32:24 +0000234 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
235 AllocaInsertPt->eraseFromParent();
236 AllocaInsertPt = 0;
237
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +0000238 // Verify that the function is well formed.
Chris Lattner4bd55962008-03-30 23:03:07 +0000239 assert(!verifyFunction(*CurFn) && "Generated function is not well formed.");
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000240}
Chris Lattner308f4312007-05-29 23:50:05 +0000241
Devang Patel8ec4f832007-09-28 21:49:18 +0000242/// isDummyBlock - Return true if BB is an empty basic block
243/// with no predecessors.
244bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
245 if (BB->empty() && pred_begin(BB) == pred_end(BB))
246 return true;
247 return false;
248}
249
Devang Patelda5d6bb2007-10-04 23:45:31 +0000250/// StartBlock - Start new block named N. If insert block is a dummy block
251/// then reuse it.
252void CodeGenFunction::StartBlock(const char *N) {
253 llvm::BasicBlock *BB = Builder.GetInsertBlock();
254 if (!isDummyBlock(BB))
Gabor Greifd36afd72008-04-06 20:42:52 +0000255 EmitBlock(llvm::BasicBlock::Create(N));
Devang Patelda5d6bb2007-10-04 23:45:31 +0000256 else
257 BB->setName(N);
258}
259
Devang Patele11664a2007-11-01 19:11:01 +0000260/// getCGRecordLayout - Return record layout info.
261const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattner86964a92008-02-05 06:55:31 +0000262 QualType Ty) {
263 const RecordType *RTy = Ty->getAsRecordType();
264 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patel3e11cce2007-10-23 02:10:49 +0000265
Chris Lattner86964a92008-02-05 06:55:31 +0000266 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patel3e11cce2007-10-23 02:10:49 +0000267}
Chris Lattnerfc944342007-12-02 01:43:38 +0000268
269/// WarnUnsupported - Print out a warning that codegen doesn't support the
270/// specified stmt yet.
Chris Lattnerf0780fa2007-12-02 01:49:16 +0000271void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattnerd45aa2a2007-12-02 07:19:18 +0000272 CGM.WarnUnsupported(S, Type);
Chris Lattnerfc944342007-12-02 01:43:38 +0000273}
274