Skip to content

Commit eba73eb

Browse files
committed
Move source code location resolution from code output to code generation
1 parent d1ecd6b commit eba73eb

File tree

8 files changed

+24
-28
lines changed

8 files changed

+24
-28
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ let generate_prelude ~out_file =
161161
~debug
162162
program
163163
in
164-
Wa_generate.output ch ~context ~debug;
164+
Wa_generate.output ch ~context;
165165
uinfo.provides
166166

167167
let build_prelude z =
@@ -324,7 +324,7 @@ let run
324324
program
325325
in
326326
if standalone then Wa_generate.add_start_function ~context toplevel_name;
327-
Wa_generate.output ch ~context ~debug;
327+
Wa_generate.output ch ~context;
328328
if times () then Format.eprintf "compilation: %a@." Timer.print t;
329329
generated_js
330330
in

compiler/lib/wasm/wa_ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ and instruction =
191191
| StructSet of var * int * expression * expression
192192
| Return_call of var * expression list
193193
| Return_call_ref of var * expression * expression list
194-
| Location of Code.loc * instruction
194+
| Location of Parse_info.t option * instruction
195195
(** Instruction with attached location information *)
196196

197197
type import_desc =

compiler/lib/wasm/wa_code_generation.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ let with_location loc instrs st =
290290
( ()
291291
, { st with
292292
instrs =
293-
(match st.instrs with
294-
| [] -> []
295-
| Location _ :: _ when Poly.equal loc No -> st.instrs
296-
| Location (_, i) :: rem -> Location (loc, i) :: rem
297-
| i :: rem -> Location (loc, i) :: rem)
293+
(match loc, st.instrs with
294+
| _, [] -> []
295+
| None, Location _ :: _ -> st.instrs
296+
| Some loc, Location (_, i) :: rem -> Location (loc, i) :: rem
297+
| _, i :: rem -> Location (Option.value ~default:None loc, i) :: rem)
298298
} )
299299

300300
let cast ?(nullable = false) typ e =

compiler/lib/wasm/wa_code_generation.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ val is_small_constant : Wa_ast.expression -> bool t
138138

139139
val get_i31_value : Wa_ast.var -> Wa_ast.var option t
140140

141-
val with_location : Code.loc -> unit t -> unit t
141+
val with_location : Parse_info.t option option -> unit t -> unit t
142142

143143
type type_def =
144144
{ supertype : Wa_ast.var option

compiler/lib/wasm/wa_generate.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,9 @@ module Generate (Target : Wa_target_sig.S) = struct
676676

677677
and translate_instr ctx context (i, loc) =
678678
with_location
679-
loc
679+
(match loc with
680+
| No -> None
681+
| _ -> Some (Parse_bytecode.Debug.find_loc ctx.debug loc))
680682
(match i with
681683
| Assign (x, y) -> assign x (load y)
682684
| Let (x, e) ->
@@ -912,7 +914,9 @@ module Generate (Target : Wa_target_sig.S) = struct
912914
let* () = translate_instrs ctx context block.body in
913915
let branch, loc = block.branch in
914916
with_location
915-
loc
917+
(match loc with
918+
| No -> None
919+
| _ -> Some (Parse_bytecode.Debug.find_loc ctx.debug loc))
916920
(match branch with
917921
| Branch cont -> translate_branch result_typ fall_through pc cont context
918922
| Return x -> (
@@ -1223,10 +1227,10 @@ let add_init_function =
12231227
let module G = Generate (Wa_gc_target) in
12241228
G.add_init_function
12251229

1226-
let output ch ~context ~debug =
1230+
let output ch ~context =
12271231
let module G = Generate (Wa_gc_target) in
12281232
let fields = G.output ~context in
1229-
Wa_wat_output.f ~debug ch fields
1233+
Wa_wat_output.f ch fields
12301234

12311235
let wasm_output ch ~context =
12321236
let module G = Generate (Wa_gc_target) in

compiler/lib/wasm/wa_generate.mli

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ val add_start_function : context:Wa_code_generation.context -> Wa_ast.var -> uni
3434

3535
val add_init_function : context:Wa_code_generation.context -> to_link:string list -> unit
3636

37-
val output :
38-
out_channel
39-
-> context:Wa_code_generation.context
40-
-> debug:Parse_bytecode.Debug.t
41-
-> unit
37+
val output : out_channel -> context:Wa_code_generation.context -> unit
4238

4339
val wasm_output : out_channel -> context:Wa_code_generation.context -> unit

compiler/lib/wasm/wa_wat_output.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,7 @@ let select i32 i64 f32 f64 op =
280280
| F32 x -> f32 "32" x
281281
| F64 x -> f64 "64" x
282282

283-
type ctx =
284-
{ mutable function_refs : Code.Var.Set.t
285-
; debug : Parse_bytecode.Debug.t
286-
}
283+
type ctx = { mutable function_refs : Code.Var.Set.t }
287284

288285
let reference_function ctx f = ctx.function_refs <- Code.Var.Set.add f ctx.function_refs
289286

@@ -523,11 +520,10 @@ let expression_or_instructions ctx st in_function =
523520
:: List.concat (List.map ~f:expression (l @ [ e ])))
524521
]
525522
| Location (loc, i) -> (
526-
let loc = Generate.source_location ctx.debug loc in
527523
match loc with
528-
| Javascript.N | U | Pi Parse_info.{ src = None; _ } ->
524+
| None | Some Parse_info.{ src = None | Some ""; _ } ->
529525
Comment "@" :: instruction i
530-
| Pi Parse_info.{ src = Some src; col; line; _ } ->
526+
| Some Parse_info.{ src = Some src; col; line; _ } ->
531527
let loc = Format.sprintf "%s:%d:%d" src line col in
532528
Comment ("@ " ^ loc) :: instruction i)
533529
and instructions l = List.concat (List.map ~f:instruction l) in
@@ -630,9 +626,9 @@ let field ctx st f =
630626
| Type [ t ] -> [ type_field st t ]
631627
| Type l -> [ List (Atom "rec" :: List.map ~f:(type_field st) l) ]
632628

633-
let f ~debug ch fields =
629+
let f ch fields =
634630
let st = build_name_tables fields in
635-
let ctx = { function_refs = Code.Var.Set.empty; debug } in
631+
let ctx = { function_refs = Code.Var.Set.empty } in
636632
let other_fields = List.concat (List.map ~f:(fun f -> field ctx st f) fields) in
637633
let funct_decl =
638634
let functions = Code.Var.Set.elements ctx.function_refs in

compiler/lib/wasm/wa_wat_output.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717
*)
1818

19-
val f : debug:Parse_bytecode.Debug.t -> out_channel -> Wa_ast.module_field list -> unit
19+
val f : out_channel -> Wa_ast.module_field list -> unit

0 commit comments

Comments
 (0)