blob: b315ab40b4fc2143f604685c94a074984f965bac [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +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"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000016#include "clang/Basic/TargetInfo.h"
17#include "clang/AST/AST.h"
Chris Lattner208ae962007-05-30 17:57:17 +000018#include "llvm/Constants.h"
Chris Lattnerd1af2d22007-05-29 23:17:50 +000019#include "llvm/DerivedTypes.h"
Chris Lattner308f4312007-05-29 23:50:05 +000020#include "llvm/Function.h"
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +000021#include "llvm/Analysis/Verifier.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000022using namespace clang;
23using namespace CodeGen;
24
Chris Lattnerd1af2d22007-05-29 23:17:50 +000025CodeGenFunction::CodeGenFunction(CodeGenModule &cgm)
Chris Lattner2ccb73b2007-06-16 00:16:26 +000026 : CGM(cgm), Target(CGM.getContext().Target) {}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000027
Chris Lattner6db1fb82007-06-02 22:49:07 +000028ASTContext &CodeGenFunction::getContext() const {
29 return CGM.getContext();
30}
31
Chris Lattnerd1af2d22007-05-29 23:17:50 +000032
Chris Lattnerac248202007-05-30 00:13:02 +000033llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) {
Chris Lattner23b7eb62007-06-15 23:05:46 +000034 llvm::BasicBlock *&BB = LabelMap[S];
Chris Lattnerac248202007-05-30 00:13:02 +000035 if (BB) return BB;
36
37 // Create, but don't insert, the new block.
Chris Lattner23b7eb62007-06-15 23:05:46 +000038 return BB = new llvm::BasicBlock(S->getName());
Chris Lattnerac248202007-05-30 00:13:02 +000039}
40
41
Chris Lattnerf033c142007-06-22 19:05:19 +000042const llvm::Type *CodeGenFunction::ConvertType(QualType T) {
43 return CGM.getTypes().ConvertType(T);
Chris Lattner45bb9142007-06-09 02:28:57 +000044}
Chris Lattnerd1af2d22007-05-29 23:17:50 +000045
Chris Lattner308f4312007-05-29 23:50:05 +000046void CodeGenFunction::GenerateCode(const FunctionDecl *FD) {
Chris Lattnerf033c142007-06-22 19:05:19 +000047 LLVMIntTy = ConvertType(getContext().IntTy);
48 LLVMPointerWidth = Target.getPointerWidth(SourceLocation());
Chris Lattner6db1fb82007-06-02 22:49:07 +000049
Chris Lattnerb6984c42007-06-20 04:44:43 +000050 CurFn = cast<llvm::Function>(CGM.GetAddrOfGlobalDecl(FD));
Chris Lattner3f3dbee2007-06-02 03:19:07 +000051 CurFuncDecl = FD;
Chris Lattnerb6984c42007-06-20 04:44:43 +000052
53 // TODO: Set up linkage and many other things.
54 assert(CurFn->isDeclaration() && "Function already has body?");
Chris Lattnerd1af2d22007-05-29 23:17:50 +000055
Chris Lattner23b7eb62007-06-15 23:05:46 +000056 llvm::BasicBlock *EntryBB = new llvm::BasicBlock("entry", CurFn);
Chris Lattner308f4312007-05-29 23:50:05 +000057
Chris Lattner308f4312007-05-29 23:50:05 +000058 Builder.SetInsertPoint(EntryBB);
Chris Lattner03df1222007-06-02 04:53:11 +000059
60 // Create a marker to make it easy to insert allocas into the entryblock
61 // later.
Chris Lattner23b7eb62007-06-15 23:05:46 +000062 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
63 AllocaInsertPt = Builder.CreateBitCast(Undef,llvm::Type::Int32Ty, "allocapt");
Chris Lattner308f4312007-05-29 23:50:05 +000064
Chris Lattner53621a52007-06-13 20:44:40 +000065 // Emit allocs for param decls.
66 llvm::Function::arg_iterator AI = CurFn->arg_begin();
67 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
68 assert(AI != CurFn->arg_end() && "Argument mismatch!");
69 EmitParmDecl(*FD->getParamDecl(i), AI);
70 }
Chris Lattner84915fa2007-06-02 04:16:21 +000071
72 // Emit the function body.
Chris Lattner308f4312007-05-29 23:50:05 +000073 EmitStmt(FD->getBody());
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +000074
Chris Lattneradb63722007-06-02 03:02:07 +000075 // Emit a return for code that falls off the end.
76 // FIXME: if this is C++ main, this should return 0.
Chris Lattnerb6984c42007-06-20 04:44:43 +000077 if (CurFn->getReturnType() == llvm::Type::VoidTy)
Chris Lattneradb63722007-06-02 03:02:07 +000078 Builder.CreateRetVoid();
79 else
Chris Lattnerb6984c42007-06-20 04:44:43 +000080 Builder.CreateRet(llvm::UndefValue::get(CurFn->getReturnType()));
Chris Lattnerdc6e3fe2007-05-30 22:55:31 +000081
82 // Verify that the function is well formed.
83 assert(!verifyFunction(*CurFn));
Chris Lattnerd1af2d22007-05-29 23:17:50 +000084}
Chris Lattner308f4312007-05-29 23:50:05 +000085