Debian Codegraph @

kernel/vmlibrary.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Names
12 open Vmemitcodes
13
14 exception Faulty of string
15
16 (* TODO: share with Library *)
17
18 module Delayed :
19 sig
20
21 type 'a t
22 val make : file:string -> ObjFile.in_handle -> segment:'a ObjFile.id -> 'a t
23 val return : 'a -> 'a t
24 val eval : 'a t -> 'a
25
26 end =
27 struct
28
29 type 'a delayed = {
30 del_file : string;
31 del_off : int64;
32 del_digest : Digest.t;
33 }
34
35 type 'a node = ToFetch of 'a delayed | Fetched of 'a
36
37 type 'a t = 'a node ref
38
39 let in_delayed f ch ~segment =
40 let seg = ObjFile.get_segment ch ~segment in
41 let digest = seg.ObjFile.hash in
42 { del_file = f; del_digest = digest; del_off = seg.ObjFile.pos; }
43
44 let make ~file ch ~segment =
45 let del = in_delayed file ch ~segment in
46 ref (ToFetch del)
47
48 (** Fetching a table of opaque terms at position [pos] in file [f],
49 expecting to find first a copy of [digest]. *)
50
51 let fetch_delayed del =
52 let { del_digest = digest; del_file = f; del_off = pos; } = del in
53 let ch = open_in_bin f in
54 let obj, digest' =
55 try
56 let () = LargeFile.seek_in ch pos in
57 let obj = System.marshal_in f ch in
58 let digest' = Digest.input ch in
59 obj, digest'
60 with e -> close_in ch; raise e
61 in
62 close_in ch;
63 if not (String.equal digest digest') then raise (Faulty f);
64 obj
65
66 let eval r = match !r with
67 | Fetched v -> v
68 | ToFetch del ->
69 let v = fetch_delayed del in
70 let () = r := Fetched v in
71 v
72
73 let return v = ref (Fetched v)
74
75 end
76
77 module VmTable =
78 struct
79
80 type t = {
81 table_len : int;
82 table_dir : DirPath.t;
83 table_val : to_patch Int.Map.t;
84 }
85
86 type index = DirPath.t * int
87
88 let empty = {
89 table_len = 0;
90 table_dir = DirPath.dummy;
91 table_val = Int.Map.empty;
92 }
93
94 let set_path dp tab =
95 let () = assert (DirPath.equal tab.table_dir DirPath.dummy) in
96 { tab with table_dir = dp }
97
98 let create code tab =
99 let id = tab.table_len in
100 let dp = tab.table_dir in
101 let () = assert (not @@ DirPath.equal dp DirPath.dummy) in
102 let table_val = Int.Map.add id code tab.table_val in
103 let ntab = { table_dir = dp; table_len = id + 1; table_val } in
104 ntab, (dp, id)
105
106 end
107
108 type compiled_library = {
109 lib_dp : DirPath.t;
110 lib_data : to_patch array;
111 }
112
113 type on_disk = DirPath.t * compiled_library Delayed.t
114
115 let inject lib =
116 (lib.lib_dp, Delayed.return lib)
117
118 type t = {
119 local : VmTable.t;
120 foreign : compiled_library Delayed.t DirPath.Map.t;
121 }
122
123 type index = VmTable.index
124
125 type indirect_code = VmTable.index pbody_code
126
127 let empty = {
128 local = VmTable.empty;
129 foreign = DirPath.Map.empty;
130 }
131
132 let set_path dp lib =
133 let local = VmTable.set_path dp lib.local in
134 { lib with local }
135
136 let add code lib =
137 let tab, idx = VmTable.create code lib.local in
138 let lib = { lib with local = tab } in
139 lib, idx
140
141 let vm_segment : compiled_library ObjFile.id = ObjFile.make_id "vmlibrary"
142
143 let load dp ~file ch =
144 (dp, Delayed.make ~file ~segment:vm_segment ch : on_disk)
145
146 let link (dp, m) libs =
147 let () = assert (not @@ DirPath.Map.mem dp libs.foreign) in
148 { libs with foreign = DirPath.Map.add dp m libs.foreign }
149
150 let missing_index dp i =
151 CErrors.anomaly Pp.(str "Missing VM index " ++ int i ++
152 str " in library " ++ DirPath.print dp)
153
154 let resolve (dp, i) libs =
155 if DirPath.equal dp libs.local.VmTable.table_dir then
156 match Int.Map.find i libs.local.VmTable.table_val with
157 | v -> v
158 | exception Not_found -> missing_index dp i
159 else match DirPath.Map.find dp libs.foreign with
160 | tab ->
161 let tab = Delayed.eval tab in
162 tab.lib_data.(i)
163 | exception Not_found ->
164 CErrors.anomaly Pp.(str "Missing VM table for library " ++
165 DirPath.print dp)
166
167 let export lib =
168 let local = lib.local in
169 let init i = Int.Map.find i local.VmTable.table_val in
170 let data = Array.init local.VmTable.table_len init in
171 { lib_dp = local.VmTable.table_dir; lib_data = data }