Debian Codegraph @

kernel/vmvalues.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 open Names
11 open Values
12
13 (********************************************)
14 (* Initialization of the abstract machine ***)
15 (* Necessary for [relaccu_tbl] *)
16 (********************************************)
17
18 external init_vm : unit -> unit = "init_rocq_vm"
19
20 let _ = init_vm ()
21
22 (******************************************************)
23 (* Abstract data types and utility functions **********)
24 (******************************************************)
25
26 (* The representation of values relies on this assertion *)
27 let _ = assert (Int.equal Obj.first_non_constant_constructor_tag 0)
28
29 (* Values of the abstract machine *)
30 type values
31 type structured_values = values
32 let val_of_obj v = ((Obj.obj v):values)
33 let crazy_val = (val_of_obj (Obj.repr 0))
34
35 type tag = int
36
37 let type_atom_tag = 2
38 let max_atom_tag = 2
39 let proj_tag = 3
40 let fix_app_tag = 4
41 let switch_tag = 5
42 let cofix_tag = 6
43 let cofix_evaluated_tag = 7
44
45 (** Structured constants are constants whose construction is done once. Their
46 occurrences share the same value modulo kernel name substitutions (for functor
47 application). Structured values have the additional property that no
48 substitution will need to be performed, so their runtime value can directly be
49 shared without reallocating a more structured representation. *)
50 type structured_constant =
51 | Const_sort of Sorts.t
52 | Const_ind of inductive
53 | Const_evar of Evar.t
54 | Const_b0 of tag
55 | Const_univ_instance of UVars.Instance.t
56 | Const_val of structured_values
57 | Const_uint of Uint63.t
58 | Const_float of Float64.t
59 | Const_string of Pstring.t
60
61 type reloc_table = (tag * int) array
62
63 (** When changing this, adapt Is_tailrec_switch in rocq_values.h accordingly *)
64 type annot_switch =
65 { rtbl : reloc_table; tailcall : bool; max_stack_size : int }
66
67 let rec eq_structured_values v1 v2 =
68 v1 == v2 ||
69 let o1 = Obj.repr v1 in
70 let o2 = Obj.repr v2 in
71 if Obj.is_int o1 && Obj.is_int o2 then o1 == o2
72 else
73 let t1 = Obj.tag o1 in
74 let t2 = Obj.tag o2 in
75 if Int.equal t1 t2 &&
76 Int.equal (Obj.size o1) (Obj.size o2)
77 then if Int.equal t1 Obj.custom_tag
78 then Int64.equal (Obj.magic v1 : int64) (Obj.magic v2 : int64)
79 else if Int.equal t1 Obj.double_tag
80 then Float64.(equal (of_float (Obj.magic v1)) (of_float (Obj.magic v2)))
81 else if Int.equal t1 Obj.string_tag
82 then String.equal (Obj.magic v1) (Obj.magic v2)
83 else begin
84 assert (t1 <= Obj.last_non_constant_constructor_tag &&
85 t2 <= Obj.last_non_constant_constructor_tag);
86 let i = ref 0 in
87 while (!i < Obj.size o1 && eq_structured_values
88 (Obj.magic (Obj.field o1 !i) : structured_values)
89 (Obj.magic (Obj.field o2 !i) : structured_values)) do
90 incr i
91 done;
92 !i >= Obj.size o1
93 end
94 else false
95
96 let hash_structured_values (v : structured_values) =
97 (* We may want a better hash function here *)
98 Hashtbl.hash v
99
100 let eq_structured_constant c1 c2 = match c1, c2 with
101 | Const_sort s1, Const_sort s2 -> Sorts.equal s1 s2
102 | Const_sort _, _ -> false
103 | Const_ind i1, Const_ind i2 -> Ind.CanOrd.equal i1 i2
104 | Const_ind _, _ -> false
105 | Const_evar e1, Const_evar e2 -> Evar.equal e1 e2
106 | Const_evar _, _ -> false
107 | Const_b0 t1, Const_b0 t2 -> Int.equal t1 t2
108 | Const_b0 _, _ -> false
109 | Const_univ_instance u1 , Const_univ_instance u2 -> UVars.Instance.equal u1 u2
110 | Const_univ_instance _ , _ -> false
111 | Const_val v1, Const_val v2 -> eq_structured_values v1 v2
112 | Const_val _, _ -> false
113 | Const_uint i1, Const_uint i2 -> Uint63.equal i1 i2
114 | Const_uint _, _ -> false
115 | Const_float f1, Const_float f2 -> Float64.equal f1 f2
116 | Const_float _, _ -> false
117 | Const_string s1, Const_string s2 -> Pstring.equal s1 s2
118 | Const_string _, _ -> false
119
120 let hash_structured_constant c =
121 let open Hashset.Combine in
122 match c with
123 | Const_sort s -> combinesmall 1 (Sorts.hash s)
124 | Const_ind i -> combinesmall 2 (Ind.CanOrd.hash i)
125 | Const_evar e -> combinesmall 3 (Evar.hash e)
126 | Const_b0 t -> combinesmall 4 (Int.hash t)
127 | Const_univ_instance u -> combinesmall 5 (UVars.Instance.hash u)
128 | Const_val v -> combinesmall 6 (hash_structured_values v)
129 | Const_uint i -> combinesmall 7 (Uint63.hash i)
130 | Const_float f -> combinesmall 8 (Float64.hash f)
131 | Const_string s -> combinesmall 9 (Pstring.hash s)
132
133 let eq_annot_switch asw1 asw2 =
134 let eq_rlc (i1, j1) (i2, j2) = Int.equal i1 i2 && Int.equal j1 j2 in
135 CArray.equal eq_rlc asw1.rtbl asw2.rtbl &&
136 (asw1.tailcall : bool) == asw2.tailcall &&
137 Int.equal asw1.max_stack_size asw2.max_stack_size
138
139 let hash_annot_switch asw =
140 let open Hashset.Combine in
141 let h1 = Array.fold_left (fun h (t, i) -> combine3 h t i) 0 asw.rtbl in
142 let h2 = if asw.tailcall then 1 else 0 in
143 combine3 h1 h2 asw.max_stack_size
144
145 let pp_sort s =
146 let open Sorts in
147 match s with
148 | SProp -> Pp.str "SProp"
149 | Prop -> Pp.str "Prop"
150 | Set -> Pp.str "Set"
151 | Type u -> Pp.(str "Type@{" ++ Univ.Universe.raw_pr u ++ str "}")
152 | QSort (q, u) ->
153 Pp.(str "QSort@{" ++ (Sorts.QVar.raw_pr q) ++ strbrk ", " ++ Univ.Universe.raw_pr u ++ str "}")
154
155 let pp_struct_const = function
156 | Const_sort s -> pp_sort s
157 | Const_ind (mind, i) -> Pp.(MutInd.print mind ++ str"#" ++ int i)
158 | Const_evar e -> Pp.( str "Evar(" ++ int (Evar.repr e) ++ str ")")
159 | Const_b0 i -> Pp.int i
160 | Const_univ_instance u -> UVars.Instance.pr Sorts.QVar.raw_pr Univ.Level.raw_pr u
161 | Const_val _ -> Pp.str "(value)"
162 | Const_uint i -> Pp.str (Uint63.to_string i)
163 | Const_float f -> Pp.str (Float64.to_string f)
164 | Const_string s -> Pp.str (Printf.sprintf "%S" (Pstring.to_string s))
165
166 (* Abstract data *)
167 type vprod
168 type vfun
169 type vfix
170 type vcofix
171 type vblock
172 type arguments
173
174 let fun_val v = (Obj.magic v : values)
175 let fix_val v = (Obj.magic v : values)
176 let cofix_upd_val v = (Obj.magic v : values)
177
178 type vm_env
179 let inj_env v = (Obj.magic v : vm_env)
180 let fun_env v = (Obj.magic v : vm_env)
181 let cofix_env v = (Obj.magic v : vm_env)
182 let cofix_upd_env v = (Obj.magic v : vm_env)
183 type vstack = values array
184
185 let fun_of_val v = (Obj.magic v : vfun)
186
187 (*******************************************)
188 (* Machine code *** ************************)
189 (*******************************************)
190
191 type tcode
192 (** A block whose first field is a C-allocated VM bytecode, encoded as char*.
193 This is compatible with the representation of OCaml closures. *)
194
195 type tcode_array
196
197 external mkAccuCode : int -> tcode = "rocq_makeaccu"
198 external offset_tcode : tcode -> int -> tcode = "rocq_offset_tcode"
199
200 let fun_code v = (Obj.magic v : tcode)
201 let fix_code = fun_code
202 let cofix_upd_code = fun_code
203
204
205 type vswitch = {
206 sw_type_code : tcode;
207 sw_code : tcode;
208 sw_annot : annot_switch;
209 sw_stk : vstack;
210 sw_env : vm_env
211 }
212
213 (* Representation of values *)
214 (* + Products : *)
215 (* - vprod = 0_[ dom | codom] *)
216 (* dom : values, codom : vfun *)
217 (* *)
218 (* + Functions have two representations : *)
219 (* - unapplied fun : vf = Ct_[ C | envofs=2 | fv1 | ... | fvn] *)
220 (* C:tcode, fvi : values *)
221 (* Remark : a function and its environment is the same value. *)
222 (* - partially applied fun : Ct_[ Restart::C | envofs=2 | vf | arg1 | ... | argn] *)
223 (* *)
224 (* + Fixpoints : *)
225 (* - Ct_[C1|envofs1=3*n-1 | Infix_t|C2|envofs2 | ... | Infix_t|Cn|envofsn=2 | fv1|...|fvn] *)
226 (* One single block to represent all of the fixpoints, each fixpoint *)
227 (* is the pointer to the field holding the pointer to its code, and *)
228 (* the infix tag is used to know where the block starts. *)
229 (* - Partial application follows the scheme of partially applied *)
230 (* functions. Note: only fixpoints not having been applied to its *)
231 (* recursive argument are coded this way. When the rec. arg. is *)
232 (* applied, either it's a constructor and the fix reduces, or it's *)
233 (* and the fix is coded as an accumulator. *)
234 (* *)
235 (* + Cofixpoints : see cbytegen.ml *)
236 (* *)
237 (* + vblock's encode (non constant) constructors as in Ocaml, but *)
238 (* starting from 0 up. *)
239 (* *)
240 (* + vm_env is the type of the machine environments (i.e. a function or *)
241 (* a fixpoint) *)
242 (* *)
243 (* + Accumulators : At_[accumulate | envofs=2 | accu | arg1 | ... | argn ] *)
244 (* - representation of [accu] : tag_[....] *)
245 (* -- tag <= 3 : encoding atom type (sorts, free vars, etc.) *)
246 (* -- 10_[accu|proj name] : a projection blocked by an accu *)
247 (* -- 11_[accu|fix_app] : a fixpoint blocked by an accu *)
248 (* -- 12_[accu|vswitch] : a match blocked by an accu *)
249 (* -- 13_[fcofix] : a cofix function *)
250 (* -- 14_[fcofix|val] : a cofix function, val represent the value *)
251 (* of the function applied to arg1 ... argn *)
252 (* The [arguments] type, which is abstracted as an array, represents : *)
253 (* tag[ _ | _ |v1|... | vn] *)
254 (* Generally the first field is a code pointer. *)
255
256 (* Do not edit this type without editing C code, especially "rocq_values.h" *)
257
258 type id_key =
259 | ConstKey of Constant.t
260 | VarKey of Id.t
261 | RelKey of Int.t
262 | EvarKey of Evar.t
263
264 let eq_id_key (k1 : id_key) (k2 : id_key) = match k1, k2 with
265 | ConstKey c1, ConstKey c2 -> Constant.CanOrd.equal c1 c2
266 | VarKey id1, VarKey id2 -> Id.equal id1 id2
267 | RelKey n1, RelKey n2 -> Int.equal n1 n2
268 | EvarKey evk1, EvarKey evk2 -> Evar.equal evk1 evk2
269 | _ -> false
270
271 type atom =
272 | Aid of id_key
273 | Aind of inductive
274 | Asort of Sorts.t
275
276 (* Zippers *)
277
278 type zipper =
279 | Zapp of arguments
280 | Zfix of vfix*arguments (* Possibly empty *)
281 | Zswitch of vswitch
282 | Zproj of int (* index of the projection as in Projection.Repr *)
283
284 type stack = zipper list
285
286 type to_update = values
287 type accumulator = atom * stack
288
289 type kind = (values, accumulator, vfun, vprod, vfix * arguments option, vcofix * to_update * arguments option, vblock) Values.kind
290
291 (* Functions over arguments *)
292 let nargs : arguments -> int = fun args -> Obj.size (Obj.repr args) - 3
293 let arg args i =
294 if 0 <= i && i < (nargs args) then
295 val_of_obj (Obj.field (Obj.repr args) (i + 3))
296 else invalid_arg
297 ("Vm.arg size = "^(string_of_int (nargs args))^
298 " acces "^(string_of_int i))
299
300 (*************************************************)
301 (* Destructors ***********************************)
302 (*************************************************)
303
304 let uni_instance (v : values) : UVars.Instance.t = Obj.magic v
305
306 let rec whd_accu a stk =
307 let stk =
308 if Int.equal (Obj.size a) 3 then stk
309 else Zapp (Obj.obj a) :: stk in
310 let at = Obj.field a 2 in
311 match Obj.tag at with
312 | i when Int.equal i type_atom_tag ->
313 begin match stk with
314 | [] -> Vaccu (Obj.magic at, stk)
315 | [Zapp args] ->
316 let () = assert (Int.equal (nargs args) 1) in
317 let inst = uni_instance (arg args 0) in
318 let s = Obj.obj (Obj.field at 0) in
319 let s = UVars.subst_instance_sort inst s in
320 Vaccu (Asort s, [])
321 | _ -> assert false
322 end
323 | i when i <= max_atom_tag ->
324 Vaccu (Obj.magic at, stk)
325 | i when Int.equal i proj_tag ->
326 let zproj = Zproj (Obj.obj (Obj.field at 0)) in
327 whd_accu (Obj.field at 1) (zproj :: stk)
328 | i when Int.equal i fix_app_tag ->
329 let fa = Obj.field at 1 in
330 let zfix =
331 Zfix (Obj.obj (Obj.field fa 2), Obj.obj fa) in
332 whd_accu (Obj.field at 0) (zfix :: stk)
333 | i when Int.equal i switch_tag ->
334 let zswitch = Zswitch (Obj.obj (Obj.field at 1)) in
335 whd_accu (Obj.field at 0) (zswitch :: stk)
336 | i when Int.equal i cofix_tag ->
337 let vcfx = Obj.obj (Obj.field at 0) in
338 let to_up = Obj.obj a in
339 begin match stk with
340 | [] -> Vcofix(vcfx, to_up, None)
341 | [Zapp args] -> Vcofix(vcfx, to_up, Some args)
342 | _ -> assert false
343 end
344 | i when Int.equal i cofix_evaluated_tag ->
345 let vcofix = Obj.obj (Obj.field at 0) in
346 let res = Obj.obj a in
347 begin match stk with
348 | [] -> Vcofix(vcofix, res, None)
349 | [Zapp args] -> Vcofix(vcofix, res, Some args)
350 | _ -> assert false
351 end
352 | tg ->
353 CErrors.anomaly
354 Pp.(strbrk "Failed to parse VM value. Tag = " ++ int tg ++ str ".")
355
356 [@@@warning "-37"]
357 type vm_closure_kind =
358 | VCfun (** closure, or fixpoint applied past the recursive argument *)
359 | VCfix (** unapplied fixpoint *)
360 | VCfix_partial (** partially applied fixpoint, but not sufficiently for recursion *)
361 | VCaccu (** accumulator *)
362 [@@@warning "+37"]
363
364 external kind_of_closure : Obj.t -> vm_closure_kind = "rocq_kind_of_closure"
365 external is_accumulate : tcode -> bool = "rocq_is_accumulate_code"
366 external int_tcode : tcode -> int -> int = "rocq_int_tcode"
367 external accumulate : unit -> tcode = "rocq_accumulate"
368 external set_bytecode_field : Obj.t -> int -> tcode -> unit = "rocq_set_bytecode_field"
369 let accumulate = accumulate ()
370
371 let whd_val (v: values) =
372 let o = Obj.repr v in
373 if Obj.is_int o then Vconst (Obj.obj o)
374 else
375 let tag = Obj.tag o in
376 if Int.equal tag 0 then
377 if Int.equal (Obj.size o) 1 then
378 Varray (Obj.obj o)
379 else Vprod (Obj.obj o)
380 else if Int.equal tag Obj.closure_tag && is_accumulate (fun_code o) then
381 whd_accu o []
382 else if Int.equal tag Obj.closure_tag || Int.equal tag Obj.infix_tag then
383 match kind_of_closure o with
384 | VCfun -> Vfun (Obj.obj o)
385 | VCfix -> Vfix (Obj.obj o, None)
386 | VCfix_partial -> Vfix (Obj.obj (Obj.field o 2), Some (Obj.obj o))
387 | VCaccu -> Vaccu (Aid (RelKey (int_tcode (fun_code o) 1)), [])
388 else if Int.equal tag Obj.custom_tag then Vint64 (Obj.magic v)
389 else if Int.equal tag Obj.double_tag then Vfloat64 (Obj.magic v)
390 else if Int.equal tag Obj.string_tag then Vstring (Obj.magic v)
391 else
392 Vblock (Obj.obj o)
393
394 (**********************************************)
395 (* Constructors *******************************)
396 (**********************************************)
397
398 let obj_of_atom : atom -> Obj.t =
399 fun a ->
400 let res = Obj.new_block Obj.closure_tag 3 in
401 set_bytecode_field res 0 accumulate;
402 Obj.set_field res 1 (Obj.repr 2);
403 Obj.set_field res 2 (Obj.repr a);
404 res
405
406 (* obj_of_str_const : structured_constant -> Obj.t *)
407 let obj_of_str_const str =
408 match str with
409 | Const_sort s -> obj_of_atom (Asort s)
410 | Const_ind ind -> obj_of_atom (Aind ind)
411 | Const_evar e -> obj_of_atom (Aid (EvarKey e))
412 | Const_b0 tag -> Obj.repr tag
413 | Const_univ_instance u -> Obj.repr u
414 | Const_val v -> Obj.repr v
415 | Const_uint i -> Obj.repr i
416 | Const_float f -> Obj.repr f
417 | Const_string s -> Obj.repr s
418
419 let val_of_block tag (args : structured_values array) =
420 let nargs = Array.length args in
421 let r = Obj.new_block tag nargs in
422 for i = 0 to nargs - 1 do
423 Obj.set_field r i (Obj.repr args.(i))
424 done;
425 (Obj.magic r : structured_values)
426
427 let val_of_obj o = ((Obj.obj o) : values)
428
429 let val_of_str_const str = val_of_obj (obj_of_str_const str)
430
431 let val_of_atom a = val_of_obj (obj_of_atom a)
432
433 let val_of_int i = (Obj.magic i : values)
434
435 let val_of_uint i = (Obj.magic i : structured_values)
436
437 let val_of_float f = (Obj.magic f : structured_values)
438
439 let val_of_string s = (Obj.magic s : structured_values)
440
441 let atom_of_proj kn v =
442 let r = Obj.new_block proj_tag 2 in
443 Obj.set_field r 0 (Obj.repr kn);
444 Obj.set_field r 1 (Obj.repr v);
445 ((Obj.obj r) : atom)
446
447 let val_of_proj kn v =
448 val_of_atom (atom_of_proj kn v)
449
450 module IdKeyHash =
451 struct
452 type t = id_key
453 let equal = eq_id_key
454 open Hashset.Combine
455 let hash : t -> tag = function
456 | ConstKey c -> combinesmall 1 (Constant.CanOrd.hash c)
457 | VarKey id -> combinesmall 2 (Id.hash id)
458 | RelKey i -> combinesmall 3 (Int.hash i)
459 | EvarKey evk -> combinesmall 4 (Evar.hash evk)
460 end
461
462 module KeyTable = Hashtbl.Make(IdKeyHash)
463
464 let idkey_tbl = KeyTable.create 31
465
466 let val_of_idkey key =
467 try KeyTable.find idkey_tbl key
468 with Not_found ->
469 let v = val_of_atom (Aid key) in
470 KeyTable.add idkey_tbl key v;
471 v
472
473 let val_of_rel k = val_of_idkey (RelKey k)
474
475 let val_of_named id = val_of_idkey (VarKey id)
476
477 let val_of_constant c = val_of_idkey (ConstKey c)
478
479 let val_of_evar evk = val_of_idkey (EvarKey evk)
480
481 external val_of_annot_switch : annot_switch -> values = "%identity"
482
483 (*************************************************)
484 (** Operations manipulating data types ***********)
485 (*************************************************)
486
487 (* Functions over products *)
488
489 let dom : vprod -> values = fun p -> val_of_obj (Obj.field (Obj.repr p) 0)
490 let codom : vprod -> vfun = fun p -> (Obj.obj (Obj.field (Obj.repr p) 1))
491
492 (* Functions over vfun *)
493
494 external closure_arity : vfun -> int = "rocq_closure_arity"
495
496 (* Functions over fixpoint *)
497
498 external current_fix : vfix -> int = "rocq_current_fix"
499 external shift_fix : vfix -> int -> vfix = "rocq_shift_fix"
500 external last_fix : vfix -> vfix = "rocq_last_fix"
501 external tcode_array : tcode_array -> tcode array = "rocq_tcode_array"
502
503 let first_fix o = shift_fix o (- current_fix o)
504 let fix_env v = (Obj.magic (last_fix v) : vm_env)
505
506 let last o = (Obj.field o (Obj.size o - 1))
507 let fix_types (v:vfix) = tcode_array (Obj.magic (last (Obj.repr v)) : tcode_array)
508 let cofix_types (v:vcofix) = tcode_array (Obj.magic (last (Obj.repr v)) : tcode_array)
509
510 let unsafe_rec_arg fb i = int_tcode (Obj.magic (shift_fix fb i)) 1
511
512 let rec_args vf =
513 let fb = first_fix vf in
514 let size = Obj.size (last (Obj.repr fb)) in
515 Array.init size (unsafe_rec_arg fb)
516
517 exception FALSE
518
519 let check_fix f1 f2 =
520 let i1, i2 = current_fix f1, current_fix f2 in
521 (* Checking starting point *)
522 if i1 = i2 then
523 let fb1,fb2 = first_fix f1, first_fix f2 in
524 let n = Obj.size (last (Obj.repr fb1)) in
525 (* Checking number of definitions *)
526 if n = Obj.size (last (Obj.repr fb2)) then
527 (* Checking recursive arguments *)
528 try
529 for i = 0 to n - 1 do
530 if unsafe_rec_arg fb1 i <> unsafe_rec_arg fb2 i
531 then raise FALSE
532 done;
533 true
534 with FALSE -> false
535 else false
536 else false
537
538 let atom_rel : atom array ref =
539 let init i = Aid (RelKey i) in
540 ref (Array.init 40 init)
541
542 let get_atom_rel () = !atom_rel
543
544 let realloc_atom_rel n =
545 let n = min (2 * n + 0x100) Sys.max_array_length in
546 let init i = Aid (RelKey i) in
547 let ans = Array.init n init in
548 atom_rel := ans
549
550 let relaccu_tbl =
551 let len = Array.length !atom_rel in
552 ref (Array.init len mkAccuCode)
553
554 let relaccu_code i =
555 let len = Array.length !relaccu_tbl in
556 if i < len then !relaccu_tbl.(i)
557 else
558 begin
559 realloc_atom_rel i;
560 let nl = Array.length !atom_rel in
561 relaccu_tbl :=
562 Array.init nl
563 (fun j -> if j < len then !relaccu_tbl.(j) else mkAccuCode j);
564 !relaccu_tbl.(i)
565 end
566
567 let mk_fix_body k ndef fb =
568 let e = Obj.dup (Obj.repr fb) in
569 let env = Obj.repr (fix_env (Obj.obj e)) in
570 for i = 0 to ndef - 1 do
571 set_bytecode_field e (3 * i) (relaccu_code (k + i))
572 done;
573 let fix_body i =
574 let c = offset_tcode (Obj.magic (shift_fix fb i)) 2 in
575 let res = Obj.new_block Obj.closure_tag 3 in
576 set_bytecode_field res 0 c;
577 Obj.set_field res 1 (Obj.repr 2);
578 Obj.set_field res 2 env;
579 ((Obj.obj res) : vfun) in
580 Array.init ndef fix_body
581
582 (* Functions over vcofix *)
583
584 let get_fcofix vcf i =
585 match whd_val (Obj.obj (Obj.field (Obj.repr vcf) (i+2))) with
586 | Vcofix(vcfi, _, _) -> vcfi
587 | _ -> assert false
588
589 let current_cofix vcf =
590 let ndef = Obj.size (last (Obj.repr vcf)) in
591 let rec find_cofix pos =
592 if pos < ndef then
593 if get_fcofix vcf pos == vcf then pos
594 else find_cofix (pos+1)
595 else raise Not_found in
596 try find_cofix 0
597 with Not_found -> assert false
598
599 let check_cofix vcf1 vcf2 =
600 (current_cofix vcf1 = current_cofix vcf2) &&
601 (Obj.size (last (Obj.repr vcf1)) = Obj.size (last (Obj.repr vcf2)))
602
603 let mk_cofix_body apply_varray k ndef vcf =
604 let e = Obj.dup (Obj.repr vcf) in
605 for i = 0 to ndef - 1 do
606 Obj.set_field e (i+2) (Obj.repr (val_of_rel (k+i)))
607 done;
608
609 let cofix_body i =
610 let vcfi = get_fcofix vcf i in
611 let c = Obj.field (Obj.repr vcfi) 0 in
612 Obj.set_field e 0 c;
613 let atom = Obj.new_block cofix_tag 1 in
614 let self = obj_of_atom (Obj.obj atom) in
615 apply_varray (Obj.obj e) [|Obj.obj self|] in
616 Array.init ndef cofix_body
617
618 (* Functions over vblock *)
619
620 let btag : vblock -> int = fun b -> Obj.tag (Obj.repr b)
621 let bsize : vblock -> int = fun b -> Obj.size (Obj.repr b)
622 let bfield b i =
623 if 0 <= i && i < (bsize b) then val_of_obj (Obj.field (Obj.repr b) i)
624 else invalid_arg "Vm.bfield"
625
626
627 (* Functions over vswitch *)
628
629 let check_switch sw1 sw2 = sw1.sw_annot.rtbl = sw2.sw_annot.rtbl
630
631 let branch_arg k (tag,arity) =
632 if Int.equal arity 0 then ((Obj.magic tag):values)
633 else
634 let b, ofs =
635 if tag < Obj.last_non_constant_constructor_tag then Obj.new_block tag arity, 0
636 else
637 let b = Obj.new_block Obj.last_non_constant_constructor_tag (arity+1) in
638 Obj.set_field b 0 (Obj.repr (tag-Obj.last_non_constant_constructor_tag));
639 b,1 in
640 for i = ofs to ofs + arity - 1 do
641 Obj.set_field b i (Obj.repr (val_of_rel (k+i)))
642 done;
643 val_of_obj b
644
645 (* Printing *)
646
647 let rec pr_atom a =
648 Pp.(match a with
649 | Aid c -> str "Aid(" ++ (match c with
650 | ConstKey c -> Constant.print c
651 | RelKey i -> str "#" ++ int i
652 | _ -> str "...") ++ str ")"
653 | Aind (mi,i) -> str "Aind(" ++ MutInd.print mi ++ str "#" ++ int i ++ str ")"
654 | Asort _ -> str "Asort(")
655 and pr_kind w =
656 let open Pp in
657 match w with
658 | Vprod _ -> str "Vprod"
659 | Vfun _ -> str "Vfun"
660 | Vfix _ -> str "Vfix"
661 | Vcofix _ -> str "Vcofix"
662 | Vconst i -> str "Vconst(" ++ int i ++ str ")"
663 | Vblock _b -> str "Vblock"
664 | Vint64 i -> i |> Format.sprintf "Vint64(%LiL)" |> str
665 | Vfloat64 f -> str "Vfloat64(" ++ str (Float64.(to_string (of_float f))) ++ str ")"
666 | Vstring s -> Pstring.to_string s |> Format.sprintf "Vstring(%S)" |> str
667 | Varray _ -> str "Varray"
668 | Vaccu (a, stk) -> str "Vaccu(" ++ pr_atom a ++ str ", " ++ pr_stack stk ++ str ")"
669 and pr_stack stk =
670 Pp.(match stk with
671 | [] -> str "[]"
672 | s :: stk -> pr_zipper s ++ str " :: " ++ pr_stack stk)
673 and pr_zipper z =
674 Pp.(match z with
675 | Zapp args -> str "Zapp(len = " ++ int (nargs args) ++ str ")"
676 | Zfix (_f,args) -> str "Zfix(..., len=" ++ int (nargs args) ++ str ")"
677 | Zswitch _s -> str "Zswitch(...)"
678 | Zproj n -> str "Zproj(" ++ int n ++ str ")")
679
680 (** Primitives implemented in OCaml *)
681
682 let parray_make = Obj.magic Parray.make
683 let parray_get = Obj.magic Parray.get
684 let parray_get_default = Obj.magic Parray.default
685 let parray_set = Obj.magic Parray.set
686 let parray_copy = Obj.magic Parray.copy
687 let parray_length = Obj.magic Parray.length
688
689 let pstring_make = Obj.magic Pstring.make
690 let pstring_length = Obj.magic Pstring.length
691 let pstring_get = Obj.magic Pstring.get
692 let pstring_sub = Obj.magic Pstring.sub
693 let pstring_cat = Obj.magic Pstring.cat
694 let pstring_compare = Obj.magic @@ fun x y ->
695 match Pstring.compare x y with 0 -> 0 | i when i < 0 -> 1 | _ -> 2