From 7f7f53e044795523187882a2014ae4cee575074e Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Tue, 19 Nov 2019 13:10:56 +0100 Subject: [PATCH] Fix issue in basic block generation --- src/aeso_fcode_to_fate.erl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index d1b9226..682e9e1 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -1393,6 +1393,8 @@ block(Blk = #blk{code = [{switch, Arg, Type, Alts, Default} | Code], _ -> FreshBlk(Default ++ [{jump, RestRef}], Catchall) %% ^ fall-through to the outer catchall end, + %% If we don't generate a switch, we need to pop the argument if on the stack. + Pop = [{'POP', ?void} || Arg == ?a], {Blk1, Code1, AltBlks} = case Type of boolean -> @@ -1408,7 +1410,7 @@ block(Blk = #blk{code = [{switch, Arg, Type, Alts, Default} | Code], _ -> FalseCode ++ [{jump, RestRef}] end, case lists:usort(Alts) == [missing] of - true -> {Blk#blk{code = [{jump, DefRef}]}, [], []}; + true -> {Blk#blk{code = Pop ++ [{jump, DefRef}]}, [], []}; false -> case Arg of ?i(false) -> {Blk#blk{code = ElseCode}, [], ThenBlk}; @@ -1418,12 +1420,18 @@ block(Blk = #blk{code = [{switch, Arg, Type, Alts, Default} | Code], end; tuple -> [TCode] = Alts, - {Blk#blk{code = TCode ++ [{jump, RestRef}]}, [], []}; + case TCode of + missing -> {Blk#blk{code = Pop ++ [{jump, DefRef}]}, [], []}; + _ -> {Blk#blk{code = Pop ++ TCode ++ [{jump, RestRef}]}, [], []} + end; {variant, [_]} -> %% [SINGLE_CON_SWITCH] Single constructor switches don't need a %% switch instruction. [AltCode] = Alts, - {Blk#blk{code = AltCode ++ [{jump, RestRef}]}, [], []}; + case AltCode of + missing -> {Blk#blk{code = Pop ++ [{jump, DefRef}]}, [], []}; + _ -> {Blk#blk{code = Pop ++ AltCode ++ [{jump, RestRef}]}, [], []} + end; {variant, _Ar} -> MkBlk = fun(missing) -> {DefRef, []}; (ACode) -> FreshBlk(ACode ++ [{jump, RestRef}], DefRef)