Skip to content

Commit c859dc3

Browse files
committed
WIP
1 parent 4da4dba commit c859dc3

29 files changed

+695
-798
lines changed

compiler/lib-wasm/closure_conversion.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let collect_free_vars program var_depth depth pc closures =
6060
Code.preorder_traverse
6161
{ fold = Code.fold_children }
6262
(fun pc () ->
63-
let block = Code.Addr.Map.find pc program.blocks in
63+
let block = Code.block pc program in
6464
Freevars.iter_block_free_vars add_if_free_variable block;
6565
List.iter block.body ~f:(fun i ->
6666
match i with
@@ -71,7 +71,7 @@ let collect_free_vars program var_depth depth pc closures =
7171
| Some _ | None -> ())
7272
| _ -> ()))
7373
pc
74-
program.blocks
74+
program
7575
();
7676
!vars
7777

@@ -87,7 +87,7 @@ let rec traverse var_depth closures program pc depth =
8787
Code.preorder_traverse
8888
{ fold = Code.fold_children }
8989
(fun pc (program : Code.program) ->
90-
let block = Code.Addr.Map.find pc program.blocks in
90+
let block = Code.block pc program in
9191
mark_bound_variables var_depth block depth;
9292
let program =
9393
List.fold_left
@@ -151,16 +151,16 @@ let rec traverse var_depth closures program pc depth =
151151
in
152152
List.concat (List.rev (Array.to_list l)))
153153
in
154-
{ program with blocks = Code.Addr.Map.add pc { block with body } program.blocks })
154+
Code.add_block pc { block with body } program)
155155
pc
156-
program.blocks
156+
program
157157
program
158158

159159
let f p =
160160
let t = Timer.make () in
161161
let nv = Var.count () in
162162
let var_depth = Array.make nv (-1) in
163163
let closures = ref Var.Map.empty in
164-
let p = traverse var_depth closures p p.start 0 in
164+
let p = traverse var_depth closures p (Code.start p) 0 in
165165
if Debug.find "times" () then Format.eprintf " closure conversion: %a@." Timer.print t;
166166
p, !closures

