blob: 662c3b513c93e9ea354bb7afff5103a38a09cef1 [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"
22using namespace clang;
23using namespace CodeGen;
24
25CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
26 : CGM(cgm), Target(CGM.getContext().Target) {}
27
28ASTContext &CodeGenFunction::getContext() const {
29 return CGM.getContext();
30}
31
32
33llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
34 llvm::BasicBlock *&BB = LabelMap[S];
35 if (BB) return BB;
36
37 // Create, but don't insert, the new block.
38 return BB = new llvm::BasicBlock(S->getName());
39}
40
41
42const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
43 return CGM.getTypes().ConvertType(T);
44}
45
46bool CodeGenFunction::hasAggregateLLVMType(QualType T) {
47 return !T->isRealType() && !T->isPointerType() && !T->isVoidType() &&
48 !T->isVectorType();
49}
50
51
52void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
53 LLVMIntTy = ConvertType(getContext().IntTy);
54 LLVMPointerWidth = Target.getPointerWidth(SourceLocation());
55
56 CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD));
57 CurFuncDecl = FD;
58
59 // TODO: Set up linkage and many other things.
60 assert(CurFn->isDeclaration() && "Function already has body?");
61
62 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
63
64 Builder.SetInsertPoint(EntryBB);
65
66 // Create a marker to make it easy to insert allocas into the entryblock
67 // later.
68 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
69 AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
70
71 // Emit allocs for param decls. Give the LLVM Argument nodes names.
72 llvm::Function::arg_iterator AI = CurFn->arg_begin();
73
74 // Name the struct return argument.
75 if (hasAggregateLLVMType(FD->getResultType())) {
76 AI->setName("agg.result");
77 ++AI;
78 }
79
80 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
81 assert(AI != CurFn->arg_end() && "Argument mismatch!");
82 EmitParmDecl(*FD->getParamDecl(i), AI);
83 }
84
85 // Emit the function body.
86 EmitStmt(FD->getBody());
87
88 // Emit a return for code that falls off the end.
89 // FIXME: if this is C++ main, this should return 0.
90 if (CurFn->getReturnType() == llvm::Type::VoidTy)
91 Builder.CreateRetVoid();
92 else
93 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
94
95 // Verify that the function is well formed.
96 assert(!verifyFunction(*CurFn));
97}
98