Skip to content

Commit c7fec10

Browse files
committed
wasmoo support
1 parent 97a03a4 commit c7fec10

File tree

5 files changed

+75
-16
lines changed

5 files changed

+75
-16
lines changed

compiler/bin-wasm_of_ocaml/cmd_arg.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type t =
6363
; params : (string * string) list
6464
; include_dirs : string list
6565
; effects : Config.effects_backend
66+
; shape_files : string list
6667
}
6768

6869
let options () =
@@ -78,6 +79,10 @@ let options () =
7879
let doc = "Compile the bytecode program [$(docv)]. " in
7980
Arg.(required & pos ~rev:true 0 (some string) None & info [] ~docv:"PROGRAM" ~doc)
8081
in
82+
let shape_files =
83+
let doc = "load shape file [$(docv)]." in
84+
Arg.(value & opt_all string [] & info [ "load-shape" ] ~docv:"FILE" ~doc)
85+
in
8186
let profile =
8287
let doc = "Set optimization profile : [$(docv)]." in
8388
let profile =
@@ -140,7 +145,8 @@ let options () =
140145
output_file
141146
input_file
142147
runtime_files
143-
effects =
148+
effects
149+
shape_files =
144150
let chop_extension s = try Filename.chop_extension s with Invalid_argument _ -> s in
145151
let output_file =
146152
let ext =
@@ -172,6 +178,7 @@ let options () =
172178
; sourcemap_root
173179
; sourcemap_don't_inline_content
174180
; effects
181+
; shape_files
175182
}
176183
in
177184
let t =
@@ -189,7 +196,8 @@ let options () =
189196
$ output_file
190197
$ input_file
191198
$ runtime_files
192-
$ effects)
199+
$ effects
200+
$ shape_files)
193201
in
194202
Term.ret t
195203

@@ -270,6 +278,7 @@ let options_runtime_only () =
270278
; sourcemap_root
271279
; sourcemap_don't_inline_content
272280
; effects
281+
; shape_files = []
273282
}
274283
in
275284
let t =

compiler/bin-wasm_of_ocaml/cmd_arg.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type t =
3232
; params : (string * string) list
3333
; include_dirs : string list
3434
; effects : Config.effects_backend
35+
; shape_files : string list
3536
}
3637

