blob: 01d902ab8725f7c1a4cdcecbd44c9868bc7ecad5 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Header file for the Virtual DataBase Engine (VDBE)
13**
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.
drh75897232000-05-29 14:26:00 +000017*/
18#ifndef _SQLITE_VDBE_H_
19#define _SQLITE_VDBE_H_
20#include <stdio.h>
21
22/*
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
25** of this structure.
26*/
27typedef struct Vdbe Vdbe;
28
29/*
danielk19772dca4ac2008-01-03 11:50:29 +000030** The names of the following types declared in vdbeInt.h are required
31** for the VdbeOp definition.
32*/
danielk19772dca4ac2008-01-03 11:50:29 +000033typedef struct Mem Mem;
dan165921a2009-08-28 18:53:45 +000034typedef struct SubProgram SubProgram;
danielk19772dca4ac2008-01-03 11:50:29 +000035
36/*
drh75897232000-05-29 14:26:00 +000037** A single instruction of the virtual machine has an opcode
38** and as many as three operands. The instruction is recorded
39** as an instance of the following structure:
40*/
41struct VdbeOp {
drh905793e2004-02-21 13:31:09 +000042 u8 opcode; /* What operation to perform */
drhc9206282008-01-09 18:31:45 +000043 signed char p4type; /* One of the P4_xxx constants for p4 */
drha6c2ed92009-11-14 23:22:23 +000044 u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
drh119a5312008-01-02 17:25:54 +000045 u8 p5; /* Fifth parameter is an unsigned character */
drh75897232000-05-29 14:26:00 +000046 int p1; /* First operand */
47 int p2; /* Second parameter (often the jump destination) */
drh66a51672008-01-03 00:01:23 +000048 int p3; /* The third parameter */
drh9c7c9132015-06-26 18:16:52 +000049 union p4union { /* fourth parameter */
danielk19772dca4ac2008-01-03 11:50:29 +000050 int i; /* Integer value if p4type==P4_INT32 */
51 void *p; /* Generic pointer */
52 char *z; /* Pointer to data for string (char array) types */
53 i64 *pI64; /* Used when p4type is P4_INT64 */
54 double *pReal; /* Used when p4type is P4_REAL */
55 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
drh9c7c9132015-06-26 18:16:52 +000056 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
danielk19772dca4ac2008-01-03 11:50:29 +000057 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
58 Mem *pMem; /* Used when p4type is P4_MEM */
danielk1977595a5232009-07-24 17:58:53 +000059 VTable *pVtab; /* Used when p4type is P4_VTAB */
danielk19772dca4ac2008-01-03 11:50:29 +000060 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
drh0acb7e42008-06-25 00:12:41 +000061 int *ai; /* Used when p4type is P4_INTARRAY */
dan165921a2009-08-28 18:53:45 +000062 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
dana205a482011-08-27 18:48:57 +000063 int (*xAdvance)(BtCursor *, int *);
drh66a51672008-01-03 00:01:23 +000064 } p4;
drhc7379ce2013-10-30 02:28:23 +000065#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
shane9bcbdad2008-05-29 20:22:37 +000066 char *zComment; /* Comment to improve readability */
drhd4e70eb2008-01-02 00:34:36 +000067#endif
drh7b396862003-01-01 23:06:20 +000068#ifdef VDBE_PROFILE
drh15ab9412014-02-24 14:24:01 +000069 u32 cnt; /* Number of times this instruction was executed */
shane9bcbdad2008-05-29 20:22:37 +000070 u64 cycles; /* Total time spent executing this instruction */
drh7b396862003-01-01 23:06:20 +000071#endif
drh688852a2014-02-17 22:40:43 +000072#ifdef SQLITE_VDBE_COVERAGE
73 int iSrcLine; /* Source-code line that generated this opcode */
74#endif
drh75897232000-05-29 14:26:00 +000075};
76typedef struct VdbeOp VdbeOp;
77
dan165921a2009-08-28 18:53:45 +000078
79/*
80** A sub-routine used to implement a trigger program.
81*/
82struct SubProgram {
83 VdbeOp *aOp; /* Array of opcodes for sub-program */
84 int nOp; /* Elements in aOp[] */
85 int nMem; /* Number of memory cells required */
86 int nCsr; /* Number of cursors required */
dan1d8cb212011-12-09 13:24:16 +000087 int nOnce; /* Number of OP_Once instructions */
dan165921a2009-08-28 18:53:45 +000088 void *token; /* id that may be used to recursive triggers */
dand46def72010-07-24 11:28:28 +000089 SubProgram *pNext; /* Next sub-program already visited */
dan165921a2009-08-28 18:53:45 +000090};
91
drh75897232000-05-29 14:26:00 +000092/*
drh905793e2004-02-21 13:31:09 +000093** A smaller version of VdbeOp used for the VdbeAddOpList() function because
94** it takes up less space.
95*/
96struct VdbeOpList {
97 u8 opcode; /* What operation to perform */
98 signed char p1; /* First operand */
drh24003452008-01-03 01:28:59 +000099 signed char p2; /* Second parameter (often the jump destination) */
100 signed char p3; /* Third parameter */
drh905793e2004-02-21 13:31:09 +0000101};
102typedef struct VdbeOpList VdbeOpList;
103
104/*
dan165921a2009-08-28 18:53:45 +0000105** Allowed values of VdbeOp.p4type
drh99fcd712001-10-13 01:06:47 +0000106*/
drh66a51672008-01-03 00:01:23 +0000107#define P4_NOTUSED 0 /* The P4 parameter is not used */
108#define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */
109#define P4_STATIC (-2) /* Pointer to a static string */
110#define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */
111#define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */
112#define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */
drh66a51672008-01-03 00:01:23 +0000113#define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */
drh8d129422011-04-05 12:25:19 +0000114#define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
drh66a51672008-01-03 00:01:23 +0000115#define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */
116#define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */
117#define P4_REAL (-12) /* P4 is a 64-bit floating point value */
118#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
119#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
drh0acb7e42008-06-25 00:12:41 +0000120#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
dan165921a2009-08-28 18:53:45 +0000121#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
dana205a482011-08-27 18:48:57 +0000122#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
drh9c7c9132015-06-26 18:16:52 +0000123#define P4_FUNCCTX (-20) /* P4 is a pointer to an sqlite3_context object */
drh99fcd712001-10-13 01:06:47 +0000124
drhf9c8ce32013-11-05 13:33:55 +0000125/* Error message codes for OP_Halt */
126#define P5_ConstraintNotNull 1
127#define P5_ConstraintUnique 2
128#define P5_ConstraintCheck 3
129#define P5_ConstraintFK 4
130
drh99fcd712001-10-13 01:06:47 +0000131/*
danielk1977955de522006-02-10 02:27:42 +0000132** The Vdbe.aColName array contains 5n Mem structures, where n is the
133** number of columns of data returned by the statement.
134*/
135#define COLNAME_NAME 0
136#define COLNAME_DECLTYPE 1
137#define COLNAME_DATABASE 2
138#define COLNAME_TABLE 3
139#define COLNAME_COLUMN 4
drh3f913572008-03-22 01:07:17 +0000140#ifdef SQLITE_ENABLE_COLUMN_METADATA
141# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
142#else
143# ifdef SQLITE_OMIT_DECLTYPE
144# define COLNAME_N 1 /* Store only the name */
145# else
146# define COLNAME_N 2 /* Store the name and decltype */
147# endif
148#endif
danielk1977955de522006-02-10 02:27:42 +0000149
150/*
drh75897232000-05-29 14:26:00 +0000151** The following macro converts a relative address in the p2 field
152** of a VdbeOp structure into a negative number so that
danielk19774adee202004-05-08 08:23:19 +0000153** sqlite3VdbeAddOpList() knows that the address is relative. Calling
drh75897232000-05-29 14:26:00 +0000154** the macro again restores the address.
155*/
156#define ADDR(X) (-1-(X))
157
158/*
drh8f619cc2002-09-08 00:04:50 +0000159** The makefile scans the vdbe.c source file and creates the "opcodes.h"
160** header file that defines a number for each opcode used by the VDBE.
drh75897232000-05-29 14:26:00 +0000161*/
drh8f619cc2002-09-08 00:04:50 +0000162#include "opcodes.h"
drh75897232000-05-29 14:26:00 +0000163
164/*
165** Prototypes for the VDBE interface. See comments on the implementation
166** for a description of what each of these routines does.
167*/
drh9ac79622013-12-18 15:11:47 +0000168Vdbe *sqlite3VdbeCreate(Parse*);
drh66a51672008-01-03 00:01:23 +0000169int sqlite3VdbeAddOp0(Vdbe*,int);
170int sqlite3VdbeAddOp1(Vdbe*,int,int);
171int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
drh076e85f2015-09-03 13:46:12 +0000172int sqlite3VdbeGoto(Vdbe*,int);
173int sqlite3VdbeLoadString(Vdbe*,int,const char*);
174void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
drh66a51672008-01-03 00:01:23 +0000175int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
176int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
drh97bae792015-06-05 15:59:57 +0000177int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
drh8cff69d2009-11-12 19:59:44 +0000178int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
drh688852a2014-02-17 22:40:43 +0000179int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
drh5d9c9da2011-06-03 20:11:17 +0000180void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
drh0ff287f2015-09-02 18:40:33 +0000181void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
drh88caeac2011-08-24 15:12:08 +0000182void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
183void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
184void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
drh35573352008-01-08 23:54:25 +0000185void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
drhd654be82005-09-20 17:42:23 +0000186void sqlite3VdbeJumpHere(Vdbe*, int addr);
drh48f2d3b2011-09-16 01:34:43 +0000187void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
drh61019c72014-01-04 16:49:02 +0000188int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
drh66a51672008-01-03 00:01:23 +0000189void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
drh2ec2fb22013-11-06 19:59:23 +0000190void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
drhfb982642007-08-30 01:19:59 +0000191void sqlite3VdbeUsesBtree(Vdbe*, int);
danielk19774adee202004-05-08 08:23:19 +0000192VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
193int sqlite3VdbeMakeLabel(Vdbe*);
drh4611d922010-02-25 14:47:01 +0000194void sqlite3VdbeRunOnlyOnce(Vdbe*);
danielk19774adee202004-05-08 08:23:19 +0000195void sqlite3VdbeDelete(Vdbe*);
drhcb103b92012-10-26 00:11:23 +0000196void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
drh124c0b42011-06-01 18:15:55 +0000197void sqlite3VdbeMakeReady(Vdbe*,Parse*);
danielk19779e6db7d2004-06-21 08:18:51 +0000198int sqlite3VdbeFinalize(Vdbe*);
danielk19774adee202004-05-08 08:23:19 +0000199void sqlite3VdbeResolveLabel(Vdbe*, int);
200int sqlite3VdbeCurrentAddr(Vdbe*);
drh87cc3b32007-05-08 21:45:27 +0000201#ifdef SQLITE_DEBUG
danf3677212009-09-10 16:14:50 +0000202 int sqlite3VdbeAssertMayAbort(Vdbe *, int);
drh87cc3b32007-05-08 21:45:27 +0000203#endif
drh3c23a882007-01-09 14:01:13 +0000204void sqlite3VdbeResetStepResult(Vdbe*);
drh124c0b42011-06-01 18:15:55 +0000205void sqlite3VdbeRewind(Vdbe*);
drhc890fec2008-08-01 20:10:08 +0000206int sqlite3VdbeReset(Vdbe*);
danielk197722322fd2004-05-25 23:35:17 +0000207void sqlite3VdbeSetNumCols(Vdbe*,int);
danielk197710fb7492008-10-31 10:53:22 +0000208int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
danielk1977b28af712004-06-21 06:50:26 +0000209void sqlite3VdbeCountChanges(Vdbe*);
danielk1977aee18ef2005-03-09 12:26:50 +0000210sqlite3 *sqlite3VdbeDb(Vdbe*);
danielk19776ab3a2e2009-02-19 14:39:25 +0000211void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
drhc5155252007-01-08 21:07:17 +0000212void sqlite3VdbeSwap(Vdbe*,Vdbe*);
dan165921a2009-08-28 18:53:45 +0000213VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
drhcf0fd4a2013-08-01 12:21:58 +0000214sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
dan1d2ce4f2009-10-19 18:11:09 +0000215void sqlite3VdbeSetVarmask(Vdbe*, int);
drhc7bc4fd2009-11-25 18:03:42 +0000216#ifndef SQLITE_OMIT_TRACE
217 char *sqlite3VdbeExpandSql(Vdbe*, const char*);
218#endif
drh3eddb232014-06-28 14:28:06 +0000219int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
drh75897232000-05-29 14:26:00 +0000220
dan03e9cfc2011-09-05 14:20:27 +0000221void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
drh75179de2014-09-16 14:37:35 +0000222int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
dan7004f3f2015-03-30 12:06:26 +0000223int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
dan03e9cfc2011-09-05 14:20:27 +0000224UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
drh1e968a02008-03-25 00:22:21 +0000225
drh75179de2014-09-16 14:37:35 +0000226typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
dan3b9330f2014-02-27 20:44:18 +0000227RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
dan1fed5da2014-02-25 21:01:25 +0000228
dand19c9332010-07-26 12:05:17 +0000229#ifndef SQLITE_OMIT_TRIGGER
230void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
231#endif
232
drh72ffd092013-10-30 15:52:32 +0000233/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
234** each VDBE opcode.
235**
236** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
237** comments in VDBE programs that show key decision points in the code
238** generator.
239*/
drhc7379ce2013-10-30 02:28:23 +0000240#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
drhd3d39e92004-05-20 22:16:29 +0000241 void sqlite3VdbeComment(Vdbe*, const char*, ...);
242# define VdbeComment(X) sqlite3VdbeComment X
drh16ee60f2008-06-20 18:13:25 +0000243 void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
244# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
drh72ffd092013-10-30 15:52:32 +0000245# ifdef SQLITE_ENABLE_MODULE_COMMENTS
246# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
247# else
248# define VdbeModuleComment(X)
249# endif
drhd3d39e92004-05-20 22:16:29 +0000250#else
251# define VdbeComment(X)
drh16ee60f2008-06-20 18:13:25 +0000252# define VdbeNoopComment(X)
drh72ffd092013-10-30 15:52:32 +0000253# define VdbeModuleComment(X)
drhd3d39e92004-05-20 22:16:29 +0000254#endif
255
drh5655c542014-02-19 19:14:34 +0000256/*
257** The VdbeCoverage macros are used to set a coverage testing point
258** for VDBE branch instructions. The coverage testing points are line
259** numbers in the sqlite3.c source file. VDBE branch coverage testing
260** only works with an amalagmation build. That's ok since a VDBE branch
261** coverage build designed for testing the test suite only. No application
262** should ever ship with VDBE branch coverage measuring turned on.
263**
264** VdbeCoverage(v) // Mark the previously coded instruction
265** // as a branch
266**
267** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
268**
269** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
270**
271** VdbeCoverageNeverTaken(v) // Previous branch is never taken
272**
273** Every VDBE branch operation must be tagged with one of the macros above.
274** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
275** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
276** routine in vdbe.c, alerting the developer to the missed tag.
277*/
drh688852a2014-02-17 22:40:43 +0000278#ifdef SQLITE_VDBE_COVERAGE
279 void sqlite3VdbeSetLineNumber(Vdbe*,int);
280# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
281# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
drh5655c542014-02-19 19:14:34 +0000282# define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
283# define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
drhb06a4ec2014-03-10 18:03:09 +0000284# define VDBE_OFFSET_LINENO(x) (__LINE__+x)
drh688852a2014-02-17 22:40:43 +0000285#else
286# define VdbeCoverage(v)
287# define VdbeCoverageIf(v,x)
drh5655c542014-02-19 19:14:34 +0000288# define VdbeCoverageAlwaysTaken(v)
289# define VdbeCoverageNeverTaken(v)
drhb06a4ec2014-03-10 18:03:09 +0000290# define VDBE_OFFSET_LINENO(x) 0
drh688852a2014-02-17 22:40:43 +0000291#endif
292
dan6f9702e2014-11-01 20:38:06 +0000293#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drh518140e2014-11-06 03:55:10 +0000294void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
dan6f9702e2014-11-01 20:38:06 +0000295#else
dan037b5322014-11-03 11:25:32 +0000296# define sqlite3VdbeScanStatus(a,b,c,d,e)
dan6f9702e2014-11-01 20:38:06 +0000297#endif
298
drh75897232000-05-29 14:26:00 +0000299#endif