Skip to content

Commit 3998e7d

Browse files
committed
Wasm code generation: separate functions to allocate regular blocks and float arrays
1 parent 0c68f65 commit 3998e7d

File tree

4 files changed

+34
-54
lines changed

4 files changed

+34
-54
lines changed

compiler/lib-wasm/curry.ml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,7 @@ module Make (Target : Target_sig.S) = struct
293293
(fun ~typ closure ->
294294
let* l = expression_list load l in
295295
call ?typ ~cps:true ~arity closure l)
296-
(let* args =
297-
(* We don't need the deadcode sentinal when the tag is 0 *)
298-
Memory.allocate
299-
~tag:0
300-
~deadcode_sentinal:(Code.Var.fresh ())
301-
~load
302-
(List.map ~f:(fun x -> `Var x) (List.tl l))
303-
in
296+
(let* args = Memory.allocate ~tag:0 (expression_list load (List.tl l)) in
304297
let* make_iterator =
305298
register_import ~name:"caml_apply_continuation" (Fun (Type.primitive_type 1))
306299
in

compiler/lib-wasm/gc_target.ml

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -666,33 +666,23 @@ module Memory = struct
666666
let* ty = Type.float_type in
667667
wasm_struct_get ty (wasm_cast ty e) 0
668668

669-
let allocate ~tag ~deadcode_sentinal ~load l =
670-
if tag = 254
671-
then
672-
let* l =
673-
expression_list
674-
(fun v ->
675-
match v with
676-
| `Var y ->
677-
if Code.Var.equal y deadcode_sentinal
678-
then return (W.Const (F64 0.))
679-
else unbox_float (load y)
680-
| `Expr e -> unbox_float (return e))
681-
l
682-
in
683-
let* ty = Type.float_array_type in
684-
return (W.ArrayNewFixed (ty, l))
685-
else
686-
let* l =
687-
expression_list
688-
(fun v ->
689-
match v with
690-
| `Var y -> load y
691-
| `Expr e -> return e)
692-
l
693-
in
694-
let* ty = Type.block_type in
695-
return (W.ArrayNewFixed (ty, RefI31 (Const (I32 (Int32.of_int tag))) :: l))
669+
let allocate ~tag l =
670+
assert (tag <> 254);
671+
let* l = l in
672+
let* ty = Type.block_type in
673+
return (W.ArrayNewFixed (ty, RefI31 (Const (I32 (Int32.of_int tag))) :: l))
674+
675+
let allocate_float_array ~deadcode_sentinal ~load l =
676+
let* l =
677+
expression_list
678+
(fun y ->
679+
if Code.Var.equal y deadcode_sentinal
680+
then return (W.Const (F64 0.))
681+
else unbox_float (load y))
682+
l
683+
in
684+
let* ty = Type.float_array_type in
685+
return (W.ArrayNewFixed (ty, l))
696686

697687
let tag e = wasm_array_get e (Arith.const 0l)
698688

compiler/lib-wasm/generate.ml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -773,16 +773,7 @@ module Generate (Target : Target_sig.S) = struct
773773
~ty:(Int Normalized)
774774
(fun i j -> Arith.((j < i) - (i < j)));
775775
register_prim "%js_array" `Pure (fun ctx _ l ->
776-
let* l =
777-
List.fold_right
778-
~f:(fun x acc ->
779-
let* x = transl_prim_arg ctx x in
780-
let* acc = acc in
781-
return (`Expr x :: acc))
782-
l
783-
~init:(return [])
784-
in
785-
Memory.allocate ~tag:0 ~deadcode_sentinal:ctx.deadcode_sentinal ~load l)
776+
Memory.allocate ~tag:0 (expression_list (fun x -> transl_prim_arg ctx x) l))
786777

787778
let rec translate_expr ctx context x e =
788779
match e with
@@ -822,11 +813,16 @@ module Generate (Target : Target_sig.S) = struct
822813
in
823814
return (W.Call (apply, args @ [ closure ]))
824815
| Block (tag, a, _, _) ->
825-
Memory.allocate
826-
~deadcode_sentinal:ctx.deadcode_sentinal
827-
~tag
828-
~load:(fun x -> load_and_box ctx x)
829-
(List.map ~f:(fun x -> `Var x) (Array.to_list a))
816+
if tag = 254
817+
then
818+
Memory.allocate_float_array
819+
~deadcode_sentinal:ctx.deadcode_sentinal
820+
~load
821+
(Array.to_list a)
822+
else
823+
Memory.allocate
824+
~tag
825+
(expression_list (fun x -> load_and_box ctx x) (Array.to_list a))
830826
| Field (x, n, Non_float) -> Memory.field (load_and_box ctx x) n
831827
| Field (x, n, Float) ->
832828
Memory.float_array_get

compiler/lib-wasm/target_sig.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ module type S = sig
2020
type expression = Code_generation.expression
2121

2222
module Memory : sig
23-
val allocate :
24-
tag:int
25-
-> deadcode_sentinal:Code.Var.t
23+
val allocate : tag:int -> Wasm_ast.expression list Code_generation.t -> expression
24+
25+
val allocate_float_array :
26+
deadcode_sentinal:Code.Var.t
2627
-> load:(Code.Var.t -> expression)
27-
-> [ `Expr of Wasm_ast.expression | `Var of Wasm_ast.var ] list
28+
-> Wasm_ast.var list
2829
-> expression
2930

3031
val load_function_pointer :

0 commit comments

Comments
 (0)