forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.c
3409 lines (3218 loc) · 126 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 "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
#include "julia_assert.h"
#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_IR)
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_IR)
static arraylist_t flagref_list;
static htable_t uniquing_table;
// 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_IR)
static arraylist_t reinit_list;
// list of stuff that is being serialized
// (only used by the incremental serializer in MODE_MODULE)
// This is not quite globally rooted, but we take care to only
// ever assigned rooted values here.
static jl_array_t *serializer_worklist JL_GLOBALLY_ROOTED;
// inverse of backedges tree
// (only used by the incremental serializer in MODE_MODULE)
htable_t edges_map;
#define TAG_SYMBOL 2
#define TAG_SSAVALUE 3
#define TAG_DATATYPE 4
#define TAG_SLOTNUMBER 5
#define TAG_SVEC 6
#define TAG_ARRAY 7
#define TAG_NULL 8
#define TAG_EXPR 9
#define TAG_PHINODE 10
#define TAG_PHICNODE 11
#define TAG_LONG_SYMBOL 12
#define TAG_LONG_SVEC 13
#define TAG_LONG_EXPR 14
#define TAG_LONG_PHINODE 15
#define TAG_LONG_PHICNODE 16
#define TAG_METHODROOT 17
#define TAG_STRING 18
#define TAG_SHORT_INT64 19
#define TAG_SHORT_GENERAL 20
#define TAG_CNULL 21
#define TAG_ARRAY1D 22
#define TAG_SINGLETON 23
#define TAG_MODULE 24
#define TAG_TVAR 25
#define TAG_METHOD_INSTANCE 26
#define TAG_METHOD 27
#define TAG_CODE_INSTANCE 28
#define TAG_COMMONSYM 29
#define TAG_NEARBYGLOBAL 30
#define TAG_GLOBALREF 31
#define TAG_CORE 32
#define TAG_BASE 33
#define TAG_BITYPENAME 34
#define TAG_NEARBYMODULE 35
#define TAG_INT32 36
#define TAG_INT64 37
#define TAG_UINT8 38
#define TAG_VECTORTY 39
#define TAG_PTRTY 40
#define TAG_LONG_SSAVALUE 41
#define TAG_LONG_METHODROOT 42
#define TAG_SHORTER_INT64 43
#define TAG_SHORT_INT32 44
#define TAG_CALL1 45
#define TAG_CALL2 46
#define TAG_LINEINFO 47
#define TAG_SHORT_BACKREF 48
#define TAG_BACKREF 49
#define TAG_UNIONALL 50
#define TAG_GOTONODE 51
#define TAG_QUOTENODE 52
#define TAG_GENERAL 53
#define LAST_TAG 53
typedef enum _DUMP_MODES {
// not in the serializer at all, or
// something is seriously wrong
MODE_INVALID = 0,
// compressing / decompressing a CodeInfo
MODE_IR,
// 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;
// method we're compressing for in MODE_IR
jl_method_t *method;
jl_ptls_t ptls;
jl_array_t *loaded_modules_array;
} jl_serializer_state;
static jl_value_t *jl_idtable_type = NULL;
static jl_typename_t *jl_idtable_typename = NULL;
static jl_value_t *jl_bigint_type = NULL;
static int gmp_limb_size = 0;
static arraylist_t builtin_typenames;
#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 host byte order */
static void write_int32(ios_t *s, int32_t i) JL_NOTSAFEPOINT
{
ios_write(s, (char*)&i, 4);
}
static int32_t read_int32(ios_t *s) JL_NOTSAFEPOINT
{
int32_t x = 0;
ios_read(s, (char*)&x, 4);
return x;
}
static void write_uint64(ios_t *s, uint64_t i) JL_NOTSAFEPOINT
{
ios_write(s, (char*)&i, 8);
}
static uint64_t read_uint64(ios_t *s) JL_NOTSAFEPOINT
{
uint64_t x = 0;
ios_read(s, (char*)&x, 8);
return x;
}
static void write_int64(ios_t *s, int64_t i) JL_NOTSAFEPOINT
{
ios_write(s, (char*)&i, 8);
}
static void write_uint16(ios_t *s, uint16_t i) JL_NOTSAFEPOINT
{
ios_write(s, (char*)&i, 2);
}
static uint16_t read_uint16(ios_t *s) JL_NOTSAFEPOINT
{
int16_t x = 0;
ios_read(s, (char*)&x, 2);
return x;
}
static void write_float64(ios_t *s, double x) JL_NOTSAFEPOINT
{
write_uint64(s, *((uint64_t*)&x));
}
//#ifdef _P64
//#define write_ulong(s, x) (write_uint64((s), (x)))
//#define read_ulong(s, x) (read_uint64((s), (x)))
//#else
//#define write_ulong(s, x) (write_uint32((s), (x)))
//#define read_ulong(s, x) (read_uint32((s), (x)))
//#endif
// --- serialize ---
#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) JL_GC_DISABLED;
static jl_value_t *jl_deserialize_value(jl_serializer_state *s, jl_value_t **loc) JL_GC_DISABLED;
static void jl_serialize_cnull(jl_serializer_state *s, jl_value_t *t)
{
backref_table_numel++;
write_uint8(s->s, TAG_CNULL);
jl_serialize_value(s, t);
}
static int module_in_worklist(jl_module_t *mod) JL_NOTSAFEPOINT
{
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) JL_NOTSAFEPOINT
{
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));
// XXX: what about Union and TypeVar??
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_NOTSAFEPOINT
{
if (!jl_is_concrete_type(p0))
return 0;
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 (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) JL_NOTSAFEPOINT
{
if (!dt->isconcretetype)
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) JL_GC_DISABLED
{
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->isconcretetype) {
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
else
tag = 10; // anything else that's internal (just may need 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 = 11; // external, but definitely new (still needs caching, but not full unique-ing)
}
else {
// this'll need 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 = 12;
}
char *dtname = jl_symbol_name(dt->name->name);
size_t dtnl = strlen(dtname);
if (dtnl > 4 && strcmp(&dtname[dtnl - 4], "##kw") == 0 && !internal && tag != 0) {
/* XXX: yuck, this is horrible, but the auto-generated kw types from the serializer isn't a real type, so we *must* be very careful */
assert(tag == 6); // other struct types should never exist
tag = 9;
if (jl_type_type_mt->kwsorter != NULL && dt == (jl_datatype_t*)jl_typeof(jl_type_type_mt->kwsorter)) {
dt = jl_datatype_type; // any representative member with this MethodTable
}
else if (jl_nonfunction_mt->kwsorter != NULL && dt == (jl_datatype_t*)jl_typeof(jl_nonfunction_mt->kwsorter)) {
dt = jl_symbol_type; // any representative member with this MethodTable
}
else {
// search for the representative member of this MethodTable
jl_methtable_t *mt = dt->name->mt;
size_t l = strlen(jl_symbol_name(mt->name));
char *prefixed;
prefixed = (char*)malloc_s(l + 2);
prefixed[0] = '#';
strcpy(&prefixed[1], jl_symbol_name(mt->name));
// remove ##kw suffix
prefixed[l-3] = 0;
jl_sym_t *tname = jl_symbol(prefixed);
free(prefixed);
jl_value_t *primarydt = jl_get_global(mt->module, tname);
if (!primarydt)
primarydt = jl_get_global(mt->module, mt->name);
primarydt = jl_unwrap_unionall(primarydt);
assert(jl_is_datatype(primarydt));
assert(primarydt == (jl_value_t*)jl_any_type || jl_typeof(((jl_datatype_t*)primarydt)->name->mt->kwsorter) == (jl_value_t*)dt);
dt = (jl_datatype_t*)primarydt;
}
}
write_uint8(s->s, TAG_DATATYPE);
write_uint8(s->s, tag);
if (tag == 6 || tag == 7) {
// for tag==6, copy its typevars in case there are references to them elsewhere
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));
write_uint8(s->s, dt->hasfreetypevars
| (dt->isconcretetype << 1)
| (dt->isdispatchtuple << 2)
| (dt->isbitstype << 3)
| (dt->zeroinit << 4)
| (dt->isinlinealloc << 5)
| (dt->has_concrete_subtype << 6));
if (!dt->abstract) {
write_uint16(s->s, dt->ninitialized);
}
write_int32(s->s, dt->hash);
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_nothing_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;
uint32_t np = dt->layout->npointers;
size_t fieldsize = jl_fielddesc_size(dt->layout->fielddesc_type);
ios_write(s->s, (const char*)dt->layout, sizeof(*dt->layout));
size_t fldsize = nf * fieldsize;
if (dt->layout->first_ptr != -1)
fldsize += np << dt->layout->fielddesc_type;
ios_write(s->s, (const char*)(dt->layout + 1), fldsize);
}
}
if (has_instance)
jl_serialize_value(s, dt->instance);
jl_serialize_value(s, dt->name);
jl_serialize_value(s, dt->names);
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)
{
write_uint8(s->s, TAG_MODULE);
jl_serialize_value(s, m->name);
size_t i;
if (!module_in_worklist(m)) {
if (m == m->parent) {
// top-level module
write_int8(s->s, 2);
int j = 0;
for (i = 0; i < jl_array_len(s->loaded_modules_array); i++) {
jl_module_t *mi = (jl_module_t*)jl_array_ptr_ref(s->loaded_modules_array, i);
if (!module_in_worklist(mi)) {
if (m == mi) {
write_int32(s->s, j);
return;
}
j++;
}
}
assert(0 && "top level module not found in modules array");
}
else {
write_int8(s->s, 1);
jl_serialize_value(s, m->parent);
}
return;
}
write_int8(s->s, 0);
jl_serialize_value(s, m->parent);
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];
jl_serialize_value(s, b->name);
jl_value_t *e = b->value;
if (!b->constp && e && jl_is_cpointer(e) && jl_unbox_voidpointer(e) != (void*)-1 && jl_unbox_voidpointer(e) != NULL)
// reset Ptr fields to C_NULL (but keep MAP_FAILED / INVALID_HANDLE)
jl_serialize_cnull(s, jl_typeof(e));
else
jl_serialize_value(s, e);
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);
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.hi);
write_uint64(s->s, m->uuid.lo);
write_uint64(s->s, m->build_id);
write_int32(s->s, m->counter);
write_int32(s->s, m->nospecialize);
write_int32(s->s, m->optlevel);
}
static int is_ir_node(jl_value_t *v)
{
// TODO: this accidentally copies QuoteNode(Expr(...)) and QuoteNode(svec(...))
return 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_uint8(v) ||
jl_is_quotenode(v) || jl_is_gotonode(v) || jl_is_linenode(v) || jl_is_globalref(v) ||
jl_is_phinode(v) || jl_is_phicnode(v) || jl_is_upsilonnode(v) || jl_is_pinode(v) ||
jl_typeis(v, jl_lineinfonode_type);
}
static int literal_val_id(jl_serializer_state *s, jl_value_t *v) JL_GC_DISABLED
{
jl_array_t *rs = s->method->roots;
int i, l = jl_array_len(rs);
if (jl_is_symbol(v) || jl_is_concrete_type(v)) {
for (i = 0; i < l; i++) {
if (jl_array_ptr_ref(rs, i) == v)
return i;
}
}
else {
for (i = 0; i < l; i++) {
if (jl_egal(jl_array_ptr_ref(rs, i), v))
return i;
}
}
jl_array_ptr_1d_push(rs, v);
return jl_array_len(rs) - 1;
}
static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_literal) JL_GC_DISABLED
{
if (v == NULL) {
write_uint8(s->s, TAG_NULL);
return;
}
void *tag = ptrhash_get(&ser_tag, v);
if (tag != HT_NOTFOUND) {
uint8_t t8 = (intptr_t)tag;
if (t8 <= LAST_TAG)
write_uint8(s->s, 0);
write_uint8(s->s, t8);
return;
}
if (jl_is_symbol(v)) {
void *idx = ptrhash_get(&common_symbol_tag, v);
if (idx != HT_NOTFOUND) {
write_uint8(s->s, TAG_COMMONSYM);
write_uint8(s->s, (uint8_t)(size_t)idx);
return;
}
}
else if (v == (jl_value_t*)jl_core_module) {
write_uint8(s->s, TAG_CORE);
return;
}
else if (v == (jl_value_t*)jl_base_module) {
write_uint8(s->s, TAG_BASE);
return;
}
if (s->mode == MODE_IR) {
if (v == (jl_value_t*)s->method->module) {
write_uint8(s->s, TAG_NEARBYMODULE);
return;
}
else if (jl_is_datatype(v) && ((jl_datatype_t*)v)->name == jl_array_typename &&
jl_is_long(jl_tparam1(v)) && jl_unbox_long(jl_tparam1(v)) == 1 &&
!((jl_datatype_t*)v)->hasfreetypevars) {
write_uint8(s->s, TAG_VECTORTY);
jl_serialize_value(s, jl_tparam0(v));
return;
}
else if (jl_is_datatype(v) && ((jl_datatype_t*)v)->name == jl_pointer_typename &&
!((jl_datatype_t*)v)->hasfreetypevars) {
write_uint8(s->s, TAG_PTRTY);
jl_serialize_value(s, jl_tparam0(v));
return;
}
else if (!as_literal && !is_ir_node(v)) {
int id = literal_val_id(s, v);
assert(id >= 0);
if (id < 256) {
write_uint8(s->s, TAG_METHODROOT);
write_uint8(s->s, id);
}
else {
assert(id <= UINT16_MAX);
write_uint8(s->s, TAG_LONG_METHODROOT);
write_uint16(s->s, id);
}
return;
}
}
else if (jl_typeis(v, jl_string_type) && jl_string_len(v) == 0) {
jl_serialize_value(s, jl_an_empty_string);
return;
}
else if (!jl_is_uint8(v)) {
void **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, TAG_SHORT_BACKREF);
write_uint16(s->s, pos);
}
else {
write_uint8(s->s, TAG_BACKREF);
write_int32(s->s, pos);
}
return;
}
intptr_t pos = backref_table_numel++;
if (((jl_datatype_t*)(jl_typeof(v)))->name == jl_idtable_typename) {
// 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);
}
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) {
write_uint8(s->s, TAG_SVEC);
write_uint8(s->s, (uint8_t)l);
}
else {
write_uint8(s->s, TAG_LONG_SVEC);
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) {
write_uint8(s->s, TAG_SYMBOL);
write_uint8(s->s, (uint8_t)l);
}
else {
write_uint8(s->s, TAG_LONG_SYMBOL);
write_int32(s->s, l);
}
ios_write(s->s, jl_symbol_name((jl_sym_t*)v), l);
}
else if (jl_is_globalref(v)) {
if (s->mode == MODE_IR && jl_globalref_mod(v) == s->method->module) {
write_uint8(s->s, TAG_NEARBYGLOBAL);
jl_serialize_value(s, jl_globalref_name(v));
}
else {
write_uint8(s->s, TAG_GLOBALREF);
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 < 256 && ((jl_ssavalue_t*)v)->id >= 0) {
write_uint8(s->s, TAG_SSAVALUE);
write_uint8(s->s, ((jl_ssavalue_t*)v)->id);
}
else if (jl_is_ssavalue(v) && ((jl_ssavalue_t*)v)->id <= UINT16_MAX && ((jl_ssavalue_t*)v)->id >= 0) {
write_uint8(s->s, TAG_LONG_SSAVALUE);
write_uint16(s->s, ((jl_ssavalue_t*)v)->id);
}
else if (jl_typeis(v, jl_slotnumber_type) && jl_slot_number(v) <= UINT16_MAX && jl_slot_number(v) >= 0) {
write_uint8(s->s, TAG_SLOTNUMBER);
write_uint16(s->s, jl_slot_number(v));
}
else if (jl_is_array(v)) {
jl_array_t *ar = (jl_array_t*)v;
jl_value_t *et = jl_tparam0(jl_typeof(ar));
int isunion = jl_is_uniontype(et);
if (ar->flags.ndims == 1 && ar->elsize <= 0x1f) {
write_uint8(s->s, TAG_ARRAY1D);
write_uint8(s->s, (ar->flags.ptrarray << 7) | (ar->flags.hasptr << 6) | (isunion << 5) | (ar->elsize & 0x1f));
}
else {
write_uint8(s->s, TAG_ARRAY);
write_uint16(s->s, ar->flags.ndims);
write_uint16(s->s, (ar->flags.ptrarray << 15) | (ar->flags.hasptr << 14) | (isunion << 13) | (ar->elsize & 0x1fff));
}
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));
size_t l = jl_array_len(ar);
if (ar->flags.ptrarray) {
for (i = 0; i < l; i++) {
jl_value_t *e = jl_array_ptr_ref(v, i);
if (e && jl_is_cpointer(e) && jl_unbox_voidpointer(e) != (void*)-1 && jl_unbox_voidpointer(e) != NULL)
// reset Ptr elements to C_NULL (but keep MAP_FAILED / INVALID_HANDLE)
jl_serialize_cnull(s, jl_typeof(e));
else
jl_serialize_value(s, e);
}
}
else if (ar->flags.hasptr) {
const char *data = (const char*)jl_array_data(ar);
uint16_t elsz = ar->elsize;
size_t j, np = ((jl_datatype_t*)et)->layout->npointers;
for (i = 0; i < l; i++) {
const char *start = data;
for (j = 0; j < np; j++) {
uint32_t ptr = jl_ptr_offset((jl_datatype_t*)et, j);
const jl_value_t *const *fld = &((const jl_value_t *const *)data)[ptr];
if ((const char*)fld != start)
ios_write(s->s, start, (const char*)fld - start);
JL_GC_PROMISE_ROOTED(*fld);
jl_serialize_value(s, *fld);
start = (const char*)&fld[1];
}
data += elsz;
if (data != start)
ios_write(s->s, start, data - start);
}
}
else if (jl_is_cpointer_type(et)) {
// reset Ptr elements to C_NULL
const void **data = (const void**)jl_array_data(ar);
for (i = 0; i < l; i++) {
const void *e = data[i];
if (e != (void*)-1)
e = NULL;
ios_write(s->s, (const char*)&e, sizeof(e));
}
}
else {
ios_write(s->s, (char*)jl_array_data(ar), l * ar->elsize);
if (jl_array_isbitsunion(ar))
ios_write(s->s, jl_array_typetagdata(ar), l);
}
}
else if (jl_is_expr(v)) {
jl_expr_t *e = (jl_expr_t*)v;
size_t l = jl_array_len(e->args);
if (e->head == call_sym) {
if (l == 2) {
write_uint8(s->s, TAG_CALL1);
jl_serialize_value(s, jl_exprarg(e, 0));
jl_serialize_value(s, jl_exprarg(e, 1));
return;
}
else if (l == 3) {
write_uint8(s->s, TAG_CALL2);
jl_serialize_value(s, jl_exprarg(e, 0));
jl_serialize_value(s, jl_exprarg(e, 1));
jl_serialize_value(s, jl_exprarg(e, 2));
return;
}
}
if (l <= 255) {
write_uint8(s->s, TAG_EXPR);
write_uint8(s->s, (uint8_t)l);
}
else {
write_uint8(s->s, TAG_LONG_EXPR);
write_int32(s->s, l);
}
jl_serialize_value(s, e->head);
for (i = 0; i < l; i++) {
jl_serialize_value(s, jl_exprarg(e, i));
}
}
else if (jl_is_phinode(v)) {
jl_array_t *edges = (jl_array_t*)jl_fieldref_noalloc(v, 0);
jl_array_t *values = (jl_array_t*)jl_fieldref_noalloc(v, 1);
size_t l = jl_array_len(edges);
if (l <= 255 && jl_array_len(values) == l) {
write_uint8(s->s, TAG_PHINODE);
write_uint8(s->s, (uint8_t)l);
}
else {
write_uint8(s->s, TAG_LONG_PHINODE);
write_int32(s->s, l);
write_int32(s->s, jl_array_len(values));
}
for (i = 0; i < l; i++) {
jl_serialize_value(s, jl_array_ptr_ref(edges, i));
}
l = jl_array_len(values);
for (i = 0; i < l; i++) {
jl_serialize_value(s, jl_array_ptr_ref(values, i));
}
}
else if (jl_is_phicnode(v)) {
jl_array_t *values = (jl_array_t*)jl_fieldref_noalloc(v, 0);
size_t l = jl_array_len(values);
if (l <= 255) {
write_uint8(s->s, TAG_PHICNODE);
write_uint8(s->s, (uint8_t)l);
}
else {
write_uint8(s->s, TAG_LONG_PHICNODE);
write_int32(s->s, l);
}
for (i = 0; i < l; i++) {
jl_serialize_value(s, jl_array_ptr_ref(values, i));
}
}
else if (jl_is_gotonode(v)) {
write_uint8(s->s, TAG_GOTONODE);
jl_serialize_value(s, jl_get_nth_field(v, 0));
}
else if (jl_is_quotenode(v)) {
write_uint8(s->s, TAG_QUOTENODE);
jl_serialize_value(s, jl_get_nth_field(v, 0));
}
else if (jl_is_datatype(v)) {
jl_serialize_datatype(s, (jl_datatype_t*)v);
}
else if (jl_is_unionall(v)) {
write_uint8(s->s, TAG_UNIONALL);
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);
}
else {
write_uint8(s->s, 0);
jl_serialize_value(s, ((jl_unionall_t*)v)->var);
jl_serialize_value(s, ((jl_unionall_t*)v)->body);
}
}
else if (jl_is_typevar(v)) {
write_uint8(s->s, TAG_TVAR);
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)) {
write_uint8(s->s, TAG_METHOD);
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);
jl_serialize_value(s, (jl_value_t*)m->module);
write_uint8(s->s, internal);
if (!internal)
return;
jl_methtable_t *mt = jl_method_table_for((jl_value_t*)m->sig);
assert((jl_value_t*)mt != jl_nothing);
external_mt = !module_in_worklist(mt->module);
jl_serialize_value(s, m->specializations);
jl_serialize_value(s, m->speckeyset);
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);
jl_serialize_value(s, jl_nothing);
}
else {
jl_serialize_value(s, (jl_value_t*)m->ambig);
jl_serialize_value(s, (jl_value_t*)m->resorted);
}
write_int32(s->s, m->called);
write_int32(s->s, m->nargs);
write_int32(s->s, m->nospecialize);
write_int32(s->s, m->nkw);
write_int8(s->s, m->isva);
write_int8(s->s, m->pure);
jl_serialize_value(s, (jl_value_t*)m->slot_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);
}
else if (jl_is_method_instance(v)) {
write_uint8(s->s, TAG_METHOD_INSTANCE);
jl_method_instance_t *mi = (jl_method_instance_t*)v;
int internal = 0;
if (!jl_is_method(mi->def.method))
internal = 1;
else if (module_in_worklist(mi->def.method->module))
internal = 2;
write_uint8(s->s, internal);
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;
}
if (internal == 1)
jl_serialize_value(s, (jl_value_t*)mi->uninferred);
jl_serialize_value(s, (jl_value_t*)mi->specTypes);
jl_serialize_value(s, mi->def.value);
if (!internal)
return;
jl_serialize_value(s, (jl_value_t*)mi->sparam_vals);
jl_array_t *backedges = mi->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);
jl_serialize_value(s, (jl_value_t*)mi->cache);
}
else if (jl_is_code_instance(v)) {
write_uint8(s->s, TAG_CODE_INSTANCE);
jl_code_instance_t *codeinst = (jl_code_instance_t*)v;
int validate = 0;
if (codeinst->max_world == ~(size_t)0)
validate = 1; // can check on deserialize if this cache entry is still valid
int flags = validate << 0;
if (codeinst->invoke == jl_fptr_const_return)
flags |= 1 << 2;
write_uint8(s->s, flags);
jl_serialize_value(s, (jl_value_t*)codeinst->def);
if (validate || codeinst->min_world == 0) {
jl_serialize_value(s, codeinst->inferred);
jl_serialize_value(s, codeinst->rettype_const);
jl_serialize_value(s, codeinst->rettype);
}
else {
// skip storing useless data
jl_serialize_value(s, NULL);
jl_serialize_value(s, NULL);
jl_serialize_value(s, jl_any_type);
}
jl_serialize_value(s, codeinst->next);
}
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)) {
write_uint8(s->s, TAG_STRING);
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_int64_type)) {
void *data = jl_data_ptr(v);
if (*(int64_t*)data >= INT16_MIN && *(int64_t*)data <= INT16_MAX) {
write_uint8(s->s, TAG_SHORTER_INT64);
write_uint16(s->s, (uint16_t)*(int64_t*)data);
}
else if (*(int64_t*)data >= S32_MIN && *(int64_t*)data <= S32_MAX) {
write_uint8(s->s, TAG_SHORT_INT64);
write_int32(s->s, (int32_t)*(int64_t*)data);
}
else {
write_uint8(s->s, TAG_INT64);
write_int64(s->s, *(int64_t*)data);
}
}
else if (jl_typeis(v, jl_int32_type)) {
void *data = jl_data_ptr(v);
if (*(int32_t*)data >= INT16_MIN && *(int32_t*)data <= INT16_MAX) {
write_uint8(s->s, TAG_SHORT_INT32);
write_uint16(s->s, (uint16_t)*(int32_t*)data);
}
else {
write_uint8(s->s, TAG_INT32);
write_int32(s->s, *(int32_t*)data);
}
}
else if (jl_typeis(v, jl_uint8_type)) {
write_uint8(s->s, TAG_UINT8);
write_int8(s->s, *(int8_t*)jl_data_ptr(v));
}
else if (jl_is_cpointer(v) && jl_unbox_voidpointer(v) == NULL) {
write_uint8(s->s, TAG_CNULL);
jl_serialize_value(s, jl_typeof(v));
return;
}
else if (jl_typeis(v, jl_lineinfonode_type)) {
write_uint8(s->s, TAG_LINEINFO);
for (i = 0; i < jl_datatype_nfields(jl_lineinfonode_type); i++)
jl_serialize_value(s, jl_get_nth_field(v, i));
}
else if (jl_bigint_type && jl_typeis(v, jl_bigint_type)) {
write_uint8(s->s, TAG_SHORT_GENERAL);
write_uint8(s->s, jl_datatype_size(jl_bigint_type));