Skip to content

Commit

Permalink
Start adding a thread through the interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
chambart committed Jun 22, 2023
1 parent c18c455 commit 01bb80b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/bin/owi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let simplify_then_link_then_run ~optimize file =
([], Link.empty_state) file
in
let interp_modul =
if test then Interpret2.I.modul
if test then (Interpret2.I.modul ())
else Interpret.modul
in
list_iter interp_modul (List.rev to_run)
Expand Down
4 changes: 2 additions & 2 deletions src/bin/owi_sym.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let simplify_then_link_then_run ~optimize file =
| _ -> Ok acc )
([], Link.empty_state) file
in
let interp_modul = Interpret2.S.modul in
list_iter interp_modul (List.rev to_run)
let state : Sym_state.P.t = () in
list_fold_left Interpret2.S.modul state (List.rev to_run)

let run_file exec filename =
if not @@ Sys.file_exists filename then
Expand Down
37 changes: 21 additions & 16 deletions src/interpret_functor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module Intf = Interpret_functor_intf
module Make (P : Intf.P) :
Intf.S
with type 'a choice := 'a P.Choice.t
and type module_to_run := P.Module_to_run.t = struct
and type module_to_run := P.Module_to_run.t
and type thread := P.t = struct
module Int32 = P.Value.I32
module Int64 = P.Value.I64
module Float32 = P.Value.F32
Expand Down Expand Up @@ -545,6 +546,7 @@ module Make (P : Intf.P) :
; func_rt : result_type
; env : P.Env.t
; count : count
; thread : P.t
}

let rec print_count ppf count =
Expand Down Expand Up @@ -590,13 +592,13 @@ module Make (P : Intf.P) :
c

type instr_result =
| Return of value list
| Return of P.t * value list
| Continue of exec_state

let return (state : exec_state) =
let args = Stack.keep state.stack (List.length state.func_rt) in
match state.return_state with
| None -> Choice.return (Return args)
| None -> Choice.return (Return (state.thread, args))
| Some state ->
let stack = args @ state.stack in
Choice.return (Continue { state with stack })
Expand Down Expand Up @@ -665,9 +667,11 @@ module Make (P : Intf.P) :
; return_state
; env
; count = enter_function_count state.count func.id id
; thread = state.thread
}

let exec_vfunc ~return (state : State.exec_state) (func : (P.Env.t', P.extern_func) Func_intf.t) =
let exec_vfunc ~return (state : State.exec_state)
(func : (P.Env.t', P.extern_func) Func_intf.t) =
match func with
| WASM (id, func, env) -> exec_func ~return ~id state env func
| Extern _f -> failwith "TODO extern func"
Expand Down Expand Up @@ -1283,16 +1287,16 @@ module Make (P : Intf.P) :
let/ state = exec_instr instr { state with pc } in
match state with
| State.Continue state -> loop state
| State.Return res -> Choice.return res
| State.Return (thread, res) -> Choice.return (thread, res)
end
| [] -> (
Log.debug2 "stack : [ %a ]@." Stack.pp state.stack;
let/ state = State.end_block state in
match state with
| State.Continue state -> loop state
| State.Return res -> Choice.return res )
| State.Return (thread, res) -> Choice.return (thread, res) )

let exec_expr env locals stack expr bt =
let exec_expr thread env locals stack expr bt =
let count = State.empty_count (Some "start") in
count.enter <- count.enter + 1;
let state : State.exec_state =
Expand All @@ -1305,6 +1309,7 @@ module Make (P : Intf.P) :
; pc = expr
; return_state = None
; count
; thread
}
in
let/ state = loop state in
Expand Down Expand Up @@ -1332,28 +1337,28 @@ module Make (P : Intf.P) :
(* | exception Trap msg -> Error msg *)
(* | exception Stack_overflow -> Error "call stack exhausted" *)

let modul (modul : P.Module_to_run.t) =
let modul thread (modul : P.Module_to_run.t) =
Log.debug0 "interpreting ...@\n";
let/ () =
let/ thread =
List.fold_left
(fun u to_run ->
let/ () = u in
let/ end_stack, count =
(fun thread to_run ->
let/ thread = thread in
let/ (thread, end_stack), count =
let env = P.Module_to_run.env modul in
exec_expr env [||] Stack.empty to_run None
exec_expr thread env [||] Stack.empty to_run None
in
Log.profile "Exec module %s@.%a@."
(Option.value (P.Module_to_run.modul modul).id ~default:"anonymous")
State.print_count count;
match end_stack with
| [] -> Choice.return ()
| [] -> Choice.return thread
| _ :: _ ->
Format.eprintf "non empty stack@\n%a@." Stack.pp end_stack;
assert false )
(Choice.return ())
(Choice.return thread)
(P.Module_to_run.to_run modul)
in
Choice.return (Ok ())
Choice.return (Ok thread)
(* TODO error handling *)
(* with *)
(* | Trap msg -> Error msg *)
Expand Down
4 changes: 3 additions & 1 deletion src/interpret_functor_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ end

module type S = sig
(** Module to interpret a linked module. *)
type thread

type 'a choice

type module_to_run

(** interpret a module *)
val modul : module_to_run -> (unit, 'a) result choice
val modul : thread -> module_to_run -> (thread, 'a) result choice

(* (\** interpret a function with a given input stack and produce a new stack*\) *)
(* val exec_vfunc : *)
Expand Down
4 changes: 1 addition & 3 deletions src/sym_state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ let ( let* ) o f = Result.fold ~ok:f ~error:trap o
module Def_value = Value

module P = struct
type toremove = unit

type t = toremove
type t = unit

module Value = struct
include Sym_value.Symbolic
Expand Down

0 comments on commit 01bb80b

Please sign in to comment.