-
Notifications
You must be signed in to change notification settings - Fork 793
/
Copy pathTypedTreePickle.fs
2909 lines (2515 loc) · 112 KB
/
TypedTreePickle.fs
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
module internal FSharp.Compiler.TypedTreePickle
open System.Collections.Generic
open System.Text
open FSharp.Compiler.IO
open Internal.Utilities
open Internal.Utilities.Collections
open Internal.Utilities.Library
open Internal.Utilities.Library.Extras
open Internal.Utilities.Library.Extras.Bits
open Internal.Utilities.Rational
open FSharp.Compiler
open FSharp.Compiler.AbstractIL.IL
open FSharp.Compiler.AbstractIL.Diagnostics
open FSharp.Compiler.CompilerGlobalState
open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Text.Position
open FSharp.Compiler.Text.Range
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTreeOps
open FSharp.Compiler.Text
open FSharp.Compiler.Xml
open FSharp.Compiler.TypedTree
open FSharp.Compiler.TypedTreeBasics
open FSharp.Compiler.TypedTreeOps
open FSharp.Compiler.TcGlobals
#if !NO_TYPEPROVIDERS
let verbose = false
#endif
let ffailwith fileName str =
let msg = FSComp.SR.pickleErrorReadingWritingMetadata(fileName, str)
System.Diagnostics.Debug.Assert(false, msg)
failwith msg
// Fixup pickled data w.r.t. a set of CCU thunks indexed by name
[<NoEquality; NoComparison>]
type PickledDataWithReferences<'rawData> =
{ /// The data that uses a collection of CcuThunks internally
RawData: 'rawData
/// The assumptions that need to be fixed up
FixupThunks: CcuThunk [] }
member x.Fixup loader =
x.FixupThunks |> Array.iter (fun reqd -> reqd.Fixup(loader reqd.AssemblyName))
x.RawData
/// Like Fixup but loader may return None, in which case there is no fixup.
member x.OptionalFixup loader =
x.FixupThunks
|> Array.iter(fun reqd->
// Only fixup what needs fixing up
if reqd.IsUnresolvedReference then
match loader reqd.AssemblyName with
| Some loaded ->
if reqd.IsUnresolvedReference then reqd.Fixup loaded
| _ -> () )
x.RawData
//---------------------------------------------------------------------------
// Basic pickle/unpickle state
//---------------------------------------------------------------------------
[<NoEquality; NoComparison>]
type Table<'T when 'T: not null> =
{ name: string
tbl: Dictionary<'T, int>
mutable rows: ResizeArray<'T>
mutable count: int }
member tbl.AsArray = Seq.toArray tbl.rows
member tbl.Size = tbl.rows.Count
member tbl.Add x =
let n = tbl.count
tbl.count <- tbl.count + 1
tbl.tbl[x] <- n
tbl.rows.Add x
n
member tbl.FindOrAdd x =
match tbl.tbl.TryGetValue x with
| true, res -> res
| _ -> tbl.Add x
static member Create n =
{ name = n
tbl = Dictionary<_, _>(1000, HashIdentity.Structural)
rows= ResizeArray<_>(1000)
count=0 }
[<NoEquality; NoComparison>]
type InputTable<'T> =
{ itbl_name: string
itbl_rows: 'T array }
let new_itbl n r = { itbl_name=n; itbl_rows=r }
[<NoEquality; NoComparison>]
type NodeOutTable<'Data, 'Node> =
{ NodeStamp : 'Node -> Stamp
NodeName : 'Node -> string
GetRange : 'Node -> range
Deref: 'Node -> 'Data
Name: string
Table: Table<Stamp> }
member x.Size = x.Table.Size
// inline this to get known-type-information through to the HashMultiMap constructor
static member inline Create (stampF, nameF, rangeF, derefF, nm) =
{ NodeStamp = stampF
NodeName = nameF
GetRange = rangeF
Deref = derefF
Name = nm
Table = Table<_>.Create nm }
[<NoEquality; NoComparison>]
type WriterState =
{ os: ByteBuffer
osB: ByteBuffer
oscope: CcuThunk
occus: Table<CcuReference>
oentities: NodeOutTable<EntityData, Entity>
otypars: NodeOutTable<TyparData, Typar>
ovals: NodeOutTable<ValData, Val>
oanoninfos: NodeOutTable<AnonRecdTypeInfo, AnonRecdTypeInfo>
ostrings: Table<string>
opubpaths: Table<int[]>
onlerefs: Table<int * int[]>
osimpletys: Table<int>
oglobals : TcGlobals
mutable isStructThisArgPos : bool
ofile : string
/// Indicates if we are using in-memory format, where we store XML docs as well
oInMem : bool
}
let pfailwith st str = ffailwith st.ofile str
[<NoEquality; NoComparison>]
type NodeInTable<'Data, 'Node> =
{ LinkNode : 'Node -> 'Data -> unit
IsLinked : 'Node -> bool
Name : string
Nodes : 'Node[] }
member x.Get n = x.Nodes[n]
member x.Count = x.Nodes.Length
static member Create (mkEmpty, lnk, isLinked, nm, n) =
{ LinkNode = lnk; IsLinked = isLinked; Name = nm; Nodes = Array.init n (fun _i -> mkEmpty() ) }
[<NoEquality; NoComparison>]
type ReaderState =
{ is: ByteStream
// secondary stream of information for F# 5.0
isB: ByteStream
iilscope: ILScopeRef
iccus: InputTable<CcuThunk>
ientities: NodeInTable<EntityData, Tycon>
itypars: NodeInTable<TyparData, Typar>
ivals: NodeInTable<ValData, Val>
ianoninfos: NodeInTable<AnonRecdTypeInfo, AnonRecdTypeInfo>
istrings: InputTable<string>
ipubpaths: InputTable<PublicPath>
inlerefs: InputTable<NonLocalEntityRef>
isimpletys: InputTable<TType>
ifile: string
iILModule : ILModuleDef option // the Abstract IL metadata for the DLL being read
}
let ufailwith st str = ffailwith st.ifile str
//---------------------------------------------------------------------------
// Basic pickle/unpickle operations
//---------------------------------------------------------------------------
type 'T pickler = 'T -> WriterState -> unit
let p_byte b st = st.os.EmitIntAsByte b
let p_byteB b st = st.osB.EmitIntAsByte b
let p_bool b st = p_byte (if b then 1 else 0) st
/// Write an uncompressed integer to the main stream.
let prim_p_int32 i st =
p_byte (b0 i) st
p_byte (b1 i) st
p_byte (b2 i) st
p_byte (b3 i) st
/// Write an uncompressed integer to the B stream.
let prim_p_int32B i st =
p_byteB (b0 i) st
p_byteB (b1 i) st
p_byteB (b2 i) st
p_byteB (b3 i) st
/// Compress integers according to the same scheme used by CLR metadata
/// This halves the size of pickled data
let p_int32 n st =
if n >= 0 && n <= 0x7F then
p_byte (b0 n) st
else if n >= 0x80 && n <= 0x3FFF then
p_byte (0x80 ||| (n >>> 8)) st
p_byte (n &&& 0xFF) st
else
p_byte 0xFF st
prim_p_int32 n st
/// Write a compressed integer to the B stream.
let p_int32B n st =
if n >= 0 && n <= 0x7F then
p_byteB (b0 n) st
else if n >= 0x80 && n <= 0x3FFF then
p_byteB ( (0x80 ||| (n >>> 8))) st
p_byteB ( (n &&& 0xFF)) st
else
p_byteB 0xFF st
prim_p_int32B n st
let space = ()
let p_space n () st =
for i = 0 to n - 1 do
p_byte 0 st
/// Represents space that was reserved but is now possibly used
let p_used_space1 f st =
p_byte 1 st
f st
// leave more space
p_space 1 space st
let p_bytes (s: byte[]) st =
let len = s.Length
p_int32 len st
st.os.EmitBytes s
let p_memory (s: System.ReadOnlyMemory<byte>) st =
let len = s.Length
p_int32 len st
st.os.EmitMemory s
let p_prim_string (s: string) st =
let bytes = Encoding.UTF8.GetBytes s
let len = bytes.Length
p_int32 len st
st.os.EmitBytes bytes
let p_int c st = p_int32 c st
let p_intB c st = p_int32B c st
let p_int8 (i: sbyte) st = p_int32 (int32 i) st
let p_uint8 (i: byte) st = p_byte (int i) st
let p_int16 (i: int16) st = p_int32 (int32 i) st
let p_uint16 (x: uint16) st = p_int32 (int32 x) st
let p_uint32 (x: uint32) st = p_int32 (int32 x) st
let p_int64 (i: int64) st =
p_int32 (int32 (i &&& 0xFFFFFFFFL)) st
p_int32 (int32 (i >>> 32)) st
let p_uint64 (x: uint64) st = p_int64 (int64 x) st
let bits_of_float32 (x: float32) = System.BitConverter.ToInt32(System.BitConverter.GetBytes x, 0)
let bits_of_float (x: float) = System.BitConverter.DoubleToInt64Bits x
let p_single i st = p_int32 (bits_of_float32 i) st
let p_char i st = p_uint16 (uint16 (int32 i)) st
let inline p_tup2 p1 p2 (a, b) (st: WriterState) =
(p1 a st : unit); (p2 b st : unit)
let inline p_tup3 p1 p2 p3 (a, b, c) (st: WriterState) =
(p1 a st : unit); (p2 b st : unit); (p3 c st : unit)
let inline p_tup4 p1 p2 p3 p4 (a, b, c, d) (st: WriterState) =
(p1 a st : unit); (p2 b st : unit); (p3 c st : unit); (p4 d st : unit)
let inline p_tup5 p1 p2 p3 p4 p5 (a, b, c, d, e) (st: WriterState) =
(p1 a st : unit); (p2 b st : unit); (p3 c st : unit); (p4 d st : unit); (p5 e st : unit)
let inline p_tup6 p1 p2 p3 p4 p5 p6 (a, b, c, d, e, f) (st: WriterState) =
(p1 a st : unit); (p2 b st : unit); (p3 c st : unit); (p4 d st : unit); (p5 e st : unit); (p6 f st : unit)
let inline p_tup9 p1 p2 p3 p4 p5 p6 p7 p8 p9 (a, b, c, d, e, f, x7, x8, x9) (st: WriterState) =
(p1 a st : unit); (p2 b st : unit); (p3 c st : unit); (p4 d st : unit); (p5 e st : unit); (p6 f st : unit); (p7 x7 st : unit); (p8 x8 st : unit); (p9 x9 st : unit)
let inline p_tup11 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 (a, b, c, d, e, f, x7, x8, x9, x10, x11) (st: WriterState) =
(p1 a st : unit); (p2 b st : unit); (p3 c st : unit); (p4 d st : unit); (p5 e st : unit); (p6 f st : unit); (p7 x7 st : unit); (p8 x8 st : unit); (p9 x9 st : unit); (p10 x10 st : unit); (p11 x11 st : unit)
let u_byte st = int (st.is.ReadByte())
/// Unpickle an uncompressed integer from the B stream
/// The extra B stream of bytes is implicitly 0 if not present
let u_byteB st =
if st.isB.IsEOF then 0 else int (st.isB.ReadByte())
type unpickler<'T> = ReaderState -> 'T
let u_bool st = let b = u_byte st in (b = 1)
/// Unpickle an uncompressed integer from the main stream
let prim_u_int32 st =
let b0 = (u_byte st)
let b1 = (u_byte st)
let b2 = (u_byte st)
let b3 = (u_byte st)
b0 ||| (b1 <<< 8) ||| (b2 <<< 16) ||| (b3 <<< 24)
/// Unpickle an uncompressed integer from the B stream
let prim_u_int32B st =
let b0 = u_byteB st
let b1 = u_byteB st
let b2 = u_byteB st
let b3 = u_byteB st
b0 ||| (b1 <<< 8) ||| (b2 <<< 16) ||| (b3 <<< 24)
let u_int32 st =
let b0 = u_byte st
if b0 <= 0x7F then b0
else if b0 <= 0xbf then
let b0 = b0 &&& 0x7F
let b1 = (u_byte st)
(b0 <<< 8) ||| b1
else
assert(b0 = 0xFF)
prim_u_int32 st
/// Unpickle a compressed integer from the B stream.
/// The integer is 0 if the B stream is not present.
let u_int32B st =
let b0 = u_byteB st
if b0 <= 0x7F then b0
else if b0 <= 0xbf then
let b0 = b0 &&& 0x7F
let b1 = u_byteB st
(b0 <<< 8) ||| b1
else
assert(b0 = 0xFF)
prim_u_int32B st
let u_byte_memory st =
let n = (u_int32 st)
st.is.ReadBytes n
let u_bytes st =
(u_byte_memory st).ToArray()
let u_prim_string st =
let len = (u_int32 st)
st.is.ReadUtf8String len
let u_int st = u_int32 st
let u_intB st = u_int32B st
let u_int8 st = sbyte (u_int32 st)
let u_uint8 st = byte (u_byte st)
let u_int16 st = int16 (u_int32 st)
let u_uint16 st = uint16 (u_int32 st)
let u_uint32 st = uint32 (u_int32 st)
let u_int64 st =
let b1 = (int64 (u_int32 st)) &&& 0xFFFFFFFFL
let b2 = int64 (u_int32 st)
b1 ||| (b2 <<< 32)
let u_uint64 st = uint64 (u_int64 st)
let float32_of_bits (x: int32) = System.BitConverter.ToSingle(System.BitConverter.GetBytes x, 0)
let float_of_bits (x: int64) = System.BitConverter.Int64BitsToDouble x
let u_single st = float32_of_bits (u_int32 st)
let u_char st = char (int32 (u_uint16 st))
let u_space n st =
for i = 0 to n - 1 do
let b = u_byte st
if b <> 0 then
warning(Error(FSComp.SR.pickleUnexpectedNonZero st.ifile, range0))
/// Represents space that was reserved but is now possibly used
let u_used_space1 f st =
let b = u_byte st
match b with
| 0 -> None
| 1 ->
let x = f st
u_space 1 st
Some x
| _ ->
warning(Error(FSComp.SR.pickleUnexpectedNonZero st.ifile, range0)); None
let inline u_tup2 p1 p2 (st: ReaderState) = let a = p1 st in let b = p2 st in (a, b)
let inline u_tup3 p1 p2 p3 (st: ReaderState) =
let a = p1 st in let b = p2 st in let c = p3 st in (a, b, c)
let inline u_tup4 p1 p2 p3 p4 (st: ReaderState) =
let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in (a, b, c, d)
let inline u_tup5 p1 p2 p3 p4 p5 (st: ReaderState) =
let a = p1 st
let b = p2 st
let c = p3 st
let d = p4 st
let e = p5 st
(a, b, c, d, e)
let inline u_tup6 p1 p2 p3 p4 p5 p6 (st: ReaderState) =
let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in let e = p5 st in let f = p6 st in (a, b, c, d, e, f)
let inline u_tup8 p1 p2 p3 p4 p5 p6 p7 p8 (st: ReaderState) =
let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in let e = p5 st in let f = p6 st in let x7 = p7 st in let x8 = p8 st in (a, b, c, d, e, f, x7, x8)
let inline u_tup9 p1 p2 p3 p4 p5 p6 p7 p8 p9 (st: ReaderState) =
let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in let e = p5 st in let f = p6 st in let x7 = p7 st in let x8 = p8 st in let x9 = p9 st in (a, b, c, d, e, f, x7, x8, x9)
let inline u_tup13 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 (st: ReaderState) =
let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in
let e = p5 st in let f = p6 st in let x7 = p7 st in let x8 = p8 st in
let x9 = p9 st in let x10 = p10 st in let x11 = p11 st in let x12 = p12 st in let x13 = p13 st in
(a, b, c, d, e, f, x7, x8, x9, x10, x11, x12, x13)
let inline u_tup17 p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 (st: ReaderState) =
let a = p1 st in let b = p2 st in let c = p3 st in let d = p4 st in
let e = p5 st in let f = p6 st in let x7 = p7 st in let x8 = p8 st in
let x9 = p9 st in let x10 = p10 st in let x11 = p11 st in let x12 = p12 st in let x13 = p13 st in
let x14 = p14 st in let x15 = p15 st in let x16 = p16 st in let x17 = p17 st in
(a, b, c, d, e, f, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17)
//---------------------------------------------------------------------------
// Pickle/unpickle operations for observably shared graph nodes
//---------------------------------------------------------------------------
// exception Nope
// ctxt is for debugging
let p_osgn_ref (_ctxt: string) (outMap : NodeOutTable<_, _>) x st =
let idx = outMap.Table.FindOrAdd (outMap.NodeStamp x)
//if ((idx = 0) && outMap.Name = "oentities") then
// let msg =
// sprintf "idx %d#%d in table %s has name '%s', was defined at '%s' and is referenced from context %s\n"
// idx (outMap.NodeStamp x)
// outMap.Name (outMap.NodeName x)
// (stringOfRange (outMap.GetRange x))
// _ctxt
// System.Diagnostics.Debug.Assert(false, msg )
p_int idx st
let p_osgn_decl (outMap : NodeOutTable<_, _>) p x st =
let stamp = outMap.NodeStamp x
let idx = outMap.Table.FindOrAdd stamp
//dprintf "decl %d#%d in table %s has name %s\n" idx (outMap.NodeStamp x) outMap.Name (outMap.NodeName x)
p_tup2 p_int p (idx, outMap.Deref x) st
let u_osgn_ref (inMap: NodeInTable<_, _>) st =
let n = u_int st
if n < 0 || n >= inMap.Count then ufailwith st ("u_osgn_ref: out of range, table = "+inMap.Name+", n = "+string n)
inMap.Get n
let u_osgn_decl (inMap: NodeInTable<_, _>) u st =
let idx, data = u_tup2 u_int u st
// dprintf "unpickling osgn %d in table %s\n" idx nm
let res = inMap.Get idx
inMap.LinkNode res data
res
//---------------------------------------------------------------------------
// Pickle/unpickle operations for interned nodes
//---------------------------------------------------------------------------
let encode_uniq (tbl: Table<_>) key = tbl.FindOrAdd key
let lookup_uniq st tbl n =
let arr = tbl.itbl_rows
if n < 0 || n >= arr.Length then ufailwith st ("lookup_uniq in table "+tbl.itbl_name+" out of range, n = "+string n+ ", sizeof(tab) = " + string (Array.length arr))
arr[n]
//---------------------------------------------------------------------------
// Pickle/unpickle arrays and lists. For lists use the same binary format as arrays so we can switch
// between internal representations relatively easily
//-------------------------------------------------------------------------
let p_array_core f (x: 'T[]) st =
for i = 0 to x.Length-1 do
f x[i] st
let p_array f (x: 'T[]) st =
p_int x.Length st
p_array_core f x st
let p_list_core f (xs: 'T list) st =
for x in xs do
f x st
let p_list f x st =
p_int (List.length x) st
p_list_core f x st
let p_listB f x st =
p_intB (List.length x) st
p_list_core f x st
let p_list_ext extraf f x st =
let n = List.length x
let n = if Option.isSome extraf then n ||| 0x80000000 else n
p_int n st
match extraf with
| None -> ()
| Some f -> f st
p_list_core f x st
let p_List f (x: 'T list) st = p_list f x st
let p_wrap (f: 'T -> 'U) (p : 'U pickler) : 'T pickler = (fun x st -> p (f x) st)
let p_option f x st =
match x with
| None -> p_byte 0 st
| Some h -> p_byte 1 st; f h st
// Pickle lazy values in such a way that they can, in some future F# compiler version, be read back
// lazily. However, a lazy reader is not used in this version because the value may contain the definitions of some
// OSGN nodes.
let private p_lazy_impl p v st =
let fixupPos1 = st.os.Position
// We fix these up after
prim_p_int32 0 st
let fixupPos2 = st.os.Position
prim_p_int32 0 st
let fixupPos3 = st.os.Position
prim_p_int32 0 st
let fixupPos4 = st.os.Position
prim_p_int32 0 st
let fixupPos5 = st.os.Position
prim_p_int32 0 st
let fixupPos6 = st.os.Position
prim_p_int32 0 st
let fixupPos7 = st.os.Position
prim_p_int32 0 st
let idx1 = st.os.Position
let otyconsIdx1 = st.oentities.Size
let otyparsIdx1 = st.otypars.Size
let ovalsIdx1 = st.ovals.Size
// Run the pickler
p v st
// Determine and fixup the length of the pickled data
let idx2 = st.os.Position
st.os.FixupInt32 fixupPos1 (idx2-idx1)
// Determine and fixup the ranges of OSGN nodes defined within the lazy portion
let otyconsIdx2 = st.oentities.Size
let otyparsIdx2 = st.otypars.Size
let ovalsIdx2 = st.ovals.Size
st.os.FixupInt32 fixupPos2 otyconsIdx1
st.os.FixupInt32 fixupPos3 otyconsIdx2
st.os.FixupInt32 fixupPos4 otyparsIdx1
st.os.FixupInt32 fixupPos5 otyparsIdx2
st.os.FixupInt32 fixupPos6 ovalsIdx1
st.os.FixupInt32 fixupPos7 ovalsIdx2
let p_lazy p x st =
p_lazy_impl p (InterruptibleLazy.force x) st
let p_maybe_lazy p (x: MaybeLazy<_>) st =
p_lazy_impl p x.Value st
let p_hole () =
let mutable h = None
(fun f -> h <- Some f), (fun x st -> match h with Some f -> f x st | None -> pfailwith st "p_hole: unfilled hole")
let p_hole2 () =
let mutable h = None
(fun f -> h <- Some f), (fun arg x st -> match h with Some f -> f arg x st | None -> pfailwith st "p_hole2: unfilled hole")
let u_array_core f n st =
let res = Array.zeroCreate n
for i = 0 to n-1 do
res[i] <- f st
res
let u_array f st =
let n = u_int st
u_array_core f n st
let u_list_core f n st =
List.init n (fun _ -> f st)
let u_list f st =
let n = u_int st
u_list_core f n st
/// Unpickle a list from the B stream. The resulting list is empty if the B stream is not present.
let u_listB f st =
let n = u_intB st
u_list_core f n st
let u_list_ext extra f st =
let n = u_int st
let extraItem =
if n &&& 0x80000000 = 0x80000000 then
Some (extra st)
else
None
let list = u_list_core f (n &&& 0x7FFFFFFF) st
extraItem, list
let u_List f st = u_list f st // new List<_> (u_array f st)
// Mark up default constraints with a priority in reverse order: last gets 0 etc. See comment on TyparConstraint.DefaultsTo
let u_list_revi f st =
let n = u_int st
[ for i = 0 to n-1 do
yield f st (n-1-i) ]
let u_wrap (f: 'U -> 'T) (u : 'U unpickler) : 'T unpickler = (u >> f)
let u_option f st =
let tag = u_byte st
match tag with
| 0 -> None
| 1 -> Some (f st)
| n -> ufailwith st ("u_option: found number " + string n)
let u_lazy u st =
// Read the number of bytes in the record
let len = prim_u_int32 st // fixupPos1
// These are the ranges of OSGN nodes defined within the lazily read portion of the graph
let otyconsIdx1 = prim_u_int32 st // fixupPos2
let otyconsIdx2 = prim_u_int32 st // fixupPos3
let otyparsIdx1 = prim_u_int32 st // fixupPos4
let otyparsIdx2 = prim_u_int32 st // fixupPos5
let ovalsIdx1 = prim_u_int32 st // fixupPos6
let ovalsIdx2 = prim_u_int32 st // fixupPos7
ignore (len, otyconsIdx1, otyconsIdx2, otyparsIdx1, otyparsIdx2, ovalsIdx1, ovalsIdx2)
InterruptibleLazy.FromValue(u st)
let u_hole () =
let mutable h = None
(fun f -> h <- Some f), (fun st -> match h with Some f -> f st | None -> ufailwith st "u_hole: unfilled hole")
//---------------------------------------------------------------------------
// Pickle/unpickle F# interface data
//---------------------------------------------------------------------------
// Strings
// A huge number of these occur in pickled F# data, so make them unique
let encode_string stringTab x = encode_uniq stringTab x
let decode_string x = x
let lookup_string st stringTab x = lookup_uniq st stringTab x
let u_encoded_string = u_prim_string
let u_string st = lookup_uniq st st.istrings (u_int st)
let u_strings = u_list u_string
let p_encoded_string = p_prim_string
let p_string s st = p_int (encode_string st.ostrings s) st
let p_strings = p_list p_string
// CCU References
// A huge number of these occur in pickled F# data, so make them unique
let encode_ccuref ccuTab (x: CcuThunk) = encode_uniq ccuTab x.AssemblyName
let lookup_ccuref st ccuTab x = lookup_uniq st ccuTab x
let u_encoded_ccuref st =
match u_byte st with
| 0 -> u_prim_string st
| n -> ufailwith st ("u_encoded_ccuref: found number " + string n)
let u_ccuref st = lookup_uniq st st.iccus (u_int st)
let p_encoded_ccuref x st =
p_byte 0 st // leave a dummy tag to make room for future encodings of ccurefs
p_prim_string x st
let p_ccuref s st = p_int (encode_ccuref st.occus s) st
// References to public items in this module
// A huge number of these occur in pickled F# data, so make them unique
let decode_pubpath st stringTab a = PubPath(Array.map (lookup_string st stringTab) a)
let u_encoded_pubpath = u_array u_int
let u_pubpath st = lookup_uniq st st.ipubpaths (u_int st)
let encode_pubpath stringTab pubpathTab (PubPath a) = encode_uniq pubpathTab (Array.map (encode_string stringTab) a)
let p_encoded_pubpath = p_array p_int
let p_pubpath x st = p_int (encode_pubpath st.ostrings st.opubpaths x) st
// References to other modules
// A huge number of these occur in pickled F# data, so make them unique
let decode_nleref st ccuTab stringTab (a, b) = mkNonLocalEntityRef (lookup_ccuref st ccuTab a) (Array.map (lookup_string st stringTab) b)
let lookup_nleref st nlerefTab x = lookup_uniq st nlerefTab x
let u_encoded_nleref = u_tup2 u_int (u_array u_int)
let u_nleref st = lookup_uniq st st.inlerefs (u_int st)
let encode_nleref ccuTab stringTab nlerefTab thisCcu (nleref: NonLocalEntityRef) =
#if !NO_TYPEPROVIDERS
// Remap references to statically-linked Entity nodes in provider-generated entities to point to the current assembly.
// References to these nodes _do_ appear in F# assembly metadata, because they may be public.
let nleref =
match nleref.Deref.PublicPath with
| Some pubpath when nleref.Deref.IsProvidedGeneratedTycon ->
if verbose then dprintfn "remapping pickled reference to provider-generated type %s" nleref.Deref.DisplayNameWithStaticParameters
rescopePubPath thisCcu pubpath
| _ -> nleref
#else
ignore thisCcu
#endif
let (NonLocalEntityRef(a, b)) = nleref
encode_uniq nlerefTab (encode_ccuref ccuTab a, Array.map (encode_string stringTab) b)
let p_encoded_nleref = p_tup2 p_int (p_array p_int)
let p_nleref x st = p_int (encode_nleref st.occus st.ostrings st.onlerefs st.oscope x) st
// Simple types are types like "int", represented as TType(Ref_nonlocal(..., "int"), []).
// A huge number of these occur in pickled F# data, so make them unique.
//
// NULLNESS - the simpletyp table now holds KnownAmbivalentToNull by default.
// For old assemblies it is, if we give those assemblies the ambivalent interpretation.
// For new assemblies compiled with null-checking on it isn't, if the default is to give
// those the KnownWithoutNull interpretation by default.
let decode_simpletyp st _ccuTab _stringTab nlerefTab a = TType_app(ERefNonLocal (lookup_nleref st nlerefTab a), [], KnownAmbivalentToNull)
let u_encoded_simpletyp st = u_int st
let u_simpletyp st = lookup_uniq st st.isimpletys (u_int st)
let encode_simpletyp ccuTab stringTab nlerefTab simpleTyTab thisCcu a = encode_uniq simpleTyTab (encode_nleref ccuTab stringTab nlerefTab thisCcu a)
let p_encoded_simpletyp x st = p_int x st
let p_simpletyp x st = p_int (encode_simpletyp st.occus st.ostrings st.onlerefs st.osimpletys st.oscope x) st
/// Arbitrary value
[<Literal>]
let PickleBufferCapacity = 50000
let pickleObjWithDanglingCcus inMem file g scope p x =
let st1 =
{ os = ByteBuffer.Create(PickleBufferCapacity, useArrayPool = true)
osB = ByteBuffer.Create(PickleBufferCapacity, useArrayPool = true)
oscope=scope
occus= Table<_>.Create "occus"
oentities=NodeOutTable<_, _>.Create((fun (tc: Tycon) -> tc.Stamp), (fun tc -> tc.LogicalName), (fun tc -> tc.Range), id , "otycons")
otypars=NodeOutTable<_, _>.Create((fun (tp: Typar) -> tp.Stamp), (fun tp -> tp.DisplayName), (fun tp -> tp.Range), id , "otypars")
ovals=NodeOutTable<_, _>.Create((fun (v: Val) -> v.Stamp), (fun v -> v.LogicalName), (fun v -> v.Range), id , "ovals")
oanoninfos=NodeOutTable<_, _>.Create((fun (v: AnonRecdTypeInfo) -> v.Stamp), (fun v -> string v.IlTypeName), (fun _ -> range0), id, "oanoninfos")
ostrings=Table<_>.Create "ostrings"
onlerefs=Table<_>.Create "onlerefs"
opubpaths=Table<_>.Create "opubpaths"
osimpletys=Table<_>.Create "osimpletys"
oglobals=g
ofile=file
oInMem=inMem
isStructThisArgPos = false }
let ccuNameTab,(ntycons, ntypars, nvals, nanoninfos),stringTab,pubpathTab,nlerefTab,simpleTyTab,phase1bytes,phase1bytesB =
p x st1
let sizes =
st1.oentities.Size,
st1.otypars.Size,
st1.ovals.Size,
st1.oanoninfos.Size
st1.occus, sizes, st1.ostrings, st1.opubpaths, st1.onlerefs, st1.osimpletys, st1.os.AsMemory(), st1.osB
let st2 =
{ os = ByteBuffer.Create(PickleBufferCapacity, useArrayPool = true)
osB = ByteBuffer.Create(PickleBufferCapacity, useArrayPool = true)
oscope=scope
occus= Table<_>.Create "occus (fake)"
oentities=NodeOutTable<_, _>.Create((fun (tc: Tycon) -> tc.Stamp), (fun tc -> tc.LogicalName), (fun tc -> tc.Range), id, "otycons")
otypars=NodeOutTable<_, _>.Create((fun (tp: Typar) -> tp.Stamp), (fun tp -> tp.DisplayName), (fun tp -> tp.Range), id, "otypars")
ovals=NodeOutTable<_, _>.Create((fun (v: Val) -> v.Stamp), (fun v -> v.LogicalName), (fun v -> v.Range), (fun osgn -> osgn), "ovals")
oanoninfos=NodeOutTable<_, _>.Create((fun (v: AnonRecdTypeInfo) -> v.Stamp), (fun v -> string v.IlTypeName), (fun _ -> range0), id, "oanoninfos")
ostrings=Table<_>.Create "ostrings (fake)"
opubpaths=Table<_>.Create "opubpaths (fake)"
onlerefs=Table<_>.Create "onlerefs (fake)"
osimpletys=Table<_>.Create "osimpletys (fake)"
oglobals=g
ofile=file
oInMem=inMem
isStructThisArgPos = false }
let phase2bytes =
p_array p_encoded_ccuref ccuNameTab.AsArray st2
// Add a 4th integer indicated by a negative 1st integer
let z1 = if nanoninfos > 0 then -ntycons-1 else ntycons
p_int z1 st2
p_tup2 p_int p_int (ntypars, nvals) st2
if nanoninfos > 0 then
p_int nanoninfos st2
p_tup5
(p_array p_encoded_string)
(p_array p_encoded_pubpath)
(p_array p_encoded_nleref)
(p_array p_encoded_simpletyp)
p_memory
(stringTab.AsArray, pubpathTab.AsArray, nlerefTab.AsArray, simpleTyTab.AsArray, phase1bytes)
st2
// The B stream should be empty in the second phase
let phase2bytesB = st2.osB.AsMemory()
if phase2bytesB.Length <> 0 then failwith "expected phase2bytesB.Length = 0"
(st2.osB :> System.IDisposable).Dispose()
st2.os
(st1.os :> System.IDisposable).Dispose()
phase2bytes, phase1bytesB
let check (ilscope: ILScopeRef) (inMap: NodeInTable<_,_>) =
for i = 0 to inMap.Count - 1 do
let n = inMap.Get i
if not (inMap.IsLinked n) then
warning(Error(FSComp.SR.pickleMissingDefinition (i, inMap.Name, ilscope.QualifiedName), range0))
// Note for compiler developers: to get information about which item this index relates to,
// enable the conditional in Pickle.p_osgn_ref to refer to the given index number and recompile
// an identical copy of the source for the DLL containing the data being unpickled. A message will
// then be printed indicating the name of the item.
let unpickleObjWithDanglingCcus file viewedScope (ilModule: ILModuleDef option) u (phase2bytes: ReadOnlyByteMemory) (phase1bytesB: ReadOnlyByteMemory) =
let st2 =
{ is = ByteStream.FromBytes (phase2bytes, 0, phase2bytes.Length)
isB = ByteStream.FromBytes (ByteMemory.FromArray([| |]).AsReadOnly(), 0, 0)
iilscope = viewedScope
iccus = new_itbl "iccus (fake)" [| |]
ientities = NodeInTable<_, _>.Create (Tycon.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "itycons", 0)
itypars = NodeInTable<_, _>.Create (Typar.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "itypars", 0)
ivals = NodeInTable<_, _>.Create (Val.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "ivals", 0)
ianoninfos = NodeInTable<_, _>.Create(AnonRecdTypeInfo.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "ianoninfos", 0)
istrings = new_itbl "istrings (fake)" [| |]
inlerefs = new_itbl "inlerefs (fake)" [| |]
ipubpaths = new_itbl "ipubpaths (fake)" [| |]
isimpletys = new_itbl "isimpletys (fake)" [| |]
ifile = file
iILModule = ilModule }
let ccuNameTab = u_array u_encoded_ccuref st2
let z1 = u_int st2
let ntycons = if z1 < 0 then -z1-1 else z1
let ntypars, nvals = u_tup2 u_int u_int st2
let nanoninfos = if z1 < 0 then u_int st2 else 0
let stringTab, pubpathTab, nlerefTab, simpleTyTab, phase1bytes =
u_tup5
(u_array u_encoded_string)
(u_array u_encoded_pubpath)
(u_array u_encoded_nleref)
(u_array u_encoded_simpletyp)
u_byte_memory
st2
let ccuTab = new_itbl "iccus" (Array.map CcuThunk.CreateDelayed ccuNameTab)
let stringTab = new_itbl "istrings" (Array.map decode_string stringTab)
let pubpathTab = new_itbl "ipubpaths" (Array.map (decode_pubpath st2 stringTab) pubpathTab)
let nlerefTab = new_itbl "inlerefs" (Array.map (decode_nleref st2 ccuTab stringTab) nlerefTab)
let simpletypTab = new_itbl "simpleTyTab" (Array.map (decode_simpletyp st2 ccuTab stringTab nlerefTab) simpleTyTab)
let data =
let st1 =
{ is = ByteStream.FromBytes (phase1bytes, 0, phase1bytes.Length)
isB = ByteStream.FromBytes (phase1bytesB, 0, phase1bytesB.Length)
iccus = ccuTab
iilscope = viewedScope
ientities = NodeInTable<_, _>.Create(Tycon.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "itycons", ntycons)
itypars = NodeInTable<_, _>.Create(Typar.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "itypars", ntypars)
ivals = NodeInTable<_, _>.Create(Val.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "ivals", nvals)
ianoninfos = NodeInTable<_, _>.Create(AnonRecdTypeInfo.NewUnlinked, (fun osgn tg -> osgn.Link tg), (fun osgn -> osgn.IsLinked), "ianoninfos", nanoninfos)
istrings = stringTab
ipubpaths = pubpathTab
inlerefs = nlerefTab
isimpletys = simpletypTab
ifile = file
iILModule = ilModule }
let res = u st1
check viewedScope st1.ientities
check viewedScope st1.ientities
check viewedScope st1.ivals
check viewedScope st1.itypars
res
{RawData=data; FixupThunks=ccuTab.itbl_rows }
//=========================================================================
// PART II
//=========================================================================
//---------------------------------------------------------------------------
// Pickle/unpickle for Abstract IL data, up to IL instructions
//---------------------------------------------------------------------------
let p_ILPublicKey x st =
match x with
| PublicKey b -> p_byte 0 st; p_bytes b st
| PublicKeyToken b -> p_byte 1 st; p_bytes b st
let p_ILVersion (x: ILVersionInfo) st = p_tup4 p_uint16 p_uint16 p_uint16 p_uint16 (x.Major, x.Minor, x.Build, x.Revision) st
let p_ILModuleRef (x: ILModuleRef) st =
p_tup3 p_string p_bool (p_option p_bytes) (x.Name, x.HasMetadata, x.Hash) st
let p_ILAssemblyRef (x: ILAssemblyRef) st =
p_byte 0 st // leave a dummy tag to make room for future encodings of assembly refs
p_tup6 p_string (p_option p_bytes) (p_option p_ILPublicKey) p_bool (p_option p_ILVersion) (p_option p_string)
( x.Name, x.Hash, x.PublicKey, x.Retargetable, x.Version, x.Locale) st
let p_ILScopeRef x st =
match x with
| ILScopeRef.Local -> p_byte 0 st
| ILScopeRef.Module mref -> p_byte 1 st; p_ILModuleRef mref st
| ILScopeRef.Assembly aref -> p_byte 2 st; p_ILAssemblyRef aref st
// Encode primary assembly as a normal assembly ref
| ILScopeRef.PrimaryAssembly -> p_byte 2 st; p_ILAssemblyRef st.oglobals.ilg.primaryAssemblyRef st
let u_ILPublicKey st =
let tag = u_byte st
match tag with
| 0 -> u_bytes st |> PublicKey
| 1 -> u_bytes st |> PublicKeyToken
| _ -> ufailwith st "u_ILPublicKey"
let u_ILVersion st =
let major, minor, build, revision = u_tup4 u_uint16 u_uint16 u_uint16 u_uint16 st
ILVersionInfo(major, minor, build, revision)
let u_ILModuleRef st =
let a, b, c = u_tup3 u_string u_bool (u_option u_bytes) st
ILModuleRef.Create(a, b, c)
let u_ILAssemblyRef st =
let tag = u_byte st
match tag with
| 0 ->
let a, b, c, d, e, f = u_tup6 u_string (u_option u_bytes) (u_option u_ILPublicKey) u_bool (u_option u_ILVersion) (u_option u_string) st
ILAssemblyRef.Create(a, b, c, d, e, f)
| _ -> ufailwith st "u_ILAssemblyRef"
// IL scope references are rescoped as they are unpickled. This means
// the pickler accepts IL fragments containing ILScopeRef.Local and adjusts them
// to be absolute scope references.
let u_ILScopeRef st =
let res =
let tag = u_byte st
match tag with
| 0 -> ILScopeRef.Local
| 1 -> u_ILModuleRef st |> ILScopeRef.Module
| 2 -> u_ILAssemblyRef st |> ILScopeRef.Assembly
| _ -> ufailwith st "u_ILScopeRef"
let res = rescopeILScopeRef st.iilscope res
res
let p_ILHasThis x st =
p_byte (match x with
| ILThisConvention.Instance -> 0
| ILThisConvention.InstanceExplicit -> 1
| ILThisConvention.Static -> 2) st
let p_ILArrayShape = p_wrap (fun (ILArrayShape x) -> x) (p_list (p_tup2 (p_option p_int32) (p_option p_int32)))
let rec p_ILType ty st =
match ty with
| ILType.Void -> p_byte 0 st
| ILType.Array (shape, ty) -> p_byte 1 st; p_tup2 p_ILArrayShape p_ILType (shape, ty) st
| ILType.Value tspec -> p_byte 2 st; p_ILTypeSpec tspec st
| ILType.Boxed tspec -> p_byte 3 st; p_ILTypeSpec tspec st
| ILType.Ptr ty -> p_byte 4 st; p_ILType ty st
| ILType.Byref ty -> p_byte 5 st; p_ILType ty st
| ILType.FunctionPointer csig -> p_byte 6 st; p_ILCallSig csig st
| ILType.TypeVar n -> p_byte 7 st; p_uint16 n st
| ILType.Modified (req, tref, ty) -> p_byte 8 st; p_tup3 p_bool p_ILTypeRef p_ILType (req, tref, ty) st
and p_ILTypes tys = p_list p_ILType tys
and p_ILBasicCallConv x st =
p_byte (match x with
| ILArgConvention.Default -> 0
| ILArgConvention.CDecl -> 1
| ILArgConvention.StdCall -> 2
| ILArgConvention.ThisCall -> 3
| ILArgConvention.FastCall -> 4
| ILArgConvention.VarArg -> 5) st
and p_ILCallConv (Callconv(x, y)) st = p_tup2 p_ILHasThis p_ILBasicCallConv (x, y) st
and p_ILCallSig x st = p_tup3 p_ILCallConv p_ILTypes p_ILType (x.CallingConv, x.ArgTypes, x.ReturnType) st
and p_ILTypeRef (x: ILTypeRef) st = p_tup3 p_ILScopeRef p_strings p_string (x.Scope, x.Enclosing, x.Name) st
and p_ILTypeSpec (a: ILTypeSpec) st = p_tup2 p_ILTypeRef p_ILTypes (a.TypeRef, a.GenericArgs) st
let u_ILBasicCallConv st =
match u_byte st with
| 0 -> ILArgConvention.Default
| 1 -> ILArgConvention.CDecl
| 2 -> ILArgConvention.StdCall
| 3 -> ILArgConvention.ThisCall
| 4 -> ILArgConvention.FastCall
| 5 -> ILArgConvention.VarArg
| _ -> ufailwith st "u_ILBasicCallConv"
let u_ILHasThis st =
match u_byte st with
| 0 -> ILThisConvention.Instance
| 1 -> ILThisConvention.InstanceExplicit
| 2 -> ILThisConvention.Static
| _ -> ufailwith st "u_ILHasThis"
let u_ILCallConv st = let a, b = u_tup2 u_ILHasThis u_ILBasicCallConv st in Callconv(a, b)
let u_ILTypeRef st = let a, b, c = u_tup3 u_ILScopeRef u_strings u_string st in ILTypeRef.Create(a, b, c)
let u_ILArrayShape = u_wrap (ILArrayShape) (u_list (u_tup2 (u_option u_int32) (u_option u_int32)))
let rec u_ILType st =