SQLite VDBE opcode

BeginSubrtn

Mark the beginning of a subroutine that can be entered in-line or that can be called using Gosub.

An example

12 of the 178 statements we compiled against the sample database use BeginSubrtn. This is the one with the shortest program; the rows that use BeginSubrtn are highlighted.

SELECT title, (SELECT count(*) FROM tracks t WHERE t.album_id = a.id) FROM albums a
addropcodep1p2p3p4p5
0Init02200
1OpenRead03030
2Rewind02100
3Column0210
4BeginSubrtn0300
5Null0440
6Integer1500
7Null0660
8OpenRead250k(2,,)2
9Rowid0700
10SeekGE214710
11IdxGT214710
12AggStep006count(0)0
13Next21110
14AggFinal600count(0)0
15Copy6400
16DecrJumpZero51700
17Return3510
18Copy4200
19ResultRow1200
20Next0301
21Halt0000
22Transaction00101
23Goto0100

Open it in the EXPLAIN visualizer →

Also in this program: AggFinal, AggStep, Column, Copy, DecrJumpZero, Goto, Halt, IdxGT, Init, Integer, Next, Null, OpenRead, ResultRow, Return, Rewind, Rowid, SeekGE, Transaction.

What the SQLite documentation says

Mark the beginning of a subroutine that can be entered in-line or that can be called using Gosub. The subroutine should be terminated by an Return instruction that has a P1 operand that is the same as the P2 operand to this opcode and that has P3 set to 1. If the subroutine is entered in-line, then the Return will simply fall through. But if the subroutine is entered using Gosub, then the Return will jump back to the first instruction after the Gosub.

This routine works by loading a NULL into the P2 register. When the return address register contains a NULL, the Return instruction is a no-op that simply falls through to the next instruction (assuming that the Return opcode has a P3 value of 1). Thus if the subroutine is entered in-line, then the Return will cause in-line execution to continue. But if the subroutine is entered via Gosub, then the Return will cause a return to the address following the Gosub.

This opcode is identical to Null. It has a different name only to make the byte code easier to read and verify.

From sqlite.org's list of opcodes for SQLite 3.54.1, in the public domain. Opcodes are not a stable interface and change between releases.

← AutoCommit · BitAnd →