4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** Header file for the Virtual DataBase Engine (VDBE)
14 ** This header defines the interface to the virtual database engine
15 ** or VDBE. The VDBE implements an abstract machine that runs a
16 ** simple program to access and modify the underlying database.
23 ** A single VDBE is an opaque structure named "Vdbe". Only routines
24 ** in the source file sqliteVdbe.c are allowed to see the insides
27 typedef struct Vdbe Vdbe
;
30 ** The names of the following types declared in vdbeInt.h are required
31 ** for the VdbeOp definition.
33 typedef struct sqlite3_value Mem
;
34 typedef struct SubProgram SubProgram
;
35 typedef struct SubrtnSig SubrtnSig
;
38 ** A signature for a reusable subroutine that materializes the RHS of
42 int selId
; /* SELECT-id for the SELECT statement on the RHS */
43 char *zAff
; /* Affinity of the overall IN expression */
44 int iTable
; /* Ephemeral table generated by the subroutine */
45 int iAddr
; /* Subroutine entry address */
46 int regReturn
; /* Register used to hold return address */
50 ** A single instruction of the virtual machine has an opcode
51 ** and as many as three operands. The instruction is recorded
52 ** as an instance of the following structure:
55 u8 opcode
; /* What operation to perform */
56 signed char p4type
; /* One of the P4_xxx constants for p4 */
57 u16 p5
; /* Fifth parameter is an unsigned 16-bit integer */
58 int p1
; /* First operand */
59 int p2
; /* Second parameter (often the jump destination) */
60 int p3
; /* The third parameter */
61 union p4union
{ /* fourth parameter */
62 int i
; /* Integer value if p4type==P4_INT32 */
63 void *p
; /* Generic pointer */
64 char *z
; /* Pointer to data for string (char array) types */
65 i64
*pI64
; /* Used when p4type is P4_INT64 */
66 double *pReal
; /* Used when p4type is P4_REAL */
67 FuncDef
*pFunc
; /* Used when p4type is P4_FUNCDEF */
68 sqlite3_context
*pCtx
; /* Used when p4type is P4_FUNCCTX */
69 CollSeq
*pColl
; /* Used when p4type is P4_COLLSEQ */
70 Mem
*pMem
; /* Used when p4type is P4_MEM */
71 VTable
*pVtab
; /* Used when p4type is P4_VTAB */
72 KeyInfo
*pKeyInfo
; /* Used when p4type is P4_KEYINFO */
73 u32
*ai
; /* Used when p4type is P4_INTARRAY */
74 SubProgram
*pProgram
; /* Used when p4type is P4_SUBPROGRAM */
75 Table
*pTab
; /* Used when p4type is P4_TABLE */
76 SubrtnSig
*pSubrtnSig
; /* Used when p4type is P4_SUBRTNSIG */
77 #ifdef SQLITE_ENABLE_CURSOR_HINTS
78 Expr
*pExpr
; /* Used when p4type is P4_EXPR */
81 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
82 char *zComment
; /* Comment to improve readability */
84 #ifdef SQLITE_VDBE_COVERAGE
85 u32 iSrcLine
; /* Source-code line that generated this opcode
86 ** with flags in the upper 8 bits */
88 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
93 typedef struct VdbeOp VdbeOp
;
97 ** A sub-routine used to implement a trigger program.
100 VdbeOp
*aOp
; /* Array of opcodes for sub-program */
101 int nOp
; /* Elements in aOp[] */
102 int nMem
; /* Number of memory cells required */
103 int nCsr
; /* Number of cursors required */
104 u8
*aOnce
; /* Array of OP_Once flags */
105 void *token
; /* id that may be used to recursive triggers */
106 SubProgram
*pNext
; /* Next sub-program already visited */
110 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
111 ** it takes up less space.
114 u8 opcode
; /* What operation to perform */
115 signed char p1
; /* First operand */
116 signed char p2
; /* Second parameter (often the jump destination) */
117 signed char p3
; /* Third parameter */
119 typedef struct VdbeOpList VdbeOpList
;
122 ** Allowed values of VdbeOp.p4type
124 #define P4_NOTUSED 0 /* The P4 parameter is not used */
125 #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
126 #define P4_STATIC (-1) /* Pointer to a static string */
127 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
128 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
129 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
130 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
131 /* Above do not own any resources. Must free those below */
132 #define P4_FREE_IF_LE (-6)
133 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
134 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
135 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
136 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
137 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
138 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
139 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
140 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
141 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
142 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
143 #define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
144 #define P4_SUBRTNSIG (-17) /* P4 is a SubrtnSig pointer */
146 /* Error message codes for OP_Halt */
147 #define P5_ConstraintNotNull 1
148 #define P5_ConstraintUnique 2
149 #define P5_ConstraintCheck 3
150 #define P5_ConstraintFK 4
153 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
154 ** number of columns of data returned by the statement.
156 #define COLNAME_NAME 0
157 #define COLNAME_DECLTYPE 1
158 #define COLNAME_DATABASE 2
159 #define COLNAME_TABLE 3
160 #define COLNAME_COLUMN 4
161 #ifdef SQLITE_ENABLE_COLUMN_METADATA
162 # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
164 # ifdef SQLITE_OMIT_DECLTYPE
165 # define COLNAME_N 1 /* Store only the name */
167 # define COLNAME_N 2 /* Store the name and decltype */
172 ** The following macro converts a label returned by sqlite3VdbeMakeLabel()
173 ** into an index into the Parse.aLabel[] array that contains the resolved
174 ** address of that label.
176 #define ADDR(X) (~(X))
179 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
180 ** header file that defines a number for each opcode used by the VDBE.
185 ** Additional non-public SQLITE_PREPARE_* flags
187 #define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
188 #define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
191 ** Prototypes for the VDBE interface. See comments on the implementation
192 ** for a description of what each of these routines does.
194 Vdbe
*sqlite3VdbeCreate(Parse
*);
195 Parse
*sqlite3VdbeParser(Vdbe
*);
196 int sqlite3VdbeAddOp0(Vdbe
*,int);
197 int sqlite3VdbeAddOp1(Vdbe
*,int,int);
198 int sqlite3VdbeAddOp2(Vdbe
*,int,int,int);
199 int sqlite3VdbeGoto(Vdbe
*,int);
200 int sqlite3VdbeLoadString(Vdbe
*,int,const char*);
201 void sqlite3VdbeMultiLoad(Vdbe
*,int,const char*,...);
202 int sqlite3VdbeAddOp3(Vdbe
*,int,int,int,int);
203 int sqlite3VdbeAddOp4(Vdbe
*,int,int,int,int,const char *zP4
,int);
204 int sqlite3VdbeAddOp4Dup8(Vdbe
*,int,int,int,int,const u8
*,int);
205 int sqlite3VdbeAddOp4Int(Vdbe
*,int,int,int,int,int);
206 int sqlite3VdbeAddFunctionCall(Parse
*,int,int,int,int,const FuncDef
*,int);
207 void sqlite3VdbeEndCoroutine(Vdbe
*,int);
208 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
209 void sqlite3VdbeVerifyNoMallocRequired(Vdbe
*p
, int N
);
210 void sqlite3VdbeVerifyNoResultRow(Vdbe
*p
);
212 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
213 # define sqlite3VdbeVerifyNoResultRow(A)
215 #if defined(SQLITE_DEBUG)
216 void sqlite3VdbeVerifyAbortable(Vdbe
*p
, int);
217 void sqlite3VdbeNoJumpsOutsideSubrtn(Vdbe
*,int,int,int);
219 # define sqlite3VdbeVerifyAbortable(A,B)
220 # define sqlite3VdbeNoJumpsOutsideSubrtn(A,B,C,D)
222 VdbeOp
*sqlite3VdbeAddOpList(Vdbe
*, int nOp
, VdbeOpList
const *aOp
,int iLineno
);
223 #ifndef SQLITE_OMIT_EXPLAIN
224 int sqlite3VdbeExplain(Parse
*,u8
,const char*,...);
225 void sqlite3VdbeExplainPop(Parse
*);
226 int sqlite3VdbeExplainParent(Parse
*);
227 # define ExplainQueryPlan(P) sqlite3VdbeExplain P
228 # ifdef SQLITE_ENABLE_STMT_SCANSTATUS
229 # define ExplainQueryPlan2(V,P) (V = sqlite3VdbeExplain P)
231 # define ExplainQueryPlan2(V,P) ExplainQueryPlan(P)
233 # define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
234 # define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
236 # define ExplainQueryPlan(P)
237 # define ExplainQueryPlan2(V,P)
238 # define ExplainQueryPlanPop(P)
239 # define ExplainQueryPlanParent(P) 0
240 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
242 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
243 void sqlite3ExplainBreakpoint(const char*,const char*);
245 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
247 void sqlite3VdbeAddParseSchemaOp(Vdbe
*, int, char*, u16
);
248 void sqlite3VdbeChangeOpcode(Vdbe
*, int addr
, u8
);
249 void sqlite3VdbeChangeP1(Vdbe
*, int addr
, int P1
);
250 void sqlite3VdbeChangeP2(Vdbe
*, int addr
, int P2
);
251 void sqlite3VdbeChangeP3(Vdbe
*, int addr
, int P3
);
252 void sqlite3VdbeChangeP5(Vdbe
*, u16 P5
);
253 void sqlite3VdbeTypeofColumn(Vdbe
*, int);
254 void sqlite3VdbeJumpHere(Vdbe
*, int addr
);
255 void sqlite3VdbeJumpHereOrPopInst(Vdbe
*, int addr
);
256 int sqlite3VdbeChangeToNoop(Vdbe
*, int addr
);
257 int sqlite3VdbeDeletePriorOpcode(Vdbe
*, u8 op
);
259 void sqlite3VdbeReleaseRegisters(Parse
*,int addr
, int n
, u32 mask
, int);
261 # define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
263 void sqlite3VdbeChangeP4(Vdbe
*, int addr
, const char *zP4
, int N
);
264 void sqlite3VdbeAppendP4(Vdbe
*, void *pP4
, int p4type
);
265 void sqlite3VdbeSetP4KeyInfo(Parse
*, Index
*);
266 void sqlite3VdbeUsesBtree(Vdbe
*, int);
267 VdbeOp
*sqlite3VdbeGetOp(Vdbe
*, int);
268 VdbeOp
*sqlite3VdbeGetLastOp(Vdbe
*);
269 int sqlite3VdbeMakeLabel(Parse
*);
270 void sqlite3VdbeRunOnlyOnce(Vdbe
*);
271 void sqlite3VdbeReusable(Vdbe
*);
272 void sqlite3VdbeDelete(Vdbe
*);
273 void sqlite3VdbeMakeReady(Vdbe
*,Parse
*);
274 int sqlite3VdbeFinalize(Vdbe
*);
275 void sqlite3VdbeResolveLabel(Vdbe
*, int);
276 int sqlite3VdbeCurrentAddr(Vdbe
*);
278 int sqlite3VdbeAssertMayAbort(Vdbe
*, int);
280 void sqlite3VdbeResetStepResult(Vdbe
*);
281 void sqlite3VdbeRewind(Vdbe
*);
282 int sqlite3VdbeReset(Vdbe
*);
283 void sqlite3VdbeSetNumCols(Vdbe
*,int);
284 int sqlite3VdbeSetColName(Vdbe
*, int, int, const char *, void(*)(void*));
285 void sqlite3VdbeCountChanges(Vdbe
*);
286 sqlite3
*sqlite3VdbeDb(Vdbe
*);
287 u8
sqlite3VdbePrepareFlags(Vdbe
*);
288 void sqlite3VdbeSetSql(Vdbe
*, const char *z
, int n
, u8
);
289 #ifdef SQLITE_ENABLE_NORMALIZE
290 void sqlite3VdbeAddDblquoteStr(sqlite3
*,Vdbe
*,const char*);
291 int sqlite3VdbeUsesDoubleQuotedString(Vdbe
*,const char*);
293 void sqlite3VdbeSwap(Vdbe
*,Vdbe
*);
294 VdbeOp
*sqlite3VdbeTakeOpArray(Vdbe
*, int*, int*);
295 sqlite3_value
*sqlite3VdbeGetBoundValue(Vdbe
*, int, u8
);
296 void sqlite3VdbeSetVarmask(Vdbe
*, int);
297 #ifndef SQLITE_OMIT_TRACE
298 char *sqlite3VdbeExpandSql(Vdbe
*, const char*);
300 int sqlite3MemCompare(const Mem
*, const Mem
*, const CollSeq
*);
301 int sqlite3BlobCompare(const Mem
*, const Mem
*);
303 void sqlite3VdbeRecordUnpack(KeyInfo
*,int,const void*,UnpackedRecord
*);
304 int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord
*);
305 int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord
*, int);
306 UnpackedRecord
*sqlite3VdbeAllocUnpackedRecord(KeyInfo
*);
308 typedef int (*RecordCompare
)(int,const void*,UnpackedRecord
*);
309 RecordCompare
sqlite3VdbeFindCompare(UnpackedRecord
*);
311 void sqlite3VdbeLinkSubProgram(Vdbe
*, SubProgram
*);
312 int sqlite3VdbeHasSubProgram(Vdbe
*);
314 void sqlite3MemSetArrayInt64(sqlite3_value
*aMem
, int iIdx
, i64 val
);
316 int sqlite3NotPureFunc(sqlite3_context
*);
317 #ifdef SQLITE_ENABLE_BYTECODE_VTAB
318 int sqlite3VdbeBytecodeVtabInit(sqlite3
*);
321 /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
324 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
325 ** comments in VDBE programs that show key decision points in the code
328 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
329 void sqlite3VdbeComment(Vdbe
*, const char*, ...);
330 # define VdbeComment(X) sqlite3VdbeComment X
331 void sqlite3VdbeNoopComment(Vdbe
*, const char*, ...);
332 # define VdbeNoopComment(X) sqlite3VdbeNoopComment X
333 # ifdef SQLITE_ENABLE_MODULE_COMMENTS
334 # define VdbeModuleComment(X) sqlite3VdbeNoopComment X
336 # define VdbeModuleComment(X)
339 # define VdbeComment(X)
340 # define VdbeNoopComment(X)
341 # define VdbeModuleComment(X)
345 ** The VdbeCoverage macros are used to set a coverage testing point
346 ** for VDBE branch instructions. The coverage testing points are line
347 ** numbers in the sqlite3.c source file. VDBE branch coverage testing
348 ** only works with an amalgamation build. That's ok since a VDBE branch
349 ** coverage build designed for testing the test suite only. No application
350 ** should ever ship with VDBE branch coverage measuring turned on.
352 ** VdbeCoverage(v) // Mark the previously coded instruction
355 ** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
357 ** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
359 ** VdbeCoverageNeverTaken(v) // Previous branch is never taken
361 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
362 ** // taken on the first two ways. The
363 ** // NULL option is not possible
365 ** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
366 ** // in distinguishing equal and not-equal.
368 ** Every VDBE branch operation must be tagged with one of the macros above.
369 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
370 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
371 ** routine in vdbe.c, alerting the developer to the missed tag.
373 ** During testing, the test application will invoke
374 ** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
375 ** routine that is invoked as each bytecode branch is taken. The callback
376 ** contains the sqlite3.c source line number of the VdbeCoverage macro and
377 ** flags to indicate whether or not the branch was taken. The test application
378 ** is responsible for keeping track of this and reporting byte-code branches
379 ** that are never taken.
381 ** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
382 ** vdbe.c source file for additional information.
384 #ifdef SQLITE_VDBE_COVERAGE
385 void sqlite3VdbeSetLineNumber(Vdbe
*,int);
386 # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
387 # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
388 # define VdbeCoverageAlwaysTaken(v) \
389 sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
390 # define VdbeCoverageNeverTaken(v) \
391 sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
392 # define VdbeCoverageNeverNull(v) \
393 sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
394 # define VdbeCoverageNeverNullIf(v,x) \
395 if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
396 # define VdbeCoverageEqNe(v) \
397 sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
398 # define VDBE_OFFSET_LINENO(x) (__LINE__+x)
400 # define VdbeCoverage(v)
401 # define VdbeCoverageIf(v,x)
402 # define VdbeCoverageAlwaysTaken(v)
403 # define VdbeCoverageNeverTaken(v)
404 # define VdbeCoverageNeverNull(v)
405 # define VdbeCoverageNeverNullIf(v,x)
406 # define VdbeCoverageEqNe(v)
407 # define VDBE_OFFSET_LINENO(x) 0
410 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
411 void sqlite3VdbeScanStatus(Vdbe
*, int, int, int, LogEst
, const char*);
412 void sqlite3VdbeScanStatusRange(Vdbe
*, int, int, int);
413 void sqlite3VdbeScanStatusCounters(Vdbe
*, int, int, int);
415 # define sqlite3VdbeScanStatus(a,b,c,d,e,f)
416 # define sqlite3VdbeScanStatusRange(a,b,c,d)
417 # define sqlite3VdbeScanStatusCounters(a,b,c,d)
420 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
421 void sqlite3VdbePrintOp(FILE*, int, VdbeOp
*);
424 #if defined(SQLITE_ENABLE_CURSOR_HINTS) && defined(SQLITE_DEBUG)
425 int sqlite3CursorRangeHintExprCheck(Walker
*pWalker
, Expr
*pExpr
);
428 #endif /* SQLITE_VDBE_H */