Skip to content

Commit 7e12ce9

Browse files
committed
Array placeholder
1 parent 726800f commit 7e12ce9

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

compiler/lib-wasm/code_generation.ml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,44 @@ let should_make_global x st = Var.Set.mem x st.context.globalized_variables, st
514514

515515
let value_type st = st.context.value_type, st
516516

517+
let get_constant x st = Var.Hashtbl.find_opt st.context.constants x, st
518+
519+
let placeholder_value typ f =
520+
let* c = get_constant typ in
521+
match c with
522+
| None ->
523+
let x = Var.fresh () in
524+
let* () = register_constant typ (W.GlobalGet x) in
525+
let* () =
526+
register_global
527+
~constant:true
528+
x
529+
{ mut = false; typ = Ref { nullable = false; typ = Type typ } }
530+
(f typ)
531+
in
532+
return (W.GlobalGet x)
533+
| Some c -> return c
534+
535+
let array_placeholder typ = placeholder_value typ (fun typ -> ArrayNewFixed (typ, []))
536+
537+
let default_value val_typ st =
538+
match val_typ with
539+
| W.Ref { typ = I31 | Eq | Any; _ } -> (W.RefI31 (Const (I32 0l)), val_typ, None), st
540+
| W.Ref { typ = Type typ; nullable = false } -> (
541+
match (Var.Hashtbl.find st.context.types typ).typ with
542+
| Array _ ->
543+
(let* placeholder = array_placeholder typ in
544+
return (placeholder, val_typ, None))
545+
st
546+
| Struct _ | Func _ ->
547+
( ( W.RefNull (Type typ)
548+
, W.Ref { typ = Type typ; nullable = true }
549+
, Some { W.typ = Type typ; nullable = false } )
550+
, st ))
551+
| W.Ref { nullable = true; _ }
552+
| W.Ref { typ = Func | Extern | Struct | Array | None_; _ }
553+
| I32 | I64 | F32 | F64 -> assert false
554+
517555
let rec store ?(always = false) ?typ x e =
518556
let* e = e in
519557
match e with

compiler/lib-wasm/code_generation.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,9 @@ val function_body :
203203
-> param_names:Code.Var.t list
204204
-> body:unit t
205205
-> (Wasm_ast.var * Wasm_ast.value_type) list * Wasm_ast.instruction list
206+
207+
val array_placeholder : Code.Var.t -> expression
208+
209+
val default_value :
210+
Wasm_ast.value_type
211+
-> (Wasm_ast.expression * Wasm_ast.value_type * Wasm_ast.ref_type option) t

compiler/lib-wasm/gc_target.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ module Value = struct
431431

432432
let dummy_block =
433433
let* t = Type.block_type in
434-
return (W.ArrayNewFixed (t, []))
434+
array_placeholder t
435435

436436
let as_block e =
437437
let* t = Type.block_type in

0 commit comments

Comments
 (0)