@@ -34,6 +34,7 @@ https://github.com/llvm/llvm-project/issues/58438
3434type constant_global =
3535 { init : W .expression option
3636 ; constant : bool
37+ ; typ : W .value_type
3738 }
3839
3940type 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))
0 commit comments