-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathdump.c
2798 lines (2625 loc) · 103 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: https://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
// 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
// (not used in MODE_AST)
static htable_t backref_table;
static 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
// (not used in MODE_AST)
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
// (not used in MODE_AST)
static arraylist_t reinit_list;
// list of stuff that is being serialized
// (only used by the incremental serializer in MODE_MODULE)
static jl_array_t *serializer_worklist;
// inverse of backedges tree
// (only used by the incremental serializer in MODE_MODULE)
htable_t edges_map;
// list of modules being deserialized with __init__ methods
// (not used in MODE_AST)
extern jl_array_t *jl_module_init_order;
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 MethodInstance
MODE_AST,
// jl_restore_new_module
// restoring a single module from disk for integration
// into the currently running system image / environment
MODE_MODULE
} DUMP_MODES;
typedef struct {
ios_t *s;
DUMP_MODES mode;
// pointers to non-AST-ish objects in a compressed tree
// (only used in MODE_AST)
jl_array_t *tree_literal_values;
jl_module_t *tree_enclosing_module;
jl_ptls_t ptls;
} jl_serializer_state;
static jl_value_t *jl_idtable_type = NULL;
static arraylist_t builtin_typenames;
// mark symbols for gen_sysimg_symtab.jl
//#define GEN_SYMTAB_MODE
#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), 0)
static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_literal);
static jl_value_t *jl_deserialize_value(jl_serializer_state *s, jl_value_t **loc);
// --- serialize ---
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_array_ptr_ref(serializer_worklist, i);
if (jl_is_module(workmod) && jl_is_submodule(mod, workmod))
return 1;
}
return 0;
}
// compute whether a type references something internal to worklist
// and thus could not have existed before deserialize
// and thus does not need delayed unique-ing
static int type_in_worklist(jl_datatype_t *dt)
{
if (module_in_worklist(dt->name->module))
return 1;
int i, l = jl_svec_len(dt->parameters);
for (i = 0; i < l; i++) {
jl_value_t *p = jl_unwrap_unionall(jl_tparam(dt, i));
if (type_in_worklist((jl_datatype_t*)(jl_is_datatype(p) ? p : jl_typeof(p))))
return 1;
}
return 0;
}
static int type_recursively_external(jl_datatype_t *dt);
static int type_parameter_recursively_external(jl_value_t *p0)
{
jl_datatype_t *p = (jl_datatype_t*)p0;
while (jl_is_unionall(p)) {
if (!type_parameter_recursively_external(((jl_unionall_t*)p)->var->lb))
return 0;
if (!type_parameter_recursively_external(((jl_unionall_t*)p)->var->ub))
return 0;
p = (jl_datatype_t*)((jl_unionall_t*)p)->body;
}
if (!jl_is_datatype(p) || p->uid == 0)
return 0;
if (module_in_worklist(p->name->module))
return 0;
if (p->name->wrapper != (jl_value_t*)p0) {
if (!type_recursively_external(p))
return 0;
}
return 1;
}
// returns true if all of the parameters are tag 6 or 7
static int type_recursively_external(jl_datatype_t *dt)
{
if (dt->uid == 0)
return 0;
if (jl_svec_len(dt->parameters) == 0)
return 1;
int i, l = jl_svec_len(dt->parameters);
for (i = 0; i < l; i++) {
if (!type_parameter_recursively_external(jl_tparam(dt, i)))
return 0;
}
return 1;
}
static void jl_serialize_datatype(jl_serializer_state *s, jl_datatype_t *dt)
{
int tag = 0;
int internal = module_in_worklist(dt->name->module);
if (!internal && jl_unwrap_unionall(dt->name->wrapper) == (jl_value_t*)dt) {
tag = 6; // external primary type
}
else if (dt->uid == 0) {
tag = 0; // normal struct
}
else if (internal) {
if (jl_unwrap_unionall(dt->name->wrapper) == (jl_value_t*)dt) // comes up often since functions create types
tag = 5; // internal, and not in the typename cache (just needs uid reassigned)
else
tag = 10; // anything else that's internal (just needs uid reassigned and possibly recaching)
}
else if (type_recursively_external(dt)) {
tag = 7; // external type that can be immediately recreated (with apply_type)
}
else if (type_in_worklist(dt)) {
tag = 10; // external, but definitely new (still needs uid and caching, but not full unique-ing)
}
else {
// this'll need a uid and unique-ing later
// 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;
tag = 10;
}
if (strncmp(jl_symbol_name(dt->name->name), "#kw#", 4) == 0) {
/* XXX: yuck, but the auto-generated kw types from the serializer isn't a real type, so we *must* be very careful */
assert(tag == 0 || tag == 5 || tag == 6 || tag == 10);
if (tag == 6) {
jl_methtable_t *mt = dt->name->mt;
jl_datatype_t *primarydt = (jl_datatype_t*)jl_unwrap_unionall(jl_get_global(mt->module, mt->name));
assert(jl_is_datatype(primarydt));
assert(jl_typeof(primarydt->name->mt->kwsorter) == (jl_value_t*)dt);
dt = primarydt;
tag = 9;
}
}
writetag(s->s, (jl_value_t*)SmallDataType_tag);
write_uint8(s->s, 0); // virtual size
jl_serialize_value(s, (jl_value_t*)jl_datatype_type);
write_uint8(s->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;
}
if (tag == 9) {
jl_serialize_value(s, dt);
return;
}
write_int32(s->s, dt->size);
int has_instance = (dt->instance != NULL);
int has_layout = (dt->layout != NULL);
write_uint8(s->s, dt->abstract | (dt->mutabl<<1) | (has_layout<<2) | (has_instance<<3) |
(dt->hasfreetypevars<<4) | (dt->isleaftype<<5));
write_int32(s->s, dt->depth);
if (!dt->abstract) {
write_uint16(s->s, dt->ninitialized);
}
if (has_layout) {
uint8_t layout = 0;
if (dt->layout == ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_array_type))->layout) {
layout = 1;
}
else if (dt->layout == jl_void_type->layout) {
layout = 2;
}
else if (dt->layout == ((jl_datatype_t*)jl_unwrap_unionall((jl_value_t*)jl_pointer_type))->layout) {
layout = 3;
}
write_uint8(s->s, layout);
if (layout == 0) {
uint32_t nf = dt->layout->nfields;
write_int32(s->s, nf);
uint32_t alignment = ((uint32_t*)dt->layout)[1];
write_int32(s->s, alignment);
if (dt->layout->npointers && nf)
write_int32(s->s, ((uint32_t*)dt->layout)[-1]);
size_t fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
ios_write(s->s, (char*)(&dt->layout[1]), nf * fieldsize);
}
}
if (has_instance)
jl_serialize_value(s, dt->instance);
jl_serialize_value(s, dt->name);
jl_serialize_value(s, dt->parameters);
jl_serialize_value(s, dt->super);
jl_serialize_value(s, dt->types);
}
static void jl_serialize_module(jl_serializer_state *s, jl_module_t *m)
{
writetag(s->s, jl_module_type);
jl_serialize_value(s, m->name);
int ref_only = 0;
if (!module_in_worklist(m))
ref_only = 1;
write_int8(s->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->s, (b->deprecated<<3) | (b->constp<<2) | (b->exportp<<1) | (b->imported));
}
}
}
jl_serialize_value(s, NULL);
if (m == jl_main_module) {
write_int32(s->s, 1);
jl_serialize_value(s, (jl_value_t*)jl_core_module);
}
else {
write_int32(s->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->s, m->istopmod);
write_uint64(s->s, m->uuid);
write_int32(s->s, m->counter);
}
static int is_ast_node(jl_value_t *v)
{
// TODO: this accidentally copies QuoteNode(Expr(...)) and QuoteNode(svec(...))
return jl_is_symbol(v) || jl_is_slot(v) || jl_is_ssavalue(v) ||
jl_is_uniontype(v) || jl_is_expr(v) || jl_is_newvarnode(v) ||
jl_is_svec(v) || jl_is_tuple(v) || ((jl_datatype_t*)jl_typeof(v))->instance ||
jl_is_int32(v) || jl_is_int64(v) || jl_is_bool(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_serializer_state *s, jl_value_t *v)
{
int i, l = jl_array_len(s->tree_literal_values);
for (i = 0; i < l; i++) {
if (jl_egal(jl_array_ptr_ref(s->tree_literal_values, i), v))
return i;
}
jl_array_ptr_1d_push(s->tree_literal_values, v);
return jl_array_len(s->tree_literal_values) - 1;
}
static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_literal)
{
if (v == NULL) {
write_uint8(s->s, Null_tag);
return;
}
void **bp = ptrhash_bp(&ser_tag, v);
if (*bp != HT_NOTFOUND) {
write_as_tag(s->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->s, (jl_value_t*)CommonSym_tag);
write_uint8(s->s, (uint8_t)(size_t)idx);
return;
}
}
if (s->mode == MODE_AST) {
// compressing tree
if (!as_literal && !is_ast_node(v)) {
writetag(s->s, (jl_value_t*)LiteralVal_tag);
int id = literal_val_id(s, v);
assert(id >= 0 && id < UINT16_MAX);
write_uint16(s->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->s, ShortBackRef_tag);
write_uint16(s->s, pos);
}
else {
write_uint8(s->s, BackRef_tag);
write_int32(s->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 (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);
}
}
// TypeMapLevels need to be rehashed
if (jl_is_mtable(v)) {
arraylist_push(&reinit_list, (void*)pos);
arraylist_push(&reinit_list, (void*)3);
}
if (jl_is_method(v) && jl_typeof(((jl_method_t*)v)->specializations.unknown) == (jl_value_t*)jl_typemap_level_type) {
arraylist_push(&reinit_list, (void*)pos);
arraylist_push(&reinit_list, (void*)4);
}
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->s, jl_simplevector_type);
write_uint8(s->s, (uint8_t)l);
}
else {
writetag(s->s, (jl_value_t*)LongSvec_tag);
write_int32(s->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->s, jl_symbol_type);
write_uint8(s->s, (uint8_t)l);
}
else {
writetag(s->s, (jl_value_t*)LongSymbol_tag);
write_int32(s->s, l);
}
#ifdef GEN_SYMTAB_MODE
write_uint8(s->s, 0);
ios_write(s->s, "JJJ", 3);
#endif
ios_write(s->s, jl_symbol_name((jl_sym_t*)v), l);
#ifdef GEN_SYMTAB_MODE
write_uint8(s->s, 0);
#endif
}
else if (jl_is_globalref(v)) {
if (s->mode == MODE_AST && jl_globalref_mod(v) == s->tree_enclosing_module) {
writetag(s->s, (jl_value_t*)NearbyGlobal_tag);
jl_serialize_value(s, jl_globalref_name(v));
}
else {
writetag(s->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_ssavalue(v) && ((jl_ssavalue_t*)v)->id < 65536) {
writetag(s->s, (jl_value_t*)jl_ssavalue_type);
write_uint16(s->s, ((jl_ssavalue_t*)v)->id);
}
else if (jl_typeis(v,jl_slotnumber_type) && jl_slot_number(v) < 65536) {
writetag(s->s, (jl_value_t*)jl_slotnumber_type);
write_uint16(s->s, jl_slot_number(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->s, (jl_value_t*)Array1d_tag);
write_uint8(s->s, (ar->flags.ptrarray<<7) | (ar->elsize & 0x7f));
}
else {
writetag(s->s, (jl_value_t*)jl_array_type);
write_uint16(s->s, ar->flags.ndims);
write_uint16(s->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 extra = jl_is_uniontype(jl_tparam0(jl_typeof(ar))) ? jl_array_len(ar) : 0;
size_t tot = jl_array_len(ar) * ar->elsize + extra;
ios_write(s->s, (char*)jl_array_data(ar), tot);
}
else {
for(i=0; i < jl_array_len(ar); i++) {
jl_serialize_value(s, jl_array_ptr_ref(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->s, jl_expr_type);
write_uint8(s->s, (uint8_t)l);
}
else {
writetag(s->s, (jl_value_t*)LongExpr_tag);
write_int32(s->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->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);
}
else if (jl_is_method(v)) {
writetag(s->s, jl_method_type);
jl_method_t *m = (jl_method_t*)v;
int internal = 1;
int external_mt = 0;
internal = module_in_worklist(m->module);
if (!internal) {
// 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;
}
jl_serialize_value(s, (jl_value_t*)m->sig);
write_uint8(s->s, internal);
if (!internal)
return;
jl_datatype_t *gf = jl_first_argument_datatype((jl_value_t*)m->sig);
assert(jl_is_datatype(gf) && gf->name->mt);
external_mt = !module_in_worklist(gf->name->mt->module);
union jl_typemap_t *tf = &m->specializations;
jl_serialize_value(s, tf->unknown);
jl_serialize_value(s, (jl_value_t*)m->name);
jl_serialize_value(s, (jl_value_t*)m->file);
write_int32(s->s, m->line);
if (external_mt)
jl_serialize_value(s, jl_nothing);
else
jl_serialize_value(s, (jl_value_t*)m->ambig);
write_int8(s->s, m->called);
write_int32(s->s, m->nargs);
write_int8(s->s, m->isva);
write_int8(s->s, m->pure);
jl_serialize_value(s, (jl_value_t*)m->module);
jl_serialize_value(s, (jl_value_t*)m->sparam_syms);
jl_serialize_value(s, (jl_value_t*)m->roots);
jl_serialize_value(s, (jl_value_t*)m->source);
jl_serialize_value(s, (jl_value_t*)m->unspecialized);
jl_serialize_value(s, (jl_value_t*)m->generator);
jl_serialize_value(s, (jl_value_t*)m->invokes.unknown);
}
else if (jl_is_method_instance(v)) {
writetag(s->s, jl_method_instance_type);
jl_method_instance_t *li = (jl_method_instance_t*)v;
int internal = 0;
if (li->max_world == 0 && li->min_world == 0) {
internal = 1; // not world-tracked
}
else if (!jl_is_method(li->def.method) || module_in_worklist(li->def.method->module)) {
if (li->max_world == ~(size_t)0) {
internal = 2; // update world on deserialization
}
else {
internal = 3; // garbage object :(
}
}
if (!internal) {
// 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;
}
jl_serialize_value(s, (jl_value_t*)li->specTypes);
if (!internal)
jl_serialize_value(s, (jl_value_t*)li->def.method->sig);
else
jl_serialize_value(s, li->def.value);
write_uint8(s->s, internal);
if (!internal)
return;
jl_serialize_value(s, li->inferred);
jl_serialize_value(s, li->inferred_const);
jl_serialize_value(s, li->rettype);
jl_serialize_value(s, (jl_value_t*)li->sparam_vals);
jl_array_t *backedges = li->backedges;
if (s->mode == MODE_MODULE && backedges) {
// filter backedges to only contain pointers
// to items that we will actually store (internal == 2)
size_t ins, i, l = jl_array_len(backedges);
jl_method_instance_t **b_edges = (jl_method_instance_t**)jl_array_data(backedges);
for (ins = i = 0; i < l; i++) {
jl_method_instance_t *backedge = b_edges[i];
if (module_in_worklist(backedge->def.method->module)) {
b_edges[ins++] = backedge;
}
}
if (ins != l)
jl_array_del_end(backedges, l - ins);
if (ins == 0)
backedges = NULL;
}
jl_serialize_value(s, (jl_value_t*)backedges);
write_uint8(s->s, li->jlcall_api == 2 ? 2 : 0);
}
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 if (jl_typeis(v, jl_string_type)) {
writetag(s->s, jl_string_type);
write_int32(s->s, jl_string_len(v));
ios_write(s->s, jl_string_data(v), jl_string_len(v));
}
else if (jl_typeis(v, jl_typemap_entry_type)) {
writetag(s->s, jl_typemap_entry_type);
size_t n = 0;
jl_typemap_entry_t *te = (jl_typemap_entry_t*)v;
while ((jl_value_t*)te != jl_nothing) {
n++; te = te->next;
}
write_int32(s->s, n);
te = (jl_typemap_entry_t*)v;
size_t i, nf = jl_datatype_nfields(jl_typemap_entry_type);
while ((jl_value_t*)te != jl_nothing) {
for (i = 1; i < nf; i++) {
if (jl_field_size(jl_typemap_entry_type, i) > 0) {
jl_serialize_value(s, jl_get_nth_field((jl_value_t*)te, i));
if (!jl_field_isptr(jl_typemap_entry_type, i))
write_int8(s->s, 0);
}
}
te = te->next;
}
}
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->s, (jl_value_t*)SmallInt64_tag);
write_int32(s->s, (int32_t)*(int64_t*)data);
}
else if (t == jl_int32_type) {
writetag(s->s, (jl_value_t*)Int32_tag);
write_int32(s->s, (int32_t)*(int32_t*)data);
}
else {
if (v == t->instance) {
if (s->mode == MODE_MODULE && !type_in_worklist(t)) {
// also flag this in the backref table as special
// if it might not be unique (is external)
uintptr_t *bp = (uintptr_t*)ptrhash_bp(&backref_table, v);
assert(*bp != (uintptr_t)HT_NOTFOUND);
*bp |= 1;
}
writetag(s->s, (jl_value_t*)Singleton_tag);
jl_serialize_value(s, t);
return;
}
assert(!t->instance && "detected singleton construction corruption");
if (t->size <= 255) {
writetag(s->s, (jl_value_t*)SmallDataType_tag);
write_uint8(s->s, t->size);
}
else {
writetag(s->s, (jl_value_t*)jl_datatype_type);
write_int32(s->s, t->size);
}
jl_serialize_value(s, t);
if (t == jl_typename_type) {
if (module_in_worklist(((jl_typename_t*)v)->module)) {
write_uint8(s->s, 0);
}
else {
write_uint8(s->s, 1);
jl_typename_t *tn = (jl_typename_t*)v;
jl_serialize_value(s, tn->module);
jl_serialize_value(s, tn->name);
return;
}
}
if (t == jl_unionall_type) {
jl_datatype_t *d = (jl_datatype_t*)jl_unwrap_unionall(v);
if (jl_is_datatype(d) && d->name->wrapper == v &&
!module_in_worklist(d->name->module)) {
write_uint8(s->s, 1);
jl_serialize_value(s, d->name->module);
jl_serialize_value(s, d->name->name);
return;
}
else {
write_uint8(s->s, 0);
}
}
if (t == jl_typemap_level_type) {
// perform some compression on the typemap levels
// (which will need to be rehashed during deserialization anyhow)
jl_typemap_level_t *node = (jl_typemap_level_t*)v;
assert( // make sure this type has the expected ordering
offsetof(jl_typemap_level_t, arg1) == 0 * sizeof(jl_value_t*) &&
offsetof(jl_typemap_level_t, targ) == 2 * sizeof(jl_value_t*) &&
offsetof(jl_typemap_level_t, linear) == 4 * sizeof(jl_value_t*) &&
offsetof(jl_typemap_level_t, any) == 5 * sizeof(jl_value_t*) &&
offsetof(jl_typemap_level_t, key) == 6 * sizeof(jl_value_t*) &&
sizeof(jl_typemap_level_t) == 7 * sizeof(jl_value_t*));
jl_serialize_value(s, jl_nothing);
jl_serialize_value(s, node->arg1.values);
jl_serialize_value(s, jl_nothing);
jl_serialize_value(s, node->targ.values);
jl_serialize_value(s, node->linear);
jl_serialize_value(s, node->any.unknown);
jl_serialize_value(s, node->key);
return;
}
size_t nf = jl_datatype_nfields(t);
if (nf == 0 && jl_datatype_size(t) > 0) {
if (t->name == jl_pointer_typename && jl_unbox_voidpointer(v) != (void*)-1) {
// normalize most pointers to NULL, to help catch memory errors
// but permit MAP_FAILED / INVALID_HANDLE to be stored unchanged
write_int32(s->s, 0);
#ifdef _P64
write_int32(s->s, 0);
#endif
}
else {
ios_write(s->s, (char*)data, jl_datatype_size(t));
}
}
else {
size_t i;
for (i = 0; i < nf; i++) {
size_t offs = jl_field_offset(t, i);
size_t fsz = jl_field_size(t, i);
if (fsz > 0) {
jl_serialize_value(s, jl_get_nth_field(v, i));
if (!jl_field_isptr(t, i)) {
uint8_t sel = 0;
if (jl_is_uniontype(jl_field_type(t, i))) {
sel = ((uint8_t*)v)[offs + fsz - 1];
}
write_int8(s->s, sel);
}
}
}
}
}
}
}
static void jl_collect_missing_backedges_to_mod(jl_methtable_t *mt)
{
jl_array_t *backedges = mt->backedges;
if (backedges) {
size_t i, l = jl_array_len(backedges);
for (i = 1; i < l; i += 2) {
jl_method_instance_t *caller = (jl_method_instance_t*)jl_array_ptr_ref(backedges, i);
if (caller->max_world == ~(size_t)0) {
jl_value_t *missing_callee = jl_array_ptr_ref(backedges, i - 1);
jl_array_t **edges = (jl_array_t**)ptrhash_bp(&edges_map, (void*)caller);
if (*edges == HT_NOTFOUND)
*edges = jl_alloc_vec_any(0);
jl_array_ptr_1d_push(*edges, missing_callee);
}
}
}
}
// the intent of this function is to invert the backedges tree
// for anything that points to a method not part of the worklist
static void collect_backedges(jl_method_instance_t *callee)
{
jl_array_t *backedges = callee->backedges;
if (backedges) {
assert(callee->max_world == ~(size_t)0);
size_t i, l = jl_array_len(backedges);
for (i = 0; i < l; i++) {
jl_method_instance_t *caller = (jl_method_instance_t*)jl_array_ptr_ref(backedges, i);
if (caller->max_world == ~(size_t)0) {
jl_array_t **edges = (jl_array_t**)ptrhash_bp(&edges_map, caller);
if (*edges == HT_NOTFOUND)
*edges = jl_alloc_vec_any(0);
jl_array_ptr_1d_push(*edges, (jl_value_t*)callee);
}
}
}
}
static int jl_collect_backedges_to_mod(jl_typemap_entry_t *ml, void *closure)
{
(void)(jl_array_t*)closure;
jl_method_instance_t *callee = ml->func.linfo;
collect_backedges(callee);
return 1;
}
static int jl_collect_methcache_from_mod(jl_typemap_entry_t *ml, void *closure)
{
jl_array_t *s = (jl_array_t*)closure;
jl_method_t *m = ml->func.method;
if (module_in_worklist(m->module)) {
jl_array_ptr_1d_push(s, (jl_value_t*)m);
jl_array_ptr_1d_push(s, (jl_value_t*)ml->simplesig);
}
else {
jl_typemap_visitor(m->specializations, jl_collect_backedges_to_mod, closure);
}
return 1;
}
static void jl_collect_methtable_from_mod(jl_array_t *s, jl_typename_t *tn)
{
jl_typemap_visitor(tn->mt->defs, jl_collect_methcache_from_mod, (void*)s);
}
static void jl_collect_lambdas_from_mod(jl_array_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) {
jl_value_t *bv = jl_unwrap_unionall(b->value);
if (jl_is_datatype(bv)) {
jl_typename_t *tn = ((jl_datatype_t*)bv)->name;
if (tn->module == m && tn->name == b->name && tn->wrapper == b->value) {
jl_methtable_t *mt = tn->mt;
if (mt != NULL &&
(jl_value_t*)mt != jl_nothing &&
(mt != jl_type_type_mt || tn == jl_type_typename)) {
jl_collect_methtable_from_mod(s, tn);
jl_collect_missing_backedges_to_mod(mt);
}
}
}
else 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_collect_lambdas_from_mod(s, (jl_module_t*)b->value);
}
}
}
}
}
}
// flatten the backedge map reachable from caller into callees
static void jl_collect_backedges_to(jl_method_instance_t *caller, jl_array_t *direct_callees, arraylist_t *to_restore)
{
jl_array_t **pcallees = (jl_array_t**)ptrhash_bp(&edges_map, (void*)caller),
*callees = *pcallees;
if (callees != HT_NOTFOUND) {
arraylist_push(to_restore, (void*)callees);
arraylist_push(to_restore, (void*)pcallees);
*pcallees = (jl_array_t*) HT_NOTFOUND;
jl_array_ptr_1d_append(direct_callees, callees);
size_t i, l = jl_array_len(callees);
for (i = 0; i < l; i++) {
jl_value_t *c = jl_array_ptr_ref(callees, i);
if (jl_is_method_instance(c)) {
jl_collect_backedges_to((jl_method_instance_t*)c, direct_callees, to_restore);
}
}
}
}
static void jl_collect_backedges(jl_array_t *s)
{
arraylist_t to_restore;
arraylist_new(&to_restore, 0);
size_t i;
void **table = edges_map.table;
for (i = 0; i < edges_map.size; i += 2) {
jl_method_instance_t *caller = (jl_method_instance_t*)table[i];
jl_array_t *callees = (jl_array_t*)table[i + 1];
if (callees != HT_NOTFOUND && module_in_worklist(caller->def.method->module)) {
size_t i, l = jl_array_len(callees); // length may change during iteration
for (i = 0; i < l; i++) { // only consider the initial list
jl_value_t *c = jl_array_ptr_ref(callees, i);
if (jl_is_method_instance(c)) {
jl_collect_backedges_to((jl_method_instance_t*)c, callees, &to_restore);
}
}
jl_array_ptr_1d_push(s, (jl_value_t*)caller);
jl_array_ptr_1d_push(s, (jl_value_t*)callees);
while (to_restore.len) {
void **pp = (void**)arraylist_pop(&to_restore);
*pp = arraylist_pop(&to_restore);
}
}
}
}
// serialize information about all of the modules accessible directly from Main
static void write_mod_list(ios_t *s)
{
jl_module_t *m = jl_main_module;