blob: 74c7d43e1d2f3dfaa425c31ffbe2313a2e828bbc [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)
Anders Carlssonc9e20912007-08-21 00:21:21 +000029 : Context(C), TheModule(M), Types(C, M), CFConstantStringClassRef(0) {}
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}
Anders Carlssonc9e20912007-08-21 00:21:21 +0000116
117llvm::Constant *CodeGenModule::GetAddrOfConstantCFString(const std::string &str)
118{
119 llvm::StringMapEntry<llvm::Constant *> &Entry =
120 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
121
122 if (Entry.getValue())
123 return Entry.getValue();
124
125 std::vector<llvm::Constant*> Fields;
126
127 if (!CFConstantStringClassRef) {
128 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
129 Ty = llvm::ArrayType::get(Ty, 0);
130
131 CFConstantStringClassRef =
132 new llvm::GlobalVariable(Ty, false,
133 llvm::GlobalVariable::ExternalLinkage, 0,
134 "__CFConstantStringClassReference",
135 &getModule());
136 }
137
138 // Class pointer.
139 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
140 llvm::Constant *Zeros[] = { Zero, Zero };
141 llvm::Constant *C =
142 llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
143 Fields.push_back(C);
144
145 // Flags.
146 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
147 Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
148
149 // String pointer.
150 C = llvm::ConstantArray::get(str);
151 C = new llvm::GlobalVariable(C->getType(), true,
152 llvm::GlobalValue::InternalLinkage,
153 C, ".str", &getModule());
154
155 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
156 Fields.push_back(C);
157
158 // String length.
159 Ty = getTypes().ConvertType(getContext().LongTy);
160 Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
161
162 // The struct.
163 Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
164 C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
165 C = new llvm::GlobalVariable(C->getType(), true,
166 llvm::GlobalVariable::InternalLinkage,
167 C, "", &getModule());
168
169 Entry.setValue(C);
170 return C;
171}