SXB 2.0 Bug: ENDSUB does not generate RETP
Bean,
The attached program gets compiled wrong if there is a·RETURN command in it.
A workaround for now is to GOTO a label above ENDSUB.
regards peter
The attached program gets compiled wrong if there is a·RETURN command in it.
134 =00000022 mysub: ;SUB mysub 135 136 0022 070D JNB onBreak,@__ELSE_1 ; if onBreak = 1 then 0023 0010 0A26 137 138 0025 000D RETP ; return 139 140 =00000026 __ELSE_1: ; endif 141 =00000026 __ENDIF_1: 142 143 0026 050D SETB onBreak ; onBreak = 1 144 145 ;ENDSUB 146 147 148 ;' ------------------------------------------------------------------------- 149 ;' Data Tables 150 ;' ------------------------------------------------------------------------- 151 0027 0000 DW 0 ;data 0
A workaround for now is to GOTO a label above ENDSUB.
134 =00000022 mysub: ;SUB mysub 135 136 0022 070D JNB onBreak,@__ELSE_1 ; if onBreak = 1 then 0023 0010 0A27 137 138 0025 0010 JMP @leaveSub ; goto leaveSub 0026 0A28 139 140 =00000027 __ELSE_1: ; endif 141 =00000027 __ENDIF_1: 142 143 0027 050D SETB onBreak ; onBreak = 1 144 145 =00000028 leaveSub: ;leaveSub: 146 147 0028 000D RETP ;ENDSUB 148 149 150 151 ;' ------------------------------------------------------------------------- 152 ;' Data Tables 153 ;' ------------------------------------------------------------------------- 154 0029 0000 DW 0 ;data 0
regards peter
Comments
If there is a RETURN between the SUB and the ENDSUB, then ENDSUB does NOT generate one. This was because alot of people had RETURN and then ENDSUB which generated two RETP instructions.
You can manually put a RETURN in before the ENDSUB, but it might be better to just let ENDSUB generate one anyway to avoid problems.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
·The next time you need a hero don't look up in the sky...Look in the mirror.
·
Maybe so. But using return should not make ENDSUB omit a RETP.
Also, having multiple exits can save codespace, especially when
returning values.
regards·peter
I must have missed that on the beta forum!
I vote for letting ENDSUB always generate a RETP.
Better safe than sorry.
regards peter
I also found a bug in ENDFUNC, if you use ENDFUNC without a return in the function (you get a warning), but __STACKPTR was not adjusted. So that has been fixed too.
These fixes will be in the next release (2.00.08 or 2.01).
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
·The next time you need a hero don't look up in the sky...Look in the mirror.
·
Now that you mention __STACKPTR,
does a return anywhere updates __STACKPTR inline·in case of locals,
or does it·jump to a compiler generated label that is at ENDSUB?
(I prefer the latter as it saves memory and also gives a single exit point).
regards peter
Cannot use a jump because you can actually declare locals after code, so you could have a RETURN in a IF block, then declare some more locals to do something else. I don't recommend doing that, but it can be done.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
·The next time you need a hero don't look up in the sky...Look in the mirror.
·
If you have a generated label at endsub or endfunc
someLabel:
··· restore __STKPOINTER
··· retp
you can jump to someLabel
Even if locals were declared after a conditional return.
the code at someLabel takes care of it.
You will know at the sub end wether or not to include stack restore code.
To me that seems the best solution.
regards peter