blob: d4068c675328c1f83bb1e0d1110275c7635734e3 [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
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-module state used while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
Chris Lattner2ccb73b2007-06-16 00:16:26 +000016#include "clang/AST/ASTContext.h"
Chris Lattnerd760e462007-06-15 21:16:23 +000017#include "clang/AST/Decl.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/Function.h"
20#include "llvm/GlobalVariable.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000021using namespace clang;
22using namespace CodeGen;
23
24
Chris Lattner2ccb73b2007-06-16 00:16:26 +000025CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M)
26 : Context(C), TheModule(M), Types(C.Target) {}
27
Chris Lattnerb6984c42007-06-20 04:44:43 +000028llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const Decl *D) {
29 // See if it is already in the map.
30 llvm::Constant *&Entry = GlobalDeclMap[D];
31 if (Entry) return Entry;
32
33 QualType ASTTy = cast<ValueDecl>(D)->getType();
34 const llvm::Type *Ty = getTypes().ConvertType(ASTTy, D->getLocation());
35 if (isa<FunctionDecl>(D)) {
36 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
37 // FIXME: param attributes for sext/zext etc.
38 return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage,
39 D->getName(), &getModule());
40 }
41
42 assert(isa<FileVarDecl>(D) && "Unknown global decl!");
43
44 return Entry = new llvm::GlobalVariable(Ty, false,
45 llvm::GlobalValue::ExternalLinkage,
46 0, D->getName(), &getModule());
47}
48
Chris Lattnerbed31442007-05-28 01:07:47 +000049void CodeGenModule::EmitFunction(FunctionDecl *FD) {
Chris Lattnerd760e462007-06-15 21:16:23 +000050 // If this is not a prototype, emit the body.
51 if (FD->getBody())
52 CodeGenFunction(*this).GenerateCode(FD);
Chris Lattnerbed31442007-05-28 01:07:47 +000053}