Stabel

Check-in [cc2965ccda]
Login
Overview
Comment:Codegen should be fixed. But the exhaustiveness checker still has bugs.
Timelines: family | ancestors | descendants | both | int-literals-in-pattern-match
Files: files | file ages | folders
SHA3-256: cc2965ccda1d913a313e360dface4ad0a6ebddb6cb827d8fb0074d471800f8a6
User & Date: robin.hansen on 2021-09-23 16:51:53
Other Links: branch diff | manifest | tags
Context
2021-09-24
09:33
Consider all multifunctions with a default branch to be exhaustive. Closed-Leaf check-in: abd5404398 user: robin.hansen tags: int-literals-in-pattern-match
2021-09-23
16:51
Codegen should be fixed. But the exhaustiveness checker still has bugs. check-in: cc2965ccda user: robin.hansen tags: int-literals-in-pattern-match
2021-09-22
10:47
Fix inexhaustiveness bugs. check-in: beb3f5edc2 user: robin.hansen tags: int-literals-in-pattern-match
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Codegen.elm from [8bebd10adc] to [91119aa4c4].

545
546
547
548
549
550
551
552
553








554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

569





570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
...
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
691
692
693
694
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
makeInequalityTest :
    List ( Type, Int )
    -> Int
    -> AST.TypeMatch
    -> Int
    -> Context
    -> ( Wasm.Instruction, Context )
makeInequalityTest boxMap selfIndex ((AST.TypeMatch _ typeFromTypeMatch _) as t_) localIdx context =
    let








        maybeBoxId =
            boxMap
                |> List.find (\( boxedType, _ ) -> Type.equalBaseType boxedType typeFromTypeMatch)
                |> Maybe.map Tuple.second
    in
    case ( t_, maybeBoxId ) of
        ( AST.TypeMatch _ Type.Int conditions, Just boxId ) ->
            ( Wasm.Batch
                [ Wasm.Local_Get localIdx
                , Wasm.I32_Load -- Load instance id
                , Wasm.I32_Const boxId
                , Wasm.I32_NotEq -- Types doesn't match?
                , Wasm.BreakIf 0 -- Move to next branch if above test is true
                , conditions
                    |> List.concatMap (matchingIntTest localIdx)

                    |> Wasm.Batch





                , Wasm.I32_Const selfIndex
                , BaseModule.callUnboxFn
                ]
            , context
            )

        ( AST.TypeMatch _ _ [], Just boxId ) ->
            ( Wasm.Batch
                [ Wasm.Local_Get localIdx
                , Wasm.I32_Load -- Load instance id
                , Wasm.I32_Const boxId
                , Wasm.I32_NotEq -- Types doesn't match?
                , Wasm.BreakIf 0 -- Move to next branch if above test is true
                , Wasm.I32_Const selfIndex
                , BaseModule.callUnboxFn
                ]
            , context
            )

        ( AST.TypeMatch _ (Type.Custom name) conditions, Nothing ) ->
            matchingStructTest boxMap selfIndex context localIdx name conditions

        ( AST.TypeMatch _ (Type.CustomGeneric name _) conditions, Nothing ) ->
            matchingStructTest boxMap selfIndex context localIdx name conditions

        _ ->
            -- Type not supported in pattern match
            -- TODO: TypeMatch should maybe change to only support types
            -- which are supported in pattern matches
            ( Wasm.Unreachable, context )


matchingIntTest : Int -> ( String, AST.TypeMatchValue ) -> List Wasm.Instruction
matchingIntTest localIdx ( _, astValue ) =
    let
        value =
            case astValue of
                AST.LiteralInt num ->
                    num

                _ ->
                    0
    in
    [ Wasm.Local_Get localIdx
    , Wasm.I32_Const BaseModule.wasmPtrSize
    , Wasm.I32_Add
    , Wasm.I32_Load -- int value
    , Wasm.I32_Const value
    , Wasm.I32_NotEq -- not same number?
    , Wasm.BreakIf 0 -- move to next branch
    ]


matchingStructTest :
    List ( Type, Int )
    -> Int
    -> Context
    -> Int
    -> String
    -> List ( String, AST.TypeMatchValue )
    -> ( Wasm.Instruction, Context )
matchingStructTest boxMap selfIndex context localIdx typeName conditions =
    let
        ( typeId, updatedContext ) =
            idForType typeName context

        ( conditionTestImpls, finalContext ) =
................................................................................
    )


matchingConditionTest :
    List ( Type, Int )
    -> Int
    -> Int
    -> ( String, AST.TypeMatchValue )
    -> ( List (List Wasm.Instruction), Context )
    -> ( List (List Wasm.Instruction), Context )
matchingConditionTest boxMap selfIndex localIdx ( fieldName, value ) ( result, context ) =
    let
        getterName =
            fieldName ++ ">"

................................................................................
        ( getterId, idContext ) =
            idForFunction getterName context

        callGetter =
            Wasm.Call getterId getterName
    in
    case value of
        AST.LiteralInt num ->
            ( [ Wasm.Local_Get localIdx
              , BaseModule.callStackPushFn
              , callGetter
              , BaseModule.callStackPopFn
              , Wasm.I32_Const num
              , Wasm.I32_NotEq -- not same number?
              , Wasm.BreakIf 0 -- move to next branch
              ]
                :: result
            , idContext
            )

        AST.LiteralType typ_ ->
            case typ_ of
                Type.Custom typeName ->
                    let
                        ( typeId, updatedContext ) =
                            idForType typeName idContext
                    in
                    ( [ Wasm.Local_Get localIdx
                      , BaseModule.callStackPushFn
                      , callGetter
                      , BaseModule.callStackPopFn
                      , Wasm.I32_Load -- get type id
                      , Wasm.I32_Const typeId
                      , Wasm.I32_NotEq -- not same type?
                      , Wasm.BreakIf 0 -- move to next branch
                      ]
                        :: result
                    , updatedContext
                    )

                _ ->
                    ( [ Wasm.Unreachable ] :: result
                    , context
                    )

        AST.RecursiveMatch match ->
            let
                nextLocalIdx =
                    localIdx + 1

                ( inequalityTestImpl, updatedContext ) =
                    makeInequalityTest boxMap selfIndex match nextLocalIdx idContext
            in







