drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2003 September 6 |
| 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 | ** This is the header file for information that is private to the |
| 13 | ** VDBE. This information used to all be at the top of the single |
| 14 | ** source code file "vdbe.c". When that file became too big (over |
| 15 | ** 6000 lines long) it was split up into several smaller files and |
| 16 | ** this header information was factored out. |
| 17 | */ |
| 18 | |
| 19 | /* |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame^] | 20 | ** When converting from the native format to the key format and back |
| 21 | ** again, in addition to changing the byte order we invert the high-order |
| 22 | ** bit of the most significant byte. This causes negative numbers to |
| 23 | ** sort before positive numbers in the memcmp() function. |
| 24 | */ |
| 25 | #define keyToInt(X) (sqliteVdbeByteSwap(X) ^ 0x80000000) |
| 26 | #define intToKey(X) (sqliteVdbeByteSwap((X) ^ 0x80000000)) |
| 27 | |
| 28 | /* |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 29 | ** The makefile scans this source file and creates the following |
| 30 | ** array of string constants which are the names of all VDBE opcodes. |
| 31 | ** This array is defined in a separate source code file named opcode.c |
| 32 | ** which is automatically generated by the makefile. |
| 33 | */ |
| 34 | extern char *sqliteOpcodeNames[]; |
| 35 | |
| 36 | /* |
| 37 | ** SQL is translated into a sequence of instructions to be |
| 38 | ** executed by a virtual machine. Each instruction is an instance |
| 39 | ** of the following structure. |
| 40 | */ |
| 41 | typedef struct VdbeOp Op; |
| 42 | |
| 43 | /* |
| 44 | ** Boolean values |
| 45 | */ |
| 46 | typedef unsigned char Bool; |
| 47 | |
| 48 | /* |
| 49 | ** A cursor is a pointer into a single BTree within a database file. |
| 50 | ** The cursor can seek to a BTree entry with a particular key, or |
| 51 | ** loop over all entries of the Btree. You can also insert new BTree |
| 52 | ** entries or retrieve the key or data from the entry that the cursor |
| 53 | ** is currently pointing to. |
| 54 | ** |
| 55 | ** Every cursor that the virtual machine has open is represented by an |
| 56 | ** instance of the following structure. |
| 57 | ** |
| 58 | ** If the Cursor.isTriggerRow flag is set it means that this cursor is |
| 59 | ** really a single row that represents the NEW or OLD pseudo-table of |
| 60 | ** a row trigger. The data for the row is stored in Cursor.pData and |
| 61 | ** the rowid is in Cursor.iKey. |
| 62 | */ |
| 63 | struct Cursor { |
| 64 | BtCursor *pCursor; /* The cursor structure of the backend */ |
| 65 | int lastRecno; /* Last recno from a Next or NextIdx operation */ |
| 66 | int nextRowid; /* Next rowid returned by OP_NewRowid */ |
| 67 | Bool recnoIsValid; /* True if lastRecno is valid */ |
| 68 | Bool keyAsData; /* The OP_Column command works on key instead of data */ |
| 69 | Bool atFirst; /* True if pointing to first entry */ |
| 70 | Bool useRandomRowid; /* Generate new record numbers semi-randomly */ |
| 71 | Bool nullRow; /* True if pointing to a row with no data */ |
| 72 | Bool nextRowidValid; /* True if the nextRowid field is valid */ |
| 73 | Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */ |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame^] | 74 | Bool deferredMoveto; /* A call to sqliteBtreeMoveto() is needed */ |
| 75 | int movetoTarget; /* Argument to the deferred sqliteBtreeMoveto() */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 76 | Btree *pBt; /* Separate file holding temporary table */ |
| 77 | int nData; /* Number of bytes in pData */ |
| 78 | char *pData; /* Data for a NEW or OLD pseudo-table */ |
| 79 | int iKey; /* Key for the NEW or OLD pseudo-table row */ |
| 80 | }; |
| 81 | typedef struct Cursor Cursor; |
| 82 | |
| 83 | /* |
| 84 | ** A sorter builds a list of elements to be sorted. Each element of |
| 85 | ** the list is an instance of the following structure. |
| 86 | */ |
| 87 | typedef struct Sorter Sorter; |
| 88 | struct Sorter { |
| 89 | int nKey; /* Number of bytes in the key */ |
| 90 | char *zKey; /* The key by which we will sort */ |
| 91 | int nData; /* Number of bytes in the data */ |
| 92 | char *pData; /* The data associated with this key */ |
| 93 | Sorter *pNext; /* Next in the list */ |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | ** Number of buckets used for merge-sort. |
| 98 | */ |
| 99 | #define NSORT 30 |
| 100 | |
| 101 | /* |
| 102 | ** Number of bytes of string storage space available to each stack |
| 103 | ** layer without having to malloc. NBFS is short for Number of Bytes |
| 104 | ** For Strings. |
| 105 | */ |
| 106 | #define NBFS 32 |
| 107 | |
| 108 | /* |
| 109 | ** A single level of the stack is an instance of the following |
| 110 | ** structure. Except, string values are stored on a separate |
| 111 | ** list of of pointers to character. The reason for storing |
| 112 | ** strings separately is so that they can be easily passed |
| 113 | ** to the callback function. |
| 114 | */ |
| 115 | struct Stack { |
| 116 | int i; /* Integer value */ |
| 117 | int n; /* Number of characters in string value, including '\0' */ |
| 118 | int flags; /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */ |
| 119 | double r; /* Real value */ |
| 120 | char z[NBFS]; /* Space for short strings */ |
| 121 | }; |
| 122 | typedef struct Stack Stack; |
| 123 | |
| 124 | /* |
| 125 | ** Memory cells use the same structure as the stack except that space |
| 126 | ** for an arbitrary string is added. |
| 127 | */ |
| 128 | struct Mem { |
| 129 | Stack s; /* All values of the memory cell besides string */ |
| 130 | char *z; /* String value for this memory cell */ |
| 131 | }; |
| 132 | typedef struct Mem Mem; |
| 133 | |
| 134 | /* |
| 135 | ** Allowed values for Stack.flags |
| 136 | */ |
| 137 | #define STK_Null 0x0001 /* Value is NULL */ |
| 138 | #define STK_Str 0x0002 /* Value is a string */ |
| 139 | #define STK_Int 0x0004 /* Value is an integer */ |
| 140 | #define STK_Real 0x0008 /* Value is a real number */ |
| 141 | #define STK_Dyn 0x0010 /* Need to call sqliteFree() on zStack[] */ |
| 142 | #define STK_Static 0x0020 /* zStack[] points to a static string */ |
| 143 | #define STK_Ephem 0x0040 /* zStack[] points to an ephemeral string */ |
| 144 | |
| 145 | /* The following STK_ value appears only in AggElem.aMem.s.flag fields. |
| 146 | ** It indicates that the corresponding AggElem.aMem.z points to a |
| 147 | ** aggregate function context that needs to be finalized. |
| 148 | */ |
| 149 | #define STK_AggCtx 0x0040 /* zStack[] points to an agg function context */ |
| 150 | |
| 151 | /* |
| 152 | ** The "context" argument for a installable function. A pointer to an |
| 153 | ** instance of this structure is the first argument to the routines used |
| 154 | ** implement the SQL functions. |
| 155 | ** |
| 156 | ** There is a typedef for this structure in sqlite.h. So all routines, |
| 157 | ** even the public interface to SQLite, can use a pointer to this structure. |
| 158 | ** But this file is the only place where the internal details of this |
| 159 | ** structure are known. |
| 160 | ** |
| 161 | ** This structure is defined inside of vdbe.c because it uses substructures |
| 162 | ** (Stack) which are only defined there. |
| 163 | */ |
| 164 | struct sqlite_func { |
| 165 | FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */ |
| 166 | Stack s; /* Small strings, ints, and double values go here */ |
| 167 | char *z; /* Space for holding dynamic string results */ |
| 168 | void *pAgg; /* Aggregate context */ |
| 169 | u8 isError; /* Set to true for an error */ |
| 170 | u8 isStep; /* Current in the step function */ |
| 171 | int cnt; /* Number of times that the step function has been called */ |
| 172 | }; |
| 173 | |
| 174 | /* |
| 175 | ** An Agg structure describes an Aggregator. Each Agg consists of |
| 176 | ** zero or more Aggregator elements (AggElem). Each AggElem contains |
| 177 | ** a key and one or more values. The values are used in processing |
| 178 | ** aggregate functions in a SELECT. The key is used to implement |
| 179 | ** the GROUP BY clause of a select. |
| 180 | */ |
| 181 | typedef struct Agg Agg; |
| 182 | typedef struct AggElem AggElem; |
| 183 | struct Agg { |
| 184 | int nMem; /* Number of values stored in each AggElem */ |
| 185 | AggElem *pCurrent; /* The AggElem currently in focus */ |
| 186 | HashElem *pSearch; /* The hash element for pCurrent */ |
| 187 | Hash hash; /* Hash table of all aggregate elements */ |
| 188 | FuncDef **apFunc; /* Information about aggregate functions */ |
| 189 | }; |
| 190 | struct AggElem { |
| 191 | char *zKey; /* The key to this AggElem */ |
| 192 | int nKey; /* Number of bytes in the key, including '\0' at end */ |
| 193 | Mem aMem[1]; /* The values for this AggElem */ |
| 194 | }; |
| 195 | |
| 196 | /* |
| 197 | ** A Set structure is used for quick testing to see if a value |
| 198 | ** is part of a small set. Sets are used to implement code like |
| 199 | ** this: |
| 200 | ** x.y IN ('hi','hoo','hum') |
| 201 | */ |
| 202 | typedef struct Set Set; |
| 203 | struct Set { |
| 204 | Hash hash; /* A set is just a hash table */ |
| 205 | HashElem *prev; /* Previously accessed hash elemen */ |
| 206 | }; |
| 207 | |
| 208 | /* |
| 209 | ** A Keylist is a bunch of keys into a table. The keylist can |
| 210 | ** grow without bound. The keylist stores the ROWIDs of database |
| 211 | ** records that need to be deleted or updated. |
| 212 | */ |
| 213 | typedef struct Keylist Keylist; |
| 214 | struct Keylist { |
| 215 | int nKey; /* Number of slots in aKey[] */ |
| 216 | int nUsed; /* Next unwritten slot in aKey[] */ |
| 217 | int nRead; /* Next unread slot in aKey[] */ |
| 218 | Keylist *pNext; /* Next block of keys */ |
| 219 | int aKey[1]; /* One or more keys. Extra space allocated as needed */ |
| 220 | }; |
| 221 | |
| 222 | /* |
| 223 | ** An instance of the virtual machine. This structure contains the complete |
| 224 | ** state of the virtual machine. |
| 225 | ** |
| 226 | ** The "sqlite_vm" structure pointer that is returned by sqlite_compile() |
| 227 | ** is really a pointer to an instance of this structure. |
| 228 | */ |
| 229 | struct Vdbe { |
| 230 | sqlite *db; /* The whole database */ |
| 231 | Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */ |
| 232 | FILE *trace; /* Write an execution trace here, if not NULL */ |
| 233 | int nOp; /* Number of instructions in the program */ |
| 234 | int nOpAlloc; /* Number of slots allocated for aOp[] */ |
| 235 | Op *aOp; /* Space to hold the virtual machine's program */ |
| 236 | int nLabel; /* Number of labels used */ |
| 237 | int nLabelAlloc; /* Number of slots allocated in aLabel[] */ |
| 238 | int *aLabel; /* Space to hold the labels */ |
| 239 | int tos; /* Index of top of stack */ |
| 240 | Stack *aStack; /* The operand stack, except string values */ |
| 241 | char **zStack; /* Text or binary values of the stack */ |
| 242 | char **azColName; /* Becomes the 4th parameter to callbacks */ |
| 243 | int nCursor; /* Number of slots in aCsr[] */ |
| 244 | Cursor *aCsr; /* One element of this array for each open cursor */ |
| 245 | Sorter *pSort; /* A linked list of objects to be sorted */ |
| 246 | FILE *pFile; /* At most one open file handler */ |
| 247 | int nField; /* Number of file fields */ |
| 248 | char **azField; /* Data for each file field */ |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 249 | int nVar; /* Number of entries in azVariable[] */ |
| 250 | char **azVar; /* Values for the OP_Variable opcode */ |
| 251 | int *anVar; /* Length of each value in azVariable[] */ |
| 252 | u8 *abVar; /* TRUE if azVariable[i] needs to be sqliteFree()ed */ |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 253 | char *zLine; /* A single line from the input file */ |
| 254 | int nLineAlloc; /* Number of spaces allocated for zLine */ |
| 255 | int magic; /* Magic number for sanity checking */ |
| 256 | int nMem; /* Number of memory locations currently allocated */ |
| 257 | Mem *aMem; /* The memory locations */ |
| 258 | Agg agg; /* Aggregate information */ |
| 259 | int nSet; /* Number of sets allocated */ |
| 260 | Set *aSet; /* An array of sets */ |
| 261 | int nCallback; /* Number of callbacks invoked so far */ |
| 262 | Keylist *pList; /* A list of ROWIDs */ |
| 263 | int keylistStackDepth; /* The size of the "keylist" stack */ |
| 264 | Keylist **keylistStack; /* The stack used by opcodes ListPush & ListPop */ |
| 265 | int pc; /* The program counter */ |
| 266 | int rc; /* Value to return */ |
| 267 | unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */ |
| 268 | int errorAction; /* Recovery action to do in case of an error */ |
| 269 | int undoTransOnError; /* If error, either ROLLBACK or COMMIT */ |
| 270 | int inTempTrans; /* True if temp database is transactioned */ |
| 271 | int returnStack[100]; /* Return address stack for OP_Gosub & OP_Return */ |
| 272 | int returnDepth; /* Next unused element in returnStack[] */ |
| 273 | int nResColumn; /* Number of columns in one row of the result set */ |
| 274 | char **azResColumn; /* Values for one row of result */ |
| 275 | int (*xCallback)(void*,int,char**,char**); /* Callback for SELECT results */ |
| 276 | void *pCbArg; /* First argument to xCallback() */ |
| 277 | int popStack; /* Pop the stack this much on entry to VdbeExec() */ |
| 278 | char *zErrMsg; /* Error message written here */ |
| 279 | u8 explain; /* True if EXPLAIN present on SQL command */ |
| 280 | }; |
| 281 | |
| 282 | /* |
| 283 | ** The following are allowed values for Vdbe.magic |
| 284 | */ |
| 285 | #define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */ |
| 286 | #define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */ |
| 287 | #define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */ |
| 288 | #define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */ |
| 289 | |
| 290 | /* |
| 291 | ** Here is a macro to handle the common case of popping the stack |
| 292 | ** once. This macro only works from within the sqliteVdbeExec() |
| 293 | ** function. |
| 294 | */ |
| 295 | #define POPSTACK \ |
| 296 | assert(p->tos>=0); \ |
| 297 | if( aStack[p->tos].flags & STK_Dyn ) sqliteFree(zStack[p->tos]); \ |
| 298 | p->tos--; |
| 299 | |
| 300 | /* |
| 301 | ** Function prototypes |
| 302 | */ |
| 303 | void sqliteVdbeCleanupCursor(Cursor*); |
| 304 | void sqliteVdbeSorterReset(Vdbe*); |
| 305 | void sqliteVdbeAggReset(Agg*); |
| 306 | void sqliteVdbeKeylistFree(Keylist*); |
| 307 | void sqliteVdbePopStack(Vdbe*,int); |
drh | a11846b | 2004-01-07 18:52:56 +0000 | [diff] [blame^] | 308 | int sqliteVdbeCursorMoveto(Cursor*); |
| 309 | int sqliteVdbeByteSwap(int); |
drh | 9a32464 | 2003-09-06 20:12:01 +0000 | [diff] [blame] | 310 | #if !defined(NDEBUG) || defined(VDBE_PROFILE) |
| 311 | void sqliteVdbePrintOp(FILE*, int, Op*); |
| 312 | #endif |