bump version number and changelog
[sqlcipher.git] / src / vdbe.h
blob3257ff68a174d8dbd675368e21e4daccd5a782ae
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
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.
18 #ifndef SQLITE_VDBE_H
19 #define SQLITE_VDBE_H
20 #include <stdio.h>
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.
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;
37 ** 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:
41 struct VdbeOp {
42 u8 opcode; /* What operation to perform */
43 signed char p4type; /* One of the P4_xxx constants for p4 */
44 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
45 int p1; /* First operand */
46 int p2; /* Second parameter (often the jump destination) */
47 int p3; /* The third parameter */
48 union p4union { /* fourth parameter */
49 int i; /* Integer value if p4type==P4_INT32 */
50 void *p; /* Generic pointer */
51 char *z; /* Pointer to data for string (char array) types */
52 i64 *pI64; /* Used when p4type is P4_INT64 */
53 double *pReal; /* Used when p4type is P4_REAL */
54 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
55 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
56 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
57 Mem *pMem; /* Used when p4type is P4_MEM */
58 VTable *pVtab; /* Used when p4type is P4_VTAB */
59 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
60 u32 *ai; /* Used when p4type is P4_INTARRAY */
61 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
62 Table *pTab; /* Used when p4type is P4_TABLE */
63 #ifdef SQLITE_ENABLE_CURSOR_HINTS
64 Expr *pExpr; /* Used when p4type is P4_EXPR */
65 #endif
66 int (*xAdvance)(BtCursor *, int);
67 } p4;
68 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
69 char *zComment; /* Comment to improve readability */
70 #endif
71 #ifdef VDBE_PROFILE
72 u32 cnt; /* Number of times this instruction was executed */
73 u64 cycles; /* Total time spent executing this instruction */
74 #endif
75 #ifdef SQLITE_VDBE_COVERAGE
76 u32 iSrcLine; /* Source-code line that generated this opcode
77 ** with flags in the upper 8 bits */
78 #endif
80 typedef struct VdbeOp VdbeOp;
84 ** A sub-routine used to implement a trigger program.
86 struct SubProgram {
87 VdbeOp *aOp; /* Array of opcodes for sub-program */
88 int nOp; /* Elements in aOp[] */
89 int nMem; /* Number of memory cells required */
90 int nCsr; /* Number of cursors required */
91 u8 *aOnce; /* Array of OP_Once flags */
92 void *token; /* id that may be used to recursive triggers */
93 SubProgram *pNext; /* Next sub-program already visited */
97 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
98 ** it takes up less space.
100 struct VdbeOpList {
101 u8 opcode; /* What operation to perform */
102 signed char p1; /* First operand */
103 signed char p2; /* Second parameter (often the jump destination) */
104 signed char p3; /* Third parameter */
106 typedef struct VdbeOpList VdbeOpList;
109 ** Allowed values of VdbeOp.p4type
111 #define P4_NOTUSED 0 /* The P4 parameter is not used */
112 #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
113 #define P4_STATIC (-1) /* Pointer to a static string */
114 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
115 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
116 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
117 #define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
118 #define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
119 /* Above do not own any resources. Must free those below */
120 #define P4_FREE_IF_LE (-7)
121 #define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
122 #define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
123 #define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
124 #define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
125 #define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
126 #define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
127 #define P4_REAL (-13) /* P4 is a 64-bit floating point value */
128 #define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
129 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
130 #define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
131 #define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */
133 /* Error message codes for OP_Halt */
134 #define P5_ConstraintNotNull 1
135 #define P5_ConstraintUnique 2
136 #define P5_ConstraintCheck 3
137 #define P5_ConstraintFK 4
140 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
141 ** number of columns of data returned by the statement.
143 #define COLNAME_NAME 0
144 #define COLNAME_DECLTYPE 1
145 #define COLNAME_DATABASE 2
146 #define COLNAME_TABLE 3
147 #define COLNAME_COLUMN 4
148 #ifdef SQLITE_ENABLE_COLUMN_METADATA
149 # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
150 #else
151 # ifdef SQLITE_OMIT_DECLTYPE
152 # define COLNAME_N 1 /* Store only the name */
153 # else
154 # define COLNAME_N 2 /* Store the name and decltype */
155 # endif
156 #endif
159 ** The following macro converts a label returned by sqlite3VdbeMakeLabel()
160 ** into an index into the Parse.aLabel[] array that contains the resolved
161 ** address of that label.
163 #define ADDR(X) (~(X))
166 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
167 ** header file that defines a number for each opcode used by the VDBE.
169 #include "opcodes.h"
172 ** Additional non-public SQLITE_PREPARE_* flags
174 #define SQLITE_PREPARE_SAVESQL 0x80 /* Preserve SQL text */
175 #define SQLITE_PREPARE_MASK 0x0f /* Mask of public flags */
178 ** Prototypes for the VDBE interface. See comments on the implementation
179 ** for a description of what each of these routines does.
181 Vdbe *sqlite3VdbeCreate(Parse*);
182 Parse *sqlite3VdbeParser(Vdbe*);
183 int sqlite3VdbeAddOp0(Vdbe*,int);
184 int sqlite3VdbeAddOp1(Vdbe*,int,int);
185 int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
186 int sqlite3VdbeGoto(Vdbe*,int);
187 int sqlite3VdbeLoadString(Vdbe*,int,const char*);
188 void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
189 int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
190 int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
191 int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
192 int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
193 int sqlite3VdbeAddFunctionCall(Parse*,int,int,int,int,const FuncDef*,int);
194 void sqlite3VdbeEndCoroutine(Vdbe*,int);
195 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
196 void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
197 void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
198 #else
199 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
200 # define sqlite3VdbeVerifyNoResultRow(A)
201 #endif
202 #if defined(SQLITE_DEBUG)
203 void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
204 #else
205 # define sqlite3VdbeVerifyAbortable(A,B)
206 #endif
207 VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
208 #ifndef SQLITE_OMIT_EXPLAIN
209 void sqlite3VdbeExplain(Parse*,u8,const char*,...);
210 void sqlite3VdbeExplainPop(Parse*);
211 int sqlite3VdbeExplainParent(Parse*);
212 # define ExplainQueryPlan(P) sqlite3VdbeExplain P
213 # define ExplainQueryPlanPop(P) sqlite3VdbeExplainPop(P)
214 # define ExplainQueryPlanParent(P) sqlite3VdbeExplainParent(P)
215 #else
216 # define ExplainQueryPlan(P)
217 # define ExplainQueryPlanPop(P)
218 # define ExplainQueryPlanParent(P) 0
219 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
220 #endif
221 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
222 void sqlite3ExplainBreakpoint(const char*,const char*);
223 #else
224 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
225 #endif
226 void sqlite3VdbeAddParseSchemaOp(Vdbe*, int, char*, u16);
227 void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
228 void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
229 void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
230 void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
231 void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
232 void sqlite3VdbeJumpHere(Vdbe*, int addr);
233 void sqlite3VdbeJumpHereOrPopInst(Vdbe*, int addr);
234 int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
235 int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
236 #ifdef SQLITE_DEBUG
237 void sqlite3VdbeReleaseRegisters(Parse*,int addr, int n, u32 mask, int);
238 #else
239 # define sqlite3VdbeReleaseRegisters(P,A,N,M,F)
240 #endif
241 void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
242 void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
243 void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
244 void sqlite3VdbeUsesBtree(Vdbe*, int);
245 VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
246 int sqlite3VdbeMakeLabel(Parse*);
247 void sqlite3VdbeRunOnlyOnce(Vdbe*);
248 void sqlite3VdbeReusable(Vdbe*);
249 void sqlite3VdbeDelete(Vdbe*);
250 void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
251 void sqlite3VdbeMakeReady(Vdbe*,Parse*);
252 int sqlite3VdbeFinalize(Vdbe*);
253 void sqlite3VdbeResolveLabel(Vdbe*, int);
254 int sqlite3VdbeCurrentAddr(Vdbe*);
255 #ifdef SQLITE_DEBUG
256 int sqlite3VdbeAssertMayAbort(Vdbe *, int);
257 #endif
258 void sqlite3VdbeResetStepResult(Vdbe*);
259 void sqlite3VdbeRewind(Vdbe*);
260 int sqlite3VdbeReset(Vdbe*);
261 void sqlite3VdbeSetNumCols(Vdbe*,int);
262 int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
263 void sqlite3VdbeCountChanges(Vdbe*);
264 sqlite3 *sqlite3VdbeDb(Vdbe*);
265 u8 sqlite3VdbePrepareFlags(Vdbe*);
266 void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
267 #ifdef SQLITE_ENABLE_NORMALIZE
268 void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
269 int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
270 #endif
271 void sqlite3VdbeSwap(Vdbe*,Vdbe*);
272 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
273 sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
274 void sqlite3VdbeSetVarmask(Vdbe*, int);
275 #ifndef SQLITE_OMIT_TRACE
276 char *sqlite3VdbeExpandSql(Vdbe*, const char*);
277 #endif
278 int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
279 int sqlite3BlobCompare(const Mem*, const Mem*);
281 void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
282 int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
283 int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
284 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
286 typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
287 RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
289 void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
290 int sqlite3VdbeHasSubProgram(Vdbe*);
292 int sqlite3NotPureFunc(sqlite3_context*);
293 #ifdef SQLITE_ENABLE_BYTECODE_VTAB
294 int sqlite3VdbeBytecodeVtabInit(sqlite3*);
295 #endif
297 /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
298 ** each VDBE opcode.
300 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
301 ** comments in VDBE programs that show key decision points in the code
302 ** generator.
304 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
305 void sqlite3VdbeComment(Vdbe*, const char*, ...);
306 # define VdbeComment(X) sqlite3VdbeComment X
307 void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
308 # define VdbeNoopComment(X) sqlite3VdbeNoopComment X
309 # ifdef SQLITE_ENABLE_MODULE_COMMENTS
310 # define VdbeModuleComment(X) sqlite3VdbeNoopComment X
311 # else
312 # define VdbeModuleComment(X)
313 # endif
314 #else
315 # define VdbeComment(X)
316 # define VdbeNoopComment(X)
317 # define VdbeModuleComment(X)
318 #endif
321 ** The VdbeCoverage macros are used to set a coverage testing point
322 ** for VDBE branch instructions. The coverage testing points are line
323 ** numbers in the sqlite3.c source file. VDBE branch coverage testing
324 ** only works with an amalagmation build. That's ok since a VDBE branch
325 ** coverage build designed for testing the test suite only. No application
326 ** should ever ship with VDBE branch coverage measuring turned on.
328 ** VdbeCoverage(v) // Mark the previously coded instruction
329 ** // as a branch
331 ** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
333 ** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
335 ** VdbeCoverageNeverTaken(v) // Previous branch is never taken
337 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
338 ** // taken on the first two ways. The
339 ** // NULL option is not possible
341 ** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
342 ** // in distingishing equal and not-equal.
344 ** Every VDBE branch operation must be tagged with one of the macros above.
345 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
346 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
347 ** routine in vdbe.c, alerting the developer to the missed tag.
349 ** During testing, the test application will invoke
350 ** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
351 ** routine that is invoked as each bytecode branch is taken. The callback
352 ** contains the sqlite3.c source line number ov the VdbeCoverage macro and
353 ** flags to indicate whether or not the branch was taken. The test application
354 ** is responsible for keeping track of this and reporting byte-code branches
355 ** that are never taken.
357 ** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
358 ** vdbe.c source file for additional information.
360 #ifdef SQLITE_VDBE_COVERAGE
361 void sqlite3VdbeSetLineNumber(Vdbe*,int);
362 # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
363 # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
364 # define VdbeCoverageAlwaysTaken(v) \
365 sqlite3VdbeSetLineNumber(v,__LINE__|0x5000000);
366 # define VdbeCoverageNeverTaken(v) \
367 sqlite3VdbeSetLineNumber(v,__LINE__|0x6000000);
368 # define VdbeCoverageNeverNull(v) \
369 sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
370 # define VdbeCoverageNeverNullIf(v,x) \
371 if(x)sqlite3VdbeSetLineNumber(v,__LINE__|0x4000000);
372 # define VdbeCoverageEqNe(v) \
373 sqlite3VdbeSetLineNumber(v,__LINE__|0x8000000);
374 # define VDBE_OFFSET_LINENO(x) (__LINE__+x)
375 #else
376 # define VdbeCoverage(v)
377 # define VdbeCoverageIf(v,x)
378 # define VdbeCoverageAlwaysTaken(v)
379 # define VdbeCoverageNeverTaken(v)
380 # define VdbeCoverageNeverNull(v)
381 # define VdbeCoverageNeverNullIf(v,x)
382 # define VdbeCoverageEqNe(v)
383 # define VDBE_OFFSET_LINENO(x) 0
384 #endif
386 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
387 void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
388 #else
389 # define sqlite3VdbeScanStatus(a,b,c,d,e)
390 #endif
392 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
393 void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
394 #endif
396 #endif /* SQLITE_VDBE_H */