1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Pretty-printing of C-- code *)
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
open Format
|
1995-06-15 01:17:29 -07:00
|
|
|
open Cmm
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let machtype_component ppf = function
|
|
|
|
| Addr -> fprintf ppf "addr"
|
|
|
|
| Int -> fprintf ppf "int"
|
|
|
|
| Float -> fprintf ppf "float"
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let machtype ppf mty =
|
1995-06-15 01:17:29 -07:00
|
|
|
match Array.length mty with
|
2000-04-21 01:13:22 -07:00
|
|
|
| 0 -> fprintf ppf "unit"
|
|
|
|
| n -> machtype_component ppf mty.(0);
|
1995-06-15 01:17:29 -07:00
|
|
|
for i = 1 to n-1 do
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "*%a" machtype_component mty.(i)
|
1995-06-15 01:17:29 -07:00
|
|
|
done
|
|
|
|
|
|
|
|
let comparison = function
|
2000-04-21 01:13:22 -07:00
|
|
|
| Ceq -> "=="
|
|
|
|
| Cne -> "!="
|
|
|
|
| Clt -> "<"
|
|
|
|
| Cle -> "<="
|
|
|
|
| Cgt -> ">"
|
|
|
|
| Cge -> ">="
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let chunk = function
|
2000-04-21 01:13:22 -07:00
|
|
|
| Byte_unsigned -> "unsigned int8"
|
|
|
|
| Byte_signed -> "signed int8"
|
|
|
|
| Sixteen_unsigned -> "unsigned int16"
|
|
|
|
| Sixteen_signed -> "signed int16"
|
|
|
|
| Thirtytwo_unsigned -> "unsigned int32"
|
|
|
|
| Thirtytwo_signed -> "signed int32"
|
|
|
|
| Word -> ""
|
|
|
|
| Single -> "float32"
|
|
|
|
| Double -> "float64"
|
|
|
|
| Double_u -> "float64u"
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let operation = function
|
2007-01-29 04:11:18 -08:00
|
|
|
| Capply(ty, d) -> "app" ^ Debuginfo.to_string d
|
|
|
|
| Cextcall(lbl, ty, alloc, d) ->
|
|
|
|
Printf.sprintf "extcall \"%s\"%s" lbl (Debuginfo.to_string d)
|
2000-04-21 01:13:22 -07:00
|
|
|
| Cload Word -> "load"
|
|
|
|
| Cload c -> Printf.sprintf "load %s" (chunk c)
|
|
|
|
| Calloc -> "alloc"
|
|
|
|
| Cstore Word -> "store"
|
|
|
|
| Cstore c -> Printf.sprintf "store %s" (chunk c)
|
|
|
|
| Caddi -> "+"
|
|
|
|
| Csubi -> "-"
|
|
|
|
| Cmuli -> "*"
|
|
|
|
| Cdivi -> "/"
|
|
|
|
| Cmodi -> "mod"
|
|
|
|
| Cand -> "and"
|
|
|
|
| Cor -> "or"
|
|
|
|
| Cxor -> "xor"
|
|
|
|
| Clsl -> "<<"
|
|
|
|
| Clsr -> ">>u"
|
|
|
|
| Casr -> ">>s"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ccmpi c -> comparison c
|
2000-04-21 01:13:22 -07:00
|
|
|
| Cadda -> "+a"
|
|
|
|
| Csuba -> "-a"
|
|
|
|
| Ccmpa c -> Printf.sprintf "%sa" (comparison c)
|
|
|
|
| Cnegf -> "~f"
|
|
|
|
| Cabsf -> "absf"
|
|
|
|
| Caddf -> "+f"
|
|
|
|
| Csubf -> "-f"
|
|
|
|
| Cmulf -> "*f"
|
|
|
|
| Cdivf -> "/f"
|
|
|
|
| Cfloatofint -> "floatofint"
|
|
|
|
| Cintoffloat -> "intoffloat"
|
|
|
|
| Ccmpf c -> Printf.sprintf "%sf" (comparison c)
|
2013-10-14 07:38:18 -07:00
|
|
|
| Craise (k, d) -> Lambda.raise_kind k ^ Debuginfo.to_string d
|
2007-01-29 04:11:18 -08:00
|
|
|
| Ccheckbound d -> "checkbound" ^ Debuginfo.to_string d
|
2000-02-08 12:00:06 -08:00
|
|
|
|
|
|
|
let rec expr ppf = function
|
2000-04-21 01:13:22 -07:00
|
|
|
| Cconst_int n -> fprintf ppf "%i" n
|
|
|
|
| Cconst_natint n -> fprintf ppf "%s" (Nativeint.to_string n)
|
|
|
|
| Cconst_float s -> fprintf ppf "%s" s
|
|
|
|
| Cconst_symbol s -> fprintf ppf "\"%s\"" s
|
|
|
|
| Cconst_pointer n -> fprintf ppf "%ia" n
|
|
|
|
| Cconst_natpointer n -> fprintf ppf "%sa" (Nativeint.to_string n)
|
2000-03-14 18:09:27 -08:00
|
|
|
| Cvar id -> Ident.print ppf id
|
1995-06-15 01:17:29 -07:00
|
|
|
| Clet(id, def, (Clet(_, _, _) as body)) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
let print_binding id ppf def =
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>%a@ %a@]" Ident.print id expr def in
|
2000-02-08 12:00:06 -08:00
|
|
|
let rec in_part ppf = function
|
|
|
|
| Clet(id, def, body) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@ %a" (print_binding id) def;
|
2000-02-08 12:00:06 -08:00
|
|
|
in_part ppf body
|
|
|
|
| exp -> exp in
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>(let@ @[<1>(%a" (print_binding id) def;
|
2000-02-08 12:00:06 -08:00
|
|
|
let exp = in_part ppf body in
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf ")@]@ %a)@]" sequence exp
|
1995-06-15 01:17:29 -07:00
|
|
|
| Clet(id, def, body) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf
|
|
|
|
"@[<2>(let@ @[<2>%a@ %a@]@ %a)@]"
|
2010-01-22 04:48:24 -08:00
|
|
|
Ident.print id expr def sequence body
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cassign(id, exp) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>(assign @[<2>%a@ %a@])@]" Ident.print id expr exp
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ctuple el ->
|
2000-02-08 12:00:06 -08:00
|
|
|
let tuple ppf el =
|
|
|
|
let first = ref true in
|
|
|
|
List.iter
|
1995-06-15 01:17:29 -07:00
|
|
|
(fun e ->
|
2000-04-21 01:13:22 -07:00
|
|
|
if !first then first := false else fprintf ppf "@ ";
|
2000-02-08 12:00:06 -08:00
|
|
|
expr ppf e)
|
|
|
|
el in
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<1>[%a]@]" tuple el
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cop(op, el) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>(%s" (operation op);
|
|
|
|
List.iter (fun e -> fprintf ppf "@ %a" expr e) el;
|
1995-06-15 01:17:29 -07:00
|
|
|
begin match op with
|
2007-01-29 04:11:18 -08:00
|
|
|
| Capply (mty, _) -> fprintf ppf "@ %a" machtype mty
|
|
|
|
| Cextcall(_, mty, _, _) -> fprintf ppf "@ %a" machtype mty
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> ()
|
|
|
|
end;
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf ")@]"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Csequence(e1, e2) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>(seq@ %a@ %a)@]" sequence e1 sequence e2
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cifthenelse(e1, e2, e3) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>(if@ %a@ %a@ %a)@]" expr e1 expr e2 expr e3
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cswitch(e1, index, cases) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
let print_case i ppf =
|
1995-06-15 01:17:29 -07:00
|
|
|
for j = 0 to Array.length index - 1 do
|
2000-04-21 01:13:22 -07:00
|
|
|
if index.(j) = i then fprintf ppf "case %i:" j
|
2000-02-08 12:00:06 -08:00
|
|
|
done in
|
|
|
|
let print_cases ppf =
|
|
|
|
for i = 0 to Array.length cases - 1 do
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@ @[<2>%t@ %a@]" (print_case i) sequence cases.(i)
|
2000-02-08 12:00:06 -08:00
|
|
|
done in
|
2010-01-22 04:48:24 -08:00
|
|
|
fprintf ppf "@[<v 0>@[<2>(switch@ %a@ @]%t)@]" expr e1 print_cases
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cloop e ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>(loop@ %a)@]" sequence e
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ccatch(i, ids, e1, e2) ->
|
2000-08-11 12:50:59 -07:00
|
|
|
fprintf ppf
|
2000-10-02 07:08:30 -07:00
|
|
|
"@[<2>(catch@ %a@;<1 -2>with(%d%a)@ %a)@]"
|
|
|
|
sequence e1 i
|
|
|
|
(fun ppf ids ->
|
|
|
|
List.iter
|
|
|
|
(fun id -> fprintf ppf " %a" Ident.print id)
|
|
|
|
ids) ids
|
|
|
|
sequence e2
|
|
|
|
| Cexit (i, el) ->
|
|
|
|
fprintf ppf "@[<2>(exit %d" i ;
|
|
|
|
List.iter (fun e -> fprintf ppf "@ %a" expr e) el;
|
|
|
|
fprintf ppf ")@]"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ctrywith(e1, id, e2) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>(try@ %a@;<1 -2>with@ %a@ %a)@]"
|
|
|
|
sequence e1 Ident.print id sequence e2
|
2000-02-08 12:00:06 -08:00
|
|
|
|
|
|
|
and sequence ppf = function
|
2000-04-21 01:13:22 -07:00
|
|
|
| Csequence(e1, e2) -> fprintf ppf "%a@ %a" sequence e1 sequence e2
|
|
|
|
| e -> expression ppf e
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
and expression ppf e = fprintf ppf "%a" expr e
|
2000-02-08 12:00:06 -08:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let fundecl ppf f =
|
2000-02-08 12:00:06 -08:00
|
|
|
let print_cases ppf cases =
|
|
|
|
let first = ref true in
|
|
|
|
List.iter
|
2010-01-22 04:48:24 -08:00
|
|
|
(fun (id, ty) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
if !first then first := false else fprintf ppf "@ ";
|
|
|
|
fprintf ppf "%a: %a" Ident.print id machtype ty)
|
2000-02-08 12:00:06 -08:00
|
|
|
cases in
|
2012-02-21 09:41:02 -08:00
|
|
|
fprintf ppf "@[<1>(function%s %s@;<1 4>@[<1>(%a)@]@ @[%a@])@]@."
|
|
|
|
(Debuginfo.to_string f.fun_dbg) f.fun_name
|
|
|
|
print_cases f.fun_args sequence f.fun_body
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let data_item ppf = function
|
|
|
|
| Cdefine_symbol s -> fprintf ppf "\"%s\":" s
|
|
|
|
| Cdefine_label l -> fprintf ppf "L%i:" l
|
2002-11-24 07:55:26 -08:00
|
|
|
| Cglobal_symbol s -> fprintf ppf "global \"%s\"" s
|
2000-04-21 01:13:22 -07:00
|
|
|
| Cint8 n -> fprintf ppf "byte %i" n
|
|
|
|
| Cint16 n -> fprintf ppf "int16 %i" n
|
|
|
|
| Cint32 n -> fprintf ppf "int32 %s" (Nativeint.to_string n)
|
|
|
|
| Cint n -> fprintf ppf "int %s" (Nativeint.to_string n)
|
|
|
|
| Csingle f -> fprintf ppf "single %s" f
|
|
|
|
| Cdouble f -> fprintf ppf "double %s" f
|
|
|
|
| Csymbol_address s -> fprintf ppf "addr \"%s\"" s
|
|
|
|
| Clabel_address l -> fprintf ppf "addr L%i" l
|
|
|
|
| Cstring s -> fprintf ppf "string \"%s\"" s
|
|
|
|
| Cskip n -> fprintf ppf "skip %i" n
|
|
|
|
| Calign n -> fprintf ppf "align %i" n
|
|
|
|
|
|
|
|
let data ppf dl =
|
|
|
|
let items ppf = List.iter (fun d -> fprintf ppf "@ %a" data_item d) dl in
|
|
|
|
fprintf ppf "@[<hv 1>(data%t)@]" items
|
|
|
|
|
|
|
|
let phrase ppf = function
|
|
|
|
| Cfunction f -> fundecl ppf f
|
|
|
|
| Cdata dl -> data ppf dl
|