forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.c
2482 lines (2313 loc) · 87.7 KB
/
dump.c
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: http://julialang.org/license
/*
saving and restoring system images
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "julia.h"
#include "julia_internal.h"
#include "builtin_proto.h"
#ifndef _OS_WINDOWS_
#include <dlfcn.h>
#endif
#ifndef _COMPILER_MICROSOFT_
#include "valgrind.h"
#else
#define RUNNING_ON_VALGRIND 0
#endif
#ifdef __cplusplus
extern "C" {
#endif
JL_DEFINE_MUTEX(dump)
// TODO: put WeakRefs on the weak_refs list during deserialization
// TODO: handle finalizers
// hash of definitions for predefined tagged object
static htable_t ser_tag;
// array of definitions for the predefined tagged object types
// (reverse of ser_tag)
static jl_value_t *deser_tag[256];
// hash of some common symbols, encoded as CommonSym_tag plus 1 byte
static htable_t common_symbol_tag;
static jl_value_t *deser_symbols[256];
// table of all objects that have been deserialized, indexed by pos
// (the order in the serializer stream) in MODE_MODULE, the low
// bit is reserved for flagging certain entries and pos is
// left shift by 1
static htable_t backref_table;
int backref_table_numel;
static arraylist_t backref_list;
// list of (jl_value_t **loc, size_t pos) entries
// for anything that was flagged by the deserializer for later
// type-rewriting of some sort
static arraylist_t flagref_list;
// list of (size_t pos, (void *f)(jl_value_t*)) entries
// for the serializer to mark values in need of rework by function f
// during deserialization later
static arraylist_t reinit_list;
// list of any methtable objects that were deserialized in MODE_MODULE
// and need to be rehashed after assigning the uid fields to types
// (only used in MODE_MODULE and MODE_MODULE_POSTWORK)
static arraylist_t methtable_list;
// list of stuff that is being serialized
// (only used by the incremental serializer in MODE_MODULE)
static jl_array_t *serializer_worklist;
// hash of definitions for predefined function pointers
static htable_t fptr_to_id;
// array of definitions for the predefined function pointers
// (reverse of fptr_to_id)
static const jl_fptr_t id_to_fptrs[] = {
NULL, NULL,
jl_f_throw, jl_f_is, jl_f_typeof, jl_f_issubtype, jl_f_isa,
jl_f_typeassert, jl_f__apply, jl_f_isdefined, jl_f_tuple, jl_f_svec,
jl_f_getfield, jl_f_setfield, jl_f_fieldtype, jl_f_nfields,
jl_f_arrayref, jl_f_arrayset, jl_f_arraysize, jl_f_apply_type,
jl_f_applicable, jl_f_invoke, jl_unprotect_stack, jl_f_sizeof, jl_f__expr,
jl_f_intrinsic_call,
NULL };
// pointers to non-AST-ish objects in a compressed tree
static jl_array_t *tree_literal_values=NULL; // (only used in MODE_AST)
static jl_module_t *tree_enclosing_module=NULL; // (only used in MODE_AST)
static const intptr_t LongSymbol_tag = 23;
static const intptr_t LongSvec_tag = 24;
static const intptr_t LongExpr_tag = 25;
static const intptr_t LiteralVal_tag = 26;
static const intptr_t SmallInt64_tag = 27;
static const intptr_t SmallDataType_tag= 28;
static const intptr_t Int32_tag = 29;
static const intptr_t Array1d_tag = 30;
static const intptr_t Singleton_tag = 31;
static const intptr_t CommonSym_tag = 32;
static const intptr_t NearbyGlobal_tag = 33; // a GlobalRef pointing to tree_enclosing_module
static const intptr_t Null_tag = 253;
static const intptr_t ShortBackRef_tag = 254;
static const intptr_t BackRef_tag = 255;
static intptr_t VALUE_TAGS;
typedef enum _DUMP_MODES {
// not in the serializer at all, or
// something is seriously wrong
MODE_INVALID = 0,
// jl_uncompress_ast
// compressing / decompressing an AST Expr in a LambdaInfo
MODE_AST,
// jl_restore_system_image
// restoring an entire system image from disk
MODE_SYSTEM_IMAGE,
// jl_restore_new_module
// restoring a single module from disk for integration
// into the currently running system image / environment
MODE_MODULE, // first-stage (pre type-uid assignment)
MODE_MODULE_POSTWORK, // second-stage (post type-uid assignment)
} DUMP_MODES;
static DUMP_MODES mode = (DUMP_MODES) 0;
static jl_value_t *jl_idtable_type=NULL;
// queue of types to cache
static jl_array_t *datatype_list=NULL; // (only used in MODE_SYSTEM_IMAGE)
#define write_uint8(s, n) ios_putc((n), (s))
#define read_uint8(s) ((uint8_t)ios_getc(s))
#define write_int8(s, n) write_uint8(s, n)
#define read_int8(s) read_uint8(s)
/* read and write in network (bigendian) order: */
static void write_int32(ios_t *s, int32_t i)
{
write_uint8(s, (i>>24) & 0xff);
write_uint8(s, (i>>16) & 0xff);
write_uint8(s, (i>> 8) & 0xff);
write_uint8(s, i & 0xff);
}
static int32_t read_int32(ios_t *s)
{
int b3 = read_uint8(s);
int b2 = read_uint8(s);
int b1 = read_uint8(s);
int b0 = read_uint8(s);
return b0 | (b1<<8) | (b2<<16) | (b3<<24);
}
static void write_uint64(ios_t *s, uint64_t i)
{
write_int32(s, (i>>32) & 0xffffffff);
write_int32(s, i & 0xffffffff);
}
static uint64_t read_uint64(ios_t *s)
{
uint64_t b1 = (uint32_t)read_int32(s);
uint64_t b0 = (uint32_t)read_int32(s);
return b0 | (b1<<32);
}
static void write_uint16(ios_t *s, uint16_t i)
{
write_uint8(s, (i>> 8) & 0xff);
write_uint8(s, i & 0xff);
}
static uint16_t read_uint16(ios_t *s)
{
int b1 = read_uint8(s);
int b0 = read_uint8(s);
return b0 | (b1<<8);
}
static void writetag(ios_t *s, void *v)
{
write_uint8(s, (uint8_t)(intptr_t)ptrhash_get(&ser_tag, v));
}
static void write_as_tag(ios_t *s, uint8_t tag)
{
if (tag < VALUE_TAGS) {
write_uint8(s, 0);
}
write_uint8(s, tag);
}
static void write_float64(ios_t *s, double x)
{
write_uint64(s, *((uint64_t*)&x));
}
// --- Static Compile ---
#define jl_serialize_value(s, v) jl_serialize_value_(s,(jl_value_t*)(v))
static void jl_serialize_value_(ios_t *s, jl_value_t *v);
static jl_value_t *jl_deserialize_value(ios_t *s, jl_value_t **loc);
static jl_value_t ***sysimg_gvars = NULL;
static void **sysimg_fvars = NULL;
#ifdef HAVE_CPUID
extern void jl_cpuid(int32_t CPUInfo[4], int32_t InfoType);
#endif
extern int globalUnique;
static void *jl_sysimg_handle = NULL;
static uint64_t sysimage_base = 0;
#ifdef _OS_WINDOWS_
#include <dbghelp.h>
#endif
JL_DLLEXPORT int jl_running_on_valgrind(void)
{
return RUNNING_ON_VALGRIND;
}
static int jl_load_sysimg_so(void)
{
#ifndef _OS_WINDOWS_
Dl_info dlinfo;
#endif
// attempt to load the pre-compiled sysimage from jl_sysimg_handle
if (jl_sysimg_handle == 0)
return -1;
int imaging_mode = jl_generating_output() && !jl_options.incremental;
// in --build mode only use sysimg data, not precompiled native code
if (!imaging_mode && jl_options.use_precompiled==JL_OPTIONS_USE_PRECOMPILED_YES) {
sysimg_gvars = (jl_value_t***)jl_dlsym(jl_sysimg_handle, "jl_sysimg_gvars");
sysimg_fvars = (void**)jl_dlsym(jl_sysimg_handle, "jl_sysimg_fvars");
globalUnique = *(size_t*)jl_dlsym(jl_sysimg_handle, "jl_globalUnique");
#ifdef JULIA_ENABLE_THREADING
size_t tls_getter_idx = *(size_t*)jl_dlsym(jl_sysimg_handle,
"jl_ptls_states_getter_idx");
*sysimg_gvars[tls_getter_idx - 1] =
(jl_value_t*)jl_get_ptls_states_getter();
#endif
const char *cpu_target = (const char*)jl_dlsym(jl_sysimg_handle, "jl_sysimg_cpu_target");
if (strcmp(cpu_target,jl_options.cpu_target) != 0)
jl_error("Julia and the system image were compiled for different architectures.\n"
"Please delete or regenerate sys.{so,dll,dylib}.");
#ifdef HAVE_CPUID
uint32_t info[4];
jl_cpuid((int32_t*)info, 1);
if (strcmp(cpu_target, "native") == 0) {
if (!RUNNING_ON_VALGRIND) {
uint64_t saved_cpuid = *(uint64_t*)jl_dlsym(jl_sysimg_handle, "jl_sysimg_cpu_cpuid");
if (saved_cpuid != (((uint64_t)info[2])|(((uint64_t)info[3])<<32)))
jl_error("Target architecture mismatch. Please delete or regenerate sys.{so,dll,dylib}.");
}
}
else if (strcmp(cpu_target,"core2") == 0) {
int HasSSSE3 = (info[2] & 1<<9);
if (!HasSSSE3)
jl_error("The current host does not support SSSE3, but the system image was compiled for Core2.\n"
"Please delete or regenerate sys.{so,dll,dylib}.");
}
#endif
#ifdef _OS_WINDOWS_
sysimage_base = (intptr_t)jl_sysimg_handle;
#else
if (dladdr((void*)sysimg_gvars, &dlinfo) != 0) {
sysimage_base = (intptr_t)dlinfo.dli_fbase;
}
else {
sysimage_base = 0;
}
#endif
}
const char *sysimg_data = (const char*)jl_dlsym_e(jl_sysimg_handle, "jl_system_image_data");
if (sysimg_data) {
size_t len = *(size_t*)jl_dlsym(jl_sysimg_handle, "jl_system_image_size");
jl_restore_system_image_data(sysimg_data, len);
return 0;
}
return -1;
}
static jl_value_t *jl_deserialize_gv(ios_t *s, jl_value_t *v)
{
// Restore the GlobalVariable reference to this jl_value_t via the sysimg_gvars table
int32_t gvname_index = read_int32(s)-1;
if (sysimg_gvars != NULL && gvname_index >= 0 && mode == MODE_SYSTEM_IMAGE) {
*sysimg_gvars[gvname_index] = v;
}
return v;
}
static void jl_serialize_gv(ios_t *s, jl_value_t *v)
{
// write the index of the literal_pointer_val into the system image
write_int32(s, jl_get_llvm_gv(v));
}
static void jl_serialize_globalvals(ios_t *s)
{
size_t i, len = backref_table.size;
void **p = backref_table.table;
for(i=0; i < len; i+=2) {
char *offs = (char*)p[i+1];
if (offs != HT_NOTFOUND) {
uintptr_t pos = offs - (char*)HT_NOTFOUND - 1;
int32_t gv = jl_get_llvm_gv((jl_value_t*)p[i]);
if (gv != 0) {
write_int32(s, pos + 1);
write_int32(s, gv);
}
}
}
write_int32(s, 0);
}
static void jl_deserialize_globalvals(ios_t *s)
{
while (1) {
intptr_t key = read_int32(s);
if (key == 0) break;
jl_deserialize_gv(s, (jl_value_t*)backref_list.items[key - 1]);
}
}
static void jl_serialize_gv_syms(ios_t *s, jl_sym_t *v)
{
// since symbols are static, they might not have had a
// reference anywhere in the code image other than here
void *bp = ptrhash_get(&backref_table, v);
if (bp == HT_NOTFOUND) {
int32_t gv = jl_get_llvm_gv((jl_value_t*)v);
if (gv != 0) {
jl_serialize_value(s, v);
write_int32(s, gv);
}
}
if (v->left) jl_serialize_gv_syms(s, v->left);
if (v->right) jl_serialize_gv_syms(s, v->right);
}
static void jl_serialize_gv_others(ios_t *s)
{
// ensures all objects referenced in the code have
// references in the system image to their global variable
// since codegen knows that some integer boxes are static,
// they might not have had a reference anywhere in the code
// image other than here
int32_t i;
for (i = -512; i < 512; i++) {
jl_value_t *v32 = jl_box_int32(i);
void *bp32 = ptrhash_get(&backref_table, v32);
if (bp32 == HT_NOTFOUND) {
int32_t gv32 = jl_get_llvm_gv(v32);
if (gv32 != 0) {
jl_serialize_value(s, v32);
write_int32(s, gv32);
}
}
}
for (i = -512; i < 512; i++) {
jl_value_t *v64 = jl_box_int64(i);
void *bp64 = ptrhash_get(&backref_table, v64);
if (bp64 == HT_NOTFOUND) {
int32_t gv64 = jl_get_llvm_gv(v64);
if (gv64 != 0) {
jl_serialize_value(s, v64);
write_int32(s, gv64);
}
}
}
jl_serialize_gv_syms(s, jl_get_root_symbol());
jl_serialize_value(s, NULL); // signal the end of this list
}
static void jl_deserialize_gv_others(ios_t *s)
{
while (1) {
jl_value_t *v = jl_deserialize_value(s, NULL);
if (!v) break;
jl_deserialize_gv(s, v);
}
}
static struct delayed_fptrs_t {
jl_lambda_info_t *li;
int32_t func;
int32_t cfunc;
} *delayed_fptrs = NULL;
static size_t delayed_fptrs_n = 0;
static size_t delayed_fptrs_max = 0;
static size_t sysimg_fvars_max = 0;
static void jl_delayed_fptrs(jl_lambda_info_t *li, int32_t func, int32_t cfunc)
{
// can't restore the fptrs until after the system image is fully restored,
// since it will try to decompress the function AST to determine the argument types
if (cfunc || func) {
if (delayed_fptrs_max < delayed_fptrs_n + 1) {
if (delayed_fptrs_max == 0)
// current measurements put the number of functions at 4508
delayed_fptrs_max = 4096;
else
delayed_fptrs_max *= 2;
delayed_fptrs = (struct delayed_fptrs_t*)realloc(delayed_fptrs, delayed_fptrs_max*sizeof(delayed_fptrs[0])); //assumes sizeof==alignof
}
delayed_fptrs[delayed_fptrs_n].li = li;
delayed_fptrs[delayed_fptrs_n].func = func;
delayed_fptrs[delayed_fptrs_n].cfunc = cfunc;
delayed_fptrs_n++;
if (func > 0 && func > sysimg_fvars_max)
sysimg_fvars_max = func;
if (cfunc > 0 && cfunc > sysimg_fvars_max)
sysimg_fvars_max = cfunc;
}
}
void jl_register_fptrs(uint64_t sysimage_base, void **fptrs, jl_lambda_info_t **linfos, size_t n);
static void jl_update_all_fptrs(void)
{
//jl_printf(JL_STDOUT, "delayed_fptrs_n: %d\n", delayed_fptrs_n);
void **fvars = sysimg_fvars;
if (fvars == 0) return;
// jl_fptr_to_llvm needs to decompress some ASTs, therefore this needs to be NULL
// to skip trying to restore GlobalVariable pointers in jl_deserialize_gv
sysimg_gvars = NULL;
sysimg_fvars = NULL;
size_t i;
jl_lambda_info_t **linfos = (jl_lambda_info_t**)malloc(sizeof(jl_lambda_info_t*) * sysimg_fvars_max);
for (i = 0; i < delayed_fptrs_n; i++) {
jl_lambda_info_t *li = delayed_fptrs[i].li;
int32_t func = delayed_fptrs[i].func - 1;
if (func >= 0) {
jl_fptr_to_llvm((jl_fptr_t)fvars[func], li, 0);
linfos[func] = li;
}
int32_t cfunc = delayed_fptrs[i].cfunc - 1;
if (cfunc >= 0) {
jl_fptr_to_llvm((jl_fptr_t)fvars[cfunc], li, 1);
linfos[cfunc] = li;
}
}
jl_register_fptrs(sysimage_base, fvars, linfos, sysimg_fvars_max);
delayed_fptrs_n = 0;
delayed_fptrs_max = 0;
sysimg_fvars_max = 0;
free(delayed_fptrs);
delayed_fptrs = NULL;
}
// --- serialize ---
static void jl_serialize_fptr(ios_t *s, void *fptr)
{
void **pbp = ptrhash_bp(&fptr_to_id, fptr);
if (*pbp == HT_NOTFOUND || fptr == NULL)
write_uint16(s, 1);
else
write_uint16(s, *(intptr_t*)pbp);
}
static int module_in_worklist(jl_module_t *mod) {
int i, l = jl_array_len(serializer_worklist);
for (i = 0; i < l; i++) {
jl_module_t *workmod = (jl_module_t*)jl_cellref(serializer_worklist, i);
if (jl_is_module(workmod) && jl_is_submodule(mod, workmod))
return 1;
}
return 0;
}
static void jl_serialize_datatype(ios_t *s, jl_datatype_t *dt)
{
int tag = 0;
if (mode == MODE_MODULE_POSTWORK) {
if (dt->uid != 0) {
if (dt->name->primary == (jl_value_t*)dt)
tag = 6; // primary type
else
tag = 7; // must use apply_type
}
}
else if (mode == MODE_MODULE) {
int internal = module_in_worklist(dt->name->module);
int i, l = jl_array_len(serializer_worklist);
for (i = 0; i < l; i++) {
jl_module_t *mod = (jl_module_t*)jl_cellref(serializer_worklist, i);
if (jl_is_module(mod) && jl_is_submodule(dt->name->module, mod)) {
internal = 1;
break;
}
}
if (!internal && dt->name->primary == (jl_value_t*)dt) {
tag = 6; // external primary type
}
else if (dt->uid == 0) {
tag = 0; // normal struct
}
else if (!internal && jl_svec_len(dt->parameters) == 0) {
tag = 7; // external type that can be immediately recreated (with apply_type)
}
else {
tag = 5; // anything else (needs uid assigned later)
if (!internal) {
// also flag this in the backref table as special
uintptr_t *bp = (uintptr_t*)ptrhash_bp(&backref_table, dt);
assert(*bp != (uintptr_t)HT_NOTFOUND);
*bp |= 1; assert(((uintptr_t)HT_NOTFOUND)|1);
}
}
}
else if (dt == jl_int32_type)
tag = 2;
else if (dt == jl_bool_type)
tag = 3;
else if (dt == jl_int64_type)
tag = 4;
writetag(s, (jl_value_t*)SmallDataType_tag);
write_uint8(s, 0); // virtual size
jl_serialize_value(s, (jl_value_t*)jl_datatype_type);
write_uint8(s, tag);
if (tag == 6) {
jl_serialize_value(s, dt->name);
return;
}
if (tag == 7) {
jl_serialize_value(s, dt->name);
jl_serialize_value(s, dt->parameters);
return;
}
size_t nf = jl_datatype_nfields(dt);
write_uint16(s, nf);
write_int32(s, dt->size);
int has_instance = !!(dt->instance != NULL);
write_uint8(s, dt->abstract | (dt->mutabl<<1) | (dt->pointerfree<<2) | (has_instance<<3));
write_int8(s, dt->fielddesc_type);
if (!dt->abstract) {
write_uint16(s, dt->ninitialized);
if (mode != MODE_MODULE && mode != MODE_MODULE_POSTWORK) {
write_int32(s, dt->uid);
}
}
if (has_instance)
jl_serialize_value(s, dt->instance);
if (nf > 0) {
write_int32(s, dt->alignment);
write_int8(s, dt->haspadding);
size_t fieldsize = jl_fielddesc_size(dt->fielddesc_type);
ios_write(s, jl_datatype_fields(dt), nf * fieldsize);
jl_serialize_value(s, dt->types);
}
jl_serialize_value(s, dt->parameters);
jl_serialize_value(s, dt->name);
jl_serialize_value(s, dt->super);
}
static void jl_serialize_module(ios_t *s, jl_module_t *m)
{
writetag(s, jl_module_type);
jl_serialize_value(s, m->name);
int ref_only = 0;
if (mode == MODE_MODULE_POSTWORK) {
assert(!module_in_worklist(m));
ref_only = 1;
}
if (mode == MODE_MODULE) {
if (!module_in_worklist(m))
ref_only = 1;
write_int8(s, ref_only);
}
jl_serialize_value(s, m->parent);
if (ref_only) {
assert(m->parent != m);
return;
}
size_t i;
void **table = m->bindings.table;
for(i=1; i < m->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->owner == m || m != jl_main_module) {
jl_serialize_value(s, b->name);
jl_serialize_value(s, b->value);
jl_serialize_value(s, b->globalref);
jl_serialize_value(s, b->owner);
write_int8(s, (b->deprecated<<3) | (b->constp<<2) | (b->exportp<<1) | (b->imported));
jl_serialize_gv(s, (jl_value_t*)b);
}
}
}
jl_serialize_value(s, NULL);
if (m == jl_main_module) {
write_int32(s, 1);
jl_serialize_value(s, (jl_value_t*)jl_core_module);
}
else {
write_int32(s, m->usings.len);
for(i=0; i < m->usings.len; i++) {
jl_serialize_value(s, (jl_value_t*)m->usings.items[i]);
}
}
write_uint8(s, m->istopmod);
write_uint8(s, m->std_imports);
write_uint64(s, m->uuid);
write_int32(s, m->counter);
}
static int is_ast_node(jl_value_t *v)
{
if (jl_is_lambda_info(v)) {
jl_lambda_info_t *li = (jl_lambda_info_t*)v;
if (jl_is_expr(li->ast)) {
li->ast = jl_compress_ast(li, li->ast);
jl_gc_wb(li, li->ast);
}
return 0;
}
return jl_is_symbol(v) || jl_is_symbolnode(v) || jl_is_gensym(v) ||
jl_is_expr(v) || jl_is_newvarnode(v) || jl_is_svec(v) ||
jl_typeis(v, jl_array_any_type) || jl_is_tuple(v) ||
jl_is_uniontype(v) || jl_is_int32(v) || jl_is_int64(v) ||
jl_is_bool(v) ||
jl_is_topnode(v) || jl_is_quotenode(v) || jl_is_gotonode(v) ||
jl_is_labelnode(v) || jl_is_linenode(v) || jl_is_globalref(v);
}
static int literal_val_id(jl_value_t *v)
{
for(int i=0; i < jl_array_len(tree_literal_values); i++) {
if (jl_egal(jl_cellref(tree_literal_values,i), v))
return i;
}
jl_cell_1d_push(tree_literal_values, v);
return jl_array_len(tree_literal_values)-1;
}
static void jl_serialize_value_(ios_t *s, jl_value_t *v)
{
if (v == NULL) {
write_uint8(s, Null_tag);
return;
}
void **bp = ptrhash_bp(&ser_tag, v);
if (*bp != HT_NOTFOUND) {
write_as_tag(s, (uint8_t)(intptr_t)*bp);
return;
}
if (jl_is_symbol(v)) {
void *idx = ptrhash_get(&common_symbol_tag, v);
if (idx != HT_NOTFOUND) {
writetag(s, (jl_value_t*)CommonSym_tag);
write_uint8(s, (uint8_t)(size_t)idx);
return;
}
}
if (mode == MODE_AST) {
// compressing tree
if (!is_ast_node(v)) {
writetag(s, (jl_value_t*)LiteralVal_tag);
int id = literal_val_id(v);
assert(id >= 0 && id < UINT16_MAX);
write_uint16(s, id);
return;
}
}
else {
bp = ptrhash_bp(&backref_table, v);
if (*bp != HT_NOTFOUND) {
uintptr_t pos = (char*)*bp - (char*)HT_NOTFOUND - 1;
if (pos < 65536) {
write_uint8(s, ShortBackRef_tag);
write_uint16(s, pos);
}
else {
write_uint8(s, BackRef_tag);
write_int32(s, pos);
}
return;
}
intptr_t pos = backref_table_numel++;
if (jl_typeof(v) == jl_idtable_type) {
// will need to rehash this, later (after types are fully constructed)
arraylist_push(&reinit_list, (void*)pos);
arraylist_push(&reinit_list, (void*)1);
}
if (mode == MODE_MODULE && jl_is_module(v)) {
jl_module_t *m = (jl_module_t*)v;
if (module_in_worklist(m) && !module_in_worklist(m->parent)) {
// will need to reinsert this into parent bindings, later (in case of any errors during reinsert)
arraylist_push(&reinit_list, (void*)pos);
arraylist_push(&reinit_list, (void*)2);
}
}
if (mode == MODE_MODULE || mode == MODE_MODULE_POSTWORK)
pos <<= 1;
ptrhash_put(&backref_table, v, (char*)HT_NOTFOUND + pos + 1);
}
size_t i;
if (jl_is_svec(v)) {
size_t l = jl_svec_len(v);
if (l <= 255) {
writetag(s, jl_simplevector_type);
write_uint8(s, (uint8_t)l);
}
else {
writetag(s, (jl_value_t*)LongSvec_tag);
write_int32(s, l);
}
for(i=0; i < l; i++) {
jl_serialize_value(s, jl_svecref(v, i));
}
}
else if (jl_is_symbol(v)) {
size_t l = strlen(jl_symbol_name((jl_sym_t*)v));
if (l <= 255) {
writetag(s, jl_symbol_type);
write_uint8(s, (uint8_t)l);
}
else {
writetag(s, (jl_value_t*)LongSymbol_tag);
write_int32(s, l);
}
ios_write(s, jl_symbol_name((jl_sym_t*)v), l);
}
else if (jl_is_globalref(v)) {
if (mode == MODE_AST && jl_globalref_mod(v) == tree_enclosing_module) {
writetag(s, (jl_value_t*)NearbyGlobal_tag);
jl_serialize_value(s, jl_globalref_name(v));
}
else {
writetag(s, (jl_value_t*)jl_globalref_type);
jl_serialize_value(s, jl_globalref_mod(v));
jl_serialize_value(s, jl_globalref_name(v));
}
}
else if (jl_is_array(v)) {
jl_array_t *ar = (jl_array_t*)v;
if (ar->flags.ndims == 1 && ar->elsize < 128) {
writetag(s, (jl_value_t*)Array1d_tag);
write_uint8(s, (ar->flags.ptrarray<<7) | (ar->elsize & 0x7f));
}
else {
writetag(s, (jl_value_t*)jl_array_type);
write_uint16(s, ar->flags.ndims);
write_uint16(s, (ar->flags.ptrarray<<15) | (ar->elsize & 0x7fff));
}
for (i=0; i < ar->flags.ndims; i++)
jl_serialize_value(s, jl_box_long(jl_array_dim(ar,i)));
jl_serialize_value(s, jl_typeof(ar));
if (!ar->flags.ptrarray) {
size_t tot = jl_array_len(ar) * ar->elsize;
ios_write(s, (char*)jl_array_data(ar), tot);
}
else {
for(i=0; i < jl_array_len(ar); i++) {
jl_serialize_value(s, jl_cellref(v, i));
}
}
}
else if (jl_is_expr(v)) {
jl_expr_t *e = (jl_expr_t*)v;
size_t l = jl_array_len(e->args);
if (l <= 255) {
writetag(s, jl_expr_type);
write_uint8(s, (uint8_t)l);
}
else {
writetag(s, (jl_value_t*)LongExpr_tag);
write_int32(s, l);
}
jl_serialize_value(s, e->head);
jl_serialize_value(s, e->etype);
for(i=0; i < l; i++) {
jl_serialize_value(s, jl_exprarg(e, i));
}
}
else if (jl_is_datatype(v)) {
jl_serialize_datatype(s, (jl_datatype_t*)v);
}
else if (jl_is_typevar(v)) {
writetag(s, jl_tvar_type);
jl_serialize_value(s, ((jl_tvar_t*)v)->name);
jl_serialize_value(s, ((jl_tvar_t*)v)->lb);
jl_serialize_value(s, ((jl_tvar_t*)v)->ub);
write_int8(s, ((jl_tvar_t*)v)->bound);
}
else if (jl_is_lambda_info(v)) {
writetag(s, jl_lambda_info_type);
jl_lambda_info_t *li = (jl_lambda_info_t*)v;
jl_serialize_value(s, li->ast);
jl_serialize_value(s, li->rettype);
jl_serialize_value(s, (jl_value_t*)li->sparam_syms);
jl_serialize_value(s, (jl_value_t*)li->sparam_vals);
// don't save cached type info for code in the Core module, because
// it might reference types in the old Base module.
if (li->module == jl_core_module) {
jl_serialize_value(s, jl_nothing);
}
else {
jl_array_t *tf = (jl_array_t*)li->tfunc;
// go through the t-func cache, replacing ASTs with just return
// types for abstract argument types. these ASTs are generally
// not needed (e.g. they don't get inlined).
if (tf && jl_typeis(tf, jl_array_any_type)) {
size_t i, l = jl_array_len(tf);
for(i=0; i < l; i += 3) {
if (!jl_is_leaf_type(jl_cellref(tf,i))) {
jl_value_t *ret = jl_cellref(tf,i+1);
if (jl_is_tuple(ret)) {
jl_value_t *ast = jl_fieldref(ret, 0);
if (jl_is_array(ast) && jl_array_len(ast) > 500)
jl_cellset(tf, i+1, jl_fieldref(ret,1));
}
}
}
}
jl_serialize_value(s, (jl_value_t*)li->tfunc);
}
jl_serialize_value(s, (jl_value_t*)li->name);
jl_serialize_value(s, (jl_value_t*)li->specTypes);
jl_serialize_value(s, (jl_value_t*)li->specializations);
write_int8(s, li->inferred);
write_int8(s, li->pure);
write_int8(s, li->called);
jl_serialize_value(s, (jl_value_t*)li->file);
write_int32(s, li->line);
jl_serialize_value(s, (jl_value_t*)li->module);
jl_serialize_value(s, (jl_value_t*)li->roots);
jl_serialize_value(s, (jl_value_t*)li->def);
jl_serialize_value(s, (jl_value_t*)li->unspecialized);
jl_serialize_fptr(s, li->fptr);
// save functionObject pointers
write_int32(s, li->functionID);
write_int32(s, li->specFunctionID);
if (li->functionID)
write_int8(s, li->jlcall_api);
write_int8(s, li->needs_sparam_vals_ducttape);
}
else if (jl_typeis(v, jl_module_type)) {
jl_serialize_module(s, (jl_module_t*)v);
}
else if (jl_typeis(v, jl_task_type)) {
jl_error("Task cannot be serialized");
}
else {
jl_datatype_t *t = (jl_datatype_t*)jl_typeof(v);
void *data = jl_data_ptr(v);
if (t == jl_int64_type &&
*(int64_t*)data >= S32_MIN && *(int64_t*)data <= S32_MAX) {
writetag(s, (jl_value_t*)SmallInt64_tag);
write_int32(s, (int32_t)*(int64_t*)data);
}
else if (t == jl_int32_type) {
writetag(s, (jl_value_t*)Int32_tag);
write_int32(s, (int32_t)*(int32_t*)data);
}
else {
if (v == t->instance) {
if (mode == MODE_MODULE) {
// also flag this in the backref table as special
uintptr_t *bp = (uintptr_t*)ptrhash_bp(&backref_table, v);
assert(*bp != (uintptr_t)HT_NOTFOUND);
*bp |= 1; assert(((uintptr_t)HT_NOTFOUND)|1);
}
writetag(s, (jl_value_t*)Singleton_tag);
jl_serialize_value(s, t);
return;
}
if (t->size <= 255) {
writetag(s, (jl_value_t*)SmallDataType_tag);
write_uint8(s, t->size);
}
else {
writetag(s, (jl_value_t*)jl_datatype_type);
write_int32(s, t->size);
}
jl_serialize_value(s, t);
if ((mode == MODE_MODULE || mode == MODE_MODULE_POSTWORK) && t == jl_typename_type) {
if (module_in_worklist(((jl_typename_t*)v)->module)) {
write_uint8(s, 0);
}
else {
write_uint8(s, 1);
jl_typename_t *tn = (jl_typename_t*)v;
jl_serialize_value(s, tn->module);
jl_serialize_value(s, tn->name);
return;
}
}
size_t nf = jl_datatype_nfields(t);
if (nf == 0 && jl_datatype_size(t)>0) {
if (t->name == jl_pointer_type->name) {
write_int32(s, 0);
#ifdef _P64
write_int32(s, 0);
#endif
}
else {
ios_write(s, (char*)data, jl_datatype_size(t));
}
}
else {
for(size_t i=0; i < nf; i++) {
if (jl_field_size(t, i) > 0) {
jl_serialize_value(s, jl_get_nth_field(v, i));
}
}
}
}
}
}
static void jl_serialize_methtable_from_mod(ios_t *s, jl_methtable_t *mt, int8_t iskw)
{
jl_sym_t *name = mt->name;
if (iskw) {
if (!mt->kwsorter)
return;
assert(mt->module == jl_gf_mtable(mt->kwsorter)->module);
mt = jl_gf_mtable(mt->kwsorter);
assert(!mt->kwsorter);
}
assert(mt->module);
jl_methlist_t *ml = mt->defs;
while (ml != (void*)jl_nothing) {
if (module_in_worklist(ml->func->module)) {
jl_serialize_value(s, mt->module);
jl_serialize_value(s, name);
write_int8(s, iskw);
jl_serialize_value(s, ml->sig);
jl_serialize_value(s, ml->func);
jl_serialize_value(s, ml->tvars);
write_int8(s, ml->isstaged);
}
ml = ml->next;
}
}
static void jl_serialize_lambdas_from_mod(ios_t *s, jl_module_t *m)
{
if (module_in_worklist(m)) return;
size_t i;
void **table = m->bindings.table;
for(i=1; i < m->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->owner == m && b->value && b->constp) {
if (jl_is_module(b->value)) {
jl_module_t *child = (jl_module_t*)b->value;
if (child != m && child->parent == m && child->name == b->name) {
// this is the original/primary binding for the submodule
jl_serialize_lambdas_from_mod(s, (jl_module_t*)b->value);
}
}
else {
jl_methtable_t *mt = jl_gf_mtable(b->value);
if (mt && mt->name == b->name && mt->module == m) {
jl_serialize_methtable_from_mod(s, mt, 0);
jl_serialize_methtable_from_mod(s, mt, 1);
}
}
}
}
}
}
// serialize information about all of the modules accessible directly from Main
static void jl_serialize_mod_list(ios_t *s)
{
jl_module_t *m = jl_main_module;
size_t i;
void **table = m->bindings.table;
for(i=1; i < m->bindings.size; i+=2) {
if (table[i] != HT_NOTFOUND) {
jl_binding_t *b = (jl_binding_t*)table[i];
if (b->owner == m &&
b->value && b->constp &&
jl_is_module(b->value) &&
!module_in_worklist((jl_module_t*)b->value)) {
jl_module_t *child = (jl_module_t*)b->value;
if (child->name == b->name) {
// this is the original/primary binding for the submodule
size_t l = strlen(jl_symbol_name(child->name));
write_int32(s, l);
ios_write(s, jl_symbol_name(child->name), l);
write_uint64(s, child->uuid);
}
}
}
}
write_int32(s, 0);
}