Stabel

Check-in [72850142b1]
Login
Overview
Comment:Implement array-push.
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: 72850142b1c9160c38888655f371b95e5feb3073a18c28fcb587e76193dba631
User & Date: robin.hansen on 2021-08-24 17:21:47
Other Links: branch diff | manifest | tags
Context
2021-08-27
08:26
First attempt at array-get. Has a bug. check-in: 9c25d24e88 user: robin.hansen tags: arrays
2021-08-24
17:21
Implement array-push. check-in: 72850142b1 user: robin.hansen tags: arrays
2021-08-23
17:37
Fix regression in codegen due to a wrong available function id constant. check-in: e726f26c61 user: robin.hansen tags: arrays
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Codegen/BaseModule.elm from [71f9fe9cb0] to [e2ab26f5c1].

636
637
638
639
640
641
642
643
644



















645
646










647








648






            , callStackPushFn
            ]
      }
    , { id = 22
      , name = arrayPushFn
      , args = []
      , results = []
      , locals = []
      , instructions =



















            -- TODO
            []










      }








    ]













|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664

665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
            , callStackPushFn
            ]
      }
    , { id = 22
      , name = arrayPushFn
      , args = []
      , results = []
      , locals = [ Wasm.Int32, Wasm.Int32, Wasm.Int32, Wasm.Int32 ]
      , instructions =
            [ callStackPopFn
            , Wasm.Local_Set 3 -- object to push
            , callStackPopFn
            , Wasm.Local_Tee 0 -- original array pointer
            , Wasm.I32_Load
            , Wasm.Local_Tee 1 -- original array length
            , Wasm.I32_Const wasmPtrSize
            , Wasm.I32_Mul -- bytes required for new array
            , Wasm.I32_Const (wasmPtrSize * 2)
            , Wasm.I32_Add -- plus length and new object
            , callAllocFn
            , Wasm.Local_Tee 2 -- new array pointer

            -- 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
            , Wasm.Local_Get 2
            , callStackPushFn
            ]
      }
    ]

Modified src/Stabel/Qualifier.elm from [286af5303d] to [dfb6e3993b].

117
118
119
120
121
122
123

124
125
126
127
128
129
130
        , ( "swap", Builtin.StackSwap )
        , ( "dup", Builtin.StackDuplicate )
        , ( "drop", Builtin.StackDrop )
        , ( "rotate", Builtin.StackRightRotate )
        , ( "-rotate", Builtin.StackLeftRotate )
        , ( "!", Builtin.Apply )
        , ( "array-length", Builtin.ArrayLength )

        ]


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







>







117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        , ( "swap", Builtin.StackSwap )
        , ( "dup", Builtin.StackDuplicate )
        , ( "drop", Builtin.StackDrop )
        , ( "rotate", Builtin.StackRightRotate )
        , ( "-rotate", Builtin.StackLeftRotate )
        , ( "!", Builtin.Apply )
        , ( "array-length", Builtin.ArrayLength )
        , ( "array-push", Builtin.ArrayPush )
        ]


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

Modified src/Stabel/Wasm.elm from [3afad62349] to [9d9b871b9d].

94
95
96
97
98
99
100

101
102
103
104
105
106
107
...
415
416
417
418
419
420
421



422
423
424
425
426
427
428
    | I32_NotEq
    | I32_EqZero
    | I32_Store
    | I32_Load
    | Drop
    | Unreachable
    | Commented String Instruction



maximumLocalIndex : Instruction -> Maybe Int
maximumLocalIndex ins =
    case ins of
        Batch insList ->
            List.filterMap maximumLocalIndex insList
................................................................................

                BatchFormat batch ->
                    BatchFormat <| (Str <| ";; " ++ comment) :: batch

                Indent batch ->
                    Indent <| (Str <| ";; " ++ comment) :: batch





formatExport : ( String, Int ) -> FormatHint
formatExport ( functionName, index ) =
    Str <|
        "(export \""
            ++ functionName
            ++ "\" (func "







>







 







>
>
>







94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
    | I32_NotEq
    | I32_EqZero
    | I32_Store
    | I32_Load
    | Drop
    | Unreachable
    | Commented String Instruction
    | Memory_Copy


maximumLocalIndex : Instruction -> Maybe Int
maximumLocalIndex ins =
    case ins of
        Batch insList ->
            List.filterMap maximumLocalIndex insList
................................................................................

                BatchFormat batch ->
                    BatchFormat <| (Str <| ";; " ++ comment) :: batch

                Indent batch ->
                    Indent <| (Str <| ";; " ++ comment) :: batch

        Memory_Copy ->
            Str "memory.copy"


formatExport : ( String, Int ) -> FormatHint
formatExport ( functionName, index ) =
    Str <|
        "(export \""
            ++ functionName
            ++ "\" (func "

Modified wasm_tests/array.test.js from [798d028502] to [481f95590d].

1











2
3
4
5
6
7
8
9
10
11
12















const compiler = require('./compiler.wrapper');












test('Simple literal', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : { 1 2 3 4 5 } array-length
    `);

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

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
















>
>
>
>
>
>
>
>
>
>
>











>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const compiler = require('./compiler.wrapper');

test('Empty', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : {} array-length
    `);

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

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

test('Simple literal', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : { 1 2 3 4 5 } array-length
    `);

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

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

test('Pushing', async () => {
    const wat = await compiler.toWat('main', `
        def: main
        : {}
          2 array-push
          1 array-push
          array-length
    `);

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

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

Modified wasm_tests/compiler.wrapper.js from [527b014945] to [897ec18e2b].

51
52
53
54
55
56
57
58


59
60
61
62
63
64
65
            }
        });
    });
}

exports.run = async function run(wat, functionName) {
    const wabt = await wabtInit();
    const wasmModule = wabt.parseWat('tmp', wat).toBinary({}).buffer;



    const memory = new WebAssembly.Memory({
        initial: 1
    });

    const imports = {
        host: {







|
>
>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
            }
        });
    });
}

exports.run = async function run(wat, functionName) {
    const wabt = await wabtInit();
    const wasmModule = wabt.parseWat('tmp', wat, {
        bulk_memory: true
    }).toBinary({}).buffer;

    const memory = new WebAssembly.Memory({
        initial: 1
    });

    const imports = {
        host: {