drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 17 | */ |
| 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 | */ |
| 27 | typedef struct Vdbe Vdbe; |
| 28 | |
| 29 | /* |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 30 | ** The names of the following types declared in vdbeInt.h are required |
| 31 | ** for the VdbeOp definition. |
| 32 | */ |
| 33 | typedef struct VdbeFunc VdbeFunc; |
| 34 | typedef struct Mem Mem; |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 35 | typedef struct SubProgram SubProgram; |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 36 | |
| 37 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 38 | ** A single instruction of the virtual machine has an opcode |
| 39 | ** and as many as three operands. The instruction is recorded |
| 40 | ** as an instance of the following structure: |
| 41 | */ |
| 42 | struct VdbeOp { |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 43 | u8 opcode; /* What operation to perform */ |
drh | c920628 | 2008-01-09 18:31:45 +0000 | [diff] [blame] | 44 | signed char p4type; /* One of the P4_xxx constants for p4 */ |
drh | a6c2ed9 | 2009-11-14 23:22:23 +0000 | [diff] [blame] | 45 | u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */ |
drh | 119a531 | 2008-01-02 17:25:54 +0000 | [diff] [blame] | 46 | u8 p5; /* Fifth parameter is an unsigned character */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 47 | int p1; /* First operand */ |
| 48 | int p2; /* Second parameter (often the jump destination) */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 49 | int p3; /* The third parameter */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 50 | union { /* fourth parameter */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 51 | int i; /* Integer value if p4type==P4_INT32 */ |
| 52 | void *p; /* Generic pointer */ |
| 53 | char *z; /* Pointer to data for string (char array) types */ |
| 54 | i64 *pI64; /* Used when p4type is P4_INT64 */ |
| 55 | double *pReal; /* Used when p4type is P4_REAL */ |
| 56 | FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */ |
| 57 | VdbeFunc *pVdbeFunc; /* Used when p4type is P4_VDBEFUNC */ |
| 58 | CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */ |
| 59 | Mem *pMem; /* Used when p4type is P4_MEM */ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 60 | VTable *pVtab; /* Used when p4type is P4_VTAB */ |
danielk1977 | 2dca4ac | 2008-01-03 11:50:29 +0000 | [diff] [blame] | 61 | KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 62 | int *ai; /* Used when p4type is P4_INTARRAY */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 63 | SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 64 | } p4; |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 65 | #ifdef SQLITE_DEBUG |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 66 | char *zComment; /* Comment to improve readability */ |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 67 | #endif |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 68 | #ifdef VDBE_PROFILE |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 69 | int cnt; /* Number of times this instruction was executed */ |
| 70 | u64 cycles; /* Total time spent executing this instruction */ |
drh | 7b39686 | 2003-01-01 23:06:20 +0000 | [diff] [blame] | 71 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 72 | }; |
| 73 | typedef struct VdbeOp VdbeOp; |
| 74 | |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | ** A sub-routine used to implement a trigger program. |
| 78 | */ |
| 79 | struct SubProgram { |
| 80 | VdbeOp *aOp; /* Array of opcodes for sub-program */ |
| 81 | int nOp; /* Elements in aOp[] */ |
| 82 | int nMem; /* Number of memory cells required */ |
| 83 | int nCsr; /* Number of cursors required */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 84 | void *token; /* id that may be used to recursive triggers */ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 85 | SubProgram *pNext; /* Next sub-program already visited */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 88 | /* |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 89 | ** A smaller version of VdbeOp used for the VdbeAddOpList() function because |
| 90 | ** it takes up less space. |
| 91 | */ |
| 92 | struct VdbeOpList { |
| 93 | u8 opcode; /* What operation to perform */ |
| 94 | signed char p1; /* First operand */ |
drh | 2400345 | 2008-01-03 01:28:59 +0000 | [diff] [blame] | 95 | signed char p2; /* Second parameter (often the jump destination) */ |
| 96 | signed char p3; /* Third parameter */ |
drh | 905793e | 2004-02-21 13:31:09 +0000 | [diff] [blame] | 97 | }; |
| 98 | typedef struct VdbeOpList VdbeOpList; |
| 99 | |
| 100 | /* |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 101 | ** Allowed values of VdbeOp.p4type |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 102 | */ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 103 | #define P4_NOTUSED 0 /* The P4 parameter is not used */ |
| 104 | #define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */ |
| 105 | #define P4_STATIC (-2) /* Pointer to a static string */ |
| 106 | #define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */ |
| 107 | #define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */ |
| 108 | #define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */ |
| 109 | #define P4_VDBEFUNC (-7) /* P4 is a pointer to a VdbeFunc structure */ |
| 110 | #define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */ |
| 111 | #define P4_TRANSIENT (-9) /* P4 is a pointer to a transient string */ |
| 112 | #define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */ |
| 113 | #define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */ |
| 114 | #define P4_REAL (-12) /* P4 is a 64-bit floating point value */ |
| 115 | #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ |
| 116 | #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 117 | #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 118 | #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 119 | |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 120 | /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 121 | ** is made. That copy is freed when the Vdbe is finalized. But if the |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 122 | ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 123 | ** gets freed when the Vdbe is finalized so it still should be obtained |
| 124 | ** from a single sqliteMalloc(). But no copy is made and the calling |
| 125 | ** function should *not* try to free the KeyInfo. |
| 126 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 127 | #define P4_KEYINFO_HANDOFF (-16) |
| 128 | #define P4_KEYINFO_STATIC (-17) |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 129 | |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 130 | /* |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 131 | ** The Vdbe.aColName array contains 5n Mem structures, where n is the |
| 132 | ** number of columns of data returned by the statement. |
| 133 | */ |
| 134 | #define COLNAME_NAME 0 |
| 135 | #define COLNAME_DECLTYPE 1 |
| 136 | #define COLNAME_DATABASE 2 |
| 137 | #define COLNAME_TABLE 3 |
| 138 | #define COLNAME_COLUMN 4 |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 139 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 140 | # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */ |
| 141 | #else |
| 142 | # ifdef SQLITE_OMIT_DECLTYPE |
| 143 | # define COLNAME_N 1 /* Store only the name */ |
| 144 | # else |
| 145 | # define COLNAME_N 2 /* Store the name and decltype */ |
| 146 | # endif |
| 147 | #endif |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 148 | |
| 149 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 150 | ** The following macro converts a relative address in the p2 field |
| 151 | ** of a VdbeOp structure into a negative number so that |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 152 | ** sqlite3VdbeAddOpList() knows that the address is relative. Calling |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 153 | ** the macro again restores the address. |
| 154 | */ |
| 155 | #define ADDR(X) (-1-(X)) |
| 156 | |
| 157 | /* |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 158 | ** The makefile scans the vdbe.c source file and creates the "opcodes.h" |
| 159 | ** header file that defines a number for each opcode used by the VDBE. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 160 | */ |
drh | 8f619cc | 2002-09-08 00:04:50 +0000 | [diff] [blame] | 161 | #include "opcodes.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | ** Prototypes for the VDBE interface. See comments on the implementation |
| 165 | ** for a description of what each of these routines does. |
| 166 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 167 | Vdbe *sqlite3VdbeCreate(sqlite3*); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 168 | int sqlite3VdbeAddOp0(Vdbe*,int); |
| 169 | int sqlite3VdbeAddOp1(Vdbe*,int,int); |
| 170 | int sqlite3VdbeAddOp2(Vdbe*,int,int,int); |
| 171 | int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); |
| 172 | int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 173 | int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 174 | int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp); |
| 175 | void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1); |
| 176 | void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2); |
danielk1977 | 1f4aa33 | 2008-01-03 09:51:55 +0000 | [diff] [blame] | 177 | void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 178 | void sqlite3VdbeChangeP5(Vdbe*, u8 P5); |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 179 | void sqlite3VdbeJumpHere(Vdbe*, int addr); |
drh | f887540 | 2006-03-17 13:56:34 +0000 | [diff] [blame] | 180 | void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 181 | void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 182 | void sqlite3VdbeUsesBtree(Vdbe*, int); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 183 | VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); |
| 184 | int sqlite3VdbeMakeLabel(Vdbe*); |
drh | 4611d92 | 2010-02-25 14:47:01 +0000 | [diff] [blame] | 185 | void sqlite3VdbeRunOnlyOnce(Vdbe*); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 186 | void sqlite3VdbeDelete(Vdbe*); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 187 | void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 188 | void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int); |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 189 | int sqlite3VdbeFinalize(Vdbe*); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 190 | void sqlite3VdbeResolveLabel(Vdbe*, int); |
| 191 | int sqlite3VdbeCurrentAddr(Vdbe*); |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 192 | #ifdef SQLITE_DEBUG |
dan | f367721 | 2009-09-10 16:14:50 +0000 | [diff] [blame] | 193 | int sqlite3VdbeAssertMayAbort(Vdbe *, int); |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 194 | void sqlite3VdbeTrace(Vdbe*,FILE*); |
| 195 | #endif |
drh | 3c23a88 | 2007-01-09 14:01:13 +0000 | [diff] [blame] | 196 | void sqlite3VdbeResetStepResult(Vdbe*); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 197 | int sqlite3VdbeReset(Vdbe*); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 198 | void sqlite3VdbeSetNumCols(Vdbe*,int); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 199 | int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*)); |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 200 | void sqlite3VdbeCountChanges(Vdbe*); |
danielk1977 | aee18ef | 2005-03-09 12:26:50 +0000 | [diff] [blame] | 201 | sqlite3 *sqlite3VdbeDb(Vdbe*); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 202 | void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int); |
drh | c515525 | 2007-01-08 21:07:17 +0000 | [diff] [blame] | 203 | void sqlite3VdbeSwap(Vdbe*,Vdbe*); |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 204 | VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 205 | sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8); |
dan | 1d2ce4f | 2009-10-19 18:11:09 +0000 | [diff] [blame] | 206 | void sqlite3VdbeSetVarmask(Vdbe*, int); |
drh | c7bc4fd | 2009-11-25 18:03:42 +0000 | [diff] [blame] | 207 | #ifndef SQLITE_OMIT_TRACE |
| 208 | char *sqlite3VdbeExpandSql(Vdbe*, const char*); |
| 209 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 210 | |
drh | 8c5d152 | 2009-04-10 00:56:28 +0000 | [diff] [blame] | 211 | UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 212 | void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 213 | int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 214 | |
dan | d19c933 | 2010-07-26 12:05:17 +0000 | [diff] [blame] | 215 | #ifndef SQLITE_OMIT_TRIGGER |
| 216 | void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); |
| 217 | #endif |
| 218 | |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 219 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 220 | #ifndef NDEBUG |
| 221 | void sqlite3VdbeComment(Vdbe*, const char*, ...); |
| 222 | # define VdbeComment(X) sqlite3VdbeComment X |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 223 | void sqlite3VdbeNoopComment(Vdbe*, const char*, ...); |
| 224 | # define VdbeNoopComment(X) sqlite3VdbeNoopComment X |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 225 | #else |
| 226 | # define VdbeComment(X) |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 227 | # define VdbeNoopComment(X) |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 228 | #endif |
| 229 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 230 | #endif |