3738
val options : unit -> t Cmdliner.Term.t

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,14 @@ let generate_prelude ~out_file =
245245
@@ fun ch ->
246246
let code, uinfo = Parse_bytecode.predefined_exceptions () in
247247
let profile = Profile.O1 in
248-
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
248+
let Driver.
249+
{ program
250+
; variable_uses
251+
; in_cps
252+
; deadcode_sentinal
253+
; shapes = _
254+
; trampolined_calls = _
255+
} =
249256
Driver.optimize ~profile ~shapes:false code
250257
in
251258
let context = Generate.start () in
@@ -327,6 +334,16 @@ let add_source_map sourcemap_don't_inline_content z opt_source_map =
327334
~name:(Link.source_name i j file)
328335
~contents:(Yojson.Basic.to_string (`String sm))))
329336

337+
let merge_shape a b =
338+
StringMap.union (fun _name s1 s2 -> if Shape.equal s1 s2 then Some s1 else None) a b
339+
340+
let sexp_of_shapes s =
341+
StringMap.bindings s
342+
|> List.map ~f:(fun (name, shape) ->
343+
Sexp.List [ Atom name; Atom (Shape.to_string shape) ])
344+
345+
let string_of_shapes s = Sexp.List (sexp_of_shapes s) |> Sexp.to_string
346+
330347
let run
331348
{ Cmd_arg.common
332349
; profile
@@ -340,11 +357,24 @@ let run
340357
; sourcemap_root
341358
; sourcemap_don't_inline_content
342359
; effects
360+
; shape_files
343361
} =
344362
Config.set_target `Wasm;
345363
Jsoo_cmdline.Arg.eval common;
346364
Config.set_effects_backend effects;
347365
Generate.init ();
366+
List.iter shape_files ~f:(fun s ->
367+
let z = Zip.open_in s in
368+
if Zip.has_entry z ~name:"shapes.sexp"
369+
then
370+
let s = Zip.read_entry z ~name:"shapes.sexp" in
371+
match Sexp.from_string s with
372+
| List l ->
373+
List.iter l ~f:(function
374+
| Sexp.List [ Atom name; Atom shape ] ->
375+
Shape.Store.set ~name (Shape.of_string shape)
376+
| _ -> ())
377+
| _ -> ());
348378
let output_file = fst output_file in
349379
if debug_mem () then Debug.start_profiling output_file;
350380
List.iter params ~f:(fun (s, v) -> Config.Param.set s v);
@@ -397,9 +427,17 @@ let run
397427
check_debug one;
398428
let code = one.code in
399429
let standalone = Option.is_none unit_name in
400-
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
401-
Driver.optimize ~profile ~shapes:false code
430+
let Driver.
431+
{ program
432+
; variable_uses
433+
; in_cps
434+
; deadcode_sentinal
435+
; shapes
436+
; trampolined_calls = _
437+
} =
438+
Driver.optimize ~profile ~shapes:true code
402439
in
440+
StringMap.iter (fun name shape -> Shape.Store.set ~name shape) shapes;
403441
let context = Generate.start () in
404442
let toplevel_name, generated_js =
405443
Generate.f
@@ -420,7 +458,7 @@ let run
420458
Generate.output ch ~context;
421459
close_out ch);
422460
if times () then Format.eprintf "compilation: %a@." Timer.print t;
423-
generated_js
461+
generated_js, shapes
424462
in
425463
(if runtime_only
426464
then (
@@ -476,7 +514,7 @@ let run
476514
then Some (Filename.temp_file unit_name ".wasm.map")
477515
else None)
478516
@@ fun opt_tmp_map_file ->
479-
let unit_data =
517+
let unit_data, shapes =
480518
Fs.with_intermediate_file (Filename.temp_file unit_name ".wasm")
481519
@@ fun input_file ->
482520
opt_with
@@ -485,7 +523,7 @@ let run
485523
then Some (Filename.temp_file unit_name ".wasm.map")
486524
else None)
487525
@@ fun opt_input_sourcemap ->
488-
let fragments =
526+
let fragments, shapes =
489527
output
490528
code
491529
~wat_file:
@@ -501,9 +539,9 @@ let run
501539
~input_file
502540
~output_file:tmp_wasm_file
503541
();
504-
{ Link.unit_name; unit_info; fragments }
542+
{ Link.unit_name; unit_info; fragments }, shapes
505543
in
506-
cont unit_data unit_name tmp_wasm_file opt_tmp_map_file
544+
cont unit_data unit_name tmp_wasm_file opt_tmp_map_file shapes
507545
in
508546
(match kind with
509547
| `Exe ->
@@ -534,7 +572,7 @@ let run
534572
then Some (Filename.temp_file "code" ".wasm.map")
535573
else None
536574
in
537-
let generated_js =
575+
let generated_js, _shapes =
538576
output
539577
code
540578
~unit_name:None
@@ -598,8 +636,9 @@ let run
598636
@@ fun tmp_output_file ->
599637
let z = Zip.open_out tmp_output_file in
600638
let compile_cmo' z cmo =
601-
compile_cmo cmo (fun unit_data _ tmp_wasm_file opt_tmp_map_file ->
639+
compile_cmo cmo (fun unit_data _ tmp_wasm_file opt_tmp_map_file shapes ->
602640
Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
641+
Zip.add_entry z ~name:"shapes.sexp" ~contents:(string_of_shapes shapes);
603642
add_source_map sourcemap_don't_inline_content z (`File opt_tmp_map_file);
604643
unit_data)
605644
in
@@ -615,8 +654,8 @@ let run
615654
List.fold_right
616655
~f:(fun cmo cont l ->
617656
compile_cmo cmo
618-
@@ fun unit_data unit_name tmp_wasm_file opt_tmp_map_file ->
619-
cont ((unit_data, unit_name, tmp_wasm_file, opt_tmp_map_file) :: l))
657+
@@ fun unit_data unit_name tmp_wasm_file opt_tmp_map_file shapes ->
658+
cont ((unit_data, unit_name, tmp_wasm_file, opt_tmp_map_file, shapes) :: l))
620659
cma.lib_units
621660
~init:(fun l ->
622661
Fs.with_intermediate_file (Filename.temp_file "wasm" ".wasm")
@@ -625,7 +664,7 @@ let run
625664
let source_map =
626665
Wasm_link.f
627666
(List.map
628-
~f:(fun (_, _, file, opt_source_map) ->
667+
~f:(fun (_, _, file, opt_source_map, _) ->
629668
{ Wasm_link.module_name = "OCaml"
630669
; file
631670
; code = None
@@ -638,10 +677,17 @@ let run
638677
~output_file:tmp_wasm_file
639678
in
640679
Zip.add_file z ~name:"code.wasm" ~file:tmp_wasm_file;
680+
let shapes =
681+
List.fold_left
682+
~init:StringMap.empty
683+
~f:(fun acc (_, _, _, _, shapes) -> merge_shape acc shapes)
684+
l
685+
in
686+
Zip.add_entry z ~name:"shapes.sexp" ~contents:(string_of_shapes shapes);
641687
if enable_source_maps
642688
then
643689
add_source_map sourcemap_don't_inline_content z (`Source_map source_map);
644-
List.map ~f:(fun (unit_data, _, _, _) -> unit_data) l)
690+
List.map ~f:(fun (unit_data, _, _, _, _) -> unit_data) l)
645691
[]
646692
in
647693
Link.add_info z ~build_info:(Build_info.create `Cma) ~unit_data ();

compiler/lib-wasm/link.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ let info_from_sexp info =
345345
let build_info =
346346
info |> member "build_info" |> mandatory (single Build_info.from_sexp)
347347
in
348+
348349
let predefined_exceptions =
349350
info
350351
|> member "predefined_exceptions"

compiler/lib/driver.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ let collects_shapes ~shapes (p : Code.program) =
120120
| Utf (Utf8 s) -> s
121121
in
122122
shapes := StringMap.add name block !shapes
123+
| Code.Let (_, Prim (Extern "caml_set_global", [ Pc (String name); Pv block ]))
124+
-> shapes := StringMap.add name block !shapes
123125
| _ -> ()))
124126
p.blocks;
125127
let map =

0 commit comments

Comments
 (0)