Skip to content

Commit 5e51faa

Browse files
authored
Merge pull request #66 from ocaml-wasm/debug-fixes
Improve the insertion of debug information in the generated Wasm code
2 parents ec0e24c + 4d34679 commit 5e51faa

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ let generate_prelude ~out_file =
148148
Driver.f ~target:Wasm (Parse_bytecode.Debug.create ~include_cmis:false false) code
149149
in
150150
let context = Wa_generate.start () in
151-
let _ = Wa_generate.f ~context ~unit_name:(Some "prelude") ~live_vars ~in_cps p in
151+
let _ =
152+
Wa_generate.f ~context ~unit_name:(Some "prelude") ~live_vars ~in_cps ~debug p
153+
in
152154
Wa_generate.output ch ~context ~debug;
153155
uinfo.provides
154156

@@ -283,7 +285,7 @@ let run
283285
in
284286
let context = Wa_generate.start () in
285287
let toplevel_name, generated_js =
286-
Wa_generate.f ~context ~unit_name ~live_vars ~in_cps p
288+
Wa_generate.f ~context ~unit_name ~live_vars ~in_cps ~debug p
287289
in
288290
if standalone then Wa_generate.add_start_function ~context toplevel_name;
289291
Wa_generate.output ch ~context ~debug;

compiler/lib/wasm/wa_code_generation.ml

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,16 @@ let blk l st =
291291
List.rev st.instrs, { st with instrs }
292292

293293
let with_location loc instrs st =
294-
let current_instrs = st.instrs in
295-
let (), st = instrs { st with instrs = [] } in
296-
let[@tail_mod_cons] rec add_loc loc = function
297-
| [] -> current_instrs
298-
| W.Nop :: rem -> W.Nop :: add_loc loc rem
299-
| Location _ :: _ as l -> l @ current_instrs (* Stop on the first location *)
300-
| i :: rem -> W.Location (loc, i) :: add_loc loc rem
301-
in
302-
(), { st with instrs = add_loc loc st.instrs }
294+
let (), st = instrs st in
295+
( ()
296+
, { st with
297+
instrs =
298+
(match st.instrs with
299+
| [] -> []
300+
| Location _ :: _ when Poly.equal loc No -> st.instrs
301+
| Location (_, i) :: rem -> Location (loc, i) :: rem
302+
| i :: rem -> Location (loc, i) :: rem)
303+
} )
303304

304305
let cast ?(nullable = false) typ e =
305306
let* e = e in
@@ -469,6 +470,13 @@ let get_i31_value x st =
469470
let x = Var.fresh () in
470471
let x, st = add_var ~typ:I32 x st in
471472
Some x, { st with instrs = LocalSet (x', RefI31 (LocalTee (x, e))) :: rem }
473+
| Location (loc, LocalSet (x', RefI31 e)) :: rem when Code.Var.equal x x' && is_smi e ->
474+
let x = Var.fresh () in
475+
let x, st = add_var ~typ:I32 x st in
476+
( Some x
477+
, { st with
478+
instrs = Location (loc, LocalSet (x', RefI31 (LocalTee (x, e)))) :: rem
479+
} )
472480
| _ -> None, st
473481

474482
let load x =

compiler/lib/wasm/wa_generate.ml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module Generate (Target : Wa_target_sig.S) = struct
3737
; blocks : block Addr.Map.t
3838
; closures : Wa_closure_conversion.closure Var.Map.t
3939
; global_context : Wa_code_generation.context
40+
; debug : Parse_bytecode.Debug.t
4041
}
4142

4243
let func_type n =
@@ -818,6 +819,20 @@ module Generate (Target : Wa_target_sig.S) = struct
818819
params
819820
((pc, _) as cont)
820821
acc =
822+
let ctx =
823+
let loc = Before pc in
824+
match Parse_bytecode.Debug.find_loc ctx.debug loc with
825+
| Some _ ->
826+
let block = Addr.Map.find pc ctx.blocks in
827+
let block =
828+
match block.body with
829+
| (i, _) :: rem -> { block with body = (i, loc) :: rem }
830+
| [] -> { block with branch = fst block.branch, loc }
831+
in
832+
let blocks = Addr.Map.add pc block ctx.blocks in
833+
{ ctx with blocks }
834+
| None -> ctx
835+
in
821836
let stack_info =
822837
Stack.generate_spilling_information
823838
p
@@ -1107,13 +1122,16 @@ module Generate (Target : Wa_target_sig.S) = struct
11071122
~in_cps (*
11081123
~should_export
11091124
~warn_on_unhandled_effect
1110-
_debug *) =
1125+
*)
1126+
~debug =
11111127
global_context.unit_name <- unit_name;
11121128
let p, closures = Wa_closure_conversion.f p in
11131129
(*
11141130
Code.Print.program (fun _ _ -> "") p;
11151131
*)
1116-
let ctx = { live = live_vars; in_cps; blocks = p.blocks; closures; global_context } in
1132+
let ctx =
1133+
{ live = live_vars; in_cps; blocks = p.blocks; closures; global_context; debug }
1134+
in
11171135
let toplevel_name = Var.fresh_n "toplevel" in
11181136
let functions =
11191137
Code.fold_closures_outermost_first
@@ -1223,15 +1241,15 @@ let start () =
12231241
| `Core -> Wa_core_target.Value.value
12241242
| `GC -> Wa_gc_target.Value.value)
12251243

1226-
let f ~context ~unit_name p ~live_vars ~in_cps =
1244+
let f ~context ~unit_name p ~live_vars ~in_cps ~debug =
12271245
let p = if Config.Flag.effects () then fix_switch_branches p else p in
12281246
match target with
12291247
| `Core ->
12301248
let module G = Generate (Wa_core_target) in
1231-
G.f ~context ~unit_name ~live_vars ~in_cps p
1249+
G.f ~context ~unit_name ~live_vars ~in_cps ~debug p
12321250
| `GC ->
12331251
let module G = Generate (Wa_gc_target) in
1234-
G.f ~context ~unit_name ~live_vars ~in_cps p
1252+
G.f ~context ~unit_name ~live_vars ~in_cps ~debug p
12351253

12361254
let add_start_function =
12371255
match target with

compiler/lib/wasm/wa_generate.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ val f :
2626
-> Code.program
2727
-> live_vars:int array
2828
-> in_cps:Effects.in_cps
29+
-> debug:Parse_bytecode.Debug.t
2930
-> Wa_ast.var * (string list * (string * Javascript.expression) list)
3031

3132
val add_start_function : context:Wa_code_generation.context -> Wa_ast.var -> unit

0 commit comments

Comments
 (0)