Debian Codegraph @

checker/votour.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Values
12
13 (** {6 Interactive visit of a vo} *)
14
15 let max_string_length = 1024
16
17 type command =
18 | CmdParent
19 | CmdChild of int
20 | CmdSort
21 | CmdList
22 | CmdHelp
23 | CmdExit
24
25 let help () =
26 Printf.printf "Help\n\
27 <n>\tenter the <n>-th child\n\
28 u\tgo up 1 level\n\
29 s\tsort\n\
30 l\ttreat current node as a list\n\
31 x\texit\n\n%!"
32
33 let quit () =
34 Printf.printf "\nGoodbye!\n%!";
35 exit 0
36
37 let rec read_num max =
38 Printf.printf "# %!";
39 let l = try read_line () with End_of_file -> quit () in
40 match l with
41 | "u" -> CmdParent
42 | "s" -> CmdSort
43 | "x" -> CmdExit
44 | "h" -> CmdHelp
45 | "l" -> CmdList
46 | _ ->
47 match int_of_string l with
48 | v ->
49 if v < 0 || v >= max then
50 let () =
51 Printf.printf "Out-of-range input! (only %d children)\n%!" max in
52 read_num max
53 else CmdChild v
54 | exception Failure _ ->
55 Printf.printf "Unrecognized input! Input h for help\n%!";
56 read_num max
57
58 type 'a repr =
59 | INT of int
60 | STRING of string
61 | BLOCK of int * 'a array
62 | OTHER
63
64 module type S =
65 sig
66 type obj
67 val input : in_channel -> obj
68 val repr : obj -> obj repr
69 val size : obj -> int
70 val oid : obj -> int option
71 end
72
73 module ReprObj : S =
74 struct
75 type obj = Obj.t * int list
76
77 let input chan =
78 let obj = input_value chan in
79 let () = CObj.register_shared_size obj in
80 (obj, [])
81
82 let repr (obj, pos) =
83 if Obj.is_block obj then
84 let tag = Obj.tag obj in
85 if tag = Obj.string_tag then STRING (Obj.magic obj)
86 else if tag < Obj.no_scan_tag then
87 let init i = (Obj.field obj i, i :: pos) in
88 let data = Array.init (Obj.size obj) init in
89 BLOCK (tag, Obj.magic data)
90 else OTHER
91 else INT (Obj.magic obj)
92
93 let size (_, p) = CObj.shared_size_of_pos p
94 let oid _ = None
95 end
96
97 module ReprMem : S =
98 struct
99 open Analyze
100
101 type obj = data
102
103 let memory = ref LargeArray.empty
104 let sizes = ref LargeArray.empty
105 (** size, in words *)
106
107 let ws = Sys.word_size / 8
108
109 let rec init_size seen k = function
110 | Int _ | Atm _ | Fun _ -> k 0
111 | Ptr p ->
112 if LargeArray.get seen p then k 0
113 else
114 let () = LargeArray.set seen p true in
115 match LargeArray.get !memory p with
116 | Struct (tag, os) ->
117 let len = Array.length os in
118 let rec fold i accu k =
119 if i == len then k accu
120 else
121 init_size seen (fun n -> fold (succ i) (accu + 1 + n) k) os.(i)
122 in
123 fold 0 1 (fun size -> let () = LargeArray.set !sizes p size in k size)
124 | Int64 _ -> k 0
125 | Float64 _ -> k 0
126 | String s ->
127 let size = 2 + (String.length s / ws) in
128 let () = LargeArray.set !sizes p size in
129 k size
130
131 let size = function
132 | Int _ | Atm _ | Fun _ -> 0
133 | Ptr p -> LargeArray.get !sizes p
134
135 let repr = function
136 | Int i -> INT i
137 | Atm t -> BLOCK (t, [||])
138 | Fun _ -> OTHER
139 | Ptr p ->
140 match LargeArray.get !memory p with
141 | Struct (tag, os) -> BLOCK (tag, os)
142 | Int64 _ -> OTHER (* TODO: pretty-print int63 values *)
143 | Float64 _ -> OTHER (* TODO: pretty-print float64 values *)
144 | String s -> STRING s
145
146 let input ch =
147 let obj, mem = parse_channel ch in
148 let () = memory := mem in
149 let () = sizes := LargeArray.make (LargeArray.length mem) (-1) in
150 let seen = LargeArray.make (LargeArray.length mem) false in
151 let () = init_size seen ignore obj in
152 obj
153
154 let oid = function
155 | Int _ | Atm _ | Fun _ -> None
156 | Ptr p -> Some p
157 end
158
159 module Visit (Repr : S) :
160 sig
161 val init : unit -> unit
162 val visit : Values.value -> Repr.obj -> int list -> unit
163 end =
164 struct
165
166 (** Name of a value *)
167
168 let rec get_name ?(extra=false) v = match kind v with
169 |Any -> "?"
170 |Fail s -> "Invalid node: "^s
171 |Tuple (name,_) -> name
172 |Sum (name,_,_) -> name
173 |Array v -> "array"^(if extra then "/"^get_name ~extra v else "")
174 |List v -> "list"^(if extra then "/"^get_name ~extra v else "")
175 |Opt v -> "option"^(if extra then "/"^get_name ~extra v else "")
176 |Int -> "int"
177 |String -> "string"
178 |Annot (s,v) -> s^"/"^get_name ~extra v
179 | Int64 -> "Int64"
180 | Float64 -> "Float64"
181
182 (** For tuples, its quite handy to display the inner 1st string (if any).
183 Cf. [structure_body] for instance *)
184
185 exception TupleString of string
186 let get_string_in_tuple o =
187 try
188 for i = 0 to Array.length o - 1 do
189 match Repr.repr o.(i) with
190 | STRING s ->
191 let len = min max_string_length (String.length s) in
192 raise (TupleString (Printf.sprintf " [..%s..]" (String.sub s 0 len)))
193 | _ -> ()
194 done;
195 ""
196 with TupleString s -> s
197
198 (** Some details : tags, integer value for non-block, etc etc *)
199
200 let rec get_details v o = match kind v, Repr.repr o with
201 | (String | Any), STRING s ->
202 let len = min max_string_length (String.length s) in
203 Printf.sprintf " [%s]" (String.escaped (String.sub s 0 len))
204 |Tuple (_,v), BLOCK (_, o) -> get_string_in_tuple o
205 |(Sum _|Any), BLOCK (tag, _) ->
206 Printf.sprintf " [tag=%i]" tag
207 |(Sum _|Any), INT i ->
208 Printf.sprintf " [imm=%i]" i
209 |Int, INT i -> Printf.sprintf " [imm=%i]" i
210 |Annot (s,v), _ -> get_details v o
211 |_ -> ""
212
213 let get_oid obj = match Repr.oid obj with
214 | None -> ""
215 | Some id -> Printf.sprintf " [0x%08x]" id
216
217 let node_info (v,o,p) =
218 get_name ~extra:true v ^ get_details v o ^
219 " (size "^ string_of_int (Repr.size o)^"w)" ^ get_oid o
220
221 (** Children of a block : type, object, position.
222 For lists, we collect all elements of the list at once *)
223
224 let access_children vs os pos =
225 if Array.length os = Array.length vs then
226 Array.mapi (fun i v -> v, os.(i), i::pos) vs
227 else raise_notrace Exit
228
229 let access_list v o pos =
230 let rec loop o pos accu = match Repr.repr o with
231 | INT 0 -> List.rev accu
232 | BLOCK (0, [|hd; tl|]) ->
233 loop tl (1 :: pos) ((v, hd, 0 :: pos) :: accu)
234 | _ -> raise_notrace Exit
235 in
236 Array.of_list (loop o pos [])
237
238 let access_block o = match Repr.repr o with
239 | BLOCK (tag, os) -> (tag, os)
240 | _ -> raise_notrace Exit
241
242 (** raises Exit if the object has not the expected structure *)
243 exception Forbidden
244 let rec get_children v o pos = match kind v with
245 |Tuple (_, v) ->
246 let (_, os) = access_block o in
247 access_children v os pos
248 |Sum (_, _, vv) ->
249 begin match Repr.repr o with
250 | BLOCK (tag, os) -> access_children vv.(tag) os pos
251 | INT _ -> [||]
252 | _ -> raise_notrace Exit
253 end
254 |Array v ->
255 let (_, os) = access_block o in
256 access_children (Array.make (Array.length os) v) os pos
257 |List v -> access_list v o pos
258 |Opt v ->
259 begin match Repr.repr o with
260 | INT 0 -> [||]
261 | BLOCK (0, [|x|]) -> [|(v, x, 0 :: pos)|]
262 | _ -> raise_notrace Exit
263 end
264 | String ->
265 begin match Repr.repr o with
266 | STRING _ -> [||]
267 | _ -> raise_notrace Exit
268 end
269 | Int ->
270 begin match Repr.repr o with
271 | INT _ -> [||]
272 | _ -> raise_notrace Exit
273 end
274 |Annot (s,v) -> get_children v o pos
275 |Any -> raise_notrace Exit
276 | Fail s -> raise Forbidden
277 | Int64 -> raise_notrace Exit
278 | Float64 -> raise_notrace Exit
279
280 let get_children v o pos =
281 try get_children v o pos
282 with Exit -> match Repr.repr o with
283 | BLOCK (_, os) -> Array.mapi (fun i o -> v_any, o, i :: pos) os
284 | _ -> [||]
285
286 type info = {
287 nam : string;
288 typ : value;
289 obj : Repr.obj;
290 pos : int list
291 }
292
293 let stk = ref ([] : info list)
294
295 let init () = stk := []
296
297 let push name v o p = stk := { nam = name; typ = v; obj = o; pos = p } :: !stk
298
299 exception EmptyStack
300 let pop () = match !stk with
301 | i::s -> stk := s; i
302 | _ -> raise EmptyStack
303
304 let print_state v o pos children =
305 Printf.printf "\nDepth %d Pos %s Context %s\n"
306 (List.length !stk)
307 (String.concat "." (List.rev_map string_of_int pos))
308 (String.concat "/" (List.rev_map (fun i -> i.nam) !stk));
309 Printf.printf "-------------\n";
310 let nchild = Array.length children in
311 Printf.printf "Here: %s, %d child%s\n"
312 (node_info (v,o,pos)) nchild (if nchild = 0 then "" else "ren:");
313 Array.iter
314 (fun (i, vop) -> Printf.printf " %d: %s\n" i (node_info vop))
315 children;
316 Printf.printf "-------------\n"
317
318 let rec visit v o pos =
319 let children = get_children v o pos in
320 let children = Array.mapi (fun i vop -> (i, vop)) children in
321 let () = print_state v o pos children in
322 read_command v o pos children
323
324 and read_command v o pos children =
325 try
326 match read_num (Array.length children) with
327 | CmdParent -> let info = pop () in visit info.typ info.obj info.pos
328 | CmdChild child ->
329 let _, (v',o',pos') = children.(child) in
330 push (get_name v) v o pos;
331 visit v' o' pos'
332 | CmdSort ->
333 let children = get_children v o pos in
334 let children = Array.mapi (fun i vop -> (i, vop)) children in
335 let sort (_, (_, o, _)) (_, (_, o', _)) =
336 Int.compare (Repr.size o) (Repr.size o')
337 in
338 let sorted = Array.copy children in
339 let () = Array.sort sort sorted in
340 let () = print_state v o pos sorted in
341 read_command v o pos children
342 | CmdList -> visit (v_list v_any) o pos
343 | CmdHelp ->
344 let () = help () in
345 read_command v o pos children
346 | CmdExit -> quit ()
347 with
348 | EmptyStack -> ()
349 | Forbidden -> let info = pop () in visit info.typ info.obj info.pos
350 | Failure _ | Invalid_argument _ -> visit v o pos
351
352 end
353
354 (** Loading the vo *)
355
356 type header = {
357 magic : string;
358 (** Magic number of the marshaller *)
359 length : int;
360 (** Size on disk in bytes *)
361 size32 : int;
362 (** Size in words when loaded on 32-bit systems *)
363 size64 : int;
364 (** Size in words when loaded on 64-bit systems *)
365 objects : int;
366 (** Number of blocks defined in the marshalled structure *)
367 }
368
369 let dummy_header = {
370 magic = "\000\000\000\000";
371 length = 0;
372 size32 = 0;
373 size64 = 0;
374 objects = 0;
375 }
376
377 let parse_header chan =
378 let magic = really_input_string chan 4 in
379 let length = input_binary_int chan in
380 let objects = input_binary_int chan in
381 let size32 = input_binary_int chan in
382 let size64 = input_binary_int chan in
383 { magic; length; size32; size64; objects }
384
385 module ObjFile =
386 struct
387
388 type segment = {
389 name : string;
390 pos : int64;
391 len : int64;
392 hash : Digest.t;
393 mutable header : header;
394 }
395
396 let input_int32 ch =
397 let accu = ref 0l in
398 for _i = 0 to 3 do
399 let c = input_byte ch in
400 accu := Int32.add (Int32.shift_left !accu 8) (Int32.of_int c)
401 done;
402 !accu
403
404 let input_int64 ch =
405 let accu = ref 0L in
406 for _i = 0 to 7 do
407 let c = input_byte ch in
408 accu := Int64.add (Int64.shift_left !accu 8) (Int64.of_int c)
409 done;
410 !accu
411
412 let input_segment_summary ch =
413 let nlen = input_int32 ch in
414 let name = really_input_string ch (Int32.to_int nlen) in
415 let pos = input_int64 ch in
416 let len = input_int64 ch in
417 let hash = Digest.input ch in
418 { name; pos; len; hash; header = dummy_header }
419
420 let rec input_segment_summaries ch n accu =
421 if Int32.equal n 0l then Array.of_list (List.rev accu)
422 else
423 let s = input_segment_summary ch in
424 let accu = s :: accu in
425 input_segment_summaries ch (Int32.pred n) accu
426
427 let parse_segments ch =
428 let magic = input_int32 ch in
429 let version = input_int32 ch in
430 let summary_pos = input_int64 ch in
431 let () = LargeFile.seek_in ch summary_pos in
432 let nsum = input_int32 ch in
433 let seg = input_segment_summaries ch nsum [] in
434 for i = 0 to Array.length seg - 1 do
435 let () = LargeFile.seek_in ch seg.(i).pos in
436 let header = parse_header ch in
437 seg.(i).header <- header
438 done;
439 (magic, version, seg)
440
441 end
442
443 let visit_vo f =
444 Printf.printf "\nWelcome to votour !\n";
445 Printf.printf "Enjoy your guided tour of a Rocq .vo or .vi file\n";
446 Printf.printf "Object sizes are in words (%d bits)\n" Sys.word_size;
447 Printf.printf "Input h for help\n\n%!";
448 let known_segments = [
449 "summary", Values.v_libsum;
450 "library", Values.v_lib;
451 "opaques", Values.v_opaquetable;
452 "vmlibrary", Values.v_vmlib;
453 ] in
454 let repr =
455 if Sys.word_size = 64 then (module ReprMem : S) else (module ReprObj : S)
456 (* On 32-bit machines, representation may exceed the max size of arrays *)
457 in
458 let module Repr = (val repr : S) in
459 let module Visit = Visit(Repr) in
460 while true do
461 let ch = open_in_bin f in
462 let (_magic, version, segments) = ObjFile.parse_segments ch in
463 Printf.printf "File format: %ld\n%!" version;
464 Printf.printf "The file has %d segments, choose the one to visit:\n"
465 (Array.length segments);
466 Array.iteri (fun i ObjFile.{ name; pos; header } ->
467 let size = if Sys.word_size = 64 then header.size64 else header.size32 in
468 Printf.printf " %d: %s, starting at byte %Ld (size %iw)\n" i name pos size)
469 segments;
470 match read_num (Array.length segments) with
471 | CmdChild seg ->
472 let seg = segments.(seg) in
473 let open ObjFile in
474 LargeFile.seek_in ch seg.pos;
475 let o = Repr.input ch in
476 let () = Visit.init () in
477 let typ = try List.assoc seg.name known_segments with Not_found -> v_any in
478 Visit.visit typ o []
479 | CmdParent | CmdSort | CmdList -> ()
480 | CmdHelp ->
481 help ()
482 | CmdExit ->
483 quit ()
484 done
485
486 let () =
487 if not !Sys.interactive then
488 Arg.parse [] visit_vo
489 ("votour: guided tour of a Rocq .vo or .vi file\n"^
490 "Usage: votour file.v[oi]")