12
let cofix_evaluated_tag = 6
14
type structured_constant =
15
| Const_sorts of sorts
16
| Const_ind of inductive
18
| Const_bn of tag * structured_constant array
21
type reloc_table = (tag * int) array
24
{ci : case_info; rtbl : reloc_table; tailcall : bool}
31
let create () = incr counter; !counter
32
let reset_label_counter () = counter := no
40
| Koffsetclosure of int
43
| Kpush_retaddr of Label.t
44
| Kapply of int (* number of arguments *)
45
| Kappterm of int * int (* number of arguments, slot size *)
46
| Kreturn of int (* slot size *)
49
| Kgrab of int (* number of arguments *)
50
| Kgrabrec of int (* rec arg *)
51
| Kclosure of Label.t * int (* label, number of free variables *)
52
| Kclosurerec of int * int * Label.t array * Label.t array
53
(* nb fv, init, lbl types, lbl bodies *)
54
| Kclosurecofix of int * int * Label.t array * Label.t array
55
(* nb fv, init, lbl types, lbl bodies *)
56
| Kgetglobal of constant
57
| Kconst of structured_constant
58
| Kmakeblock of int * tag (* size, tag *)
60
| Kmakeswitchblock of Label.t * Label.t * annot_switch * int
61
| Kswitch of Label.t array * Label.t array (* consts,blocks *)
66
| Ksequence of bytecodes * bytecodes
67
(* spiwack: instructions concerning integers *)
68
| Kbranch of Label.t (* jump to label *)
69
| Kaddint31 (* adds the int31 in the accu
70
and the one ontop of the stack *)
71
| Kaddcint31 (* makes the sum and keeps the carry *)
72
| Kaddcarrycint31 (* sum +1, keeps the carry *)
73
| Ksubint31 (* subtraction modulo *)
74
| Ksubcint31 (* subtraction, keeps the carry *)
75
| Ksubcarrycint31 (* subtraction -1, keeps the carry *)
76
| Kmulint31 (* multiplication modulo *)
77
| Kmulcint31 (* multiplication, result in two
78
int31, for exact computation *)
79
| Kdiv21int31 (* divides a double size integer
80
(represented by an int31 in the
81
accumulator and one on the top of
82
the stack) by an int31. The result
83
is a pair of the quotient and the
85
If the divisor is 0, it returns
87
| Kdivint31 (* euclidian division (returns a pair
89
| Kaddmuldivint31 (* generic operation for shifting and
90
cycling. Takes 3 int31 i j and s,
91
and returns x*2^s+y/(2^(31-s) *)
92
| Kcompareint31 (* unsigned comparison of int31
94
kernel/byterun/coq_interp.c
96
| Khead0int31 (* Give the numbers of 0 in head of a in31*)
97
| Ktail0int31 (* Give the numbers of 0 in tail of a in31
99
| Kisconst of Label.t (* conditional jump *)
100
| Kareconst of int*Label.t (* conditional jump *)
101
| Kcompint31 (* dynamic compilation of int31 *)
102
| Kdecompint31 (* dynamic decompilation of int31 *)
105
and bytecodes = instruction list
107
type fv_elem = FVnamed of identifier | FVrel of int
109
type fv = fv_elem array
111
(* spiwack: this exception is expected to be raised by function expecting
116
(*spiwack: both type have been moved from Cbytegen because I needed then
117
for the retroknowledge *)
119
size : int; (* longueur de la liste [n] *)
120
fv_rev : fv_elem list (* [fvn; ... ;fv1] *)
125
nb_stack : int; (* nbre de variables sur la pile *)
126
in_stack : int list; (* position dans la pile *)
127
nb_rec : int; (* nbre de fonctions mutuellement *)
128
(* recursives = nbr *)
129
pos_rec : instruction list; (* instruction d'acces pour les variables *)
130
(* de point fix ou de cofix *)
137
(* --- Pretty print *)
139
let rec instruction ppf = function
140
| Klabel lbl -> fprintf ppf "L%i:" lbl
141
| Kacc n -> fprintf ppf "\tacc %i" n
142
| Kenvacc n -> fprintf ppf "\tenvacc %i" n
143
| Koffsetclosure n -> fprintf ppf "\toffsetclosure %i" n
144
| Kpush -> fprintf ppf "\tpush"
145
| Kpop n -> fprintf ppf "\tpop %i" n
146
| Kpush_retaddr lbl -> fprintf ppf "\tpush_retaddr L%i" lbl
147
| Kapply n -> fprintf ppf "\tapply %i" n
149
fprintf ppf "\tappterm %i, %i" n m
150
| Kreturn n -> fprintf ppf "\treturn %i" n
151
| Kjump -> fprintf ppf "\tjump"
152
| Krestart -> fprintf ppf "\trestart"
153
| Kgrab n -> fprintf ppf "\tgrab %i" n
154
| Kgrabrec n -> fprintf ppf "\tgrabrec %i" n
155
| Kclosure(lbl, n) ->
156
fprintf ppf "\tclosure L%i, %i" lbl n
157
| Kclosurerec(fv,init,lblt,lblb) ->
158
fprintf ppf "\tclosurerec";
159
fprintf ppf "%i , %i, " fv init;
160
print_string "types = ";
161
Array.iter (fun lbl -> fprintf ppf " %i" lbl) lblt;
162
print_string " bodies = ";
163
Array.iter (fun lbl -> fprintf ppf " %i" lbl) lblb;
164
| Kclosurecofix (fv,init,lblt,lblb) ->
165
fprintf ppf "\tclosurecofix";
166
fprintf ppf " %i , %i, " fv init;
167
print_string "types = ";
168
Array.iter (fun lbl -> fprintf ppf " %i" lbl) lblt;
169
print_string " bodies = ";
170
Array.iter (fun lbl -> fprintf ppf " %i" lbl) lblb;
171
| Kgetglobal id -> fprintf ppf "\tgetglobal %s" (Names.string_of_con id)
173
fprintf ppf "\tconst"
174
| Kmakeblock(n, m) ->
175
fprintf ppf "\tmakeblock %i, %i" n m
176
| Kmakeprod -> fprintf ppf "\tmakeprod"
177
| Kmakeswitchblock(lblt,lbls,_,sz) ->
178
fprintf ppf "\tmakeswitchblock %i, %i, %i" lblt lbls sz
179
| Kswitch(lblc,lblb) ->
180
fprintf ppf "\tswitch";
181
Array.iter (fun lbl -> fprintf ppf " %i" lbl) lblc;
182
Array.iter (fun lbl -> fprintf ppf " %i" lbl) lblb;
183
| Kpushfields n -> fprintf ppf "\tpushfields %i" n
184
| Ksetfield n -> fprintf ppf "\tsetfield %i" n
185
| Kfield n -> fprintf ppf "\tgetfield %i" n
186
| Kstop -> fprintf ppf "\tstop"
187
| Ksequence (c1,c2) ->
188
fprintf ppf "%a@ %a" instruction_list c1 instruction_list c2
190
| Kbranch lbl -> fprintf ppf "\tbranch %i" lbl
191
| Kaddint31 -> fprintf ppf "\taddint31"
192
| Kaddcint31 -> fprintf ppf "\taddcint31"
193
| Kaddcarrycint31 -> fprintf ppf "\taddcarrycint31"
194
| Ksubint31 -> fprintf ppf "\tsubint31"
195
| Ksubcint31 -> fprintf ppf "\tsubcint31"
196
| Ksubcarrycint31 -> fprintf ppf "\tsubcarrycint31"
197
| Kmulint31 -> fprintf ppf "\tmulint31"
198
| Kmulcint31 -> fprintf ppf "\tmulcint31"
199
| Kdiv21int31 -> fprintf ppf "\tdiv21int31"
200
| Kdivint31 -> fprintf ppf "\tdivint31"
201
| Kcompareint31 -> fprintf ppf "\tcompareint31"
202
| Khead0int31 -> fprintf ppf "\thead0int31"
203
| Ktail0int31 -> fprintf ppf "\ttail0int31"
204
| Kaddmuldivint31 -> fprintf ppf "\taddmuldivint31"
205
| Kisconst lbl -> fprintf ppf "\tisconst %i" lbl
206
| Kareconst(n,lbl) -> fprintf ppf "\tareconst %i %i" n lbl
207
| Kcompint31 -> fprintf ppf "\tcompint31"
208
| Kdecompint31 -> fprintf ppf "\tdecompint"
213
and instruction_list ppf = function
215
| Klabel lbl :: il ->
216
fprintf ppf "L%i:%a" lbl instruction_list il
218
fprintf ppf "%a@ %a" instruction instr instruction_list il
221
(*spiwack: moved this type in this file because I needed it for
222
retroknowledge which can't depend from cbytegen *)
225
| Bstrconst of structured_constant
226
| Bmakeblock of int * block array
227
| Bconstruct_app of int * int * int * block array
228
(* tag , nparams, arity *)
229
| Bspecial of (comp_env -> block array -> int -> bytecodes -> bytecodes) * block array
230
(* spiwack: compilation given by a function *)
231
(* compilation function (see get_vm_constant_dynamic_info in
232
retroknowledge.mli for more info) , argument array *)
237
fprintf std_formatter "@[<v 0>%a@]" instruction_list c