1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Pretty-printing of C-- code *)
|
|
|
|
|
1999-11-08 09:06:33 -08:00
|
|
|
open Formatmsg
|
1995-06-15 01:17:29 -07:00
|
|
|
open Cmm
|
|
|
|
|
|
|
|
let machtype_component = function
|
|
|
|
Addr -> print_string "addr"
|
|
|
|
| Int -> print_string "int"
|
|
|
|
| Float -> print_string "float"
|
|
|
|
|
|
|
|
let machtype mty =
|
|
|
|
match Array.length mty with
|
|
|
|
0 -> print_string "unit"
|
|
|
|
| n -> machtype_component mty.(0);
|
|
|
|
for i = 1 to n-1 do
|
|
|
|
print_string "*"; machtype_component mty.(i)
|
|
|
|
done
|
|
|
|
|
|
|
|
let comparison = function
|
|
|
|
Ceq -> print_string "=="
|
|
|
|
| Cne -> print_string "!="
|
|
|
|
| Clt -> print_string "<"
|
|
|
|
| Cle -> print_string "<="
|
|
|
|
| Cgt -> print_string ">"
|
|
|
|
| Cge -> print_string ">="
|
|
|
|
|
|
|
|
let chunk = function
|
2000-02-04 04:43:18 -08:00
|
|
|
Byte_unsigned -> print_string "unsigned int8"
|
|
|
|
| Byte_signed -> print_string "signed int8"
|
|
|
|
| Sixteen_unsigned -> print_string "unsigned int16"
|
|
|
|
| Sixteen_signed -> print_string "signed int16"
|
|
|
|
| Thirtytwo_unsigned -> print_string "unsigned int32"
|
|
|
|
| Thirtytwo_signed -> print_string "signed int32"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Word -> ()
|
2000-02-04 04:43:18 -08:00
|
|
|
| Single -> print_string "float32"
|
|
|
|
| Double -> print_string "float64"
|
2000-03-10 06:31:06 -08:00
|
|
|
| Double_u -> print_string "float64u"
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let operation = function
|
|
|
|
Capply ty -> print_string "app"
|
2000-02-08 12:00:06 -08:00
|
|
|
| Cextcall(lbl, ty, alloc) -> printf "extcall \"%s\"" lbl
|
2000-02-04 04:43:18 -08:00
|
|
|
| Cload Word -> print_string "load"
|
|
|
|
| Cload c -> print_string "load "; chunk c
|
1995-06-15 01:17:29 -07:00
|
|
|
| Calloc -> print_string "alloc"
|
2000-02-04 04:43:18 -08:00
|
|
|
| Cstore Word -> print_string "store"
|
|
|
|
| Cstore c -> print_string "store "; chunk c
|
1995-06-15 01:17:29 -07:00
|
|
|
| Caddi -> print_string "+"
|
|
|
|
| Csubi -> print_string "-"
|
|
|
|
| Cmuli -> print_string "*"
|
|
|
|
| Cdivi -> print_string "/"
|
|
|
|
| Cmodi -> print_string "mod"
|
|
|
|
| Cand -> print_string "and"
|
|
|
|
| Cor -> print_string "or"
|
|
|
|
| Cxor -> print_string "xor"
|
|
|
|
| Clsl -> print_string "<<"
|
|
|
|
| Clsr -> print_string ">>u"
|
|
|
|
| Casr -> print_string ">>s"
|
|
|
|
| Ccmpi c -> comparison c
|
|
|
|
| Cadda -> print_string "+a"
|
|
|
|
| Csuba -> print_string "-a"
|
|
|
|
| Ccmpa c -> comparison c; print_string "a"
|
1996-03-07 05:45:17 -08:00
|
|
|
| Cnegf -> print_string "~f"
|
|
|
|
| Cabsf -> print_string "absf"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Caddf -> print_string "+f"
|
|
|
|
| Csubf -> print_string "-f"
|
|
|
|
| Cmulf -> print_string "*f"
|
|
|
|
| Cdivf -> print_string "/f"
|
|
|
|
| Cfloatofint -> print_string "floatofint"
|
|
|
|
| Cintoffloat -> print_string "intoffloat"
|
|
|
|
| Ccmpf c -> comparison c; print_string "f"
|
|
|
|
| Craise -> print_string "raise"
|
1995-07-07 09:42:05 -07:00
|
|
|
| Ccheckbound -> print_string "checkbound"
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-03-14 18:09:27 -08:00
|
|
|
let print_id ppf id = Ident.print ppf id;;
|
2000-02-08 12:00:06 -08:00
|
|
|
|
|
|
|
let rec expr ppf = function
|
1995-07-02 09:41:48 -07:00
|
|
|
Cconst_int n -> print_int n
|
1997-03-04 02:19:51 -08:00
|
|
|
| Cconst_natint n -> print_string(Nativeint.to_string n)
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cconst_float s -> print_string s
|
2000-02-08 12:00:06 -08:00
|
|
|
| Cconst_symbol s -> printf "\"%s\"" s
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cconst_pointer n -> printf "%ia" n
|
|
|
|
| Cconst_natpointer n -> printf "%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 =
|
|
|
|
printf "@[<2>%a@ %a@]" print_id id expr def in
|
|
|
|
let rec in_part ppf = function
|
|
|
|
| Clet(id, def, body) ->
|
|
|
|
printf "@ %a" (print_binding id) def;
|
|
|
|
in_part ppf body
|
|
|
|
| exp -> exp in
|
|
|
|
printf "@[<2>(let@ @[<1>(%a" (print_binding id) def;
|
|
|
|
let exp = in_part ppf body in
|
|
|
|
printf ")@]@ %a)@]" sequence exp
|
1995-06-15 01:17:29 -07:00
|
|
|
| Clet(id, def, body) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<2>(let@ @[<2>%a@ %a@]@ %a)@]" print_id id expr def sequence body
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cassign(id, exp) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<2>(assign @[<2>%a@ %a@])@]" print_id 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 ->
|
|
|
|
if !first then first := false else print_space();
|
2000-02-08 12:00:06 -08:00
|
|
|
expr ppf e)
|
|
|
|
el in
|
|
|
|
printf "@[<1>[%a]@]" tuple el
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cop(op, el) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<2>(";
|
|
|
|
operation op;
|
|
|
|
List.iter (fun e -> printf "@ %a" expr e) el;
|
1995-06-15 01:17:29 -07:00
|
|
|
begin match op with
|
|
|
|
Capply mty -> print_space(); machtype mty
|
1995-07-10 02:48:27 -07:00
|
|
|
| Cextcall(_, mty, _) -> print_space(); machtype mty
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> ()
|
|
|
|
end;
|
2000-02-08 12:00:06 -08:00
|
|
|
printf ")@]"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Csequence(e1, e2) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<2>(seq@ %a@ %a)@]" sequence e1 sequence e2
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cifthenelse(e1, e2, e3) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<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-02-08 12:00:06 -08:00
|
|
|
if index.(j) = i then printf "case %i:" j
|
|
|
|
done in
|
|
|
|
let print_cases ppf =
|
|
|
|
for i = 0 to Array.length cases - 1 do
|
|
|
|
printf "@ @[<2>%t@ %a@]" (print_case i) sequence cases.(i)
|
|
|
|
done in
|
|
|
|
printf "@[<v 0>@[<2>(switch@ %a@ @]%t)@]" expr e1 print_cases
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cloop e ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<2>(loop@ %a)@]" sequence e
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ccatch(e1, e2) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<2>(catch@ %a@;<1 -2>with@ %a)@]" sequence e1 sequence e2
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cexit ->
|
|
|
|
print_string "exit"
|
|
|
|
| Ctrywith(e1, id, e2) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "@[<2>(try@ %a@;<1 -2>with@ %a@ %a)@]"
|
|
|
|
sequence e1 print_id id sequence e2
|
|
|
|
|
|
|
|
and sequence ppf = function
|
1995-06-15 01:17:29 -07:00
|
|
|
Csequence(e1, e2) ->
|
2000-02-08 12:00:06 -08:00
|
|
|
printf "%a@ %a" sequence e1 sequence e2
|
1995-06-15 01:17:29 -07:00
|
|
|
| e ->
|
|
|
|
expression e
|
|
|
|
|
2000-02-08 12:00:06 -08:00
|
|
|
and expression e = printf "%a" expr e
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let fundecl f =
|
2000-02-08 12:00:06 -08:00
|
|
|
let print_cases ppf cases =
|
|
|
|
let first = ref true in
|
|
|
|
List.iter
|
|
|
|
(fun (id, ty) ->
|
|
|
|
if !first then first := false else print_space();
|
|
|
|
printf "%a: " print_id id;
|
|
|
|
machtype ty)
|
|
|
|
cases in
|
|
|
|
printf "@[<1>(function %s@;<1 4>@[<1>(%a)@]@ @[%a@])@]@."
|
|
|
|
f.fun_name print_cases f.fun_args sequence f.fun_body
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let data_item = function
|
2000-02-08 12:00:06 -08:00
|
|
|
Cdefine_symbol s -> printf "\"%s\":" s
|
|
|
|
| Cdefine_label l -> printf "L%i:" l
|
|
|
|
| Cint8 n -> printf "byte %i" n
|
|
|
|
| Cint16 n -> printf "int16 %i" n
|
|
|
|
| Cint32 n -> printf "int32 %s" (Nativeint.to_string n)
|
|
|
|
| Cint n -> printf "int %s" (Nativeint.to_string n)
|
|
|
|
| Csingle f -> printf "single %s" f
|
|
|
|
| Cdouble f -> printf "double %s" f
|
|
|
|
| Csymbol_address s -> printf "addr \"%s\"" s
|
|
|
|
| Clabel_address l -> printf "addr L%i" l
|
|
|
|
| Cstring s -> printf "string \"%s\"" s
|
|
|
|
| Cskip n -> printf "skip %i" n
|
|
|
|
| Calign n -> printf "align %i" n
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let data dl =
|
2000-02-08 12:00:06 -08:00
|
|
|
let items ppf = List.iter (fun d -> print_space(); data_item d) dl in
|
|
|
|
printf "@[<hv 1>(data%t)@]" items
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let phrase = function
|
|
|
|
Cfunction f -> fundecl f
|
|
|
|
| Cdata dl -> data dl
|