Skip to content

Commit 5682bd1

Browse files
committed
WIP
1 parent 4b5ba14 commit 5682bd1

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

compiler/lib-wasm/code_generation.ml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ https://github.com/llvm/llvm-project/issues/58438
3434
type constant_global =
3535
{ init : W.expression option
3636
; constant : bool
37+
; typ : W.value_type
3738
}
3839

3940
type context =
@@ -206,6 +207,7 @@ let register_global name ?exported_name ?(constant = false) typ init st =
206207
name
207208
{ init = (if not typ.mut then Some init else None)
208209
; constant = (not typ.mut) || constant
210+
; typ = typ.typ
209211
}
210212
st.context.constant_globals;
211213
(), st
@@ -521,7 +523,6 @@ and expression_type (e : W.expression) st =
521523
| I64ExtendI32 _
522524
| F32DemoteF64 _
523525
| F64PromoteF32 _
524-
| GlobalGet _
525526
| BlockExpr _
526527
| Call _
527528
| RefFunc _
@@ -535,6 +536,9 @@ and expression_type (e : W.expression) st =
535536
| Try _
536537
| Br_on_null _ -> None, st
537538
| LocalGet x | LocalTee (x, _) -> variable_type x st
539+
| GlobalGet x ->
540+
let typ = (Var.Map.find x st.context.constant_globals).typ in
541+
(if Poly.equal typ st.context.value_type then None else Some typ), st
538542
| Seq (_, e') -> expression_type e' st
539543
| Pop typ -> Some typ, st
540544
| RefI31 _ -> Some (Ref { nullable = false; typ = I31 }), st
@@ -624,21 +628,29 @@ let rec store ?(always = false) ?typ x e =
624628
let* b = should_make_global x in
625629
if b
626630
then
627-
let* typ =
628-
match typ with
629-
| Some typ -> return typ
630-
| None -> value_type
631-
in
632631
let* () =
633632
let* b = global_is_registered x in
634633
if b
635634
then return ()
636635
else
637-
register_global
638-
~constant:true
639-
x
640-
{ mut = true; typ }
641-
(W.RefI31 (Const (I32 0l)))
636+
let* typ =
637+
match typ with
638+
| Some typ -> return typ
639+
| None -> (
640+
let* typ = expression_type e in
641+
match typ with
642+
| None -> value_type
643+
| Some typ -> return typ)
644+
in
645+
let* default, typ', cast = default_value typ in
646+
let* () =
647+
register_constant
648+
x
649+
(match cast with
650+
| Some typ -> W.RefCast (typ, W.GlobalGet x)
651+
| None -> W.GlobalGet x)
652+
in
653+
register_global ~constant:true x { mut = true; typ = typ' } default
642654
in
643655
let* () = register_constant x (W.GlobalGet x) in
644656
instr (GlobalSet (x, e))

compiler/lib-wasm/gc_target.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,13 +1050,15 @@ module Constant = struct
10501050
if b then return c else store_in_global c
10511051
| Const_named name -> store_in_global ~name c
10521052
| Mutated ->
1053+
let* typ = Type.string_type in
10531054
let name = Code.Var.fresh_n "const" in
1055+
let* placeholder = array_placeholder typ in
10541056
let* () =
10551057
register_global
10561058
~constant:true
10571059
name
1058-
{ mut = true; typ = Type.value }
1059-
(W.RefI31 (Const (I32 0l)))
1060+
{ mut = true; typ = Ref { nullable = false; typ = Type typ } }
1061+
placeholder
10601062
in
10611063
let* () = register_init_code (instr (W.GlobalSet (name, c))) in
10621064
return (W.GlobalGet name)

0 commit comments

Comments
 (0)