Skip to content

Commit b2444cc

Browse files
committed
boilerplate for haskell subcommand
1 parent 59fabf7 commit b2444cc

8 files changed

Lines changed: 107 additions & 0 deletions

File tree

doc/src/manpages/owi.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ COMMANDS
2121
fmt [--inplace] [OPTION]… FILE…
2222
Format a .wat or .wast file
2323

24+
haskell [OPTION]… FILE…
25+
Compile a Haskell file to Wasm and run the symbolic interpreter on
26+
it
27+
2428
instrument COMMAND …
2529
Instrument a program in various ways
2630

src/bin/owi.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,22 @@ let fmt_cmd =
402402
and+ () = setup_log in
403403
Cmd_fmt.cmd ~inplace ~files
404404

405+
(* owi haskell *)
406+
407+
let haskell_info =
408+
let doc =
409+
"Compile a Haskell file to Wasm and run the symbolic interpreter on it"
410+
in
411+
let man = [] @ shared_man in
412+
Cmd.info "haskell" ~version ~doc ~sdocs ~man
413+
414+
let haskell_cmd =
415+
let+ files
416+
and+ out_file
417+
and+ () = setup_log
418+
and+ symbolic_parameters = symbolic_parameters (Some "_start") in
419+
Cmd_haskell.cmd ~symbolic_parameters ~files ~out_file
420+
405421
(* owi instrument *)
406422

407423
let instrument_info =
@@ -658,6 +674,7 @@ let cli =
658674
; Cmd.v fmt_info fmt_cmd
659675
; Cmd.group instrument_info
660676
[ Cmd.v instrument_label_info instrument_label_cmd ]
677+
; Cmd.v haskell_info haskell_cmd
661678
; Cmd.v iso_info iso_cmd
662679
; Cmd.v replay_info replay_cmd
663680
; Cmd.v run_info run_cmd

src/cmd/cmd_haskell.ml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
2+
(* Copyright © 2021-2024 OCamlPro *)
3+
(* Written by the Owi programmers *)
4+
5+
open Bos
6+
open Syntax
7+
8+
let compile ~workspace ~out_file (files : Fpath.t list) : Fpath.t Result.t =
9+
let* haskell_bin =
10+
let name = "haskell" in
11+
match OS.Cmd.resolve @@ Cmd.v name with
12+
| Error _ ->
13+
Fmt.error_msg
14+
"The `%s` binary was not found, please make sure it is in your path."
15+
name
16+
| Ok _ as ok -> ok
17+
in
18+
19+
let out = Option.value ~default:Fpath.(workspace / "out.wasm") out_file in
20+
let haskell : Cmd.t =
21+
Cmd.(
22+
haskell_bin
23+
(* output and input *)
24+
% "-o"
25+
% p out
26+
%% Cmd.of_list (List.map p files)
27+
(* % p libhaskell *) )
28+
in
29+
30+
let err =
31+
match Logs.Src.level Log.main_src with
32+
| Some (Logs.Debug | Logs.Info) -> OS.Cmd.err_run_out
33+
| None | Some _ -> OS.Cmd.err_null
34+
in
35+
36+
let+ () =
37+
Log.bench_fn "compiling time" @@ fun () ->
38+
match OS.Cmd.run ~err haskell with
39+
| Ok _ as v -> v
40+
| Error (`Msg e) ->
41+
Log.debug (fun m -> m "haskell failed: %s" e);
42+
Fmt.error_msg
43+
"haskell failed: run with -vv to get the full error message if it was \
44+
not displayed above"
45+
in
46+
47+
out
48+
49+
let cmd ~(symbolic_parameters : Symbolic_parameters.t) ~files ~out_file :
50+
unit Result.t =
51+
let* workspace =
52+
match symbolic_parameters.workspace with
53+
| Some path -> Ok path
54+
| None -> OS.Dir.tmp "cmd_haskell_%s"
55+
in
56+
let* _did_create : bool = OS.Dir.create workspace in
57+
58+
let* source_file = compile ~workspace ~out_file files in
59+
let workspace = Some workspace in
60+
61+
let parameters = { symbolic_parameters with workspace } in
62+
63+
Cmd_sym.cmd ~parameters ~source_file

src/cmd/cmd_haskell.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
2+
(* Copyright © 2021-2024 OCamlPro *)
3+
(* Written by the Owi programmers *)
4+
5+
val cmd :
6+
symbolic_parameters:Symbolic_parameters.t
7+
-> files:Fpath.t list
8+
-> out_file:Fpath.t option
9+
-> unit Result.t

src/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
cmd_cfg
2222
cmd_cpp
2323
cmd_fmt
24+
cmd_haskell
2425
cmd_instrument_label
2526
cmd_iso
2627
cmd_replay

src/owi.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Cmd_call_graph = Cmd_call_graph
88
module Cmd_cfg = Cmd_cfg
99
module Cmd_cpp = Cmd_cpp
1010
module Cmd_fmt = Cmd_fmt
11+
module Cmd_haskell = Cmd_haskell
1112
module Cmd_instrument_label = Cmd_instrument_label
1213
module Cmd_iso = Cmd_iso
1314
module Cmd_replay = Cmd_replay

src/owi.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,14 @@ module Cmd_fmt : sig
13361336
val cmd : inplace:bool -> files:Fpath.t list -> unit Result.t
13371337
end
13381338

1339+
module Cmd_haskell : sig
1340+
val cmd :
1341+
symbolic_parameters:Symbolic_parameters.t
1342+
-> files:Fpath.t list
1343+
-> out_file:Fpath.t option
1344+
-> unit Result.t
1345+
end
1346+
13391347
module Cmd_instrument_label : sig
13401348
val cmd :
13411349
unsafe:bool

test/help/help.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ no subcommand should print help
1919
fmt [--inplace] [OPTION]… FILE…
2020
Format a .wat or .wast file
2121

22+
haskell [OPTION]… FILE…
23+
Compile a Haskell file to Wasm and run the symbolic interpreter on
24+
it
25+
2226
instrument COMMAND …
2327
Instrument a program in various ways
2428

0 commit comments

Comments
 (0)