|

>
>
>
>
>
>
>
>





|
|






|
|
>
|
>
>
>
>
>






|












|


|








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







|







 







|







 







|












<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
<
<
<
<
<







545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
...
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
691
692
693
694
695
696
697
698
699





700
701
702
703
704
705
706
makeInequalityTest :
    List ( Type, Int )
    -> Int
    -> AST.TypeMatch
    -> Int
    -> Context
    -> ( Wasm.Instruction, Context )
makeInequalityTest boxMap selfIndex match localIdx context =
    let
        typeFromTypeMatch =
            case match of
                AST.TypeMatchInt _ _ ->
                    Type.Int

                AST.TypeMatchType _ tipe _ ->
                    tipe

        maybeBoxId =
            boxMap
                |> List.find (\( boxedType, _ ) -> Type.equalBaseType boxedType typeFromTypeMatch)
                |> Maybe.map Tuple.second
    in
    case ( match, maybeBoxId ) of
        ( AST.TypeMatchInt _ value, Just boxId ) ->
            ( Wasm.Batch
                [ Wasm.Local_Get localIdx
                , Wasm.I32_Load -- Load instance id
                , Wasm.I32_Const boxId
                , Wasm.I32_NotEq -- Types doesn't match?
                , Wasm.BreakIf 0 -- Move to next branch if above test is true
                , Wasm.Batch
                    [ Wasm.Local_Get localIdx
                    , Wasm.I32_Const BaseModule.wasmPtrSize
                    , Wasm.I32_Add
                    , Wasm.I32_Load -- int value
                    , Wasm.I32_Const value
                    , Wasm.I32_NotEq -- not same number?
                    , Wasm.BreakIf 0 -- move to next branch
                    ]
                , Wasm.I32_Const selfIndex
                , BaseModule.callUnboxFn
                ]
            , context
            )

        ( AST.TypeMatchType _ _ [], Just boxId ) ->
            ( Wasm.Batch
                [ Wasm.Local_Get localIdx
                , Wasm.I32_Load -- Load instance id
                , Wasm.I32_Const boxId
                , Wasm.I32_NotEq -- Types doesn't match?
                , Wasm.BreakIf 0 -- Move to next branch if above test is true
                , Wasm.I32_Const selfIndex
                , BaseModule.callUnboxFn
                ]
            , context
            )

        ( AST.TypeMatchType _ (Type.Custom name) conditions, Nothing ) ->
            matchingStructTest boxMap selfIndex context localIdx name conditions

        ( AST.TypeMatchType _ (Type.CustomGeneric name _) conditions, Nothing ) ->
            matchingStructTest boxMap selfIndex context localIdx name conditions

        _ ->
            -- Type not supported in pattern match
            -- TODO: TypeMatch should maybe change to only support types
            -- which are supported in pattern matches
            ( Wasm.Unreachable, context )























matchingStructTest :
    List ( Type, Int )
    -> Int
    -> Context
    -> Int
    -> String
    -> List ( String, AST.TypeMatch )
    -> ( Wasm.Instruction, Context )
matchingStructTest boxMap selfIndex context localIdx typeName conditions =
    let
        ( typeId, updatedContext ) =
            idForType typeName context

        ( conditionTestImpls, finalContext ) =
................................................................................
    )


matchingConditionTest :
    List ( Type, Int )
    -> Int
    -> Int
    -> ( String, AST.TypeMatch )
    -> ( List (List Wasm.Instruction), Context )
    -> ( List (List Wasm.Instruction), Context )
matchingConditionTest boxMap selfIndex localIdx ( fieldName, value ) ( result, context ) =
    let
        getterName =
            fieldName ++ ">"

................................................................................
        ( getterId, idContext ) =
            idForFunction getterName context

        callGetter =
            Wasm.Call getterId getterName
    in
    case value of
        AST.TypeMatchInt _ num ->
            ( [ Wasm.Local_Get localIdx
              , BaseModule.callStackPushFn
              , callGetter
              , BaseModule.callStackPopFn
              , Wasm.I32_Const num
              , Wasm.I32_NotEq -- not same number?
              , Wasm.BreakIf 0 -- move to next branch
              ]
                :: result
            , idContext
            )



        AST.TypeMatchType _ (Type.Custom typeName) [] ->
            let
                ( typeId, updatedContext ) =
                    idForType typeName idContext
            in
            ( [ Wasm.Local_Get localIdx
              , BaseModule.callStackPushFn
              , callGetter
              , BaseModule.callStackPopFn
              , Wasm.I32_Load -- get type id
              , Wasm.I32_Const typeId
              , Wasm.I32_NotEq -- not same type?
              , Wasm.BreakIf 0 -- move to next branch
              ]
                :: result
            , updatedContext
            )

        match ->





            let
                nextLocalIdx =
                    localIdx + 1

                ( inequalityTestImpl, updatedContext ) =
                    makeInequalityTest boxMap selfIndex match nextLocalIdx idContext
            in