Shop OBEX P1 Docs P2 Docs Learn Events
SXB 2.0 Bug: ENDSUB does not generate RETP — Parallax Forums

SXB 2.0 Bug: ENDSUB does not generate RETP

Peter VerkaikPeter Verkaik Posts: 3,956
edited 2009-01-29 18:39 in General Discussion
Bean,

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

  • JonnyMacJonnyMac Posts: 9,214
    edited 2009-01-29 15:13
    Isn't the second version considered better programming practice: one entry, one exit?
  • BeanBean Posts: 8,129
    edited 2009-01-29 15:41
    Peter,
    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.


    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-01-29 15:43
    Jon,
    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-01-29 15:51
    Bean,

    I must have missed that on the beta forum!

    I vote for letting ENDSUB always generate a RETP.
    Better safe than sorry.


    regards peter
  • BeanBean Posts: 8,129
    edited 2009-01-29 15:52
    I have changed the compiler so that ENDSUB always generates a RETP (and adjusts __STACKPTR if locals are used).

    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.


    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-01-29 16:44
    Bean,

    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
  • BeanBean Posts: 8,129
    edited 2009-01-29 17:33
    RETURN anywhere in a SUB or FUNC adjusts __STACKPTR if any locals are used.

    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.


    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-01-29 18:39
    Bean,
    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
Sign In or Register to comment.