Stabel

Check-in [60f3575980]
Login
Overview
Comment:Wire up the first array builtins. We still require some changes in codegen and typechecker before we're ready to write the builtin implementation.
Timelines: family | ancestors | descendants | both | arrays
Files: files | file ages | folders
SHA3-256: 60f35759805c9357a6fb33d3bc8134a7d63fcf550071805097e3ca6598ac07a5
User & Date: robin.hansen on 2021-08-22 14:58:18
Other Links: branch diff | manifest | tags
Context
2021-08-23
16:56
Fill in missing pieces in type checker and codegen. Should be ready to implement array builtins. check-in: 73bcd35203 user: robin.hansen tags: arrays
2021-08-22
14:58
Wire up the first array builtins. We still require some changes in codegen and typechecker before we... check-in: 60f3575980 user: robin.hansen tags: arrays
10:39
I think type checking of arrays work now :) check-in: b5450e1656 user: robin.hansen tags: arrays
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Codegen.elm from [4ff3b6f63b] to [d82bd3d76e].

70
71
72
73
74
75
76

77
78
79
80
81
82
83
...
706
707
708
709
710
711
712





713
714
715
716
717
718
719
...
821
822
823
824
825
826
827









828
829
830
831
832
833


-- Codegen


type AstNode
    = IntLiteral Int

    | Function Int String -- id name
    | FunctionRef Int String -- id name
    | ConstructType Int Int -- id memberQty
    | SetMember Int Int -- offset memberQty
    | GetMember Int -- offset
    | Builtin Builtin
    | Box Int Int -- stackIdx typeId
................................................................................
    case node of
        IntLiteral value ->
            Wasm.Batch
                [ Wasm.I32_Const value
                , BaseModule.callStackPushFn
                ]






        Function id name ->
            Wasm.Call id name

        FunctionRef _ name ->
            let
                indexOfId =
                    indexOf name context.inlineFunctionNames
................................................................................

                Builtin.StackLeftRotate ->
                    BaseModule.callLeftRotFn

                Builtin.Apply ->
                    BaseModule.callExecInlineFn










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







>







 







>
>
>
>
>







 







>
>
>
>
>
>
>
>
>






70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
...
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
...
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848


-- Codegen


type AstNode
    = IntLiteral Int
    | ArrayLiteral (List AstNode)
    | Function Int String -- id name
    | FunctionRef Int String -- id name
    | ConstructType Int Int -- id memberQty
    | SetMember Int Int -- offset memberQty
    | GetMember Int -- offset
    | Builtin Builtin
    | Box Int Int -- stackIdx typeId
................................................................................
    case node of
        IntLiteral value ->
            Wasm.Batch
                [ Wasm.I32_Const value
                , BaseModule.callStackPushFn
                ]

        ArrayLiteral nodes ->
            Wasm.Batch <|
                BaseModule.callArrayEmptyFn
                    :: List.concatMap (\n -> [ nodeToInstruction context n, BaseModule.callArrayPushFn ]) nodes

        Function id name ->
            Wasm.Call id name

        FunctionRef _ name ->
            let
                indexOfId =
                    indexOf name context.inlineFunctionNames
................................................................................

                Builtin.StackLeftRotate ->
                    BaseModule.callLeftRotFn

                Builtin.Apply ->
                    BaseModule.callExecInlineFn

                Builtin.ArrayEmpty ->
                    BaseModule.callArrayEmptyFn

                Builtin.ArrayLength ->
                    BaseModule.callArrayLengthFn

                Builtin.ArrayPush ->
                    BaseModule.callArrayPushFn

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

Modified src/Stabel/Codegen/BaseModule.elm from [491f472a19] to [ec6af68d9f].

227
228
229
230
231
232
233






























234
235
236
237
238
239
240
...
574
575
576
577
578
579
580
581




























callExecInlineFn : Wasm.Instruction
callExecInlineFn =
    Wasm.Call 19 execInlineFn

































-- Base module


baseModule : Wasm.Module
baseModule =
    let
................................................................................
      , results = []
      , locals = []
      , instructions =
            [ callStackPopFn
            , Wasm.CallIndirect
            ]
      }
    ]

































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
...
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637


callExecInlineFn : Wasm.Instruction
callExecInlineFn =
    Wasm.Call 19 execInlineFn


arrayEmptyFn : String
arrayEmptyFn =
    "__array_empty"


callArrayEmptyFn : Wasm.Instruction
callArrayEmptyFn =
    Wasm.Call 20 arrayEmptyFn


