Stabel

Check-in [c00f05aca3]
Login
Overview
Comment:Implement array-set.
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: c00f05aca303453284219a561eab029187f1bf1cd4e30155ad90c6bbe9c6c0ff
User & Date: robin.hansen on 2021-08-28 17:56:35
Other Links: branch diff | manifest | tags
Context
2021-08-29
17:14
Simplify implementation of __copy_struct by using memory_copy instruction. check-in: 9c5f55e7c2 user: robin.hansen tags: arrays
2021-08-28
17:56
Implement array-set. check-in: c00f05aca3 user: robin.hansen tags: arrays
10:35
Fixed bugs in array built ins. check-in: 66a08ffa3d user: robin.hansen tags: arrays
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Codegen.elm from [a7ce3dc66b] to [65a3071605].

844
845
846
847
848
849
850



851
852
853
854
855
856

                Builtin.ArrayPush ->
                    BaseModule.callArrayPushFn

                Builtin.ArrayGet ->
                    BaseModule.callArrayGetFn




        Box stackPos id ->
            Wasm.Batch
                [ Wasm.I32_Const stackPos
                , Wasm.I32_Const id
                , BaseModule.callBoxFn
                ]







>
>
>






844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859

                Builtin.ArrayPush ->
                    BaseModule.callArrayPushFn

                Builtin.ArrayGet ->
                    BaseModule.callArrayGetFn

                Builtin.ArraySet ->
                    BaseModule.callArraySetFn

        Box stackPos id ->
            Wasm.Batch
                [ Wasm.I32_Const stackPos
                , Wasm.I32_Const id
                , BaseModule.callBoxFn
                ]

Modified src/Stabel/Codegen/BaseModule.elm from [49b98645bd] to [70add3368f].

267
268
269
270
271
272
273










274
275
276
277
278
279
280
...
744
745
746
747
748
749
750
751












752






















































callArrayGetFn : Wasm.Instruction
callArrayGetFn =
    Wasm.Call 23 arrayGetFn













-- Base module


baseModule : Wasm.Module
baseModule =
    let
................................................................................
                [ -- return failure
                  Wasm.I32_Const 0
                , Wasm.I32_Const 0
                , callStackPushFn
                , callStackPushFn
                ]
            ]
      }












    ]



























































>
>
>
>
>
>
>
>
>
>







 








>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
...
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826


callArrayGetFn : Wasm.Instruction
callArrayGetFn =
    Wasm.Call 23 arrayGetFn


arraySetFn : String
arraySetFn =
    "__array_set"


callArraySetFn : Wasm.Instruction
callArraySetFn =
    Wasm.Call 24 arraySetFn



-- Base module


baseModule : Wasm.Module
baseModule =
    let
................................................................................
                [ -- 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 ]
      , 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
            ]
      }
    ]

Modified src/Stabel/Data/Builtin.elm from [8788cd1d69] to [bc2c41c8f3].

18
19
20
21
22
23
24

25
26
27
28
29
30
31
...
103
104
105
106
107
108
109





    | StackRightRotate
    | StackLeftRotate
    | Apply
    | ArrayEmpty
    | ArrayLength
    | ArrayPush
    | ArrayGet



functionType : Builtin -> FunctionType
functionType builtin =
    case builtin of
        Plus ->
            { input = [ Type.Int, Type.Int ]
................................................................................
            , output = [ Type.Array (Type.Generic "a") ]
            }

        ArrayGet ->
            { input = [ Type.Array (Type.Generic "a"), Type.Int ]
            , output = [ Type.Int, Type.Generic "a" ]
            }












>







 







>
>
>
>
>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
...
104
105
106
107
108
109
110
111
112
113
114
115
    | StackRightRotate
    | StackLeftRotate
    | Apply
    | ArrayEmpty
    | ArrayLength
    | ArrayPush
    | ArrayGet
    | ArraySet


functionType : Builtin -> FunctionType
functionType builtin =
    case builtin of
        Plus ->
            { input = [ Type.Int, Type.Int ]
................................................................................
            , output = [ Type.Array (Type.Generic "a") ]
            }

        ArrayGet ->
            { input = [ Type.Array (Type.Generic "a"), Type.Int ]
            , output = [ Type.Int, Type.Generic "a" ]
            }

        ArraySet ->
            { input = [ Type.Array (Type.Generic "a"), Type.Generic "a", Type.Int ]
            , output = [ Type.Array (Type.Generic "a") ]
            }

Modified src/Stabel/Qualifier.elm from [7624a204a9] to [e859a8f4e9].

119
120
121
122
123
124
125

126
127
128
129
130
131
132
        , ( "drop", Builtin.StackDrop )
        , ( "rotate", Builtin.StackRightRotate )
        , ( "-rotate", Builtin.StackLeftRotate )
        , ( "!", Builtin.Apply )
        , ( "array-length", Builtin.ArrayLength )
        , ( "array-push", Builtin.ArrayPush )
        , ( "array-get", Builtin.ArrayGet )

        ]


type alias RunConfig =
    { packageName : String
    , modulePath : String
    , ast : Parser.AST







>







119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        , ( "drop", Builtin.StackDrop )
        , ( "rotate", Builtin.StackRightRotate )
        , ( "-rotate", Builtin.StackLeftRotate )
        , ( "!", Builtin.Apply )
        , ( "array-length", Builtin.ArrayLength )
        , ( "array-push", Builtin.ArrayPush )
        , ( "array-get", Builtin.ArrayGet )
        , ( "array-set", Builtin.ArraySet )
        ]


type alias RunConfig =
    { packageName : String
    , modulePath : String
    , ast : Parser.AST

Modified wasm_tests/array.test.js from [651ccf85db] to [0d3ab8746c].

98
99
100
101
102
103
104
105
106
107
108


109
110
111
112
113
114
115
116

    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(0);
});

test('Get out of lower bound', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : { 5 6 7 }


          5 array-get
          drop
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(0);
});








|


|
>
>
|
|




|

>
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(0);
});

test('Set works', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : { 5 6 7 8 }
          20
          2 array-set
          2 array-get
          swap drop
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(20);
});