Wiki Roadmap Timeline Tickets New Ticket Source Search Help / Guide About Trac Login

root/gen/tollvm.cpp

Revision 1650:40bd4a0d4870, 27.2 kB (checked in by Tomas Lindquist Olsen, 2 years ago)

Update to work with LLVM 2.7.

Removed use of dyn_cast, llvm no compiles
without exceptions and rtti by
default. We do need exceptions for the libconfig stuff, but rtti isn't
necessary (anymore).

Debug info needs to be rewritten, as in LLVM 2.7 the format has
completely changed. To have something to look at while rewriting, the
old code has been wrapped inside #ifndef DISABLE_DEBUG_INFO , this means
that you have to define this to compile at the moment.

Updated tango 0.99.9 patch to include updated EH runtime code, which is
needed for LLVM 2.7 as well.

Line 
1 #include "gen/llvm.h"
2
3 #include "dsymbol.h"
4 #include "aggregate.h"
5 #include "declaration.h"
6 #include "init.h"
7 #include "module.h"
8
9 #include "gen/tollvm.h"
10 #include "gen/irstate.h"
11 #include "gen/logger.h"
12 #include "gen/runtime.h"
13 #include "gen/arrays.h"
14 #include "gen/dvalue.h"
15 #include "gen/functions.h"
16 #include "gen/structs.h"
17 #include "gen/classes.h"
18 #include "gen/typeinf.h"
19 #include "gen/complex.h"
20 #include "gen/llvmhelpers.h"
21 #include "gen/linkage.h"
22
23 #include "ir/irtype.h"
24 #include "ir/irtypeclass.h"
25 #include "ir/irtypefunction.h"
26
27 bool DtoIsPassedByRef(Type* type)
28 {
29     Type* typ = type->toBasetype();
30     TY t = typ->ty;
31     return (t == Tstruct || t == Tsarray);
32 }
33
34 unsigned DtoShouldExtend(Type* type)
35 {
36     type = type->toBasetype();
37     if (type->isintegral())
38     {
39         switch(type->ty)
40         {
41         case Tint8:
42         case Tint16:
43             return llvm::Attribute::SExt;
44
45         case Tuns8:
46         case Tuns16:
47             return llvm::Attribute::ZExt;
48         }
49     }
50     return llvm::Attribute::None;
51 }
52
53 const LLType* DtoType(Type* t)
54 {
55     t = stripModifiers( t );
56
57     if (t->irtype)
58     {
59         return t->irtype->get();
60     }
61
62     IF_LOG Logger::println("Building type: %s", t->toChars());
63
64     assert(t);
65     switch (t->ty)
66     {
67     // basic types
68     case Tvoid:
69     case Tint8:
70     case Tuns8:
71     case Tint16:
72     case Tuns16:
73     case Tint32:
74     case Tuns32:
75     case Tint64:
76     case Tuns64:
77     case Tfloat32:
78     case Tfloat64:
79     case Tfloat80:
80     case Timaginary32:
81     case Timaginary64:
82     case Timaginary80:
83     case Tcomplex32:
84     case Tcomplex64:
85     case Tcomplex80:
86     //case Tbit:
87     case Tbool:
88     case Tchar:
89     case Twchar:
90     case Tdchar:
91     {
92         t->irtype = new IrTypeBasic(t);
93         return t->irtype->buildType();
94     }
95
96     // pointers
97     case Tpointer:
98     {
99         t->irtype = new IrTypePointer(t);
100         return t->irtype->buildType();
101     }
102
103     // arrays
104     case Tarray:
105     {
106         t->irtype = new IrTypeArray(t);
107         return t->irtype->buildType();
108     }
109
110     case Tsarray:
111     {
112         t->irtype = new IrTypeSArray(t);
113         return t->irtype->buildType();
114     }
115
116     // aggregates
117     case Tstruct:    {
118         TypeStruct* ts = (TypeStruct*)t;
119         t->irtype = new IrTypeStruct(ts->sym);
120         return t->irtype->buildType();
121     }
122     case Tclass:    {
123         TypeClass* tc = (TypeClass*)t;
124         t->irtype = new IrTypeClass(tc->sym);
125         return t->irtype->buildType();
126     }
127
128     // functions
129     case Tfunction:
130     {
131         t->irtype = new IrTypeFunction(t);
132         return t->irtype->buildType();
133     }
134
135     // delegates
136     case Tdelegate:
137     {
138         t->irtype = new IrTypeDelegate(t);
139         return t->irtype->buildType();
140     }
141
142     // typedefs
143     // enum
144
145     // FIXME: maybe just call toBasetype first ?
146     case Ttypedef:
147     case Tenum:
148     {
149         Type* bt = t->toBasetype();
150         assert(bt);
151         return DtoType(bt);
152     }
153
154     // associative arrays
155     case Taarray:
156         return getVoidPtrType();
157
158 /*
159     Not needed atm as VarDecls for tuples are rewritten as a string of
160     VarDecls for the fields (u -> _u_field_0, ...)
161
162     case Ttuple:
163     {
164         TypeTuple* ttupl = (TypeTuple*)t;
165         return DtoStructTypeFromArguments(ttupl->arguments);
166     }
167 */
168
169     default:
170         printf("trying to convert unknown type '%s' with value %d\n", t->toChars(), t->ty);
171         assert(0);
172     }
173     return 0;
174 }
175
176 //////////////////////////////////////////////////////////////////////////////////////////
177
178 /*
179 const LLType* DtoStructTypeFromArguments(Arguments* arguments)
180 {
181     if (!arguments)
182         return LLType::getVoidTy(gIR->context());
183
184     std::vector<const LLType*> types;
185     for (size_t i = 0; i < arguments->dim; i++)
186     {
187         Argument *arg = (Argument *)arguments->data[i];
188         assert(arg && arg->type);
189
190         types.push_back(DtoType(arg->type));
191     }
192     return LLStructType::get(types);
193 }
194 */
195
196 //////////////////////////////////////////////////////////////////////////////////////////
197
198 const LLType* DtoTypeNotVoid(Type* t)
199 {
200     const LLType* lt = DtoType(t);
201     if (lt == LLType::getVoidTy(gIR->context()))
202         return LLType::getInt8Ty(gIR->context());
203     return lt;
204 }
205
206 //////////////////////////////////////////////////////////////////////////////////////////
207
208 LLValue* DtoDelegateEquals(TOK op, LLValue* lhs, LLValue* rhs)
209 {
210     Logger::println("Doing delegate equality");
211     llvm::Value *b1, *b2;
212     if (rhs == NULL)
213     {
214         rhs = LLConstant::getNullValue(lhs->getType());
215     }
216
217     LLValue* l = gIR->ir->CreateExtractValue(lhs, 0);
218     LLValue* r = gIR->ir->CreateExtractValue(rhs, 0);
219     b1 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,l,r,"tmp");
220
221     l = gIR->ir->CreateExtractValue(lhs, 1);
222     r = gIR->ir->CreateExtractValue(rhs, 1);
223     b2 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,l,r,"tmp");
224
225     LLValue* b = gIR->ir->CreateAnd(b1,b2,"tmp");
226
227     if (op == TOKnotequal || op == TOKnotidentity)
228         return gIR->ir->CreateNot(b,"tmp");
229
230     return b;
231 }
232
233 //////////////////////////////////////////////////////////////////////////////////////////
234
235 LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
236 {
237     // global variable
238     if (VarDeclaration* vd = sym->isVarDeclaration())
239     {
240         if (mustDefineSymbol(vd))
241             Logger::println("Variable %savailable externally: %s", (vd->availableExternally ? "" : "not "), vd->toChars());
242         // generated by inlining semantics run
243         if (vd->availableExternally && mustDefineSymbol(sym))
244             return llvm::GlobalValue::AvailableExternallyLinkage;
245         // template
246         if (needsTemplateLinkage(sym))
247             return templateLinkage;
248     }
249     // function
250     else if (FuncDeclaration* fdecl = sym->isFuncDeclaration())
251     {
252         if (mustDefineSymbol(fdecl))
253             Logger::println("Function %savailable externally: %s", (fdecl->availableExternally ? "" : "not "), fdecl->toChars());
254         assert(fdecl->type->ty == Tfunction);
255         TypeFunction* ft = (TypeFunction*)fdecl->type;
256
257         // intrinsics are always external
258         if (fdecl->llvmInternal == LLVMintrinsic)
259             return llvm::GlobalValue::ExternalLinkage;
260         // generated by inlining semantics run
261         if (fdecl->availableExternally && mustDefineSymbol(sym))
262             return llvm::GlobalValue::AvailableExternallyLinkage;
263         // array operations are always template linkage
264         if (fdecl->isArrayOp)
265             return templateLinkage;
266         // template instances should have weak linkage
267         // but only if there's a body, and it's not naked
268         // otherwise we make it external
269         else if (needsTemplateLinkage(fdecl) && fdecl->fbody && !fdecl->naked)
270             return templateLinkage;
271         // extern(C) functions are always external
272         else if (ft->linkage == LINKc)
273             return llvm::GlobalValue::ExternalLinkage;
274     }
275     // class
276     else if (ClassDeclaration* cd = sym->isClassDeclaration())
277     {
278         if (mustDefineSymbol(cd))
279             Logger::println("Class %savailable externally: %s", (cd->availableExternally ? "" : "not "), vd->toChars());
280         // generated by inlining semantics run
281         if (cd->availableExternally && mustDefineSymbol(sym))
282             return llvm::GlobalValue::AvailableExternallyLinkage;
283         // template
284         if (needsTemplateLinkage(cd))
285             return templateLinkage;
286     }
287     else
288     {
289         assert(0 && "not global/function");
290     }
291
292     // The following breaks for nested naked functions and other declarations, so check for that.
293     bool skipNestedCheck = !mustDefineSymbol(sym);
294     if (FuncDeclaration* fd = sym->isFuncDeclaration())
295         skipNestedCheck = (fd->naked != 0);
296
297     // Any symbol nested in a function can't be referenced directly from
298     // outside that function, so we can give such symbols internal linkage.
299     // This holds even if nested indirectly, such as member functions of
300     // aggregates nested in functions.
301     //
302     // Note: This must be checked after things like template member-ness or
303     // symbols nested in templates would get duplicated for each module,
304     // breaking things like
305     // ---
306     // int counter(T)() { static int i; return i++; }"
307     // ---
308     // if instances get emitted in multiple object files because they'd use
309     // different instances of 'i'.
310     if (!skipNestedCheck)
311         for (Dsymbol* parent = sym->parent; parent ; parent = parent->parent) {
312             if (parent->isFuncDeclaration())
313                 return llvm::GlobalValue::InternalLinkage;
314         }
315
316     // default to external linkage
317     return llvm::GlobalValue::ExternalLinkage;
318 }
319
320 static bool isAvailableExternally(Dsymbol* sym)
321 {
322     if (VarDeclaration* vd = sym->isVarDeclaration())
323         return vd->availableExternally;
324     if (FuncDeclaration* fd = sym->isFuncDeclaration())
325         return fd->availableExternally;
326     if (AggregateDeclaration* ad = sym->isAggregateDeclaration())
327         return ad->availableExternally;
328     return false;
329 }
330
331 llvm::GlobalValue::LinkageTypes DtoInternalLinkage(Dsymbol* sym)
332 {
333     if (needsTemplateLinkage(sym)) {
334         if (isAvailableExternally(sym) && mustDefineSymbol(sym))
335             return llvm::GlobalValue::AvailableExternallyLinkage;
336         return templateLinkage;
337     }
338     else
339         return llvm::GlobalValue::InternalLinkage;
340 }
341
342 llvm::GlobalValue::LinkageTypes DtoExternalLinkage(Dsymbol* sym)
343 {
344     if (isAvailableExternally(sym) && mustDefineSymbol(sym))
345         return llvm::GlobalValue::AvailableExternallyLinkage;
346     if (needsTemplateLinkage(sym))
347         return templateLinkage;
348     else
349         return llvm::GlobalValue::ExternalLinkage;
350 }
351
352 //////////////////////////////////////////////////////////////////////////////////////////
353
354 LLValue* DtoPointedType(LLValue* ptr, LLValue* val)
355 {
356     const LLType* ptrTy = ptr->getType()->getContainedType(0);
357     const LLType* valTy = val->getType();
358     // ptr points to val's type
359     if (ptrTy == valTy)
360     {
361         return val;
362     }
363     // ptr is integer pointer
364     else if (ptrTy->isIntegerTy())
365     {
366         // val is integer
367         assert(valTy->isInteger());
368         const LLIntegerType* pt = llvm::cast<const LLIntegerType>(ptrTy);
369         const LLIntegerType* vt = llvm::cast<const LLIntegerType>(valTy);
370         if (pt->getBitWidth() < vt->getBitWidth()) {
371             return new llvm::TruncInst(val, pt, "tmp", gIR->scopebb());
372         }
373         else
374         assert(0);
375     }
376     // something else unsupported
377     else
378     {
379         if (Logger::enabled())
380             Logger::cout() << *ptrTy << '|' << *valTy << '\n';
381         assert(0);
382     }
383     return 0;
384 }
385
386 //////////////////////////////////////////////////////////////////////////////////////////
387
388 const LLIntegerType* DtoSize_t()
389 {
390     // the type of size_t does not change once set
391     static const LLIntegerType* t = NULL;
392     if (t == NULL)
393         t = (global.params.is64bit) ? LLType::getInt64Ty(gIR->context()) : LLType::getInt32Ty(gIR->context());
394     return t;
395 }
396
397 //////////////////////////////////////////////////////////////////////////////////////////
398
399 LLValue* DtoGEP1(LLValue* ptr, LLValue* i0, const char* var, llvm::BasicBlock* bb)
400 {
401     return llvm::GetElementPtrInst::Create(ptr, i0, var?var:"tmp", bb?bb:gIR->scopebb());
402 }
403
404 //////////////////////////////////////////////////////////////////////////////////////////
405
406 LLValue* DtoGEP(LLValue* ptr, LLValue* i0, LLValue* i1, const char* var, llvm::BasicBlock* bb)
407 {
408     LLSmallVector<LLValue*,2> v(2);
409     v[0] = i0;
410     v[1] = i1;
411     return llvm::GetElementPtrInst::Create(ptr, v.begin(), v.end(), var?var:"tmp", bb?bb:gIR->scopebb());
412 }
413
414 //////////////////////////////////////////////////////////////////////////////////////////
415
416 LLValue* DtoGEPi1(LLValue* ptr, unsigned i, const char* var, llvm::BasicBlock* bb)
417 {
418     return llvm::GetElementPtrInst::Create(ptr, DtoConstUint(i), var?var:"tmp", bb?bb:gIR->scopebb());
419 }
420
421 //////////////////////////////////////////////////////////////////////////////////////////
422
423 LLValue* DtoGEPi(LLValue* ptr, unsigned i0, unsigned i1, const char* var, llvm::BasicBlock* bb)
424 {
425     LLSmallVector<LLValue*,2> v(2);
426     v[0] = DtoConstUint(i0);
427     v[1] = DtoConstUint(i1);
428     return llvm::GetElementPtrInst::Create(ptr, v.begin(), v.end(), var?var:"tmp", bb?bb:gIR->scopebb());
429 }
430
431 //////////////////////////////////////////////////////////////////////////////////////////
432
433 LLConstant* DtoGEPi(LLConstant* ptr, unsigned i0, unsigned i1)
434 {
435     LLValue* v[2] = { DtoConstUint(i0), DtoConstUint(i1) };
436     return llvm::ConstantExpr::getGetElementPtr(ptr, v, 2);
437 }
438
439 //////////////////////////////////////////////////////////////////////////////////////////
440
441 void DtoMemSet(LLValue* dst, LLValue* val, LLValue* nbytes)
442 {
443     dst = DtoBitCast(dst,getVoidPtrType());
444
445     const LLType* intTy = DtoSize_t();
446     llvm::Function* fn = llvm::Intrinsic::getDeclaration(gIR->module,
447         llvm::Intrinsic::memset, &intTy, 1);
448
449     gIR->ir->CreateCall4(fn, dst, val, nbytes, DtoConstUint(0), "");
450 }
451
452 //////////////////////////////////////////////////////////////////////////////////////////
453
454 void DtoMemSetZero(LLValue* dst, LLValue* nbytes)
455 {
456     DtoMemSet(dst, DtoConstUbyte(0), nbytes);
457 }
458
459 //////////////////////////////////////////////////////////////////////////////////////////
460
461 void DtoMemCpy(LLValue* dst, LLValue* src, LLValue* nbytes, unsigned align)
462 {
463     dst = DtoBitCast(dst,getVoidPtrType());
464     src = DtoBitCast(src,getVoidPtrType());
465
466     const LLType* intTy = DtoSize_t();
467     llvm::Function* fn = llvm::Intrinsic::getDeclaration(gIR->module,
468         llvm::Intrinsic::memcpy, &intTy, 1);
469
470     gIR->ir->CreateCall4(fn, dst, src, nbytes, DtoConstUint(align), "");
471 }
472
473 //////////////////////////////////////////////////////////////////////////////////////////
474
475 LLValue* DtoMemCmp(LLValue* lhs, LLValue* rhs, LLValue* nbytes)
476 {
477     // int memcmp ( const void * ptr1, const void * ptr2, size_t num );
478
479     LLFunction* fn = gIR->module->getFunction("memcmp");
480     if (!fn)
481     {
482         std::vector<const LLType*> params(3);
483         params[0] = getVoidPtrType();
484         params[1] = getVoidPtrType();
485         params[2] = DtoSize_t();
486         const LLFunctionType* fty = LLFunctionType::get(LLType::getInt32Ty(gIR->context()), params, false);
487         fn = LLFunction::Create(fty, LLGlobalValue::ExternalLinkage, "memcmp", gIR->module);
488     }
489
490     lhs = DtoBitCast(lhs,getVoidPtrType());
491     rhs = DtoBitCast(rhs,getVoidPtrType());
492
493     return gIR->ir->CreateCall3(fn, lhs, rhs, nbytes, "tmp");
494 }
495
496 //////////////////////////////////////////////////////////////////////////////////////////
497
498 void DtoAggrZeroInit(LLValue* v)
499 {
500     uint64_t n = getTypeStoreSize(v->getType()->getContainedType(0));
501     DtoMemSetZero(v, DtoConstSize_t(n));
502 }
503
504 //////////////////////////////////////////////////////////////////////////////////////////
505
506 void DtoAggrCopy(LLValue* dst, LLValue* src)
507 {
508     uint64_t n = getTypeStoreSize(dst->getType()->getContainedType(0));
509     DtoMemCpy(dst, src, DtoConstSize_t(n));
510 }
511
512 //////////////////////////////////////////////////////////////////////////////////////////
513
514 void DtoMemoryBarrier(bool ll, bool ls, bool sl, bool ss, bool device)
515 {
516     llvm::Function* fn = GET_INTRINSIC_DECL(memory_barrier);
517     assert(fn != NULL);
518
519     LLSmallVector<LLValue*, 5> llargs;
520     llargs.push_back(DtoConstBool(ll));
521     llargs.push_back(DtoConstBool(ls));
522     llargs.push_back(DtoConstBool(sl));
523     llargs.push_back(DtoConstBool(ss));
524     llargs.push_back(DtoConstBool(device));
525
526     llvm::CallInst::Create(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
527 }
528
529 //////////////////////////////////////////////////////////////////////////////////////////
530
531 llvm::ConstantInt* DtoConstSize_t(uint64_t i)
532 {
533     return LLConstantInt::get(DtoSize_t(), i, false);
534 }
535 llvm::ConstantInt* DtoConstUint(unsigned i)
536 {
537     return LLConstantInt::get(LLType::getInt32Ty(gIR->context()), i, false);
538 }
539 llvm::ConstantInt* DtoConstInt(int i)
540 {
541     return LLConstantInt::get(LLType::getInt32Ty(gIR->context()), i, true);
542 }
543 LLConstant* DtoConstBool(bool b)
544 {
545     return LLConstantInt::get(LLType::getInt1Ty(gIR->context()), b, false);
546 }
547 llvm::ConstantInt* DtoConstUbyte(unsigned char i)
548 {
549     return LLConstantInt::get(LLType::getInt8Ty(gIR->context()), i, false);
550 }
551
552 LLConstant* DtoConstFP(Type* t, long double value)
553 {
554     const LLType* llty = DtoType(t);
555     assert(llty->isFloatingPoint());
556
557     if(llty == LLType::getFloatTy(gIR->context()) || llty == LLType::getDoubleTy(gIR->context()))
558         return LLConstantFP::get(llty, value);
559     else if(llty == LLType::getX86_FP80Ty(gIR->context())) {
560         uint64_t bits[] = {0, 0};
561         bits[0] = *(uint64_t*)&value;
562         bits[1] = *(uint16_t*)((uint64_t*)&value + 1);
563         return LLConstantFP::get(gIR->context(), APFloat(APInt(80, 2, bits)));
564     } else {
565         assert(0 && "Unknown floating point type encountered");
566     }
567 }
568
569 //////////////////////////////////////////////////////////////////////////////////////////
570
571 LLConstant* DtoConstString(const char* str)
572 {
573     llvm::StringRef s(str?str:"");
574     LLConstant* init = LLConstantArray::get(gIR->context(), s, true);
575     llvm::GlobalVariable* gvar = new llvm::GlobalVariable(
576         *gIR->module, init->getType(), true,llvm::GlobalValue::InternalLinkage, init, ".str");
577     LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
578     return DtoConstSlice(
579         DtoConstSize_t(s.size()),
580         llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2)
581     );
582 }
583 LLConstant* DtoConstStringPtr(const char* str, const char* section)
584 {
585     llvm::StringRef s(str);
586     LLConstant* init = LLConstantArray::get(gIR->context(), s, true);
587     llvm::GlobalVariable* gvar = new llvm::GlobalVariable(
588         *gIR->module, init->getType(), true,llvm::GlobalValue::InternalLinkage, init, ".str");
589     if (section) gvar->setSection(section);
590     LLConstant* idxs[2] = { DtoConstUint(0), DtoConstUint(0) };
591     return llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
592 }
593
594 //////////////////////////////////////////////////////////////////////////////////////////
595
596 LLValue* DtoLoad(LLValue* src, const char* name)
597 {
598 //     if (Logger::enabled())
599 //         Logger::cout() << "loading " << *src <<  '\n';
600     llvm::LoadInst* ld = gIR->ir->CreateLoad(src, name ? name : "tmp");
601     //ld->setVolatile(gIR->func()->inVolatile);
602     return ld;
603 }
604
605 // Like DtoLoad, but the pointer is guaranteed to be aligned appropriately for the type.
606 LLValue* DtoAlignedLoad(LLValue* src, const char* name)
607 {
608     llvm::LoadInst* ld = gIR->ir->CreateLoad(src, name ? name : "tmp");
609     ld->setAlignment(getABITypeAlign(ld->getType()));
610     return ld;
611 }
612
613
614 void DtoStore(LLValue* src, LLValue* dst)
615 {
616 //     if (Logger::enabled())
617 //         Logger::cout() << "storing " << *src << " into " << *dst << '\n';
618     LLValue* st = gIR->ir->CreateStore(src,dst);
619     //st->setVolatile(gIR->func()->inVolatile);
620 }
621
622 // Like DtoStore, but the pointer is guaranteed to be aligned appropriately for the type.
623 void DtoAlignedStore(LLValue* src, LLValue* dst)
624 {
625     llvm::StoreInst* st = gIR->ir->CreateStore(src,dst);
626     st->setAlignment(getABITypeAlign(src->getType()));
627 }
628
629 //////////////////////////////////////////////////////////////////////////////////////////
630
631 LLValue* DtoBitCast(LLValue* v, const LLType* t, const char* name)
632 {
633     if (v->getType() == t)
634         return v;
635     assert(!isaStruct(t));
636     return gIR->ir->CreateBitCast(v, t, name ? name : "tmp");
637 }
638
639 LLConstant* DtoBitCast(LLConstant* v, const LLType* t)
640 {
641     if (v->getType() == t)
642         return v;
643     return llvm::ConstantExpr::getBitCast(v, t);
644 }
645
646 //////////////////////////////////////////////////////////////////////////////////////////
647
648 LLValue* DtoInsertValue(LLValue* aggr, LLValue* v, unsigned idx)
649 {
650     return gIR->ir->CreateInsertValue(aggr, v, idx);
651 }
652
653 LLValue* DtoExtractValue(LLValue* aggr, unsigned idx)
654 {
655     return gIR->ir->CreateExtractValue(aggr, idx);
656 }
657
658 //////////////////////////////////////////////////////////////////////////////////////////
659
660 const LLPointerType* isaPointer(LLValue* v)
661 {
662     return llvm::dyn_cast<LLPointerType>(v->getType());
663 }
664
665 const LLPointerType* isaPointer(const LLType* t)
666 {
667     return llvm::dyn_cast<LLPointerType>(t);
668 }
669
670 const LLArrayType* isaArray(LLValue* v)
671 {
672     return llvm::dyn_cast<LLArrayType>(v->getType());
673 }
674
675 const LLArrayType* isaArray(const LLType* t)
676 {
677     return llvm::dyn_cast<LLArrayType>(t);
678 }
679
680 const LLStructType* isaStruct(LLValue* v)
681 {
682     return llvm::dyn_cast<LLStructType>(v->getType());
683 }
684
685 const LLStructType* isaStruct(const LLType* t)
686 {
687     return llvm::dyn_cast<LLStructType>(t);
688 }
689
690 const LLFunctionType* isaFunction(LLValue* v)
691 {
692     return llvm::dyn_cast<LLFunctionType>(v->getType());
693 }
694
695 const LLFunctionType* isaFunction(const LLType* t)
696 {
697     return llvm::dyn_cast<LLFunctionType>(t);
698 }
699
700 LLConstant* isaConstant(LLValue* v)
701 {
702     return llvm::dyn_cast<llvm::Constant>(v);
703 }
704
705 llvm::ConstantInt* isaConstantInt(LLValue* v)
706 {
707     return llvm::dyn_cast<llvm::ConstantInt>(v);
708 }
709
710 llvm::Argument* isaArgument(LLValue* v)
711 {
712     return llvm::dyn_cast<llvm::Argument>(v);
713 }
714
715 llvm::GlobalVariable* isaGlobalVar(LLValue* v)
716 {
717     return llvm::dyn_cast<llvm::GlobalVariable>(v);
718 }
719
720 //////////////////////////////////////////////////////////////////////////////////////////
721
722 const LLPointerType* getPtrToType(const LLType* t)
723 {
724     if (t == LLType::getVoidTy(gIR->context()))
725         t = LLType::getInt8Ty(gIR->context());
726     return LLPointerType::get(t, 0);
727 }
728
729 const LLPointerType* getVoidPtrType()
730 {
731     return getPtrToType(LLType::getInt8Ty(gIR->context()));
732 }
733
734 llvm::ConstantPointerNull* getNullPtr(const LLType* t)
735 {
736     const LLPointerType* pt = llvm::cast<LLPointerType>(t);
737     return llvm::ConstantPointerNull::get(pt);
738 }
739
740 LLConstant* getNullValue(const LLType* t)
741 {
742     return LLConstant::getNullValue(t);
743 }
744
745 //////////////////////////////////////////////////////////////////////////////////////////
746
747 size_t getTypeBitSize(const LLType* t)
748 {
749     return gTargetData->getTypeSizeInBits(t);
750 }
751
752 size_t getTypeStoreSize(const LLType* t)
753 {
754     return gTargetData->getTypeStoreSize(t);
755 }
756
757 size_t getTypePaddedSize(const LLType* t)
758 {
759     size_t sz = gTargetData->getTypeAllocSize(t);
760     //Logger::cout() << "abi type size of: " << *t << " == " << sz << '\n';
761     return sz;
762 }
763
764 unsigned char getABITypeAlign(const LLType* t)
765 {
766     return gTargetData->getABITypeAlignment(t);
767 }
768
769 unsigned char getPrefTypeAlign(const LLType* t)
770 {
771     return gTargetData->getPrefTypeAlignment(t);
772 }
773
774 const LLType* getBiggestType(const LLType** begin, size_t n)
775 {
776     const LLType* bigTy = 0;
777     size_t bigSize = 0;
778     size_t bigAlign = 0;
779
780     const LLType** end = begin+n;
781     while (begin != end)
782     {
783         const LLType* T = *begin;
784
785         size_t sz = getTypePaddedSize(T);
786         size_t ali = getABITypeAlign(T);
787         if (sz > bigSize || (sz == bigSize && ali > bigAlign))
788         {
789             bigTy = T;
790             bigSize = sz;
791             bigAlign = ali;
792         }
793
794         ++begin;
795     }
796
797     // will be null for n==0
798     return bigTy;
799 }
800
801 //////////////////////////////////////////////////////////////////////////////////////////
802
803 const LLStructType* DtoInterfaceInfoType()
804 {
805     if (gIR->interfaceInfoType)
806         return gIR->interfaceInfoType;
807
808     // build interface info type
809     std::vector<const LLType*> types;
810     // ClassInfo classinfo
811     ClassDeclaration* cd2 = ClassDeclaration::classinfo;
812     DtoResolveClass(cd2);
813     types.push_back(DtoType(cd2->type));
814     // void*[] vtbl
815     std::vector<const LLType*> vtbltypes;
816     vtbltypes.push_back(DtoSize_t());
817     const LLType* byteptrptrty = getPtrToType(getPtrToType(LLType::getInt8Ty(gIR->context())));
818     vtbltypes.push_back(byteptrptrty);
819     types.push_back(LLStructType::get(gIR->context(), vtbltypes));
820     // int offset
821     types.push_back(LLType::getInt32Ty(gIR->context()));
822     // create type
823     gIR->interfaceInfoType = LLStructType::get(gIR->context(), types);
824
825     return gIR->interfaceInfoType;
826 }
827
828 //////////////////////////////////////////////////////////////////////////////////////////
829
830 const LLStructType* DtoMutexType()
831 {
832     if (gIR->mutexType)
833         return gIR->mutexType;
834
835     // win32
836     if (global.params.os == OSWindows)
837     {
838         // CRITICAL_SECTION.sizeof == 68
839         std::vector<const LLType*> types(17, LLType::getInt32Ty(gIR->context()));
840         return LLStructType::get(gIR->context(), types);
841     }
842
843     // FreeBSD
844     else if (global.params.os == OSFreeBSD) {
845         // Just a pointer
846         return LLStructType::get(gIR->context(), DtoSize_t(), NULL);
847     }
848
849     // pthread_fastlock
850     std::vector<const LLType*> types2;
851     types2.push_back(DtoSize_t());
852     types2.push_back(LLType::getInt32Ty(gIR->context()));
853     const LLStructType* fastlock = LLStructType::get(gIR->context(), types2);
854
855     // pthread_mutex
856     std::vector<const LLType*> types1;
857     types1.push_back(LLType::getInt32Ty(gIR->context()));
858     types1.push_back(LLType::getInt32Ty(gIR->context()));
859     types1.push_back(getVoidPtrType());
860     types1.push_back(LLType::getInt32Ty(gIR->context()));
861     types1.push_back(fastlock);
862     const LLStructType* pmutex = LLStructType::get(gIR->context(), types1);
863
864     // D_CRITICAL_SECTION
865     LLOpaqueType* opaque = LLOpaqueType::get(gIR->context());
866     std::vector<const LLType*> types;
867     types.push_back(getPtrToType(opaque));
868     types.push_back(pmutex);
869
870     // resolve type
871     pmutex = LLStructType::get(gIR->context(), types);
872     LLPATypeHolder pa(pmutex);
873     opaque->refineAbstractTypeTo(pa.get());
874     pmutex = isaStruct(pa.get());
875
876     gIR->mutexType = pmutex;
877     gIR->module->addTypeName("D_CRITICAL_SECTION", pmutex);
878     return pmutex;
879 }
880
881 //////////////////////////////////////////////////////////////////////////////////////////
882
883 const LLStructType* DtoModuleReferenceType()
884 {
885     if (gIR->moduleRefType)
886         return gIR->moduleRefType;
887
888     // this is a recursive type so start out with the opaque
889     LLOpaqueType* opaque = LLOpaqueType::get(gIR->context());
890
891     // add members
892     std::vector<const LLType*> types;
893     types.push_back(getPtrToType(opaque));
894     types.push_back(DtoType(Module::moduleinfo->type));
895
896     // resolve type
897     const LLStructType* st = LLStructType::get(gIR->context(), types);
898     LLPATypeHolder pa(st);
899     opaque->refineAbstractTypeTo(pa.get());
900     st = isaStruct(pa.get());
901
902     // done
903     gIR->moduleRefType = st;
904     gIR->module->addTypeName("ModuleReference", st);
905     return st;
906 }
907
908 //////////////////////////////////////////////////////////////////////////////////////////
909
910 LLValue* DtoAggrPair(const LLType* type, LLValue* V1, LLValue* V2, const char* name)
911 {
912     LLValue* res = llvm::UndefValue::get(type);
913     res = gIR->ir->CreateInsertValue(res, V1, 0, "tmp");
914     return gIR->ir->CreateInsertValue(res, V2, 1, name?name:"tmp");
915 }
916
917 LLValue* DtoAggrPair(LLValue* V1, LLValue* V2, const char* name)
918 {
919     const LLType* t = LLStructType::get(gIR->context(), V1->getType(), V2->getType(), NULL);
920     return DtoAggrPair(t, V1, V2, name);
921 }
922
923 LLValue* DtoAggrPaint(LLValue* aggr, const LLType* as)
924 {
925     if (aggr->getType() == as)
926         return aggr;
927
928     LLValue* res = llvm::UndefValue::get(as);
929
930     LLValue* V = gIR->ir->CreateExtractValue(aggr, 0, "tmp");;
931     V = DtoBitCast(V, as->getContainedType(0));
932     res = gIR->ir->CreateInsertValue(res, V, 0, "tmp");
933
934     V = gIR->ir->CreateExtractValue(aggr, 1, "tmp");;
935     V = DtoBitCast(V, as->getContainedType(1));
936     return gIR->ir->CreateInsertValue(res, V, 1, "tmp");
937 }
938
939 LLValue* DtoAggrPairSwap(LLValue* aggr)
940 {
941     Logger::println("swapping aggr pair");
942     LLValue* r = gIR->ir->CreateExtractValue(aggr, 0);
943     LLValue* i = gIR->ir->CreateExtractValue(aggr, 1);
944     return DtoAggrPair(i, r, "swapped");
945 }
Note: See TracBrowser for help on using the browser.
Copyright © 2008, LDC Development Team.