blob: 5cbd1958f48db226f66f7814c43e36016c0cc5da [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
21#include "llvm/Analysis/Verifier.h"
Devang Pateld9363c32007-09-28 21:49:18 +000022#include "llvm/Support/CFG.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace CodeGen;
25
26CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Devang Patelc049e4f2007-10-08 20:57:48 +000027 : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL),
28 CaseRangeBlock(NULL) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000029
30ASTContext &CodeGenFunction::getContext() const {
31 return CGM.getContext();
32}
33
34
35llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
36 llvm::BasicBlock *&BB = LabelMap[S];
37 if (BB) return BB;
38
39 // Create, but don't insert, the new block.
40 return BB = new llvm::BasicBlock(S->getName());
41}
42
43
44const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
45 return CGM.getTypes().ConvertType(T);
46}
47
48bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
Anders Carlsson793680e2007-10-12 23:56:29 +000049 return !T->isRealType() && !T->isPointerType() && !T->isReferenceType() &&
50 !T->isVoidType() && !T->isVectorType() && !T->isFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +000051}
52
53
54void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
55 LLVMIntTy = ConvertType(getContext().IntTy);
Chris Lattner47f7dbf2007-09-04 02:34:27 +000056 LLVMPointerWidth = static_cast<unsigned>(
Chris Lattnerd2d2a112007-07-14 01:29:45 +000057 getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy),
Chris Lattner47f7dbf2007-09-04 02:34:27 +000058 SourceLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +000059
Reid Spencer5f016e22007-07-11 17:01:13 +000060 CurFuncDecl = FD;
Chris Lattner9cd4fe42007-12-02 07:09:19 +000061 CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
Reid Spencer5f016e22007-07-11 17:01:13 +000062 assert(CurFn->isDeclaration() && "Function already has body?");
63
Chris Lattner5c6a42a2007-11-27 06:46:51 +000064 // TODO: Set up linkage and many other things. Note, this is a simple
65 // approximation of what we really want.
66 if (FD->getStorageClass() == FunctionDecl::Static)
67 CurFn->setLinkage(llvm::Function::InternalLinkage);
68 else if (FD->isInline())
69 CurFn->setLinkage(llvm::Function::WeakLinkage);
70
Reid Spencer5f016e22007-07-11 17:01:13 +000071 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
72
73 Builder.SetInsertPoint(EntryBB);
74
75 // Create a marker to make it easy to insert allocas into the entryblock
76 // later.
77 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
78 AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
79
80 // Emit allocs for param decls. Give the LLVM Argument nodes names.
81 llvm::Function::arg_iterator AI = CurFn->arg_begin();
82
83 // Name the struct return argument.
84 if (hasAggregateLLVMType(FD->getResultType())) {
85 AI->setName("agg.result");
86 ++AI;
87 }
88
89 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
90 assert(AI != CurFn->arg_end() && "Argument mismatch!");
91 EmitParmDecl(*FD->getParamDecl(i), AI);
92 }
93
94 // Emit the function body.
95 EmitStmt(FD->getBody());
96
Devang Pateld9363c32007-09-28 21:49:18 +000097 // Emit a return for code that falls off the end. If insert point
98 // is a dummy block with no predecessors then remove the block itself.
99 llvm::BasicBlock *BB = Builder.GetInsertBlock();
100 if (isDummyBlock(BB))
101 BB->eraseFromParent();
102 else {
103 // FIXME: if this is C++ main, this should return 0.
104 if (CurFn->getReturnType() == llvm::Type::VoidTy)
105 Builder.CreateRetVoid();
106 else
107 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
108 }
Chris Lattnerda138702007-07-16 21:28:45 +0000109 assert(BreakContinueStack.empty() &&
110 "mismatched push/pop in break/continue stack!");
111
Chris Lattner5a2fa142007-12-02 06:32:24 +0000112 // Remove the AllocaInsertPt instruction, which is just a convenience for us.
113 AllocaInsertPt->eraseFromParent();
114 AllocaInsertPt = 0;
115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 // Verify that the function is well formed.
117 assert(!verifyFunction(*CurFn));
118}
119
Devang Pateld9363c32007-09-28 21:49:18 +0000120/// isDummyBlock - Return true if BB is an empty basic block
121/// with no predecessors.
122bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
123 if (BB->empty() && pred_begin(BB) == pred_end(BB))
124 return true;
125 return false;
126}
127
Devang Patel51b09f22007-10-04 23:45:31 +0000128/// StartBlock - Start new block named N. If insert block is a dummy block
129/// then reuse it.
130void CodeGenFunction::StartBlock(const char *N) {
131 llvm::BasicBlock *BB = Builder.GetInsertBlock();
132 if (!isDummyBlock(BB))
133 EmitBlock(new llvm::BasicBlock(N));
134 else
135 BB->setName(N);
136}
137
Devang Patel88a981b2007-11-01 19:11:01 +0000138/// getCGRecordLayout - Return record layout info.
139const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT,
140 QualType RTy) {
Devang Patelb84a06e2007-10-23 02:10:49 +0000141 assert (isa<RecordType>(RTy)
142 && "Unexpected type. RecordType expected here.");
143
144 const llvm::Type *Ty = ConvertType(RTy);
145 assert (Ty && "Unable to find llvm::Type");
146
Devang Patel88a981b2007-11-01 19:11:01 +0000147 return CGT.getCGRecordLayout(Ty);
Devang Patelb84a06e2007-10-23 02:10:49 +0000148}
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000149
150/// WarnUnsupported - Print out a warning that codegen doesn't support the
151/// specified stmt yet.
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000152void CodeGenFunction::WarnUnsupported(const Stmt *S, const char *Type) {
Chris Lattner2c8569d2007-12-02 07:19:18 +0000153 CGM.WarnUnsupported(S, Type);
Chris Lattnerdc5e8262007-12-02 01:43:38 +0000154}
155