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