blob: 4bf4b86bcb63c30f551a78f864993610b28b0d9a [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 Lattner09153c02007-06-22 18:48:09 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
Chris Lattner09153c02007-06-22 18:48:09 +000022#include "llvm/Intrinsics.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000023using namespace clang;
24using namespace CodeGen;
25
26
Chris Lattner2ccb73b2007-06-16 00:16:26 +000027CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M)
28 : Context(C), TheModule(M), Types(C.Target) {}
29
Chris Lattnerb6984c42007-06-20 04:44:43 +000030llvm::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();
Chris Lattnerf033c142007-06-22 19:05:19 +000036 const llvm::Type *Ty = getTypes().ConvertType(ASTTy);
Chris Lattnerb6984c42007-06-20 04:44:43 +000037 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 Lattnerd14bfa92007-07-13 05:13:43 +000051void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
Chris Lattnerd760e462007-06-15 21:16:23 +000052 // If this is not a prototype, emit the body.
53 if (FD->getBody())
54 CodeGenFunction(*this).GenerateCode(FD);
Chris Lattnerbed31442007-05-28 01:07:47 +000055}
Chris Lattner09153c02007-06-22 18:48:09 +000056
Chris Lattnerd14bfa92007-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}
Chris Lattner09153c02007-06-22 18:48:09 +000090
Chris Lattner6ee31f52007-07-14 00:16:50 +000091/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
92/// declarator chain.
93void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) {
94 for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator()))
95 EmitGlobalVar(D);
96}
Chris Lattner09153c02007-06-22 18:48:09 +000097
98llvm::Function *CodeGenModule::getMemCpyFn() {
99 if (MemCpyFn) return MemCpyFn;
100 llvm::Intrinsic::ID IID;
101 switch (Context.Target.getPointerWidth(SourceLocation())) {
102 default: assert(0 && "Unknown ptr width");
103 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
104 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
105 }
106 return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID);
107}