ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How is the work of subroutin implemented?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How is the work of subroutin implemented?

    Hello everyone. I have a question about sabroutins. In the debugger, I can see that switching to subrutine does not cause the stack to grow, as is the case with functions. How, after the transition to the subrutine and its completion, is the return to the calling code carried out?

  • #2
    In RPG3 (RPG/400) it's possible to see the Intermediate Representation of the Program (IRP) by using GENOPT(*LIST) on the CRTRPGPGM command. This gives some insight into how the compiled RPG program works and, for subroutines, it appears that the CALLI (Call Internal) instruction is used and the 3rd operand (Return target) of this instruction stores the address of the instruction immediately following the CALLI instruction. The subroutine is compiled as instructions which end with a 'B' (Branch) instruction which uses the stored address to transfer control back to the instruction after the CALLI instruction (i.e. the RPG statement immediately following the EXSR operation).

    As far as I know, it's not possible to get such a listing for an RPG4 (ILE RPG) module or program, but as you say, the subroutine does NOT generally appear in the Program Stack displayed by the WRKJOB command so it could be that RPG4 is the same or similar in this regard.

    Somebody from IBM can probably give more information on this.

    Comment


    • #3
      I believe subroutines are implemented as a simple branch. The normal method for this kind of stuff is that the required return address is stored in a location associated with the subroutine and is used as the target for the return branch. In pseudo code it looks like this:

      Main line:
      1 Store address 3 in Subr1RtnPtr
      2 Branch to address of Subr1
      3 Whatever comes next

      Subroutine:
      Execute Subrs logic
      Branch to address in Subr1RtnPtr

      Hope this helps - but why do you care? This is a compiler implementation detail and is different between different RPG compilers.

      Comment

      Working...
      X