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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU General Public License as published by *)
(* the Free Software Foundation, either version 2 of the License, or *)
(* (at your option) any later version. This file is also distributed *)
(* under the terms of the INRIA Non-Commercial License Agreement. *)
(* *)
(* *********************************************************************)
(** Pretty-printer for Cminor *)
open Format
open Camlcoq
open Datatypes
open Integers
open AST
open PrintAST
open Cminor
(* Precedences and associativity -- like those of C *)
type associativity = LtoR | RtoL | NA
let rec precedence = function
| Evar _ -> (16, NA)
| Econst _ -> (16, NA)
| Eunop _ -> (15, RtoL)
| Ebinop((Omul|Odiv|Odivu|Omod|Omodu|Omulf|Odivf|Omull|Odivl|Odivlu|Omodl|Omodlu), _, _) -> (13, LtoR)
| Ebinop((Oadd|Osub|Oaddf|Osubf|Oaddl|Osubl), _, _) -> (12, LtoR)
| Ebinop((Oshl|Oshr|Oshru|Oshll|Oshrl|Oshrlu), _, _) -> (11, LtoR)
| Ebinop((Ocmp _|Ocmpu _|Ocmpf _|Ocmpl _|Ocmplu _), _, _) -> (10, LtoR)
| Ebinop((Oand|Oandl), _, _) -> (8, LtoR)
| Ebinop((Oxor|Oxorl), _, _) -> (7, LtoR)
| Ebinop((Oor|Oorl), _, _) -> (6, LtoR)
| Eload _ -> (15, RtoL)
(* Naming idents. *)
let ident_name id = "'" ^ Camlcoq.extern_atom id ^ "'"
(* Naming operators *)
let name_of_unop = function
| Ocast8unsigned -> "int8u"
| Ocast8signed -> "int8s"
| Ocast16unsigned -> "int16u"
| Ocast16signed -> "int16s"
| Onegint -> "-"
| Onotint -> "~"
| Onegf -> "-f"
| Oabsf -> "absf"
| Osingleoffloat -> "float32"
| Ointoffloat -> "intoffloat"
| Ointuoffloat -> "intuoffloat"
| Ofloatofint -> "floatofint"
| Ofloatofintu -> "floatofintu"
| Onegl -> "-l"
| Onotl -> "~l"
| Ointoflong -> "intofflong"
| Olongofint -> "longofint"
| Olongofintu -> "longofintu"
| Olongoffloat -> "longoffloat"
| Olonguoffloat -> "longuoffloat"
| Ofloatoflong -> "floatoflong"
| Ofloatoflongu -> "floatoflongu"
| Osingleoflong -> "singleoflong"
| Osingleoflongu -> "singleoflongu"
let comparison_name = function
| Ceq -> "=="
| Cne -> "!="
| Clt -> "<"
| Cle -> "<="
| Cgt -> ">"
| Cge -> ">="
let name_of_binop = function
| Oadd -> "+"
| Osub -> "-"
| Omul -> "*"
| Odiv -> "/"
| Odivu -> "/u"
| Omod -> "%"
| Omodu -> "%u"
| Oand -> "&"
| Oor -> "|"
| Oxor -> "^"
| Oshl -> "<<"
| Oshr -> ">>"
| Oshru -> ">>u"
| Oaddf -> "+f"
| Osubf -> "-f"
| Omulf -> "*f"
| Odivf -> "/f"
| Oaddl -> "+l"
| Osubl -> "-l"
| Omull -> "*l"
| Odivl -> "/l"
| Odivlu -> "/lu"
| Omodl -> "%l"
| Omodlu -> "%lu"
| Oandl -> "&l"
| Oorl -> "|l"
| Oxorl -> "^l"
| Oshll -> "<<l"
| Oshrl -> ">>l"
| Oshrlu -> ">>lu"
| Ocmp c -> comparison_name c
| Ocmpu c -> comparison_name c ^ "u"
| Ocmpf c -> comparison_name c ^ "f"
| Ocmpl c -> comparison_name c ^ "l"
| Ocmplu c -> comparison_name c ^ "lu"
(* Expressions *)
let rec expr p (prec, e) =
let (prec', assoc) = precedence e in
let (prec1, prec2) =
if assoc = LtoR
then (prec', prec' + 1)
else (prec' + 1, prec') in
if prec' < prec
then fprintf p "@[<hov 2>("
else fprintf p "@[<hov 2>";
begin match e with
| Evar id ->
fprintf p "%s" (ident_name id)
| Econst(Ointconst n) ->
fprintf p "%ld" (camlint_of_coqint n)
| Econst(Ofloatconst f) ->
fprintf p "%F" (camlfloat_of_coqfloat f)
| Econst(Olongconst n) ->
fprintf p "%LdLL" (camlint64_of_coqint n)
| Econst(Oaddrsymbol(id, ofs)) ->
let ofs = camlint_of_coqint ofs in
if ofs = 0l
then fprintf p "\"%s\"" (extern_atom id)
else fprintf p "(\"%s\" + %ld)" (extern_atom id) ofs
| Econst(Oaddrstack n) ->
fprintf p "&%ld" (camlint_of_coqint n)
| Eunop(op, a1) ->
fprintf p "%s %a" (name_of_unop op) expr (prec', a1)
| Ebinop(op, a1, a2) ->
fprintf p "%a@ %s %a"
expr (prec1, a1) (name_of_binop op) expr (prec2, a2)
| Eload(chunk, a1) ->
fprintf p "%s[%a]" (name_of_chunk chunk) expr (0, a1)
end;
if prec' < prec then fprintf p ")@]" else fprintf p "@]"
let print_expr p e = expr p (0, e)
let rec print_expr_list p (first, rl) =
match rl with
| [] -> ()
| r :: rl ->
if not first then fprintf p ",@ ";
expr p (2, r);
print_expr_list p (false, rl)
(* Types *)
let name_of_type = function
| Tint -> "int"
| Tfloat -> "float"
| Tlong -> "long"
| Tsingle -> "single"
let rec print_sig p = function
| {sig_args = []; sig_res = None} -> fprintf p "void"
| {sig_args = []; sig_res = Some ty} -> fprintf p "%s" (name_of_type ty)
| {sig_args = t1 :: tl; sig_res = tres} ->
fprintf p "%s ->@ " (name_of_type t1);
print_sig p {sig_args = tl; sig_res = tres}
(* Statements *)
let rec print_stmt p s =
match s with
| Sskip ->
fprintf p "/*skip*/"
| Sassign(id, e2) ->
fprintf p "@[<hv 2>%s =@ %a;@]" (ident_name id) print_expr e2
| Sstore(chunk, a1, a2) ->
fprintf p "@[<hv 2>%s[%a] =@ %a;@]"
(name_of_chunk chunk) print_expr a1 print_expr a2
| Scall(None, sg, e1, el) ->
fprintf p "@[<hv 2>%a@,(@[<hov 0>%a@])@ : @[<hov 0>%a@];@]"
print_expr e1
print_expr_list (true, el)
print_sig sg
| Scall(Some id, sg, e1, el) ->
fprintf p "@[<hv 2>%s =@ %a@,(@[<hov 0>%a@])@] : @[<hov 0>%a;@]"
(ident_name id)
print_expr e1
print_expr_list (true, el)
print_sig sg
| Stailcall(sg, e1, el) ->
fprintf p "@[<hv 2>tailcall %a@,(@[<hov 0>%a@])@ : @[<hov 0>%a@];@]"
print_expr e1
print_expr_list (true, el)
print_sig sg
| Sbuiltin(None, ef, el) ->
fprintf p "@[<hv 2>builtin %s@,(@[<hov 0>%a@])@;@]"
(name_of_external ef)
print_expr_list (true, el)
| Sbuiltin(Some id, ef, el) ->
fprintf p "@[<hv 2>%s =@ builtin %s@,(@[<hov 0>%a@]);@]@]"
(ident_name id)
(name_of_external ef)
print_expr_list (true, el)
| Sseq(Sskip, s2) ->
print_stmt p s2
| Sseq(s1, Sskip) ->
print_stmt p s1
| Sseq(s1, s2) ->
fprintf p "%a@ %a" print_stmt s1 print_stmt s2
| Sifthenelse(e, s1, Sskip) ->
fprintf p "@[<v 2>if (%a) {@ %a@;<0 -2>}@]"
print_expr e
print_stmt s1
| Sifthenelse(e, Sskip, s2) ->
fprintf p "@[<v 2>if (! %a) {@ %a@;<0 -2>}@]"
expr (15, e)
print_stmt s2
| Sifthenelse(e, s1, s2) ->
fprintf p "@[<v 2>if (%a) {@ %a@;<0 -2>} else {@ %a@;<0 -2>}@]"
print_expr e
print_stmt s1
print_stmt s2
| Sloop(s) ->
fprintf p "@[<v 2>loop {@ %a@;<0 -2>}@]"
print_stmt s
| Sblock(s) ->
fprintf p "@[<v 3>{{ %a@;<0 -3>}}@]"
print_stmt s
| Sexit n ->
fprintf p "exit %d;" (Nat.to_int n)
| Sswitch(e, cases, dfl) ->
fprintf p "@[<v 2>switch (%a) {" print_expr e;
List.iter
(fun (n, x) ->
fprintf p "@ case %ld: exit %d;\n"
(camlint_of_coqint n) (Nat.to_int x))
cases;
fprintf p "@ default: exit %d;\n" (Nat.to_int dfl);
fprintf p "@;<0 -2>}@]"
| Sreturn None ->
fprintf p "return;"
| Sreturn (Some e) ->
fprintf p "return %a;" print_expr e
| Slabel(lbl, s1) ->
fprintf p "%s:@ %a" (ident_name lbl) print_stmt s1 (* wrong for Cminorgen output *)
| Sgoto lbl ->
fprintf p "goto %s;" (ident_name lbl) (* wrong for Cminorgen output *)
(* Functions *)
let rec print_varlist p (vars, first) =
match vars with
| [] -> ()
| v1 :: vl ->
if not first then fprintf p ",@ ";
fprintf p "%s" (ident_name v1);
print_varlist p (vl, false)
let print_function p id f =
fprintf p "@[<hov 4>\"%s\"(@[<hov 0>%a@])@ : @[<hov 0>%a@]@]@ "
(extern_atom id)
print_varlist (f.fn_params, true)
print_sig f.fn_sig;
fprintf p "@[<v 2>{@ ";
let stksz = Z.to_int32 f.fn_stackspace in
if stksz <> 0l then
fprintf p "stack %ld;@ " stksz;
if f.fn_vars <> [] then
fprintf p "var @[<hov 0>%a;@]@ " print_varlist (f.fn_vars, true);
print_stmt p f.fn_body;
fprintf p "@;<0 -2>}@]@ "
let print_extfun p id ef =
fprintf p "@[<v 0>extern @[<hov 2>\"%s\" =@ %s :@ %a@]@]@ "
(extern_atom id) (name_of_external ef) print_sig (ef_sig ef)
let print_init_data p = function
| Init_int8 i -> fprintf p "int8 %ld" (camlint_of_coqint i)
| Init_int16 i -> fprintf p "int16 %ld" (camlint_of_coqint i)
| Init_int32 i -> fprintf p "%ld" (camlint_of_coqint i)
| Init_int64 i -> fprintf p "%Ld" (camlint64_of_coqint i)
| Init_float32 f -> fprintf p "float32 %F" (camlfloat_of_coqfloat f)
| Init_float64 f -> fprintf p "%F" (camlfloat_of_coqfloat f)
| Init_space i -> fprintf p "[%ld]" (camlint_of_coqint i)
| Init_addrof(id,off) -> fprintf p "%ld(\"%s\")" (camlint_of_coqint off) (extern_atom id)
let rec print_init_data_list p = function
| [] -> ()
| [item] -> print_init_data p item
| item::rest ->
(print_init_data p item;
fprintf p ",";
print_init_data_list p rest)
let print_globvar p gv =
if (gv.gvar_readonly) then
fprintf p "readonly ";
if (gv.gvar_volatile) then
fprintf p "volatile ";
fprintf p "{";
print_init_data_list p gv.gvar_init;
fprintf p "}"
let print_globdef p (id, gd) =
match gd with
| Gfun(External ef) ->
print_extfun p id ef
| Gfun(Internal f) ->
print_function p id f
| Gvar gv ->
fprintf p "var \"%s\" %a\n" (extern_atom id) print_globvar gv
let print_program p prog =
fprintf p "@[<v 0>";
List.iter (print_globdef p) prog.prog_defs;
fprintf p "@]@."
let destination : string option ref = ref None
let print_if prog =
match !destination with
| None -> ()
| Some f ->
let oc = open_out f in
print_program (formatter_of_out_channel oc) prog;
close_out oc
|