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