Stabel

Check-in [66a08ffa3d]
Login
Overview
Comment:Fixed bugs in array built ins.
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: 66a08ffa3d99acca5eba1932c421b03d95951f7b658eca1e5b4d7e927cbfc7e3
User & Date: robin.hansen on 2021-08-28 10:35:58
Other Links: branch diff | manifest | tags
Context
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
07:34
Update test_expression.js, with instructions on how to use it. check-in: 8f6b7a467b user: robin.hansen tags: arrays
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

668
669
670
671
672
673
674
675
676
677
678
679
680



681
682
683
684
685
686
687
688

689
690
691
692
693
694
695
...
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
748
749
750
751
752
            -- Set length
            , Wasm.Local_Get 1
            , Wasm.I32_Const 1
            , Wasm.I32_Add
            , Wasm.I32_Store -- store length in new array

            -- Copy
            , Wasm.Local_Get 0
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Add -- original array content start ptr
            , Wasm.Local_Get 2
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Add -- new array content start ptr



            , Wasm.Local_Get 1
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Mul -- bytes to copy to new array
            , Wasm.Memory_Copy

            -- Set new object
            , Wasm.Local_Get 2
            , Wasm.Local_Get 2

            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Mul
            , Wasm.I32_Add -- Last object pos
            , Wasm.Local_Get 3
            , Wasm.I32_Store

            -- 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














                [ Wasm.I32_Const 0
                , Wasm.I32_Const 0
                , callStackPushFn
                , callStackPushFn
                , Wasm.Return
                ]
                []

            -- 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
            ]
      }
    ]







<
<
<



>
>
>








>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
|



<

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



668
669
670
671
672
673
674



675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
...
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748

749














750
751
752
            -- Set length
            , Wasm.Local_Get 1
            , Wasm.I32_Const 1
            , Wasm.I32_Add
            , Wasm.I32_Store -- store length in new array

            -- Copy



            , Wasm.Local_Get 2
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Add -- new array content start ptr
            , Wasm.Local_Get 0
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Add -- original array content start ptr
            , Wasm.Local_Get 1
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Mul -- bytes to copy to new array
            , Wasm.Memory_Copy

            -- Set new object
            , Wasm.Local_Get 2
            , Wasm.Local_Get 2
            , Wasm.I32_Load
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Mul
            , Wasm.I32_Add -- Last object pos
            , Wasm.Local_Get 3
            , Wasm.I32_Store

            -- 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

                ]














            ]
      }
    ]

Modified test_expression.js from [115b939d03] to [284ab87777].

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

global.memView = new Uint32Array(memory.buffer, 0, 512);

async function init() {
    const wabt = await wabtInit();
    const wat = await compiler.toWat('main', `
        def: main
        : { 2 3 4 }
          0 1 -
          array-get
          drop
    `);

    const wasmModule = wabt.parseWat('tmp', wat, {
        bulk_memory: true
    }).toBinary({}).buffer;

    const imports = {







|
<
|
|







10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
25
26

global.memView = new Uint32Array(memory.buffer, 0, 512);

async function init() {
    const wabt = await wabtInit();
    const wat = await compiler.toWat('main', `
        def: main
        : { 5 6 7 }

          1 array-get
          swap drop
    `);

    const wasmModule = wabt.parseWat('tmp', wat, {
        bulk_memory: true
    }).toBinary({}).buffer;

    const imports = {

Modified wasm_tests/array.test.js from [0708b0e372] to [651ccf85db].

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    expect(result.stackElement()).toBe(7);
});

test('Get succeess', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : { 5 6 7 }
          2 array-get
          drop
    `);

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

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

test('Get out of lower bound', async () => {
    const wat = await compiler.toWat('main', `







|



<







75
76
77
78
79
80
81
82
83
84
85

86
87
88
89
90
91
92
    expect(result.stackElement()).toBe(7);
});

test('Get succeess', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : { 5 6 7 }
          1 array-get
          drop
    `);


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

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

test('Get out of lower bound', async () => {
    const wat = await compiler.toWat('main', `