Debian Codegraph @

kernel/vm.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 Values
12 open Vmvalues
13
14 external mkPopStopCode : int -> tcode = "rocq_pushpop"
15
16 let popstop_tbl = ref (Array.init 30 mkPopStopCode)
17
18 let popstop_code i =
19 let len = Array.length !popstop_tbl in
20 if i < len then !popstop_tbl.(i)
21 else
22 begin
23 popstop_tbl :=
24 Array.init (i+10)
25 (fun j -> if j < len then !popstop_tbl.(j) else mkPopStopCode j);
26 !popstop_tbl.(i)
27 end
28
29 let stop = popstop_code 0
30
31 (************************************************)
32 (* Abstract machine *****************************)
33 (************************************************)
34
35 (* gestion de la pile *)
36 external push_ra : tcode -> unit = "rocq_push_ra"
37 external push_val : values -> unit = "rocq_push_val"
38 external push_arguments : arguments -> unit = "rocq_push_arguments"
39 external push_vstack : vstack -> int -> unit = "rocq_push_vstack"
40
41 let interprete = Vmsymtable.vm_interp
42
43 (* Functions over arguments *)
44
45 (* Apply a value to arguments contained in [vargs] *)
46 let apply_arguments vf vargs =
47 let n = nargs vargs in
48 if Int.equal n 0 then fun_val vf
49 else
50 begin
51 push_ra stop;
52 push_arguments vargs;
53 interprete (fun_code vf) (fun_val vf) (fun_env vf) (n - 1)
54 end
55
56 (* Apply value [vf] to an array of argument values [varray] *)
57 let apply_varray vf varray =
58 let n = Array.length varray in
59 if Int.equal n 0 then fun_val vf
60 else
61 begin
62 push_ra stop;
63 (* The fun code of [vf] will make sure we have enough stack, so we put 0
64 here. *)
65 push_vstack varray 0;
66 interprete (fun_code vf) (fun_val vf) (fun_env vf) (n - 1)
67 end
68
69 let mkrel_vstack k arity =
70 let max = k + arity - 1 in
71 Array.init arity (fun i -> val_of_rel (max - i))
72
73 let reduce_fun k vf =
74 let vargs = mkrel_vstack k 1 in
75 apply_varray vf vargs
76
77 let decompose_vfun2 k vf1 vf2 =
78 let arity = min (closure_arity vf1) (closure_arity vf2) in
79 assert (0 < arity && arity < Sys.max_array_length);
80 let vargs = mkrel_vstack k arity in
81 let v1 = apply_varray vf1 vargs in
82 let v2 = apply_varray vf2 vargs in
83 arity, v1, v2
84
85 (* Functions over vfix *)
86
87 let reduce_fix k vf =
88 let fb = first_fix vf in
89 (* computing types *)
90 let fc_typ = fix_types fb in
91 let ndef = Array.length fc_typ in
92 let et = fix_env fb in
93 let ftyp =
94 Array.map
95 (fun c -> interprete c crazy_val et 0) fc_typ in
96 (* Construction of the environment of fix bodies *)
97 (mk_fix_body k ndef fb, ftyp)
98
99 let reduce_cofix k vcf =
100 let fc_typ = cofix_types vcf in
101 let ndef = Array.length fc_typ in
102 let ftyp =
103 (* Evaluate types *)
104 Array.map (fun c -> interprete c crazy_val (cofix_env vcf) 0) fc_typ in
105
106 (* Construction of the environment of cofix bodies *)
107 (mk_cofix_body apply_varray k ndef vcf, ftyp)
108
109 let type_of_switch sw =
110 (* The fun code of types will make sure we have enough stack, so we put 0
111 here. *)
112 push_vstack sw.sw_stk 0;
113 interprete sw.sw_type_code crazy_val sw.sw_env 0
114
115 let apply_switch sw arg =
116 let tc = sw.sw_annot.tailcall in
117 if tc then
118 (push_ra stop;push_vstack sw.sw_stk sw.sw_annot.max_stack_size)
119 else
120 (push_vstack sw.sw_stk sw.sw_annot.max_stack_size;
121 push_ra (popstop_code (Array.length sw.sw_stk)));
122 interprete sw.sw_code arg sw.sw_env 0
123
124 let branch_of_switch k sw =
125 let eval_branch (_,arity as ta) =
126 let arg = branch_arg k ta in
127 let v = apply_switch sw arg in
128 (arity, v)
129 in
130 Array.map eval_branch sw.sw_annot.rtbl
131
132 (* Apply the term represented by a under stack stk to argument v *)
133 (* t = a stk --> t v *)
134 let rec apply_stack a stk v =
135 match stk with
136 | [] -> apply_varray (fun_of_val a) [|v|]
137 | Zapp args :: stk -> apply_stack (apply_arguments (fun_of_val a) args) stk v
138 | Zproj kn :: stk -> apply_stack (val_of_proj kn a) stk v
139 | Zfix(f,args) :: stk ->
140 let a,stk =
141 match stk with
142 | Zapp args' :: stk ->
143 push_ra stop;
144 push_arguments args';
145 push_val a;
146 push_arguments args;
147 let a =
148 interprete (fix_code f) (fix_val f) (fix_env f)
149 (nargs args+ nargs args') in
150 a, stk
151 | _ ->
152 push_ra stop;
153 push_val a;
154 push_arguments args;
155 let a =
156 interprete (fix_code f) (fix_val f) (fix_env f)
157 (nargs args) in
158 a, stk in
159 apply_stack a stk v
160 | Zswitch sw :: stk ->
161 apply_stack (apply_switch sw a) stk v
162
163 let apply_whd k whd =
164 let v = val_of_rel k in
165 match whd with
166 | Vprod _ | Vconst _ | Vblock _ | Vint64 _ | Vfloat64 _ | Vstring _ | Varray _ ->
167 assert false
168 | Vfun f -> reduce_fun k f
169 | Vfix(f, None) ->
170 push_ra stop;
171 push_val v;
172 interprete (fix_code f) (fix_val f) (fix_env f) 0
173 | Vfix(f, Some args) ->
174 push_ra stop;
175 push_val v;
176 push_arguments args;
177 interprete (fix_code f) (fix_val f) (fix_env f) (nargs args)
178 | Vcofix(_,to_up,_) ->
179 push_ra stop;
180 push_val v;
181 interprete (cofix_upd_code to_up) (cofix_upd_val to_up) (cofix_upd_env to_up) 0
182 | Vaccu (a, stk) ->
183 apply_stack (val_of_atom a) stk v
184