blob: a1a498b082825f6a40d0459a43cd0044b62654e8 [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"
Chris Lattner8f32f712007-07-14 00:23:28 +000019#include "llvm/Constants.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/GlobalVariable.h"
23#include "llvm/Intrinsics.h"
24using namespace clang;
25using namespace CodeGen;
26
27
28CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M)
Chris Lattnerd2d2a112007-07-14 01:29:45 +000029 : Context(C), TheModule(M), Types(C) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000030
31llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const Decl *D) {
32 // See if it is already in the map.
33 llvm::Constant *&Entry = GlobalDeclMap[D];
34 if (Entry) return Entry;
35
36 QualType ASTTy = cast<ValueDecl>(D)->getType();
37 const llvm::Type *Ty = getTypes().ConvertType(ASTTy);
38 if (isa<FunctionDecl>(D)) {
39 const llvm::FunctionType *FTy = cast<llvm::FunctionType>(Ty);
40 // FIXME: param attributes for sext/zext etc.
41 return Entry = new llvm::Function(FTy, llvm::Function::ExternalLinkage,
42 D->getName(), &getModule());
43 }
44
45 assert(isa<FileVarDecl>(D) && "Unknown global decl!");
46
47 return Entry = new llvm::GlobalVariable(Ty, false,
48 llvm::GlobalValue::ExternalLinkage,
49 0, D->getName(), &getModule());
50}
51
Chris Lattner88a69ad2007-07-13 05:13:43 +000052void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
Reid Spencer5f016e22007-07-11 17:01:13 +000053 // If this is not a prototype, emit the body.
54 if (FD->getBody())
55 CodeGenFunction(*this).GenerateCode(FD);
56}
57
Chris Lattner88a69ad2007-07-13 05:13:43 +000058void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
59 llvm::GlobalVariable *GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalDecl(D));
60
61 // If the storage class is external and there is no initializer, just leave it
62 // as a declaration.
63 if (D->getStorageClass() == VarDecl::Extern && D->getInit() == 0)
64 return;
65
66 // Otherwise, convert the initializer, or use zero if appropriate.
Chris Lattner8f32f712007-07-14 00:23:28 +000067 llvm::Constant *Init = 0;
68 if (D->getInit() == 0) {
Chris Lattner88a69ad2007-07-13 05:13:43 +000069 Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
Chris Lattner8f32f712007-07-14 00:23:28 +000070 } else if (D->getType()->isIntegerType()) {
Chris Lattnerd2d2a112007-07-14 01:29:45 +000071 llvm::APSInt Value(getContext().getTypeSize(D->getInit()->getType(),
72 SourceLocation()));
Chris Lattner590b6642007-07-15 23:26:56 +000073 if (D->getInit()->isIntegerConstantExpr(Value, Context))
Chris Lattner8f32f712007-07-14 00:23:28 +000074 Init = llvm::ConstantInt::get(Value);
75 }
76 assert(Init && "FIXME: Global variable initializers unimp!");
77
Chris Lattner88a69ad2007-07-13 05:13:43 +000078 GV->setInitializer(Init);
79
80 // Set the llvm linkage type as appropriate.
81 // FIXME: This isn't right. This should handle common linkage and other
82 // stuff.
83 switch (D->getStorageClass()) {
84 case VarDecl::Auto:
85 case VarDecl::Register:
86 assert(0 && "Can't have auto or register globals");
87 case VarDecl::None:
88 case VarDecl::Extern:
89 // todo: common
90 break;
91 case VarDecl::Static:
92 GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
93 break;
94 }
95}
Reid Spencer5f016e22007-07-11 17:01:13 +000096
Chris Lattner32b266c2007-07-14 00:16:50 +000097/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
98/// declarator chain.
99void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) {
100 for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator()))
101 EmitGlobalVar(D);
102}
Reid Spencer5f016e22007-07-11 17:01:13 +0000103
104llvm::Function *CodeGenModule::getMemCpyFn() {
105 if (MemCpyFn) return MemCpyFn;
106 llvm::Intrinsic::ID IID;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000107 uint64_t Size; unsigned Align;
108 Context.Target.getPointerInfo(Size, Align, SourceLocation());
109 switch (Size) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 default: assert(0 && "Unknown ptr width");
111 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
112 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
113 }
114 return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID);
115}