Debian Codegraph @

kernel/vmbytecodes.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 (* Support for native arithmetics by Arnaud Spiwack, May 2007 *)
14
15 (* This file defines the type of bytecode instructions *)
16
17 open Names
18 open Vmvalues
19 open Constr
20
21 module Label =
22 struct
23 type t = int
24 let no = -1
25 let counter = ref no
26 let create () = incr counter; !counter
27 let reset_label_counter () = counter := no
28 end
29
30 type caml_prim =
31 | CAML_Arraymake
32 | CAML_Arrayget
33 | CAML_Arraydefault
34 | CAML_Arrayset
35 | CAML_Arraycopy
36 | CAML_Arraylength
37 | CAML_Stringmake
38 | CAML_Stringlength
39 | CAML_Stringget
40 | CAML_Stringsub
41 | CAML_Stringcat
42 | CAML_Stringcompare
43
44 type instruction =
45 | Klabel of Label.t
46 | Kacc of int
47 | Kenvacc of int
48 | Koffsetclosure of int
49 | Kpush
50 | Kpop of int
51 | Kpush_retaddr of Label.t
52 | Kshort_apply of int
53 | Kapply of int
54 | Kappterm of int * int
55 | Kreturn of int
56 | Kjump
57 | Krestart
58 | Kgrab of int
59 | Kgrabrec of int
60 | Kclosure of Label.t * int
61 | Kclosurerec of int * int * Label.t array * Label.t array
62 | Kclosurecofix of int * int * Label.t array * Label.t array
63 (* nb fv, init, lbl types, lbl bodies *)
64 | Kgetglobal of Constant.t
65 | Ksubstinstance of UVars.Instance.t
66 | Kconst of structured_constant
67 | Kmakeblock of int * tag
68 | Kmakeswitchblock of Label.t * Label.t * annot_switch * int
69 | Kswitch of Label.t array * Label.t array
70 | Kpushfields of int
71 | Kfield of int
72 | Ksetfield of int
73 | Kstop
74 | Ksequence of bytecodes
75 | Kproj of int
76 | Kensurestackcapacity of int
77 | Kbranch of Label.t (* jump to label *)
78 | Kprim of CPrimitives.t * pconstant
79 | Kcamlprim of caml_prim * Label.t
80
81 and bytecodes = instruction list
82
83 type fv_elem =
84 | FVnamed of Id.t
85 | FVrel of int
86
87 type fv = fv_elem array
88
89 (* --- Pretty print *)
90 open Pp
91 open Util
92
93 let caml_prim_to_prim = function
94 | CAML_Arraymake -> CPrimitives.Arraymake
95 | CAML_Arrayget -> CPrimitives.Arrayget
96 | CAML_Arraydefault -> CPrimitives.Arraydefault
97 | CAML_Arrayset -> CPrimitives.Arrayset
98 | CAML_Arraycopy -> CPrimitives.Arraycopy
99 | CAML_Arraylength -> CPrimitives.Arraylength
100 | CAML_Stringmake -> CPrimitives.Stringmake
101 | CAML_Stringlength -> CPrimitives.Stringlength
102 | CAML_Stringget -> CPrimitives.Stringget
103 | CAML_Stringsub -> CPrimitives.Stringsub
104 | CAML_Stringcat -> CPrimitives.Stringcat
105 | CAML_Stringcompare -> CPrimitives.Stringcompare
106
107 let pp_lbl lbl = str "L" ++ int lbl
108
109 let pp_fv_elem = function
110 | FVnamed id -> str "FVnamed(" ++ Id.print id ++ str ")"
111 | FVrel i -> str "Rel(" ++ int i ++ str ")"
112
113 let rec pp_instr i =
114 match i with
115 | Klabel _ | Ksequence _ -> assert false
116 | Kacc n -> str "acc " ++ int n
117 | Kenvacc n -> str "envacc " ++ int n
118 | Koffsetclosure n -> str "offsetclosure " ++ int n
119 | Kpush -> str "push"
120 | Kpop n -> str "pop " ++ int n
121 | Kpush_retaddr lbl -> str "push_retaddr " ++ pp_lbl lbl
122 | Kshort_apply n -> str "short_apply " ++ int n
123 | Kapply n -> str "apply " ++ int n
124 | Kappterm(n, m) ->
125 str "appterm " ++ int n ++ str ", " ++ int m
126 | Kreturn n -> str "return " ++ int n
127 | Kjump -> str "jump"
128 | Krestart -> str "restart"
129 | Kgrab n -> str "grab " ++ int n
130 | Kgrabrec n -> str "grabrec " ++ int n
131 | Kclosure(lbl, n) ->
132 str "closure " ++ pp_lbl lbl ++ str ", " ++ int n
133 | Kclosurerec(fv,init,lblt,lblb) ->
134 hv 1 (str "closurerec " ++
135 int fv ++ str ", " ++ int init ++
136 str " types = " ++
137 prlist_with_sep spc pp_lbl (Array.to_list lblt) ++
138 str " bodies = " ++
139 prlist_with_sep spc pp_lbl (Array.to_list lblb))
140 | Kclosurecofix (fv,init,lblt,lblb) ->
141 hv 1 (str "closurecofix " ++
142 int fv ++ str ", " ++ int init ++
143 str " types = " ++
144 prlist_with_sep spc pp_lbl (Array.to_list lblt) ++
145 str " bodies = " ++
146 prlist_with_sep spc pp_lbl (Array.to_list lblb))
147 | Kgetglobal idu -> str "getglobal " ++ Constant.print idu
148 | Ksubstinstance u ->
149 str "subst_instance " ++
150 UVars.Instance.pr Sorts.QVar.raw_pr Univ.Level.raw_pr u
151 | Kconst sc ->
152 str "const " ++ pp_struct_const sc
153 | Kmakeblock(n, m) ->
154 str "makeblock " ++ int n ++ str ", " ++ int m
155 | Kmakeswitchblock(lblt,lbls,_,sz) ->
156 str "makeswitchblock " ++ pp_lbl lblt ++ str ", " ++
157 pp_lbl lbls ++ str ", " ++ int sz
158 | Kswitch(lblc,lblb) ->
159 hv 1 (str "switch " ++
160 prlist_with_sep spc pp_lbl (Array.to_list lblc) ++
161 str " | " ++
162 prlist_with_sep spc pp_lbl (Array.to_list lblb))
163 | Kpushfields n -> str "pushfields " ++ int n
164 | Kfield n -> str "field " ++ int n
165 | Ksetfield n -> str "setfield " ++ int n
166
167 | Kstop -> str "stop"
168
169 | Kbranch lbl -> str "branch " ++ pp_lbl lbl
170
171 | Kproj p -> str "proj " ++ int p
172
173 | Kensurestackcapacity size -> str "growstack " ++ int size
174
175 | Kprim (op, id) -> str (CPrimitives.to_string op) ++ str " " ++
176 (Constant.print (fst id))
177
178 | Kcamlprim (op, lbl) ->
179 str "camlcall " ++ str (CPrimitives.to_string (caml_prim_to_prim op)) ++ str ", branch " ++
180 pp_lbl lbl ++ str " on accu"
181
182 and pp_bytecodes c =
183 match c with
184 | [] -> str ""
185 | Klabel lbl :: c ->
186 str "L" ++ int lbl ++ str ":" ++ fnl () ++
187 pp_bytecodes c
188 | Ksequence l :: c ->
189 pp_bytecodes l ++ pp_bytecodes c
190 | i :: c ->
191 pp_instr i ++ fnl () ++ pp_bytecodes c