Debian Codegraph @

kernel/vmsymtable.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 (* Created by Bruno Barras for Benjamin Grégoire as part of the
12 bytecode-based reduction machine, Oct 2004 *)
13 (* Bug fix #1419 by Jean-Marc Notin, Mar 2007 *)
14
15 (* This file manages the table of global symbols for the bytecode machine *)
16
17 open Util
18 open Names
19 open Vmvalues
20 open Vmemitcodes
21 open Vmbytecodes
22 open Declarations
23 open Environ
24 open Vmbytegen
25
26 module NamedDecl = Context.Named.Declaration
27 module RelDecl = Context.Rel.Declaration
28
29 type vm_global = values array
30
31 (* interpreter *)
32 external rocq_interprete : tcode -> values -> atom array -> vm_global -> Vmvalues.vm_env -> int -> values =
33 "rocq_interprete_byte" "rocq_interprete_ml"
34
35 (* table for structured constants and switch annotations *)
36
37 module HashMap (M : Hashtbl.HashedType) :
38 sig
39 type +'a t
40 val empty : 'a t
41 val add : M.t -> 'a -> 'a t -> 'a t
42 val find : M.t -> 'a t -> 'a
43 end =
44 struct
45 type 'a t = (M.t * 'a) list Int.Map.t
46 let empty = Int.Map.empty
47 let add k v m =
48 let hash = M.hash k in
49 try Int.Map.modify hash (fun _ l -> (k, v) :: l) m
50 with Not_found -> Int.Map.add hash [k, v] m
51 let find k m =
52 let hash = M.hash k in
53 let l = Int.Map.find hash m in
54 List.assoc_f M.equal k l
55 end
56
57 module SConstTable = HashMap (struct
58 type t = structured_constant
59 let equal = eq_structured_constant
60 let hash = hash_structured_constant
61 end)
62
63 module AnnotTable = HashMap (struct
64 type t = annot_switch
65 let equal = eq_annot_switch
66 let hash = hash_annot_switch
67 end)
68
69 module GlobVal :
70 sig
71 type key = int
72 type t
73 val empty : int -> t
74 val push : t -> values -> key * t
75 val vm_global : t -> vm_global
76 (** Warning: due to sharing, the resulting value can be modified by
77 persistent operations. When calling this function one must ensure
78 that no other context modification is performed before the value drops
79 out of scope. *)
80 end =
81 struct
82 type key = int
83
84 type view =
85 | Root of values array
86 | DSet of int * values * view ref
87
88 type t = {
89 data : view ref;
90 size : int;
91 (** number of actual elements in the array *)
92 }
93
94 let empty n = {
95 data = ref (Root (Array.make n crazy_val));
96 size = 0;
97 }
98
99 let rec rerootk t k = match !t with
100 | Root _ -> k ()
101 | DSet (i, v, t') ->
102 let next () = match !t' with
103 | Root a as n ->
104 let v' = Array.unsafe_get a i in
105 let () = Array.unsafe_set a i v in
106 let () = t := n in
107 let () = t' := DSet (i, v', t) in
108 k ()
109 | DSet _ -> assert false
110 in
111 rerootk t' next
112
113 let reroot t = rerootk t (fun () -> ())
114
115 let push { data = t; size = i } v =
116 let () = reroot t in
117 match !t with
118 | DSet _ -> assert false
119 | Root a as n ->
120 let len = Array.length a in
121 let data =
122 if i < len then
123 let old = Array.unsafe_get a i in
124 if old == v then t
125 else
126 let () = Array.unsafe_set a i v in
127 let res = ref n in
128 let () = t := DSet (i, old, res) in
129 res
130 else
131 let nlen = 2 * len + 1 in
132 let nlen = min nlen Sys.max_array_length in
133 let () = assert (i < nlen) in
134 let a' = Array.make nlen crazy_val in
135 let () = Array.blit a 0 a' 0 len in
136 let () = Array.unsafe_set a' i v in
137 let res = ref (Root a') in
138 let () = t := DSet (i, crazy_val, res) in
139 res
140 in
141 (i, { data; size = i + 1 })
142
143 let vm_global { data; _ } =
144 let () = reroot data in
145 match !data with
146 | DSet _ -> assert false
147 | Root a -> (a : vm_global)
148
149 end
150
151
152 (****************)
153 (* Code linking *)
154 (****************)
155
156 (* Global Table *)
157
158 (** [global_table] contains values of global constants, switch annotations,
159 and structured constants. *)
160
161 type global_table = {
162 glob_val : GlobVal.t;
163 glob_sconst : GlobVal.key SConstTable.t;
164 glob_annot : GlobVal.key AnnotTable.t;
165 glob_prim : GlobVal.key array;
166 }
167
168 let get_global_data table = GlobVal.vm_global table.glob_val
169
170 let set_global v rtable =
171 let table = !rtable in
172 let (n, glob_val) = GlobVal.push table.glob_val v in
173 let table = { table with glob_val } in
174 let () = rtable := table in
175 n
176
177 (*************************************************************)
178 (*** Mise a jour des valeurs des variables et des constantes *)
179 (*************************************************************)
180
181 exception NotEvaluated
182
183 let key rk =
184 match !rk with
185 | None -> raise NotEvaluated
186 | Some k ->
187 try CEphemeron.get k
188 with CEphemeron.InvalidKey -> raise NotEvaluated
189
190 (************************)
191 (* traduction des patch *)
192
193 (* slot_for_*, calcul la valeur de l'objet, la place
194 dans la table global, rend sa position dans la table *)
195
196 let slot_for_str_cst key table =
197 try SConstTable.find key !table.glob_sconst
198 with Not_found ->
199 let n = set_global (val_of_str_const key) table in
200 let glob_sconst = SConstTable.add key n !table.glob_sconst in
201 let () = table := { !table with glob_sconst } in
202 n
203
204 let slot_for_annot key table =
205 try AnnotTable.find key !table.glob_annot
206 with Not_found ->
207 let n = set_global (val_of_annot_switch key) table in
208 let glob_annot = AnnotTable.add key n !table.glob_annot in
209 let () = table := { !table with glob_annot } in
210 n
211
212 (* Initialization of OCaml primitives *)
213 let eval_caml_prim = function
214 | CAML_Arraymake -> 0, Vmvalues.parray_make
215 | CAML_Arrayget -> 1, Vmvalues.parray_get
216 | CAML_Arraydefault -> 2, Vmvalues.parray_get_default
217 | CAML_Arrayset -> 3, Vmvalues.parray_set
218 | CAML_Arraycopy -> 4, Vmvalues.parray_copy
219 | CAML_Arraylength -> 5, Vmvalues.parray_length
220 | CAML_Stringmake -> 6, Vmvalues.pstring_make
221 | CAML_Stringlength -> 7, Vmvalues.pstring_length
222 | CAML_Stringget -> 8, Vmvalues.pstring_get
223 | CAML_Stringsub -> 9, Vmvalues.pstring_sub
224 | CAML_Stringcat -> 10, Vmvalues.pstring_cat
225 | CAML_Stringcompare -> 11, Vmvalues.pstring_compare
226
227 let make_static_prim table =
228 let fold table prim =
229 let _, v = eval_caml_prim prim in
230 let key, table = GlobVal.push table v in
231 table, key
232 in
233 (* Keep synchronized *)
234 let prims = [
235 CAML_Arraymake;
236 CAML_Arrayget;
237 CAML_Arraydefault;
238 CAML_Arrayset;
239 CAML_Arraycopy;
240 CAML_Arraylength;
241 CAML_Stringmake;
242 CAML_Stringlength;
243 CAML_Stringget;
244 CAML_Stringsub;
245 CAML_Stringcat;
246 CAML_Stringcompare;
247 ] in
248 let table, prims = CList.fold_left_map fold table prims in
249 table, Array.of_list prims
250
251 let slot_for_caml_prim op table =
252 !table.glob_prim.(fst (eval_caml_prim op))
253
254 type envcache = {
255 named_cache : values Id.Map.t ref;
256 rel_cache : values Int.Map.t ref;
257 rel_adjust : int;
258 }
259
260 let cache_named envcache x v = envcache.named_cache := Id.Map.add x v !(envcache.named_cache)
261 let cache_rel envcache i v = envcache.rel_cache := Int.Map.add (i + envcache.rel_adjust) v !(envcache.rel_cache)
262
263 let envcache_of_rel i envcache = {
264 envcache with
265 rel_adjust = envcache.rel_adjust + i
266 }
267
268 let rec slot_for_getglobal env sigma kn envcache table =
269 let cb = CClosure.lookup_constant_handler env sigma.Genlambda.evars_val kn in
270 let rk =
271 if Environ.mem_constant kn env then
272 let (_, (_, rk),_) = lookup_constant_key kn env in
273 rk
274 else
275 ref None
276 in
277 try key rk
278 with NotEvaluated ->
279 let pos = match cb.const_body_code with
280 | BCdefined (_, index, patches) ->
281 let code = index () in
282 let code = (code, patches) in
283 let v = eval_to_patch env sigma code envcache table in
284 set_global v table
285 | BCalias kn' -> slot_for_getglobal env sigma kn' envcache table
286 | BCconstant -> set_global (val_of_constant kn) table
287 in
288 rk := Some (CEphemeron.create pos);
289 pos
290
291 and slot_for_fv env sigma fv envcache table =
292 let val_of_rel i = val_of_rel (nb_rel env - i) in
293 match fv with
294 | FVnamed id ->
295 let nv = Id.Map.find_opt id !(envcache.named_cache) in
296 begin match nv with
297 | None ->
298 let v = match env |> lookup_named id |> NamedDecl.get_value with
299 | None -> val_of_named id
300 | Some c -> val_of_constr env sigma c envcache table
301 in
302 cache_named envcache id v; v
303 | Some v -> v
304 end
305 | FVrel i ->
306 let rv = Int.Map.find_opt (i + envcache.rel_adjust) !(envcache.rel_cache) in
307 begin match rv with
308 | None ->
309 let v = match env |> lookup_rel i |> RelDecl.get_value with
310 | None -> val_of_rel i
311 | Some c -> val_of_constr (env_of_rel i env) sigma c (envcache_of_rel i envcache) table
312 in
313 cache_rel envcache i v; v
314 | Some v -> v
315 end
316
317 and eval_to_patch env sigma code envcache table =
318 let slots = function
319 | Reloc_annot a -> slot_for_annot a table
320 | Reloc_const sc -> slot_for_str_cst sc table
321 | Reloc_getglobal kn -> slot_for_getglobal env sigma kn envcache table
322 | Reloc_caml_prim op -> slot_for_caml_prim op table
323 in
324 let tc, fv = patch code slots in
325 let vm_env =
326 (* Environment should look like a closure, so free variables start at slot 2. *)
327 let a = Array.make (Array.length fv + 2) crazy_val in
328 a.(1) <- Obj.magic 2;
329 let iter i fv =
330 let v = slot_for_fv env sigma fv envcache table in
331 a.(i + 2) <- v
332 in
333 let () = Array.iteri iter fv in
334 a
335 in
336 let global = get_global_data !table in
337 let v = rocq_interprete tc crazy_val (get_atom_rel ()) global (inj_env vm_env) 0 in
338 v
339
340 and val_of_constr env sigma c envcache table =
341 match compile ~fail_on_error:true env sigma c with
342 | Some (_, code, patch) -> eval_to_patch env sigma (code, patch) envcache table
343 | None -> assert false
344
345 let global_table =
346 let glob_val = GlobVal.empty 4096 in
347 (* Register OCaml primitives statically *)
348 let glob_val, glob_prim = make_static_prim glob_val in
349 ref {
350 glob_val; glob_prim;
351 glob_sconst = SConstTable.empty;
352 glob_annot = AnnotTable.empty;
353 }
354
355 let fresh_envcache () = {
356 named_cache = ref Id.Map.empty;
357 rel_cache = ref Int.Map.empty;
358 rel_adjust = 0;
359 }
360
361 let val_of_constr env sigma c =
362 let v = val_of_constr env sigma c (fresh_envcache ()) global_table in
363 v
364
365 let vm_interp code v env k =
366 rocq_interprete code v (get_atom_rel ()) (get_global_data !global_table) env k