Mercurial > projects > ldc
comparison gen/llvmhelpers.cpp @ 244:a95056b3c996 trunk
[svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB.
Did a lot of smaller cleans up here and there.
Replaced more llvm::Foo with LLFoo for common stuff.
Split up tollvm.cpp.
author | lindquist |
---|---|
date | Mon, 09 Jun 2008 09:37:08 +0200 |
parents | |
children | fc9c1a0eabbd |
comparison
equal
deleted
inserted
replaced
243:4d006f7b2ada | 244:a95056b3c996 |
---|---|
1 #include "gen/llvm.h" | |
2 | |
3 #include "mars.h" | |
4 #include "init.h" | |
5 | |
6 #include "gen/tollvm.h" | |
7 #include "gen/llvmhelpers.h" | |
8 #include "gen/irstate.h" | |
9 #include "gen/runtime.h" | |
10 #include "gen/logger.h" | |
11 #include "gen/arrays.h" | |
12 #include "gen/dvalue.h" | |
13 #include "gen/complex.h" | |
14 #include "gen/classes.h" | |
15 #include "gen/functions.h" | |
16 #include "gen/typeinf.h" | |
17 | |
18 /****************************************************************************************/ | |
19 /*//////////////////////////////////////////////////////////////////////////////////////// | |
20 // DYNAMIC MEMORY HELPERS | |
21 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
22 | |
23 LLValue* DtoNew(Type* newtype) | |
24 { | |
25 // get runtime function | |
26 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_allocmemoryT"); | |
27 // get type info | |
28 LLConstant* ti = DtoTypeInfoOf(newtype); | |
29 assert(isaPointer(ti)); | |
30 // call runtime allocator | |
31 LLValue* mem = gIR->ir->CreateCall(fn, ti, ".gc_mem"); | |
32 // cast | |
33 return DtoBitCast(mem, getPtrToType(DtoType(newtype)), ".gc_mem"); | |
34 } | |
35 | |
36 void DtoDeleteMemory(LLValue* ptr) | |
37 { | |
38 // get runtime function | |
39 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delmemory"); | |
40 // build args | |
41 LLSmallVector<LLValue*,1> arg; | |
42 arg.push_back(DtoBitCast(ptr, getVoidPtrType(), ".tmp")); | |
43 // call | |
44 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); | |
45 } | |
46 | |
47 void DtoDeleteClass(LLValue* inst) | |
48 { | |
49 // get runtime function | |
50 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delclass"); | |
51 // build args | |
52 LLSmallVector<LLValue*,1> arg; | |
53 arg.push_back(DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")); | |
54 // call | |
55 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); | |
56 } | |
57 | |
58 void DtoDeleteInterface(LLValue* inst) | |
59 { | |
60 // get runtime function | |
61 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delinterface"); | |
62 // build args | |
63 LLSmallVector<LLValue*,1> arg; | |
64 arg.push_back(DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")); | |
65 // call | |
66 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); | |
67 } | |
68 | |
69 void DtoDeleteArray(DValue* arr) | |
70 { | |
71 // get runtime function | |
72 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delarray"); | |
73 // build args | |
74 LLSmallVector<LLValue*,2> arg; | |
75 arg.push_back(DtoArrayLen(arr)); | |
76 arg.push_back(DtoBitCast(DtoArrayPtr(arr), getVoidPtrType(), ".tmp")); | |
77 // call | |
78 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); | |
79 } | |
80 | |
81 /****************************************************************************************/ | |
82 /*//////////////////////////////////////////////////////////////////////////////////////// | |
83 // ASSERT HELPER | |
84 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
85 | |
86 void DtoAssert(Loc* loc, DValue* msg) | |
87 { | |
88 std::vector<LLValue*> args; | |
89 LLConstant* c; | |
90 | |
91 // func | |
92 const char* fname = msg ? "_d_assert_msg" : "_d_assert"; | |
93 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname); | |
94 | |
95 // param attrs | |
96 llvm::PAListPtr palist; | |
97 int idx = 1; | |
98 | |
99 c = DtoConstString(loc->filename); | |
100 | |
101 // msg param | |
102 if (msg) | |
103 { | |
104 if (DSliceValue* s = msg->isSlice()) | |
105 { | |
106 llvm::AllocaInst* alloc = gIR->func()->msgArg; | |
107 if (!alloc) | |
108 { | |
109 alloc = new llvm::AllocaInst(c->getType(), ".assertmsg", gIR->topallocapoint()); | |
110 DtoSetArray(alloc, DtoArrayLen(s), DtoArrayPtr(s)); | |
111 gIR->func()->msgArg = alloc; | |
112 } | |
113 args.push_back(alloc); | |
114 } | |
115 else | |
116 { | |
117 args.push_back(msg->getRVal()); | |
118 } | |
119 palist = palist.addAttr(idx++, llvm::ParamAttr::ByVal); | |
120 } | |
121 | |
122 // file param | |
123 llvm::AllocaInst* alloc = gIR->func()->srcfileArg; | |
124 if (!alloc) | |
125 { | |
126 alloc = new llvm::AllocaInst(c->getType(), ".srcfile", gIR->topallocapoint()); | |
127 gIR->func()->srcfileArg = alloc; | |
128 } | |
129 LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp"); | |
130 DtoStore(c->getOperand(0), ptr); | |
131 ptr = DtoGEPi(alloc, 0,1, "tmp"); | |
132 DtoStore(c->getOperand(1), ptr); | |
133 | |
134 args.push_back(alloc); | |
135 palist = palist.addAttr(idx++, llvm::ParamAttr::ByVal); | |
136 | |
137 | |
138 // line param | |
139 c = DtoConstUint(loc->linnum); | |
140 args.push_back(c); | |
141 | |
142 // call | |
143 llvm::CallInst* call = llvm::CallInst::Create(fn, args.begin(), args.end(), "", gIR->scopebb()); | |
144 call->setParamAttrs(palist); | |
145 } | |
146 | |
147 /****************************************************************************************/ | |
148 /*//////////////////////////////////////////////////////////////////////////////////////// | |
149 // NESTED VARIABLE HELPERS | |
150 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
151 | |
152 static const LLType* get_next_frame_ptr_type(Dsymbol* sc) | |
153 { | |
154 assert(sc->isFuncDeclaration() || sc->isClassDeclaration()); | |
155 Dsymbol* p = sc->toParent2(); | |
156 if (!p->isFuncDeclaration() && !p->isClassDeclaration()) | |
157 Logger::println("unexpected parent symbol found while resolving frame pointer - '%s' kind: '%s'", p->toChars(), p->kind()); | |
158 assert(p->isFuncDeclaration() || p->isClassDeclaration()); | |
159 if (FuncDeclaration* fd = p->isFuncDeclaration()) | |
160 { | |
161 LLValue* v = fd->ir.irFunc->nestedVar; | |
162 assert(v); | |
163 return v->getType(); | |
164 } | |
165 else if (ClassDeclaration* cd = p->isClassDeclaration()) | |
166 { | |
167 return DtoType(cd->type); | |
168 } | |
169 else | |
170 { | |
171 Logger::println("symbol: '%s' kind: '%s'", sc->toChars(), sc->kind()); | |
172 assert(0); | |
173 } | |
174 } | |
175 | |
176 ////////////////////////////////////////////////////////////////////////////////////////// | |
177 | |
178 static LLValue* get_frame_ptr_impl(FuncDeclaration* func, Dsymbol* sc, LLValue* v) | |
179 { | |
180 LOG_SCOPE; | |
181 if (sc == func) | |
182 { | |
183 return v; | |
184 } | |
185 else if (FuncDeclaration* fd = sc->isFuncDeclaration()) | |
186 { | |
187 Logger::println("scope is function: %s", fd->toChars()); | |
188 | |
189 if (fd->toParent2() == func) | |
190 { | |
191 if (!func->ir.irFunc->nestedVar) | |
192 return NULL; | |
193 return DtoBitCast(v, func->ir.irFunc->nestedVar->getType()); | |
194 } | |
195 | |
196 v = DtoBitCast(v, get_next_frame_ptr_type(fd)); | |
197 Logger::cout() << "v = " << *v << '\n'; | |
198 | |
199 if (fd->toParent2()->isFuncDeclaration()) | |
200 { | |
201 v = DtoGEPi(v, 0,0, "tmp"); | |
202 v = DtoLoad(v); | |
203 } | |
204 else if (ClassDeclaration* cd = fd->toParent2()->isClassDeclaration()) | |
205 { | |
206 size_t idx = 2; | |
207 //idx += cd->ir.irStruct->interfaceVec.size(); | |
208 v = DtoGEPi(v,0,idx,"tmp"); | |
209 v = DtoLoad(v); | |
210 } | |
211 else | |
212 { | |
213 assert(0); | |
214 } | |
215 return get_frame_ptr_impl(func, fd->toParent2(), v); | |
216 } | |
217 else if (ClassDeclaration* cd = sc->isClassDeclaration()) | |
218 { | |
219 Logger::println("scope is class: %s", cd->toChars()); | |
220 /*size_t idx = 2; | |
221 idx += cd->llvmIrStruct->interfaces.size(); | |
222 v = DtoGEPi(v,0,idx,"tmp"); | |
223 Logger::cout() << "gep = " << *v << '\n'; | |
224 v = DtoLoad(v);*/ | |
225 return get_frame_ptr_impl(func, cd->toParent2(), v); | |
226 } | |
227 else | |
228 { | |
229 Logger::println("symbol: '%s'", sc->toPrettyChars()); | |
230 assert(0); | |
231 } | |
232 } | |
233 | |
234 ////////////////////////////////////////////////////////////////////////////////////////// | |
235 | |
236 static LLValue* get_frame_ptr(FuncDeclaration* func) | |
237 { | |
238 Logger::println("Resolving context pointer for nested function: '%s'", func->toPrettyChars()); | |
239 LOG_SCOPE; | |
240 IrFunction* irfunc = gIR->func(); | |
241 | |
242 // in the right scope already | |
243 if (func == irfunc->decl) | |
244 return irfunc->decl->ir.irFunc->nestedVar; | |
245 | |
246 // use the 'this' pointer | |
247 LLValue* ptr = irfunc->decl->ir.irFunc->thisVar; | |
248 assert(ptr); | |
249 | |
250 // return the fully resolved frame pointer | |
251 ptr = get_frame_ptr_impl(func, irfunc->decl, ptr); | |
252 if (ptr) Logger::cout() << "Found context!" << *ptr; | |
253 else Logger::cout() << "NULL context!\n"; | |
254 | |
255 return ptr; | |
256 } | |
257 | |
258 ////////////////////////////////////////////////////////////////////////////////////////// | |
259 | |
260 LLValue* DtoNestedContext(FuncDeclaration* func) | |
261 { | |
262 // resolve frame ptr | |
263 LLValue* ptr = get_frame_ptr(func); | |
264 Logger::cout() << "Nested context ptr = "; | |
265 if (ptr) Logger::cout() << *ptr; | |
266 else Logger::cout() << "NULL"; | |
267 Logger::cout() << '\n'; | |
268 return ptr; | |
269 } | |
270 | |
271 ////////////////////////////////////////////////////////////////////////////////////////// | |
272 | |
273 static void print_frame_worker(VarDeclaration* vd, Dsymbol* par) | |
274 { | |
275 if (vd->toParent2() == par) | |
276 { | |
277 Logger::println("found: '%s' kind: '%s'", par->toChars(), par->kind()); | |
278 return; | |
279 } | |
280 | |
281 Logger::println("diving into: '%s' kind: '%s'", par->toChars(), par->kind()); | |
282 LOG_SCOPE; | |
283 print_frame_worker(vd, par->toParent2()); | |
284 } | |
285 | |
286 ////////////////////////////////////////////////////////////////////////////////////////// | |
287 | |
288 static void print_nested_frame_list(VarDeclaration* vd, Dsymbol* par) | |
289 { | |
290 Logger::println("Frame pointer list for nested var: '%s'", vd->toPrettyChars()); | |
291 LOG_SCOPE; | |
292 if (vd->toParent2() != par) | |
293 print_frame_worker(vd, par); | |
294 else | |
295 Logger::println("Found at level 0"); | |
296 Logger::println("Done"); | |
297 } | |
298 | |
299 ////////////////////////////////////////////////////////////////////////////////////////// | |
300 | |
301 LLValue* DtoNestedVariable(VarDeclaration* vd) | |
302 { | |
303 // log the frame list | |
304 IrFunction* irfunc = gIR->func(); | |
305 if (Logger::enabled()) | |
306 print_nested_frame_list(vd, irfunc->decl); | |
307 | |
308 // resolve frame ptr | |
309 FuncDeclaration* func = vd->toParent2()->isFuncDeclaration(); | |
310 assert(func); | |
311 LLValue* ptr = DtoNestedContext(func); | |
312 assert(ptr && "nested var, but no context"); | |
313 | |
314 // we must cast here to be sure. nested classes just have a void* | |
315 ptr = DtoBitCast(ptr, func->ir.irFunc->nestedVar->getType()); | |
316 | |
317 // index nested var and load (if necessary) | |
318 LLValue* v = DtoGEPi(ptr, 0, vd->ir.irLocal->nestedIndex, "tmp"); | |
319 // references must be loaded, for normal variables this IS already the variable storage!!! | |
320 if (vd->isParameter() && (vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type))) | |
321 v = DtoLoad(v); | |
322 | |
323 // log and return | |
324 Logger::cout() << "Nested var ptr = " << *v << '\n'; | |
325 return v; | |
326 } | |
327 | |
328 /****************************************************************************************/ | |
329 /*//////////////////////////////////////////////////////////////////////////////////////// | |
330 // ASSIGNMENT HELPER (store this in that) | |
331 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
332 | |
333 void DtoAssign(DValue* lhs, DValue* rhs) | |
334 { | |
335 Logger::cout() << "DtoAssign(...);\n"; | |
336 LOG_SCOPE; | |
337 | |
338 Type* t = DtoDType(lhs->getType()); | |
339 Type* t2 = DtoDType(rhs->getType()); | |
340 | |
341 if (t->ty == Tstruct) { | |
342 if (t2 != t) { | |
343 // TODO: fix this, use 'rhs' for something | |
344 DtoAggrZeroInit(lhs->getLVal()); | |
345 } | |
346 else if (!rhs->inPlace()) { | |
347 DtoAggrCopy(lhs->getLVal(), rhs->getRVal()); | |
348 } | |
349 } | |
350 else if (t->ty == Tarray) { | |
351 // lhs is slice | |
352 if (DSliceValue* s = lhs->isSlice()) { | |
353 if (DSliceValue* s2 = rhs->isSlice()) { | |
354 DtoArrayCopySlices(s, s2); | |
355 } | |
356 else if (t->next == t2) { | |
357 if (s->len) | |
358 DtoArrayInit(s->ptr, s->len, rhs->getRVal()); | |
359 else | |
360 DtoArrayInit(s->ptr, rhs->getRVal()); | |
361 } | |
362 else { | |
363 DtoArrayCopyToSlice(s, rhs); | |
364 } | |
365 } | |
366 // rhs is slice | |
367 else if (DSliceValue* s = rhs->isSlice()) { | |
368 assert(s->getType()->toBasetype() == lhs->getType()->toBasetype()); | |
369 DtoSetArray(lhs->getLVal(),DtoArrayLen(s),DtoArrayPtr(s)); | |
370 } | |
371 // null | |
372 else if (rhs->isNull()) { | |
373 DtoSetArrayToNull(lhs->getLVal()); | |
374 } | |
375 // reference assignment | |
376 else { | |
377 DtoArrayAssign(lhs->getLVal(), rhs->getRVal()); | |
378 } | |
379 } | |
380 else if (t->ty == Tsarray) { | |
381 if (DtoType(lhs->getType()) == DtoType(rhs->getType())) { | |
382 DtoStaticArrayCopy(lhs->getLVal(), rhs->getRVal()); | |
383 } | |
384 else { | |
385 DtoArrayInit(lhs->getLVal(), rhs->getRVal()); | |
386 } | |
387 } | |
388 else if (t->ty == Tdelegate) { | |
389 if (rhs->isNull()) | |
390 DtoAggrZeroInit(lhs->getLVal()); | |
391 else if (!rhs->inPlace()) { | |
392 LLValue* l = lhs->getLVal(); | |
393 LLValue* r = rhs->getRVal(); | |
394 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n'; | |
395 DtoAggrCopy(l, r); | |
396 } | |
397 } | |
398 else if (t->ty == Tclass) { | |
399 assert(t2->ty == Tclass); | |
400 // assignment to this in constructor special case | |
401 if (lhs->isThis()) { | |
402 LLValue* tmp = rhs->getRVal(); | |
403 FuncDeclaration* fdecl = gIR->func()->decl; | |
404 // respecify the this param | |
405 if (!llvm::isa<llvm::AllocaInst>(fdecl->ir.irFunc->thisVar)) | |
406 fdecl->ir.irFunc->thisVar = new llvm::AllocaInst(tmp->getType(), "newthis", gIR->topallocapoint()); | |
407 DtoStore(tmp, fdecl->ir.irFunc->thisVar); | |
408 } | |
409 // regular class ref -> class ref assignment | |
410 else { | |
411 DtoStore(rhs->getRVal(), lhs->getLVal()); | |
412 } | |
413 } | |
414 else if (t->iscomplex()) { | |
415 assert(!lhs->isComplex()); | |
416 | |
417 LLValue* dst; | |
418 if (DLRValue* lr = lhs->isLRValue()) { | |
419 dst = lr->getLVal(); | |
420 rhs = DtoCastComplex(rhs, lr->getLType()); | |
421 } | |
422 else { | |
423 dst = lhs->getRVal(); | |
424 } | |
425 | |
426 if (DComplexValue* cx = rhs->isComplex()) | |
427 DtoComplexSet(dst, cx->re, cx->im); | |
428 else | |
429 DtoComplexAssign(dst, rhs->getRVal()); | |
430 } | |
431 else { | |
432 LLValue* l = lhs->getLVal(); | |
433 LLValue* r = rhs->getRVal(); | |
434 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n'; | |
435 const LLType* lit = l->getType()->getContainedType(0); | |
436 if (r->getType() != lit) { | |
437 // handle lvalue cast assignments | |
438 if (DLRValue* lr = lhs->isLRValue()) { | |
439 Logger::println("lvalue cast!"); | |
440 r = DtoCast(rhs, lr->getLType())->getRVal(); | |
441 } | |
442 else { | |
443 r = DtoCast(rhs, lhs->getType())->getRVal(); | |
444 } | |
445 Logger::cout() << "really assign\nlhs: " << *l << "rhs: " << *r << '\n'; | |
446 assert(r->getType() == l->getType()->getContainedType(0)); | |
447 } | |
448 gIR->ir->CreateStore(r, l); | |
449 } | |
450 } | |
451 | |
452 /****************************************************************************************/ | |
453 /*//////////////////////////////////////////////////////////////////////////////////////// | |
454 // CASTING HELPERS | |
455 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
456 | |
457 DValue* DtoCastInt(DValue* val, Type* _to) | |
458 { | |
459 const LLType* tolltype = DtoType(_to); | |
460 | |
461 Type* to = DtoDType(_to); | |
462 Type* from = DtoDType(val->getType()); | |
463 assert(from->isintegral()); | |
464 | |
465 size_t fromsz = from->size(); | |
466 size_t tosz = to->size(); | |
467 | |
468 LLValue* rval = val->getRVal(); | |
469 if (rval->getType() == tolltype) { | |
470 return new DImValue(_to, rval); | |
471 } | |
472 | |
473 if (to->isintegral()) { | |
474 if (fromsz < tosz) { | |
475 Logger::cout() << "cast to: " << *tolltype << '\n'; | |
476 if (from->isunsigned() || from->ty == Tbool) { | |
477 rval = new llvm::ZExtInst(rval, tolltype, "tmp", gIR->scopebb()); | |
478 } else { | |
479 rval = new llvm::SExtInst(rval, tolltype, "tmp", gIR->scopebb()); | |
480 } | |
481 } | |
482 else if (fromsz > tosz) { | |
483 rval = new llvm::TruncInst(rval, tolltype, "tmp", gIR->scopebb()); | |
484 } | |
485 else { | |
486 rval = DtoBitCast(rval, tolltype); | |
487 } | |
488 } | |
489 else if (to->isfloating()) { | |
490 if (from->isunsigned()) { | |
491 rval = new llvm::UIToFPInst(rval, tolltype, "tmp", gIR->scopebb()); | |
492 } | |
493 else { | |
494 rval = new llvm::SIToFPInst(rval, tolltype, "tmp", gIR->scopebb()); | |
495 } | |
496 } | |
497 else if (to->ty == Tpointer) { | |
498 Logger::cout() << "cast pointer: " << *tolltype << '\n'; | |
499 rval = gIR->ir->CreateIntToPtr(rval, tolltype, "tmp"); | |
500 } | |
501 else { | |
502 assert(0 && "bad int cast"); | |
503 } | |
504 | |
505 return new DImValue(_to, rval); | |
506 } | |
507 | |
508 DValue* DtoCastPtr(DValue* val, Type* to) | |
509 { | |
510 const LLType* tolltype = DtoType(to); | |
511 | |
512 Type* totype = DtoDType(to); | |
513 Type* fromtype = DtoDType(val->getType()); | |
514 assert(fromtype->ty == Tpointer || fromtype->ty == Tfunction); | |
515 | |
516 LLValue* rval; | |
517 | |
518 if (totype->ty == Tpointer || totype->ty == Tclass) { | |
519 LLValue* src = val->getRVal(); | |
520 Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n'; | |
521 rval = DtoBitCast(src, tolltype); | |
522 } | |
523 else if (totype->isintegral()) { | |
524 rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); | |
525 } | |
526 else { | |
527 Logger::println("invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars()); | |
528 assert(0); | |
529 } | |
530 | |
531 return new DImValue(to, rval); | |
532 } | |
533 | |
534 DValue* DtoCastFloat(DValue* val, Type* to) | |
535 { | |
536 if (val->getType() == to) | |
537 return val; | |
538 | |
539 const LLType* tolltype = DtoType(to); | |
540 | |
541 Type* totype = DtoDType(to); | |
542 Type* fromtype = DtoDType(val->getType()); | |
543 assert(fromtype->isfloating()); | |
544 | |
545 size_t fromsz = fromtype->size(); | |
546 size_t tosz = totype->size(); | |
547 | |
548 LLValue* rval; | |
549 | |
550 if (totype->iscomplex()) { | |
551 assert(0); | |
552 //return new DImValue(to, DtoComplex(to, val)); | |
553 } | |
554 else if (totype->isfloating()) { | |
555 if ((fromtype->ty == Tfloat80 || fromtype->ty == Tfloat64) && (totype->ty == Tfloat80 || totype->ty == Tfloat64)) { | |
556 rval = val->getRVal(); | |
557 } | |
558 else if (fromsz < tosz) { | |
559 rval = new llvm::FPExtInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); | |
560 } | |
561 else if (fromsz > tosz) { | |
562 rval = new llvm::FPTruncInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); | |
563 } | |
564 else { | |
565 assert(0 && "bad float cast"); | |
566 } | |
567 } | |
568 else if (totype->isintegral()) { | |
569 if (totype->isunsigned()) { | |
570 rval = new llvm::FPToUIInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); | |
571 } | |
572 else { | |
573 rval = new llvm::FPToSIInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); | |
574 } | |
575 } | |
576 else { | |
577 assert(0 && "bad float cast"); | |
578 } | |
579 | |
580 return new DImValue(to, rval); | |
581 } | |
582 | |
583 DValue* DtoCast(DValue* val, Type* to) | |
584 { | |
585 Type* fromtype = DtoDType(val->getType()); | |
586 Logger::println("Casting from '%s' to '%s'", fromtype->toChars(), to->toChars()); | |
587 if (fromtype->isintegral()) { | |
588 return DtoCastInt(val, to); | |
589 } | |
590 else if (fromtype->iscomplex()) { | |
591 return DtoCastComplex(val, to); | |
592 } | |
593 else if (fromtype->isfloating()) { | |
594 return DtoCastFloat(val, to); | |
595 } | |
596 else if (fromtype->ty == Tclass) { | |
597 return DtoCastClass(val, to); | |
598 } | |
599 else if (fromtype->ty == Tarray || fromtype->ty == Tsarray) { | |
600 return DtoCastArray(val, to); | |
601 } | |
602 else if (fromtype->ty == Tpointer || fromtype->ty == Tfunction) { | |
603 return DtoCastPtr(val, to); | |
604 } | |
605 else { | |
606 assert(0); | |
607 } | |
608 } | |
609 | |
610 /****************************************************************************************/ | |
611 /*//////////////////////////////////////////////////////////////////////////////////////// | |
612 // TEMPLATE HELPERS | |
613 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
614 | |
615 bool DtoIsTemplateInstance(Dsymbol* s) | |
616 { | |
617 if (!s) return false; | |
618 if (s->isTemplateInstance() && !s->isTemplateMixin()) | |
619 return true; | |
620 else if (s->parent) | |
621 return DtoIsTemplateInstance(s->parent); | |
622 return false; | |
623 } | |
624 | |
625 /****************************************************************************************/ | |
626 /*//////////////////////////////////////////////////////////////////////////////////////// | |
627 // LAZY STATIC INIT HELPER | |
628 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
629 | |
630 void DtoLazyStaticInit(bool istempl, LLValue* gvar, Initializer* init, Type* t) | |
631 { | |
632 // create a flag to make sure initialization only happens once | |
633 llvm::GlobalValue::LinkageTypes gflaglink = istempl ? llvm::GlobalValue::WeakLinkage : llvm::GlobalValue::InternalLinkage; | |
634 std::string gflagname(gvar->getName()); | |
635 gflagname.append("__initflag"); | |
636 llvm::GlobalVariable* gflag = new llvm::GlobalVariable(LLType::Int1Ty,false,gflaglink,DtoConstBool(false),gflagname,gIR->module); | |
637 | |
638 // check flag and do init if not already done | |
639 llvm::BasicBlock* oldend = gIR->scopeend(); | |
640 llvm::BasicBlock* initbb = llvm::BasicBlock::Create("ifnotinit",gIR->topfunc(),oldend); | |
641 llvm::BasicBlock* endinitbb = llvm::BasicBlock::Create("ifnotinitend",gIR->topfunc(),oldend); | |
642 LLValue* cond = gIR->ir->CreateICmpEQ(gIR->ir->CreateLoad(gflag,"tmp"),DtoConstBool(false)); | |
643 gIR->ir->CreateCondBr(cond, initbb, endinitbb); | |
644 gIR->scope() = IRScope(initbb,endinitbb); | |
645 DValue* ie = DtoInitializer(init); | |
646 if (!ie->inPlace()) { | |
647 DValue* dst = new DVarValue(t, gvar, true); | |
648 DtoAssign(dst, ie); | |
649 } | |
650 gIR->ir->CreateStore(DtoConstBool(true), gflag); | |
651 gIR->ir->CreateBr(endinitbb); | |
652 gIR->scope() = IRScope(endinitbb,oldend); | |
653 } | |
654 | |
655 /****************************************************************************************/ | |
656 /*//////////////////////////////////////////////////////////////////////////////////////// | |
657 // PROCESSING QUEUE HELPERS | |
658 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
659 | |
660 void DtoResolveDsymbol(Dsymbol* dsym) | |
661 { | |
662 if (StructDeclaration* sd = dsym->isStructDeclaration()) { | |
663 DtoResolveStruct(sd); | |
664 } | |
665 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
666 DtoResolveClass(cd); | |
667 } | |
668 else if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
669 DtoResolveFunction(fd); | |
670 } | |
671 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) { | |
672 DtoResolveTypeInfo(fd); | |
673 } | |
674 else { | |
675 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars()); | |
676 assert(0 && "unsupported dsymbol for DtoResolveDsymbol"); | |
677 } | |
678 } | |
679 | |
680 ////////////////////////////////////////////////////////////////////////////////////////// | |
681 | |
682 void DtoDeclareDsymbol(Dsymbol* dsym) | |
683 { | |
684 if (StructDeclaration* sd = dsym->isStructDeclaration()) { | |
685 DtoDeclareStruct(sd); | |
686 } | |
687 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
688 DtoDeclareClass(cd); | |
689 } | |
690 else if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
691 DtoDeclareFunction(fd); | |
692 } | |
693 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) { | |
694 DtoDeclareTypeInfo(fd); | |
695 } | |
696 else { | |
697 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars()); | |
698 assert(0 && "unsupported dsymbol for DtoDeclareDsymbol"); | |
699 } | |
700 } | |
701 | |
702 ////////////////////////////////////////////////////////////////////////////////////////// | |
703 | |
704 void DtoConstInitDsymbol(Dsymbol* dsym) | |
705 { | |
706 if (StructDeclaration* sd = dsym->isStructDeclaration()) { | |
707 DtoConstInitStruct(sd); | |
708 } | |
709 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
710 DtoConstInitClass(cd); | |
711 } | |
712 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) { | |
713 DtoConstInitTypeInfo(fd); | |
714 } | |
715 else if (VarDeclaration* vd = dsym->isVarDeclaration()) { | |
716 DtoConstInitGlobal(vd); | |
717 } | |
718 else { | |
719 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars()); | |
720 assert(0 && "unsupported dsymbol for DtoConstInitDsymbol"); | |
721 } | |
722 } | |
723 | |
724 ////////////////////////////////////////////////////////////////////////////////////////// | |
725 | |
726 void DtoDefineDsymbol(Dsymbol* dsym) | |
727 { | |
728 if (StructDeclaration* sd = dsym->isStructDeclaration()) { | |
729 DtoDefineStruct(sd); | |
730 } | |
731 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) { | |
732 DtoDefineClass(cd); | |
733 } | |
734 else if (FuncDeclaration* fd = dsym->isFuncDeclaration()) { | |
735 DtoDefineFunc(fd); | |
736 } | |
737 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) { | |
738 DtoDefineTypeInfo(fd); | |
739 } | |
740 else { | |
741 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars()); | |
742 assert(0 && "unsupported dsymbol for DtoDefineDsymbol"); | |
743 } | |
744 } | |
745 | |
746 ////////////////////////////////////////////////////////////////////////////////////////// | |
747 | |
748 void DtoConstInitGlobal(VarDeclaration* vd) | |
749 { | |
750 if (vd->ir.initialized) return; | |
751 vd->ir.initialized = gIR->dmodule; | |
752 | |
753 Logger::println("* DtoConstInitGlobal(%s)", vd->toChars()); | |
754 LOG_SCOPE; | |
755 | |
756 bool emitRTstaticInit = false; | |
757 | |
758 LLConstant* _init = 0; | |
759 if (vd->parent && vd->parent->isFuncDeclaration() && vd->init && vd->init->isExpInitializer()) { | |
760 _init = DtoConstInitializer(vd->type, NULL); | |
761 emitRTstaticInit = true; | |
762 } | |
763 else { | |
764 _init = DtoConstInitializer(vd->type, vd->init); | |
765 } | |
766 | |
767 const LLType* _type = DtoType(vd->type); | |
768 Type* t = DtoDType(vd->type); | |
769 | |
770 //Logger::cout() << "initializer: " << *_init << '\n'; | |
771 if (_type != _init->getType()) { | |
772 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n"; | |
773 // zero initalizer | |
774 if (_init->isNullValue()) | |
775 _init = llvm::Constant::getNullValue(_type); | |
776 // pointer to global constant (struct.init) | |
777 else if (llvm::isa<llvm::GlobalVariable>(_init)) | |
778 { | |
779 assert(_init->getType()->getContainedType(0) == _type); | |
780 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init); | |
781 assert(t->ty == Tstruct); | |
782 TypeStruct* ts = (TypeStruct*)t; | |
783 assert(ts->sym->ir.irStruct->constInit); | |
784 _init = ts->sym->ir.irStruct->constInit; | |
785 } | |
786 // array single value init | |
787 else if (isaArray(_type)) | |
788 { | |
789 _init = DtoConstStaticArray(_type, _init); | |
790 } | |
791 else { | |
792 Logger::cout() << "Unexpected initializer type: " << *_type << '\n'; | |
793 //assert(0); | |
794 } | |
795 } | |
796 | |
797 bool istempl = false; | |
798 if ((vd->storage_class & STCcomdat) || (vd->parent && DtoIsTemplateInstance(vd->parent))) { | |
799 istempl = true; | |
800 } | |
801 | |
802 if (_init && _init->getType() != _type) | |
803 _type = _init->getType(); | |
804 llvm::cast<LLOpaqueType>(vd->ir.irGlobal->type.get())->refineAbstractTypeTo(_type); | |
805 _type = vd->ir.irGlobal->type.get(); | |
806 //_type->dump(); | |
807 assert(!_type->isAbstract()); | |
808 | |
809 llvm::GlobalVariable* gvar = llvm::cast<llvm::GlobalVariable>(vd->ir.irGlobal->value); | |
810 if (!(vd->storage_class & STCextern) && (vd->getModule() == gIR->dmodule || istempl)) | |
811 { | |
812 gvar->setInitializer(_init); | |
813 } | |
814 | |
815 if (emitRTstaticInit) | |
816 DtoLazyStaticInit(istempl, gvar, vd->init, t); | |
817 } | |
818 | |
819 ////////////////////////////////////////////////////////////////////////////////////////// | |
820 | |
821 void DtoEmptyResolveList() | |
822 { | |
823 //Logger::println("DtoEmptyResolveList()"); | |
824 Dsymbol* dsym; | |
825 while (!gIR->resolveList.empty()) { | |
826 dsym = gIR->resolveList.front(); | |
827 gIR->resolveList.pop_front(); | |
828 DtoResolveDsymbol(dsym); | |
829 } | |
830 } | |
831 | |
832 ////////////////////////////////////////////////////////////////////////////////////////// | |
833 | |
834 void DtoEmptyDeclareList() | |
835 { | |
836 //Logger::println("DtoEmptyDeclareList()"); | |
837 Dsymbol* dsym; | |
838 while (!gIR->declareList.empty()) { | |
839 dsym = gIR->declareList.front(); | |
840 gIR->declareList.pop_front(); | |
841 DtoDeclareDsymbol(dsym); | |
842 } | |
843 } | |
844 | |
845 ////////////////////////////////////////////////////////////////////////////////////////// | |
846 | |
847 void DtoEmptyConstInitList() | |
848 { | |
849 //Logger::println("DtoEmptyConstInitList()"); | |
850 Dsymbol* dsym; | |
851 while (!gIR->constInitList.empty()) { | |
852 dsym = gIR->constInitList.front(); | |
853 gIR->constInitList.pop_front(); | |
854 DtoConstInitDsymbol(dsym); | |
855 } | |
856 } | |
857 | |
858 ////////////////////////////////////////////////////////////////////////////////////////// | |
859 | |
860 void DtoEmptyDefineList() | |
861 { | |
862 //Logger::println("DtoEmptyDefineList()"); | |
863 Dsymbol* dsym; | |
864 while (!gIR->defineList.empty()) { | |
865 dsym = gIR->defineList.front(); | |
866 gIR->defineList.pop_front(); | |
867 DtoDefineDsymbol(dsym); | |
868 } | |
869 } | |
870 | |
871 ////////////////////////////////////////////////////////////////////////////////////////// | |
872 void DtoEmptyAllLists() | |
873 { | |
874 for(;;) | |
875 { | |
876 Dsymbol* dsym; | |
877 if (!gIR->resolveList.empty()) { | |
878 dsym = gIR->resolveList.front(); | |
879 gIR->resolveList.pop_front(); | |
880 DtoResolveDsymbol(dsym); | |
881 } | |
882 else if (!gIR->declareList.empty()) { | |
883 dsym = gIR->declareList.front(); | |
884 gIR->declareList.pop_front(); | |
885 DtoDeclareDsymbol(dsym); | |
886 } | |
887 else if (!gIR->constInitList.empty()) { | |
888 dsym = gIR->constInitList.front(); | |
889 gIR->constInitList.pop_front(); | |
890 DtoConstInitDsymbol(dsym); | |
891 } | |
892 else if (!gIR->defineList.empty()) { | |
893 dsym = gIR->defineList.front(); | |
894 gIR->defineList.pop_front(); | |
895 DtoDefineDsymbol(dsym); | |
896 } | |
897 else { | |
898 break; | |
899 } | |
900 } | |
901 } | |
902 | |
903 ////////////////////////////////////////////////////////////////////////////////////////// | |
904 | |
905 void DtoForceDeclareDsymbol(Dsymbol* dsym) | |
906 { | |
907 if (dsym->ir.declared) return; | |
908 Logger::println("DtoForceDeclareDsymbol(%s)", dsym->toPrettyChars()); | |
909 LOG_SCOPE; | |
910 DtoResolveDsymbol(dsym); | |
911 | |
912 DtoEmptyResolveList(); | |
913 | |
914 DtoDeclareDsymbol(dsym); | |
915 } | |
916 | |
917 ////////////////////////////////////////////////////////////////////////////////////////// | |
918 | |
919 void DtoForceConstInitDsymbol(Dsymbol* dsym) | |
920 { | |
921 if (dsym->ir.initialized) return; | |
922 Logger::println("DtoForceConstInitDsymbol(%s)", dsym->toPrettyChars()); | |
923 LOG_SCOPE; | |
924 DtoResolveDsymbol(dsym); | |
925 | |
926 DtoEmptyResolveList(); | |
927 DtoEmptyDeclareList(); | |
928 | |
929 DtoConstInitDsymbol(dsym); | |
930 } | |
931 | |
932 ////////////////////////////////////////////////////////////////////////////////////////// | |
933 | |
934 void DtoForceDefineDsymbol(Dsymbol* dsym) | |
935 { | |
936 if (dsym->ir.defined) return; | |
937 Logger::println("DtoForceDefineDsymbol(%s)", dsym->toPrettyChars()); | |
938 LOG_SCOPE; | |
939 DtoResolveDsymbol(dsym); | |
940 | |
941 DtoEmptyResolveList(); | |
942 DtoEmptyDeclareList(); | |
943 DtoEmptyConstInitList(); | |
944 | |
945 DtoDefineDsymbol(dsym); | |
946 } | |
947 | |
948 /****************************************************************************************/ | |
949 /*//////////////////////////////////////////////////////////////////////////////////////// | |
950 // INITIALIZER HELPERS | |
951 ////////////////////////////////////////////////////////////////////////////////////////*/ | |
952 | |
953 LLConstant* DtoConstInitializer(Type* type, Initializer* init) | |
954 { | |
955 LLConstant* _init = 0; // may return zero | |
956 if (!init) | |
957 { | |
958 Logger::println("const default initializer for %s", type->toChars()); | |
959 _init = type->defaultInit()->toConstElem(gIR); | |
960 } | |
961 else if (ExpInitializer* ex = init->isExpInitializer()) | |
962 { | |
963 Logger::println("const expression initializer"); | |
964 _init = ex->exp->toConstElem(gIR); | |
965 } | |
966 else if (StructInitializer* si = init->isStructInitializer()) | |
967 { | |
968 Logger::println("const struct initializer"); | |
969 _init = DtoConstStructInitializer(si); | |
970 } | |
971 else if (ArrayInitializer* ai = init->isArrayInitializer()) | |
972 { | |
973 Logger::println("const array initializer"); | |
974 _init = DtoConstArrayInitializer(ai); | |
975 } | |
976 else if (init->isVoidInitializer()) | |
977 { | |
978 Logger::println("const void initializer"); | |
979 const LLType* ty = DtoType(type); | |
980 _init = llvm::Constant::getNullValue(ty); | |
981 } | |
982 else { | |
983 Logger::println("unsupported const initializer: %s", init->toChars()); | |
984 } | |
985 return _init; | |
986 } | |
987 | |
988 ////////////////////////////////////////////////////////////////////////////////////////// | |
989 | |
990 LLConstant* DtoConstFieldInitializer(Type* t, Initializer* init) | |
991 { | |
992 Logger::println("DtoConstFieldInitializer"); | |
993 LOG_SCOPE; | |
994 | |
995 const LLType* _type = DtoType(t); | |
996 | |
997 LLConstant* _init = DtoConstInitializer(t, init); | |
998 assert(_init); | |
999 if (_type != _init->getType()) | |
1000 { | |
1001 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n'; | |
1002 if (t->ty == Tsarray) | |
1003 { | |
1004 const LLArrayType* arrty = isaArray(_type); | |
1005 uint64_t n = arrty->getNumElements(); | |
1006 std::vector<LLConstant*> vals(n,_init); | |
1007 _init = llvm::ConstantArray::get(arrty, vals); | |
1008 } | |
1009 else if (t->ty == Tarray) | |
1010 { | |
1011 assert(isaStruct(_type)); | |
1012 _init = llvm::ConstantAggregateZero::get(_type); | |
1013 } | |
1014 else if (t->ty == Tstruct) | |
1015 { | |
1016 const LLStructType* structty = isaStruct(_type); | |
1017 TypeStruct* ts = (TypeStruct*)t; | |
1018 assert(ts); | |
1019 assert(ts->sym); | |
1020 assert(ts->sym->ir.irStruct->constInit); | |
1021 _init = ts->sym->ir.irStruct->constInit; | |
1022 } | |
1023 else if (t->ty == Tclass) | |
1024 { | |
1025 _init = llvm::Constant::getNullValue(_type); | |
1026 } | |
1027 else { | |
1028 Logger::println("failed for type %s", t->toChars()); | |
1029 assert(0); | |
1030 } | |
1031 } | |
1032 | |
1033 return _init; | |
1034 } | |
1035 | |
1036 ////////////////////////////////////////////////////////////////////////////////////////// | |
1037 | |
1038 DValue* DtoInitializer(Initializer* init) | |
1039 { | |
1040 if (ExpInitializer* ex = init->isExpInitializer()) | |
1041 { | |
1042 Logger::println("expression initializer"); | |
1043 assert(ex->exp); | |
1044 return ex->exp->toElem(gIR); | |
1045 } | |
1046 else if (init->isVoidInitializer()) | |
1047 { | |
1048 // do nothing | |
1049 } | |
1050 else { | |
1051 Logger::println("unsupported initializer: %s", init->toChars()); | |
1052 assert(0); | |
1053 } | |
1054 return 0; | |
1055 } | |
1056 | |
1057 | |
1058 ////////////////////////////////////////////////////////////////////////////////////////// | |
1059 | |
1060 void DtoAnnotation(const char* str) | |
1061 { | |
1062 std::string s("CODE: "); | |
1063 s.append(str); | |
1064 char* p = &s[0]; | |
1065 while (*p) | |
1066 { | |
1067 if (*p == '"') | |
1068 *p = '\''; | |
1069 ++p; | |
1070 } | |
1071 // create a noop with the code as the result name! | |
1072 gIR->ir->CreateAnd(DtoConstSize_t(0),DtoConstSize_t(0),s.c_str()); | |
1073 } | |
1074 | |
1075 ////////////////////////////////////////////////////////////////////////////////////////// | |
1076 | |
1077 LLConstant* DtoTypeInfoOf(Type* type, bool base) | |
1078 { | |
1079 const LLType* typeinfotype = DtoType(Type::typeinfo->type); | |
1080 if (!type->vtinfo) | |
1081 type->getTypeInfo(NULL); | |
1082 TypeInfoDeclaration* tidecl = type->vtinfo; | |
1083 DtoForceDeclareDsymbol(tidecl); | |
1084 assert(tidecl->ir.irGlobal != NULL); | |
1085 LLConstant* c = isaConstant(tidecl->ir.irGlobal->value); | |
1086 assert(c != NULL); | |
1087 if (base) | |
1088 return llvm::ConstantExpr::getBitCast(c, typeinfotype); | |
1089 return c; | |
1090 } |