summaryrefslogtreecommitdiff
path: root/tools/dumpapprox.ml
blob: 2af4fb6af113a56693dd908339a5aba1d69f5236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(***********************************************************************)
(*                                                                     *)
(*                           Objective Caml                            *)
(*                                                                     *)
(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 1996 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(* $Id$ *)

(* Dump a .cmx file *)

open Config
open Format
open Clambda
open Compilenv

let print_digest d =
  for i = 0 to String.length d - 1 do
    print_string(Printf.sprintf "%02x" (Char.code d.[i]))
  done

let rec print_approx = function
    Value_closure(fundesc, approx) ->
      open_box 2;
      print_string "function "; print_string fundesc.fun_label;
      print_space(); print_string "arity "; print_int fundesc.fun_arity;
      if fundesc.fun_closed then begin
        print_space(); print_string "(closed)"
      end;
      if fundesc.fun_inline <> None then begin
        print_space(); print_string "(inline)"
      end;
      print_space(); print_string "->"; print_space();
      print_approx approx;
      close_box()
  | Value_tuple approx ->
      open_hvbox 1;
      print_string "[";
      for i = 0 to Array.length approx - 1 do
        if i > 0 then (print_string ";"; print_space());
        print_int i; print_string ": "; print_approx approx.(i)
      done;
      print_string "]";
      close_box()
  | Value_unknown ->
      print_string "_"
  | Value_integer n ->
      print_int n

let print_name_crc (name, crc) =
  print_space(); print_string name;
  print_string " ("; print_digest crc; print_string ")"

let print_infos (ui, crc) =
  print_string "Name: "; print_string ui.ui_name; print_newline();
  print_string "CRC of implementation: "; print_digest crc; print_newline();
  open_vbox 2;
  print_string "Interfaces imported:";
  List.iter print_name_crc ui.ui_imports_cmi;
  close_box(); print_newline();
  open_vbox 2;
  print_string "Implementations imported:";
  List.iter print_name_crc ui.ui_imports_cmx;
  close_box(); print_newline();
  open_vbox 2;
  print_string "Approximation:"; print_space(); print_approx ui.ui_approx;
  close_box(); print_newline();
  open_box 2;
  print_string "Currying functions:";
  List.iter
    (fun arity -> print_space(); print_int arity)
    ui.ui_curry_fun;
  close_box(); print_newline();
  open_box 2;
  print_string "Apply functions:";
  List.iter
    (fun arity -> print_space(); print_int arity)
    ui.ui_apply_fun;
  close_box(); print_newline()

let print_unit_info filename =
  let ic = open_in_bin filename in
  try
    let buffer = String.create (String.length cmx_magic_number) in
    really_input ic buffer 0 (String.length cmx_magic_number);
    if buffer = cmx_magic_number then begin
      let ui = (input_value ic : unit_infos) in
      let crc = Digest.input ic in
      close_in ic;
      print_infos (ui, crc)
    end else if buffer = cmxa_magic_number then begin
      let info_crc_list = (input_value ic : (unit_infos * Digest.t) list) in
      close_in ic;
      List.iter print_infos info_crc_list
    end else begin
      close_in ic;
      prerr_endline "Wrong magic number";
      exit 2
    end
  with End_of_file | Failure _ ->
    close_in ic;
    prerr_endline "Error reading file";
    exit 2

let main () =
  print_unit_info Sys.argv.(1);
  exit 0

let _ = main ()