arrayLengthFn : String
arrayLengthFn =
    "__array_length"


callArrayLengthFn : Wasm.Instruction
callArrayLengthFn =
    Wasm.Call 21 arrayLengthFn


arrayPushFn : String
arrayPushFn =
    "__array_push"


callArrayPushFn : Wasm.Instruction
callArrayPushFn =
    Wasm.Call 22 arrayPushFn



-- Base module


baseModule : Wasm.Module
baseModule =
    let
................................................................................
      , results = []
      , locals = []
      , instructions =
            [ callStackPopFn
            , Wasm.CallIndirect
            ]
      }

    -- ARRAY
    , { id = 20
      , name = arrayEmptyFn
      , args = []
      , results = []
      , locals = []
      , instructions =
            []
      }
    , { id = 21
      , name = arrayLengthFn
      , args = []
      , results = []
      , locals = []
      , instructions =
            []
      }
    , { id = 22
      , name = arrayPushFn
      , args = []
      , results = []
      , locals = []
      , instructions =
            []
      }
    ]

Modified src/Stabel/Data/Builtin.elm from [65af0595b2] to [272a506fbc].

14
15
16
17
18
19
20



21
22
23
24
25
26
27
..
79
80
81
82
83
84
85















    | Equal
    | StackDuplicate
    | StackDrop
    | StackSwap
    | StackRightRotate
    | StackLeftRotate
    | Apply





functionType : Builtin -> FunctionType
functionType builtin =
    case builtin of
        Plus ->
            { input = [ Type.Int, Type.Int ]
................................................................................
                , Type.FunctionSignature
                    { input = [ Type.StackRange "a" ]
                    , output = [ Type.StackRange "b" ]
                    }
                ]
            , output = [ Type.StackRange "b" ]
            }






















>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    | Equal
    | StackDuplicate
    | StackDrop
    | StackSwap
    | StackRightRotate
    | StackLeftRotate
    | Apply
    | ArrayEmpty
    | ArrayLength
    | ArrayPush


functionType : Builtin -> FunctionType
functionType builtin =
    case builtin of
        Plus ->
            { input = [ Type.Int, Type.Int ]
................................................................................
                , Type.FunctionSignature
                    { input = [ Type.StackRange "a" ]
                    , output = [ Type.StackRange "b" ]
                    }
                ]
            , output = [ Type.StackRange "b" ]
            }

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

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

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

Modified src/Stabel/Qualifier.elm from [8b735a46e7] to [38a212f63a].

1409
1410
1411
1412
1413
1414
1415







1416
1417
1418
1419
1420
1421
1422
                            acc.inlineFunctionNames
                                |> Set.union qualifyNodeResult.inlineFunctionNames
                                |> Set.insert inlineFuncName
                    }

                Err err ->
                    { acc | qualifiedNodes = Err err :: acc.qualifiedNodes }








        Parser.ArrayLiteral loc nodes ->
            let
                qualifyNodeResult =
                    initQualifyNode
                        config
                        acc.qualifiedTypes







>
>
>
>
>
>
>







1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
                            acc.inlineFunctionNames
                                |> Set.union qualifyNodeResult.inlineFunctionNames
                                |> Set.insert inlineFuncName
                    }

                Err err ->
                    { acc | qualifiedNodes = Err err :: acc.qualifiedNodes }

        Parser.ArrayLiteral loc [] ->
            { acc
                | qualifiedNodes =
                    Ok (Builtin (mapLoc loc) Builtin.ArrayEmpty)
                        :: acc.qualifiedNodes
            }

        Parser.ArrayLiteral loc nodes ->
            let
                qualifyNodeResult =
                    initQualifyNode
                        config
                        acc.qualifiedTypes

Modified src/Stabel/TypeChecker.elm from [1893c3764f] to [86e12614c6].

1258
1259
1260
1261
1262
1263
1264



1265
1266
1267
1268
1269
1270
1271
                unionizeTypes ts =
                    unionizeTypesHelper ts []

                unionizeTypesHelper ts acc =
                    case ts of
                        [] ->
                            case acc of



                                [ t ] ->
                                    t

                                _ ->
                                    Type.Union Nothing acc

                        t :: rest ->







>
>
>







1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
                unionizeTypes ts =
                    unionizeTypesHelper ts []

                unionizeTypesHelper ts acc =
                    case ts of
                        [] ->
                            case acc of
                                [] ->
                                    Type.Generic "a"

                                [ t ] ->
                                    t

                                _ ->
                                    Type.Union Nothing acc

                        t :: rest ->