comparison gen/tollvm.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 a168a2c3ea48
children b604c56945b0
comparison
equal deleted inserted replaced
243:4d006f7b2ada 244:a95056b3c996
16 #include "gen/functions.h" 16 #include "gen/functions.h"
17 #include "gen/structs.h" 17 #include "gen/structs.h"
18 #include "gen/classes.h" 18 #include "gen/classes.h"
19 #include "gen/typeinf.h" 19 #include "gen/typeinf.h"
20 #include "gen/complex.h" 20 #include "gen/complex.h"
21 #include "gen/llvmhelpers.h"
21 22
22 bool DtoIsPassedByRef(Type* type) 23 bool DtoIsPassedByRef(Type* type)
23 { 24 {
24 Type* typ = DtoDType(type); 25 Type* typ = DtoDType(type);
25 TY t = typ->ty; 26 TY t = typ->ty;
50 { 51 {
51 // integers 52 // integers
52 case Tint8: 53 case Tint8:
53 case Tuns8: 54 case Tuns8:
54 case Tchar: 55 case Tchar:
55 return (const LLType*)llvm::Type::Int8Ty; 56 return (const LLType*)LLType::Int8Ty;
56 case Tint16: 57 case Tint16:
57 case Tuns16: 58 case Tuns16:
58 case Twchar: 59 case Twchar:
59 return (const LLType*)llvm::Type::Int16Ty; 60 return (const LLType*)LLType::Int16Ty;
60 case Tint32: 61 case Tint32:
61 case Tuns32: 62 case Tuns32:
62 case Tdchar: 63 case Tdchar:
63 return (const LLType*)llvm::Type::Int32Ty; 64 return (const LLType*)LLType::Int32Ty;
64 case Tint64: 65 case Tint64:
65 case Tuns64: 66 case Tuns64:
66 return (const LLType*)llvm::Type::Int64Ty; 67 return (const LLType*)LLType::Int64Ty;
67 68
68 case Tbool: 69 case Tbool:
69 return (const LLType*)llvm::ConstantInt::getTrue()->getType(); 70 return (const LLType*)llvm::ConstantInt::getTrue()->getType();
70 71
71 // floats 72 // floats
72 case Tfloat32: 73 case Tfloat32:
73 case Timaginary32: 74 case Timaginary32:
74 return llvm::Type::FloatTy; 75 return LLType::FloatTy;
75 case Tfloat64: 76 case Tfloat64:
76 case Timaginary64: 77 case Timaginary64:
77 return llvm::Type::DoubleTy; 78 return LLType::DoubleTy;
78 case Tfloat80: 79 case Tfloat80:
79 case Timaginary80: 80 case Timaginary80:
80 return (global.params.useFP80) ? llvm::Type::X86_FP80Ty : llvm::Type::DoubleTy; 81 return (global.params.useFP80) ? LLType::X86_FP80Ty : LLType::DoubleTy;
81 82
82 // complex 83 // complex
83 case Tcomplex32: 84 case Tcomplex32:
84 case Tcomplex64: 85 case Tcomplex64:
85 case Tcomplex80: 86 case Tcomplex80:
87 88
88 // pointers 89 // pointers
89 case Tpointer: { 90 case Tpointer: {
90 assert(t->next); 91 assert(t->next);
91 if (t->next->ty == Tvoid) 92 if (t->next->ty == Tvoid)
92 return (const LLType*)getPtrToType(llvm::Type::Int8Ty); 93 return (const LLType*)getPtrToType(LLType::Int8Ty);
93 else 94 else
94 return (const LLType*)getPtrToType(DtoType(t->next)); 95 return (const LLType*)getPtrToType(DtoType(t->next));
95 } 96 }
96 97
97 // arrays 98 // arrays
100 case Tsarray: 101 case Tsarray:
101 return DtoStaticArrayType(t); 102 return DtoStaticArrayType(t);
102 103
103 // void 104 // void
104 case Tvoid: 105 case Tvoid:
105 return llvm::Type::VoidTy; 106 return LLType::VoidTy;
106 107
107 // aggregates 108 // aggregates
108 case Tstruct: { 109 case Tstruct: {
109 if (!t->ir.type || *t->ir.type == NULL) {
110 // recursive or cyclic declaration
111 if (!gIR->structs.empty())
112 {
113 IrStruct* found = 0;
114 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i)
115 {
116 if (t == (*i)->type)
117 {
118 return (*i)->recty.get();
119 }
120 }
121 }
122 }
123
124 TypeStruct* ts = (TypeStruct*)t; 110 TypeStruct* ts = (TypeStruct*)t;
125 assert(ts->sym); 111 assert(ts->sym);
126 DtoResolveDsymbol(ts->sym); 112 DtoResolveDsymbol(ts->sym);
127 return ts->sym->ir.irStruct->recty.get(); // t->ir.type->get(); 113 return ts->sym->ir.irStruct->recty.get(); // t->ir.type->get();
128 } 114 }
129 115
130 case Tclass: { 116 case Tclass: {
131 /*if (!t].type || *gIR->irType[t->ir.type == NULL) {
132 // recursive or cyclic declaration
133 if (!gIR->structs.empty())
134 {
135 IrStruct* found = 0;
136 for (IRState::StructVector::iterator i=gIR->structs.begin(); i!=gIR->structs.end(); ++i)
137 {
138 if (t == (*i)->type)
139 {
140 return getPtrToType((*i)->recty.get());
141 }
142 }
143 }
144 Logger::println("no type found");
145 }*/
146
147 TypeClass* tc = (TypeClass*)t; 117 TypeClass* tc = (TypeClass*)t;
148 assert(tc->sym); 118 assert(tc->sym);
149 DtoResolveDsymbol(tc->sym); 119 DtoResolveDsymbol(tc->sym);
150 return getPtrToType(tc->sym->ir.irStruct->recty.get()); // t->ir.type->get()); 120 return getPtrToType(tc->sym->ir.irStruct->recty.get()); // t->ir.type->get());
151 } 121 }
184 154
185 // associative arrays 155 // associative arrays
186 case Taarray: 156 case Taarray:
187 { 157 {
188 TypeAArray* taa = (TypeAArray*)t; 158 TypeAArray* taa = (TypeAArray*)t;
189 std::vector<const LLType*> types; 159 return getPtrToType(LLStructType::get(DtoType(taa->key), DtoType(taa->next), 0));
190 types.push_back(DtoType(taa->key));
191 types.push_back(DtoType(taa->next));
192 return getPtrToType(llvm::StructType::get(types));
193 } 160 }
194 161
195 default: 162 default:
196 printf("trying to convert unknown type with value %d\n", t->ty); 163 printf("trying to convert unknown type with value %d\n", t->ty);
197 assert(0); 164 assert(0);
199 return 0; 166 return 0;
200 } 167 }
201 168
202 ////////////////////////////////////////////////////////////////////////////////////////// 169 //////////////////////////////////////////////////////////////////////////////////////////
203 170
204 const llvm::StructType* DtoDelegateType(Type* t) 171 const LLStructType* DtoDelegateType(Type* t)
205 { 172 {
206 const LLType* i8ptr = getPtrToType(llvm::Type::Int8Ty); 173 const LLType* i8ptr = getVoidPtrType();
207 const LLType* func = DtoFunctionType(t->next, i8ptr); 174 const LLType* func = DtoFunctionType(t->next, i8ptr);
208 const LLType* funcptr = getPtrToType(func); 175 const LLType* funcptr = getPtrToType(func);
209 176 return LLStructType::get(i8ptr, funcptr, 0);
210 std::vector<const LLType*> types;
211 types.push_back(i8ptr);
212 types.push_back(funcptr);
213 return llvm::StructType::get(types);
214 }
215
216 //////////////////////////////////////////////////////////////////////////////////////////
217
218 /*
219 static llvm::Function* LLVM_DeclareMemIntrinsic(const char* name, int bits, bool set=false)
220 {
221 assert(bits == 32 || bits == 64);
222 const LLType* int8ty = (const LLType*)llvm::Type::Int8Ty;
223 const LLType* int32ty = (const LLType*)llvm::Type::Int32Ty;
224 const LLType* int64ty = (const LLType*)llvm::Type::Int64Ty;
225 const LLType* int8ptrty = (const LLType*)getPtrToType(llvm::Type::Int8Ty);
226 const LLType* voidty = (const LLType*)llvm::Type::VoidTy;
227
228 assert(gIR);
229 assert(gIR->module);
230
231 // parameter types
232 std::vector<const LLType*> pvec;
233 pvec.push_back(int8ptrty);
234 pvec.push_back(set?int8ty:int8ptrty);
235 pvec.push_back(bits==32?int32ty:int64ty);
236 pvec.push_back(int32ty);
237 llvm::FunctionType* functype = llvm::FunctionType::get(voidty, pvec, false);
238 return llvm::cast<llvm::Function>(gIR->module->getOrInsertFunction(name, functype));
239 }
240 */
241
242 //////////////////////////////////////////////////////////////////////////////////////////
243
244 // llvm.memset.i32
245 llvm::Function* LLVM_DeclareMemSet32()
246 {
247 return GET_INTRINSIC_DECL(memset_i32);
248 }
249
250 //////////////////////////////////////////////////////////////////////////////////////////
251
252 llvm::Function* LLVM_DeclareMemSet64()
253 {
254 return GET_INTRINSIC_DECL(memset_i64);
255 }
256
257 //////////////////////////////////////////////////////////////////////////////////////////
258
259 // llvm.memcpy.i32
260 llvm::Function* LLVM_DeclareMemCpy32()
261 {
262 return GET_INTRINSIC_DECL(memcpy_i32);
263 }
264
265 //////////////////////////////////////////////////////////////////////////////////////////
266
267 // llvm.memcpy.i64
268 llvm::Function* LLVM_DeclareMemCpy64()
269 {
270 return GET_INTRINSIC_DECL(memcpy_i64);
271 }
272
273 void DtoMemoryBarrier(bool ll, bool ls, bool sl, bool ss, bool device)
274 {
275 llvm::Function* fn = GET_INTRINSIC_DECL(memory_barrier);
276 assert(fn != NULL);
277
278 LLSmallVector<LLValue*, 5> llargs;
279 llargs.push_back(DtoConstBool(ll));
280 llargs.push_back(DtoConstBool(ls));
281 llargs.push_back(DtoConstBool(sl));
282 llargs.push_back(DtoConstBool(ss));
283 llargs.push_back(DtoConstBool(device));
284
285 llvm::CallInst::Create(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
286 }
287
288 //////////////////////////////////////////////////////////////////////////////////////////
289
290 void DtoDelegateToNull(LLValue* v)
291 {
292 LLSmallVector<LLValue*, 4> args;
293 args.push_back(DtoBitCast(v, getVoidPtrType()));
294 args.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty));
295 args.push_back(DtoConstInt(global.params.is64bit ? 16 : 8));
296 args.push_back(DtoConstInt(0));
297 gIR->ir->CreateCall(GET_INTRINSIC_DECL(memset_i32), args.begin(), args.end(), "");
298 }
299
300 //////////////////////////////////////////////////////////////////////////////////////////
301
302 void DtoDelegateCopy(LLValue* dst, LLValue* src)
303 {
304 LLSmallVector<LLValue*, 4> args;
305 args.push_back(DtoBitCast(dst,getVoidPtrType()));
306 args.push_back(DtoBitCast(src,getVoidPtrType()));
307 args.push_back(DtoConstInt(global.params.is64bit ? 16 : 8));
308 args.push_back(DtoConstInt(0));
309 gIR->ir->CreateCall(GET_INTRINSIC_DECL(memcpy_i32), args.begin(), args.end(), "");
310 } 177 }
311 178
312 ////////////////////////////////////////////////////////////////////////////////////////// 179 //////////////////////////////////////////////////////////////////////////////////////////
313 180
314 LLValue* DtoDelegateCompare(TOK op, LLValue* lhs, LLValue* rhs) 181 LLValue* DtoDelegateCompare(TOK op, LLValue* lhs, LLValue* rhs)
316 Logger::println("Doing delegate compare"); 183 Logger::println("Doing delegate compare");
317 llvm::ICmpInst::Predicate pred = (op == TOKequal || op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; 184 llvm::ICmpInst::Predicate pred = (op == TOKequal || op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE;
318 llvm::Value *b1, *b2; 185 llvm::Value *b1, *b2;
319 if (rhs == NULL) 186 if (rhs == NULL)
320 { 187 {
321 LLValue* l = gIR->ir->CreateLoad(DtoGEPi(lhs,0,0,"tmp"),"tmp"); 188 LLValue* l = DtoLoad(DtoGEPi(lhs,0,0));
322 LLValue* r = llvm::Constant::getNullValue(l->getType()); 189 LLValue* r = llvm::Constant::getNullValue(l->getType());
323 b1 = gIR->ir->CreateICmp(pred,l,r,"tmp"); 190 b1 = gIR->ir->CreateICmp(pred,l,r,"tmp");
324 l = gIR->ir->CreateLoad(DtoGEPi(lhs,0,1,"tmp"),"tmp"); 191 l = DtoLoad(DtoGEPi(lhs,0,1));
325 r = llvm::Constant::getNullValue(l->getType()); 192 r = llvm::Constant::getNullValue(l->getType());
326 b2 = gIR->ir->CreateICmp(pred,l,r,"tmp"); 193 b2 = gIR->ir->CreateICmp(pred,l,r,"tmp");
327 } 194 }
328 else 195 else
329 { 196 {
330 LLValue* l = gIR->ir->CreateLoad(DtoGEPi(lhs,0,0,"tmp"),"tmp"); 197 LLValue* l = DtoLoad(DtoGEPi(lhs,0,0));
331 LLValue* r = gIR->ir->CreateLoad(DtoGEPi(rhs,0,0,"tmp"),"tmp"); 198 LLValue* r = DtoLoad(DtoGEPi(rhs,0,0));
332 b1 = gIR->ir->CreateICmp(pred,l,r,"tmp"); 199 b1 = gIR->ir->CreateICmp(pred,l,r,"tmp");
333 l = gIR->ir->CreateLoad(DtoGEPi(lhs,0,1,"tmp"),"tmp"); 200 l = DtoLoad(DtoGEPi(lhs,0,1));
334 r = gIR->ir->CreateLoad(DtoGEPi(rhs,0,1,"tmp"),"tmp"); 201 r = DtoLoad(DtoGEPi(rhs,0,1));
335 b2 = gIR->ir->CreateICmp(pred,l,r,"tmp"); 202 b2 = gIR->ir->CreateICmp(pred,l,r,"tmp");
336 } 203 }
337 LLValue* b = gIR->ir->CreateAnd(b1,b2,"tmp"); 204 LLValue* b = gIR->ir->CreateAnd(b1,b2,"tmp");
338 if (op == TOKnotequal || op == TOKnotidentity) 205 if (op == TOKnotequal || op == TOKnotidentity)
339 return gIR->ir->CreateNot(b,"tmp"); 206 return gIR->ir->CreateNot(b,"tmp");
340 return b; 207 return b;
341 } 208 }
342 209
343 ////////////////////////////////////////////////////////////////////////////////////////// 210 //////////////////////////////////////////////////////////////////////////////////////////
344 211
345 llvm::GlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym) 212 LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
346 { 213 {
347 // global variable 214 // global variable
348 if (VarDeclaration* vd = sym->isVarDeclaration()) 215 if (VarDeclaration* vd = sym->isVarDeclaration())
349 { 216 {
350 // template 217 // template
382 assert(0 && "not global/function"); 249 assert(0 && "not global/function");
383 } 250 }
384 251
385 // default to external linkage 252 // default to external linkage
386 return llvm::GlobalValue::ExternalLinkage; 253 return llvm::GlobalValue::ExternalLinkage;
387
388 // llvm linkage types
389 /* ExternalLinkage = 0, LinkOnceLinkage, WeakLinkage, AppendingLinkage,
390 InternalLinkage, DLLImportLinkage, DLLExportLinkage, ExternalWeakLinkage,
391 GhostLinkage */
392 } 254 }
393 255
394 llvm::GlobalValue::LinkageTypes DtoInternalLinkage(Dsymbol* sym) 256 llvm::GlobalValue::LinkageTypes DtoInternalLinkage(Dsymbol* sym)
395 { 257 {
396 if (DtoIsTemplateInstance(sym)) 258 if (DtoIsTemplateInstance(sym))
435 // ptr is integer pointer 297 // ptr is integer pointer
436 else if (ptrTy->isInteger()) 298 else if (ptrTy->isInteger())
437 { 299 {
438 // val is integer 300 // val is integer
439 assert(valTy->isInteger()); 301 assert(valTy->isInteger());
440 const llvm::IntegerType* pt = llvm::cast<const llvm::IntegerType>(ptrTy); 302 const LLIntegerType* pt = llvm::cast<const LLIntegerType>(ptrTy);
441 const llvm::IntegerType* vt = llvm::cast<const llvm::IntegerType>(valTy); 303 const LLIntegerType* vt = llvm::cast<const LLIntegerType>(valTy);
442 if (pt->getBitWidth() < vt->getBitWidth()) { 304 if (pt->getBitWidth() < vt->getBitWidth()) {
443 return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb()); 305 return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb());
444 } 306 }
445 else 307 else
446 assert(0); 308 assert(0);
459 LLValue* DtoBoolean(LLValue* val) 321 LLValue* DtoBoolean(LLValue* val)
460 { 322 {
461 const LLType* t = val->getType(); 323 const LLType* t = val->getType();
462 if (t->isInteger()) 324 if (t->isInteger())
463 { 325 {
464 if (t == llvm::Type::Int1Ty) 326 if (t == LLType::Int1Ty)
465 return val; 327 return val;
466 else { 328 else {
467 LLValue* zero = llvm::ConstantInt::get(t, 0, false); 329 LLValue* zero = llvm::ConstantInt::get(t, 0, false);
468 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb()); 330 return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb());
469 } 331 }
482 344
483 ////////////////////////////////////////////////////////////////////////////////////////// 345 //////////////////////////////////////////////////////////////////////////////////////////
484 346
485 const LLType* DtoSize_t() 347 const LLType* DtoSize_t()
486 { 348 {
487 if (global.params.is64bit) 349 // the type of size_t does not change once set
488 return llvm::Type::Int64Ty; 350 static const LLType* t = NULL;
489 else 351 if (t == NULL)
490 return llvm::Type::Int32Ty; 352 t = (global.params.is64bit) ? LLType::Int64Ty : LLType::Int32Ty;
491 } 353 return t;
492 354 }
493 ////////////////////////////////////////////////////////////////////////////////////////// 355
494 356 //////////////////////////////////////////////////////////////////////////////////////////
495 LLConstant* DtoConstInitializer(Type* type, Initializer* init) 357
496 { 358 LLValue* DtoGEP1(LLValue* ptr, LLValue* i0, const char* var, llvm::BasicBlock* bb)
497 LLConstant* _init = 0; // may return zero 359 {
498 if (!init) 360 return llvm::GetElementPtrInst::Create(ptr, i0, var?var:"tmp", bb?bb:gIR->scopebb());
499 {
500 Logger::println("const default initializer for %s", type->toChars());
501 _init = type->defaultInit()->toConstElem(gIR);
502 }
503 else if (ExpInitializer* ex = init->isExpInitializer())
504 {
505 Logger::println("const expression initializer");
506 _init = ex->exp->toConstElem(gIR);
507 }
508 else if (StructInitializer* si = init->isStructInitializer())
509 {
510 Logger::println("const struct initializer");
511 _init = DtoConstStructInitializer(si);
512 }
513 else if (ArrayInitializer* ai = init->isArrayInitializer())
514 {
515 Logger::println("const array initializer");
516 _init = DtoConstArrayInitializer(ai);
517 }
518 else if (init->isVoidInitializer())
519 {
520 Logger::println("const void initializer");
521 const LLType* ty = DtoType(type);
522 _init = llvm::Constant::getNullValue(ty);
523 }
524 else {
525 Logger::println("unsupported const initializer: %s", init->toChars());
526 }
527 return _init;
528 }
529
530 //////////////////////////////////////////////////////////////////////////////////////////
531
532 LLConstant* DtoConstFieldInitializer(Type* t, Initializer* init)
533 {
534 Logger::println("DtoConstFieldInitializer");
535 LOG_SCOPE;
536
537 const LLType* _type = DtoType(t);
538
539 LLConstant* _init = DtoConstInitializer(t, init);
540 assert(_init);
541 if (_type != _init->getType())
542 {
543 Logger::cout() << "field init is: " << *_init << " type should be " << *_type << '\n';
544 if (t->ty == Tsarray)
545 {
546 const llvm::ArrayType* arrty = isaArray(_type);
547 uint64_t n = arrty->getNumElements();
548 std::vector<LLConstant*> vals(n,_init);
549 _init = llvm::ConstantArray::get(arrty, vals);
550 }
551 else if (t->ty == Tarray)
552 {
553 assert(isaStruct(_type));
554 _init = llvm::ConstantAggregateZero::get(_type);
555 }
556 else if (t->ty == Tstruct)
557 {
558 const llvm::StructType* structty = isaStruct(_type);
559 TypeStruct* ts = (TypeStruct*)t;
560 assert(ts);
561 assert(ts->sym);
562 assert(ts->sym->ir.irStruct->constInit);
563 _init = ts->sym->ir.irStruct->constInit;
564 }
565 else if (t->ty == Tclass)
566 {
567 _init = llvm::Constant::getNullValue(_type);
568 }
569 else {
570 Logger::println("failed for type %s", t->toChars());
571 assert(0);
572 }
573 }
574
575 return _init;
576 }
577
578 //////////////////////////////////////////////////////////////////////////////////////////
579
580 DValue* DtoInitializer(Initializer* init)
581 {
582 if (ExpInitializer* ex = init->isExpInitializer())
583 {
584 Logger::println("expression initializer");
585 assert(ex->exp);
586 return ex->exp->toElem(gIR);
587 }
588 else if (init->isVoidInitializer())
589 {
590 // do nothing
591 }
592 else {
593 Logger::println("unsupported initializer: %s", init->toChars());
594 assert(0);
595 }
596 return 0;
597 } 361 }
598 362
599 ////////////////////////////////////////////////////////////////////////////////////////// 363 //////////////////////////////////////////////////////////////////////////////////////////
600 364
601 LLValue* DtoGEP(LLValue* ptr, LLValue* i0, LLValue* i1, const char* var, llvm::BasicBlock* bb) 365 LLValue* DtoGEP(LLValue* ptr, LLValue* i0, LLValue* i1, const char* var, llvm::BasicBlock* bb)
602 { 366 {
603 LLSmallVector<LLValue*,2> v(2); 367 LLSmallVector<LLValue*,2> v(2);
604 v[0] = i0; 368 v[0] = i0;
605 v[1] = i1; 369 v[1] = i1;
606 return llvm::GetElementPtrInst::Create(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb()); 370 return llvm::GetElementPtrInst::Create(ptr, v.begin(), v.end(), var?var:"tmp", bb?bb:gIR->scopebb());
607 } 371 }
608 372
609 ////////////////////////////////////////////////////////////////////////////////////////// 373 //////////////////////////////////////////////////////////////////////////////////////////
610 374
611 LLValue* DtoGEPi(LLValue* ptr, const DStructIndexVector& src, const char* var, llvm::BasicBlock* bb) 375 LLValue* DtoGEPi(LLValue* ptr, const DStructIndexVector& src, const char* var, llvm::BasicBlock* bb)
615 379
616 size_t j=0; 380 size_t j=0;
617 for (DStructIndexVector::const_iterator i=src.begin(); i!=src.end(); ++i) 381 for (DStructIndexVector::const_iterator i=src.begin(); i!=src.end(); ++i)
618 dst[j++] = DtoConstUint(*i); 382 dst[j++] = DtoConstUint(*i);
619 383
620 return llvm::GetElementPtrInst::Create(ptr, dst.begin(), dst.end(), var, bb?bb:gIR->scopebb()); 384 return llvm::GetElementPtrInst::Create(ptr, dst.begin(), dst.end(), var?var:"tmp", bb?bb:gIR->scopebb());
621 } 385 }
622 386
623 ////////////////////////////////////////////////////////////////////////////////////////// 387 //////////////////////////////////////////////////////////////////////////////////////////
624 388
625 LLValue* DtoGEPi(LLValue* ptr, unsigned i, const char* var, llvm::BasicBlock* bb) 389 LLValue* DtoGEPi1(LLValue* ptr, unsigned i, const char* var, llvm::BasicBlock* bb)
626 { 390 {
627 return llvm::GetElementPtrInst::Create(ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false), var, bb?bb:gIR->scopebb()); 391 return llvm::GetElementPtrInst::Create(ptr, DtoConstUint(i), var?var:"tmp", bb?bb:gIR->scopebb());
628 } 392 }
629 393
630 ////////////////////////////////////////////////////////////////////////////////////////// 394 //////////////////////////////////////////////////////////////////////////////////////////
631 395
632 LLValue* DtoGEPi(LLValue* ptr, unsigned i0, unsigned i1, const char* var, llvm::BasicBlock* bb) 396 LLValue* DtoGEPi(LLValue* ptr, unsigned i0, unsigned i1, const char* var, llvm::BasicBlock* bb)
633 { 397 {
634 LLSmallVector<LLValue*,2> v(2); 398 LLSmallVector<LLValue*,2> v(2);
635 v[0] = DtoConstUint(i0); 399 v[0] = DtoConstUint(i0);
636 v[1] = DtoConstUint(i1); 400 v[1] = DtoConstUint(i1);
637 return llvm::GetElementPtrInst::Create(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb()); 401 return llvm::GetElementPtrInst::Create(ptr, v.begin(), v.end(), var?var:"tmp", bb?bb:gIR->scopebb());
638 } 402 }
639 403
640 ////////////////////////////////////////////////////////////////////////////////////////// 404 //////////////////////////////////////////////////////////////////////////////////////////
641 405
642 LLValue* DtoNew(Type* newtype) 406 void DtoMemSetZero(LLValue* dst, LLValue* nbytes)
643 { 407 {
644 // get runtime function 408 dst = DtoBitCast(dst,getVoidPtrType());
645 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_allocmemoryT"); 409
646 // get type info 410 llvm::Function* fn;
647 LLConstant* ti = DtoTypeInfoOf(newtype); 411 if (global.params.is64bit)
648 assert(isaPointer(ti)); 412 fn = GET_INTRINSIC_DECL(memset_i64);
649 // call runtime allocator 413 else
650 LLValue* mem = gIR->ir->CreateCall(fn, ti, ".gc_mem"); 414 fn = GET_INTRINSIC_DECL(memset_i32);
651 // cast 415
652 return DtoBitCast(mem, getPtrToType(DtoType(newtype)), ".gc_mem"); 416 gIR->ir->CreateCall4(fn, dst, DtoConstUbyte(0), nbytes, DtoConstUint(0), "");
653 } 417 }
654 418
655 void DtoDeleteMemory(LLValue* ptr) 419 //////////////////////////////////////////////////////////////////////////////////////////
656 { 420
657 // get runtime function 421 void DtoMemCpy(LLValue* dst, LLValue* src, LLValue* nbytes)
658 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delmemory"); 422 {
659 // build args 423 dst = DtoBitCast(dst,getVoidPtrType());
660 LLSmallVector<LLValue*,1> arg; 424 src = DtoBitCast(src,getVoidPtrType());
661 arg.push_back(DtoBitCast(ptr, getVoidPtrType(), ".tmp")); 425
662 // call 426 llvm::Function* fn;
663 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); 427 if (global.params.is64bit)
664 } 428 fn = GET_INTRINSIC_DECL(memcpy_i64);
665 429 else
666 void DtoDeleteClass(LLValue* inst) 430 fn = GET_INTRINSIC_DECL(memcpy_i32);
667 { 431
668 // get runtime function 432 gIR->ir->CreateCall4(fn, dst, src, nbytes, DtoConstUint(0), "");
669 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delclass"); 433 }
670 // build args 434
671 LLSmallVector<LLValue*,1> arg; 435 //////////////////////////////////////////////////////////////////////////////////////////
672 arg.push_back(DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")); 436
673 // call 437 void DtoAggrZeroInit(LLValue* v)
674 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); 438 {
675 } 439 uint64_t n = getTypeStoreSize(v->getType()->getContainedType(0));
676 440 DtoMemSetZero(v, DtoConstSize_t(n));
677 void DtoDeleteInterface(LLValue* inst) 441 }
678 { 442
679 // get runtime function 443 //////////////////////////////////////////////////////////////////////////////////////////
680 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delinterface"); 444
681 // build args 445 void DtoAggrCopy(LLValue* dst, LLValue* src)
682 LLSmallVector<LLValue*,1> arg; 446 {
683 arg.push_back(DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")); 447 uint64_t n = getTypeStoreSize(dst->getType()->getContainedType(0));
684 // call 448 DtoMemCpy(dst, src, DtoConstSize_t(n));
685 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); 449 }
686 } 450
687 451 //////////////////////////////////////////////////////////////////////////////////////////
688 void DtoDeleteArray(DValue* arr) 452
689 { 453 void DtoMemoryBarrier(bool ll, bool ls, bool sl, bool ss, bool device)
690 // get runtime function 454 {
691 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delarray"); 455 llvm::Function* fn = GET_INTRINSIC_DECL(memory_barrier);
692 // build args 456 assert(fn != NULL);
693 LLSmallVector<LLValue*,2> arg; 457
694 arg.push_back(DtoArrayLen(arr)); 458 LLSmallVector<LLValue*, 5> llargs;
695 arg.push_back(DtoBitCast(DtoArrayPtr(arr), getVoidPtrType(), ".tmp")); 459 llargs.push_back(DtoConstBool(ll));
696 // call 460 llargs.push_back(DtoConstBool(ls));
697 llvm::CallInst::Create(fn, arg.begin(), arg.end(), "", gIR->scopebb()); 461 llargs.push_back(DtoConstBool(sl));
698 } 462 llargs.push_back(DtoConstBool(ss));
699 463 llargs.push_back(DtoConstBool(device));
700 ////////////////////////////////////////////////////////////////////////////////////////// 464
701 465 llvm::CallInst::Create(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
702 void DtoAssert(Loc* loc, DValue* msg)
703 {
704 std::vector<LLValue*> args;
705 LLConstant* c;
706
707 // func
708 const char* fname = msg ? "_d_assert_msg" : "_d_assert";
709 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname);
710
711 // param attrs
712 llvm::PAListPtr palist;
713 int idx = 1;
714
715 c = DtoConstString(loc->filename);
716
717 // msg param
718 if (msg)
719 {
720 if (DSliceValue* s = msg->isSlice())
721 {
722 llvm::AllocaInst* alloc = gIR->func()->msgArg;
723 if (!alloc)
724 {
725 alloc = new llvm::AllocaInst(c->getType(), ".assertmsg", gIR->topallocapoint());
726 DtoSetArray(alloc, DtoArrayLen(s), DtoArrayPtr(s));
727 gIR->func()->msgArg = alloc;
728 }
729 args.push_back(alloc);
730 }
731 else
732 {
733 args.push_back(msg->getRVal());
734 }
735 palist = palist.addAttr(idx++, llvm::ParamAttr::ByVal);
736 }
737
738 // file param
739 llvm::AllocaInst* alloc = gIR->func()->srcfileArg;
740 if (!alloc)
741 {
742 alloc = new llvm::AllocaInst(c->getType(), ".srcfile", gIR->topallocapoint());
743 gIR->func()->srcfileArg = alloc;
744 }
745 LLValue* ptr = DtoGEPi(alloc, 0,0, "tmp");
746 DtoStore(c->getOperand(0), ptr);
747 ptr = DtoGEPi(alloc, 0,1, "tmp");
748 DtoStore(c->getOperand(1), ptr);
749
750 args.push_back(alloc);
751 palist = palist.addAttr(idx++, llvm::ParamAttr::ByVal);
752
753
754 // line param
755 c = DtoConstUint(loc->linnum);
756 args.push_back(c);
757
758 // call
759 llvm::CallInst* call = llvm::CallInst::Create(fn, args.begin(), args.end(), "", gIR->scopebb());
760 call->setParamAttrs(palist);
761 }
762
763 //////////////////////////////////////////////////////////////////////////////////////////
764
765 static const LLType* get_next_frame_ptr_type(Dsymbol* sc)
766 {
767 assert(sc->isFuncDeclaration() || sc->isClassDeclaration());
768 Dsymbol* p = sc->toParent2();
769 if (!p->isFuncDeclaration() && !p->isClassDeclaration())
770 Logger::println("unexpected parent symbol found while resolving frame pointer - '%s' kind: '%s'", p->toChars(), p->kind());
771 assert(p->isFuncDeclaration() || p->isClassDeclaration());
772 if (FuncDeclaration* fd = p->isFuncDeclaration())
773 {
774 LLValue* v = fd->ir.irFunc->nestedVar;
775 assert(v);
776 return v->getType();
777 }
778 else if (ClassDeclaration* cd = p->isClassDeclaration())
779 {
780 return DtoType(cd->type);
781 }
782 else
783 {
784 Logger::println("symbol: '%s' kind: '%s'", sc->toChars(), sc->kind());
785 assert(0);
786 }
787 }
788
789 //////////////////////////////////////////////////////////////////////////////////////////
790
791 static LLValue* get_frame_ptr_impl(FuncDeclaration* func, Dsymbol* sc, LLValue* v)
792 {
793 LOG_SCOPE;
794 if (sc == func)
795 {
796 return v;
797 }
798 else if (FuncDeclaration* fd = sc->isFuncDeclaration())
799 {
800 Logger::println("scope is function: %s", fd->toChars());
801
802 if (fd->toParent2() == func)
803 {
804 if (!func->ir.irFunc->nestedVar)
805 return NULL;
806 return DtoBitCast(v, func->ir.irFunc->nestedVar->getType());
807 }
808
809 v = DtoBitCast(v, get_next_frame_ptr_type(fd));
810 Logger::cout() << "v = " << *v << '\n';
811
812 if (fd->toParent2()->isFuncDeclaration())
813 {
814 v = DtoGEPi(v, 0,0, "tmp");
815 v = DtoLoad(v);
816 }
817 else if (ClassDeclaration* cd = fd->toParent2()->isClassDeclaration())
818 {
819 size_t idx = 2;
820 //idx += cd->ir.irStruct->interfaceVec.size();
821 v = DtoGEPi(v,0,idx,"tmp");
822 v = DtoLoad(v);
823 }
824 else
825 {
826 assert(0);
827 }
828 return get_frame_ptr_impl(func, fd->toParent2(), v);
829 }
830 else if (ClassDeclaration* cd = sc->isClassDeclaration())
831 {
832 Logger::println("scope is class: %s", cd->toChars());
833 /*size_t idx = 2;
834 idx += cd->llvmIrStruct->interfaces.size();
835 v = DtoGEPi(v,0,idx,"tmp");
836 Logger::cout() << "gep = " << *v << '\n';
837 v = DtoLoad(v);*/
838 return get_frame_ptr_impl(func, cd->toParent2(), v);
839 }
840 else
841 {
842 Logger::println("symbol: '%s'", sc->toPrettyChars());
843 assert(0);
844 }
845 }
846
847 //////////////////////////////////////////////////////////////////////////////////////////
848
849 static LLValue* get_frame_ptr(FuncDeclaration* func)
850 {
851 Logger::println("Resolving context pointer for nested function: '%s'", func->toPrettyChars());
852 LOG_SCOPE;
853 IrFunction* irfunc = gIR->func();
854
855 // in the right scope already
856 if (func == irfunc->decl)
857 return irfunc->decl->ir.irFunc->nestedVar;
858
859 // use the 'this' pointer
860 LLValue* ptr = irfunc->decl->ir.irFunc->thisVar;
861 assert(ptr);
862
863 // return the fully resolved frame pointer
864 ptr = get_frame_ptr_impl(func, irfunc->decl, ptr);
865 if (ptr) Logger::cout() << "Found context!" << *ptr;
866 else Logger::cout() << "NULL context!\n";
867
868 return ptr;
869 }
870
871 //////////////////////////////////////////////////////////////////////////////////////////
872
873 LLValue* DtoNestedContext(FuncDeclaration* func)
874 {
875 // resolve frame ptr
876 LLValue* ptr = get_frame_ptr(func);
877 Logger::cout() << "Nested context ptr = ";
878 if (ptr) Logger::cout() << *ptr;
879 else Logger::cout() << "NULL";
880 Logger::cout() << '\n';
881 return ptr;
882 }
883
884 //////////////////////////////////////////////////////////////////////////////////////////
885
886 static void print_frame_worker(VarDeclaration* vd, Dsymbol* par)
887 {
888 if (vd->toParent2() == par)
889 {
890 Logger::println("found: '%s' kind: '%s'", par->toChars(), par->kind());
891 return;
892 }
893
894 Logger::println("diving into: '%s' kind: '%s'", par->toChars(), par->kind());
895 LOG_SCOPE;
896 print_frame_worker(vd, par->toParent2());
897 }
898
899 //////////////////////////////////////////////////////////////////////////////////////////
900
901 static void print_nested_frame_list(VarDeclaration* vd, Dsymbol* par)
902 {
903 Logger::println("Frame pointer list for nested var: '%s'", vd->toPrettyChars());
904 LOG_SCOPE;
905 if (vd->toParent2() != par)
906 print_frame_worker(vd, par);
907 else
908 Logger::println("Found at level 0");
909 Logger::println("Done");
910 }
911
912 //////////////////////////////////////////////////////////////////////////////////////////
913
914 LLValue* DtoNestedVariable(VarDeclaration* vd)
915 {
916 // log the frame list
917 IrFunction* irfunc = gIR->func();
918 if (Logger::enabled())
919 print_nested_frame_list(vd, irfunc->decl);
920
921 // resolve frame ptr
922 FuncDeclaration* func = vd->toParent2()->isFuncDeclaration();
923 assert(func);
924 LLValue* ptr = DtoNestedContext(func);
925 assert(ptr && "nested var, but no context");
926
927 // we must cast here to be sure. nested classes just have a void*
928 ptr = DtoBitCast(ptr, func->ir.irFunc->nestedVar->getType());
929
930 // index nested var and load (if necessary)
931 LLValue* v = DtoGEPi(ptr, 0, vd->ir.irLocal->nestedIndex, "tmp");
932 // references must be loaded, for normal variables this IS already the variable storage!!!
933 if (vd->isParameter() && (vd->isRef() || vd->isOut() || DtoIsPassedByRef(vd->type)))
934 v = DtoLoad(v);
935
936 // log and return
937 Logger::cout() << "Nested var ptr = " << *v << '\n';
938 return v;
939 }
940
941 //////////////////////////////////////////////////////////////////////////////////////////
942
943 void DtoAssign(DValue* lhs, DValue* rhs)
944 {
945 Logger::cout() << "DtoAssign(...);\n";
946 LOG_SCOPE;
947
948 Type* t = DtoDType(lhs->getType());
949 Type* t2 = DtoDType(rhs->getType());
950
951 if (t->ty == Tstruct) {
952 if (t2 != t) {
953 // TODO: fix this, use 'rhs' for something
954 DtoStructZeroInit(lhs->getLVal());
955 }
956 else if (!rhs->inPlace()) {
957 DtoStructCopy(lhs->getLVal(),rhs->getRVal());
958 }
959 }
960 else if (t->ty == Tarray) {
961 // lhs is slice
962 if (DSliceValue* s = lhs->isSlice()) {
963 if (DSliceValue* s2 = rhs->isSlice()) {
964 DtoArrayCopySlices(s, s2);
965 }
966 else if (t->next == t2) {
967 if (s->len)
968 DtoArrayInit(s->ptr, s->len, rhs->getRVal());
969 else
970 DtoArrayInit(s->ptr, rhs->getRVal());
971 }
972 else {
973 DtoArrayCopyToSlice(s, rhs);
974 }
975 }
976 // rhs is slice
977 else if (DSliceValue* s = rhs->isSlice()) {
978 assert(s->getType()->toBasetype() == lhs->getType()->toBasetype());
979 DtoSetArray(lhs->getLVal(),DtoArrayLen(s),DtoArrayPtr(s));
980 }
981 // null
982 else if (rhs->isNull()) {
983 DtoSetArrayToNull(lhs->getLVal());
984 }
985 // reference assignment
986 else {
987 DtoArrayAssign(lhs->getLVal(), rhs->getRVal());
988 }
989 }
990 else if (t->ty == Tsarray) {
991 if (DtoType(lhs->getType()) == DtoType(rhs->getType())) {
992 DtoStaticArrayCopy(lhs->getLVal(), rhs->getRVal());
993 }
994 else {
995 DtoArrayInit(lhs->getLVal(), rhs->getRVal());
996 }
997 }
998 else if (t->ty == Tdelegate) {
999 if (rhs->isNull())
1000 DtoDelegateToNull(lhs->getLVal());
1001 else if (!rhs->inPlace()) {
1002 LLValue* l = lhs->getLVal();
1003 LLValue* r = rhs->getRVal();
1004 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n';
1005 DtoDelegateCopy(l, r);
1006 }
1007 }
1008 else if (t->ty == Tclass) {
1009 assert(t2->ty == Tclass);
1010 // assignment to this in constructor special case
1011 if (lhs->isThis()) {
1012 LLValue* tmp = rhs->getRVal();
1013 FuncDeclaration* fdecl = gIR->func()->decl;
1014 // respecify the this param
1015 if (!llvm::isa<llvm::AllocaInst>(fdecl->ir.irFunc->thisVar))
1016 fdecl->ir.irFunc->thisVar = new llvm::AllocaInst(tmp->getType(), "newthis", gIR->topallocapoint());
1017 DtoStore(tmp, fdecl->ir.irFunc->thisVar);
1018 }
1019 // regular class ref -> class ref assignment
1020 else {
1021 DtoStore(rhs->getRVal(), lhs->getLVal());
1022 }
1023 }
1024 else if (t->iscomplex()) {
1025 assert(!lhs->isComplex());
1026
1027 LLValue* dst;
1028 if (DLRValue* lr = lhs->isLRValue()) {
1029 dst = lr->getLVal();
1030 rhs = DtoCastComplex(rhs, lr->getLType());
1031 }
1032 else {
1033 dst = lhs->getRVal();
1034 }
1035
1036 if (DComplexValue* cx = rhs->isComplex())
1037 DtoComplexSet(dst, cx->re, cx->im);
1038 else
1039 DtoComplexAssign(dst, rhs->getRVal());
1040 }
1041 else {
1042 LLValue* l = lhs->getLVal();
1043 LLValue* r = rhs->getRVal();
1044 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n';
1045 const LLType* lit = l->getType()->getContainedType(0);
1046 if (r->getType() != lit) {
1047 // handle lvalue cast assignments
1048 if (DLRValue* lr = lhs->isLRValue()) {
1049 Logger::println("lvalue cast!");
1050 r = DtoCast(rhs, lr->getLType())->getRVal();
1051 }
1052 else {
1053 r = DtoCast(rhs, lhs->getType())->getRVal();
1054 }
1055 Logger::cout() << "really assign\nlhs: " << *l << "rhs: " << *r << '\n';
1056 assert(r->getType() == l->getType()->getContainedType(0));
1057 }
1058 gIR->ir->CreateStore(r, l);
1059 }
1060 }
1061
1062 //////////////////////////////////////////////////////////////////////////////////////////
1063 DValue* DtoCastInt(DValue* val, Type* _to)
1064 {
1065 const LLType* tolltype = DtoType(_to);
1066
1067 Type* to = DtoDType(_to);
1068 Type* from = DtoDType(val->getType());
1069 assert(from->isintegral());
1070
1071 size_t fromsz = from->size();
1072 size_t tosz = to->size();
1073
1074 LLValue* rval = val->getRVal();
1075 if (rval->getType() == tolltype) {
1076 return new DImValue(_to, rval);
1077 }
1078
1079 if (to->isintegral()) {
1080 if (fromsz < tosz) {
1081 Logger::cout() << "cast to: " << *tolltype << '\n';
1082 if (from->isunsigned() || from->ty == Tbool) {
1083 rval = new llvm::ZExtInst(rval, tolltype, "tmp", gIR->scopebb());
1084 } else {
1085 rval = new llvm::SExtInst(rval, tolltype, "tmp", gIR->scopebb());
1086 }
1087 }
1088 else if (fromsz > tosz) {
1089 rval = new llvm::TruncInst(rval, tolltype, "tmp", gIR->scopebb());
1090 }
1091 else {
1092 rval = DtoBitCast(rval, tolltype);
1093 }
1094 }
1095 else if (to->isfloating()) {
1096 if (from->isunsigned()) {
1097 rval = new llvm::UIToFPInst(rval, tolltype, "tmp", gIR->scopebb());
1098 }
1099 else {
1100 rval = new llvm::SIToFPInst(rval, tolltype, "tmp", gIR->scopebb());
1101 }
1102 }
1103 else if (to->ty == Tpointer) {
1104 Logger::cout() << "cast pointer: " << *tolltype << '\n';
1105 rval = gIR->ir->CreateIntToPtr(rval, tolltype, "tmp");
1106 }
1107 else {
1108 assert(0 && "bad int cast");
1109 }
1110
1111 return new DImValue(_to, rval);
1112 }
1113
1114 DValue* DtoCastPtr(DValue* val, Type* to)
1115 {
1116 const LLType* tolltype = DtoType(to);
1117
1118 Type* totype = DtoDType(to);
1119 Type* fromtype = DtoDType(val->getType());
1120 assert(fromtype->ty == Tpointer || fromtype->ty == Tfunction);
1121
1122 LLValue* rval;
1123
1124 if (totype->ty == Tpointer || totype->ty == Tclass) {
1125 LLValue* src = val->getRVal();
1126 Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n';
1127 rval = DtoBitCast(src, tolltype);
1128 }
1129 else if (totype->isintegral()) {
1130 rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
1131 }
1132 else {
1133 Logger::println("invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars());
1134 assert(0);
1135 }
1136
1137 return new DImValue(to, rval);
1138 }
1139
1140 DValue* DtoCastFloat(DValue* val, Type* to)
1141 {
1142 if (val->getType() == to)
1143 return val;
1144
1145 const LLType* tolltype = DtoType(to);
1146
1147 Type* totype = DtoDType(to);
1148 Type* fromtype = DtoDType(val->getType());
1149 assert(fromtype->isfloating());
1150
1151 size_t fromsz = fromtype->size();
1152 size_t tosz = totype->size();
1153
1154 LLValue* rval;
1155
1156 if (totype->iscomplex()) {
1157 assert(0);
1158 //return new DImValue(to, DtoComplex(to, val));
1159 }
1160 else if (totype->isfloating()) {
1161 if ((fromtype->ty == Tfloat80 || fromtype->ty == Tfloat64) && (totype->ty == Tfloat80 || totype->ty == Tfloat64)) {
1162 rval = val->getRVal();
1163 }
1164 else if (fromsz < tosz) {
1165 rval = new llvm::FPExtInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
1166 }
1167 else if (fromsz > tosz) {
1168 rval = new llvm::FPTruncInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
1169 }
1170 else {
1171 assert(0 && "bad float cast");
1172 }
1173 }
1174 else if (totype->isintegral()) {
1175 if (totype->isunsigned()) {
1176 rval = new llvm::FPToUIInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
1177 }
1178 else {
1179 rval = new llvm::FPToSIInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
1180 }
1181 }
1182 else {
1183 assert(0 && "bad float cast");
1184 }
1185
1186 return new DImValue(to, rval);
1187 }
1188
1189 DValue* DtoCastComplex(DValue* val, Type* _to)
1190 {
1191 Type* to = DtoDType(_to);
1192 Type* vty = val->getType();
1193 if (to->iscomplex()) {
1194 if (vty->size() == to->size())
1195 return val;
1196
1197 llvm::Value *re, *im;
1198 DtoGetComplexParts(val, re, im);
1199 const LLType* toty = DtoComplexBaseType(to);
1200
1201 if (to->size() < vty->size()) {
1202 re = gIR->ir->CreateFPTrunc(re, toty, "tmp");
1203 im = gIR->ir->CreateFPTrunc(im, toty, "tmp");
1204 }
1205 else if (to->size() > vty->size()) {
1206 re = gIR->ir->CreateFPExt(re, toty, "tmp");
1207 im = gIR->ir->CreateFPExt(im, toty, "tmp");
1208 }
1209 else {
1210 return val;
1211 }
1212
1213 if (val->isComplex())
1214 return new DComplexValue(_to, re, im);
1215
1216 // unfortunately at this point, the cast value can show up as the lvalue for += and similar expressions.
1217 // so we need to give it storage, or fix the system that handles this stuff (DLRValue)
1218 LLValue* mem = new llvm::AllocaInst(DtoType(_to), "castcomplextmp", gIR->topallocapoint());
1219 DtoComplexSet(mem, re, im);
1220 return new DLRValue(val->getType(), val->getRVal(), _to, mem);
1221 }
1222 else if (to->isimaginary()) {
1223 if (val->isComplex())
1224 return new DImValue(to, val->isComplex()->im);
1225 LLValue* v = val->getRVal();
1226 DImValue* im = new DImValue(to, DtoLoad(DtoGEPi(v,0,1,"tmp")));
1227 return DtoCastFloat(im, to);
1228 }
1229 else if (to->isfloating()) {
1230 if (val->isComplex())
1231 return new DImValue(to, val->isComplex()->re);
1232 LLValue* v = val->getRVal();
1233 DImValue* re = new DImValue(to, DtoLoad(DtoGEPi(v,0,0,"tmp")));
1234 return DtoCastFloat(re, to);
1235 }
1236 else
1237 assert(0);
1238 }
1239
1240 DValue* DtoCast(DValue* val, Type* to)
1241 {
1242 Type* fromtype = DtoDType(val->getType());
1243 Logger::println("Casting from '%s' to '%s'", fromtype->toChars(), to->toChars());
1244 if (fromtype->isintegral()) {
1245 return DtoCastInt(val, to);
1246 }
1247 else if (fromtype->iscomplex()) {
1248 return DtoCastComplex(val, to);
1249 }
1250 else if (fromtype->isfloating()) {
1251 return DtoCastFloat(val, to);
1252 }
1253 else if (fromtype->ty == Tclass) {
1254 return DtoCastClass(val, to);
1255 }
1256 else if (fromtype->ty == Tarray || fromtype->ty == Tsarray) {
1257 return DtoCastArray(val, to);
1258 }
1259 else if (fromtype->ty == Tpointer || fromtype->ty == Tfunction) {
1260 return DtoCastPtr(val, to);
1261 }
1262 else {
1263 assert(0);
1264 }
1265 } 466 }
1266 467
1267 ////////////////////////////////////////////////////////////////////////////////////////// 468 //////////////////////////////////////////////////////////////////////////////////////////
1268 469
1269 llvm::ConstantInt* DtoConstSize_t(size_t i) 470 llvm::ConstantInt* DtoConstSize_t(size_t i)
1270 { 471 {
1271 return llvm::ConstantInt::get(DtoSize_t(), i, false); 472 return llvm::ConstantInt::get(DtoSize_t(), i, false);
1272 } 473 }
1273 llvm::ConstantInt* DtoConstUint(unsigned i) 474 llvm::ConstantInt* DtoConstUint(unsigned i)
1274 { 475 {
1275 return llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false); 476 return llvm::ConstantInt::get(LLType::Int32Ty, i, false);
1276 } 477 }
1277 llvm::ConstantInt* DtoConstInt(int i) 478 llvm::ConstantInt* DtoConstInt(int i)
1278 { 479 {
1279 return llvm::ConstantInt::get(llvm::Type::Int32Ty, i, true); 480 return llvm::ConstantInt::get(LLType::Int32Ty, i, true);
1280 } 481 }
1281 LLConstant* DtoConstBool(bool b) 482 LLConstant* DtoConstBool(bool b)
1282 { 483 {
1283 return llvm::ConstantInt::get(llvm::Type::Int1Ty, b, false); 484 return llvm::ConstantInt::get(LLType::Int1Ty, b, false);
485 }
486 llvm::ConstantInt* DtoConstUbyte(unsigned char i)
487 {
488 return llvm::ConstantInt::get(LLType::Int8Ty, i, false);
1284 } 489 }
1285 490
1286 llvm::ConstantFP* DtoConstFP(Type* t, long double value) 491 llvm::ConstantFP* DtoConstFP(Type* t, long double value)
1287 { 492 {
1288 TY ty = DtoDType(t)->ty; 493 TY ty = DtoDType(t)->ty;
1289 if (ty == Tfloat32 || ty == Timaginary32) 494 if (ty == Tfloat32 || ty == Timaginary32)
1290 return llvm::ConstantFP::get(llvm::APFloat(float(value))); 495 return llvm::ConstantFP::get(llvm::APFloat(float(value)));
1291 else if (ty == Tfloat64 || ty == Timaginary64 || ty == Tfloat80 || ty == Timaginary80) 496 else if (ty == Tfloat64 || ty == Timaginary64 || ty == Tfloat80 || ty == Timaginary80)
1292 return llvm::ConstantFP::get(llvm::APFloat(double(value))); 497 return llvm::ConstantFP::get(llvm::APFloat(double(value)));
1293 } 498 }
1294
1295 499
1296 ////////////////////////////////////////////////////////////////////////////////////////// 500 //////////////////////////////////////////////////////////////////////////////////////////
1297 501
1298 LLConstant* DtoConstString(const char* str) 502 LLConstant* DtoConstString(const char* str)
1299 { 503 {
1318 return llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2); 522 return llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
1319 } 523 }
1320 524
1321 ////////////////////////////////////////////////////////////////////////////////////////// 525 //////////////////////////////////////////////////////////////////////////////////////////
1322 526
1323 void DtoMemSetZero(LLValue* dst, LLValue* nbytes)
1324 {
1325 const LLType* arrty = getPtrToType(llvm::Type::Int8Ty);
1326 llvm::Value *dstarr;
1327 if (dst->getType() == arrty)
1328 {
1329 dstarr = dst;
1330 }
1331 else
1332 {
1333 dstarr = DtoBitCast(dst,arrty);
1334 }
1335
1336 llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemSet64() : LLVM_DeclareMemSet32();
1337 std::vector<LLValue*> llargs;
1338 llargs.resize(4);
1339 llargs[0] = dstarr;
1340 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false);
1341 llargs[2] = nbytes;
1342 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
1343
1344 llvm::CallInst::Create(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
1345 }
1346
1347 //////////////////////////////////////////////////////////////////////////////////////////
1348
1349 void DtoMemCpy(LLValue* dst, LLValue* src, LLValue* nbytes)
1350 {
1351 const LLType* arrty = getVoidPtrType();
1352
1353 LLValue* dstarr;
1354 if (dst->getType() == arrty)
1355 dstarr = dst;
1356 else
1357 dstarr = DtoBitCast(dst, arrty, "tmp");
1358
1359 LLValue* srcarr;
1360 if (src->getType() == arrty)
1361 srcarr = src;
1362 else
1363 srcarr = DtoBitCast(src, arrty, "tmp");
1364
1365 llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32();
1366 std::vector<LLValue*> llargs;
1367 llargs.resize(4);
1368 llargs[0] = dstarr;
1369 llargs[1] = srcarr;
1370 llargs[2] = nbytes;
1371 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
1372
1373 llvm::CallInst::Create(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
1374 }
1375
1376 //////////////////////////////////////////////////////////////////////////////////////////
1377
1378 LLValue* DtoLoad(LLValue* src, const char* name) 527 LLValue* DtoLoad(LLValue* src, const char* name)
1379 { 528 {
1380 LLValue* ld = gIR->ir->CreateLoad(src, name ? name : "tmp"); 529 LLValue* ld = gIR->ir->CreateLoad(src, name ? name : "tmp");
1381 //ld->setVolatile(gIR->func()->inVolatile); 530 //ld->setVolatile(gIR->func()->inVolatile);
1382 return ld; 531 return ld;
1405 return gIR->ir->CreateBitCast(v, t, name ? name : "tmp"); 554 return gIR->ir->CreateBitCast(v, t, name ? name : "tmp");
1406 } 555 }
1407 556
1408 ////////////////////////////////////////////////////////////////////////////////////////// 557 //////////////////////////////////////////////////////////////////////////////////////////
1409 558
1410 const llvm::PointerType* isaPointer(LLValue* v) 559 const LLPointerType* isaPointer(LLValue* v)
1411 { 560 {
1412 return llvm::dyn_cast<llvm::PointerType>(v->getType()); 561 return llvm::dyn_cast<LLPointerType>(v->getType());
1413 } 562 }
1414 563
1415 const llvm::PointerType* isaPointer(const LLType* t) 564 const LLPointerType* isaPointer(const LLType* t)
1416 { 565 {
1417 return llvm::dyn_cast<llvm::PointerType>(t); 566 return llvm::dyn_cast<LLPointerType>(t);
1418 } 567 }
1419 568
1420 const llvm::ArrayType* isaArray(LLValue* v) 569 const LLArrayType* isaArray(LLValue* v)
1421 { 570 {
1422 return llvm::dyn_cast<llvm::ArrayType>(v->getType()); 571 return llvm::dyn_cast<LLArrayType>(v->getType());
1423 } 572 }
1424 573
1425 const llvm::ArrayType* isaArray(const LLType* t) 574 const LLArrayType* isaArray(const LLType* t)
1426 { 575 {
1427 return llvm::dyn_cast<llvm::ArrayType>(t); 576 return llvm::dyn_cast<LLArrayType>(t);
1428 } 577 }
1429 578
1430 const llvm::StructType* isaStruct(LLValue* v) 579 const LLStructType* isaStruct(LLValue* v)
1431 { 580 {
1432 return llvm::dyn_cast<llvm::StructType>(v->getType()); 581 return llvm::dyn_cast<LLStructType>(v->getType());
1433 } 582 }
1434 583
1435 const llvm::StructType* isaStruct(const LLType* t) 584 const LLStructType* isaStruct(const LLType* t)
1436 { 585 {
1437 return llvm::dyn_cast<llvm::StructType>(t); 586 return llvm::dyn_cast<LLStructType>(t);
1438 } 587 }
1439 588
1440 LLConstant* isaConstant(LLValue* v) 589 LLConstant* isaConstant(LLValue* v)
1441 { 590 {
1442 return llvm::dyn_cast<llvm::Constant>(v); 591 return llvm::dyn_cast<llvm::Constant>(v);
1457 return llvm::dyn_cast<llvm::GlobalVariable>(v); 606 return llvm::dyn_cast<llvm::GlobalVariable>(v);
1458 } 607 }
1459 608
1460 ////////////////////////////////////////////////////////////////////////////////////////// 609 //////////////////////////////////////////////////////////////////////////////////////////
1461 610
1462 const llvm::PointerType* getPtrToType(const LLType* t) 611 const LLPointerType* getPtrToType(const LLType* t)
1463 { 612 {
1464 return llvm::PointerType::get(t, 0); 613 return LLPointerType::get(t, 0);
1465 } 614 }
1466 615
1467 const llvm::PointerType* getVoidPtrType() 616 const LLPointerType* getVoidPtrType()
1468 { 617 {
1469 return getPtrToType(llvm::Type::Int8Ty); 618 return getPtrToType(LLType::Int8Ty);
1470 } 619 }
1471 620
1472 llvm::ConstantPointerNull* getNullPtr(const LLType* t) 621 llvm::ConstantPointerNull* getNullPtr(const LLType* t)
1473 { 622 {
1474 const llvm::PointerType* pt = llvm::cast<llvm::PointerType>(t); 623 const LLPointerType* pt = llvm::cast<LLPointerType>(t);
1475 return llvm::ConstantPointerNull::get(pt); 624 return llvm::ConstantPointerNull::get(pt);
1476 } 625 }
1477 626
1478 ////////////////////////////////////////////////////////////////////////////////////////// 627 //////////////////////////////////////////////////////////////////////////////////////////
1479 628
1490 size_t getABITypeSize(const LLType* t) 639 size_t getABITypeSize(const LLType* t)
1491 { 640 {
1492 return gTargetData->getABITypeSize(t); 641 return gTargetData->getABITypeSize(t);
1493 } 642 }
1494 643
1495 ////////////////////////////////////////////////////////////////////////////////////////// 644 unsigned char getABITypeAlign(const LLType* t)
1496 645 {
1497 bool DtoIsTemplateInstance(Dsymbol* s) 646 return gTargetData->getABITypeAlignment(t);
1498 { 647 }
1499 if (!s) return false; 648
1500 if (s->isTemplateInstance() && !s->isTemplateMixin()) 649 unsigned char getPrefTypeAlign(const LLType* t)
1501 return true; 650 {
1502 else if (s->parent) 651 return gTargetData->getPrefTypeAlignment(t);
1503 return DtoIsTemplateInstance(s->parent); 652 }
1504 return false; 653
1505 } 654 //////////////////////////////////////////////////////////////////////////////////////////
1506 655
1507 ////////////////////////////////////////////////////////////////////////////////////////// 656 const LLStructType* DtoInterfaceInfoType()
1508
1509 void DtoLazyStaticInit(bool istempl, LLValue* gvar, Initializer* init, Type* t)
1510 {
1511 // create a flag to make sure initialization only happens once
1512 llvm::GlobalValue::LinkageTypes gflaglink = istempl ? llvm::GlobalValue::WeakLinkage : llvm::GlobalValue::InternalLinkage;
1513 std::string gflagname(gvar->getName());
1514 gflagname.append("__initflag");
1515 llvm::GlobalVariable* gflag = new llvm::GlobalVariable(llvm::Type::Int1Ty,false,gflaglink,DtoConstBool(false),gflagname,gIR->module);
1516
1517 // check flag and do init if not already done
1518 llvm::BasicBlock* oldend = gIR->scopeend();
1519 llvm::BasicBlock* initbb = llvm::BasicBlock::Create("ifnotinit",gIR->topfunc(),oldend);
1520 llvm::BasicBlock* endinitbb = llvm::BasicBlock::Create("ifnotinitend",gIR->topfunc(),oldend);
1521 LLValue* cond = gIR->ir->CreateICmpEQ(gIR->ir->CreateLoad(gflag,"tmp"),DtoConstBool(false));
1522 gIR->ir->CreateCondBr(cond, initbb, endinitbb);
1523 gIR->scope() = IRScope(initbb,endinitbb);
1524 DValue* ie = DtoInitializer(init);
1525 if (!ie->inPlace()) {
1526 DValue* dst = new DVarValue(t, gvar, true);
1527 DtoAssign(dst, ie);
1528 }
1529 gIR->ir->CreateStore(DtoConstBool(true), gflag);
1530 gIR->ir->CreateBr(endinitbb);
1531 gIR->scope() = IRScope(endinitbb,oldend);
1532 }
1533
1534 //////////////////////////////////////////////////////////////////////////////////////////
1535
1536 void DtoResolveDsymbol(Dsymbol* dsym)
1537 {
1538 if (StructDeclaration* sd = dsym->isStructDeclaration()) {
1539 DtoResolveStruct(sd);
1540 }
1541 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) {
1542 DtoResolveClass(cd);
1543 }
1544 else if (FuncDeclaration* fd = dsym->isFuncDeclaration()) {
1545 DtoResolveFunction(fd);
1546 }
1547 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) {
1548 DtoResolveTypeInfo(fd);
1549 }
1550 else {
1551 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars());
1552 assert(0 && "unsupported dsymbol for DtoResolveDsymbol");
1553 }
1554 }
1555
1556 //////////////////////////////////////////////////////////////////////////////////////////
1557
1558 void DtoDeclareDsymbol(Dsymbol* dsym)
1559 {
1560 if (StructDeclaration* sd = dsym->isStructDeclaration()) {
1561 DtoDeclareStruct(sd);
1562 }
1563 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) {
1564 DtoDeclareClass(cd);
1565 }
1566 else if (FuncDeclaration* fd = dsym->isFuncDeclaration()) {
1567 DtoDeclareFunction(fd);
1568 }
1569 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) {
1570 DtoDeclareTypeInfo(fd);
1571 }
1572 else {
1573 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars());
1574 assert(0 && "unsupported dsymbol for DtoDeclareDsymbol");
1575 }
1576 }
1577
1578 //////////////////////////////////////////////////////////////////////////////////////////
1579
1580 void DtoConstInitDsymbol(Dsymbol* dsym)
1581 {
1582 if (StructDeclaration* sd = dsym->isStructDeclaration()) {
1583 DtoConstInitStruct(sd);
1584 }
1585 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) {
1586 DtoConstInitClass(cd);
1587 }
1588 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) {
1589 DtoConstInitTypeInfo(fd);
1590 }
1591 else if (VarDeclaration* vd = dsym->isVarDeclaration()) {
1592 DtoConstInitGlobal(vd);
1593 }
1594 else {
1595 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars());
1596 assert(0 && "unsupported dsymbol for DtoConstInitDsymbol");
1597 }
1598 }
1599
1600 //////////////////////////////////////////////////////////////////////////////////////////
1601
1602 void DtoDefineDsymbol(Dsymbol* dsym)
1603 {
1604 if (StructDeclaration* sd = dsym->isStructDeclaration()) {
1605 DtoDefineStruct(sd);
1606 }
1607 else if (ClassDeclaration* cd = dsym->isClassDeclaration()) {
1608 DtoDefineClass(cd);
1609 }
1610 else if (FuncDeclaration* fd = dsym->isFuncDeclaration()) {
1611 DtoDefineFunc(fd);
1612 }
1613 else if (TypeInfoDeclaration* fd = dsym->isTypeInfoDeclaration()) {
1614 DtoDefineTypeInfo(fd);
1615 }
1616 else {
1617 error(dsym->loc, "unsupported dsymbol: %s", dsym->toChars());
1618 assert(0 && "unsupported dsymbol for DtoDefineDsymbol");
1619 }
1620 }
1621
1622 //////////////////////////////////////////////////////////////////////////////////////////
1623
1624 void DtoConstInitGlobal(VarDeclaration* vd)
1625 {
1626 if (vd->ir.initialized) return;
1627 vd->ir.initialized = gIR->dmodule;
1628
1629 Logger::println("* DtoConstInitGlobal(%s)", vd->toChars());
1630 LOG_SCOPE;
1631
1632 bool emitRTstaticInit = false;
1633
1634 LLConstant* _init = 0;
1635 if (vd->parent && vd->parent->isFuncDeclaration() && vd->init && vd->init->isExpInitializer()) {
1636 _init = DtoConstInitializer(vd->type, NULL);
1637 emitRTstaticInit = true;
1638 }
1639 else {
1640 _init = DtoConstInitializer(vd->type, vd->init);
1641 }
1642
1643 const LLType* _type = DtoType(vd->type);
1644 Type* t = DtoDType(vd->type);
1645
1646 //Logger::cout() << "initializer: " << *_init << '\n';
1647 if (_type != _init->getType()) {
1648 Logger::cout() << "got type '" << *_init->getType() << "' expected '" << *_type << "'\n";
1649 // zero initalizer
1650 if (_init->isNullValue())
1651 _init = llvm::Constant::getNullValue(_type);
1652 // pointer to global constant (struct.init)
1653 else if (llvm::isa<llvm::GlobalVariable>(_init))
1654 {
1655 assert(_init->getType()->getContainedType(0) == _type);
1656 llvm::GlobalVariable* gv = llvm::cast<llvm::GlobalVariable>(_init);
1657 assert(t->ty == Tstruct);
1658 TypeStruct* ts = (TypeStruct*)t;
1659 assert(ts->sym->ir.irStruct->constInit);
1660 _init = ts->sym->ir.irStruct->constInit;
1661 }
1662 // array single value init
1663 else if (isaArray(_type))
1664 {
1665 _init = DtoConstStaticArray(_type, _init);
1666 }
1667 else {
1668 Logger::cout() << "Unexpected initializer type: " << *_type << '\n';
1669 //assert(0);
1670 }
1671 }
1672
1673 bool istempl = false;
1674 if ((vd->storage_class & STCcomdat) || (vd->parent && DtoIsTemplateInstance(vd->parent))) {
1675 istempl = true;
1676 }
1677
1678 if (_init && _init->getType() != _type)
1679 _type = _init->getType();
1680 llvm::cast<llvm::OpaqueType>(vd->ir.irGlobal->type.get())->refineAbstractTypeTo(_type);
1681 _type = vd->ir.irGlobal->type.get();
1682 //_type->dump();
1683 assert(!_type->isAbstract());
1684
1685 llvm::GlobalVariable* gvar = llvm::cast<llvm::GlobalVariable>(vd->ir.irGlobal->value);
1686 if (!(vd->storage_class & STCextern) && (vd->getModule() == gIR->dmodule || istempl))
1687 {
1688 gvar->setInitializer(_init);
1689 }
1690
1691 if (emitRTstaticInit)
1692 DtoLazyStaticInit(istempl, gvar, vd->init, t);
1693 }
1694
1695 //////////////////////////////////////////////////////////////////////////////////////////
1696
1697 void DtoEmptyResolveList()
1698 {
1699 //Logger::println("DtoEmptyResolveList()");
1700 Dsymbol* dsym;
1701 while (!gIR->resolveList.empty()) {
1702 dsym = gIR->resolveList.front();
1703 gIR->resolveList.pop_front();
1704 DtoResolveDsymbol(dsym);
1705 }
1706 }
1707
1708 //////////////////////////////////////////////////////////////////////////////////////////
1709
1710 void DtoEmptyDeclareList()
1711 {
1712 //Logger::println("DtoEmptyDeclareList()");
1713 Dsymbol* dsym;
1714 while (!gIR->declareList.empty()) {
1715 dsym = gIR->declareList.front();
1716 gIR->declareList.pop_front();
1717 DtoDeclareDsymbol(dsym);
1718 }
1719 }
1720
1721 //////////////////////////////////////////////////////////////////////////////////////////
1722
1723 void DtoEmptyConstInitList()
1724 {
1725 //Logger::println("DtoEmptyConstInitList()");
1726 Dsymbol* dsym;
1727 while (!gIR->constInitList.empty()) {
1728 dsym = gIR->constInitList.front();
1729 gIR->constInitList.pop_front();
1730 DtoConstInitDsymbol(dsym);
1731 }
1732 }
1733
1734 //////////////////////////////////////////////////////////////////////////////////////////
1735
1736 void DtoEmptyDefineList()
1737 {
1738 //Logger::println("DtoEmptyDefineList()");
1739 Dsymbol* dsym;
1740 while (!gIR->defineList.empty()) {
1741 dsym = gIR->defineList.front();
1742 gIR->defineList.pop_front();
1743 DtoDefineDsymbol(dsym);
1744 }
1745 }
1746
1747 //////////////////////////////////////////////////////////////////////////////////////////
1748 void DtoEmptyAllLists()
1749 {
1750 for(;;)
1751 {
1752 Dsymbol* dsym;
1753 if (!gIR->resolveList.empty()) {
1754 dsym = gIR->resolveList.front();
1755 gIR->resolveList.pop_front();
1756 DtoResolveDsymbol(dsym);
1757 }
1758 else if (!gIR->declareList.empty()) {
1759 dsym = gIR->declareList.front();
1760 gIR->declareList.pop_front();
1761 DtoDeclareDsymbol(dsym);
1762 }
1763 else if (!gIR->constInitList.empty()) {
1764 dsym = gIR->constInitList.front();
1765 gIR->constInitList.pop_front();
1766 DtoConstInitDsymbol(dsym);
1767 }
1768 else if (!gIR->defineList.empty()) {
1769 dsym = gIR->defineList.front();
1770 gIR->defineList.pop_front();
1771 DtoDefineDsymbol(dsym);
1772 }
1773 else {
1774 break;
1775 }
1776 }
1777 }
1778
1779 //////////////////////////////////////////////////////////////////////////////////////////
1780
1781 void DtoForceDeclareDsymbol(Dsymbol* dsym)
1782 {
1783 if (dsym->ir.declared) return;
1784 Logger::println("DtoForceDeclareDsymbol(%s)", dsym->toPrettyChars());
1785 LOG_SCOPE;
1786 DtoResolveDsymbol(dsym);
1787
1788 DtoEmptyResolveList();
1789
1790 DtoDeclareDsymbol(dsym);
1791 }
1792
1793 //////////////////////////////////////////////////////////////////////////////////////////
1794
1795 void DtoForceConstInitDsymbol(Dsymbol* dsym)
1796 {
1797 if (dsym->ir.initialized) return;
1798 Logger::println("DtoForceConstInitDsymbol(%s)", dsym->toPrettyChars());
1799 LOG_SCOPE;
1800 DtoResolveDsymbol(dsym);
1801
1802 DtoEmptyResolveList();
1803 DtoEmptyDeclareList();
1804
1805 DtoConstInitDsymbol(dsym);
1806 }
1807
1808 //////////////////////////////////////////////////////////////////////////////////////////
1809
1810 void DtoForceDefineDsymbol(Dsymbol* dsym)
1811 {
1812 if (dsym->ir.defined) return;
1813 Logger::println("DtoForceDefineDsymbol(%s)", dsym->toPrettyChars());
1814 LOG_SCOPE;
1815 DtoResolveDsymbol(dsym);
1816
1817 DtoEmptyResolveList();
1818 DtoEmptyDeclareList();
1819 DtoEmptyConstInitList();
1820
1821 DtoDefineDsymbol(dsym);
1822 }
1823
1824 //////////////////////////////////////////////////////////////////////////////////////////
1825
1826 void DtoAnnotation(const char* str)
1827 {
1828 std::string s("CODE: ");
1829 s.append(str);
1830 char* p = &s[0];
1831 while (*p)
1832 {
1833 if (*p == '"')
1834 *p = '\'';
1835 ++p;
1836 }
1837 // create a noop with the code as the result name!
1838 gIR->ir->CreateAnd(DtoConstSize_t(0),DtoConstSize_t(0),s.c_str());
1839 }
1840
1841 //////////////////////////////////////////////////////////////////////////////////////////
1842
1843 const llvm::StructType* DtoInterfaceInfoType()
1844 { 657 {
1845 if (gIR->interfaceInfoType) 658 if (gIR->interfaceInfoType)
1846 return gIR->interfaceInfoType; 659 return gIR->interfaceInfoType;
1847 660
1848 // build interface info type 661 // build interface info type
1852 DtoResolveClass(cd2); 665 DtoResolveClass(cd2);
1853 types.push_back(getPtrToType(cd2->type->ir.type->get())); 666 types.push_back(getPtrToType(cd2->type->ir.type->get()));
1854 // void*[] vtbl 667 // void*[] vtbl
1855 std::vector<const LLType*> vtbltypes; 668 std::vector<const LLType*> vtbltypes;
1856 vtbltypes.push_back(DtoSize_t()); 669 vtbltypes.push_back(DtoSize_t());
1857 const LLType* byteptrptrty = getPtrToType(getPtrToType(llvm::Type::Int8Ty)); 670 const LLType* byteptrptrty = getPtrToType(getPtrToType(LLType::Int8Ty));
1858 vtbltypes.push_back(byteptrptrty); 671 vtbltypes.push_back(byteptrptrty);
1859 types.push_back(llvm::StructType::get(vtbltypes)); 672 types.push_back(LLStructType::get(vtbltypes));
1860 // int offset 673 // int offset
1861 types.push_back(llvm::Type::Int32Ty); 674 types.push_back(LLType::Int32Ty);
1862 // create type 675 // create type
1863 gIR->interfaceInfoType = llvm::StructType::get(types); 676 gIR->interfaceInfoType = LLStructType::get(types);
1864 677
1865 return gIR->interfaceInfoType; 678 return gIR->interfaceInfoType;
1866 } 679 }
1867 680
1868 ////////////////////////////////////////////////////////////////////////////////////////// 681
1869 682
1870 LLConstant* DtoTypeInfoOf(Type* type, bool base) 683
1871 { 684
1872 const LLType* typeinfotype = DtoType(Type::typeinfo->type); 685
1873 if (!type->vtinfo) 686
1874 type->getTypeInfo(NULL); 687
1875 TypeInfoDeclaration* tidecl = type->vtinfo; 688
1876 DtoForceDeclareDsymbol(tidecl); 689
1877 assert(tidecl->ir.irGlobal != NULL);
1878 LLConstant* c = isaConstant(tidecl->ir.irGlobal->value);
1879 assert(c != NULL);
1880 if (base)
1881 return llvm::ConstantExpr::getBitCast(c, typeinfotype);
1882 return c;
1883 }
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895