Skip to content

Commit d1a3713

Browse files
committed
WIP
1 parent 0f1c60f commit d1a3713

File tree

7 files changed

+575
-245
lines changed

7 files changed

+575
-245
lines changed

compiler/lib-wasm/code_generation.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -566,21 +566,22 @@ let rec store ?(always = false) ?typ x e =
566566
let* b = should_make_global x in
567567
if b
568568
then
569-
let* typ =
570-
match typ with
571-
| Some typ -> return typ
572-
| None -> value_type
573-
in
574569
let* () =
575570
let* b = global_is_registered x in
576571
if b
577572
then return ()
578573
else
579-
register_global
580-
~constant:true
581-
x
582-
{ mut = true; typ }
583-
(W.RefI31 (Const (I32 0l)))
574+
let* typ =
575+
match typ with
576+
| Some typ -> return typ
577+
| None -> value_type
578+
in
579+
let default =
580+
match typ with
581+
| I32 -> W.Const (I32 0l)
582+
| _ -> W.RefI31 (Const (I32 0l))
583+
in
584+
register_global ~constant:true x { mut = true; typ } default
584585
in
585586
let* () = register_constant x (W.GlobalGet x) in
586587
instr (GlobalSet (x, e))

compiler/lib-wasm/curry.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ module Make (Target : Target_sig.S) = struct
298298
Memory.allocate
299299
~tag:0
300300
~deadcode_sentinal:(Code.Var.fresh ())
301+
~load
301302
(List.map ~f:(fun x -> `Var x) (List.tl l))
302303
in
303304
let* make_iterator =

compiler/lib-wasm/gc_target.ml

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,11 @@ module Value = struct
455455
let* i = i in
456456
return (W.RefTest ({ nullable = false; typ = I31 }, i))
457457

458-
let not i = val_int (Arith.eqz (int_val i))
458+
let not i = Arith.eqz i
459459

460-
let binop op i i' = val_int (op (int_val i) (int_val i'))
460+
let lt = Arith.( < )
461461

462-
let lt = binop Arith.( < )
463-
464-
let le = binop Arith.( <= )
462+
let le = Arith.( <= )
465463

466464
let ref_eq i i' =
467465
let* i = i in
@@ -571,41 +569,41 @@ module Value = struct
571569
(let* () = store xv x in
572570
let* () = store yv y in
573571
return ())
574-
(val_int (if negate then Arith.eqz n else n))
572+
(if negate then Arith.eqz n else n)
575573

576574
let eq x y = eq_gen ~negate:false x y
577575

578576
let neq x y = eq_gen ~negate:true x y
579577

580-
let ult = binop Arith.(ult)
578+
let ult = Arith.(ult)
581579

582580
let is_int i =
583581
let* i = i in
584-
val_int (return (W.RefTest ({ nullable = false; typ = I31 }, i)))
582+
return (W.RefTest ({ nullable = false; typ = I31 }, i))
585583

586-
let int_add = binop Arith.( + )
584+
let int_add = Arith.( + )
587585

588-
let int_sub = binop Arith.( - )
586+
let int_sub = Arith.( - )
589587

590-
let int_mul = binop Arith.( * )
588+
let int_mul = Arith.( * )
591589

592-
let int_div = binop Arith.( / )
590+
let int_div = Arith.( / )
593591

594-
let int_mod = binop Arith.( mod )
592+
let int_mod = Arith.( mod )
595593

596-
let int_neg i = val_int Arith.(const 0l - int_val i)
594+
let int_neg i = Arith.(const 0l - i)
597595

598-
let int_or = binop Arith.( lor )
596+
let int_or = Arith.( lor )
599597

600-
let int_and = binop Arith.( land )
598+
let int_and = Arith.( land )
601599

602-
let int_xor = binop Arith.( lxor )
600+
let int_xor = Arith.( lxor )
603601

604-
let int_lsl = binop Arith.( lsl )
602+
let int_lsl = Arith.( lsl )
605603

606-
let int_lsr i i' = val_int Arith.((int_val i land const 0x7fffffffl) lsr int_val i')
604+
let int_lsr i i' = Arith.((i land const 0x7fffffffl) lsr i')
607605

608-
let int_asr = binop Arith.( asr )
606+
let int_asr = Arith.( asr )
609607
end
610608

611609
module Memory = struct
@@ -657,7 +655,7 @@ module Memory = struct
657655
let* ty = Type.float_type in
658656
wasm_struct_get ty (wasm_cast ty e) 0
659657

660-
let allocate ~tag ~deadcode_sentinal l =
658+
let allocate ~tag ~deadcode_sentinal ~load l =
661659
if tag = 254
662660
then
663661
let* l =
@@ -728,23 +726,22 @@ module Memory = struct
728726
let* e = float_array_length (load a) in
729727
instr (W.Push e))
730728

731-
let array_get e e' = wasm_array_get e Arith.(Value.int_val e' + const 1l)
729+
let array_get e e' = wasm_array_get e Arith.(e' + const 1l)
732730

733-
let array_set e e' e'' = wasm_array_set e Arith.(Value.int_val e' + const 1l) e''
731+
let array_set e e' e'' = wasm_array_set e Arith.(e' + const 1l) e''
734732

735-
let float_array_get e e' =
736-
box_float (wasm_array_get ~ty:Type.float_array_type e (Value.int_val e'))
733+
let float_array_get e e' = box_float (wasm_array_get ~ty:Type.float_array_type e e')
737734

738735
let float_array_set e e' e'' =
739-
wasm_array_set ~ty:Type.float_array_type e (Value.int_val e') (unbox_float e'')
736+
wasm_array_set ~ty:Type.float_array_type e e' (unbox_float e'')
740737

741738
let gen_array_get e e' =
742739
let a = Code.Var.fresh_n "a" in
743740
let i = Code.Var.fresh_n "i" in
744741
block_expr
745742
{ params = []; result = [ Type.value ] }
746743
(let* () = store a e in
747-
let* () = store ~typ:I32 i (Value.int_val e') in
744+
let* () = store ~typ:I32 i e' in
748745
let* () =
749746
drop
750747
(block_expr
@@ -771,7 +768,7 @@ module Memory = struct
771768
let i = Code.Var.fresh_n "i" in
772769
let v = Code.Var.fresh_n "v" in
773770
let* () = store a e in
774-
let* () = store ~typ:I32 i (Value.int_val e') in
771+
let* () = store ~typ:I32 i e' in
775772
let* () = store v e'' in
776773
block
777774
{ params = []; result = [] }
@@ -801,11 +798,9 @@ module Memory = struct
801798
let* e = wasm_cast ty e in
802799
return (W.ArrayLen e)
803800

804-
let bytes_get e e' =
805-
Value.val_int (wasm_array_get ~ty:Type.string_type e (Value.int_val e'))
801+
let bytes_get e e' = wasm_array_get ~ty:Type.string_type e e'
806802

807-
let bytes_set e e' e'' =
808-
wasm_array_set ~ty:Type.string_type e (Value.int_val e') (Value.int_val e'')
803+
let bytes_set e e' e'' = wasm_array_set ~ty:Type.string_type e e' e''
809804

810805
let field e idx = wasm_array_get e (Arith.const (Int32.of_int (idx + 1)))
811806

@@ -1031,7 +1026,7 @@ module Constant = struct
10311026
let* e = Memory.make_int32 ~kind:`Nativeint (return (W.Const (I32 i))) in
10321027
return (Const, e)
10331028

1034-
let translate c =
1029+
let translate' c =
10351030
let* const, c = translate_rec c in
10361031
match const with
10371032
| Const ->
@@ -1049,6 +1044,11 @@ module Constant = struct
10491044
in
10501045
let* () = register_init_code (instr (W.GlobalSet (name, c))) in
10511046
return (W.GlobalGet name)
1047+
1048+
let translate ?(boxed = false) c =
1049+
match c with
1050+
| Code.Int i when not boxed -> return (W.Const (I32 (Targetint.to_int32 i)))
1051+
| _ -> translate' c
10521052
end
10531053

10541054
module Closure = struct

0 commit comments

Comments
 (0)