Stabel

Check-in [38a80b873f]
Login
Overview
Comment:Simplify implementation of array-get by using a block and break-if, instead of if instructions with possibly empty branches.
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: 38a80b873f44b502de72b8bf09ae70c28cff3e8444f34022cb850b0dd80b1a8d
User & Date: robin.hansen on 2021-08-29 17:32:42
Other Links: branch diff | manifest | tags
Context
2021-08-29
17:50
Added tests to verify correct array implementation. check-in: f3dcfa50ab user: robin.hansen tags: arrays
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

695
696
697
698
699
700
701
702

703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

722
723
724
725
726
727
728
729
730
731
732
733

734
735


736
737
738
739
740
741
742
743
744
745
746
747
      , results = []
      , locals = [ Wasm.Int32, Wasm.Int32 ]
      , instructions =
            [ callStackPopFn
            , Wasm.Local_Set 0 -- index to get
            , callStackPopFn
            , Wasm.Local_Set 1 -- array


            -- check for negative index
            , Wasm.Local_Get 0
            , Wasm.I32_Const 0
            , Wasm.I32_LT
            , Wasm.If
                [ Wasm.I32_Const 0
                , Wasm.I32_Const 0
                , callStackPushFn
                , callStackPushFn
                , Wasm.Return
                ]
                []

            -- Check for index too large
            , Wasm.Local_Get 0
            , Wasm.Local_Get 1
            , Wasm.I32_Load -- length of index
            , Wasm.I32_LT
            , Wasm.If

                [ -- Get element at index
                  Wasm.Local_Get 1
                , Wasm.I32_Const wasmPtrSize
                , Wasm.I32_Add -- move pointer beyond length
                , Wasm.Local_Get 0
                , Wasm.I32_Const wasmPtrSize
                , Wasm.I32_Mul -- offset
                , Wasm.I32_Add -- starting position + offset = ptr to element
                , Wasm.I32_Load -- element to return
                , Wasm.I32_Const 1 -- success flag
                , callStackPushFn
                , callStackPushFn

                ]
                [ -- return failure


                  Wasm.I32_Const 0
                , Wasm.I32_Const 0
                , callStackPushFn
                , callStackPushFn
                ]
            ]
      }
    , { id = 24
      , name = arraySetFn
      , args = []
      , results = []
      , locals = [ Wasm.Int32, Wasm.Int32, Wasm.Int32, Wasm.Int32, Wasm.Int32 ]







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










>

<
>
>
|
|
|
|
<







695
696
697
698
699
700
701

702
703
704
705
706
707





708


709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729

730
731
732
733
734
735

736
737
738
739
740
741
742
      , results = []
      , locals = [ Wasm.Int32, Wasm.Int32 ]
      , instructions =
            [ callStackPopFn
            , Wasm.Local_Set 0 -- index to get
            , callStackPopFn
            , Wasm.Local_Set 1 -- array

            , Wasm.Block
                [ -- check for negative index
                  Wasm.Local_Get 0
                , Wasm.I32_Const 0
                , Wasm.I32_LT
                , Wasm.BreakIf 0








                -- Check for index too large
                , Wasm.Local_Get 0
                , Wasm.Local_Get 1
                , Wasm.I32_Load -- length of array
                , Wasm.I32_GTE
                , Wasm.BreakIf 0

                -- Get element at index
                , Wasm.Local_Get 1
                , Wasm.I32_Const wasmPtrSize
                , Wasm.I32_Add -- move pointer beyond length
                , Wasm.Local_Get 0
                , Wasm.I32_Const wasmPtrSize
                , Wasm.I32_Mul -- offset
                , Wasm.I32_Add -- starting position + offset = ptr to element
                , Wasm.I32_Load -- element to return
                , Wasm.I32_Const 1 -- success flag
                , callStackPushFn
                , callStackPushFn
                , Wasm.Return
                ]


            -- Failure case
            , Wasm.I32_Const 0
            , Wasm.I32_Const 0
            , callStackPushFn
            , callStackPushFn

            ]
      }
    , { id = 24
      , name = arraySetFn
      , args = []
      , results = []
      , locals = [ Wasm.Int32, Wasm.Int32, Wasm.Int32, Wasm.Int32, Wasm.Int32 ]

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

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
        I32_Load ->
            Str "i32.load"

        I32_LT ->
            Str "i32.lt_s"

        I32_GTE ->
            Str "i32.gte_s"

        Drop ->
            Str "drop"

        Unreachable ->
            Str "unreachable"








|







424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
        I32_Load ->
            Str "i32.load"

        I32_LT ->
            Str "i32.lt_s"

        I32_GTE ->
            Str "i32.ge_s"

        Drop ->
            Str "drop"

        Unreachable ->
            Str "unreachable"