Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

PrintCminor.ml 11.5 KB
Newer Older
xleroy's avatar
xleroy committed
1
2
3
4
5
6
7
8
(* *********************************************************************)
(*                                                                     *)
(*              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       *)
9
10
11
12
13
(*  under the terms of the GNU Lesser General Public License as        *)
(*  published by the Free Software Foundation, either version 2.1 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.                            *)
xleroy's avatar
xleroy committed
14
15
16
17
18
19
(*                                                                     *)
(* *********************************************************************)

(** Pretty-printer for Cminor *)

open Format
20
open! Camlcoq
xleroy's avatar
xleroy committed
21
22
open Integers
open AST
xleroy's avatar
xleroy committed
23
open PrintAST
xleroy's avatar
xleroy committed
24
25
26
27
28
29
open Cminor

(* Precedences and associativity -- like those of C *)

type associativity = LtoR | RtoL | NA

Bernhard Schommer's avatar
Bernhard Schommer committed
30
let precedence = function
xleroy's avatar
xleroy committed
31
32
33
  | Evar _ -> (16, NA)
  | Econst _ -> (16, NA)
  | Eunop _ -> (15, RtoL)
xleroy's avatar
xleroy committed
34
35
  | Ebinop((Omul|Odiv|Odivu|Omod|Omodu|Omulf|Odivf|Omulfs|Odivfs|Omull|Odivl|Odivlu|Omodl|Omodlu), _, _) -> (13, LtoR)
  | Ebinop((Oadd|Osub|Oaddf|Osubf|Oaddfs|Osubfs|Oaddl|Osubl), _, _) -> (12, LtoR)
36
  | Ebinop((Oshl|Oshr|Oshru|Oshll|Oshrl|Oshrlu), _, _) -> (11, LtoR)
xleroy's avatar
xleroy committed
37
  | Ebinop((Ocmp _|Ocmpu _|Ocmpf _|Ocmpfs _|Ocmpl _|Ocmplu _), _, _) -> (10, LtoR)
38
  | Ebinop((Oexpect _), _, _) -> (9, LtoR)
39
40
41
  | Ebinop((Oand|Oandl), _, _) -> (8, LtoR)
  | Ebinop((Oxor|Oxorl), _, _) -> (7, LtoR)
  | Ebinop((Oor|Oorl), _, _) -> (6, LtoR)
xleroy's avatar
xleroy committed
42
43
  | Eload _ -> (15, RtoL)

xleroy's avatar
xleroy committed
44
(* Naming idents. *)
xleroy's avatar
xleroy committed
45

xleroy's avatar
xleroy committed
46
let ident_name id = "'" ^ Camlcoq.extern_atom id ^ "'"
xleroy's avatar
xleroy committed
47
48
49
50
51
52
53
54
55
56
57
58

(* Naming operators *)

let name_of_unop = function
  | Ocast8unsigned -> "int8u"
  | Ocast8signed -> "int8s"
  | Ocast16unsigned -> "int16u"
  | Ocast16signed -> "int16s"
  | Onegint -> "-"
  | Onotint -> "~"
  | Onegf -> "-f"
  | Oabsf -> "absf"
xleroy's avatar
xleroy committed
59
60
  | Onegfs -> "-s"
  | Oabsfs -> "abss"
xleroy's avatar
xleroy committed
61
  | Osingleoffloat -> "float32"
xleroy's avatar
xleroy committed
62
  | Ofloatofsingle -> "float64"
xleroy's avatar
xleroy committed
63
64
65
66
  | Ointoffloat -> "intoffloat"
  | Ointuoffloat -> "intuoffloat"
  | Ofloatofint -> "floatofint"
  | Ofloatofintu -> "floatofintu"
xleroy's avatar
xleroy committed
67
68
69
70
  | Ointofsingle -> "intofsingle"
  | Ointuofsingle -> "intuofsingle"
  | Osingleofint -> "singleofint"
  | Osingleofintu -> "singleofintu"
71
72
  | Onegl -> "-l"
  | Onotl -> "~l"
73
  | Ointoflong -> "intoflong"
74
75
76
77
78
79
  | Olongofint -> "longofint"
  | Olongofintu -> "longofintu"
  | Olongoffloat -> "longoffloat"
  | Olonguoffloat -> "longuoffloat"
  | Ofloatoflong -> "floatoflong"
  | Ofloatoflongu -> "floatoflongu"
