@@ -34,6 +34,7 @@ https://github.com/llvm/llvm-project/issues/58438
34
34
type constant_global =
35
35
{ init : W .expression option
36
36
; constant : bool
37
+ ; typ : W .value_type
37
38
}
38
39
39
40
type context =
@@ -206,6 +207,7 @@ let register_global name ?exported_name ?(constant = false) typ init st =
206
207
name
207
208
{ init = (if not typ.mut then Some init else None )
208
209
; constant = (not typ.mut) || constant
210
+ ; typ = typ.typ
209
211
}
210
212
st.context.constant_globals;
211
213
() , st
@@ -521,7 +523,6 @@ and expression_type (e : W.expression) st =
521
523
| I64ExtendI32 _
522
524
| F32DemoteF64 _
523
525
| F64PromoteF32 _
524
- | GlobalGet _
525
526
| BlockExpr _
526
527
| Call _
527
528
| RefFunc _
@@ -535,6 +536,9 @@ and expression_type (e : W.expression) st =
535
536
| Try _
536
537
| Br_on_null _ -> None , st
537
538
| 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
538
542
| Seq (_ , e' ) -> expression_type e' st
539
543
| Pop typ -> Some typ, st
540
544
| RefI31 _ -> Some (Ref { nullable = false ; typ = I31 }), st
@@ -624,21 +628,29 @@ let rec store ?(always = false) ?typ x e =
624
628
let * b = should_make_global x in
625
629
if b
626
630
then
627
- let * typ =
628
- match typ with
629
- | Some typ -> return typ
630
- | None -> value_type
631
- in
632
631
let * () =
633
632
let * b = global_is_registered x in
634
633
if b
635
634
then return ()
636
635
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
642
654
in
643
655
let * () = register_constant x (W. GlobalGet x) in
644
656
instr (GlobalSet (x, e))
0 commit comments