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 pseudo machine code *)
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
open Format
|
1995-06-15 01:17:29 -07:00
|
|
|
open Cmm
|
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let reg ppf r =
|
1995-06-15 01:17:29 -07:00
|
|
|
if String.length r.name > 0 then
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "%s" r.name
|
1995-06-15 01:17:29 -07:00
|
|
|
else
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "%s" (match r.typ with Addr -> "A" | Int -> "I" | Float -> "F");
|
|
|
|
fprintf ppf "/%i" r.stamp;
|
1995-06-15 01:17:29 -07:00
|
|
|
begin match r.loc with
|
2000-04-21 01:13:22 -07:00
|
|
|
| Unknown -> ()
|
2010-01-22 04:48:24 -08:00
|
|
|
| Reg r ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "[%s]" (Proc.register_name r)
|
1995-06-15 01:17:29 -07:00
|
|
|
| Stack(Local s) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "[s%i]" s
|
1995-06-15 01:17:29 -07:00
|
|
|
| Stack(Incoming s) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "[si%i]" s
|
1995-06-15 01:17:29 -07:00
|
|
|
| Stack(Outgoing s) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "[so%i]" s
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let regs ppf v =
|
1995-06-15 01:17:29 -07:00
|
|
|
match Array.length v with
|
2000-04-21 01:13:22 -07:00
|
|
|
| 0 -> ()
|
|
|
|
| 1 -> reg ppf v.(0)
|
|
|
|
| n -> reg ppf v.(0);
|
2007-01-29 04:11:18 -08:00
|
|
|
for i = 1 to n-1 do fprintf ppf " %a" reg v.(i) done
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let regset ppf s =
|
1995-06-15 01:17:29 -07:00
|
|
|
let first = ref true in
|
|
|
|
Reg.Set.iter
|
|
|
|
(fun r ->
|
2000-04-21 01:13:22 -07:00
|
|
|
if !first then begin first := false; fprintf ppf "%a" reg r end
|
|
|
|
else fprintf ppf "@ %a" reg r)
|
1995-06-15 01:17:29 -07:00
|
|
|
s
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let regsetaddr ppf s =
|
1995-07-07 05:07:07 -07:00
|
|
|
let first = ref true in
|
|
|
|
Reg.Set.iter
|
|
|
|
(fun r ->
|
2000-04-21 01:13:22 -07:00
|
|
|
if !first then begin first := false; fprintf ppf "%a" reg r end
|
|
|
|
else fprintf ppf "@ %a" reg r;
|
|
|
|
match r.typ with Addr -> fprintf ppf "*" | _ -> ())
|
1995-07-07 05:07:07 -07:00
|
|
|
s
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let intcomp = function
|
2000-04-21 01:13:22 -07:00
|
|
|
| Isigned c -> Printf.sprintf " %ss " (Printcmm.comparison c)
|
|
|
|
| Iunsigned c -> Printf.sprintf " %su " (Printcmm.comparison c)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let floatcomp c =
|
2000-04-21 01:13:22 -07:00
|
|
|
Printf.sprintf " %sf " (Printcmm.comparison c)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let intop = function
|
2000-04-21 01:13:22 -07:00
|
|
|
| Iadd -> " + "
|
|
|
|
| Isub -> " - "
|
|
|
|
| Imul -> " * "
|
|
|
|
| Idiv -> " div "
|
|
|
|
| Imod -> " mod "
|
|
|
|
| Iand -> " & "
|
|
|
|
| Ior -> " | "
|
|
|
|
| Ixor -> " ^ "
|
|
|
|
| Ilsl -> " << "
|
|
|
|
| Ilsr -> " >>u "
|
|
|
|
| Iasr -> " >>s "
|
1995-06-15 01:17:29 -07:00
|
|
|
| Icomp cmp -> intcomp cmp
|
2000-04-21 01:13:22 -07:00
|
|
|
| Icheckbound -> " check > "
|
|
|
|
|
|
|
|
let test tst ppf arg =
|
1995-06-15 01:17:29 -07:00
|
|
|
match tst with
|
2000-04-21 01:13:22 -07:00
|
|
|
| Itruetest -> reg ppf arg.(0)
|
|
|
|
| Ifalsetest -> fprintf ppf "not %a" reg arg.(0)
|
|
|
|
| Iinttest cmp -> fprintf ppf "%a%s%a" reg arg.(0) (intcomp cmp) reg arg.(1)
|
|
|
|
| Iinttest_imm(cmp, n) -> fprintf ppf "%a%s%i" reg arg.(0) (intcomp cmp) n
|
1995-11-28 06:25:57 -08:00
|
|
|
| Ifloattest(cmp, neg) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "%s%a%s%a"
|
|
|
|
(if neg then "not " else "")
|
|
|
|
reg arg.(0) (floatcomp cmp) reg arg.(1)
|
|
|
|
| Ieventest -> fprintf ppf "%a & 1 == 0" reg arg.(0)
|
|
|
|
| Ioddtest -> fprintf ppf "%a & 1 == 1" reg arg.(0)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let print_live = ref false
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let operation op arg ppf res =
|
|
|
|
if Array.length res > 0 then fprintf ppf "%a := " regs res;
|
1995-06-15 01:17:29 -07:00
|
|
|
match op with
|
2000-04-21 01:13:22 -07:00
|
|
|
| Imove -> regs ppf arg
|
|
|
|
| Ispill -> fprintf ppf "%a (spill)" regs arg
|
|
|
|
| Ireload -> fprintf ppf "%a (reload)" regs arg
|
|
|
|
| Iconst_int n -> fprintf ppf "%s" (Nativeint.to_string n)
|
|
|
|
| Iconst_float s -> fprintf ppf "%s" s
|
|
|
|
| Iconst_symbol s -> fprintf ppf "\"%s\"" s
|
|
|
|
| Icall_ind -> fprintf ppf "call %a" regs arg
|
|
|
|
| Icall_imm lbl -> fprintf ppf "call \"%s\" %a" lbl regs arg
|
|
|
|
| Itailcall_ind -> fprintf ppf "tailcall %a" regs arg
|
|
|
|
| Itailcall_imm lbl -> fprintf ppf "tailcall \"%s\" %a" lbl regs arg
|
1995-07-10 02:48:27 -07:00
|
|
|
| Iextcall(lbl, alloc) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "extcall \"%s\" %a%s" lbl regs arg
|
|
|
|
(if not alloc then "" else " (noalloc)")
|
1995-06-15 01:17:29 -07:00
|
|
|
| Istackoffset n ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "offset stack %i" n
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iload(chunk, addr) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "%s[%a]"
|
|
|
|
(Printcmm.chunk chunk) (Arch.print_addressing reg addr) arg
|
1995-12-17 08:24:34 -08:00
|
|
|
| Istore(chunk, addr) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "%s[%a] := %a"
|
|
|
|
(Printcmm.chunk chunk)
|
|
|
|
(Arch.print_addressing reg addr)
|
|
|
|
(Array.sub arg 1 (Array.length arg - 1))
|
|
|
|
reg arg.(0)
|
|
|
|
| Ialloc n -> fprintf ppf "alloc %i" n
|
|
|
|
| Iintop(op) -> fprintf ppf "%a%s%a" reg arg.(0) (intop op) reg arg.(1)
|
|
|
|
| Iintop_imm(op, n) -> fprintf ppf "%a%s%i" reg arg.(0) (intop op) n
|
|
|
|
| Inegf -> fprintf ppf "-f %a" reg arg.(0)
|
|
|
|
| Iabsf -> fprintf ppf "absf %a" reg arg.(0)
|
|
|
|
| Iaddf -> fprintf ppf "%a +f %a" reg arg.(0) reg arg.(1)
|
|
|
|
| Isubf -> fprintf ppf "%a -f %a" reg arg.(0) reg arg.(1)
|
|
|
|
| Imulf -> fprintf ppf "%a *f %a" reg arg.(0) reg arg.(1)
|
|
|
|
| Idivf -> fprintf ppf "%a /f %a" reg arg.(0) reg arg.(1)
|
|
|
|
| Ifloatofint -> fprintf ppf "floatofint %a" reg arg.(0)
|
|
|
|
| Iintoffloat -> fprintf ppf "intoffloat %a" reg arg.(0)
|
1995-07-07 09:42:05 -07:00
|
|
|
| Ispecific op ->
|
2000-04-21 01:13:22 -07:00
|
|
|
Arch.print_specific_operation reg op ppf arg
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let rec instr ppf i =
|
1995-06-15 01:17:29 -07:00
|
|
|
if !print_live then begin
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<1>{%a" regsetaddr i.live;
|
|
|
|
if Array.length i.arg > 0 then fprintf ppf "@ +@ %a" regs i.arg;
|
|
|
|
fprintf ppf "}@]@,";
|
1995-06-15 01:17:29 -07:00
|
|
|
end;
|
|
|
|
begin match i.desc with
|
2000-04-21 01:13:22 -07:00
|
|
|
| Iend -> ()
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iop op ->
|
2000-04-21 01:13:22 -07:00
|
|
|
operation op i.arg ppf i.res
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ireturn ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "return %a" regs i.arg
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iifthenelse(tst, ifso, ifnot) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<v 2>if %a then@,%a" (test tst) i.arg instr ifso;
|
1995-06-15 01:17:29 -07:00
|
|
|
begin match ifnot.desc with
|
2000-04-21 01:13:22 -07:00
|
|
|
| Iend -> ()
|
|
|
|
| _ -> fprintf ppf "@;<0 -2>else@,%a" instr ifnot
|
1995-06-15 01:17:29 -07:00
|
|
|
end;
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@;<0 -2>endif@]"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iswitch(index, cases) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "switch %a" reg i.arg.(0);
|
1995-06-15 01:17:29 -07:00
|
|
|
for i = 0 to Array.length cases - 1 do
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@,@[<v 2>@[";
|
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
|
1995-06-15 01:17:29 -07:00
|
|
|
done;
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@]@,%a@]" instr cases.(i)
|
1995-06-15 01:17:29 -07:00
|
|
|
done;
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@,endswitch"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iloop(body) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<v 2>loop@,%a@;<0 -2>endloop@]" instr body
|
2000-08-11 12:50:59 -07:00
|
|
|
| Icatch(i, body, handler) ->
|
|
|
|
fprintf
|
|
|
|
ppf "@[<v 2>catch@,%a@;<0 -2>with(%d)@,%a@;<0 -2>endcatch@]"
|
|
|
|
instr body i instr handler
|
|
|
|
| Iexit i ->
|
|
|
|
fprintf ppf "exit(%d)" i
|
1995-06-15 01:17:29 -07:00
|
|
|
| Itrywith(body, handler) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<v 2>try@,%a@;<0 -2>with@,%a@;<0 -2>endtry@]"
|
|
|
|
instr body instr handler
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iraise ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "raise %a" reg i.arg.(0)
|
1995-06-15 01:17:29 -07:00
|
|
|
end;
|
2012-02-21 09:41:02 -08:00
|
|
|
if not (Debuginfo.is_none i.dbg) then
|
|
|
|
fprintf ppf "%s" (Debuginfo.to_string i.dbg);
|
1995-06-15 01:17:29 -07:00
|
|
|
begin match i.next.desc with
|
|
|
|
Iend -> ()
|
2000-04-21 01:13:22 -07:00
|
|
|
| _ -> fprintf ppf "@,%a" instr i.next
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let fundecl ppf f =
|
2012-02-21 09:41:02 -08:00
|
|
|
let dbg =
|
|
|
|
if Debuginfo.is_none f.fun_dbg then
|
|
|
|
""
|
|
|
|
else
|
|
|
|
" " ^ Debuginfo.to_string f.fun_dbg in
|
|
|
|
fprintf ppf "@[<v 2>%s(%a)%s@,%a@]"
|
|
|
|
f.fun_name regs f.fun_args dbg instr f.fun_body
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let phase msg ppf f =
|
|
|
|
fprintf ppf "*** %s@.%a@." msg fundecl f
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let interference ppf r =
|
2000-02-08 12:00:06 -08:00
|
|
|
let interf ppf =
|
|
|
|
List.iter
|
2000-04-21 01:13:22 -07:00
|
|
|
(fun r -> fprintf ppf "@ %a" reg r)
|
2000-02-08 12:00:06 -08:00
|
|
|
r.interf in
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>%a:%t@]@." reg r interf
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let interferences ppf () =
|
|
|
|
fprintf ppf "*** Interferences@.";
|
|
|
|
List.iter (interference ppf) (Reg.all_registers())
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let preference ppf r =
|
2000-02-08 12:00:06 -08:00
|
|
|
let prefs ppf =
|
|
|
|
List.iter
|
2000-04-21 01:13:22 -07:00
|
|
|
(fun (r, w) -> fprintf ppf "@ %a weight %i" reg r w)
|
2000-02-08 12:00:06 -08:00
|
|
|
r.prefer in
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "@[<2>%a: %t@]@." reg r prefs
|
2000-02-08 12:00:06 -08:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let preferences ppf () =
|
|
|
|
fprintf ppf "*** Preferences@.";
|
|
|
|
List.iter (preference ppf) (Reg.all_registers())
|