xleroy's avatar
xleroy committed
80
81
  | Olongofsingle -> "longofsingle"
  | Olonguofsingle -> "longuofsingle"
82
83
  | Osingleoflong -> "singleoflong"
  | Osingleoflongu -> "singleoflongu"
xleroy's avatar
xleroy committed
84
85
86
87
88
89
90
91
92
93

let comparison_name = function
  | Ceq -> "=="
  | Cne -> "!="
  | Clt -> "<"
  | Cle -> "<="
  | Cgt -> ">"
  | Cge -> ">="

let name_of_binop = function
94
  | Oexpect _ -> "expect"
xleroy's avatar
xleroy committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  | Oadd -> "+"
  | Osub -> "-"
  | Omul -> "*"
  | Odiv -> "/"
  | Odivu -> "/u"
  | Omod -> "%"
  | Omodu -> "%u"
  | Oand -> "&"
  | Oor -> "|"
  | Oxor -> "^"
  | Oshl -> "<<"
  | Oshr -> ">>"
  | Oshru -> ">>u"
  | Oaddf -> "+f"
  | Osubf -> "-f"
  | Omulf -> "*f"
  | Odivf -> "/f"
xleroy's avatar
xleroy committed
112
113
114
115
  | Oaddfs -> "+s"
  | Osubfs -> "-s"
  | Omulfs -> "*s"
  | Odivfs -> "/s"
116
117
118
119
120
121
122
123
124
125
126
127
128
  | 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"
xleroy's avatar
xleroy committed
129
130
131
  | Ocmp c -> comparison_name c
  | Ocmpu c -> comparison_name c ^ "u"
  | Ocmpf c -> comparison_name c ^ "f"
xleroy's avatar
xleroy committed
132
  | Ocmpfs c -> comparison_name c ^ "s"
133
134
  | Ocmpl c -> comparison_name c ^ "l"
  | Ocmplu c -> comparison_name c ^ "lu"
xleroy's avatar
xleroy committed
135
136
137
138
139
140
141
142
143

(* 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
144
  if prec' < prec
xleroy's avatar
xleroy committed
145
146
147
148
149
150
151
152
  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) ->
153
      fprintf p "%.15F" (camlfloat_of_coqfloat f)
xleroy's avatar
xleroy committed
154
  | Econst(Osingleconst f) ->
155
      fprintf p "%.15Ff" (camlfloat_of_coqfloat32 f)
156
157
  | Econst(Olongconst n) ->
      fprintf p "%LdLL" (camlint64_of_coqint n)
xleroy's avatar
xleroy committed
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
  | 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"
190
  | Tlong -> "long"
xleroy's avatar
xleroy committed
191
  | Tsingle -> "single"
xleroy's avatar
xleroy committed
192
193
  | Tany32 -> "any32"
  | Tany64 -> "any64"
xleroy's avatar
xleroy committed
194

195
196
197
198
let print_sig p sg =
  List.iter
    (fun t -> fprintf p "%s ->@ " (name_of_type t))
    sg.sig_args;
199
  fprintf p "%s" (name_of_rettype sg.sig_res)
xleroy's avatar
xleroy committed
200

201
202
203
204
205
206
207
let rec just_skips s =
  match s with
  | Sskip -> true
  | Sseq(s1,s2) -> just_skips s1 && just_skips s2
  | _ -> false


xleroy's avatar
xleroy committed
208
209
210
211
212
(* Statements *)

let rec print_stmt p s =
  match s with
  | Sskip ->
xleroy's avatar
xleroy committed
213
      fprintf p "/*skip*/"
xleroy's avatar
xleroy committed
214
215
216
217
218
219
220
221
222
223
224
  | 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) ->
225
      fprintf p "@[<hv 2>%s =@ %a@,(@[<hov 0>%a@])@] : @[<hov 0>%a;@]"
