blob: e8a00c89ea0b6a229440df03b52ed906e89a80b8 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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 Patel97299362007-09-28 21:49:18 +000022#include "llvm/Support/CFG.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24using namespace CodeGen;
25
26CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
27 : CGM(cgm), Target(CGM.getContext().Target) {}
28
29ASTContext &CodeGenFunction::getContext() const {
30 return CGM.getContext();
31}
32
33
34llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
35 llvm::BasicBlock *&BB = LabelMap[S];
36 if (BB) return BB;
37
38 // Create, but don't insert, the new block.
39 return BB = new llvm::BasicBlock(S->getName());
40}
41
42
43const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
44 return CGM.getTypes().ConvertType(T);
45}
46
47bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
48 return !T->isRealType() && !T->isPointerType() && !T->isVoidType() &&
Chris Lattner59802042007-08-10 17:02:28 +000049 !T->isVectorType() && !T->isFunctionType();
Chris Lattner4b009652007-07-25 00:24:17 +000050}
51
52
53void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
54 LLVMIntTy = ConvertType(getContext().IntTy);
Chris Lattnera96e0d82007-09-04 02:34:27 +000055 LLVMPointerWidth = static_cast<unsigned>(
Chris Lattner4b009652007-07-25 00:24:17 +000056 getContext().getTypeSize(getContext().getPointerType(getContext().VoidTy),
Chris Lattnera96e0d82007-09-04 02:34:27 +000057 SourceLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +000058
59 CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD));
60 CurFuncDecl = FD;
61
62 // TODO: Set up linkage and many other things.
63 assert(CurFn->isDeclaration() && "Function already has body?");
64
65 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
66
67 Builder.SetInsertPoint(EntryBB);
68
69 // Create a marker to make it easy to insert allocas into the entryblock
70 // later.
71 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
72 AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
73
74 // Emit allocs for param decls. Give the LLVM Argument nodes names.
75 llvm::Function::arg_iterator AI = CurFn->arg_begin();
76
77 // Name the struct return argument.
78 if (hasAggregateLLVMType(FD->getResultType())) {
79 AI->setName("agg.result");
80 ++AI;
81 }
82
83 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
84 assert(AI != CurFn->arg_end() && "Argument mismatch!");
85 EmitParmDecl(*FD->getParamDecl(i), AI);
86 }
87
88 // Emit the function body.
89 EmitStmt(FD->getBody());
90
Devang Patel97299362007-09-28 21:49:18 +000091 // Emit a return for code that falls off the end. If insert point
92 // is a dummy block with no predecessors then remove the block itself.
93 llvm::BasicBlock *BB = Builder.GetInsertBlock();
94 if (isDummyBlock(BB))
95 BB->eraseFromParent();
96 else {
97 // FIXME: if this is C++ main, this should return 0.
98 if (CurFn->getReturnType() == llvm::Type::VoidTy)
99 Builder.CreateRetVoid();
100 else
101 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
102 }
Chris Lattner4b009652007-07-25 00:24:17 +0000103 assert(BreakContinueStack.empty() &&
104 "mismatched push/pop in break/continue stack!");
105
106 // Verify that the function is well formed.
107 assert(!verifyFunction(*CurFn));
108}
109
Devang Patel97299362007-09-28 21:49:18 +0000110/// isDummyBlock - Return true if BB is an empty basic block
111/// with no predecessors.
112bool CodeGenFunction::isDummyBlock(const llvm::BasicBlock *BB) {
113 if (BB->empty() && pred_begin(BB) == pred_end(BB))
114 return true;
115 return false;
116}
117
Devang Patele58e0802007-10-04 23:45:31 +0000118/// StartBlock - Start new block named N. If insert block is a dummy block
119/// then reuse it.
120void CodeGenFunction::StartBlock(const char *N) {
121 llvm::BasicBlock *BB = Builder.GetInsertBlock();
122 if (!isDummyBlock(BB))
123 EmitBlock(new llvm::BasicBlock(N));
124 else
125 BB->setName(N);
126}
127