blob: b610d3eb27ef62c252ce340cb8b03fb40dfb5c86 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/Basic/TargetInfo.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
22#include "llvm/Intrinsics.h"
23using namespace clang;
24using namespace CodeGen;
25
26
27CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M)
28 : Context(C), TheModule(M), Types(C.Target) {}
29
30llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const Decl *D) {
31 // See if it is already in the map.
32 llvm::Constant *&Entry = GlobalDeclMap[D];
33 if (Entry) return Entry;
34
35 QualType ASTTy = cast<ValueDecl>(D)->getType();
36 const llvm::Type *Ty = getTypes().ConvertType(ASTTy);
37 if (isa<FunctionDecl>(D)) {
38 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
39 // FIXME: param attributes for sext/zext etc.
40 return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage,
41 D->getName(), &getModule());
42 }
43
44 assert(isa<FileVarDecl>(D) && "Unknown global decl!");
45
46 return Entry = new llvm::GlobalVariable(Ty, false,
47 llvm::GlobalValue::ExternalLinkage,
48 0, D->getName(), &getModule());
49}
50
Chris Lattner88a69ad2007-07-13 05:13:43 +000051void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +000052 // If this is not a prototype, emit the body.
53 if (FD->getBody())
54 CodeGenFunction(*this).GenerateCode(FD);
55}
56
Chris Lattner88a69ad2007-07-13 05:13:43 +000057void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
58 llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D));
59
60 // If the storage class is external and there is no initializer, just leave it
61 // as a declaration.
62 if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
63 return;
64
65 // Otherwise, convert the initializer, or use zero if appropriate.
66 llvm::Constant *Init;
67 if (D->getInit() == 0)
68 Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
69 else
70 assert(D->getInit() == 0 && "FIXME: Global variable initializers unimp!");
71
72 GV->setInitializer(Init);
73
74 // Set the llvm linkage type as appropriate.
75 // FIXME: This isn't right. This should handle common linkage and other
76 // stuff.
77 switch (D->getStorageClass()) {
78 case VarDecl::Auto:
79 case VarDecl::Register:
80 assert(0 && "Can't have auto or register globals");
81 case VarDecl::None:
82 case VarDecl::Extern:
83 // todo: common
84 break;
85 case VarDecl::Static:
86 GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
87 break;
88 }
89}
Reid Spencer5f016e22007-07-11 17:01:13 +000090
91
92llvm::Function *CodeGenModule::getMemCpyFn() {
93 if (MemCpyFn) return MemCpyFn;
94 llvm::Intrinsic::ID IID;
95 switch (Context.Target.getPointerWidth(SourceLocation())) {
96 default: assert(0 && "Unknown ptr width");
97 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
98 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
99 }
100 return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID);
101}