Table of Contents

5.4.2.14 GoSub Statement

Note

This section describes the implementation of MS-VBAL §5.4.2.14 GoSub Statement.

Syntax

AST node Instruction kind Notes
GoSubStatementNode GoSub Instruction.Target holds the resolved offset of the target label.

A GoSub statement's target is an expression naming or numbering a label or line. There is no static link between the GoSubStatementNode and the LineLabelNode or LineNumberNode it targets (RD-VBAL §3.4.2 Simple Statements).

As for GoTo, lowering alone resolves a GoSub target label. A GoSub statement lowers to InstructionKind.GoSub, whose Target holds the resolved offset (RD-VBAL §3.5.2 Instruction, RD-VBAL §3.5.3 Lowering Block Statements).

Static Semantics

Condition Result
The procedure defines the target label. Target is the label's offset.
The procedure does not define the target label, e.g. GoSub Done with no Done: line. The GoSub is left unresolved, and lowering reports VBC09309 Label not defined for it, as for GoTo (RD-VBAL §5.4.2.12 GoTo Statement).

Runtime Semantics

GoSub and Return give each activation its own GoSub Resumption List: a per-activation LIFO stack of return offsets (RD-VBAL §3.5.4 Execution).

  1. The GoSub pushes the offset right after itself onto the activation's GoSub Resumption List.
  2. Execution branches to the GoSub's target.

A successful On…GoSub branch performs the same push (RD-VBAL §5.4.2.16 On...GoSub Statement).

Nested GoSubs unwind in LIFO order: the innermost Return is taken first (RD-VBAL §5.4.2.15 Return Statement).

Neither GoSub's nor Return's runtime semantics resolve a target label at execution time; lowering resolves the target.

A label with no statement after it, including the end-label of the lexically enclosing procedure declaration, resolves to the offset right past the last instruction. A branch to it completes the activation as if execution had reached the end of the procedure body (RD-VBAL §3.5.1 InstructionList).

Implementation

  • RDCore.Runtime.Execution.ProcedureExecutor dispatches GoSub instructions.
  • The GoSub Resumption List is manipulated through CallStackFrame.PushGoSubReturn and CallStackFrame.TryPopGoSubReturn.
  • The GoSub Resumption List is a plain stack, not a per-offset dictionary like the other three activation states (block state, For loop state, For Each state).
  • ICallStackFrame.GoSubDepth exposes the number of entries in the GoSub Resumption List, not its top entry. No member exposes the entries themselves (RD-VBAL §3.5.5 Placement and Licensing).

⏮️ RD-VBAL §5.4.2.13 On...GoTo Statement | ⏭️ RD-VBAL §5.4.2.15 Return Statement