blob: 6701b71f0977f6c367abbc455d6d3004bb496cc6 [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"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
Chris Lattnerddee4232008-03-03 03:28:21 +000021#include "llvm/ParamAttrsList.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/Analysis/Verifier.h"
Devang Pateld9363c32007-09-28 21:49:18 +000023#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25using namespace CodeGen;
26
27CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000028 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
29 CaseRangeBlock(NULL) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000030
31ASTContext &CodeGenFunction::getContext() const {
32 return CGM.getContext();
33}
34
35
36llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
37 llvm::BasicBlock *&BB = LabelMap[S];
38 if (BB) return BB;
39
40 // Create, but don't insert, the new block.
41 return BB = new llvm::BasicBlock(S->getName());
42}
43
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000044llvm::Constant *
45CodeGenFunction::GetAddrOfStaticLocalVar(const BlockVarDecl *BVD) {
46 return cast<llvm::Constant>(LocalDeclMap[BVD]);
47}
Reid Spencer5f016e22007-07-11 17:01:13 +000048
49const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
50 return CGM.getTypes().ConvertType(T);
51}
52
53bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Anders Carlsson793680e2007-10-12 23:56:29 +000054 return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() &&
55 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000056}
57
58
59void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
60 LLVMIntTy = ConvertType(getContext().IntTy);
Chris Lattner47f7dbf2007-09-04 02:34:27 +000061 LLVMPointerWidth = static_cast<unsigned>(
Chris Lattnerd2d2a112007-07-14 01:29:45 +000062 getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy),
Chris Lattner47f7dbf2007-09-04 02:34:27 +000063 SourceLocation()));
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
80 if (const VisibilityAttr *attr = FD->getAttr<VisibilityAttr>())
81 CurFn->setVisibility(attr->getVisibility());
82 // FIXME: else handle -fvisibility
83
84
85 llvm::ParamAttrsVector ParamAttrsVec;
86
87 if (FD->getAttr<NoThrowAttr>())
88 ParamAttrsVec.push_back(
89 llvm::ParamAttrsWithIndex::get(ParamAttrsVec.size(), llvm::ParamAttr::NoUnwind));
90 if (FD->getAttr<NoReturnAttr>())
91 ParamAttrsVec.push_back(
92 llvm::ParamAttrsWithIndex::get(ParamAttrsVec.size(), llvm::ParamAttr::NoReturn));
93
94 if (!ParamAttrsVec.empty())
95 CurFn->setParamAttrs(llvm::ParamAttrsList::get(ParamAttrsVec));
96
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
99
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 // Create a marker to make it easy to insert allocas into the entryblock
Chris Lattner55352a22007-12-17 20:50:59 +0000101 // later. Don't create this with the builder, because we don't want it
102 // folded.
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
Chris Lattner55352a22007-12-17 20:50:59 +0000104 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
105 EntryBB);
106
107 Builder.SetInsertPoint(EntryBB);
Reid Spencer5f016e22007-07-11 17:01:13 +0000108
109 // Emit allocs for param decls. Give the LLVM Argument nodes names.
110 llvm::Function::arg_iterator AI = CurFn->arg_begin();
111
112 // Name the struct return argument.
113 if (hasAggregateLLVMType(FD->getResultType())) {
114 AI->setName("agg.result");
115 ++AI;
116 }
117
118 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
119 assert(AI != CurFn->arg_end() && "Argument mismatch!");
120 EmitParmDecl(*FD->getParamDecl(i), AI);
121 }
122
123 // Emit the function body.
124 EmitStmt(FD->getBody());
125
Devang Pateld9363c32007-09-28 21:49:18 +0000126 // Emit a return for code that falls off the end. If insert point
127 // is a dummy block with no predecessors then remove the block itself.
128 llvm::BasicBlock *BB = Builder.GetInsertBlock();
129 if (isDummyBlock(BB))
130 BB->eraseFromParent();
131 else {
132 // FIXME: if this is C++ main, this should return 0.
133 if (CurFn->getReturnType() == llvm::Type::VoidTy)
134 Builder.CreateRetVoid();
135 else
136 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
137 }
Chris Lattnerda138702007-07-16 21:28:45 +0000138 assert(BreakContinueStack.empty() &&
139 "mismatched push/pop in break/continue stack!");
140
Chris Lattner5a2fa142007-12-02 06:32:24 +0000141 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
142 AllocaInsertPt->eraseFromParent();
143 AllocaInsertPt = 0;
144
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 // Verify that the function is well formed.
146 assert(!verifyFunction(*CurFn));
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) {
152 if (BB->empty() && pred_begin(BB) == pred_end(BB))
153 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))
162 EmitBlock(new llvm::BasicBlock(N));
163 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