Something went wrong. Try again.
forked from certirocq/certirocq
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378(* *********************************************************************)(* *)(* 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 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. *)(* *)(* *********************************************************************)
(** Pretty-printer for Clight *)
open Formatopen Camlrocqopen PrintASTopen Ctypesopen Copopen PrintCsyntaxopen Clight
(* Naming temporaries. Some temporaries are obtained by lifting variables in SimplLocals. For these we use a meaningful name "$var", as found in the table of atoms. Other temporaries are generated during SimplExpr, and are not in the table of atoms. We print them as "$NNN" (a unique integer). *)
let temp_name (id: AST.ident) = try "$" ^ Hashtbl.find string_of_atom (P.to_int id) with Not_found -> Printf.sprintf "$%d" (P.to_int id)
(* Declarator (identifier + type) -- reuse from PrintCsyntax *)
(* Precedences and associativity (H&S section 7.2) *)
let precedence = function | Evar _ -> (16, NA) | Etempvar _ -> (16, NA) | Ederef _ -> (15, RtoL) | Efield _ -> (16, LtoR) | Econst_int _ -> (16, NA) | Econst_float _ -> (16, NA) | Econst_single _ -> (16, NA) | Econst_long _ -> (16, NA) | Esizeof _ -> (15, RtoL) | Ealignof _ -> (15, RtoL) | Eunop _ -> (15, RtoL) | Eaddrof _ -> (15, RtoL) | Ecast _ -> (14, RtoL) | Ebinop((Omul|Odiv|Omod), _, _, _) -> (13, LtoR) | Ebinop((Oadd|Osub), _, _, _) -> (12, LtoR) | Ebinop((Oshl|Oshr), _, _, _) -> (11, LtoR) | Ebinop((Olt|Ogt|Ole|Oge), _, _, _) -> (10, LtoR) | Ebinop((Oeq|Cop.One), _, _, _) -> (9, LtoR) | Ebinop(Oand, _, _, _) -> (8, LtoR) | Ebinop(Oxor, _, _, _) -> (7, LtoR) | Ebinop(Oor, _, _, _) -> (6, LtoR)
(* 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" (extern_atom id) | Etempvar(id, _) -> fprintf p "%s" (temp_name id) | Ederef(a1, _) -> fprintf p "*%a" expr (prec', a1) | Efield(a1, f, _) -> fprintf p "%a.%s" expr (prec', a1) (extern_atom f) | Econst_int(n, Tint(I32, Unsigned, _)) -> fprintf p "%luU" (camlint_of_coqint n) | Econst_int(n, _) -> fprintf p "%ld" (camlint_of_coqint n) | Econst_float(f, _) -> fprintf p "%.18g" (camlfloat_of_coqfloat f) | Econst_single(f, _) -> fprintf p "%.18gf" (camlfloat_of_coqfloat32 f) | Econst_long(n, Tlong(Unsigned, _)) -> fprintf p "%LuLLU" (camlint64_of_coqint n) | Econst_long(n, _) -> fprintf p "%LdLL" (camlint64_of_coqint n) | Eunop(Oabsfloat, a1, _) -> fprintf p "__builtin_fabs(%a)" expr (2, a1) | Eunop(op, a1, _) -> fprintf p "%s%a" (name_unop op) expr (prec', a1) | Eaddrof(a1, _) -> fprintf p "&%a" expr (prec', a1) | Ebinop(op, a1, a2, _) -> fprintf p "%a@ %s %a" expr (prec1, a1) (name_binop op) expr (prec2, a2) | Ecast(a1, ty) -> fprintf p "(%s) %a" (name_type ty) expr (prec', a1) | Esizeof(ty, _) -> fprintf p "sizeof(%s)" (name_type ty) | Ealignof(ty, _) -> fprintf p "__alignof__(%s)" (name_type ty) 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)
(* Statements *)
let rec print_stmt p s = match s with | Sskip -> fprintf p "/*skip*/;" | Sassign(e1, e2) -> fprintf p "@[<hv 2>%a =@ %a;@]" print_expr e1 print_expr e2 | Sset(id, e2) -> fprintf p "@[<hv 2>%s =@ %a;@]" (temp_name id) print_expr e2 | Scall(None, e1, el) -> fprintf p "@[<hv 2>%a@,(@[<hov 0>%a@]);@]" expr (15, e1) print_expr_list (true, el) | Scall(Some id, e1, el) -> fprintf p "@[<hv 2>%s =@ %a@,(@[<hov 0>%a@]);@]" (temp_name id) expr (15, e1) print_expr_list (true, el) | Sbuiltin(None, ef, tyargs, el) -> fprintf p "@[<hv 2>builtin %s@,(@[<hov 0>%a@]);@]" (name_of_external ef) print_expr_list (true, el) | Sbuiltin(Some id, ef, tyargs, el) -> fprintf p "@[<hv 2>%s =@ builtin %s@,(@[<hov 0>%a@]);@]" (temp_name id) (name_of_external ef) print_expr_list (true, el) | Ssequence(Sskip, s2) -> print_stmt p s2 | Ssequence(s1, Sskip) -> print_stmt p s1 | Ssequence(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(s1, Sskip) -> fprintf p "@[<v 2>while (1) {@ %a@;<0 -2>}@]" print_stmt s1 | Sloop(s1, s2) -> fprintf p "@[<v 2>for (@[<hv 0>;@ 1;@ %a) {@]@ %a@;<0 -2>}@]" print_stmt_for s2 print_stmt s1 | Sbreak -> fprintf p "break;" | Scontinue -> fprintf p "continue;" | Sswitch(e, cases) -> fprintf p "@[<v 2>switch (%a) {@ %a@;<0 -2>}@]" print_expr e print_cases cases | Sreturn None -> fprintf p "return;" | Sreturn (Some e) -> fprintf p "return %a;" print_expr e | Slabel(lbl, s1) -> fprintf p "%s:@ %a" (extern_atom lbl) print_stmt s1 | Sgoto lbl -> fprintf p "goto %s;" (extern_atom lbl)
and print_cases p cases = match cases with | LSnil -> () | LScons(lbl, Sskip, rem) -> fprintf p "%a:@ %a" print_case_label lbl print_cases rem | LScons(lbl, s, rem) -> fprintf p "@[<v 2>%a:@ %a@]@ %a" print_case_label lbl print_stmt s print_cases rem
and print_case_label p = function | None -> fprintf p "default" | Some lbl -> fprintf p "case %s" (Z.to_string lbl)
and print_stmt_for p s = match s with | Sskip -> fprintf p "(void)0" | Sassign(e1, e2) -> fprintf p "%a = %a" print_expr e1 print_expr e2 | Sset(id, e2) -> fprintf p "%s = %a" (temp_name id) print_expr e2 | Ssequence(Sskip, s2) -> print_stmt_for p s2 | Ssequence(s1, s2) -> fprintf p "%a, %a" print_stmt_for s1 print_stmt_for s2 | Scall(None, e1, el) -> fprintf p "@[<hv 2>%a@,(@[<hov 0>%a@])@]" expr (15, e1) print_expr_list (true, el) | Scall(Some id, e1, el) -> fprintf p "@[<hv 2>%s =@ %a@,(@[<hov 0>%a@])@]" (temp_name id) expr (15, e1) print_expr_list (true, el) | Sbuiltin(None, ef, tyargs, el) -> fprintf p "@[<hv 2>builtin %s@,(@[<hov 0>%a@]);@]" (name_of_external ef) print_expr_list (true, el) | Sbuiltin(Some id, ef, tyargs, el) -> fprintf p "@[<hv 2>%s =@ builtin %s@,(@[<hov 0>%a@]);@]" (temp_name id) (name_of_external ef) print_expr_list (true, el) | _ -> fprintf p "({ %a })" print_stmt s
(* There are two versions of Clight, Clight1 and Clight2, that differ only in the meaning of function parameters: - in Clight1, function parameters are variables - in Clight2, function parameters are temporaries.*)
type clight_version = Clight1 | Clight2
let name_param = function Clight1 -> extern_atom | Clight2 -> temp_name
let print_function ver p id f = fprintf p "%s@ " (name_cdecl (name_function_parameters (name_param ver) (extern_atom id) f.fn_params f.fn_callconv) f.fn_return); fprintf p "@[<v 2>{@ "; List.iter (fun (id, ty) -> fprintf p "%s;@ " (name_cdecl (extern_atom id) ty)) f.fn_vars; List.iter (fun (id, ty) -> fprintf p "register %s;@ " (name_cdecl (temp_name id) ty)) f.fn_temps; print_stmt p f.fn_body; fprintf p "@;<0 -2>}@]@ @ "
let print_fundef ver p id fd = match fd with | Ctypes.External(_, _, _, _) -> () | Internal f -> print_function ver p id f
let print_fundecl p id fd = match fd with | Ctypes.External((AST.EF_external _ | AST.EF_runtime _ | AST.EF_malloc | AST.EF_free), args, res, cconv) -> fprintf p "extern %s;@ " (name_cdecl (extern_atom id) (Tfunction(args, res, cconv))) | Ctypes.External(_, _, _, _) -> () | Internal f -> fprintf p "%s;@ " (name_cdecl (extern_atom id) (Clight.type_of_function f))
let print_globdef var p (id, gd) = match gd with | AST.Gfun f -> print_fundef var p id f | AST.Gvar v -> print_globvar p id v (* from PrintCsyntax *)
let print_globdecl p (id, gd) = match gd with | AST.Gfun f -> print_fundecl p id f | AST.Gvar v -> ()
let print_program ver p prog = fprintf p "@[<v 0>"; List.iter (declare_composite p) prog.prog_types; List.iter (define_composite p) prog.prog_types; List.iter (print_globdecl p) prog.prog_defs; List.iter (print_globdef ver p) prog.prog_defs; fprintf p "@]@."
let destination : string option ref = ref None
let print_if_gen ver prog = match !destination with | None -> () | Some f -> let oc = open_out f in print_program ver (formatter_of_out_channel oc) prog; close_out oc
(* print_if is called from driver/Compiler.v between the SimplExpr and SimplLocals passes. It receives Clight1 syntax. *)let print_if prog = print_if_gen Clight1 prog
(* print_if_2 is called from clightgen/Clightgen.ml, after the SimplLocals pass. It receives Clight2 syntax. *)let print_if_2 prog = print_if_gen Clight2 prog
let add_name (a, n) = let i = P.to_int a in match n with | BasicAst.Coq_nAnon -> () | BasicAst.Coq_nNamed s -> let cs = Caml_bytestring.caml_string_of_bytestring s in Hashtbl.add atom_of_string cs i; Hashtbl.add string_of_atom i cs; ()
let remove_primes (a, n) = match n with | BasicAst.Coq_nAnon -> (a,n) | BasicAst.Coq_nNamed s -> let s' = Str.global_replace (Str.regexp "'") "p" (Caml_bytestring.caml_string_of_bytestring s) in let s'' = Str.global_replace (Str.regexp "\\.") "d" s' in (a, BasicAst.Coq_nNamed (Caml_bytestring.bytestring_of_caml_string s''))
let print_dest prog dest = let oc = open_out (implode dest) in print_program Clight2 (formatter_of_out_channel oc) prog; close_out oc
let print_dest_names prog names dest = let oc = open_out (Caml_bytestring.caml_string_of_bytestring dest) in List.iter (fun n -> add_name (remove_primes n)) names; print_program Clight2 (formatter_of_out_channel oc) prog; close_out oc
let guardify (s : string) : string = String.uppercase_ascii (Str.global_replace (Str.regexp "[^a-zA-Z0-9]") "_" s)
let print_dest_names_imports prog names (dest : string) (imports : string list) = let oc = open_out dest in List.iter (fun n -> add_name (remove_primes n)) names; let fm = formatter_of_out_channel oc in open_vbox 0; (* generating an include guard with C macros *) let guard_name = guardify (List.hd (List.rev (String.split_on_char '/' dest))) in fprintf fm "#ifndef %s" guard_name; pp_print_newline fm (); fprintf fm "#define %s" guard_name; pp_print_newline fm (); List.iter (fun s -> fprintf fm "%s" s ; pp_print_newline fm ()) imports; close_box (); open_box 0; print_program Clight2 fm prog; fprintf fm "#endif /* %s */" guard_name; pp_print_newline fm (); close_box (); close_out oc