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. 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. 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 /* 30 ** The names of the following types declared in vdbeInt.h are required 31 ** for the VdbeOp definition. 32 */ 33 typedef struct Mem Mem; 34 typedef struct SubProgram SubProgram; 35 36 /* 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: 40 */ 41 struct VdbeOp { 42 u8 opcode; /* What operation to perform */ 43 signed char p4type; /* One of the P4_xxx constants for p4 */ 44 u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */ 45 u8 p5; /* Fifth parameter is an unsigned character */ 46 int p1; /* First operand */ 47 int p2; /* Second parameter (often the jump destination) */ 48 int p3; /* The third parameter */ 49 union p4union { /* fourth parameter */ 50 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 */ 56 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */ 57 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */ 58 Mem *pMem; /* Used when p4type is P4_MEM */ 59 VTable *pVtab; /* Used when p4type is P4_VTAB */ 60 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ 61 int *ai; /* Used when p4type is P4_INTARRAY */ 62 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ 63 int (*xAdvance)(BtCursor *, int *); 64 } p4; 65 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 66 char *zComment; /* Comment to improve readability */ 67 #endif 68 #ifdef VDBE_PROFILE 69 u32 cnt; /* Number of times this instruction was executed */ 70 u64 cycles; /* Total time spent executing this instruction */ 71 #endif 72 #ifdef SQLITE_VDBE_COVERAGE 73 int iSrcLine; /* Source-code line that generated this opcode */ 74 #endif 75 }; 76 typedef struct VdbeOp VdbeOp; 77 78 79 /* 80 ** A sub-routine used to implement a trigger program. 81 */ 82 struct 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 */ 87 int nOnce; /* Number of OP_Once instructions */ 88 void *token; /* id that may be used to recursive triggers */ 89 SubProgram *pNext; /* Next sub-program already visited */ 90 }; 91 92 /* 93 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because 94 ** it takes up less space. 95 */ 96 struct VdbeOpList { 97 u8 opcode; /* What operation to perform */ 98 signed char p1; /* First operand */ 99 signed char p2; /* Second parameter (often the jump destination) */ 100 signed char p3; /* Third parameter */ 101 }; 102 typedef struct VdbeOpList VdbeOpList; 103 104 /* 105 ** Allowed values of VdbeOp.p4type 106 */ 107 #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 */ 113 #define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */ 114 #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */ 115 #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 */ 120 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ 121 #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ 122 #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */ 123 #define P4_FUNCCTX (-20) /* P4 is a pointer to an sqlite3_context object */ 124 125 /* 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 131 /* 132 ** 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 140 #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 149 150 /* 151 ** The following macro converts a relative address in the p2 field 152 ** of a VdbeOp structure into a negative number so that 153 ** sqlite3VdbeAddOpList() knows that the address is relative. Calling 154 ** the macro again restores the address. 155 */ 156 #define ADDR(X) (-1-(X)) 157 158 /* 159 ** 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. 161 */ 162 #include "opcodes.h" 163 164 /* 165 ** Prototypes for the VDBE interface. See comments on the implementation 166 ** for a description of what each of these routines does. 167 */ 168 Vdbe *sqlite3VdbeCreate(Parse*); 169 int sqlite3VdbeAddOp0(Vdbe*,int); 170 int sqlite3VdbeAddOp1(Vdbe*,int,int); 171 int sqlite3VdbeAddOp2(Vdbe*,int,int,int); 172 int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); 173 int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); 174 int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int); 175 int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); 176 int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno); 177 void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*); 178 void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1); 179 void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2); 180 void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3); 181 void sqlite3VdbeChangeP5(Vdbe*, u8 P5); 182 void sqlite3VdbeJumpHere(Vdbe*, int addr); 183 void sqlite3VdbeChangeToNoop(Vdbe*, int addr); 184 int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op); 185 void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); 186 void sqlite3VdbeSetP4KeyInfo(Parse*, Index*); 187 void sqlite3VdbeUsesBtree(Vdbe*, int); 188 VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); 189 int sqlite3VdbeMakeLabel(Vdbe*); 190 void sqlite3VdbeRunOnlyOnce(Vdbe*); 191 void sqlite3VdbeDelete(Vdbe*); 192 void sqlite3VdbeClearObject(sqlite3*,Vdbe*); 193 void sqlite3VdbeMakeReady(Vdbe*,Parse*); 194 int sqlite3VdbeFinalize(Vdbe*); 195 void sqlite3VdbeResolveLabel(Vdbe*, int); 196 int sqlite3VdbeCurrentAddr(Vdbe*); 197 #ifdef SQLITE_DEBUG 198 int sqlite3VdbeAssertMayAbort(Vdbe *, int); 199 #endif 200 void sqlite3VdbeResetStepResult(Vdbe*); 201 void sqlite3VdbeRewind(Vdbe*); 202 int sqlite3VdbeReset(Vdbe*); 203 void sqlite3VdbeSetNumCols(Vdbe*,int); 204 int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*)); 205 void sqlite3VdbeCountChanges(Vdbe*); 206 sqlite3 *sqlite3VdbeDb(Vdbe*); 207 void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int); 208 void sqlite3VdbeSwap(Vdbe*,Vdbe*); 209 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); 210 sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8); 211 void sqlite3VdbeSetVarmask(Vdbe*, int); 212 #ifndef SQLITE_OMIT_TRACE 213 char *sqlite3VdbeExpandSql(Vdbe*, const char*); 214 #endif 215 int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); 216 217 void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*); 218 int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); 219 int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int); 220 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **); 221 222 typedef int (*RecordCompare)(int,const void*,UnpackedRecord*); 223 RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); 224 225 #ifndef SQLITE_OMIT_TRIGGER 226 void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); 227 #endif 228 229 /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on 230 ** each VDBE opcode. 231 ** 232 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op 233 ** comments in VDBE programs that show key decision points in the code 234 ** generator. 235 */ 236 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 237 void sqlite3VdbeComment(Vdbe*, const char*, ...); 238 # define VdbeComment(X) sqlite3VdbeComment X 239 void sqlite3VdbeNoopComment(Vdbe*, const char*, ...); 240 # define VdbeNoopComment(X) sqlite3VdbeNoopComment X 241 # ifdef SQLITE_ENABLE_MODULE_COMMENTS 242 # define VdbeModuleComment(X) sqlite3VdbeNoopComment X 243 # else 244 # define VdbeModuleComment(X) 245 # endif 246 #else 247 # define VdbeComment(X) 248 # define VdbeNoopComment(X) 249 # define VdbeModuleComment(X) 250 #endif 251 252 /* 253 ** The VdbeCoverage macros are used to set a coverage testing point 254 ** for VDBE branch instructions. The coverage testing points are line 255 ** numbers in the sqlite3.c source file. VDBE branch coverage testing 256 ** only works with an amalagmation build. That's ok since a VDBE branch 257 ** coverage build designed for testing the test suite only. No application 258 ** should ever ship with VDBE branch coverage measuring turned on. 259 ** 260 ** VdbeCoverage(v) // Mark the previously coded instruction 261 ** // as a branch 262 ** 263 ** VdbeCoverageIf(v, conditional) // Mark previous if conditional true 264 ** 265 ** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken 266 ** 267 ** VdbeCoverageNeverTaken(v) // Previous branch is never taken 268 ** 269 ** Every VDBE branch operation must be tagged with one of the macros above. 270 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and 271 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch() 272 ** routine in vdbe.c, alerting the developer to the missed tag. 273 */ 274 #ifdef SQLITE_VDBE_COVERAGE 275 void sqlite3VdbeSetLineNumber(Vdbe*,int); 276 # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__) 277 # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__) 278 # define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2); 279 # define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1); 280 # define VDBE_OFFSET_LINENO(x) (__LINE__+x) 281 #else 282 # define VdbeCoverage(v) 283 # define VdbeCoverageIf(v,x) 284 # define VdbeCoverageAlwaysTaken(v) 285 # define VdbeCoverageNeverTaken(v) 286 # define VDBE_OFFSET_LINENO(x) 0 287 #endif 288 289 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 290 void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*); 291 #else 292 # define sqlite3VdbeScanStatus(a,b,c,d,e) 293 #endif 294 295 #endif 296