Stabel

Check-in [bc0bc601c3]
Login
Overview
Comment:Simplify implementation of array-set by using a block and break-if instead of using if instructions where only one branch is in use
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: bc0bc601c3df920bd11ed7b8e3f96a1cbb1b65a5b9b6b0b7a251e2e1aca631f6
User & Date: robin.hansen on 2021-08-29 17:24:22
Original Comment: Simplify implementation of __copy_struct by using memory_copy instruction.
Other Links: branch diff | manifest | tags
Context
2021-08-29
17:32
Simplify implementation of array-get by using a block and break-if, instead of if instructions with ... check-in: 38a80b873f user: robin.hansen tags: arrays
17:24
Simplify implementation of array-set by using a block and break-if instead of using if instructions ... check-in: bc0bc601c3 user: robin.hansen tags: arrays
17:14
Simplify implementation of __copy_struct by using memory_copy instruction. check-in: 9c5f55e7c2 user: robin.hansen tags: arrays
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Codegen/BaseModule.elm from [f2161dd629] to [5a86d4cec2].

748
749
750
751
752
753
754
755

756

757
758
759







760
761











762
















763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
      , instructions =
            [ callStackPopFn
            , Wasm.Local_Set 0 -- index
            , callStackPopFn
            , Wasm.Local_Set 1 -- new value
            , callStackPopFn
            , Wasm.Local_Set 2 -- original array


            -- Bounds check

            , Wasm.Local_Get 0
            , Wasm.I32_Const 0
            , Wasm.I32_LT







            , Wasm.If
                -- TODO: Investigate using block and breakIf instead of single-branched if











                [ Wasm.Local_Get 2
















                , callStackPushFn
                , Wasm.Return
                ]
                []
            , Wasm.Local_Get 0
            , Wasm.Local_Get 2
            , Wasm.I32_Load
            , Wasm.Local_Tee 3 -- original array length
            , Wasm.I32_LT
            , Wasm.If
                []
                [ Wasm.Local_Get 2
                , callStackPushFn
                ]

            -- Copy original array
            , Wasm.Local_Get 3
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Mul
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Add -- Size of original array in bytes
            , Wasm.Local_Tee 3 -- overwrite
            , callAllocFn
            , Wasm.Local_Tee 4 -- New array

            , Wasm.Local_Get 2
            , Wasm.Local_Get 3
            , Wasm.Memory_Copy

            -- Set element at idx
            , Wasm.Local_Get 0
            , Wasm.I32_Const 1
            , Wasm.I32_Add -- to 'jump over' the length field of the array
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Mul -- offset
            , Wasm.Local_Get 4
            , Wasm.I32_Add -- address of idx in new array
            , Wasm.Local_Get 1
            , Wasm.I32_Store

            --Return
            , Wasm.Local_Get 4
            , callStackPushFn
            ]
      }
    ]







<
>
|
>
|
|
|
>
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
>

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




748
749
750
751
752
753
754

755
756
757
758
759
760
761
762
763
764
765
766
767
768

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799










800










801
802
















803
804
805
806
      , instructions =
            [ callStackPopFn
            , Wasm.Local_Set 0 -- index
            , callStackPopFn
            , Wasm.Local_Set 1 -- new value
            , callStackPopFn
            , Wasm.Local_Set 2 -- original array

            , Wasm.Block
                [ -- Bounds check
                  -- Lower bound
                  Wasm.Local_Get 0
                , Wasm.I32_Const 0
                , Wasm.I32_LT
                , Wasm.BreakIf 0

                -- Upper bound
                , Wasm.Local_Get 0
                , Wasm.Local_Get 2
                , Wasm.I32_Load
                , Wasm.Local_Tee 3 -- original array length
                , Wasm.I32_GTE

                , Wasm.BreakIf 0

                -- Copy original array
                , Wasm.Local_Get 3
                , Wasm.I32_Const wasmPtrSize
                , Wasm.I32_Mul
                , Wasm.I32_Const wasmPtrSize
                , Wasm.I32_Add -- Size of original array in bytes
                , Wasm.Local_Tee 3 -- overwrite
                , callAllocFn
                , Wasm.Local_Tee 4 -- New array
                , Wasm.Local_Get 2
                , Wasm.Local_Get 3
                , Wasm.Memory_Copy

                -- Set element at idx
                , Wasm.Local_Get 0
                , Wasm.I32_Const 1
                , Wasm.I32_Add -- to 'jump over' the length field of the array
                , Wasm.I32_Const wasmPtrSize
                , Wasm.I32_Mul -- offset
                , Wasm.Local_Get 4
                , Wasm.I32_Add -- address of idx in new array
                , Wasm.Local_Get 1
                , Wasm.I32_Store

                --Return
                , Wasm.Local_Get 4
                , callStackPushFn
                , Wasm.Return
                ]





















            -- Failure case
            , Wasm.Local_Get 2
















            , callStackPushFn
            ]
      }
    ]

Modified src/Stabel/Wasm.elm from [0c24fd4e9e] to [2e19074027].

93
94
95
96
97
98
99

100
101
102
103
104
105
106
...
422
423
424
425
426
427
428



429
430
431
432
433
434
435
    | I32_Div
    | I32_Eq
    | I32_NotEq
    | I32_EqZero
    | I32_Store
    | I32_Load
    | I32_LT

    | Drop
    | Unreachable
    | Commented String Instruction
    | Memory_Copy


maximumLocalIndex : Instruction -> Maybe Int
................................................................................

        I32_Load ->
            Str "i32.load"

        I32_LT ->
            Str "i32.lt_s"




        Drop ->
            Str "drop"

        Unreachable ->
            Str "unreachable"

        Commented comment inst ->







>







 







>
>
>







93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
    | I32_Div
    | I32_Eq
    | I32_NotEq
    | I32_EqZero
    | I32_Store
    | I32_Load
    | I32_LT
    | I32_GTE
    | Drop
    | Unreachable
    | Commented String Instruction
    | Memory_Copy


maximumLocalIndex : Instruction -> Maybe Int
................................................................................

        I32_Load ->
            Str "i32.load"

        I32_LT ->
            Str "i32.lt_s"

        I32_GTE ->
            Str "i32.gte_s"

        Drop ->
            Str "drop"

        Unreachable ->
            Str "unreachable"

        Commented comment inst ->