From e0818586981617f5c545713115001d76517c0e99 Mon Sep 17 00:00:00 2001 From: zapashcanon Date: Sat, 25 Nov 2023 23:29:11 +0100 Subject: [PATCH] clean some code --- src/assigned.ml | 13 +++---- src/check.ml | 2 +- src/choice_monad.ml | 14 ++++---- src/cmd_opt.ml | 4 +-- src/cmd_run.ml | 2 +- src/cmd_script.ml | 2 +- src/cmd_sym.ml | 35 ++++++++++--------- src/concrete.ml | 2 -- src/concrete_memory.ml | 2 -- src/concrete_memory.mli | 2 -- src/concrete_value.ml | 62 +++++++++++++++++++--------------- src/dune | 1 - src/env_id.ml | 21 +++++------- src/env_id.mli | 2 -- src/float32.ml | 3 +- src/float64.ml | 3 +- src/format.ml | 8 ++++- src/format.mli | 14 +++++++- src/func_id.ml | 6 ++-- src/func_id.mli | 2 -- src/grouped.ml | 2 +- src/indexed.ml | 4 +-- src/indexed.mli | 2 -- src/interpret.ml | 16 ++++----- src/interpret_functor_intf.ml | 2 -- src/link.ml | 4 +-- src/link_env.ml | 24 ++----------- src/link_env.mli | 4 --- src/log.ml | 28 ++++++++++----- src/log.mli | 59 ++++++++++++++++++++++++++++---- src/memory_limits.ml | 58 ------------------------------- src/menhir_parser.mly | 8 ++--- src/named.ml | 3 -- src/named.mli | 2 -- src/optimize.ml | 2 +- src/parse.ml | 4 +-- src/rewrite.ml | 4 +-- src/script.ml | 52 ++++++++++++++-------------- src/simplified.ml | 3 +- src/spectest.ml | 13 ++++--- src/stack.ml | 2 +- src/text.ml | 6 ++-- src/typecheck.ml | 11 +++--- src/types.ml | 24 +++++-------- test/fuzz/fuzzer.ml | 34 +++++++++---------- test/fuzz/interprets.ml | 8 ++--- test/print/dune | 6 ---- test/print/locals.t | 2 +- test/print/locals_drop.t | 3 +- test/print/locals_drop.wast | 1 - test/print/print_optimized.ml | 13 ------- test/print/print_simplified.ml | 2 +- test/print/print_text.ml | 2 +- 53 files changed, 274 insertions(+), 334 deletions(-) delete mode 100644 src/memory_limits.ml delete mode 100644 test/print/print_optimized.ml diff --git a/src/assigned.ml b/src/assigned.ml index 1e1acbde2..c3366ec18 100644 --- a/src/assigned.ml +++ b/src/assigned.ml @@ -121,13 +121,8 @@ let name kind ~get_name values = let check_type_id (types : simplified str_type Named.t) (check : Grouped.type_check) = let id, func_type = check in - let* id = - match id with - | Raw i -> Ok i - | Text name -> ( - match String_map.find_opt name types.named with - | None -> error_s "internal error: can't find type with name %s" name - | Some t -> Ok t ) + let id = + match id with Raw i -> i | Text name -> String_map.find name types.named in (* TODO more efficient version of that *) match Indexed.get_at id types.values with @@ -137,10 +132,10 @@ let check_type_id (types : simplified str_type Named.t) if not (equal_func_types func_type func_type') then Error "inline function type" else Ok () - | Some _ -> Error "TODO: Simplify.check_type_id" + | Some _ -> assert false let of_grouped (modul : Grouped.t) : t Result.t = - Log.debug "assigning ...@\n"; + Log.debug0 "assigning ...@\n"; let* typ = assign_types modul in let* global = name "global" diff --git a/src/check.ml b/src/check.ml index 36e549ae2..894ecf4dd 100644 --- a/src/check.ml +++ b/src/check.ml @@ -24,7 +24,7 @@ let empty_env () = } let modul m = - Log.debug "checking ...@\n"; + Log.debug0 "checking ...@\n"; let add_global = let seen = Hashtbl.create 512 in function diff --git a/src/choice_monad.ml b/src/choice_monad.ml index dc3161e5a..be5434c6e 100644 --- a/src/choice_monad.ml +++ b/src/choice_monad.ml @@ -23,13 +23,13 @@ let check (sym_bool : vbool) (state : Thread.t) : bool = | Val False -> true | _ -> let check = no :: pc in - Format.printf "CHECK:@.%a" - (Format.pp_list ~pp_sep:Format.pp_print_newline Expr.pp) + Format.pp_std "CHECK:@.%a" + (Format.pp_list ~pp_sep:Format.pp_newline Expr.pp) check; let module Solver = (val solver_module) in let r = Solver.check solver check in let msg = if r then "KO" else "OK" in - Format.printf "@./CHECK %s@." msg; + Format.pp_std "@./CHECK %s@." msg; not r (* TODO: make this a CLI flag ? *) @@ -77,7 +77,7 @@ struct | false, false -> M.empty | true, false | false, true -> M.return (sat_true, state) | true, true -> - if print_choice then Format.printf "CHOICE: %a@." Expr.pp v; + if print_choice then Format.pp_std "CHOICE: %a@." Expr.pp v; let state1 = Thread.clone { state with pc = with_v } in let state2 = Thread.clone { state with pc = with_not_v } in M.cons (true, state1) (M.return (false, state2)) ) @@ -125,7 +125,7 @@ struct match model with | None -> assert false (* ? *) | Some model -> ( - Format.printf "Model:@.%a@." Model.pp model; + Format.pp_std "Model:@.%a@." Model.pp model; let v = Model.evaluate model sym in match v with | None -> assert false (* ? *) @@ -390,7 +390,7 @@ module WQ = struct let take_as_producer q = Mutex.lock q.mutex; q.producers <- q.producers - 1; - (* Format.printf "TAKE COUNT %i@." q.producers; *) + (* Format.pp_std "TAKE COUNT %i@." q.producers; *) let r = try while Queue.is_empty q.queue do @@ -401,7 +401,7 @@ module WQ = struct q.producers <- q.producers + 1; Some v with Exit -> - (* Format.printf "@.@.TAKE EXIT@.@."; *) + (* Format.pp_std "@.@.TAKE EXIT@.@."; *) Condition.broadcast q.cond; None in diff --git a/src/cmd_opt.ml b/src/cmd_opt.ml index 090061b47..8eae8672e 100644 --- a/src/cmd_opt.ml +++ b/src/cmd_opt.ml @@ -14,7 +14,7 @@ let optimize_file ~unsafe filename = let cmd debug unsafe (file : string) = if debug then Log.debug_on := true; match optimize_file ~unsafe file with - | Ok modul -> Format.printf "%a@\n" Simplified.Pp.modul modul + | Ok modul -> Format.pp_std "%a@\n" Simplified.Pp.modul modul | Error e -> - Format.eprintf "%s@." e; + Format.pp_err "%s@." e; exit 1 diff --git a/src/cmd_run.ml b/src/cmd_run.ml index d117c1b85..d1540bdb2 100644 --- a/src/cmd_run.ml +++ b/src/cmd_run.ml @@ -36,5 +36,5 @@ let cmd profiling debug unsafe optimize files = match result with | Ok () -> () | Error e -> - Format.eprintf "%s@." e; + Format.pp_err "%s@\n" e; exit 1 diff --git a/src/cmd_script.ml b/src/cmd_script.ml index fcdc41621..1848aa310 100644 --- a/src/cmd_script.ml +++ b/src/cmd_script.ml @@ -19,5 +19,5 @@ let cmd profiling debug optimize files no_exhaustion = match result with | Ok () -> () | Error e -> - Format.eprintf "%s@." e; + Format.pp_err "%s@." e; exit 1 diff --git a/src/cmd_sym.ml b/src/cmd_sym.ml index ea7870cda..38fa26911 100644 --- a/src/cmd_sym.ml +++ b/src/cmd_sym.ml @@ -11,7 +11,7 @@ let print_path_condition = false let print_extern_module : Symbolic.P.extern_func Link.extern_module = let print_i32 (i : Value.int32) : unit Choice.t = - Printf.printf "%s\n%!" (Expr.to_string i); + Format.pp_std "%s@\n" (Expr.to_string i); Choice.return () in (* we need to describe their types *) @@ -47,7 +47,6 @@ let assert_extern_module : Symbolic.P.extern_func Link.extern_module = let names = [| "plop"; "foo"; "bar" |] let symbolic_extern_module : Symbolic.P.extern_func Link.extern_module = - let sprintf = Printf.sprintf in let sym_cnt = Atomic.make 0 in let mk_symbol = Encoding.Symbol.mk_symbol in let symbolic_i32 (i : Value.int32) : Value.int32 Choice.t = @@ -60,13 +59,13 @@ let symbolic_extern_module : Symbolic.P.extern_func Link.extern_module = in let id = Atomic.fetch_and_add sym_cnt 1 in let r = - Expr.mk_symbol @@ mk_symbol (Ty_bitv S32) (sprintf "%s_%i" name id) + Expr.mk_symbol @@ mk_symbol (Ty_bitv S32) (Format.sprintf "%s_%i" name id) in Choice.return r in let symbol ty () : Value.int32 Choice.t = let id = Atomic.fetch_and_add sym_cnt 1 in - let r = Expr.mk_symbol @@ mk_symbol ty (sprintf "symbol_%i" id) in + let r = Expr.mk_symbol @@ mk_symbol ty (Format.sprintf "symbol_%i" id) in Choice.return r in let assume_i32 (i : Value.int32) : unit Choice.t = @@ -250,21 +249,21 @@ let cmd profiling debug unsafe optimize workers no_stop_at_failure workspace (fun (result, thread) -> let pc = Thread.pc thread in if print_path_condition then - Format.printf "PATH CONDITION:@\n%a@\n" Expr.pp_list pc; + Format.pp_std "PATH CONDITION:@\n%a@\n" Expr.pp_list pc; let model = get_model solver pc in let result = match result with | Choice_monad_intf.EVal (Ok ()) -> None | EAssert assertion -> - Format.printf "Assert failure: %a@\n" Expr.pp assertion; - Format.printf "Model:@\n @[%a@]@\n" Encoding.Model.pp model; + Format.pp_std "Assert failure: %a@\n" Expr.pp assertion; + Format.pp_std "Model:@\n @[%a@]@\n" Encoding.Model.pp model; Some pc | ETrap tr -> - Format.printf "Trap: %s@\n" (Trap.to_string tr); - Format.printf "Model:@\n @[%a@]@\n" Encoding.Model.pp model; + Format.pp_std "Trap: %s@\n" (Trap.to_string tr); + Format.pp_std "Model:@\n @[%a@]@\n" Encoding.Model.pp model; Some pc | EVal (Error e) -> - Format.eprintf "Error: %s@\n" e; + Format.pp_err "Error: %s@\n" e; exit 1 in let testcase = @@ -280,18 +279,18 @@ let cmd profiling debug unsafe optimize workers no_stop_at_failure workspace let () = if no_stop_at_failure then let failures = Seq.fold_left (fun n _ -> succ n) 0 failing in - if failures = 0 then Format.printf "All OK@\n" - else Format.printf "Reached %i problems!@\n" failures + if failures = 0 then Format.pp_std "All OK@\n" + else Format.pp_err "Reached %i problems!@\n" failures else match failing () with - | Nil -> Format.printf "All OK@\n" - | Cons (_thread, _) -> Format.printf "Reached problem!@\n" + | Nil -> Format.pp_std "All OK@\n" + | Cons (_thread, _) -> Format.pp_err "Reached problem!@\n" in let time = !Thread.Solver.solver_time in let count = !Thread.Solver.solver_count in if print_solver_time then begin - Format.printf "@\n"; - Format.printf "Solver time %fs@\n" time; - Format.printf " calls %i@\n" count; - Format.printf " mean time %fms@\n" (1000. *. time /. float count) + Format.pp_std "@\n"; + Format.pp_std "Solver time %fs@\n" time; + Format.pp_std " calls %i@\n" count; + Format.pp_std " mean time %fms@\n" (1000. *. time /. float count) end diff --git a/src/concrete.ml b/src/concrete.ml index 3dbd78056..b04191690 100644 --- a/src/concrete.ml +++ b/src/concrete.ml @@ -94,8 +94,6 @@ module P = struct let drop_elem = Link_env.drop_elem let drop_data = Link_env.drop_data - - let pp = Link_env.pp end module Module_to_run = struct diff --git a/src/concrete_memory.ml b/src/concrete_memory.ml index 3ed4bc76e..fe5d145cb 100644 --- a/src/concrete_memory.ml +++ b/src/concrete_memory.ml @@ -62,8 +62,6 @@ let blit_string mem str ~src ~dst ~len = ( Bytes.unsafe_blit_string str src mem.data dst len; false ) -let get_data { data; _ } = data - let get_limit_max { limits; _ } = Option.map Int64.of_int limits.max let get_limits { limits; _ } = limits diff --git a/src/concrete_memory.mli b/src/concrete_memory.mli index 2e540529a..6c827a448 100644 --- a/src/concrete_memory.mli +++ b/src/concrete_memory.mli @@ -1,8 +1,6 @@ (** runtime memory *) type t -val get_data : t -> bytes - val get_limit_max : t -> int64 option val get_limits : t -> Types.limits diff --git a/src/concrete_value.ml b/src/concrete_value.ml index 1cab6aae6..bbd3b7e33 100644 --- a/src/concrete_value.ml +++ b/src/concrete_value.ml @@ -3,6 +3,7 @@ (* Copyright © 2021 Pierre Chambart *) open Types +open Format module Make_extern_func (V : Func_intf.Value_types) (M : Func_intf.Monad_type) = struct @@ -64,7 +65,7 @@ struct type t = Func_intf.t let fresh = - let r = ref (-1) in + let r = ref ~-1 in fun () -> incr r; !r @@ -76,24 +77,23 @@ struct (* | Extern (Extern_func (t, _f)) -> extern_type t *) end -module Concrete_value_types = struct - type int32 = Int32.t - - type int64 = Int64.t +module Func = struct + include + Make_extern_func + (struct + type int32 = Int32.t - type float32 = Float32.t + type int64 = Int64.t - type float64 = Float64.t + type float32 = Float32.t - type vbool = bool -end + type float64 = Float64.t -module Id_monad = struct - type 'a t = 'a -end - -module Func = struct - include Make_extern_func (Concrete_value_types) (Id_monad) + type vbool = bool + end) + (struct + type 'a t = 'a + end) end type externref = E : 'a Type.Id.t * 'a -> externref @@ -106,6 +106,11 @@ type ref_value = | Funcref of Func_intf.t option | Arrayref of unit Array.t option +let pp_ref_value fmt = function + | Externref _ -> pp fmt "externref" + | Funcref _ -> pp fmt "funcref" + | Arrayref _ -> pp fmt "array" + type t = | I32 of Int32.t | I64 of Int64.t @@ -113,6 +118,7 @@ type t = | F64 of Float64.t | Ref of ref_value +(* TODO: make a new kind of instr for this *) let of_instr (i : simplified instr) : t = match i with | I32_const c -> I32 c @@ -126,24 +132,22 @@ let to_instr = function | I64 c -> I64_const c | F32 c -> F32_const c | F64 c -> F64_const c - | _ -> assert false - -let pp_ref fmt = function - | Externref _ -> Format.fprintf fmt "externref" - | Funcref _ -> Format.fprintf fmt "funcref" - | Arrayref _ -> Format.fprintf fmt "array" + | Ref _ -> assert false let pp fmt = function - | I32 i -> Format.fprintf fmt "i32.const %ld" i - | I64 i -> Format.fprintf fmt "i64.const %Ld" i - | F32 f -> Format.fprintf fmt "f32.const %a" Float32.pp f - | F64 f -> Format.fprintf fmt "f64.const %a" Float64.pp f - | Ref r -> pp_ref fmt r + | I32 i -> pp fmt "i32.const %ld" i + | I64 i -> pp fmt "i64.const %Ld" i + | F32 f -> pp fmt "f32.const %a" Float32.pp f + | F64 f -> pp fmt "f64.const %a" Float64.pp f + | Ref r -> pp_ref_value fmt r let ref_null' = function | Func_ht -> Funcref None | Extern_ht -> Externref None - | _ -> failwith "TODO ref_null' Value.ml" + | Array_ht -> Arrayref None + | Any_ht | None_ht | Eq_ht | I31_ht | Struct_ht | No_func_ht | No_extern_ht + | Def_ht _ -> + assert false let ref_null typ = Ref (ref_null' typ) @@ -152,4 +156,6 @@ let ref_func (f : Func.t) : t = Ref (Funcref (Some f)) let ref_externref (type x) (t : x Type.Id.t) (v : x) : t = Ref (Externref (Some (E (t, v)))) -let ref_is_null = function Funcref None | Externref None -> true | _ -> false +let ref_is_null = function + | Funcref None | Externref None | Arrayref None -> true + | Funcref (Some _) | Externref (Some _) | Arrayref (Some _) -> false diff --git a/src/dune b/src/dune index 9003914c4..35b05291c 100644 --- a/src/dune +++ b/src/dune @@ -36,7 +36,6 @@ link link_env log - memory_limits menhir_parser named optimize diff --git a/src/env_id.ml b/src/env_id.ml index 32106d08f..e602c37d0 100644 --- a/src/env_id.ml +++ b/src/env_id.ml @@ -4,26 +4,23 @@ type t = int -module IMap = Map.Make (Int) +module Map = Map.Make (Int) type 'a collection = - { c : 'a IMap.t + { c : 'a Map.t ; last : int } -let empty = { c = IMap.empty; last = 0 } +let empty = { c = Map.empty; last = 0 } -let with_fresh_id f c = +let with_fresh_id f { c; last } = let open Syntax in - let last = c.last in - let* e, r = f last in - Ok ({ c = IMap.add c.last e c.c; last = c.last + 1 }, r) + let+ e, r = f last in + let c = Map.add last e c in + let last = succ last in + ({ c; last }, r) -let get i c = IMap.find i c.c - -let pp ppf i = Format.fprintf ppf "f_%i" i - -module Map = IMap +let get i c = Map.find i c.c module Tbl = Hashtbl.Make (struct include Int diff --git a/src/env_id.mli b/src/env_id.mli index d36db3e2d..eb1eb965d 100644 --- a/src/env_id.mli +++ b/src/env_id.mli @@ -9,8 +9,6 @@ val with_fresh_id : val get : t -> 'a collection -> 'a -val pp : Format.formatter -> t -> unit - module Map : Map.S with type key = t module Tbl : Hashtbl.S with type key = t diff --git a/src/float32.ml b/src/float32.ml index 81ae8e4b0..3cb667e14 100644 --- a/src/float32.ml +++ b/src/float32.ml @@ -309,6 +309,7 @@ let group_digits = Buffer.add_substring buf s exp (len - exp); Buffer.contents buf +(* TODO: convert all the following to a proper use of Format and stop concatenating strings *) let to_string' convert is_digit n x = (if x < Int32.zero then "-" else "") ^ @@ -322,4 +323,4 @@ let to_string' convert is_digit n x = let to_string = to_string' (Printf.sprintf "%.17g") is_digit 3 -let pp fmt v = Format.fprintf fmt "%s" (to_string v) +let pp fmt v = Format.pp_string fmt (to_string v) diff --git a/src/float64.ml b/src/float64.ml index 28122c7ad..516b20203 100644 --- a/src/float64.ml +++ b/src/float64.ml @@ -309,6 +309,7 @@ let group_digits = Buffer.add_substring buf s exp (len - exp); Buffer.contents buf +(* TODO: convert all the following to a proper use of Format and stop concatenating strings *) let to_string' convert is_digit n x = (if x < Int64.zero then "-" else "") ^ @@ -326,4 +327,4 @@ let to_hex_string x = if is_inf x then to_string x else to_string' (Printf.sprintf "%h") is_hex_digit 4 x -let pp fmt v = Format.fprintf fmt "%s" (to_string v) +let pp fmt v = Format.pp_string fmt (to_string v) diff --git a/src/format.ml b/src/format.ml index beb9881a6..8918f2584 100644 --- a/src/format.ml +++ b/src/format.ml @@ -2,6 +2,10 @@ include Stdlib.Format let pp = fprintf +let pp_err = eprintf + +let pp_std = printf + let pp_nothing _fmt () = () let pp_list = pp_print_list @@ -18,6 +22,8 @@ let pp_bool = pp_print_bool let pp_flush = pp_print_flush -let pp_space fmt () = pp fmt " " +let pp_space fmt () = pp_string fmt " " + +let pp_newline fmt () = pp fmt "@\n" let pp_int = pp_print_int diff --git a/src/format.mli b/src/format.mli index 85b1992d7..d939e2864 100644 --- a/src/format.mli +++ b/src/format.mli @@ -1,7 +1,11 @@ -include module type of Stdlib.Format +type formatter = Stdlib.Format.formatter val pp : formatter -> ('a, formatter, unit) format -> 'a +val pp_err : ('a, formatter, unit) format -> 'a + +val pp_std : ('a, formatter, unit) format -> 'a + val pp_nothing : formatter -> unit -> unit val pp_space : formatter -> unit -> unit @@ -42,3 +46,11 @@ val pp_option : -> formatter -> 'a option -> unit + +val pp_newline : formatter -> unit -> unit + +val sprintf : ('a, unit, string) format -> 'a + +val asprintf : ('a, formatter, unit, string) format4 -> 'a + +val kasprintf : (string -> 'a) -> ('b, formatter, unit, 'a) format4 -> 'b diff --git a/src/func_id.ml b/src/func_id.ml index 286c94c42..5e14a48bd 100644 --- a/src/func_id.ml +++ b/src/func_id.ml @@ -15,7 +15,9 @@ type 'a collection = let empty = { c = IMap.empty; last = 0 } -let add f t c = (c.last, { c = IMap.add c.last (f, t) c.c; last = c.last + 1 }) +let add f t { last; c } = + let c = IMap.add last (f, t) c in + (last, { c; last = succ last }) let get i c = let v, _ = IMap.find i c.c in @@ -24,5 +26,3 @@ let get i c = let get_typ i c = let _, t = IMap.find i c.c in t - -let pp ppf i = Format.fprintf ppf "f_%i" i diff --git a/src/func_id.mli b/src/func_id.mli index 0e45b7b05..62416d618 100644 --- a/src/func_id.mli +++ b/src/func_id.mli @@ -11,5 +11,3 @@ val add : 'a -> simplified func_type -> 'a collection -> t * 'a collection val get : t -> 'a collection -> 'a val get_typ : t -> 'a collection -> simplified func_type - -val pp : Format.formatter -> t -> unit diff --git a/src/grouped.ml b/src/grouped.ml index cf16b62c4..cc6a4827a 100644 --- a/src/grouped.ml +++ b/src/grouped.ml @@ -112,7 +112,7 @@ let check_limit { min; max } = else Ok () let of_symbolic (modul : Text.modul) : t Result.t = - Log.debug "grouping ...@\n"; + Log.debug0 "grouping ...@\n"; let add ((fields : t), curr) field : (t * curr) Result.t = match field with | Text.MType typ -> diff --git a/src/indexed.ml b/src/indexed.ml index 1dc3af078..35862d469 100644 --- a/src/indexed.ml +++ b/src/indexed.ml @@ -11,8 +11,6 @@ let get v = v.value let get_index v = v.index -let bind v f = f v.value - let map f v = { index = v.index; value = f v.value } let return index value = { index; value } @@ -28,4 +26,4 @@ let get_at i values = | None -> None | Some { value; _ } -> Some value -let pp f fmt v = Format.fprintf fmt "%a" f v.value +let pp f fmt v = f fmt v.value diff --git a/src/indexed.mli b/src/indexed.mli index 9c9922add..44d0d4009 100644 --- a/src/indexed.mli +++ b/src/indexed.mli @@ -4,8 +4,6 @@ val get : 'a t -> 'a val get_index : 'a t -> int -val bind : 'a t -> ('a -> 'b t) -> 'b t - val map : ('a -> 'b) -> 'a t -> 'b t val return : int -> 'a -> 'a t diff --git a/src/interpret.ml b/src/interpret.ml index 5d5586eb0..b48cfbadc 100644 --- a/src/interpret.ml +++ b/src/interpret.ml @@ -618,20 +618,20 @@ module Make (P : Interpret_functor_intf.P) : match l with | [] -> () | _ :: _ -> - Format.fprintf ppf "@ @[calls@ %a@]" + Format.pp ppf "@ @[calls@ %a@]" (Format.pp_list - ~pp_sep:(fun ppf () -> Format.fprintf ppf "@ ") + ~pp_sep:(fun ppf () -> Format.pp ppf "@ ") (fun ppf ((Raw id : simplified indice), count) -> let name ppf = function | None -> () - | Some name -> Format.fprintf ppf " %s" name + | Some name -> Format.pp ppf " %s" name in - Format.fprintf ppf "@[id %i%a@ %a@]" id name count.name + Format.pp ppf "@[id %i%a@ %a@]" id name count.name print_count count ) ) l in - Format.fprintf ppf "@[enter %i@ intrs %i%a@]" count.enter - count.instructions calls count.calls + Format.pp ppf "@[enter %i@ intrs %i%a@]" count.enter count.instructions + calls count.calls let empty_count name = { name; enter = 0; instructions = 0; calls = Hashtbl.create 0 } @@ -1547,14 +1547,14 @@ module Make (P : Interpret_functor_intf.P) : exec_expr envs env (State.Locals.of_list []) Stack.empty to_run None in - Log.profile "Exec module %s@.%a@." + Log.profile3 "Exec module %s@.%a@." (Option.value (Module_to_run.modul modul).id ~default:"anonymous" ) State.print_count count; match end_stack with | [] -> Choice.return () | _ :: _ -> - Format.eprintf "non empty stack@\n%a@." Stack.pp end_stack; + Format.pp_err "non empty stack@\n%a@." Stack.pp end_stack; assert false ) (Choice.return ()) (Module_to_run.to_run modul) diff --git a/src/interpret_functor_intf.ml b/src/interpret_functor_intf.ml index 330b6f4a6..f0c23c916 100644 --- a/src/interpret_functor_intf.ml +++ b/src/interpret_functor_intf.ml @@ -192,8 +192,6 @@ module type P = sig val drop_elem : elem -> unit val drop_data : data -> unit - - val pp : Format.formatter -> t -> unit end module Module_to_run : sig diff --git a/src/link.ml b/src/link.ml index c74a54728..bf57c0655 100644 --- a/src/link.ml +++ b/src/link.ml @@ -55,7 +55,7 @@ let load_from_module ls f (import : _ Imported.t) = | exports -> ( match StringMap.find import.name (f exports) with | exception Not_found -> - Log.debug "unknown import %s" import.name; + Log.debug1 "unknown import %s" import.name; if StringSet.mem import.name exports.defined_names then Error "incompatible import type (Link.load_from_module)" else Error "unknown import" @@ -305,7 +305,7 @@ let populate_exports env (exports : Simplified.exports) : exports Result.t = Ok { globals; memories; tables; functions; defined_names = names } let modul (ls : 'f state) ~name (modul : Simplified.modul) = - Log.debug "linking ...@\n"; + Log.debug0 "linking ...@\n"; let* envs, (env, init_active_data, init_active_elem) = Env_id.with_fresh_id (fun env_id -> diff --git a/src/link_env.ml b/src/link_env.ml index d3973eeb2..4dbc8ed6b 100644 --- a/src/link_env.ml +++ b/src/link_env.ml @@ -29,19 +29,6 @@ type 'ext t = ; id : Env_id.t } -let pp fmt t = - let global fmt (id, (global : Concrete_global.t)) = - Format.pp fmt "%a -> %a" Format.pp_int id Concrete_value.pp global.value - in - let func fmt (id, (_func : Concrete_value.Func.t)) = - Format.pp fmt "%a -> func" Format.pp_print_int id - in - Format.fprintf fmt "@[{@ (globals %a)@ (functions %a)@ }@]" - (Format.pp_list ~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ") global) - (IMap.bindings t.globals) - (Format.pp_list ~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ") func) - (IMap.bindings t.functions) - let id (env : _ t) = env.id let get_global (env : _ t) id = IMap.find id env.globals @@ -58,13 +45,6 @@ let get_elem (env : _ t) id = IMap.find id env.elem let get_extern_func env id = Func_id.get id env.extern_funcs -let get_func_typ env f = - match f with - | Func_intf.WASM (_, func, _) -> - let (Bt_raw ((None | Some _), t)) = func.type_f in - t - | Extern id -> Func_id.get_typ id env.extern_funcs - module Build = struct type t = { globals : Concrete_global.t IMap.t @@ -103,7 +83,7 @@ module Build = struct let get_global (env : t) id = match IMap.find_opt id env.globals with | None -> - (* Log.debug "%a@." pp env; *) + (* Log.debug2 "%a@." pp env; *) Error "unknown global" | Some v -> Ok v @@ -116,7 +96,7 @@ module Build = struct let get_func (env : t) id = match IMap.find_opt id env.functions with | None -> - (* Log.debug "%a@." pp env; *) + (* Log.debug2 "%a@." pp env; *) error_s "unknown function %a" Format.pp_int id | Some v -> Ok v end diff --git a/src/link_env.mli b/src/link_env.mli index 95892175b..b6abd531b 100644 --- a/src/link_env.mli +++ b/src/link_env.mli @@ -28,12 +28,8 @@ val drop_data : data -> unit val get_extern_func : 'ext t -> Func_id.t -> 'ext -val get_func_typ : _ t -> func -> simplified func_type - val id : _ t -> Env_id.t -val pp : Format.formatter -> _ t -> unit - module Build : sig type t diff --git a/src/log.ml b/src/log.ml index 92a783bc3..099899726 100644 --- a/src/log.ml +++ b/src/log.ml @@ -6,19 +6,29 @@ let debug_on = ref false let profiling_on = ref false -let on_debug f = if !debug_on then f () +let debug0 t : unit = if !debug_on then Format.pp_err t -let debug t = - if !debug_on then Format.eprintf t else Format.ifprintf Format.err_formatter t +let debug1 t a : unit = if !debug_on then Format.pp_err t a -let debug0 t : unit = if !debug_on then Format.eprintf t +let debug2 t a b : unit = if !debug_on then Format.pp_err t a b -let debug1 t a : unit = if !debug_on then Format.eprintf t a +let debug3 t a b c : unit = if !debug_on then Format.pp_err t a b c -let debug2 t a b : unit = if !debug_on then Format.eprintf t a b +let debug4 t a b c d : unit = if !debug_on then Format.pp_err t a b c d -let profile t = - if !profiling_on then Format.eprintf t - else Format.ifprintf Format.err_formatter t +let debug5 t a b c d e : unit = if !debug_on then Format.pp_err t a b c d e + +let profile0 t : unit = if !profiling_on then Format.pp_err t + +let profile1 t a : unit = if !profiling_on then Format.pp_err t a + +let profile2 t a b : unit = if !profiling_on then Format.pp_err t a b + +let profile3 t a b c : unit = if !profiling_on then Format.pp_err t a b c + +let profile4 t a b c d : unit = if !profiling_on then Format.pp_err t a b c d + +let profile5 t a b c d e : unit = + if !profiling_on then Format.pp_err t a b c d e let err f = Format.kasprintf failwith f diff --git a/src/log.mli b/src/log.mli index 23cc35ca9..48dc44df3 100644 --- a/src/log.mli +++ b/src/log.mli @@ -6,9 +6,6 @@ val debug_on : bool ref (** wether profiling printing is enabled or not *) val profiling_on : bool ref -(** execute the function only when debugging is on *) -val on_debug : (unit -> unit) -> unit - (** print some debug info *) val debug0 : (unit, Format.formatter, unit) format -> unit @@ -17,11 +14,61 @@ val debug1 : ('a -> unit, Format.formatter, unit) format -> 'a -> unit val debug2 : ('a -> 'b -> unit, Format.formatter, unit) format -> 'a -> 'b -> unit -(** print some debug info *) -val debug : ('a, Format.formatter, unit) format -> 'a +val debug3 : + ('a -> 'b -> 'c -> unit, Format.formatter, unit) format + -> 'a + -> 'b + -> 'c + -> unit + +val debug4 : + ('a -> 'b -> 'c -> 'd -> unit, Format.formatter, unit) format + -> 'a + -> 'b + -> 'c + -> 'd + -> unit + +val debug5 : + ('a -> 'b -> 'c -> 'd -> 'e -> unit, Format.formatter, unit) format + -> 'a + -> 'b + -> 'c + -> 'd + -> 'e + -> unit (** print some profiling info *) -val profile : ('a, Format.formatter, unit) format -> 'a +val profile0 : (unit, Format.formatter, unit) format -> unit + +val profile1 : ('a -> unit, Format.formatter, unit) format -> 'a -> unit + +val profile2 : + ('a -> 'b -> unit, Format.formatter, unit) format -> 'a -> 'b -> unit + +val profile3 : + ('a -> 'b -> 'c -> unit, Format.formatter, unit) format + -> 'a + -> 'b + -> 'c + -> unit + +val profile4 : + ('a -> 'b -> 'c -> 'd -> unit, Format.formatter, unit) format + -> 'a + -> 'b + -> 'c + -> 'd + -> unit + +val profile5 : + ('a -> 'b -> 'c -> 'd -> 'e -> unit, Format.formatter, unit) format + -> 'a + -> 'b + -> 'c + -> 'd + -> 'e + -> unit (** print some error and exit *) val err : ('a, Format.formatter, unit, 'b) format4 -> 'a diff --git a/src/memory_limits.ml b/src/memory_limits.ml deleted file mode 100644 index ae0c9d739..000000000 --- a/src/memory_limits.ml +++ /dev/null @@ -1,58 +0,0 @@ -(* SPDX-License-Identifier: AGPL-3.0-or-later *) -(* Copyright © 2021 Léo Andrès *) -(* Copyright © 2021 Pierre Chambart *) - -open Types - -let page_size = 65_536 - -module Make (M : Interpret_functor_intf.Memory_data) = struct - type t = - { id : int - ; label : string option - ; limits : limits - ; mutable data : M.t - } - - let fresh = - let r = ref (-1) in - fun () -> - incr r; - !r - - let update_memory mem data = mem.data <- data - - let get_limit_max { limits; _ } = limits.max - - let get_limits { limits; _ } = limits - - let load_8_s mem addr = M.load_8_s mem.data addr - - let load_8_u mem addr = M.load_8_u mem.data addr - - let load_16_s mem addr = M.load_16_s mem.data addr - - let load_16_u mem addr = M.load_16_u mem.data addr - - let load_32 mem addr = M.load_32 mem.data addr - - let load_64 mem addr = M.load_64 mem.data addr - - let store_8 mem ~addr value = M.store_8 mem.data ~addr value - - let store_16 mem ~addr value = M.store_16 mem.data ~addr value - - let store_32 mem ~addr value = M.store_32 mem.data ~addr value - - let store_64 mem ~addr value = M.store_64 mem.data ~addr value - - let create label limits size = - { id = fresh (); label; limits; data = M.create size } - - let grow t size = M.grow t.data size - (* let limits = *) - (* { mem.limits with min = max mem.limits.min (size / page_size) } *) - (* in *) - (* mem.limits <- limits; *) - (* t.data <- data *) -end diff --git a/src/menhir_parser.mly b/src/menhir_parser.mly index c1f060dd2..45090e580 100644 --- a/src/menhir_parser.mly +++ b/src/menhir_parser.mly @@ -754,7 +754,7 @@ let func == | MExport e -> MExport { e with desc = Export_func func_id } | MFunc f -> MFunc { f with id } | MData _ | MElem _ | MGlobal _ | MStart _ | MType _ | MTable _ | MMem _ as field -> begin - Format.eprintf "got invalid field: `%a`@." Pp.module_field field; + Format.pp_err "got invalid field: `%a`@." Pp.module_field field; assert false end ) func_fields @@ -875,7 +875,7 @@ let table == | Import_func _ | Import_global _ | Import_mem _ -> assert false end | MMem _ | MData _ | MStart _ | MFunc _ | MGlobal _ | MType _ as field -> begin - Format.eprintf "got invalid field: `%a`@." Pp.module_field field; + Format.pp_err "got invalid field: `%a`@." Pp.module_field field; assert false end ) table_fields @@ -922,7 +922,7 @@ let memory == | Import_table _ | Import_func _ | Import_global _ -> assert false end | MElem _ | MType _ | MTable _ | MFunc _ | MGlobal _ | MStart _ as field -> begin - Format.eprintf "got invalid field: `%a`@." Pp.module_field field; + Format.pp_err "got invalid field: `%a`@." Pp.module_field field; assert false end ) memory_fields @@ -957,7 +957,7 @@ let global == | Import_mem _ | Import_table _ | Import_func _ -> assert false end | MStart _ | MFunc _ | MData _ | MElem _ | MMem _ | MTable _ | MType _ as field -> begin - Format.eprintf "got invalid field: `%a`@." Pp.module_field field; + Format.pp_err "got invalid field: `%a`@." Pp.module_field field; assert false end ) global_fields diff --git a/src/named.ml b/src/named.ml index b016d4682..260c4ddc1 100644 --- a/src/named.ml +++ b/src/named.ml @@ -13,9 +13,6 @@ let fold f v acc = (fun acc v -> f (Indexed.get_index v) (Indexed.get v) acc) acc v.values -let iter f v = - List.iter (fun v -> f (Indexed.get_index v) (Indexed.get v)) v.values - let map f v = let values = List.map f v.values in { v with values } diff --git a/src/named.mli b/src/named.mli index 28e68f641..5bcda1ef1 100644 --- a/src/named.mli +++ b/src/named.mli @@ -5,6 +5,4 @@ type 'a t = val fold : (int -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b -val iter : (int -> 'a -> unit) -> 'a t -> unit - val map : ('a Indexed.t -> 'b Indexed.t) -> 'a t -> 'b t diff --git a/src/optimize.ml b/src/optimize.ml index 8fbdd7118..f5b542d13 100644 --- a/src/optimize.ml +++ b/src/optimize.ml @@ -523,6 +523,6 @@ let optimize_runtime_func f = let optimize_funcs funs = Named.map optimize_runtime_func funs let modul m = - Log.debug "optimizing ...@\n"; + Log.debug0 "optimizing ...@\n"; let func = optimize_funcs m.func in { m with func } diff --git a/src/parse.ml b/src/parse.ml index a74f3c20d..a7994beb4 100644 --- a/src/parse.ml +++ b/src/parse.ml @@ -22,10 +22,10 @@ struct Printf.sprintf "File \"%s\", line %i, character %i:" pos.pos_fname pos.pos_lnum cpos in - Log.debug "%s@\n%s@\n" file_line msg + Log.debug2 "%s@\n%s@\n" file_line msg in fun buf -> - Log.debug "parsing ...@\n"; + Log.debug0 "parsing ...@\n"; let provider () = let tok = Lexer.token buf in let start, stop = Sedlexing.lexing_positions buf in diff --git a/src/rewrite.ml b/src/rewrite.ml index e4e0700ca..ddc22a386 100644 --- a/src/rewrite.ml +++ b/src/rewrite.ml @@ -355,7 +355,7 @@ let rewrite_expr (modul : Assigned.t) (locals : simplified param list) | ( Array_new_data _ | Array_new _ | Array_new_elem _ | Array_new_fixed _ | Array_get_u _ | Struct_get _ | Struct_get_s _ | Struct_set _ | Struct_new _ | Br_on_non_null _ | Br_on_null _ ) as i -> - Log.debug "TODO (Rewrite.body) %a@\n" Text.Pp.instr i; + Log.debug2 "TODO (Rewrite.body) %a@\n" Text.Pp.instr i; Ok Nop and expr (e : text expr) (loop_count, block_ids) : simplified expr Result.t = list_map (fun i -> body (loop_count, block_ids) i) e @@ -501,7 +501,7 @@ let rewrite_named f named = { named with Named.values } let modul (modul : Assigned.t) : Simplified.modul Result.t = - Log.debug "rewriting ...@\n"; + Log.debug0 "rewriting ...@\n"; let* (global : (Simplified.global, simplified global_type) Runtime.t Named.t) = let* { Named.named; values } = diff --git a/src/script.ml b/src/script.ml index dffd2fa82..2d3da5be8 100644 --- a/src/script.ml +++ b/src/script.ml @@ -22,16 +22,16 @@ let check_error ~expected ~got = && (expected = "i32 constant out of range" || expected = "i32 constant") in if not ok then begin - Log.debug "expected: `%s`@." expected; - Log.debug "got : `%s`@." got; + Log.debug1 "expected: `%s`@." expected; + Log.debug1 "got : `%s`@." got; Error got end else Ok () let check_error_result expected = function | Ok _whatever -> - Log.debug "expected: `%s`@." expected; - Log.debug "got : Ok@."; + Log.debug1 "expected: `%s`@." expected; + Log.debug0 "got : Ok@."; error_s "expected Error (%S) but got Ok" expected | Error got -> check_error ~expected ~got @@ -102,8 +102,8 @@ let compare_result_const result (const : Concrete_value.t) = | Result_const (Literal (Const_host _)), _ -> false | _ -> - Log.debug "TODO (Script.compare_result_const)@\n"; - false + Log.debug0 "TODO (Script.compare_result_const)@\n"; + assert false let value_of_const : text const -> V.t Result.t = function | Const_I32 v -> ok @@ Concrete_value.I32 v @@ -115,12 +115,12 @@ let value_of_const : text const -> V.t Result.t = function Concrete_value.ref_null rt | Const_extern i -> ok @@ Concrete_value.Ref (Host_externref.value i) | i -> - Log.debug "TODO (Script.value_of_const) %a@\n" Text.Pp.const i; - ok @@ Concrete_value.I32 (Int32.of_int 666) + Log.debug2 "TODO (Script.value_of_const) %a@\n" Text.Pp.const i; + assert false let action (link_state : Concrete_value.Func.extern_func Link.state) = function | Text.Invoke (mod_id, f, args) -> begin - Log.debug "invoke %a %s %a...@\n" + Log.debug5 "invoke %a %s %a...@\n" (Format.pp_option ~none:Format.pp_nothing Format.pp_string) mod_id f Text.Pp.consts args; let* f, env_id = load_func_from_module link_state mod_id f in @@ -130,7 +130,7 @@ let action (link_state : Concrete_value.Func.extern_func Link.state) = function ~envs:link_state.envs f end | Get (mod_id, name) -> - Log.debug "get...@\n"; + Log.debug0 "get...@\n"; let+ global = load_global_from_module link_state mod_id name in [ global.value ] @@ -149,7 +149,7 @@ let run ~no_exhaustion ~optimize script = (fun (link_state : Concrete_value.Func.extern_func Link.state) -> function | Text.Module m -> if !curr_module = 0 then Log.debug_on := false; - Log.debug "*** module@\n"; + Log.debug0 "*** module@\n"; incr curr_module; let+ link_state = Compile.until_interpret link_state ~unsafe ~optimize ~name:None m @@ -157,7 +157,7 @@ let run ~no_exhaustion ~optimize script = Log.debug_on := debug_on; link_state | Assert (Assert_trap_module (m, expected)) -> - Log.debug "*** assert_trap@\n"; + Log.debug0 "*** assert_trap@\n"; incr curr_module; let* m, link_state = Compile.until_link link_state ~unsafe ~optimize ~name:None m @@ -168,11 +168,11 @@ let run ~no_exhaustion ~optimize script = in link_state | Assert (Assert_malformed_binary _) -> - Log.debug "*** assert_malformed_binary@\n"; + Log.debug0 "*** assert_malformed_binary@\n"; (* TODO: check this when binary format is supported *) Ok link_state | Assert (Assert_malformed_quote (m, expected)) -> - Log.debug "*** assert_malformed_quote@\n"; + Log.debug0 "*** assert_malformed_quote@\n"; let+ () = match Parse.Script.from_string (String.concat "\n" m) with | Error got -> check_error ~expected ~got @@ -186,11 +186,11 @@ let run ~no_exhaustion ~optimize script = in link_state | Assert (Assert_invalid_binary _) -> - Log.debug "*** assert_invalid_binary@\n"; + Log.debug0 "*** assert_invalid_binary@\n"; (* TODO: check this when binary format is supported *) Ok link_state | Assert (Assert_invalid (m, expected)) -> - Log.debug "*** assert_invalid@\n"; + Log.debug0 "*** assert_invalid@\n"; let+ () = match Compile.until_link link_state ~unsafe ~optimize ~name:None m @@ -200,39 +200,39 @@ let run ~no_exhaustion ~optimize script = in link_state | Assert (Assert_invalid_quote (m, expected)) -> - Log.debug "*** assert_invalid_quote@\n"; + Log.debug0 "*** assert_invalid_quote@\n"; let got = Parse.Script.from_string (String.concat "\n" m) in let+ () = check_error_result expected got in link_state | Assert (Assert_unlinkable (m, expected)) -> - Log.debug "*** assert_unlinkable@\n"; + Log.debug0 "*** assert_unlinkable@\n"; let+ () = check_error_result expected (Compile.until_link link_state ~unsafe ~optimize ~name:None m) in link_state | Assert (Assert_malformed _) -> - Log.debug "*** assert_malformed@\n"; + Log.debug0 "*** assert_malformed@\n"; Log.err "TODO" | Assert (Assert_return (a, res)) -> - Log.debug "*** assert_return@\n"; + Log.debug0 "*** assert_return@\n"; let* stack = action link_state a in if List.compare_lengths res stack <> 0 || not (List.for_all2 compare_result_const res (List.rev stack)) then begin - Format.eprintf "got: %a@.expected: %a@." Stack.pp - (List.rev stack) Text.Pp.results res; + Format.pp_err "got: %a@.expected: %a@." Stack.pp (List.rev stack) + Text.Pp.results res; Error "Bad result" end else Ok link_state | Assert (Assert_trap (a, expected)) -> - Log.debug "*** assert_trap@\n"; + Log.debug0 "*** assert_trap@\n"; let got = action link_state a in let+ () = check_error_result expected got in link_state | Assert (Assert_exhaustion (a, expected)) -> - Log.debug "*** assert_exhaustion@\n"; + Log.debug0 "*** assert_exhaustion@\n"; let+ () = if no_exhaustion then Ok () else @@ -242,12 +242,12 @@ let run ~no_exhaustion ~optimize script = link_state | Register (name, mod_name) -> if !curr_module = 1 && !registered = false then Log.debug_on := false; - Log.debug "*** register@\n"; + Log.debug0 "*** register@\n"; let+ state = Link.register_module link_state ~name ~id:mod_name in Log.debug_on := debug_on; state | Action a -> - Log.debug "*** action@\n"; + Log.debug0 "*** action@\n"; let+ _stack = action link_state a in link_state ) state script diff --git a/src/simplified.ml b/src/simplified.ml index 3541eb71e..7a17087b1 100644 --- a/src/simplified.ml +++ b/src/simplified.ml @@ -67,8 +67,7 @@ module Pp = struct | Runtime.Local f -> Types.Pp.func fmt f | Runtime.Imported { Imported.modul; name; _ } -> pp fmt "%s.%s" modul name - let lst f fmt l = - (pp_list ~pp_sep:(fun fmt () -> pp fmt "@\n") f) fmt (List.rev l) + let lst f fmt l = (pp_list ~pp_sep:pp_newline f) fmt (List.rev l) let funcs fmt funcs = lst (Indexed.pp func) fmt funcs.Named.values diff --git a/src/spectest.ml b/src/spectest.ml index bde26ae29..7dfe6a010 100644 --- a/src/spectest.ml +++ b/src/spectest.ml @@ -2,19 +2,18 @@ (* Copyright © 2021 Léo Andrès *) (* Copyright © 2021 Pierre Chambart *) +open Format open Types open Concrete_value.Func -type extern_module = Concrete_value.Func.extern_func Link.extern_module +type extern_module = extern_func Link.extern_module let extern_m = - let pp = Format.pp in - let fmt = Format.std_formatter in let print = () in - let print_i32 i = pp fmt "%li@\n%!" i in - let print_i64 i = pp fmt "%Li@\n%!" i in - let print_f32 f = pp fmt "%a@\n%!" Float32.pp f in - let print_f64 f = pp fmt "%a@\n%!" Float64.pp f in + let print_i32 i = pp_std "%li@\n%!" i in + let print_i64 i = pp_std "%Li@\n%!" i in + let print_f32 f = pp_std "%a@\n%!" Float32.pp f in + let print_f64 f = pp_std "%a@\n%!" Float64.pp f in let print_i32_f32 i f = print_i32 i; print_f32 f diff --git a/src/stack.ml b/src/stack.ml index 9055028dc..d6920174d 100644 --- a/src/stack.ml +++ b/src/stack.ml @@ -141,7 +141,7 @@ module Make (V : Value_intf.T) : (* push s (Ref (Arrayref (Some a))) *) let pp fmt (s : t) = - Format.pp_list ~pp_sep:(fun fmt () -> Format.fprintf fmt " ; ") V.pp fmt s + Format.pp_list ~pp_sep:(fun fmt () -> Format.pp_string fmt " ; ") V.pp fmt s let pop = function [] -> raise Empty | hd :: tl -> (hd, tl) diff --git a/src/text.ml b/src/text.ml index 2155f6b6d..7bb7ca189 100644 --- a/src/text.ml +++ b/src/text.ml @@ -131,7 +131,7 @@ module Pp = struct let elem fmt (e : elem) = pp fmt "@[(elem %a %a %a %a)@]" pp_id_opt e.id elem_mode e.mode pp_ref_type e.typ - (pp_list ~pp_sep:(fun fmt () -> pp fmt "@\n") elemexpr) + (pp_list ~pp_sep:pp_newline elemexpr) e.init let module_field fmt = function @@ -148,7 +148,7 @@ module Pp = struct let modul fmt (m : modul) = pp fmt "(module %a@\n @[%a@]@\n)" pp_id_opt m.id - (pp_list ~pp_sep:(fun fmt () -> pp fmt "@\n") module_field) + (pp_list ~pp_sep:pp_newline module_field) m.fields let register fmt (s, _name) = pp fmt "(register %s)" s @@ -223,5 +223,5 @@ module Pp = struct | Register (s, name) -> register fmt (s, name) | Action _a -> pp fmt "" - let file fmt l = pp_list ~pp_sep:(fun fmt () -> pp fmt "@\n") cmd fmt l + let file fmt l = pp_list ~pp_sep:pp_newline cmd fmt l end diff --git a/src/typecheck.ml b/src/typecheck.ml index d836c1b21..89112a103 100644 --- a/src/typecheck.ml +++ b/src/typecheck.ml @@ -19,8 +19,7 @@ let pp_typ fmt = function | Any -> pp_string fmt "any" | Something -> pp_string fmt "something" -let pp_typ_list fmt l = - pp_list ~pp_sep:(fun fmt () -> pp_string fmt " ") pp_typ fmt l +let pp_typ_list fmt l = pp_list ~pp_sep:pp_space pp_typ fmt l let typ_of_val_type = function | Types.Ref_type (_null, t) -> Ref_type t @@ -149,7 +148,7 @@ end = struct let pp fmt (s : stack) = pp fmt "[%a]" pp_typ_list s let pp_error fmt (expected, got) = - fprintf fmt "requires %a but stack has %a" pp expected pp got + Format.pp fmt "requires %a but stack has %a" pp expected pp got let match_num_type (required : num_type) (got : num_type) = match (required, got) with @@ -490,8 +489,8 @@ let rec typecheck_instr (env : env) (stack : stack) (instr : simplified instr) : | Struct_new_default _ | Extern_externalize | Extern_internalize | Ref_as_non_null | Ref_cast _ | Ref_test _ | Br_on_non_null _ | Br_on_null _ | Br_on_cast _ | Br_on_cast_fail _ | Ref_eq ) as i -> - Log.debug "TODO (typecheck instr) %a" Types.Pp.instr i; - Ok stack + Log.debug2 "TODO (typecheck instr) %a" Types.Pp.instr i; + assert false and typecheck_expr env expr ~is_loop (block_type : simplified block_type option) ~stack:previous_stack : stack Result.t = @@ -623,7 +622,7 @@ let typecheck_data modul refs (data : data Indexed.t) = | _whatever -> Error "type mismatch (typecheck_data)" ) let modul (modul : modul) = - Log.debug "typechecking ...@\n"; + Log.debug0 "typechecking ...@\n"; let refs = Hashtbl.create 512 in let* () = list_iter (typecheck_global modul refs) modul.global.values in let* () = list_iter (typecheck_elem modul refs) modul.elem.values in diff --git a/src/types.ml b/src/types.ml index 7c4086543..354596b33 100644 --- a/src/types.ml +++ b/src/types.ml @@ -217,8 +217,7 @@ let pp_param fmt (id, vt) = pp fmt "(param %a %a)" pp_id_opt id pp_val_type vt type nonrec 'a param_type = 'a param list -let pp_param_type fmt params = - pp_list ~pp_sep:(fun fmt () -> pp fmt " ") pp_param fmt params +let pp_param_type fmt params = pp_list ~pp_sep:pp_space pp_param fmt params type nonrec 'a result_type = 'a val_type list @@ -482,8 +481,7 @@ module Const = struct | Array_new_default _ -> pp fmt "array.new_default" | Ref_i31 -> pp fmt "ref.i31" - let expr fmt instrs = - pp_list ~pp_sep:(fun fmt () -> pp fmt "@\n") instr fmt instrs + let expr fmt instrs = pp_list ~pp_sep:pp_newline instr fmt instrs end end @@ -666,7 +664,7 @@ module Pp = struct | Br_if id -> pp fmt "br_if %a" pp_indice id | Br_table (ids, id) -> pp fmt "br_table %a %a" - (pp_list ~pp_sep:(fun fmt () -> pp fmt " ") pp_indice) + (pp_list ~pp_sep:pp_space pp_indice) (Array.to_list ids) pp_indice id | Return -> pp fmt "return" | Return_call id -> pp fmt "return_call %a" pp_indice id @@ -712,8 +710,7 @@ module Pp = struct t | Ref_eq -> pp fmt "ref.eq" - and expr fmt instrs = - pp_list ~pp_sep:(fun fmt () -> pp fmt "@\n") instr fmt instrs + and expr fmt instrs = pp_list ~pp_sep:pp_newline instr fmt instrs let func : type kind. formatter -> kind func -> unit = fun fmt f -> @@ -722,7 +719,7 @@ module Pp = struct f.type_f locals f.locals expr f.body let funcs fmt (funcs : 'a func list) = - pp_list ~pp_sep:(fun fmt () -> pp fmt "@\n") func fmt funcs + pp_list ~pp_sep:pp_newline func fmt funcs let start fmt start = pp fmt "(start %a)" pp_indice start @@ -752,14 +749,13 @@ module Pp = struct | Const -> pp fmt " %a" storage_type t | Var -> pp fmt "(%a %a)" mut m storage_type t - let fields fmt = pp_list ~pp_sep:(fun fmt () -> pp fmt " ") field_type fmt + let fields fmt = pp_list ~pp_sep:pp_space field_type fmt let struct_field fmt ((n : string option), f) = pp fmt "@\n @[(field %a%a)@]" pp_id_opt n fields f let struct_type fmt = - pp fmt "(struct %a)" - (pp_list ~pp_sep:(fun fmt () -> pp fmt " ") struct_field) + pp fmt "(struct %a)" (pp_list ~pp_sep:pp_space struct_field) let array_type fmt = pp fmt "(array %a)" field_type @@ -768,8 +764,7 @@ module Pp = struct | Def_array_t t -> array_type fmt t | Def_func_t t -> func_type fmt t - let indices fmt ids = - pp_list ~pp_sep:(fun fmt () -> pp fmt " ") pp_indice fmt ids + let indices fmt ids = pp_list ~pp_sep:pp_space pp_indice fmt ids let final fmt = function | Final -> pp fmt "final" @@ -785,6 +780,5 @@ module Pp = struct match l with | [] -> () | [ t ] -> type_def fmt t - | l -> - pp fmt "(rec %a)" (pp_list ~pp_sep:(fun fmt () -> pp fmt " ") type_def) l + | l -> pp fmt "(rec %a)" (pp_list ~pp_sep:pp_space type_def) l end diff --git a/test/fuzz/fuzzer.ml b/test/fuzz/fuzzer.ml index 7e1bc8748..ac2381151 100644 --- a/test/fuzz/fuzzer.ml +++ b/test/fuzz/fuzzer.ml @@ -9,22 +9,22 @@ let global_count = ref 0 let compare (module I1 : Interprets.INTERPRET) (module I2 : Interprets.INTERPRET) m = if Param.debug then begin - Format.eprintf "comparing %s and %s@\n @[" I1.name I2.name; - Format.eprintf "running %s@\n" I1.name; - Format.pp_flush Format.err_formatter () + Format.pp_err "comparing %s and %s@\n @[" I1.name I2.name; + Format.pp_err "running %s@\n" I1.name; + Format.pp_flush Stdlib.Format.err_formatter () end; let r1 = let m = I1.of_symbolic m in I1.run m in if Param.debug then begin - Format.eprintf "running %s@\n" I2.name + Format.pp_err "running %s@\n" I2.name end; let r2 = let m = I2.of_symbolic m in I2.run m in - Format.eprintf "@]"; + Format.pp_err "@]"; match (r1, r2) with | Ok (), Ok () -> true | Error "timeout", Error "timeout" -> @@ -33,36 +33,36 @@ let compare (module I1 : Interprets.INTERPRET) | Error "timeout", Ok () -> Param.allow_partial_timeout || - ( Format.eprintf "timeout for `%s` but not for `%s`" I1.name I2.name; + ( Format.pp_err "timeout for `%s` but not for `%s`" I1.name I2.name; false ) | Ok (), Error "timeout" -> Param.allow_partial_timeout || - ( Format.eprintf "timeout for `%s` but not for `%s`" I2.name I1.name; + ( Format.pp_err "timeout for `%s` but not for `%s`" I2.name I1.name; false ) | Error "timeout", Error msg -> Param.allow_partial_timeout || - ( Format.eprintf "timeout for `%s` but error `%s` for `%s`" I1.name msg + ( Format.pp_err "timeout for `%s` but error `%s` for `%s`" I1.name msg I2.name; false ) | Error msg, Error "timeout" -> Param.allow_partial_timeout || - ( Format.eprintf "timeout for `%s` but error `%s` for `%s`" I2.name msg + ( Format.pp_err "timeout for `%s` but error `%s` for `%s`" I2.name msg I1.name; false ) | Error msg1, Error msg2 -> true (* TODO: fixme *) || msg1 = msg2 || - ( Format.eprintf "`%s` gave error `%s` but `%s` gave error `%s`" I1.name - msg1 I2.name msg2; + ( Format.pp_err "`%s` gave error `%s` but `%s` gave error `%s`" I1.name msg1 + I2.name msg2; false ) | Ok (), Error msg -> - Format.eprintf "`%s` was OK but `%s` gave error `%s`" I1.name I2.name msg; + Format.pp_err "`%s` was OK but `%s` gave error `%s`" I1.name I2.name msg; false | Error msg, Ok () -> - Format.eprintf "`%s` was OK but `%s` gave error `%s`" I2.name I1.name msg; + Format.pp_err "`%s` was OK but `%s` gave error `%s`" I2.name I1.name msg; false let check_optimized m = @@ -84,9 +84,9 @@ let gen = Crowbar.with_printer Owi.Text.Pp.modul Gen.modul let () = Crowbar.add_test ~name:"fuzzing" [ gen ] (fun m -> incr global_count; - if Param.debug then Format.eprintf "%a@\n" Owi.Text.Pp.modul m; - Format.eprintf "test module %d [got %d timeouts...]@\n@[" !global_count + if Param.debug then Format.pp_err "%a@\n" Owi.Text.Pp.modul m; + Format.pp_err "test module %d [got %d timeouts...]@\n@[" !global_count !timeout_count; - Format.pp_flush Format.err_formatter (); + Format.pp_flush Stdlib.Format.err_formatter (); Crowbar.check (check_optimized m); - Format.eprintf "@]" ) + Format.pp_err "@]" ) diff --git a/test/fuzz/interprets.ml b/test/fuzz/interprets.ml index ac5add037..19f09a066 100644 --- a/test/fuzz/interprets.ml +++ b/test/fuzz/interprets.ml @@ -83,18 +83,18 @@ module Reference : INTERPRET = struct let suffix = ".wast" in let tmp_file = Filename.temp_file prefix suffix in let chan = open_out tmp_file in - let fmt = Format.formatter_of_out_channel chan in + let fmt = Stdlib.Format.formatter_of_out_channel chan in Format.pp_string fmt modul; close_out chan; let n = - Sys.command - @@ Format.sprintf "timeout %fs wasm %s" Param.max_time_execution tmp_file + Format.kasprintf Sys.command "timeout %fs wasm %s" + Param.max_time_execution tmp_file in match n with | 0 -> Ok () | 42 -> Error "trap" | 124 -> Error "timeout" - | n -> failwith (Format.sprintf "error %d" n) + | n -> Format.kasprintf failwith "error %d" n (* TODO: https://github.com/OCamlPro/owi/pull/28#discussion_r1212866678 *) let name = "reference" diff --git a/test/print/dune b/test/print/dune index ef14f5345..894c6bf9c 100644 --- a/test/print/dune +++ b/test/print/dune @@ -8,16 +8,10 @@ (modules print_text) (libraries owi)) -(executable - (name print_optimized) - (modules print_optimized) - (libraries owi)) - (cram (deps print_text.exe print_simplified.exe - print_optimized.exe m.wast locals.wast locals_drop.wast)) diff --git a/test/print/locals.t b/test/print/locals.t index db90a4a4b..ed34e7c9c 100644 --- a/test/print/locals.t +++ b/test/print/locals.t @@ -1,5 +1,5 @@ print optimized locals: - $ dune exec -- ./print_optimized.exe locals.wast + $ dune exec owi -- opt locals.wast (module (func $f1 (param $a1 i32) (param $a2 i32) (param $a3 i32) (local $l1 i32) (local $l2 i32) local.get 3 diff --git a/test/print/locals_drop.t b/test/print/locals_drop.t index e80f3f7b4..6f9381d28 100644 --- a/test/print/locals_drop.t +++ b/test/print/locals_drop.t @@ -1,5 +1,5 @@ print optimized locals: - $ dune exec -- ./print_optimized.exe locals_drop.wast + $ dune exec owi -- opt locals_drop.wast (module (func $f1 (param $a1 i32) (param $a2 i32) (param $a3 i32) @@ -58,7 +58,6 @@ print optimized locals: i32.const 1 i32.const 2 call 7 - drop ) (start 8) ) diff --git a/test/print/locals_drop.wast b/test/print/locals_drop.wast index 2dcc5adcf..35b240609 100644 --- a/test/print/locals_drop.wast +++ b/test/print/locals_drop.wast @@ -79,7 +79,6 @@ (call $f6 (i32.const 0) (i32.const 1) (i32.const 2)) (call $f7 (i32.const 0) (i32.const 1) (i32.const 2)) (call $f8 (i32.const 0) (i32.const 1) (i32.const 2)) - drop ) (start $start) diff --git a/test/print/print_optimized.ml b/test/print/print_optimized.ml deleted file mode 100644 index 8de9e4218..000000000 --- a/test/print/print_optimized.ml +++ /dev/null @@ -1,13 +0,0 @@ -open Owi - -let m = - match Parse.Module.from_file ~filename:Sys.argv.(1) with - | Ok m -> m - | Error msg -> failwith msg - -let m = - match Compile.until_simplify ~unsafe:false m with - | Ok m -> m - | Error msg -> failwith msg - -let () = Format.printf "%a@\n" Simplified.Pp.modul (Optimize.modul m) diff --git a/test/print/print_simplified.ml b/test/print/print_simplified.ml index d96fcefe8..bd4d7fe3f 100644 --- a/test/print/print_simplified.ml +++ b/test/print/print_simplified.ml @@ -24,4 +24,4 @@ let m = | Ok m -> m | Error msg -> failwith msg -let () = Format.printf "%a@\n" Simplified.Pp.modul m +let () = Format.pp_std "%a@\n" Simplified.Pp.modul m diff --git a/test/print/print_text.ml b/test/print/print_text.ml index 95b108bb9..6153d25ba 100644 --- a/test/print/print_text.ml +++ b/test/print/print_text.ml @@ -24,4 +24,4 @@ let m = | Ok m -> m | Error msg -> failwith msg -let () = Format.printf "%a@\n" Text.Pp.modul m +let () = Format.pp_std "%a@\n" Text.Pp.modul m