/
tollvm.cpp
1090 lines (913 loc) · 32.6 KB
/
tollvm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===-- tollvm.cpp --------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/tollvm.h"
#include "aggregate.h"
#include "declaration.h"
#include "dsymbol.h"
#include "id.h"
#include "init.h"
#include "module.h"
#include "gen/arrays.h"
#include "gen/classes.h"
#include "gen/complex.h"
#include "gen/dvalue.h"
#include "gen/functions.h"
#include "gen/irstate.h"
#include "gen/linkage.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/pragma.h"
#include "gen/runtime.h"
#include "gen/structs.h"
#include "gen/typeinf.h"
#include "ir/irtype.h"
#include "ir/irtypeclass.h"
#include "ir/irtypefunction.h"
#include "ir/irtypestruct.h"
bool DtoIsPassedByRef(Type* type)
{
Type* typ = type->toBasetype();
TY t = typ->ty;
return (t == Tstruct || t == Tsarray);
}
#if LDC_LLVM_VER >= 303
llvm::Attribute::AttrKind DtoShouldExtend(Type* type)
#elif LDC_LLVM_VER == 302
llvm::Attributes::AttrVal DtoShouldExtend(Type* type)
#else
llvm::Attributes DtoShouldExtend(Type* type)
#endif
{
type = type->toBasetype();
if (type->isintegral())
{
switch(type->ty)
{
case Tint8:
case Tint16:
#if LDC_LLVM_VER >= 303
return llvm::Attribute::SExt;
#elif LDC_LLVM_VER == 302
return llvm::Attributes::SExt;
#else
return llvm::Attribute::SExt;
#endif
case Tuns8:
case Tuns16:
#if LDC_LLVM_VER >= 303
return llvm::Attribute::ZExt;
#elif LDC_LLVM_VER == 302
return llvm::Attributes::ZExt;
#else
return llvm::Attribute::ZExt;
#endif
default:
// Do not extend.
break;
}
}
#if LDC_LLVM_VER >= 303
return llvm::Attribute::None;
#elif LDC_LLVM_VER == 302
return llvm::Attributes::None;
#else
return llvm::Attribute::None;
#endif
}
LLType* DtoType(Type* t)
{
t = stripModifiers( t );
if (t->irtype)
{
return t->irtype->getLLType();
}
IF_LOG Logger::println("Building type: %s", t->toChars());
LOG_SCOPE;
assert(t);
switch (t->ty)
{
// basic types
case Tvoid:
case Tint8:
case Tuns8:
case Tint16:
case Tuns16:
case Tint32:
case Tuns32:
case Tint64:
case Tuns64:
case Tfloat32:
case Tfloat64:
case Tfloat80:
case Timaginary32:
case Timaginary64:
case Timaginary80:
case Tcomplex32:
case Tcomplex64:
case Tcomplex80:
//case Tbit:
case Tbool:
case Tchar:
case Twchar:
case Tdchar:
{
return IrTypeBasic::get(t)->getLLType();
}
// pointers
case Tnull:
case Tpointer:
{
return IrTypePointer::get(t)->getLLType();
}
// arrays
case Tarray:
{
return IrTypeArray::get(t)->getLLType();
}
case Tsarray:
{
return IrTypeSArray::get(t)->getLLType();
}
// aggregates
case Tstruct:
{
TypeStruct* ts = static_cast<TypeStruct*>(t);
if (ts->sym->type->irtype)
{
// This should not happen, but the frontend seems to be buggy. Not
// sure if this is the best way to handle the situation, but we
// certainly don't want to override ts->sym->type->irtype.
IF_LOG Logger::cout() << "Struct with multiple Types detected: " <<
ts->toChars() << " (" << ts->sym->locToChars() << ")" << std::endl;
return ts->sym->type->irtype->getLLType();
}
return IrTypeStruct::get(ts->sym)->getLLType();
}
case Tclass:
{
TypeClass* tc = static_cast<TypeClass*>(t);
if (tc->sym->type->irtype)
{
// See Tstruct case.
IF_LOG Logger::cout() << "Class with multiple Types detected: " <<
tc->toChars() << " (" << tc->sym->locToChars() << ")" << std::endl;
return tc->sym->type->irtype->getLLType();
}
return IrTypeClass::get(tc->sym)->getLLType();
}
// functions
case Tfunction:
{
return IrTypeFunction::get(t)->getLLType();
}
// delegates
case Tdelegate:
{
return IrTypeDelegate::get(t)->getLLType();
}
// typedefs
// enum
// FIXME: maybe just call toBasetype first ?
case Ttypedef:
case Tenum:
{
Type* bt = t->toBasetype();
assert(bt);
return DtoType(bt);
}
// associative arrays
case Taarray:
return getVoidPtrType();
case Tvector:
{
return IrTypeVector::get(t)->getLLType();
}
/*
Not needed atm as VarDecls for tuples are rewritten as a string of
VarDecls for the fields (u -> _u_field_0, ...)
case Ttuple:
{
TypeTuple* ttupl = static_cast<TypeTuple*>(t);
return DtoStructTypeFromArguments(ttupl->arguments);
}
*/
default:
llvm_unreachable("Unknown class of D Type!");
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////
/*
LLType* DtoStructTypeFromArguments(Arguments* arguments)
{
if (!arguments)
return LLType::getVoidTy(gIR->context());
std::vector<LLType*> types;
for (size_t i = 0; i < arguments->dim; i++)
{
Argument *arg = static_cast<Argument *>(arguments->data[i]);
assert(arg && arg->type);
types.push_back(DtoType(arg->type));
}
return LLStructType::get(types);
}
*/
//////////////////////////////////////////////////////////////////////////////////////////
LLType* voidToI8(LLType* t)
{
if (t == LLType::getVoidTy(gIR->context()))
return LLType::getInt8Ty(gIR->context());
return t;
}
//////////////////////////////////////////////////////////////////////////////////////////
LLType* i1ToI8(LLType* t)
{
if (t == LLType::getInt1Ty(gIR->context()))
return LLType::getInt8Ty(gIR->context());
return t;
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoDelegateEquals(TOK op, LLValue* lhs, LLValue* rhs)
{
Logger::println("Doing delegate equality");
llvm::Value *b1, *b2;
if (rhs == NULL)
{
rhs = LLConstant::getNullValue(lhs->getType());
}
LLValue* l = gIR->ir->CreateExtractValue(lhs, 0);
LLValue* r = gIR->ir->CreateExtractValue(rhs, 0);
b1 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,l,r,"tmp");
l = gIR->ir->CreateExtractValue(lhs, 1);
r = gIR->ir->CreateExtractValue(rhs, 1);
b2 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,l,r,"tmp");
LLValue* b = gIR->ir->CreateAnd(b1,b2,"tmp");
if (op == TOKnotequal || op == TOKnotidentity)
return gIR->ir->CreateNot(b,"tmp");
return b;
}
//////////////////////////////////////////////////////////////////////////////////////////
LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
{
// global/static variable
if (VarDeclaration* vd = sym->isVarDeclaration())
{
IF_LOG Logger::println("Variable %savailable externally: %s",
(vd->availableExternally ? "" : "not "), vd->toChars());
// generated by inlining semantics run
if (vd->availableExternally)
return llvm::GlobalValue::AvailableExternallyLinkage;
// template
if (DtoIsTemplateInstance(sym))
return templateLinkage;
// Currently, we have to consider all variables, even function-local
// statics, to be external, as CTFE might cause template functions
// instances to be semantic3'd that occur within the body of a function
// from an imported module. Consequently, a copy of them is codegen'd
// in the importing module, even if they might reference a static in a
// function in the imported module (e.g. via an alias parameter).
//
// A fix for this would be to track instantiations/semantic3 runs made
// solely for CTFE purposes in a way similar to how the extra inlining
// semantic runs are handled.
//
// LDC_FIXME: Can this also occur for functions? Find a better solution.
if (true || vd->storage_class & STCextern)
return llvm::GlobalValue::ExternalLinkage;
}
else if (FuncDeclaration* fdecl = sym->isFuncDeclaration())
{
IF_LOG Logger::println("Function %savailable externally: %s",
(fdecl->availableExternally ? "" : "not "), fdecl->toChars());
assert(fdecl->type->ty == Tfunction);
TypeFunction* ft = static_cast<TypeFunction*>(fdecl->type);
// intrinsics are always external
if (fdecl->llvmInternal == LLVMintrinsic)
return llvm::GlobalValue::ExternalLinkage;
// Mark functions generated by an inlining semantic run as
// available_externally. Naked functions are turned into module-level
// inline asm and are thus declaration-only as far as the LLVM IR level
// is concerned.
if (fdecl->availableExternally && !fdecl->naked)
return llvm::GlobalValue::AvailableExternallyLinkage;
// array operations are always template linkage
if (fdecl->isArrayOp == 1)
return templateLinkage;
// template instances should have weak linkage
// but only if there's a body, and it's not naked
// otherwise we make it external
if (DtoIsTemplateInstance(fdecl) && fdecl->fbody && !fdecl->naked)
return templateLinkage;
// extern(C) functions are always external
if (ft->linkage == LINKc)
return llvm::GlobalValue::ExternalLinkage;
// If a function without a body is nested in another
// function, we cannot use internal linkage for that
// function (see below about nested functions)
// FIXME: maybe there is a better way without emission
// of needless symbols?
if (!fdecl->fbody)
return llvm::GlobalValue::ExternalLinkage;
}
// class
else if (ClassDeclaration* cd = sym->isClassDeclaration())
{
IF_LOG Logger::println("Class %savailable externally: %s",
(cd->availableExternally ? "" : "not "), vd->toChars());
// generated by inlining semantics run
if (cd->availableExternally)
return llvm::GlobalValue::AvailableExternallyLinkage;
// template
if (DtoIsTemplateInstance(cd))
return templateLinkage;
}
else
{
llvm_unreachable("not global/function");
}
// Check if sym is a nested function and we can declare it as internal.
//
// Nested naked functions and the implicitly generated __require/__ensure
// functions for in/out contracts cannot be internalized. The reason
// for the latter is that contract functions, despite being nested, can be
// referenced from other D modules e.g. in the case of contracts on
// interface methods (where __require/__ensure are emitted to the module
// where the interface is declared, but an actual interface implementation
// can be in a completely different place).
FuncDeclaration* fd = sym->isFuncDeclaration();
if (!fd || (!fd->naked && fd->ident != Id::require && fd->ident != Id::ensure))
{
// Any symbol nested in a function that cannot be inlined can't be
// referenced directly from outside that function, so we can give
// such symbols internal linkage. This holds even if nested indirectly,
// such as member functions of aggregates nested in functions.
//
// Note: This must be checked after things like template member-ness or
// symbols nested in templates would get duplicated for each module,
// breaking things like
// ---
// int counter(T)() { static int i; return i++; }"
// ---
// if instances get emitted in multiple object files because they'd use
// different instances of 'i'.
// TODO: Check if we are giving away too much inlining potential due to
// canInline being overly conservative here.
for (Dsymbol* parent = sym->parent; parent ; parent = parent->parent)
{
FuncDeclaration *parentFd = parent->isFuncDeclaration();
if (parentFd && !parentFd->canInline(parentFd->needThis(), false, false))
{
// We also cannot internalize nested functions which are
// leaked to the outside via a templated return type, because
// that type will also be codegen'd in any caller modules (see
// GitHub issue #131).
// Since we can't easily determine if this is really the case
// here, just don't internalize it if the parent returns a
// template at all, to be safe.
TypeFunction* tf = static_cast<TypeFunction*>(parentFd->type);
if (!DtoIsTemplateInstance(tf->next->toDsymbol(parentFd->scope)))
return llvm::GlobalValue::InternalLinkage;
}
}
}
// default to external linkage
return llvm::GlobalValue::ExternalLinkage;
}
static bool isAvailableExternally(Dsymbol* sym)
{
if (VarDeclaration* vd = sym->isVarDeclaration())
return vd->availableExternally;
if (FuncDeclaration* fd = sym->isFuncDeclaration())
return fd->availableExternally;
if (AggregateDeclaration* ad = sym->isAggregateDeclaration())
return ad->availableExternally;
return false;
}
llvm::GlobalValue::LinkageTypes DtoInternalLinkage(Dsymbol* sym)
{
if (DtoIsTemplateInstance(sym)) {
if (isAvailableExternally(sym))
return llvm::GlobalValue::AvailableExternallyLinkage;
return templateLinkage;
}
else
return llvm::GlobalValue::InternalLinkage;
}
llvm::GlobalValue::LinkageTypes DtoExternalLinkage(Dsymbol* sym)
{
if (DtoIsTemplateInstance(sym))
return templateLinkage;
else if (isAvailableExternally(sym))
return llvm::GlobalValue::AvailableExternallyLinkage;
else
return llvm::GlobalValue::ExternalLinkage;
}
//////////////////////////////////////////////////////////////////////////////////////////
LLIntegerType* DtoSize_t()
{
// the type of size_t does not change once set
static LLIntegerType* t = NULL;
if (t == NULL)
t = (global.params.is64bit) ? LLType::getInt64Ty(gIR->context()) : LLType::getInt32Ty(gIR->context());
return t;
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoGEP1(LLValue* ptr, LLValue* i0, const char* var, llvm::BasicBlock* bb)
{
return llvm::GetElementPtrInst::Create(ptr, i0, var?var:"tmp", bb?bb:gIR->scopebb());
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoGEP(LLValue* ptr, LLValue* i0, LLValue* i1, const char* var, llvm::BasicBlock* bb)
{
LLValue* v[] = { i0, i1 };
return llvm::GetElementPtrInst::Create(ptr, v, var?var:"tmp", bb?bb:gIR->scopebb());
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoGEPi1(LLValue* ptr, unsigned i, const char* var, llvm::BasicBlock* bb)
{
return llvm::GetElementPtrInst::Create(ptr, DtoConstUint(i), var?var:"tmp", bb?bb:gIR->scopebb());
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoGEPi(LLValue* ptr, unsigned i0, unsigned i1, const char* var, llvm::BasicBlock* bb)
{
LLValue* v[] = { DtoConstUint(i0), DtoConstUint(i1) };
return llvm::GetElementPtrInst::Create(ptr, v, var?var:"tmp", bb?bb:gIR->scopebb());
}
//////////////////////////////////////////////////////////////////////////////////////////
LLConstant* DtoGEPi(LLConstant* ptr, unsigned i0, unsigned i1)
{
LLValue* v[] = { DtoConstUint(i0), DtoConstUint(i1) };
return llvm::ConstantExpr::getGetElementPtr(ptr, v, true);
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoMemSet(LLValue* dst, LLValue* val, LLValue* nbytes)
{
LLType* VoidPtrTy = getVoidPtrType();
dst = DtoBitCast(dst, VoidPtrTy);
gIR->ir->CreateMemSet(dst, val, nbytes, 1 /*Align*/, false /*isVolatile*/);
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoMemSetZero(LLValue* dst, LLValue* nbytes)
{
DtoMemSet(dst, DtoConstUbyte(0), nbytes);
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoMemCpy(LLValue* dst, LLValue* src, LLValue* nbytes, unsigned align)
{
LLType* VoidPtrTy = getVoidPtrType();
dst = DtoBitCast(dst, VoidPtrTy);
src = DtoBitCast(src, VoidPtrTy);
gIR->ir->CreateMemCpy(dst, src, nbytes, align, false /*isVolatile*/);
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoMemCmp(LLValue* lhs, LLValue* rhs, LLValue* nbytes)
{
// int memcmp ( const void * ptr1, const void * ptr2, size_t num );
LLType* VoidPtrTy = getVoidPtrType();
LLFunction* fn = gIR->module->getFunction("memcmp");
if (!fn)
{
LLType* Tys[] = { VoidPtrTy, VoidPtrTy, DtoSize_t() };
LLFunctionType* fty = LLFunctionType::get(LLType::getInt32Ty(gIR->context()),
Tys, false);
fn = LLFunction::Create(fty, LLGlobalValue::ExternalLinkage, "memcmp", gIR->module);
}
lhs = DtoBitCast(lhs, VoidPtrTy);
rhs = DtoBitCast(rhs, VoidPtrTy);
return gIR->ir->CreateCall3(fn, lhs, rhs, nbytes, "tmp");
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoAggrZeroInit(LLValue* v)
{
uint64_t n = getTypeStoreSize(v->getType()->getContainedType(0));
DtoMemSetZero(v, DtoConstSize_t(n));
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoAggrCopy(LLValue* dst, LLValue* src)
{
uint64_t n = getTypeStoreSize(dst->getType()->getContainedType(0));
DtoMemCpy(dst, src, DtoConstSize_t(n));
}
//////////////////////////////////////////////////////////////////////////////////////////
void DtoMemoryBarrier(bool ll, bool ls, bool sl, bool ss, bool device)
{
// FIXME: implement me
/*llvm::Function* fn = GET_INTRINSIC_DECL(memory_barrier);
assert(fn != NULL);
LLSmallVector<LLValue*, 5> llargs;
llargs.push_back(DtoConstBool(ll));
llargs.push_back(DtoConstBool(ls));
llargs.push_back(DtoConstBool(sl));
llargs.push_back(DtoConstBool(ss));
llargs.push_back(DtoConstBool(device));
llvm::CallInst::Create(fn, llargs, "", gIR->scopebb());*/
}
//////////////////////////////////////////////////////////////////////////////////////////
llvm::ConstantInt* DtoConstSize_t(uint64_t i)
{
return LLConstantInt::get(DtoSize_t(), i, false);
}
llvm::ConstantInt* DtoConstUint(unsigned i)
{
return LLConstantInt::get(LLType::getInt32Ty(gIR->context()), i, false);
}
llvm::ConstantInt* DtoConstInt(int i)
{
return LLConstantInt::get(LLType::getInt32Ty(gIR->context()), i, true);
}
LLConstant* DtoConstBool(bool b)
{
return LLConstantInt::get(LLType::getInt1Ty(gIR->context()), b, false);
}
llvm::ConstantInt* DtoConstUbyte(unsigned char i)
{
return LLConstantInt::get(LLType::getInt8Ty(gIR->context()), i, false);
}
LLConstant* DtoConstFP(Type* t, longdouble value)
{
LLType* llty = DtoType(t);
assert(llty->isFloatingPointTy());
if(llty == LLType::getFloatTy(gIR->context()) || llty == LLType::getDoubleTy(gIR->context()))
return LLConstantFP::get(llty, value);
else if(llty == LLType::getX86_FP80Ty(gIR->context())) {
uint64_t bits[] = { 0, 0 };
bits[0] = *reinterpret_cast<uint64_t*>(&value);
bits[1] = *reinterpret_cast<uint16_t*>(reinterpret_cast<uint64_t*>(&value) + 1);
#if LDC_LLVM_VER >= 303
return LLConstantFP::get(gIR->context(), APFloat(APFloat::x87DoubleExtended, APInt(80, 2, bits)));
#else
return LLConstantFP::get(gIR->context(), APFloat(APInt(80, 2, bits)));
#endif
} else if(llty == LLType::getPPC_FP128Ty(gIR->context())) {
uint64_t bits[] = {0, 0};
bits[0] = *reinterpret_cast<uint64_t*>(&value);
bits[1] = *reinterpret_cast<uint16_t*>(reinterpret_cast<uint64_t*>(&value) + 1);
#if LDC_LLVM_VER >= 303
return LLConstantFP::get(gIR->context(), APFloat(APFloat::PPCDoubleDouble, APInt(128, 2, bits)));
#else
return LLConstantFP::get(gIR->context(), APFloat(APInt(128, 2, bits)));
#endif
}
llvm_unreachable("Unknown floating point type encountered");
}
//////////////////////////////////////////////////////////////////////////////////////////
LLConstant* DtoConstString(const char* str)
{
llvm::StringRef s(str ? str : "");
LLConstant* init = llvm::ConstantDataArray::getString(gIR->context(), s, true);
llvm::GlobalVariable* gvar = new llvm::GlobalVariable(
*gIR->module, init->getType(), true, llvm::GlobalValue::InternalLinkage, init, ".str");
gvar->setUnnamedAddr(true);
LLConstant* idxs[] = { DtoConstUint(0), DtoConstUint(0) };
return DtoConstSlice(
DtoConstSize_t(s.size()),
llvm::ConstantExpr::getGetElementPtr(gvar, idxs, true),
Type::tchar->arrayOf()
);
}
LLConstant* DtoConstStringPtr(const char* str, const char* section)
{
llvm::StringRef s(str);
LLConstant* init = llvm::ConstantDataArray::getString(gIR->context(), s, true);
llvm::GlobalVariable* gvar = new llvm::GlobalVariable(
*gIR->module, init->getType(), true, llvm::GlobalValue::InternalLinkage, init, ".str");
if (section) gvar->setSection(section);
gvar->setUnnamedAddr(true);
LLConstant* idxs[] = { DtoConstUint(0), DtoConstUint(0) };
return llvm::ConstantExpr::getGetElementPtr(gvar, idxs, true);
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoLoad(LLValue* src, const char* name)
{
// if (Logger::enabled())
// Logger::cout() << "loading " << *src << '\n';
llvm::LoadInst* ld = gIR->ir->CreateLoad(src, name ? name : "tmp");
//ld->setVolatile(gIR->func()->inVolatile);
return ld;
}
// Like DtoLoad, but the pointer is guaranteed to be aligned appropriately for the type.
LLValue* DtoAlignedLoad(LLValue* src, const char* name)
{
llvm::LoadInst* ld = gIR->ir->CreateLoad(src, name ? name : "tmp");
ld->setAlignment(getABITypeAlign(ld->getType()));
return ld;
}
void DtoStore(LLValue* src, LLValue* dst)
{
assert(src->getType() != llvm::Type::getInt1Ty(gIR->context()) &&
"Should store bools as i8 instead of i1.");
gIR->ir->CreateStore(src,dst);
}
void DtoStoreZextI8(LLValue* src, LLValue* dst)
{
if (src->getType() == llvm::Type::getInt1Ty(gIR->context()))
{
llvm::Type* i8 = llvm::Type::getInt8Ty(gIR->context());
assert(dst->getType()->getContainedType(0) == i8);
src = gIR->ir->CreateZExt(src, i8);
}
gIR->ir->CreateStore(src, dst);
}
// Like DtoStore, but the pointer is guaranteed to be aligned appropriately for the type.
void DtoAlignedStore(LLValue* src, LLValue* dst)
{
assert(src->getType() != llvm::Type::getInt1Ty(gIR->context()) &&
"Should store bools as i8 instead of i1.");
llvm::StoreInst* st = gIR->ir->CreateStore(src,dst);
st->setAlignment(getABITypeAlign(src->getType()));
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoBitCast(LLValue* v, LLType* t, const char* name)
{
if (v->getType() == t)
return v;
assert(!isaStruct(t));
return gIR->ir->CreateBitCast(v, t, name ? name : "tmp");
}
LLConstant* DtoBitCast(LLConstant* v, LLType* t)
{
if (v->getType() == t)
return v;
return llvm::ConstantExpr::getBitCast(v, t);
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoInsertValue(LLValue* aggr, LLValue* v, unsigned idx, const char* name)
{
return gIR->ir->CreateInsertValue(aggr, v, idx, name ? name : "tmp");
}
LLValue* DtoExtractValue(LLValue* aggr, unsigned idx, const char* name)
{
return gIR->ir->CreateExtractValue(aggr, idx, name ? name : "tmp");
}
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoInsertElement(LLValue* vec, LLValue* v, LLValue *idx, const char* name)
{
return gIR->ir->CreateInsertElement(vec, v, idx, name ? name : "tmp");
}
LLValue* DtoExtractElement(LLValue* vec, LLValue *idx, const char* name)
{
return gIR->ir->CreateExtractElement(vec, idx, name ? name : "tmp");
}
LLValue* DtoInsertElement(LLValue* vec, LLValue* v, unsigned idx, const char* name)
{
return DtoInsertElement(vec, v, DtoConstUint(idx), name);
}
LLValue* DtoExtractElement(LLValue* vec, unsigned idx, const char* name)
{
return DtoExtractElement(vec, DtoConstUint(idx), name);
}
//////////////////////////////////////////////////////////////////////////////////////////
LLPointerType* isaPointer(LLValue* v)
{
return llvm::dyn_cast<LLPointerType>(v->getType());
}
LLPointerType* isaPointer(LLType* t)
{
return llvm::dyn_cast<LLPointerType>(t);
}
LLArrayType* isaArray(LLValue* v)
{
return llvm::dyn_cast<LLArrayType>(v->getType());
}
LLArrayType* isaArray(LLType* t)
{
return llvm::dyn_cast<LLArrayType>(t);
}
LLStructType* isaStruct(LLValue* v)
{
return llvm::dyn_cast<LLStructType>(v->getType());
}
LLStructType* isaStruct(LLType* t)
{
return llvm::dyn_cast<LLStructType>(t);
}
LLFunctionType* isaFunction(LLValue* v)
{
return llvm::dyn_cast<LLFunctionType>(v->getType());
}
LLFunctionType* isaFunction(LLType* t)
{
return llvm::dyn_cast<LLFunctionType>(t);
}
LLConstant* isaConstant(LLValue* v)
{
return llvm::dyn_cast<llvm::Constant>(v);
}
llvm::ConstantInt* isaConstantInt(LLValue* v)
{
return llvm::dyn_cast<llvm::ConstantInt>(v);
}
llvm::Argument* isaArgument(LLValue* v)
{
return llvm::dyn_cast<llvm::Argument>(v);
}
llvm::GlobalVariable* isaGlobalVar(LLValue* v)
{
return llvm::dyn_cast<llvm::GlobalVariable>(v);
}
//////////////////////////////////////////////////////////////////////////////////////////
LLPointerType* getPtrToType(LLType* t)
{
if (t == LLType::getVoidTy(gIR->context()))
t = LLType::getInt8Ty(gIR->context());
return LLPointerType::get(t, 0);
}
LLPointerType* getVoidPtrType()
{
return getPtrToType(LLType::getInt8Ty(gIR->context()));
}
llvm::ConstantPointerNull* getNullPtr(LLType* t)
{
LLPointerType* pt = llvm::cast<LLPointerType>(t);
return llvm::ConstantPointerNull::get(pt);
}
LLConstant* getNullValue(LLType* t)
{
return LLConstant::getNullValue(t);
}
//////////////////////////////////////////////////////////////////////////////////////////
size_t getTypeBitSize(LLType* t)
{
return gDataLayout->getTypeSizeInBits(t);
}
size_t getTypeStoreSize(LLType* t)
{
return gDataLayout->getTypeStoreSize(t);
}
size_t getTypePaddedSize(LLType* t)
{
size_t sz = gDataLayout->getTypeAllocSize(t);
//Logger::cout() << "abi type size of: " << *t << " == " << sz << '\n';
return sz;
}
size_t getTypeAllocSize(LLType* t)
{
return gDataLayout->getTypeAllocSize(t);
}
unsigned char getABITypeAlign(LLType* t)
{
return gDataLayout->getABITypeAlignment(t);
}
unsigned char getPrefTypeAlign(LLType* t)
{
return gDataLayout->getPrefTypeAlignment(t);
}
LLType* getBiggestType(LLType** begin, size_t n)
{
LLType* bigTy = 0;
size_t bigSize = 0;
size_t bigAlign = 0;
LLType** end = begin+n;
while (begin != end)
{
LLType* T = *begin;
size_t sz = getTypePaddedSize(T);
size_t ali = getABITypeAlign(T);
if (sz > bigSize || (sz == bigSize && ali > bigAlign))
{
bigTy = T;
bigSize = sz;
bigAlign = ali;
}
++begin;
}
// will be null for n==0
return bigTy;
}
//////////////////////////////////////////////////////////////////////////////////////////
LLStructType* DtoInterfaceInfoType()
{
if (gIR->interfaceInfoType)
return gIR->interfaceInfoType;
// build interface info type
LLSmallVector<LLType*, 3> types;
// ClassInfo classinfo
ClassDeclaration* cd2 = ClassDeclaration::classinfo;
DtoResolveClass(cd2);
types.push_back(DtoType(cd2->type));
// void*[] vtbl
LLSmallVector<LLType*, 2> vtbltypes;
vtbltypes.push_back(DtoSize_t());
LLType* byteptrptrty = getPtrToType(getPtrToType(LLType::getInt8Ty(gIR->context())));
vtbltypes.push_back(byteptrptrty);
types.push_back(LLStructType::get(gIR->context(), vtbltypes));
// int offset
types.push_back(LLType::getInt32Ty(gIR->context()));
// create type
gIR->interfaceInfoType = LLStructType::get(gIR->context(), types);
return gIR->interfaceInfoType;
}
//////////////////////////////////////////////////////////////////////////////////////////
LLStructType* DtoMutexType()
{
if (gIR->mutexType)
return gIR->mutexType;
// The structures defined here must be the same as in druntime/src/rt/critical.c
// Windows
if (global.params.targetTriple.isOSWindows())
{
llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(gIR->context());
llvm::Type *Int32Ty = llvm::Type::getInt32Ty(gIR->context());
// Build RTL_CRITICAL_SECTION; size is 24 (32bit) or 40 (64bit)
LLType *rtl_types[] = {
VoidPtrTy, // Pointer to DebugInfo
Int32Ty, // LockCount
Int32Ty, // RecursionCount
VoidPtrTy, // Handle of OwningThread
VoidPtrTy, // Handle of LockSemaphore
VoidPtrTy // SpinCount
};
LLStructType* rtl = LLStructType::create(gIR->context(), rtl_types, "RTL_CRITICAL_SECTION");
// Build D_CRITICAL_SECTION; size is 28 (32bit) or 48 (64bit)
LLStructType *mutex = LLStructType::create(gIR->context(), "D_CRITICAL_SECTION");
LLType *types[] = { getPtrToType(mutex), rtl };
mutex->setBody(types);
// Cache type
gIR->mutexType = mutex;
return mutex;
}
// FreeBSD
else if (global.params.targetTriple.getOS() == llvm::Triple::FreeBSD) {
// Just a pointer
return LLStructType::get(gIR->context(), DtoSize_t());
}
// pthread_fastlock