compiler/lib-wasm/generate.ml

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module Generate (Target : Target_sig.S) = struct
3636
{ live : int array
3737
; in_cps : Effects.in_cps
3838
; deadcode_sentinal : Var.t
39-
; blocks : block Addr.Map.t
39+
; p : program
4040
; closures : Closure_conversion.closure Var.Map.t
4141
; global_context : Code_generation.context
4242
}
@@ -830,7 +830,7 @@ module Generate (Target : Target_sig.S) = struct
830830
Code.traverse
831831
{ fold = fold_children_skip_try_body }
832832
(fun pc n ->
833-
let block = Addr.Map.find pc p.blocks in
833+
let block = Code.block pc p in
834834
List.fold_left
835835
~f:(fun n i ->
836836
match i with
@@ -863,7 +863,7 @@ module Generate (Target : Target_sig.S) = struct
863863
~init:n
864864
block.body)
865865
pc
866-
p.blocks
866+
p
867867
(false, false)
868868

869869
let wrap_with_handler needed pc handler ~result_typ ~fall_through ~context body =
@@ -914,18 +914,18 @@ module Generate (Target : Target_sig.S) = struct
914914
((pc, _) as cont)
915915
cloc
916916
acc =
917-
let g = Structure.build_graph ctx.blocks pc in
917+
let g = Structure.build_graph ctx.p pc in
918918
let dom = Structure.dominator_tree g in
919919
let rec translate_tree result_typ fall_through pc context =
920-
let block = Addr.Map.find pc ctx.blocks in
920+
let block = Code.block pc ctx.p in
921921
let keep_ouside pc' =
922922
match block.branch with
923923
| Switch _ -> true
924924
| Cond (_, (pc1, _), (pc2, _)) when pc' = pc1 && pc' = pc2 -> true
925925
| _ -> Structure.is_merge_node g pc'
926926
in
927927
let code ~context =
928-
let block = Addr.Map.find pc ctx.blocks in
928+
let block = Code.block pc ctx.p in
929929
let* () = translate_instrs ctx context block.body in
930930
translate_node_within
931931
~result_typ
@@ -960,7 +960,7 @@ module Generate (Target : Target_sig.S) = struct
960960
if
961961
(not (List.is_empty rem))
962962
||
963-
let block = Addr.Map.find pc ctx.blocks in
963+
let block = Code.block pc ctx.p in
964964
match block.branch with
965965
| Cond _ | Pushtrap _ -> false (*ZZZ also some Switch*)
966966
| _ -> true
@@ -970,7 +970,7 @@ module Generate (Target : Target_sig.S) = struct
970970
in
971971
translate_tree result_typ fall_through pc' context
972972
| [] -> (
973-
let block = Addr.Map.find pc ctx.blocks in
973+
let block = Code.block pc ctx.p in
974974
let branch = block.branch in
975975
match branch with
976976
| Branch cont -> translate_branch result_typ fall_through pc cont context
@@ -1028,7 +1028,7 @@ module Generate (Target : Target_sig.S) = struct
10281028
if List.is_empty args
10291029
then return ()
10301030
else
1031-
let block = Addr.Map.find dst ctx.blocks in
1031+
let block = Code.block dst ctx.p in
10321032
parallel_renaming block.params args
10331033
in
10341034
match fall_through with
@@ -1077,7 +1077,7 @@ module Generate (Target : Target_sig.S) = struct
10771077
~param_names
10781078
~body:
10791079
(let* () =
1080-
let block = Addr.Map.find pc ctx.blocks in
1080+
let block = Code.block pc ctx.p in
10811081
match block.body with
10821082
| Event start_loc :: _ -> event start_loc
10831083
| _ -> no_event
@@ -1190,13 +1190,7 @@ module Generate (Target : Target_sig.S) = struct
11901190
Code.Print.program (fun _ _ -> "") p;
11911191
*)
11921192
let ctx =
1193-
{ live = live_vars
1194-
; in_cps
1195-
; deadcode_sentinal
1196-
; blocks = p.blocks
1197-
; closures
1198-
; global_context
1199-
}
1193+
{ live = live_vars; in_cps; deadcode_sentinal; p; closures; global_context }
12001194
in
12011195
let toplevel_name = Var.fresh_n "toplevel" in
12021196
let functions =
@@ -1275,16 +1269,12 @@ let fix_switch_branches p =
12751269
with
12761270
| Some x -> x
12771271
| None ->
1278-
let pc' = !p'.free_pc in
1272+
let pc' = Code.free_pc !p' in
12791273
p' :=
1280-
{ !p' with
1281-
blocks =
1282-
Addr.Map.add
1283-
pc'
1284-
{ params = []; body = []; branch = Branch cont }
1285-
!p'.blocks
1286-
; free_pc = pc' + 1
1287-
};
1274+
Code.add_block
1275+
pc'
1276+
{ params = []; body = []; branch = Branch cont }
1277+
!p';
12881278
updates := Addr.Map.add pc ((args, pc') :: l) !updates;
12891279
pc')
12901280
, [] ))
@@ -1295,7 +1285,7 @@ let fix_switch_branches p =
12951285
match block.branch with
12961286
| Switch (_, l) -> fix_branches l
12971287
| _ -> ())
1298-
p.blocks;
1288+
(Code.blocks p);
12991289
!p'
13001290

13011291
module G = Generate (Gc_target)

compiler/lib-wasm/globalize.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ let traverse_instruction st i =
100100
| Event _ -> st
101101

102102
let traverse_block p st pc =
103-
let b = Code.Addr.Map.find pc p.Code.blocks in
103+
let b = Code.block pc p in
104104
let st = List.fold_left ~f:(fun st x -> declare x st) ~init:st b.Code.params in
105105
List.fold_left ~f:(fun st i -> traverse_instruction st i) ~init:st b.Code.body
106106

107107
let f p g closures =
108108
let l = Structure.blocks_in_reverse_post_order g in
109-
let in_loop = Freevars.find_loops_in_closure p p.Code.start in
109+
let in_loop = Freevars.find_loops_in_closure p (Code.start p) in
110110
let st =
111111
List.fold_left
112112
~f:(fun st pc ->

0 commit comments

Comments
 (0)