Mercurial > projects > ldc
annotate gen/functions.cpp @ 106:5b5194b25f33 trunk
[svn r110] Fixed typeinfo for classes.
author | lindquist |
---|---|
date | Mon, 19 Nov 2007 06:01:48 +0100 |
parents | 027b8d8b71ec |
children | 288fe1029e1f |
rev | line source |
---|---|
100 | 1 #include "gen/llvm.h" |
2 | |
3 #include "mtype.h" | |
4 #include "aggregate.h" | |
5 #include "init.h" | |
6 #include "declaration.h" | |
7 #include "template.h" | |
8 #include "module.h" | |
9 #include "statement.h" | |
10 | |
11 #include "gen/irstate.h" | |
12 #include "gen/tollvm.h" | |
13 #include "gen/runtime.h" | |
14 #include "gen/arrays.h" | |
15 #include "gen/logger.h" | |
16 #include "gen/functions.h" | |
17 #include "gen/todebug.h" | |
18 #include "gen/classes.h" | |
19 | |
20 const llvm::FunctionType* DtoFunctionType(Type* type, const llvm::Type* thistype, bool ismain) | |
21 { | |
22 TypeFunction* f = (TypeFunction*)type; | |
23 assert(f != 0); | |
24 | |
25 if (type->llvmType != NULL) { | |
26 return llvm::cast<llvm::FunctionType>(type->llvmType->get()); | |
27 } | |
28 | |
29 bool typesafeVararg = false; | |
30 if (f->linkage == LINKd && f->varargs == 1) { | |
31 typesafeVararg = true; | |
32 } | |
33 | |
34 // return value type | |
35 const llvm::Type* rettype; | |
36 const llvm::Type* actualRettype; | |
37 Type* rt = f->next; | |
38 bool retinptr = false; | |
39 bool usesthis = false; | |
40 | |
41 if (ismain) { | |
42 rettype = llvm::Type::Int32Ty; | |
43 actualRettype = rettype; | |
44 } | |
45 else { | |
46 assert(rt); | |
47 if (DtoIsPassedByRef(rt)) { | |
48 rettype = llvm::PointerType::get(DtoType(rt)); | |
49 actualRettype = llvm::Type::VoidTy; | |
50 f->llvmRetInPtr = retinptr = true; | |
51 } | |
52 else { | |
53 rettype = DtoType(rt); | |
54 actualRettype = rettype; | |
55 } | |
56 } | |
57 | |
58 // parameter types | |
59 std::vector<const llvm::Type*> paramvec; | |
60 | |
61 if (retinptr) { | |
62 Logger::cout() << "returning through pointer parameter: " << *rettype << '\n'; | |
63 paramvec.push_back(rettype); | |
64 } | |
65 | |
66 if (thistype) { | |
67 paramvec.push_back(thistype); | |
68 usesthis = true; | |
69 } | |
70 | |
71 if (typesafeVararg) { | |
72 ClassDeclaration* ti = Type::typeinfo; | |
73 ti->toObjFile(); | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
74 DtoForceConstInitDsymbol(ti); |
100 | 75 assert(ti->llvmInitZ); |
76 std::vector<const llvm::Type*> types; | |
77 types.push_back(DtoSize_t()); | |
78 types.push_back(llvm::PointerType::get(llvm::PointerType::get(ti->llvmInitZ->getType()))); | |
79 const llvm::Type* t1 = llvm::StructType::get(types); | |
80 paramvec.push_back(llvm::PointerType::get(t1)); | |
81 paramvec.push_back(llvm::PointerType::get(llvm::Type::Int8Ty)); | |
82 } | |
83 | |
84 size_t n = Argument::dim(f->parameters); | |
85 | |
86 for (int i=0; i < n; ++i) { | |
87 Argument* arg = Argument::getNth(f->parameters, i); | |
88 // ensure scalar | |
89 Type* argT = DtoDType(arg->type); | |
90 assert(argT); | |
91 | |
92 if ((arg->storageClass & STCref) || (arg->storageClass & STCout)) { | |
93 //assert(arg->vardecl); | |
94 //arg->vardecl->refparam = true; | |
95 } | |
96 else | |
97 arg->llvmCopy = true; | |
98 | |
99 const llvm::Type* at = DtoType(argT); | |
100 if (isaStruct(at)) { | |
101 Logger::println("struct param"); | |
102 paramvec.push_back(llvm::PointerType::get(at)); | |
103 } | |
104 else if (isaArray(at)) { | |
105 Logger::println("sarray param"); | |
106 assert(argT->ty == Tsarray); | |
107 //paramvec.push_back(llvm::PointerType::get(at->getContainedType(0))); | |
108 paramvec.push_back(llvm::PointerType::get(at)); | |
109 } | |
110 else if (llvm::isa<llvm::OpaqueType>(at)) { | |
111 Logger::println("opaque param"); | |
112 assert(argT->ty == Tstruct || argT->ty == Tclass); | |
113 paramvec.push_back(llvm::PointerType::get(at)); | |
114 } | |
115 else { | |
116 if (!arg->llvmCopy) { | |
117 Logger::println("ref param"); | |
118 at = llvm::PointerType::get(at); | |
119 } | |
120 else { | |
121 Logger::println("in param"); | |
122 } | |
123 paramvec.push_back(at); | |
124 } | |
125 } | |
126 | |
127 // construct function type | |
128 bool isvararg = !typesafeVararg && f->varargs; | |
129 llvm::FunctionType* functype = llvm::FunctionType::get(actualRettype, paramvec, isvararg); | |
130 | |
131 f->llvmRetInPtr = retinptr; | |
132 f->llvmUsesThis = usesthis; | |
133 | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
134 //if (!f->llvmType) |
100 | 135 f->llvmType = new llvm::PATypeHolder(functype); |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
136 //else |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
137 //assert(functype == f->llvmType->get()); |
100 | 138 |
139 return functype; | |
140 } | |
141 | |
142 ////////////////////////////////////////////////////////////////////////////////////////// | |
143 | |
144 static const llvm::FunctionType* DtoVaFunctionType(FuncDeclaration* fdecl) | |
145 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
146 // type has already been resolved |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
147 if (fdecl->type->llvmType != 0) { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
148 return llvm::cast<llvm::FunctionType>(fdecl->type->llvmType->get()); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
149 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
150 |
100 | 151 TypeFunction* f = (TypeFunction*)fdecl->type; |
152 assert(f != 0); | |
153 | |
154 const llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
155 std::vector<const llvm::Type*> args; | |
156 | |
157 if (fdecl->llvmInternal == LLVMva_start) { | |
158 args.push_back(i8pty); | |
159 } | |
160 else if (fdecl->llvmInternal == LLVMva_intrinsic) { | |
161 size_t n = Argument::dim(f->parameters); | |
162 for (size_t i=0; i<n; ++i) { | |
163 args.push_back(i8pty); | |
164 } | |
165 } | |
166 else | |
167 assert(0); | |
168 | |
169 const llvm::FunctionType* fty = llvm::FunctionType::get(llvm::Type::VoidTy, args, false); | |
170 | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
171 f->llvmType = new llvm::PATypeHolder(fty); |
100 | 172 |
173 return fty; | |
174 } | |
175 | |
176 ////////////////////////////////////////////////////////////////////////////////////////// | |
177 | |
178 const llvm::FunctionType* DtoFunctionType(FuncDeclaration* fdecl) | |
179 { | |
180 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
181 return DtoVaFunctionType(fdecl); | |
182 } | |
183 | |
184 // type has already been resolved | |
185 if (fdecl->type->llvmType != 0) { | |
186 return llvm::cast<llvm::FunctionType>(fdecl->type->llvmType->get()); | |
187 } | |
188 | |
189 const llvm::Type* thisty = NULL; | |
190 if (fdecl->needThis()) { | |
191 if (AggregateDeclaration* ad = fdecl->isMember()) { | |
192 Logger::print("isMember = this is: %s\n", ad->type->toChars()); | |
193 thisty = DtoType(ad->type); | |
194 Logger::cout() << "this llvm type: " << *thisty << '\n'; | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
195 if (isaStruct(thisty) || (!gIR->structs.empty() && thisty == gIR->topstruct()->recty.get())) |
100 | 196 thisty = llvm::PointerType::get(thisty); |
197 } | |
198 else | |
199 assert(0); | |
200 } | |
201 else if (fdecl->isNested()) { | |
202 thisty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
203 } | |
204 | |
205 const llvm::FunctionType* functype = DtoFunctionType(fdecl->type, thisty, fdecl->isMain()); | |
206 | |
207 return functype; | |
208 } | |
209 | |
210 ////////////////////////////////////////////////////////////////////////////////////////// | |
211 | |
212 static llvm::Function* DtoDeclareVaFunction(FuncDeclaration* fdecl) | |
213 { | |
214 TypeFunction* f = (TypeFunction*)DtoDType(fdecl->type); | |
215 const llvm::FunctionType* fty = DtoVaFunctionType(fdecl); | |
216 llvm::Constant* fn = 0; | |
217 | |
218 if (fdecl->llvmInternal == LLVMva_start) { | |
219 fn = gIR->module->getOrInsertFunction("llvm.va_start", fty); | |
220 assert(fn); | |
221 } | |
222 else if (fdecl->llvmInternal == LLVMva_intrinsic) { | |
223 fn = gIR->module->getOrInsertFunction(fdecl->llvmInternal1, fty); | |
224 assert(fn); | |
225 } | |
226 else | |
227 assert(0); | |
228 | |
229 llvm::Function* func = llvm::dyn_cast<llvm::Function>(fn); | |
230 assert(func); | |
231 assert(func->isIntrinsic()); | |
232 fdecl->llvmValue = func; | |
233 return func; | |
234 } | |
235 | |
236 ////////////////////////////////////////////////////////////////////////////////////////// | |
237 | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
238 void DtoResolveFunction(FuncDeclaration* fdecl) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
239 { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
240 if (fdecl->llvmResolved) return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
241 fdecl->llvmResolved = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
242 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
243 Logger::println("DtoResolveFunction(%s)", fdecl->toPrettyChars()); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
244 LOG_SCOPE; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
245 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
246 if (fdecl->llvmRunTimeHack) { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
247 gIR->declareList.push_back(fdecl); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
248 return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
249 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
250 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
251 if (fdecl->isUnitTestDeclaration()) { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
252 Logger::attention("ignoring unittest declaration: %s", fdecl->toChars()); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
253 return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
254 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
255 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
256 if (fdecl->parent) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
257 if (TemplateInstance* tinst = fdecl->parent->isTemplateInstance()) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
258 { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
259 TemplateDeclaration* tempdecl = tinst->tempdecl; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
260 if (tempdecl->llvmInternal == LLVMva_arg) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
261 { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
262 Logger::println("magic va_arg found"); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
263 fdecl->llvmInternal = LLVMva_arg; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
264 fdecl->llvmDeclared = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
265 fdecl->llvmInitialized = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
266 fdecl->llvmDefined = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
267 return; // this gets mapped to an instruction so a declaration makes no sence |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
268 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
269 else if (tempdecl->llvmInternal == LLVMva_start) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
270 { |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
271 Logger::println("magic va_start found"); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
272 fdecl->llvmInternal = LLVMva_start; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
273 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
274 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
275 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
276 DtoFunctionType(fdecl); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
277 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
278 // queue declaration |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
279 gIR->declareList.push_back(fdecl); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
280 } |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
281 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
282 ////////////////////////////////////////////////////////////////////////////////////////// |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
283 |
100 | 284 void DtoDeclareFunction(FuncDeclaration* fdecl) |
285 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
286 if (fdecl->llvmDeclared) return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
287 fdecl->llvmDeclared = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
288 |
100 | 289 Logger::println("DtoDeclareFunction(%s)", fdecl->toPrettyChars()); |
290 LOG_SCOPE; | |
291 | |
292 if (fdecl->llvmRunTimeHack) { | |
293 Logger::println("runtime hack func chars: %s", fdecl->toChars()); | |
294 if (!fdecl->llvmValue) | |
295 fdecl->llvmValue = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars()); | |
296 return; | |
297 } | |
298 | |
299 bool declareOnly = false; | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
300 bool templInst = fdecl->parent && DtoIsTemplateInstance(fdecl->parent); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
301 if (!templInst && fdecl->getModule() != gIR->dmodule) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
302 declareOnly = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
303 else if (fdecl->llvmInternal == LLVMva_start) |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
304 declareOnly = true; |
100 | 305 |
306 if (!fdecl->llvmIRFunc) { | |
307 fdecl->llvmIRFunc = new IRFunction(fdecl); | |
308 } | |
309 | |
310 // mangled name | |
311 char* mangled_name; | |
312 if (fdecl->llvmInternal == LLVMintrinsic) | |
313 mangled_name = fdecl->llvmInternal1; | |
314 else | |
315 mangled_name = fdecl->mangle(); | |
316 | |
317 // unit test special handling | |
318 if (fdecl->isUnitTestDeclaration()) | |
319 { | |
320 assert(0 && "no unittests yet"); | |
321 /*const llvm::FunctionType* fnty = llvm::FunctionType::get(llvm::Type::VoidTy, std::vector<const llvm::Type*>(), false); | |
322 // make the function | |
323 llvm::Function* func = gIR->module->getFunction(mangled_name); | |
324 if (func == 0) | |
325 func = new llvm::Function(fnty,llvm::GlobalValue::InternalLinkage,mangled_name,gIR->module); | |
326 func->setCallingConv(llvm::CallingConv::Fast); | |
327 fdecl->llvmValue = func; | |
328 return func; | |
329 */ | |
330 } | |
331 | |
332 if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { | |
333 error("intrinsics cannot have function bodies"); | |
334 fatal(); | |
335 } | |
336 | |
337 llvm::Function* vafunc = 0; | |
338 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
339 vafunc = DtoDeclareVaFunction(fdecl); | |
340 } | |
341 | |
342 Type* t = DtoDType(fdecl->type); | |
343 TypeFunction* f = (TypeFunction*)t; | |
344 | |
345 // construct function | |
346 const llvm::FunctionType* functype = DtoFunctionType(fdecl); | |
347 llvm::Function* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); | |
348 if (!func) | |
349 func = new llvm::Function(functype, DtoLinkage(fdecl->protection, fdecl->storage_class), mangled_name, gIR->module); | |
350 else | |
351 assert(func->getFunctionType() == functype); | |
352 | |
353 // add func to IRFunc | |
354 fdecl->llvmIRFunc->func = func; | |
355 | |
356 // calling convention | |
357 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) | |
358 func->setCallingConv(DtoCallingConv(f->linkage)); | |
359 | |
360 // template instances should have weak linkage | |
361 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic && fdecl->parent && DtoIsTemplateInstance(fdecl->parent)) | |
362 func->setLinkage(llvm::GlobalValue::WeakLinkage); | |
363 | |
364 fdecl->llvmValue = func; | |
365 assert(llvm::isa<llvm::FunctionType>(f->llvmType->get())); | |
366 | |
367 if (fdecl->isMain()) { | |
368 gIR->mainFunc = func; | |
369 } | |
370 | |
371 // name parameters | |
372 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
373 int k = 0; | |
374 if (f->llvmRetInPtr) { | |
375 iarg->setName("retval"); | |
376 f->llvmRetArg = iarg; | |
377 ++iarg; | |
378 } | |
379 if (f->llvmUsesThis) { | |
380 iarg->setName("this"); | |
381 ++iarg; | |
382 } | |
383 int varargs = -1; | |
384 if (f->linkage == LINKd && f->varargs == 1) | |
385 varargs = 0; | |
386 for (; iarg != func->arg_end(); ++iarg) | |
387 { | |
388 Argument* arg = Argument::getNth(f->parameters, k++); | |
389 //arg->llvmValue = iarg; | |
390 //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
391 if (arg && arg->ident != 0) { | |
392 if (arg->vardecl) { | |
393 arg->vardecl->llvmValue = iarg; | |
394 } | |
395 iarg->setName(arg->ident->toChars()); | |
396 } | |
397 else if (!arg && varargs >= 0) { | |
398 if (varargs == 0) { | |
399 iarg->setName("_arguments"); | |
400 fdecl->llvmArguments = iarg; | |
401 } | |
402 else if (varargs == 1) { | |
403 iarg->setName("_argptr"); | |
404 fdecl->llvmArgPtr = iarg; | |
405 } | |
406 else | |
407 assert(0); | |
408 varargs++; | |
409 } | |
410 else { | |
411 iarg->setName("unnamed"); | |
412 } | |
413 } | |
414 | |
415 if (!declareOnly) | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
416 gIR->defineList.push_back(fdecl); |
100 | 417 |
418 Logger::cout() << "func decl: " << *func << '\n'; | |
419 } | |
420 | |
421 ////////////////////////////////////////////////////////////////////////////////////////// | |
422 | |
423 // TODO split this monster up | |
424 void DtoDefineFunc(FuncDeclaration* fd) | |
425 { | |
102
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
426 if (fd->llvmDefined) return; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
427 fd->llvmDefined = true; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
428 |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
429 Logger::println("DtoDefineFunc(%s)", fd->toPrettyChars()); |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
430 LOG_SCOPE; |
027b8d8b71ec
[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up.
lindquist
parents:
100
diff
changeset
|
431 |
100 | 432 // debug info |
433 if (global.params.symdebug) { | |
434 Module* mo = fd->getModule(); | |
435 if (!mo->llvmCompileUnit) { | |
436 mo->llvmCompileUnit = DtoDwarfCompileUnit(mo,false); | |
437 } | |
438 fd->llvmDwarfSubProgram = DtoDwarfSubProgram(fd, mo->llvmCompileUnit); | |
439 } | |
440 | |
441 Type* t = DtoDType(fd->type); | |
442 TypeFunction* f = (TypeFunction*)t; | |
443 | |
444 assert(f->llvmType); | |
445 llvm::Function* func = fd->llvmIRFunc->func; | |
446 const llvm::FunctionType* functype = func->getFunctionType(); | |
447 | |
448 // only members of the current module or template instances maybe be defined | |
449 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
450 { | |
451 fd->llvmDModule = gIR->dmodule; | |
452 | |
453 // handle static constructor / destructor | |
454 if (fd->isStaticCtorDeclaration() || fd->isStaticDtorDeclaration()) { | |
455 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
456 //Logger::cout() << "static ctor type: " << *sctor_type << '\n'; | |
457 | |
458 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(fd->llvmValue); | |
459 //Logger::cout() << "static ctor func: " << *sctor_func << '\n'; | |
460 | |
461 llvm::Constant* sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
462 | |
463 //Logger::cout() << "static ctor init: " << *sctor_init << '\n'; | |
464 | |
465 // output the llvm.global_ctors array | |
466 const char* varname = fd->isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
467 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
468 } | |
469 | |
470 // function definition | |
471 if (fd->fbody != 0) | |
472 { | |
473 Logger::println("Doing function body for: %s", fd->toChars()); | |
474 assert(fd->llvmIRFunc); | |
475 gIR->functions.push_back(fd->llvmIRFunc); | |
476 | |
477 // this handling | |
478 if (f->llvmUsesThis) { | |
479 Logger::println("uses this"); | |
480 if (f->llvmRetInPtr) | |
481 fd->llvmThisVar = ++func->arg_begin(); | |
482 else | |
483 fd->llvmThisVar = func->arg_begin(); | |
484 assert(fd->llvmThisVar != 0); | |
485 } | |
486 | |
487 if (fd->isMain()) | |
488 gIR->emitMain = true; | |
489 | |
490 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
491 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
492 | |
493 //assert(gIR->scopes.empty()); | |
494 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
495 | |
496 // create alloca point | |
497 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
498 gIR->func()->allocapoint = f->llvmAllocaPoint; | |
499 | |
500 // give arguments storage | |
501 size_t n = Argument::dim(f->parameters); | |
502 for (int i=0; i < n; ++i) { | |
503 Argument* arg = Argument::getNth(f->parameters, i); | |
504 if (arg && arg->vardecl) { | |
505 VarDeclaration* vd = arg->vardecl; | |
506 if (!vd->llvmNeedsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) | |
507 continue; | |
508 llvm::Value* a = vd->llvmValue; | |
509 assert(a); | |
510 std::string s(a->getName()); | |
511 Logger::println("giving argument '%s' storage", s.c_str()); | |
512 s.append("_storage"); | |
513 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,f->llvmAllocaPoint); | |
514 gIR->ir->CreateStore(a,v); | |
515 vd->llvmValue = v; | |
516 } | |
517 else { | |
518 Logger::attention("some unknown argument: %s", arg ? arg->toChars() : 0); | |
519 } | |
520 } | |
521 | |
522 // debug info | |
523 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
524 | |
525 llvm::Value* parentNested = NULL; | |
526 if (FuncDeclaration* fd2 = fd->toParent()->isFuncDeclaration()) { | |
527 parentNested = fd2->llvmNested; | |
528 } | |
529 | |
530 // construct nested variables struct | |
531 if (!fd->llvmNestedVars.empty() || parentNested) { | |
532 std::vector<const llvm::Type*> nestTypes; | |
533 int j = 0; | |
534 if (parentNested) { | |
535 nestTypes.push_back(parentNested->getType()); | |
536 j++; | |
537 } | |
538 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
539 VarDeclaration* vd = *i; | |
540 vd->llvmNestedIndex = j++; | |
541 if (vd->isParameter()) { | |
542 assert(vd->llvmValue); | |
543 nestTypes.push_back(vd->llvmValue->getType()); | |
544 } | |
545 else { | |
546 nestTypes.push_back(DtoType(vd->type)); | |
547 } | |
548 } | |
549 const llvm::StructType* nestSType = llvm::StructType::get(nestTypes); | |
550 Logger::cout() << "nested var struct has type:" << '\n' << *nestSType; | |
551 fd->llvmNested = new llvm::AllocaInst(nestSType,"nestedvars",f->llvmAllocaPoint); | |
552 if (parentNested) { | |
553 assert(fd->llvmThisVar); | |
554 llvm::Value* ptr = gIR->ir->CreateBitCast(fd->llvmThisVar, parentNested->getType(), "tmp"); | |
555 gIR->ir->CreateStore(ptr, DtoGEPi(fd->llvmNested, 0,0, "tmp")); | |
556 } | |
557 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
558 VarDeclaration* vd = *i; | |
559 if (vd->isParameter()) { | |
560 gIR->ir->CreateStore(vd->llvmValue, DtoGEPi(fd->llvmNested, 0, vd->llvmNestedIndex, "tmp")); | |
561 vd->llvmValue = fd->llvmNested; | |
562 } | |
563 } | |
564 } | |
565 | |
566 // copy _argptr to a memory location | |
567 if (f->linkage == LINKd && f->varargs == 1) | |
568 { | |
569 llvm::Value* argptrmem = new llvm::AllocaInst(fd->llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); | |
570 new llvm::StoreInst(fd->llvmArgPtr, argptrmem, gIR->scopebb()); | |
571 fd->llvmArgPtr = argptrmem; | |
572 } | |
573 | |
574 // output function body | |
575 fd->fbody->toIR(gIR); | |
576 | |
577 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
578 // in automatically, so we do it here. | |
579 if (!fd->isMain()) { | |
580 if (!gIR->scopereturned()) { | |
581 // pass the previous block into this block | |
582 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
583 if (func->getReturnType() == llvm::Type::VoidTy) { | |
584 new llvm::ReturnInst(gIR->scopebb()); | |
585 } | |
586 else { | |
587 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
588 } | |
589 } | |
590 } | |
591 | |
592 // erase alloca point | |
593 f->llvmAllocaPoint->eraseFromParent(); | |
594 f->llvmAllocaPoint = 0; | |
595 gIR->func()->allocapoint = 0; | |
596 | |
597 gIR->scopes.pop_back(); | |
598 | |
599 // get rid of the endentry block, it's never used | |
600 assert(!func->getBasicBlockList().empty()); | |
601 func->getBasicBlockList().pop_back(); | |
602 | |
603 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
604 // would be nice to figure out how to assert that this is correct | |
605 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
606 if (lastbb->empty()) { | |
607 if (lastbb->getNumUses() == 0) | |
608 lastbb->eraseFromParent(); | |
609 else { | |
610 new llvm::UnreachableInst(lastbb); | |
611 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
612 new llvm::ReturnInst(lastbb); | |
613 } | |
614 else { | |
615 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
616 }*/ | |
617 } | |
618 } | |
619 | |
620 gIR->functions.pop_back(); | |
621 } | |
622 } | |
623 } | |
624 | |
625 ////////////////////////////////////////////////////////////////////////////////////////// | |
626 | |
627 void DtoMain() | |
628 { | |
629 // emit main function llvm style | |
630 // int main(int argc, char**argv, char**env); | |
631 | |
632 assert(gIR != 0); | |
633 IRState& ir = *gIR; | |
634 | |
635 assert(ir.emitMain && ir.mainFunc); | |
636 | |
637 // parameter types | |
638 std::vector<const llvm::Type*> pvec; | |
639 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
640 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
641 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
642 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
643 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
644 | |
645 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
646 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
647 | |
648 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
649 | |
650 // call static ctors | |
651 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors"); | |
652 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); | |
653 | |
654 // call user main function | |
655 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType(); | |
656 llvm::CallInst* call; | |
657 if (mainty->getNumParams() > 0) | |
658 { | |
659 // main with arguments | |
660 assert(mainty->getNumParams() == 1); | |
661 std::vector<llvm::Value*> args; | |
662 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args"); | |
663 | |
664 llvm::Function::arg_iterator argi = func->arg_begin(); | |
665 args.push_back(argi++); | |
666 args.push_back(argi++); | |
667 | |
668 const llvm::Type* at = mainty->getParamType(0)->getContainedType(0); | |
669 llvm::Value* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt); | |
670 llvm::Value* a = new llvm::AllocaInst(at, "argarray", apt); | |
671 llvm::Value* ptr = DtoGEPi(a,0,0,"tmp",bb); | |
672 llvm::Value* v = args[0]; | |
673 if (v->getType() != DtoSize_t()) | |
674 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb); | |
675 new llvm::StoreInst(v,ptr,bb); | |
676 ptr = DtoGEPi(a,0,1,"tmp",bb); | |
677 new llvm::StoreInst(arr,ptr,bb); | |
678 args.push_back(a); | |
679 new llvm::CallInst(mfn, args.begin(), args.end(), "", bb); | |
680 call = new llvm::CallInst(ir.mainFunc,a,"ret",bb); | |
681 } | |
682 else | |
683 { | |
684 // main with no arguments | |
685 call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
686 } | |
687 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
688 | |
689 // call static dtors | |
690 fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors"); | |
691 new llvm::CallInst(fn,"",bb); | |
692 | |
693 // return | |
694 new llvm::ReturnInst(call,bb); | |
695 } | |
696 | |
697 ////////////////////////////////////////////////////////////////////////////////////////// |