xleroy's avatar
xleroy committed
226
227
228
229
230
231
232
233
234
                (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
xleroy's avatar
xleroy committed
235
  | Sbuiltin(None, ef, el) ->
236
      fprintf p "@[<hv 2>builtin %s@,(@[<hov 0>%a@])@ : @[<hov 0>%a@];@]"
xleroy's avatar
xleroy committed
237
238
                (name_of_external ef)
                print_expr_list (true, el)
239
	        print_sig (ef_sig ef)
xleroy's avatar
xleroy committed
240
  | Sbuiltin(Some id, ef, el) ->
241
      fprintf p "@[<hv 2>%s =@ builtin %s@,(@[<hov 0>%a@]) : @[<hov 0>%a@];@]"
xleroy's avatar
xleroy committed
242
243
244
                (ident_name id)
                (name_of_external ef)
                print_expr_list (true, el)
245
246
247
	        print_sig (ef_sig ef)
  | Sseq(s1,s2) when just_skips s1 && just_skips s2 ->
      ()
248
  | Sseq(s1, s2) when just_skips s1 ->
xleroy's avatar
xleroy committed
249
      print_stmt p s2
250
  | Sseq(s1, s2) when just_skips s2 ->
xleroy's avatar
xleroy committed
251
252
253
254
255
256
257
      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
258
(*  | Sifthenelse(e, Sskip, s2) ->
xleroy's avatar
xleroy committed
259
260
      fprintf p "@[<v 2>if (! %a) {@ %a@;<0 -2>}@]"
              expr (15, e)
261
              print_stmt s2 *)
xleroy's avatar
xleroy committed
262
263
264
265
266
267
268
269
270
271
272
273
  | 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 ->
274
      fprintf p "exit %d;" (Nat.to_int n)
275
  | Sswitch(long, e, cases, dfl) ->
276
277
278
279
280
281
    let print_case (n,x) =
      let x = Nat.to_int x in
      if long then
        fprintf p "@ case %LdLL: exit %d;" (Z.to_int64 n) x
      else
        fprintf p "@ case %ld: exit %d;" (Z.to_int32 n) x in
282
283
      fprintf p "@[<v 2>switch%s (%a) {"
        (if long then "l" else "") print_expr e;
284
      List.iter print_case cases;
285
      fprintf p "@ default: exit %d;\n" (Nat.to_int dfl);
xleroy's avatar
xleroy committed
286
287
288
289
290
291
      fprintf p "@;<0 -2>}@]"
  | Sreturn None ->
      fprintf p "return;"
  | Sreturn (Some e) ->
      fprintf p "return %a;" print_expr e
  | Slabel(lbl, s1) ->
292
      fprintf p "%s:@ %a" (ident_name lbl) print_stmt s1  (* wrong for Cminorgen output *)
xleroy's avatar
xleroy committed
293
  | Sgoto lbl ->
294
      fprintf p "goto %s;" (ident_name lbl)               (* wrong for Cminorgen output *)
xleroy's avatar
xleroy committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

(* 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>{@ ";
312
  let stksz = Z.to_int32 f.fn_stackspace in
xleroy's avatar
xleroy committed
313
314
315
316
317
318
319
  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>}@]@ "

320
let print_extfun p id ef =
321
  fprintf p "@[<v 0>extern @[<hov 2>\"%s\" =@ %s :@ %a@]@]@ "
322
    (extern_atom id) (name_of_external ef) print_sig (ef_sig ef)
323
324
325
326
327

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)
328
  | Init_int64 i -> fprintf p "%LdLL" (camlint64_of_coqint i)
329
330
  | Init_float32 f -> fprintf p "float32 %.15F" (camlfloat_of_coqfloat f)
  | Init_float64 f -> fprintf p "%.15F" (camlfloat_of_coqfloat f)
331
  | Init_space i -> fprintf p "[%s]" (Z.to_string i)
332
333
334
335
336
  | 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
337
  | item::rest ->
338
339
340
341
      (print_init_data p item;
       fprintf p ",";
       print_init_data_list p rest)

342
let print_globvar p gv =
343
344
345
346
347
348
349
350
  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 "}"

351
352
353
354
355
356
357
358
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
359

xleroy's avatar
xleroy committed
360
let print_program p prog =
361
  fprintf p "@[<v 0>";
362
363
  if extern_atom prog.prog_main <> "main" then
    fprintf p "= \"%s\"\n" (extern_atom prog.prog_main);
364
  List.iter (print_globdef p) prog.prog_defs;
xleroy's avatar
xleroy committed
365
366
367
368
369
370
371
372
373
374
375
  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