blob: a6a887f90fad1b8e7e4d218aaf6fbddbfa3e8c78 [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
30llvm::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();
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 Lattnerd2d2a112007-07-14 01:29:45 +000070 llvm::APSInt Value(getContext().getTypeSize(D->getInit()->getType(),
71 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.
130 assert(getModule().getFunction(Name) == 0 && "FIXME: Name collision");
131
132 // FIXME: param attributes for sext/zext etc.
133 return FunctionSlot = new llvm::Function(Ty, llvm::Function::ExternalLinkage,
134 Name, &getModule());
135}
136
137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138llvm::Function *CodeGenModule::getMemCpyFn() {
139 if (MemCpyFn) return MemCpyFn;
140 llvm::Intrinsic::ID IID;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000141 uint64_t Size; unsigned Align;
142 Context.Target.getPointerInfo(Size, Align, SourceLocation());
143 switch (Size) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 default: assert(0 && "Unknown ptr width");
145 case 32: IID = llvm::Intrinsic::memcpy_i32; break;
146 case 64: IID = llvm::Intrinsic::memcpy_i64; break;
147 }
148 return MemCpyFn = llvm::Intrinsic::getDeclaration(&TheModule, IID);
149}
Anders Carlssonc9e20912007-08-21 00:21:21 +0000150
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000151llvm::Constant *CodeGenModule::
152GetAddrOfConstantCFString(const std::string &str) {
Anders Carlssonc9e20912007-08-21 00:21:21 +0000153 llvm::StringMapEntry<llvm::Constant *> &Entry =
154 CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]);
155
156 if (Entry.getValue())
157 return Entry.getValue();
158
159 std::vector<llvm::Constant*> Fields;
160
161 if (!CFConstantStringClassRef) {
162 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
163 Ty = llvm::ArrayType::get(Ty, 0);
164
165 CFConstantStringClassRef =
166 new llvm::GlobalVariable(Ty, false,
167 llvm::GlobalVariable::ExternalLinkage, 0,
168 "__CFConstantStringClassReference",
169 &getModule());
170 }
171
172 // Class pointer.
173 llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty);
174 llvm::Constant *Zeros[] = { Zero, Zero };
175 llvm::Constant *C =
176 llvm::ConstantExpr::getGetElementPtr(CFConstantStringClassRef, Zeros, 2);
177 Fields.push_back(C);
178
179 // Flags.
180 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
181 Fields.push_back(llvm::ConstantInt::get(Ty, 1992));
182
183 // String pointer.
184 C = llvm::ConstantArray::get(str);
185 C = new llvm::GlobalVariable(C->getType(), true,
186 llvm::GlobalValue::InternalLinkage,
187 C, ".str", &getModule());
188
189 C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2);
190 Fields.push_back(C);
191
192 // String length.
193 Ty = getTypes().ConvertType(getContext().LongTy);
194 Fields.push_back(llvm::ConstantInt::get(Ty, str.length()));
195
196 // The struct.
197 Ty = getTypes().ConvertType(getContext().getCFConstantStringType());
198 C = llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Fields);
199 C = new llvm::GlobalVariable(C->getType(), true,
200 llvm::GlobalVariable::InternalLinkage,
201 C, "", &getModule());
202
203 Entry.setValue(C);
204 return C;
205}