blob: 262469e34bec10df220529dc3ca97836b8fb2b43 [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 Lattner9d3b0e02007-07-14 00:23:28 +000019#include "llvm/Constants.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000020#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/GlobalVariable.h"
Chris Lattner09153c02007-06-22 18:48:09 +000023#include "llvm/Intrinsics.h"
Chris Lattnerbed31442007-05-28 01:07:47 +000024using namespace clang;
25using namespace CodeGen;
26
27
Chris Lattner2ccb73b2007-06-16 00:16:26 +000028CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M)
Chris Lattner4481b422007-07-14 01:29:45 +000029 : Context(C), TheModule(M), Types(C) {}
Chris Lattner2ccb73b2007-06-16 00:16:26 +000030
Chris Lattnerb6984c42007-06-20 04:44:43 +000031llvm::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();
Chris Lattnerf033c142007-06-22 19:05:19 +000037 const llvm::Type *Ty = getTypes().ConvertType(ASTTy);
Chris Lattnerb6984c42007-06-20 04:44:43 +000038 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 Lattnerd14bfa92007-07-13 05:13:43 +000052void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
Chris Lattnerd760e462007-06-15 21:16:23 +000053 // If this is not a prototype, emit the body.
54 if (FD->getBody())
55 CodeGenFunction(*this).GenerateCode(FD);
Chris Lattnerbed31442007-05-28 01:07:47 +000056}
Chris Lattner09153c02007-06-22 18:48:09 +000057
Chris Lattnerd14bfa92007-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 Lattner9d3b0e02007-07-14 00:23:28 +000067 llvm::Constant *Init = 0;
68 if (D->getInit() == 0) {
Chris Lattnerd14bfa92007-07-13 05:13:43 +000069 Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
Chris Lattner9d3b0e02007-07-14 00:23:28 +000070 } else if (D->getType()->isIntegerType()) {
Chris Lattner4481b422007-07-14 01:29:45 +000071 llvm::APSInt Value(getContext().getTypeSize(D->getInit()->getType(),
72 SourceLocation()));
Chris Lattner9d3b0e02007-07-14 00:23:28 +000073 if (D->getInit()->isIntegerConstantExpr(Value))
74 Init = llvm::ConstantInt::get(Value);
75 }
76 assert(Init && "FIXME: Global variable initializers unimp!");
77
Chris Lattnerd14bfa92007-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}
Chris Lattner09153c02007-06-22 18:48:09 +000096
Chris Lattner6ee31f52007-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}
Chris Lattner09153c02007-06-22 18:48:09 +0000103
104llvm::Function *CodeGenModule::getMemCpyFn() {
105 if (MemCpyFn) return MemCpyFn;
106 llvm::Intrinsic::ID IID;
Chris Lattner4481b422007-07-14 01:29:45 +0000107 uint64_t Size; unsigned Align;
108 Context.Target.getPointerInfo(Size, Align, SourceLocation());
109 switch (Size) {
Chris Lattner09153c02007-06-22 18:48:09 +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}