blob: 7d70e6c83105ee3970588543713155c25037b58b [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"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/AST/AST.h"
Nate Begeman440b4562008-03-07 20:04:22 +000018#include "llvm/CallingConv.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
Chris Lattnerddee4232008-03-03 03:28:21 +000022#include "llvm/ParamAttrsList.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "llvm/Analysis/Verifier.h"
Devang Pateld9363c32007-09-28 21:49:18 +000024#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26using namespace CodeGen;
27
28CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000029 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
30 CaseRangeBlock(NULL) {}
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.
42 return BB = new llvm::BasicBlock(S->getName());
43}
44
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000045llvm::Constant *
46CodeGenFunction::GetAddrOfStaticLocalVar(const BlockVarDecl *BVD) {
47 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
54bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Anders Carlsson793680e2007-10-12 23:56:29 +000055 return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() &&
56 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000057}
58
59
60void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
61 LLVMIntTy = ConvertType(getContext().IntTy);
Chris Lattner47f7dbf2007-09-04 02:34:27 +000062 LLVMPointerWidth = static_cast<unsigned>(
Chris Lattner98be4942008-03-05 18:54:05 +000063 getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy)));
Reid Spencer5f016e22007-07-11 17:01:13 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 CurFuncDecl = FD;
Chris Lattner9cd4fe42007-12-02 07:09:19 +000066 CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
Reid Spencer5f016e22007-07-11 17:01:13 +000067 assert(CurFn->isDeclaration() && "Function already has body?");
68
Chris Lattner5c6a42a2007-11-27 06:46:51 +000069 // TODO: Set up linkage and many other things. Note, this is a simple
70 // approximation of what we really want.
Chris Lattnerddee4232008-03-03 03:28:21 +000071 if (FD->getAttr<DLLImportAttr>())
72 CurFn->setLinkage(llvm::Function::DLLImportLinkage);
73 else if (FD->getAttr<DLLExportAttr>())
74 CurFn->setLinkage(llvm::Function::DLLExportLinkage);
75 else if (FD->getAttr<WeakAttr>() || FD->isInline())
Chris Lattner5c6a42a2007-11-27 06:46:51 +000076 CurFn->setLinkage(llvm::Function::WeakLinkage);
Chris Lattnerddee4232008-03-03 03:28:21 +000077 else if (FD->getStorageClass() == FunctionDecl::Static)
78 CurFn->setLinkage(llvm::Function::InternalLinkage);
79
Nate Begeman440b4562008-03-07 20:04:22 +000080 if (FD->getAttr<FastCallAttr>())
81 CurFn->setCallingConv(llvm::CallingConv::Fast);
82
Chris Lattnerddee4232008-03-03 03:28:21 +000083 if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>())
84 CurFn->setVisibility(attr->getVisibility());
85 // FIXME: else handle -fvisibility
86
87
Chris Lattnerf89e88d2008-03-03 03:45:26 +000088 unsigned FuncAttrs = 0;
Chris Lattnerddee4232008-03-03 03:28:21 +000089 if (FD->getAttr<NoThrowAttr>())
Chris Lattnerf89e88d2008-03-03 03:45:26 +000090 FuncAttrs |= llvm::ParamAttr::NoUnwind;
Chris Lattnerddee4232008-03-03 03:28:21 +000091 if (FD->getAttr<NoReturnAttr>())
Chris Lattnerf89e88d2008-03-03 03:45:26 +000092 FuncAttrs |= llvm::ParamAttr::NoReturn;
93
94 if (FuncAttrs) {
95 llvm::ParamAttrsVector ParamAttrsVec;
96 ParamAttrsVec.push_back(llvm::ParamAttrsWithIndex::get(0, FuncAttrs));
Chris Lattnerddee4232008-03-03 03:28:21 +000097 CurFn->setParamAttrs(llvm::ParamAttrsList::get(ParamAttrsVec));
Chris Lattnerf89e88d2008-03-03 03:45:26 +000098 }
Chris Lattnerddee4232008-03-03 03:28:21 +000099
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // Create a marker to make it easy to insert allocas into the entryblock
Chris Lattner55352a22007-12-17 20:50:59 +0000103 // later. Don't create this with the builder, because we don't want it
104 // folded.
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
Chris Lattner55352a22007-12-17 20:50:59 +0000106 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
107 EntryBB);
108
109 Builder.SetInsertPoint(EntryBB);
Reid Spencer5f016e22007-07-11 17:01:13 +0000110
111 // Emit allocs for param decls. Give the LLVM Argument nodes names.
112 llvm::Function::arg_iterator AI = CurFn->arg_begin();
113
114 // Name the struct return argument.
115 if (hasAggregateLLVMType(FD->getResultType())) {
116 AI->setName("agg.result");
117 ++AI;
118 }
119
120 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
121 assert(AI != CurFn->arg_end() && "Argument mismatch!");
122 EmitParmDecl(*FD->getParamDecl(i), AI);
123 }
124
125 // Emit the function body.
126 EmitStmt(FD->getBody());
127
Devang Pateld9363c32007-09-28 21:49:18 +0000128 // Emit a return for code that falls off the end. If insert point
129 // is a dummy block with no predecessors then remove the block itself.
130 llvm::BasicBlock *BB = Builder.GetInsertBlock();
131 if (isDummyBlock(BB))
132 BB->eraseFromParent();
133 else {
134 // FIXME: if this is C++ main, this should return 0.
135 if (CurFn->getReturnType() == llvm::Type::VoidTy)
136 Builder.CreateRetVoid();
137 else
138 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
139 }
Chris Lattnerda138702007-07-16 21:28:45 +0000140 assert(BreakContinueStack.empty() &&
141 "mismatched push/pop in break/continue stack!");
142
Chris Lattner5a2fa142007-12-02 06:32:24 +0000143 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
144 AllocaInsertPt->eraseFromParent();
145 AllocaInsertPt = 0;
146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // Verify that the function is well formed.
148 assert(!verifyFunction(*CurFn));
149}
150
Devang Pateld9363c32007-09-28 21:49:18 +0000151/// isDummyBlock - Return true if BB is an empty basic block
152/// with no predecessors.
153bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
154 if (BB->empty() && pred_begin(BB) == pred_end(BB))
155 return true;
156 return false;
157}
158
Devang Patel51b09f22007-10-04 23:45:31 +0000159/// StartBlock - Start new block named N. If insert block is a dummy block
160/// then reuse it.
161void CodeGenFunction::StartBlock(const char *N) {
162 llvm::BasicBlock *BB = Builder.GetInsertBlock();
163 if (!isDummyBlock(BB))
164 EmitBlock(new llvm::BasicBlock(N));
165 else
166 BB->setName(N);
167}
168
Devang Patel88a981b2007-11-01 19:11:01 +0000169/// getCGRecordLayout - Return record layout info.
170const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
Chris Lattneraf319132008-02-05 06:55:31 +0000171 QualType Ty) {
172 const RecordType *RTy = Ty->getAsRecordType();
173 assert (RTy && "Unexpected type. RecordType expected here.");
Devang Patelb84a06e2007-10-23 02:10:49 +0000174
Chris Lattneraf319132008-02-05 06:55:31 +0000175 return CGT.getCGRecordLayout(RTy->getDecl());
Devang Patelb84a06e2007-10-23 02:10:49 +0000176}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000177
178/// WarnUnsupported - Print out a warning that codegen doesn't support the
179/// specified stmt yet.
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000180void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattner2c8569d2007-12-02 07:19:18 +0000181 CGM.WarnUnsupported(S, Type);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000182}
183