Mercurial > projects > ldc
comparison gen/functions.cpp @ 100:5071469303d4 trunk
[svn r104] TONS OF FIXES.
Split up declaration, constant initializer gen and definition for globals, structs, classes and functions.
Improved ClassInfo support (not complete), not in vtable yet.
Fixed a bunch of forward reference problems.
Much more. Major commit! :)
author | lindquist |
---|---|
date | Fri, 16 Nov 2007 08:21:47 +0100 |
parents | |
children | 027b8d8b71ec |
comparison
equal
deleted
inserted
replaced
99:a676a7743642 | 100:5071469303d4 |
---|---|
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(); | |
74 DtoConstInitClass(ti); | |
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 | |
134 if (!f->llvmType) | |
135 f->llvmType = new llvm::PATypeHolder(functype); | |
136 else | |
137 assert(functype == f->llvmType->get()); | |
138 | |
139 return functype; | |
140 } | |
141 | |
142 ////////////////////////////////////////////////////////////////////////////////////////// | |
143 | |
144 static const llvm::FunctionType* DtoVaFunctionType(FuncDeclaration* fdecl) | |
145 { | |
146 TypeFunction* f = (TypeFunction*)fdecl->type; | |
147 assert(f != 0); | |
148 | |
149 const llvm::PointerType* i8pty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
150 std::vector<const llvm::Type*> args; | |
151 | |
152 if (fdecl->llvmInternal == LLVMva_start) { | |
153 args.push_back(i8pty); | |
154 } | |
155 else if (fdecl->llvmInternal == LLVMva_intrinsic) { | |
156 size_t n = Argument::dim(f->parameters); | |
157 for (size_t i=0; i<n; ++i) { | |
158 args.push_back(i8pty); | |
159 } | |
160 } | |
161 else | |
162 assert(0); | |
163 | |
164 const llvm::FunctionType* fty = llvm::FunctionType::get(llvm::Type::VoidTy, args, false); | |
165 | |
166 if (!f->llvmType) | |
167 f->llvmType = new llvm::PATypeHolder(fty); | |
168 else | |
169 assert(fty == f->llvmType->get()); | |
170 | |
171 return fty; | |
172 } | |
173 | |
174 ////////////////////////////////////////////////////////////////////////////////////////// | |
175 | |
176 const llvm::FunctionType* DtoFunctionType(FuncDeclaration* fdecl) | |
177 { | |
178 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
179 return DtoVaFunctionType(fdecl); | |
180 } | |
181 | |
182 // type has already been resolved | |
183 if (fdecl->type->llvmType != 0) { | |
184 return llvm::cast<llvm::FunctionType>(fdecl->type->llvmType->get()); | |
185 } | |
186 | |
187 const llvm::Type* thisty = NULL; | |
188 if (fdecl->needThis()) { | |
189 if (AggregateDeclaration* ad = fdecl->isMember()) { | |
190 Logger::print("isMember = this is: %s\n", ad->type->toChars()); | |
191 thisty = DtoType(ad->type); | |
192 Logger::cout() << "this llvm type: " << *thisty << '\n'; | |
193 if (isaStruct(thisty) || thisty == gIR->topstruct()->recty.get()) | |
194 thisty = llvm::PointerType::get(thisty); | |
195 } | |
196 else | |
197 assert(0); | |
198 } | |
199 else if (fdecl->isNested()) { | |
200 thisty = llvm::PointerType::get(llvm::Type::Int8Ty); | |
201 } | |
202 | |
203 const llvm::FunctionType* functype = DtoFunctionType(fdecl->type, thisty, fdecl->isMain()); | |
204 | |
205 return functype; | |
206 } | |
207 | |
208 ////////////////////////////////////////////////////////////////////////////////////////// | |
209 | |
210 static llvm::Function* DtoDeclareVaFunction(FuncDeclaration* fdecl) | |
211 { | |
212 TypeFunction* f = (TypeFunction*)DtoDType(fdecl->type); | |
213 const llvm::FunctionType* fty = DtoVaFunctionType(fdecl); | |
214 llvm::Constant* fn = 0; | |
215 | |
216 if (fdecl->llvmInternal == LLVMva_start) { | |
217 fn = gIR->module->getOrInsertFunction("llvm.va_start", fty); | |
218 assert(fn); | |
219 } | |
220 else if (fdecl->llvmInternal == LLVMva_intrinsic) { | |
221 fn = gIR->module->getOrInsertFunction(fdecl->llvmInternal1, fty); | |
222 assert(fn); | |
223 } | |
224 else | |
225 assert(0); | |
226 | |
227 llvm::Function* func = llvm::dyn_cast<llvm::Function>(fn); | |
228 assert(func); | |
229 assert(func->isIntrinsic()); | |
230 fdecl->llvmValue = func; | |
231 return func; | |
232 } | |
233 | |
234 ////////////////////////////////////////////////////////////////////////////////////////// | |
235 | |
236 void DtoDeclareFunction(FuncDeclaration* fdecl) | |
237 { | |
238 Logger::println("DtoDeclareFunction(%s)", fdecl->toPrettyChars()); | |
239 LOG_SCOPE; | |
240 | |
241 if (fdecl->llvmRunTimeHack) { | |
242 Logger::println("runtime hack func chars: %s", fdecl->toChars()); | |
243 if (!fdecl->llvmValue) | |
244 fdecl->llvmValue = LLVM_D_GetRuntimeFunction(gIR->module, fdecl->toChars()); | |
245 return; | |
246 } | |
247 | |
248 if (fdecl->isUnitTestDeclaration()) { | |
249 Logger::attention("ignoring unittest declaration: %s", fdecl->toChars()); | |
250 return; | |
251 } | |
252 | |
253 bool declareOnly = false; | |
254 if (fdecl->parent) | |
255 { | |
256 if (TemplateInstance* tinst = fdecl->parent->isTemplateInstance()) | |
257 { | |
258 TemplateDeclaration* tempdecl = tinst->tempdecl; | |
259 if (tempdecl->llvmInternal == LLVMva_start) | |
260 { | |
261 Logger::println("magic va_start found"); | |
262 fdecl->llvmInternal = LLVMva_start; | |
263 declareOnly = true; | |
264 } | |
265 else if (tempdecl->llvmInternal == LLVMva_arg) | |
266 { | |
267 Logger::println("magic va_arg found"); | |
268 fdecl->llvmInternal = LLVMva_arg; | |
269 return; | |
270 } | |
271 } | |
272 } | |
273 | |
274 if (fdecl->llvmTouched) return; | |
275 fdecl->llvmTouched = true; | |
276 | |
277 if (!fdecl->llvmIRFunc) { | |
278 fdecl->llvmIRFunc = new IRFunction(fdecl); | |
279 } | |
280 | |
281 // mangled name | |
282 char* mangled_name; | |
283 if (fdecl->llvmInternal == LLVMintrinsic) | |
284 mangled_name = fdecl->llvmInternal1; | |
285 else | |
286 mangled_name = fdecl->mangle(); | |
287 | |
288 // unit test special handling | |
289 if (fdecl->isUnitTestDeclaration()) | |
290 { | |
291 assert(0 && "no unittests yet"); | |
292 /*const llvm::FunctionType* fnty = llvm::FunctionType::get(llvm::Type::VoidTy, std::vector<const llvm::Type*>(), false); | |
293 // make the function | |
294 llvm::Function* func = gIR->module->getFunction(mangled_name); | |
295 if (func == 0) | |
296 func = new llvm::Function(fnty,llvm::GlobalValue::InternalLinkage,mangled_name,gIR->module); | |
297 func->setCallingConv(llvm::CallingConv::Fast); | |
298 fdecl->llvmValue = func; | |
299 return func; | |
300 */ | |
301 } | |
302 | |
303 if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { | |
304 error("intrinsics cannot have function bodies"); | |
305 fatal(); | |
306 } | |
307 | |
308 llvm::Function* vafunc = 0; | |
309 if ((fdecl->llvmInternal == LLVMva_start) || (fdecl->llvmInternal == LLVMva_intrinsic)) { | |
310 vafunc = DtoDeclareVaFunction(fdecl); | |
311 } | |
312 | |
313 Type* t = DtoDType(fdecl->type); | |
314 TypeFunction* f = (TypeFunction*)t; | |
315 | |
316 // construct function | |
317 const llvm::FunctionType* functype = DtoFunctionType(fdecl); | |
318 llvm::Function* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); | |
319 if (!func) | |
320 func = new llvm::Function(functype, DtoLinkage(fdecl->protection, fdecl->storage_class), mangled_name, gIR->module); | |
321 else | |
322 assert(func->getFunctionType() == functype); | |
323 | |
324 // add func to IRFunc | |
325 fdecl->llvmIRFunc->func = func; | |
326 | |
327 // calling convention | |
328 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) | |
329 func->setCallingConv(DtoCallingConv(f->linkage)); | |
330 | |
331 // template instances should have weak linkage | |
332 if (!vafunc && fdecl->llvmInternal != LLVMintrinsic && fdecl->parent && DtoIsTemplateInstance(fdecl->parent)) | |
333 func->setLinkage(llvm::GlobalValue::WeakLinkage); | |
334 | |
335 fdecl->llvmValue = func; | |
336 assert(llvm::isa<llvm::FunctionType>(f->llvmType->get())); | |
337 | |
338 if (fdecl->isMain()) { | |
339 gIR->mainFunc = func; | |
340 } | |
341 | |
342 // name parameters | |
343 llvm::Function::arg_iterator iarg = func->arg_begin(); | |
344 int k = 0; | |
345 if (f->llvmRetInPtr) { | |
346 iarg->setName("retval"); | |
347 f->llvmRetArg = iarg; | |
348 ++iarg; | |
349 } | |
350 if (f->llvmUsesThis) { | |
351 iarg->setName("this"); | |
352 ++iarg; | |
353 } | |
354 int varargs = -1; | |
355 if (f->linkage == LINKd && f->varargs == 1) | |
356 varargs = 0; | |
357 for (; iarg != func->arg_end(); ++iarg) | |
358 { | |
359 Argument* arg = Argument::getNth(f->parameters, k++); | |
360 //arg->llvmValue = iarg; | |
361 //Logger::println("identifier: '%s' %p\n", arg->ident->toChars(), arg->ident); | |
362 if (arg && arg->ident != 0) { | |
363 if (arg->vardecl) { | |
364 arg->vardecl->llvmValue = iarg; | |
365 } | |
366 iarg->setName(arg->ident->toChars()); | |
367 } | |
368 else if (!arg && varargs >= 0) { | |
369 if (varargs == 0) { | |
370 iarg->setName("_arguments"); | |
371 fdecl->llvmArguments = iarg; | |
372 } | |
373 else if (varargs == 1) { | |
374 iarg->setName("_argptr"); | |
375 fdecl->llvmArgPtr = iarg; | |
376 } | |
377 else | |
378 assert(0); | |
379 varargs++; | |
380 } | |
381 else { | |
382 iarg->setName("unnamed"); | |
383 } | |
384 } | |
385 | |
386 if (!declareOnly) | |
387 gIR->defineQueue.push_back(fdecl); | |
388 | |
389 Logger::cout() << "func decl: " << *func << '\n'; | |
390 } | |
391 | |
392 ////////////////////////////////////////////////////////////////////////////////////////// | |
393 | |
394 // TODO split this monster up | |
395 void DtoDefineFunc(FuncDeclaration* fd) | |
396 { | |
397 // debug info | |
398 if (global.params.symdebug) { | |
399 Module* mo = fd->getModule(); | |
400 if (!mo->llvmCompileUnit) { | |
401 mo->llvmCompileUnit = DtoDwarfCompileUnit(mo,false); | |
402 } | |
403 fd->llvmDwarfSubProgram = DtoDwarfSubProgram(fd, mo->llvmCompileUnit); | |
404 } | |
405 | |
406 Type* t = DtoDType(fd->type); | |
407 TypeFunction* f = (TypeFunction*)t; | |
408 | |
409 assert(f->llvmType); | |
410 llvm::Function* func = fd->llvmIRFunc->func; | |
411 const llvm::FunctionType* functype = func->getFunctionType(); | |
412 | |
413 // only members of the current module or template instances maybe be defined | |
414 if (fd->getModule() == gIR->dmodule || DtoIsTemplateInstance(fd->parent)) | |
415 { | |
416 fd->llvmDModule = gIR->dmodule; | |
417 | |
418 // handle static constructor / destructor | |
419 if (fd->isStaticCtorDeclaration() || fd->isStaticDtorDeclaration()) { | |
420 const llvm::ArrayType* sctor_type = llvm::ArrayType::get(llvm::PointerType::get(functype),1); | |
421 //Logger::cout() << "static ctor type: " << *sctor_type << '\n'; | |
422 | |
423 llvm::Constant* sctor_func = llvm::cast<llvm::Constant>(fd->llvmValue); | |
424 //Logger::cout() << "static ctor func: " << *sctor_func << '\n'; | |
425 | |
426 llvm::Constant* sctor_init = llvm::ConstantArray::get(sctor_type,&sctor_func,1); | |
427 | |
428 //Logger::cout() << "static ctor init: " << *sctor_init << '\n'; | |
429 | |
430 // output the llvm.global_ctors array | |
431 const char* varname = fd->isStaticCtorDeclaration() ? "_d_module_ctor_array" : "_d_module_dtor_array"; | |
432 llvm::GlobalVariable* sctor_arr = new llvm::GlobalVariable(sctor_type, false, llvm::GlobalValue::AppendingLinkage, sctor_init, varname, gIR->module); | |
433 } | |
434 | |
435 // function definition | |
436 if (fd->fbody != 0) | |
437 { | |
438 Logger::println("Doing function body for: %s", fd->toChars()); | |
439 assert(fd->llvmIRFunc); | |
440 gIR->functions.push_back(fd->llvmIRFunc); | |
441 | |
442 // this handling | |
443 if (f->llvmUsesThis) { | |
444 Logger::println("uses this"); | |
445 if (f->llvmRetInPtr) | |
446 fd->llvmThisVar = ++func->arg_begin(); | |
447 else | |
448 fd->llvmThisVar = func->arg_begin(); | |
449 assert(fd->llvmThisVar != 0); | |
450 } | |
451 | |
452 if (fd->isMain()) | |
453 gIR->emitMain = true; | |
454 | |
455 llvm::BasicBlock* beginbb = new llvm::BasicBlock("entry",func); | |
456 llvm::BasicBlock* endbb = new llvm::BasicBlock("endentry",func); | |
457 | |
458 //assert(gIR->scopes.empty()); | |
459 gIR->scopes.push_back(IRScope(beginbb, endbb)); | |
460 | |
461 // create alloca point | |
462 f->llvmAllocaPoint = new llvm::BitCastInst(llvm::ConstantInt::get(llvm::Type::Int32Ty,0,false),llvm::Type::Int32Ty,"alloca point",gIR->scopebb()); | |
463 gIR->func()->allocapoint = f->llvmAllocaPoint; | |
464 | |
465 // give arguments storage | |
466 size_t n = Argument::dim(f->parameters); | |
467 for (int i=0; i < n; ++i) { | |
468 Argument* arg = Argument::getNth(f->parameters, i); | |
469 if (arg && arg->vardecl) { | |
470 VarDeclaration* vd = arg->vardecl; | |
471 if (!vd->llvmNeedsStorage || vd->nestedref || vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)) | |
472 continue; | |
473 llvm::Value* a = vd->llvmValue; | |
474 assert(a); | |
475 std::string s(a->getName()); | |
476 Logger::println("giving argument '%s' storage", s.c_str()); | |
477 s.append("_storage"); | |
478 llvm::Value* v = new llvm::AllocaInst(a->getType(),s,f->llvmAllocaPoint); | |
479 gIR->ir->CreateStore(a,v); | |
480 vd->llvmValue = v; | |
481 } | |
482 else { | |
483 Logger::attention("some unknown argument: %s", arg ? arg->toChars() : 0); | |
484 } | |
485 } | |
486 | |
487 // debug info | |
488 if (global.params.symdebug) DtoDwarfFuncStart(fd); | |
489 | |
490 llvm::Value* parentNested = NULL; | |
491 if (FuncDeclaration* fd2 = fd->toParent()->isFuncDeclaration()) { | |
492 parentNested = fd2->llvmNested; | |
493 } | |
494 | |
495 // construct nested variables struct | |
496 if (!fd->llvmNestedVars.empty() || parentNested) { | |
497 std::vector<const llvm::Type*> nestTypes; | |
498 int j = 0; | |
499 if (parentNested) { | |
500 nestTypes.push_back(parentNested->getType()); | |
501 j++; | |
502 } | |
503 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
504 VarDeclaration* vd = *i; | |
505 vd->llvmNestedIndex = j++; | |
506 if (vd->isParameter()) { | |
507 assert(vd->llvmValue); | |
508 nestTypes.push_back(vd->llvmValue->getType()); | |
509 } | |
510 else { | |
511 nestTypes.push_back(DtoType(vd->type)); | |
512 } | |
513 } | |
514 const llvm::StructType* nestSType = llvm::StructType::get(nestTypes); | |
515 Logger::cout() << "nested var struct has type:" << '\n' << *nestSType; | |
516 fd->llvmNested = new llvm::AllocaInst(nestSType,"nestedvars",f->llvmAllocaPoint); | |
517 if (parentNested) { | |
518 assert(fd->llvmThisVar); | |
519 llvm::Value* ptr = gIR->ir->CreateBitCast(fd->llvmThisVar, parentNested->getType(), "tmp"); | |
520 gIR->ir->CreateStore(ptr, DtoGEPi(fd->llvmNested, 0,0, "tmp")); | |
521 } | |
522 for (std::set<VarDeclaration*>::iterator i=fd->llvmNestedVars.begin(); i!=fd->llvmNestedVars.end(); ++i) { | |
523 VarDeclaration* vd = *i; | |
524 if (vd->isParameter()) { | |
525 gIR->ir->CreateStore(vd->llvmValue, DtoGEPi(fd->llvmNested, 0, vd->llvmNestedIndex, "tmp")); | |
526 vd->llvmValue = fd->llvmNested; | |
527 } | |
528 } | |
529 } | |
530 | |
531 // copy _argptr to a memory location | |
532 if (f->linkage == LINKd && f->varargs == 1) | |
533 { | |
534 llvm::Value* argptrmem = new llvm::AllocaInst(fd->llvmArgPtr->getType(), "_argptrmem", gIR->topallocapoint()); | |
535 new llvm::StoreInst(fd->llvmArgPtr, argptrmem, gIR->scopebb()); | |
536 fd->llvmArgPtr = argptrmem; | |
537 } | |
538 | |
539 // output function body | |
540 fd->fbody->toIR(gIR); | |
541 | |
542 // llvm requires all basic blocks to end with a TerminatorInst but DMD does not put a return statement | |
543 // in automatically, so we do it here. | |
544 if (!fd->isMain()) { | |
545 if (!gIR->scopereturned()) { | |
546 // pass the previous block into this block | |
547 if (global.params.symdebug) DtoDwarfFuncEnd(fd); | |
548 if (func->getReturnType() == llvm::Type::VoidTy) { | |
549 new llvm::ReturnInst(gIR->scopebb()); | |
550 } | |
551 else { | |
552 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), gIR->scopebb()); | |
553 } | |
554 } | |
555 } | |
556 | |
557 // erase alloca point | |
558 f->llvmAllocaPoint->eraseFromParent(); | |
559 f->llvmAllocaPoint = 0; | |
560 gIR->func()->allocapoint = 0; | |
561 | |
562 gIR->scopes.pop_back(); | |
563 | |
564 // get rid of the endentry block, it's never used | |
565 assert(!func->getBasicBlockList().empty()); | |
566 func->getBasicBlockList().pop_back(); | |
567 | |
568 // if the last block is empty now, it must be unreachable or it's a bug somewhere else | |
569 // would be nice to figure out how to assert that this is correct | |
570 llvm::BasicBlock* lastbb = &func->getBasicBlockList().back(); | |
571 if (lastbb->empty()) { | |
572 if (lastbb->getNumUses() == 0) | |
573 lastbb->eraseFromParent(); | |
574 else { | |
575 new llvm::UnreachableInst(lastbb); | |
576 /*if (func->getReturnType() == llvm::Type::VoidTy) { | |
577 new llvm::ReturnInst(lastbb); | |
578 } | |
579 else { | |
580 new llvm::ReturnInst(llvm::UndefValue::get(func->getReturnType()), lastbb); | |
581 }*/ | |
582 } | |
583 } | |
584 | |
585 gIR->functions.pop_back(); | |
586 } | |
587 } | |
588 } | |
589 | |
590 ////////////////////////////////////////////////////////////////////////////////////////// | |
591 | |
592 void DtoMain() | |
593 { | |
594 // emit main function llvm style | |
595 // int main(int argc, char**argv, char**env); | |
596 | |
597 assert(gIR != 0); | |
598 IRState& ir = *gIR; | |
599 | |
600 assert(ir.emitMain && ir.mainFunc); | |
601 | |
602 // parameter types | |
603 std::vector<const llvm::Type*> pvec; | |
604 pvec.push_back((const llvm::Type*)llvm::Type::Int32Ty); | |
605 const llvm::Type* chPtrType = (const llvm::Type*)llvm::PointerType::get(llvm::Type::Int8Ty); | |
606 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
607 pvec.push_back((const llvm::Type*)llvm::PointerType::get(chPtrType)); | |
608 const llvm::Type* rettype = (const llvm::Type*)llvm::Type::Int32Ty; | |
609 | |
610 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false); | |
611 llvm::Function* func = new llvm::Function(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module); | |
612 | |
613 llvm::BasicBlock* bb = new llvm::BasicBlock("entry",func); | |
614 | |
615 // call static ctors | |
616 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_ctors"); | |
617 llvm::Instruction* apt = new llvm::CallInst(fn,"",bb); | |
618 | |
619 // call user main function | |
620 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType(); | |
621 llvm::CallInst* call; | |
622 if (mainty->getNumParams() > 0) | |
623 { | |
624 // main with arguments | |
625 assert(mainty->getNumParams() == 1); | |
626 std::vector<llvm::Value*> args; | |
627 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args"); | |
628 | |
629 llvm::Function::arg_iterator argi = func->arg_begin(); | |
630 args.push_back(argi++); | |
631 args.push_back(argi++); | |
632 | |
633 const llvm::Type* at = mainty->getParamType(0)->getContainedType(0); | |
634 llvm::Value* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt); | |
635 llvm::Value* a = new llvm::AllocaInst(at, "argarray", apt); | |
636 llvm::Value* ptr = DtoGEPi(a,0,0,"tmp",bb); | |
637 llvm::Value* v = args[0]; | |
638 if (v->getType() != DtoSize_t()) | |
639 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb); | |
640 new llvm::StoreInst(v,ptr,bb); | |
641 ptr = DtoGEPi(a,0,1,"tmp",bb); | |
642 new llvm::StoreInst(arr,ptr,bb); | |
643 args.push_back(a); | |
644 new llvm::CallInst(mfn, args.begin(), args.end(), "", bb); | |
645 call = new llvm::CallInst(ir.mainFunc,a,"ret",bb); | |
646 } | |
647 else | |
648 { | |
649 // main with no arguments | |
650 call = new llvm::CallInst(ir.mainFunc,"ret",bb); | |
651 } | |
652 call->setCallingConv(ir.mainFunc->getCallingConv()); | |
653 | |
654 // call static dtors | |
655 fn = LLVM_D_GetRuntimeFunction(ir.module,"_d_run_module_dtors"); | |
656 new llvm::CallInst(fn,"",bb); | |
657 | |
658 // return | |
659 new llvm::ReturnInst(call,bb); | |
660 } | |
661 | |
662 ////////////////////////////////////////////////////////////////////////////////////////// |