-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
codegen.cpp
8487 lines (8018 loc) · 370 KB
/
codegen.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
// This file is a part of Julia. License is MIT: https://julialang.org/license
#include "llvm-version.h"
#include "platform.h"
#if defined(_OS_WINDOWS_)
// use ELF because RuntimeDyld COFF i686 support didn't exist
// use ELF because RuntimeDyld COFF X86_64 doesn't seem to work (fails to generate function pointers)?
#define FORCE_ELF
#endif
#if defined(_CPU_X86_)
#define JL_NEED_FLOATTEMP_VAR 1
#endif
#if defined(_OS_WINDOWS_) || defined(_OS_FREEBSD_)
#define JL_DISABLE_FPO
#endif
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#include <setjmp.h>
#include <string>
#include <fstream>
#include <map>
#include <array>
#include <vector>
#include <set>
#include <functional>
// target machine computation
#include <llvm/CodeGen/TargetSubtargetInfo.h>
#if JL_LLVM_VERSION >= 140000
#include <llvm/MC/TargetRegistry.h>
#else
#include <llvm/Support/TargetRegistry.h>
#endif
#include <llvm/Target/TargetOptions.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Object/SymbolSize.h>
#include <llvm/InitializePasses.h>
// IR building
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/Object/ObjectFile.h>
#include <llvm/IR/DIBuilder.h>
#include <llvm/AsmParser/Parser.h>
#include <llvm/DebugInfo/DIContext.h>
#include "llvm/IR/DebugInfoMetadata.h"
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/Attributes.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/MDBuilder.h>
// support
#include <llvm/ADT/SmallBitVector.h>
#include <llvm/ADT/Optional.h>
#include <llvm/ADT/Statistic.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/SourceMgr.h> // for llvmcall
#include <llvm/Transforms/Utils/Cloning.h> // for llvmcall inlining
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
#include <llvm/IR/Verifier.h> // for llvmcall validation
#include <llvm/IR/PassTimingInfo.h>
#include <llvm/Bitcode/BitcodeWriter.h>
// C API
#include <llvm-c/Types.h>
// for configuration options
#include <llvm/Support/PrettyStackTrace.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Process.h>
#include <llvm/IR/InlineAsm.h>
#if defined(_CPU_ARM_) || defined(_CPU_AARCH64_)
# include <sys/utsname.h>
#endif
#if defined(USE_POLLY)
#include <polly/RegisterPasses.h>
#include <polly/ScopDetection.h>
#endif
#include <llvm/Target/TargetMachine.h>
#include "llvm/Support/Path.h" // for llvm::sys::path
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Linker/Linker.h>
using namespace llvm;
//Drag some useful type functions into our namespace
//to reduce verbosity of our code
auto getInt1Ty(LLVMContext &ctxt) {
return Type::getInt1Ty(ctxt);
}
auto getInt8Ty(LLVMContext &ctxt) {
return Type::getInt8Ty(ctxt);
}
auto getInt16Ty(LLVMContext &ctxt) {
return Type::getInt16Ty(ctxt);
}
auto getInt32Ty(LLVMContext &ctxt) {
return Type::getInt32Ty(ctxt);
}
auto getInt64Ty(LLVMContext &ctxt) {
return Type::getInt64Ty(ctxt);
}
auto getHalfTy(LLVMContext &ctxt) {
return Type::getHalfTy(ctxt);
}
auto getFloatTy(LLVMContext &ctxt) {
return Type::getFloatTy(ctxt);
}
auto getDoubleTy(LLVMContext &ctxt) {
return Type::getDoubleTy(ctxt);
}
auto getFP128Ty(LLVMContext &ctxt) {
return Type::getFP128Ty(ctxt);
}
auto getVoidTy(LLVMContext &ctxt) {
return Type::getVoidTy(ctxt);
}
auto getCharTy(LLVMContext &ctxt) {
return getInt32Ty(ctxt);
}
auto getSizeTy(LLVMContext &ctxt) {
if (sizeof(size_t) > sizeof(uint32_t)) {
return getInt64Ty(ctxt);
} else {
return getInt32Ty(ctxt);
}
}
auto getInt8PtrTy(LLVMContext &ctxt) {
return Type::getInt8PtrTy(ctxt);
}
auto getInt16PtrTy(LLVMContext &ctxt) {
return Type::getInt16PtrTy(ctxt);
}
auto getInt32PtrTy(LLVMContext &ctxt) {
return Type::getInt32PtrTy(ctxt);
}
auto getInt64PtrTy(LLVMContext &ctxt) {
return Type::getInt64PtrTy(ctxt);
}
auto getFloatPtrTy(LLVMContext &ctxt) {
return Type::getFloatPtrTy(ctxt);
}
auto getDoublePtrTy(LLVMContext &ctxt) {
return Type::getDoublePtrTy(ctxt);
}
auto getSizePtrTy(LLVMContext &ctxt) {
if (sizeof(size_t) > sizeof(uint32_t)) {
return getInt64PtrTy(ctxt);
} else {
return getInt32PtrTy(ctxt);
}
}
typedef Instruction TerminatorInst;
#if defined(_OS_WINDOWS_) && !defined(NOMINMAX)
#define NOMINMAX
#endif
#include "julia.h"
#include "julia_internal.h"
#include "jitlayers.h"
#include "codegen_shared.h"
#include "processor.h"
#include "julia_assert.h"
JL_STREAM *dump_emitted_mi_name_stream = NULL;
extern "C" JL_DLLEXPORT
void jl_dump_emitted_mi_name_impl(void *s)
{
dump_emitted_mi_name_stream = (JL_STREAM*)s;
}
extern "C" {
#include "builtin_proto.h"
extern void __stack_chk_fail();
#ifdef _OS_WINDOWS_
#if defined(_CPU_X86_64_)
#if defined(_COMPILER_GCC_)
extern void ___chkstk_ms(void);
#else
extern void __chkstk(void);
#endif
#else
#if defined(_COMPILER_GCC_)
#undef _alloca
extern void _alloca(void);
#else
extern void _chkstk(void);
#endif
#endif
//void *force_chkstk(void) {
// return alloca(40960);
//}
#endif
}
// for image reloading
bool imaging_mode = false;
// shared llvm state
static LLVMContext &jl_LLVMContext = *(new LLVMContext());
TargetMachine *jl_TargetMachine;
static DataLayout &jl_data_layout = *(new DataLayout(""));
#define jl_Module ctx.f->getParent()
#define jl_builderModule(builder) (builder).GetInsertBlock()->getParent()->getParent()
#define prepare_call(Callee) prepare_call_in(jl_Module, (Callee))
// types
struct jl_typecache_t {
Type *T_jlvalue;
Type *T_pjlvalue;
Type *T_prjlvalue;
Type *T_ppjlvalue;
Type *T_pprjlvalue;
StructType *T_jlarray;
Type *T_pjlarray;
FunctionType *T_jlfunc;
FunctionType *T_jlfuncparams;
IntegerType *T_sigatomic;
Type *T_ppint8;
bool initialized;
jl_typecache_t() :
T_jlvalue(nullptr), T_pjlvalue(nullptr), T_prjlvalue(nullptr),
T_ppjlvalue(nullptr), T_pprjlvalue(nullptr), T_jlarray(nullptr),
T_pjlarray(nullptr), T_jlfunc(nullptr), T_jlfuncparams(nullptr),
T_sigatomic(nullptr), T_ppint8(nullptr), initialized(false) {}
void initialize(LLVMContext &context) {
if (initialized) {
return;
}
initialized = true;
T_ppint8 = PointerType::get(getInt8PtrTy(context), 0);
T_sigatomic = Type::getIntNTy(jl_LLVMContext, sizeof(sig_atomic_t) * 8);
T_jlvalue = JuliaType::get_jlvalue_ty(context);
T_pjlvalue = PointerType::get(T_jlvalue, 0);
T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
T_ppjlvalue = PointerType::get(T_pjlvalue, 0);
T_pprjlvalue = PointerType::get(T_prjlvalue, 0);
T_jlfunc = JuliaType::get_jlfunc_ty(context);
assert(T_jlfunc != NULL);
T_jlfuncparams = JuliaType::get_jlfuncparams_ty(context);
assert(T_jlfuncparams != NULL);
Type *vaelts[] = {PointerType::get(getInt8Ty(context), AddressSpace::Loaded)
, getSizeTy(context)
, getInt16Ty(context)
, getInt16Ty(context)
, getInt32Ty(context)
};
static_assert(sizeof(jl_array_flags_t) == sizeof(int16_t),
"Size of jl_array_flags_t is not the same as int16_t");
T_jlarray = StructType::get(context, makeArrayRef(vaelts));
T_pjlarray = PointerType::get(T_jlarray, 0);
}
};
struct jl_tbaacache_t {
// type-based alias analysis nodes. Indentation of comments indicates hierarchy.
MDNode *tbaa_root; // Everything
MDNode *tbaa_gcframe; // GC frame
// LLVM should have enough info for alias analysis of non-gcframe stack slot
// this is mainly a place holder for `jl_cgval_t::tbaa`
MDNode *tbaa_stack; // stack slot
MDNode *tbaa_unionselbyte; // a selector byte in isbits Union struct fields
MDNode *tbaa_data; // Any user data that `pointerset/ref` are allowed to alias
MDNode *tbaa_binding; // jl_binding_t::value
MDNode *tbaa_value; // jl_value_t, that is not jl_array_t
MDNode *tbaa_mutab; // mutable type
MDNode *tbaa_datatype; // datatype
MDNode *tbaa_immut; // immutable type
MDNode *tbaa_ptrarraybuf; // Data in an array of boxed values
MDNode *tbaa_arraybuf; // Data in an array of POD
MDNode *tbaa_array; // jl_array_t
MDNode *tbaa_arrayptr; // The pointer inside a jl_array_t
MDNode *tbaa_arraysize; // A size in a jl_array_t
MDNode *tbaa_arraylen; // The len in a jl_array_t
MDNode *tbaa_arrayflags; // The flags in a jl_array_t
MDNode *tbaa_arrayoffset; // The offset in a jl_array_t
MDNode *tbaa_arrayselbyte; // a selector byte in a isbits Union jl_array_t
MDNode *tbaa_const; // Memory that is immutable by the time LLVM can see it
bool initialized;
jl_tbaacache_t(): tbaa_root(nullptr), tbaa_gcframe(nullptr), tbaa_stack(nullptr),
tbaa_unionselbyte(nullptr), tbaa_data(nullptr), tbaa_binding(nullptr),
tbaa_value(nullptr), tbaa_mutab(nullptr), tbaa_datatype(nullptr),
tbaa_immut(nullptr), tbaa_ptrarraybuf(nullptr), tbaa_arraybuf(nullptr),
tbaa_array(nullptr), tbaa_arrayptr(nullptr), tbaa_arraysize(nullptr),
tbaa_arraylen(nullptr), tbaa_arrayflags(nullptr), tbaa_arrayoffset(nullptr),
tbaa_arrayselbyte(nullptr), tbaa_const(nullptr), initialized(false) {}
auto tbaa_make_child(MDBuilder &mbuilder, const char *name, MDNode *parent = nullptr, bool isConstant = false) {
MDNode *scalar = mbuilder.createTBAAScalarTypeNode(name, parent ? parent : tbaa_root);
MDNode *n = mbuilder.createTBAAStructTagNode(scalar, scalar, 0, isConstant);
return std::make_pair(n, scalar);
}
void initialize(llvm::LLVMContext &context) {
if (initialized) {
assert(&tbaa_root->getContext() == &context);
return;
}
initialized = true;
MDBuilder mbuilder(context);
MDNode *jtbaa = mbuilder.createTBAARoot("jtbaa");
tbaa_root = mbuilder.createTBAAScalarTypeNode("jtbaa", jtbaa);
tbaa_gcframe = tbaa_make_child(mbuilder, "jtbaa_gcframe").first;
MDNode *tbaa_stack_scalar;
std::tie(tbaa_stack, tbaa_stack_scalar) = tbaa_make_child(mbuilder, "jtbaa_stack");
tbaa_unionselbyte = tbaa_make_child(mbuilder, "jtbaa_unionselbyte", tbaa_stack_scalar).first;
MDNode *tbaa_data_scalar;
std::tie(tbaa_data, tbaa_data_scalar) = tbaa_make_child(mbuilder, "jtbaa_data");
tbaa_binding = tbaa_make_child(mbuilder, "jtbaa_binding", tbaa_data_scalar).first;
MDNode *tbaa_value_scalar;
std::tie(tbaa_value, tbaa_value_scalar) =
tbaa_make_child(mbuilder, "jtbaa_value", tbaa_data_scalar);
MDNode *tbaa_mutab_scalar;
std::tie(tbaa_mutab, tbaa_mutab_scalar) =
tbaa_make_child(mbuilder, "jtbaa_mutab", tbaa_value_scalar);
tbaa_datatype = tbaa_make_child(mbuilder, "jtbaa_datatype", tbaa_mutab_scalar).first;
tbaa_immut = tbaa_make_child(mbuilder, "jtbaa_immut", tbaa_value_scalar).first;
tbaa_arraybuf = tbaa_make_child(mbuilder, "jtbaa_arraybuf", tbaa_data_scalar).first;
tbaa_ptrarraybuf = tbaa_make_child(mbuilder, "jtbaa_ptrarraybuf", tbaa_data_scalar).first;
MDNode *tbaa_array_scalar;
std::tie(tbaa_array, tbaa_array_scalar) = tbaa_make_child(mbuilder, "jtbaa_array");
tbaa_arrayptr = tbaa_make_child(mbuilder, "jtbaa_arrayptr", tbaa_array_scalar).first;
tbaa_arraysize = tbaa_make_child(mbuilder, "jtbaa_arraysize", tbaa_array_scalar).first;
tbaa_arraylen = tbaa_make_child(mbuilder, "jtbaa_arraylen", tbaa_array_scalar).first;
tbaa_arrayflags = tbaa_make_child(mbuilder, "jtbaa_arrayflags", tbaa_array_scalar).first;
tbaa_arrayoffset = tbaa_make_child(mbuilder, "jtbaa_arrayoffset", tbaa_array_scalar).first;
tbaa_const = tbaa_make_child(mbuilder, "jtbaa_const", nullptr, true).first;
tbaa_arrayselbyte = tbaa_make_child(mbuilder, "jtbaa_arrayselbyte", tbaa_array_scalar).first;
}
};
// Basic DITypes
static DICompositeType *jl_value_dillvmt;
static DIDerivedType *jl_pvalue_dillvmt;
static DIDerivedType *jl_ppvalue_dillvmt;
static DISubroutineType *jl_di_func_sig;
static DISubroutineType *jl_di_func_null_sig;
// constants
static bool type_is_ghost(Type *ty)
{
return (ty == getVoidTy(ty->getContext()) || ty->isEmptyTy());
}
// should agree with `Core.Compiler.hasuniquerep`
static bool type_has_unique_rep(jl_value_t *t)
{
if (t == (jl_value_t*)jl_typeofbottom_type)
return false;
if (t == jl_bottom_type)
return true;
if (jl_is_typevar(t))
return false;
if (!jl_is_kind(jl_typeof(t)))
return true;
if (jl_is_concrete_type(t))
return true;
if (jl_is_datatype(t)) {
jl_datatype_t *dt = (jl_datatype_t*)t;
if (dt->name != jl_tuple_typename) {
for (size_t i = 0; i < jl_nparams(dt); i++)
if (!type_has_unique_rep(jl_tparam(dt, i)))
return false;
return true;
}
}
return false;
}
static bool is_uniquerep_Type(jl_value_t *t)
{
return jl_is_type_type(t) && type_has_unique_rep(jl_tparam0(t));
}
class jl_codectx_t;
struct JuliaVariable {
public:
StringLiteral name;
bool isconst;
Type *(*_type)(LLVMContext &C);
JuliaVariable(const JuliaVariable&) = delete;
JuliaVariable(const JuliaVariable&&) = delete;
GlobalVariable *realize(Module *m) {
if (GlobalValue *V = m->getNamedValue(name))
return cast<GlobalVariable>(V);
return new GlobalVariable(*m, _type(m->getContext()),
isconst, GlobalVariable::ExternalLinkage,
NULL, name);
}
GlobalVariable *realize(jl_codectx_t &ctx);
};
static inline void add_named_global(JuliaVariable *name, void *addr)
{
add_named_global(name->name, addr);
}
struct JuliaFunction {
public:
llvm::StringLiteral name;
llvm::FunctionType *(*_type)(llvm::LLVMContext &C);
llvm::AttributeList (*_attrs)(llvm::LLVMContext &C);
JuliaFunction(const JuliaFunction&) = delete;
JuliaFunction(const JuliaFunction&&) = delete;
llvm::Function *realize(llvm::Module *m) {
if (llvm::GlobalValue *V = m->getNamedValue(name))
return llvm::cast<llvm::Function>(V);
llvm::Function *F = llvm::Function::Create(_type(m->getContext()),
llvm::Function::ExternalLinkage,
name, m);
if (_attrs)
F->setAttributes(_attrs(m->getContext()));
return F;
}
};
template<typename T>
static inline void add_named_global(JuliaFunction *name, T *addr)
{
// cast through integer to avoid c++ pedantic warning about casting between
// data and code pointers
add_named_global(name->name, (void*)(uintptr_t)addr);
}
template<typename T>
static inline void add_named_global(StringRef name, T *addr)
{
// cast through integer to avoid c++ pedantic warning about casting between
// data and code pointers
add_named_global(name, (void*)(uintptr_t)addr);
}
AttributeSet Attributes(LLVMContext &C, std::initializer_list<Attribute::AttrKind> attrkinds)
{
SmallVector<Attribute, 8> attrs(attrkinds.size());
for (size_t i = 0; i < attrkinds.size(); i++)
attrs[i] = Attribute::get(C, attrkinds.begin()[i]);
return AttributeSet::get(C, makeArrayRef(attrs));
}
static Type *get_pjlvalue(LLVMContext &C) { return JuliaType::get_pjlvalue_ty(C); }
static FunctionType *get_func_sig(LLVMContext &C) { return JuliaType::get_jlfunc_ty(C); }
static AttributeList get_func_attrs(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet::get(C, makeArrayRef({Attribute::get(C, "thunk")})),
Attributes(C, {Attribute::NonNull}),
None);
}
static AttributeList get_donotdelete_func_attrs(LLVMContext &C)
{
AttributeSet FnAttrs = AttributeSet::get(C, makeArrayRef({Attribute::get(C, "thunk")}));
FnAttrs = FnAttrs.addAttribute(C, Attribute::InaccessibleMemOnly);
FnAttrs = FnAttrs.addAttribute(C, Attribute::WillReturn);
FnAttrs = FnAttrs.addAttribute(C, Attribute::NoUnwind);
return AttributeList::get(C,
FnAttrs,
Attributes(C, {Attribute::NonNull}),
None);
}
static AttributeList get_attrs_noreturn(LLVMContext &C)
{
return AttributeList::get(C,
Attributes(C, {Attribute::NoReturn}),
AttributeSet(),
None);
}
static AttributeList get_attrs_sext(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
{Attributes(C, {Attribute::SExt})});
}
static AttributeList get_attrs_zext(LLVMContext &C)
{
return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
{Attributes(C, {Attribute::ZExt})});
}
// global vars
static const auto jlRTLD_DEFAULT_var = new JuliaVariable{
XSTR(jl_RTLD_DEFAULT_handle),
true,
[](LLVMContext &C) { return static_cast<llvm::Type*>(getInt8PtrTy(C)); },
};
#ifdef _OS_WINDOWS_
static const auto jlexe_var = new JuliaVariable{
XSTR(jl_exe_handle),
true,
[](LLVMContext &C) { return static_cast<llvm::Type*>(getInt8PtrTy(C)); },
};
static const auto jldll_var = new JuliaVariable{
XSTR(jl_libjulia_handle),
true,
[](LLVMContext &C) { return static_cast<llvm::Type*>(getInt8PtrTy(C)); },
};
static const auto jldlli_var = new JuliaVariable{
XSTR(jl_libjulia_internal_handle),
true,
[](LLVMContext &C) { return static_cast<llvm::Type*>(getInt8PtrTy(C)); },
};
#endif //_OS_WINDOWS_
static const auto jlstack_chk_guard_var = new JuliaVariable{
XSTR(__stack_chk_guard),
true,
get_pjlvalue,
};
static const auto jlgetworld_global = new JuliaVariable{
XSTR(jl_world_counter),
false,
[](LLVMContext &C) { return (Type*)getSizeTy(C); },
};
static const auto jlboxed_int8_cache = new JuliaVariable{
XSTR(jl_boxed_int8_cache),
true,
[](LLVMContext &C) { return (Type*)ArrayType::get(get_pjlvalue(C), 256); },
};
static const auto jlboxed_uint8_cache = new JuliaVariable{
XSTR(jl_boxed_uint8_cache),
true,
[](LLVMContext &C) { return (Type*)ArrayType::get(get_pjlvalue(C), 256); },
};
static const auto jlpgcstack_func = new JuliaFunction{
"julia.get_pgcstack",
[](LLVMContext &C) { return FunctionType::get(PointerType::get(JuliaType::get_ppjlvalue_ty(C), 0), false); },
nullptr,
};
// important functions
// Symbols are not gc-tracked, but we'll treat them as callee rooted anyway,
// because they may come from a gc-rooted location
static const auto jlnew_func = new JuliaFunction{
XSTR(jl_new_structv),
get_func_sig,
get_func_attrs,
};
static const auto jlsplatnew_func = new JuliaFunction{
XSTR(jl_new_structt),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_prjlvalue}, false);
},
get_func_attrs,
};
static const auto jlthrow_func = new JuliaFunction{
XSTR(jl_throw),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlerror_func = new JuliaFunction{
XSTR(jl_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt8PtrTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jlatomicerror_func = new JuliaFunction{
XSTR(jl_atomic_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt8PtrTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jltypeerror_func = new JuliaFunction{
XSTR(jl_type_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt8PtrTy(C), JuliaType::get_prjlvalue_ty(C), PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlundefvarerror_func = new JuliaFunction{
XSTR(jl_undefined_var_error),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
get_attrs_noreturn,
};
static const auto jlboundserrorv_func = new JuliaFunction{
XSTR(jl_bounds_error_ints),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted), getSizePtrTy(C), getSizeTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jlboundserror_func = new JuliaFunction{
XSTR(jl_bounds_error_int),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted), getSizeTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jlvboundserror_func = new JuliaFunction{
XSTR(jl_bounds_error_tuple_int),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{JuliaType::get_pprjlvalue_ty(C), getSizeTy(C), getSizeTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jluboundserror_func = new JuliaFunction{
XSTR(jl_bounds_error_unboxed_int),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(getInt8Ty(C), AddressSpace::Derived), JuliaType::get_pjlvalue_ty(C), getSizeTy(C)}, false); },
get_attrs_noreturn,
};
static const auto jlcheckassign_func = new JuliaFunction{
XSTR(jl_checked_assignment),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{JuliaType::get_pjlvalue_ty(C), PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
nullptr,
};
static const auto jldeclareconst_func = new JuliaFunction{
XSTR(jl_declare_constant),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{JuliaType::get_pjlvalue_ty(C)}, false); },
nullptr,
};
static const auto jlgetbindingorerror_func = new JuliaFunction{
XSTR(jl_get_binding_or_error),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(T_pjlvalue,
{T_pjlvalue, T_pjlvalue}, false);
},
nullptr,
};
static const auto jlboundp_func = new JuliaFunction{
XSTR(jl_boundp),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(getInt32Ty(C),
{T_pjlvalue, T_pjlvalue}, false);
},
nullptr,
};
static const auto jltopeval_func = new JuliaFunction{
XSTR(jl_toplevel_eval),
[](LLVMContext &C) {
auto T_pjlvalue = JuliaType::get_pjlvalue_ty(C);
return FunctionType::get(T_pjlvalue,
{T_pjlvalue, T_pjlvalue}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jlcopyast_func = new JuliaFunction{
XSTR(jl_copy_ast),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
//static const auto jlnsvec_func = new JuliaFunction{
// XSTR(jl_svec),
// [](LLVMContext &C) { return FunctionType::get(T_prjlvalue,
// {getSizeTy(C)}, true); },
// [](LLVMContext &C) { return AttributeList::get(C,
// AttributeSet(),
// Attributes(C, {Attribute::NonNull}),
// None); },
//};
static const auto jlapplygeneric_func = new JuliaFunction{
XSTR(jl_apply_generic),
get_func_sig,
get_func_attrs,
};
static const auto jlinvoke_func = new JuliaFunction{
XSTR(jl_invoke),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue, PointerType::get(T_prjlvalue, 0), getInt32Ty(C), T_prjlvalue}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
{AttributeSet(),
Attributes(C, {Attribute::ReadOnly, Attribute::NoCapture})}); },
};
static const auto jlmethod_func = new JuliaFunction{
XSTR(jl_method_def),
[](LLVMContext &C) {
auto T_jlvalue = JuliaType::get_jlvalue_ty(C);
auto T_pjlvalue = PointerType::get(T_jlvalue, 0);
auto T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue, T_prjlvalue, T_prjlvalue, T_pjlvalue}, false);
},
nullptr,
};
static const auto jlgenericfunction_func = new JuliaFunction{
XSTR(jl_generic_function_def),
[](LLVMContext &C) {
auto T_jlvalue = JuliaType::get_jlvalue_ty(C);
auto T_pjlvalue = PointerType::get(T_jlvalue, 0);
auto T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
auto T_pprjlvalue = PointerType::get(T_prjlvalue, 0);
return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pprjlvalue, T_pjlvalue, T_pjlvalue}, false);
},
nullptr,
};
static const auto jllockvalue_func = new JuliaFunction{
XSTR(jl_lock_value),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
AttributeSet(),
{Attributes(C, {Attribute::NoCapture})}); },
};
static const auto jlunlockvalue_func = new JuliaFunction{
XSTR(jl_unlock_value),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::CalleeRooted)}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
AttributeSet(),
{Attributes(C, {Attribute::NoCapture})}); },
};
static const auto jlenter_func = new JuliaFunction{
XSTR(jl_enter_handler),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt8PtrTy(C)}, false); },
nullptr,
};
static const auto jl_current_exception_func = new JuliaFunction{
XSTR(jl_current_exception),
[](LLVMContext &C) { return FunctionType::get(JuliaType::get_prjlvalue_ty(C), false); },
nullptr,
};
static const auto jlleave_func = new JuliaFunction{
XSTR(jl_pop_handler),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getInt32Ty(C)}, false); },
nullptr,
};
static const auto jl_restore_excstack_func = new JuliaFunction{
XSTR(jl_restore_excstack),
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{getSizeTy(C)}, false); },
nullptr,
};
static const auto jl_excstack_state_func = new JuliaFunction{
XSTR(jl_excstack_state),
[](LLVMContext &C) { return FunctionType::get(getSizeTy(C), false); },
nullptr,
};
static const auto jlegalx_func = new JuliaFunction{
XSTR(jl_egal__unboxed),
[](LLVMContext &C) {
Type *T = PointerType::get(JuliaType::get_jlvalue_ty(C), AddressSpace::Derived);
return FunctionType::get(getInt32Ty(C), {T, T, JuliaType::get_prjlvalue_ty(C)}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoUnwind, Attribute::ArgMemOnly}),
AttributeSet(),
None); },
};
static const auto jl_alloc_obj_func = new JuliaFunction{
"julia.gc_alloc_obj",
[](LLVMContext &C) {
auto T_jlvalue = JuliaType::get_jlvalue_ty(C);
auto T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
auto T_ppjlvalue = PointerType::get(PointerType::get(T_jlvalue, 0), 0);
return FunctionType::get(T_prjlvalue,
{T_ppjlvalue, getSizeTy(C), T_prjlvalue}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet::get(C, makeArrayRef({Attribute::getWithAllocSizeArgs(C, 1, None)})), // returns %1 bytes
Attributes(C, {Attribute::NoAlias, Attribute::NonNull}),
None); },
};
static const auto jl_newbits_func = new JuliaFunction{
XSTR(jl_new_bits),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue, getInt8PtrTy(C)}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
// `julia.typeof` does read memory, but it is effectively readnone before we lower
// the allocation function. This is OK as long as we lower `julia.typeof` no later than
// `julia.gc_alloc_obj`.
static const auto jl_typeof_func = new JuliaFunction{
"julia.typeof",
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadNone, Attribute::NoUnwind, Attribute::NoRecurse}),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jl_loopinfo_marker_func = new JuliaFunction{
"julia.loopinfo_marker",
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C), false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoRecurse, Attribute::InaccessibleMemOnly}),
AttributeSet(),
None); },
};
static const auto jl_write_barrier_func = new JuliaFunction{
"julia.write_barrier",
[](LLVMContext &C) { return FunctionType::get(getVoidTy(C),
{JuliaType::get_prjlvalue_ty(C)}, true); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::NoUnwind, Attribute::NoRecurse, Attribute::InaccessibleMemOnly}),
AttributeSet(),
{Attributes(C, {Attribute::ReadOnly})}); },
};
static const auto jlisa_func = new JuliaFunction{
XSTR(jl_isa),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(getInt32Ty(C),
{T_prjlvalue, T_prjlvalue}, false);
},
nullptr,
};
static const auto jlsubtype_func = new JuliaFunction{
XSTR(jl_subtype),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(getInt32Ty(C),
{T_prjlvalue, T_prjlvalue}, false);
},
nullptr,
};
static const auto jlapplytype_func = new JuliaFunction{
XSTR(jl_instantiate_type_in_env),
[](LLVMContext &C) {
auto T_jlvalue = JuliaType::get_jlvalue_ty(C);
auto T_pjlvalue = PointerType::get(T_jlvalue, 0);
auto T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
auto T_pprjlvalue = PointerType::get(T_prjlvalue, 0);
return FunctionType::get(T_prjlvalue,
{T_pjlvalue, T_pjlvalue, T_pprjlvalue}, false);
},
[](LLVMContext &C) {
return AttributeList::get(C,
AttributeSet(),
AttributeSet::get(C, makeArrayRef({Attribute::get(C, Attribute::NonNull),
Attribute::getWithAlignment(C, Align(16))})),
None);
},
};
static const auto jl_object_id__func = new JuliaFunction{
XSTR(jl_object_id_),
[](LLVMContext &C) { return FunctionType::get(getSizeTy(C),
{JuliaType::get_prjlvalue_ty(C), PointerType::get(getInt8Ty(C), AddressSpace::Derived)}, false); },
nullptr,
};
static const auto setjmp_func = new JuliaFunction{
jl_setjmp_name,
[](LLVMContext &C) { return FunctionType::get(getInt32Ty(C),
{getInt8PtrTy(C),
#ifndef _OS_WINDOWS_
getInt32Ty(C),
#endif
}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReturnsTwice}),
AttributeSet(),
None); },
};
static const auto memcmp_func = new JuliaFunction{
XSTR(memcmp),
[](LLVMContext &C) { return FunctionType::get(getInt32Ty(C),
{getInt8PtrTy(C), getInt8PtrTy(C), getSizeTy(C)}, false); },
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoUnwind, Attribute::ArgMemOnly}),
AttributeSet(),
None); },
// TODO: inferLibFuncAttributes(*memcmp_func, TLI);
};
static const auto jldlsym_func = new JuliaFunction{
XSTR(jl_load_and_lookup),
[](LLVMContext &C) { return FunctionType::get(JuliaType::get_pvoidfunc_ty(C),
{getInt8PtrTy(C), getInt8PtrTy(C), PointerType::get(getInt8PtrTy(C), 0)}, false); },
nullptr,
};
static const auto jllazydlsym_func = new JuliaFunction{
XSTR(jl_lazy_load_and_lookup),
[](LLVMContext &C) { return FunctionType::get(JuliaType::get_pvoidfunc_ty(C),
{JuliaType::get_prjlvalue_ty(C), getInt8PtrTy(C)}, false); },
nullptr,
};
static const auto jltypeassert_func = new JuliaFunction{
XSTR(jl_typeassert),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(getVoidTy(C),
{T_prjlvalue, T_prjlvalue}, false);
},
nullptr,
};
static const auto jlgetnthfieldchecked_func = new JuliaFunction{
XSTR(jl_get_nth_field_checked),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue, getSizeTy(C)}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto jlgetcfunctiontrampoline_func = new JuliaFunction{
XSTR(jl_get_cfunction_trampoline),
[](LLVMContext &C) {
auto T_jlvalue = JuliaType::get_jlvalue_ty(C);
auto T_pjlvalue = PointerType::get(T_jlvalue, 0);
auto T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
auto T_ppjlvalue = PointerType::get(T_pjlvalue, 0);
auto T_pprjlvalue = PointerType::get(T_prjlvalue, 0);
return FunctionType::get(T_prjlvalue,
{
T_prjlvalue, // f (object)
T_pjlvalue, // result
getInt8PtrTy(C), // cache
T_pjlvalue, // fill
FunctionType::get(getInt8PtrTy(C), { getInt8PtrTy(C), T_ppjlvalue }, false)->getPointerTo(), // trampoline
T_pjlvalue, // env
T_pprjlvalue, // vals
}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
AttributeSet(),
Attributes(C, {Attribute::NonNull}),
None); },
};
static const auto diff_gc_total_bytes_func = new JuliaFunction{
XSTR(jl_gc_diff_total_bytes),
[](LLVMContext &C) { return FunctionType::get(getInt64Ty(C), false); },
nullptr,
};
static const auto sync_gc_total_bytes_func = new JuliaFunction{
XSTR(jl_gc_sync_total_bytes),
[](LLVMContext &C) { return FunctionType::get(getInt64Ty(C),
{getInt64Ty(C)}, false); },
nullptr,
};
static const auto jlarray_data_owner_func = new JuliaFunction{
XSTR(jl_array_data_owner),
[](LLVMContext &C) {
auto T_prjlvalue = JuliaType::get_prjlvalue_ty(C);
return FunctionType::get(T_prjlvalue,
{T_prjlvalue}, false);
},
[](LLVMContext &C) { return AttributeList::get(C,
Attributes(C, {Attribute::ReadOnly, Attribute::NoUnwind}),