blob: e14b738a3141221647c326c8cd1bba3966f5b7a6 [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"
Chris Lattnerbef20ac2007-08-31 04:31:45 +000021#include "llvm/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/Intrinsics.h"
23using namespace clang;
24using namespace CodeGen;
25
26
27CodeGenModule::CodeGenModule(ASTContext &C, llvm::Module &M)
Anders Carlssonc9e20912007-08-21 00:21:21 +000028 : Context(C), TheModule(M), Types(C, M), CFConstantStringClassRef(0) {}
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Steve Naroff8e74c932007-09-13 21:41:19 +000030llvm::Constant *CodeGenModule::GetAddrOfGlobalDecl(const ValueDecl *D) {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // 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.
Chris Lattner8f32f712007-07-14 00:23:28 +000066 llvm::Constant *Init = 0;
67 if (D->getInit() == 0) {
Chris Lattner88a69ad2007-07-13 05:13:43 +000068 Init = llvm::Constant::getNullValue(GV->getType()->getElementType());
Chris Lattner8f32f712007-07-14 00:23:28 +000069 } else if (D->getType()->isIntegerType()) {
Chris Lattner47f7dbf2007-09-04 02:34:27 +000070 llvm::APSInt Value(static_cast<unsigned>(
71 getContext().getTypeSize(D->getInit()->getType(), SourceLocation())));
Chris Lattner590b6642007-07-15 23:26:56 +000072 if (D->getInit()->isIntegerConstantExpr(Value, Context))
Chris Lattner8f32f712007-07-14 00:23:28 +000073 Init = llvm::ConstantInt::get(Value);
74 }
75 assert(Init && "FIXME: Global variable initializers unimp!");
76
Chris Lattner88a69ad2007-07-13 05:13:43 +000077 GV->setInitializer(Init);
78
79 // Set the llvm linkage type as appropriate.
80 // FIXME: This isn't right. This should handle common linkage and other
81 // stuff.
82 switch (D->getStorageClass()) {
83 case VarDecl::Auto:
84 case VarDecl::Register:
85 assert(0 && "Can't have auto or register globals");
86 case VarDecl::None:
87 case VarDecl::Extern:
88 // todo: common
89 break;
90 case VarDecl::Static:
91 GV->setLinkage(llvm::GlobalVariable::InternalLinkage);
92 break;
93 }
94}
Reid Spencer5f016e22007-07-11 17:01:13 +000095
Chris Lattner32b266c2007-07-14 00:16:50 +000096/// EmitGlobalVarDeclarator - Emit all the global vars attached to the specified
97/// declarator chain.
98void CodeGenModule::EmitGlobalVarDeclarator(const FileVarDecl *D) {
99 for (; D; D = cast_or_null<FileVarDecl>(D->getNextDeclarator()))
100 EmitGlobalVar(D);
101}
Reid Spencer5f016e22007-07-11 17:01:13 +0000102
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000103/// getBuiltinLibFunction
104llvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) {
105 if (BuiltinFunctions.size() <= BuiltinID)
106 BuiltinFunctions.resize(BuiltinID);
107
108 // Already available?
109 llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID];
110 if (FunctionSlot)
111 return FunctionSlot;
112
113 assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn");
114
115 // Get the name, skip over the __builtin_ prefix.
116 const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10;
117
118 // Get the type for the builtin.
119 QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context);
120 const llvm::FunctionType *Ty =
121 cast<llvm::FunctionType>(getTypes().ConvertType(Type));
122
123 // FIXME: This has a serious problem with code like this:
124 // void abs() {}
125 // ... __builtin_abs(x);
126 // The two versions of abs will collide. The fix is for the builtin to win,
127 // and for the existing one to be turned into a constantexpr cast of the
128 // builtin. In the case where the existing one is a static function, it
129 // should just be renamed.
Chris Lattnerc5e940f2007-08-31 04:44:06 +0000130 if (llvm::Function *Existing = getModule().getFunction(Name)) {
131 if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage())
132 return FunctionSlot = Existing;
133 assert(Existing == 0 && "FIXME: Name collision");
134 }
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000135
136 // FIXME: param attributes for sext/zext etc.
137 return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage,
138 Name, &getModule());
139}
140
141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142llvm::Function *CodeGenModule::getMemCpyFn() {
143 if (MemCpyFn) return MemCpyFn;
144 llvm::Intrinsic::ID IID;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000145 uint64_t Size; unsigned Align;
146 Context.Target.getPointerInfo(Size, Align, SourceLocation());
147 switch (Size) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 default: assert(0 && "Unknown ptr width");
149 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
150 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
151 }
152 return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID);
153}
Anders Carlssonc9e20912007-08-21 00:21:21 +0000154
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000155llvm::Constant *CodeGenModule::
156GetAddrOfConstantCFString(const std::string &str) {
Anders Carlssonc9e20912007-08-21 00:21:21 +0000157 llvm::StringMapEntry<llvm::Constant *> &Entry =
158 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
159
160 if (Entry.getValue())
161 return Entry.getValue();
162
163 std::vector<llvm::Constant*> Fields;
164
165 if (!CFConstantStringClassRef) {
166 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
167 Ty = llvm::ArrayType::get(Ty, 0);
168
169 CFConstantStringClassRef =
170 new llvm::GlobalVariable(Ty, false,
171 llvm::GlobalVariable::ExternalLinkage, 0,
172 "__CFConstantStringClassReference",
173 &getModule());
174 }
175
176 // Class pointer.
177 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
178 llvm::Constant *Zeros[] = { Zero, Zero };
179 llvm::Constant *C =
180 llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
181 Fields.push_back(C);
182
183 // Flags.
184 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
185 Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
186
187 // String pointer.
188 C = llvm::ConstantArray::get(str);
189 C = new llvm::GlobalVariable(C->getType(), true,
190 llvm::GlobalValue::InternalLinkage,
191 C, ".str", &getModule());
192
193 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
194 Fields.push_back(C);
195
196 // String length.
197 Ty = getTypes().ConvertType(getContext().LongTy);
198 Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
199
200 // The struct.
201 Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
202 C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
203 C = new llvm::GlobalVariable(C->getType(), true,
204 llvm::GlobalVariable::InternalLinkage,
205 C, "", &getModule());
206
207 Entry.setValue(C);
208 return C;
209}