2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2003 September 6
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This is the header file for information that is private to the
2N/A** VDBE. This information used to all be at the top of the single
2N/A** source code file "vdbe.c". When that file became too big (over
2N/A** 6000 lines long) it was split up into several smaller files and
2N/A** this header information was factored out.
2N/A*/
2N/A
2N/A/*
2N/A** When converting from the native format to the key format and back
2N/A** again, in addition to changing the byte order we invert the high-order
2N/A** bit of the most significant byte. This causes negative numbers to
2N/A** sort before positive numbers in the memcmp() function.
2N/A*/
2N/A#define keyToInt(X) (sqliteVdbeByteSwap(X) ^ 0x80000000)
2N/A#define intToKey(X) (sqliteVdbeByteSwap((X) ^ 0x80000000))
2N/A
2N/A/*
2N/A** The makefile scans this source file and creates the following
2N/A** array of string constants which are the names of all VDBE opcodes.
2N/A** This array is defined in a separate source code file named opcode.c
2N/A** which is automatically generated by the makefile.
2N/A*/
2N/Aextern char *sqliteOpcodeNames[];
2N/A
2N/A/*
2N/A** SQL is translated into a sequence of instructions to be
2N/A** executed by a virtual machine. Each instruction is an instance
2N/A** of the following structure.
2N/A*/
2N/Atypedef struct VdbeOp Op;
2N/A
2N/A/*
2N/A** Boolean values
2N/A*/
2N/Atypedef unsigned char Bool;
2N/A
2N/A/*
2N/A** A cursor is a pointer into a single BTree within a database file.
2N/A** The cursor can seek to a BTree entry with a particular key, or
2N/A** loop over all entries of the Btree. You can also insert new BTree
2N/A** entries or retrieve the key or data from the entry that the cursor
2N/A** is currently pointing to.
2N/A**
2N/A** Every cursor that the virtual machine has open is represented by an
2N/A** instance of the following structure.
2N/A**
2N/A** If the Cursor.isTriggerRow flag is set it means that this cursor is
2N/A** really a single row that represents the NEW or OLD pseudo-table of
2N/A** a row trigger. The data for the row is stored in Cursor.pData and
2N/A** the rowid is in Cursor.iKey.
2N/A*/
2N/Astruct Cursor {
2N/A BtCursor *pCursor; /* The cursor structure of the backend */
2N/A int lastRecno; /* Last recno from a Next or NextIdx operation */
2N/A int nextRowid; /* Next rowid returned by OP_NewRowid */
2N/A Bool recnoIsValid; /* True if lastRecno is valid */
2N/A Bool keyAsData; /* The OP_Column command works on key instead of data */
2N/A Bool atFirst; /* True if pointing to first entry */
2N/A Bool useRandomRowid; /* Generate new record numbers semi-randomly */
2N/A Bool nullRow; /* True if pointing to a row with no data */
2N/A Bool nextRowidValid; /* True if the nextRowid field is valid */
2N/A Bool pseudoTable; /* This is a NEW or OLD pseudo-tables of a trigger */
2N/A Bool deferredMoveto; /* A call to sqliteBtreeMoveto() is needed */
2N/A int movetoTarget; /* Argument to the deferred sqliteBtreeMoveto() */
2N/A Btree *pBt; /* Separate file holding temporary table */
2N/A int nData; /* Number of bytes in pData */
2N/A char *pData; /* Data for a NEW or OLD pseudo-table */
2N/A int iKey; /* Key for the NEW or OLD pseudo-table row */
2N/A};
2N/Atypedef struct Cursor Cursor;
2N/A
2N/A/*
2N/A** A sorter builds a list of elements to be sorted. Each element of
2N/A** the list is an instance of the following structure.
2N/A*/
2N/Atypedef struct Sorter Sorter;
2N/Astruct Sorter {
2N/A int nKey; /* Number of bytes in the key */
2N/A char *zKey; /* The key by which we will sort */
2N/A int nData; /* Number of bytes in the data */
2N/A char *pData; /* The data associated with this key */
2N/A Sorter *pNext; /* Next in the list */
2N/A};
2N/A
2N/A/*
2N/A** Number of buckets used for merge-sort.
2N/A*/
2N/A#define NSORT 30
2N/A
2N/A/*
2N/A** Number of bytes of string storage space available to each stack
2N/A** layer without having to malloc. NBFS is short for Number of Bytes
2N/A** For Strings.
2N/A*/
2N/A#define NBFS 32
2N/A
2N/A/*
2N/A** A single level of the stack or a single memory cell
2N/A** is an instance of the following structure.
2N/A*/
2N/Astruct Mem {
2N/A int i; /* Integer value */
2N/A int n; /* Number of characters in string value, including '\0' */
2N/A int flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
2N/A double r; /* Real value */
2N/A char *z; /* String value */
2N/A char zShort[NBFS]; /* Space for short strings */
2N/A};
2N/Atypedef struct Mem Mem;
2N/A
2N/A/*
2N/A** Allowed values for Mem.flags
2N/A*/
2N/A#define MEM_Null 0x0001 /* Value is NULL */
2N/A#define MEM_Str 0x0002 /* Value is a string */
2N/A#define MEM_Int 0x0004 /* Value is an integer */
2N/A#define MEM_Real 0x0008 /* Value is a real number */
2N/A#define MEM_Dyn 0x0010 /* Need to call sqliteFree() on Mem.z */
2N/A#define MEM_Static 0x0020 /* Mem.z points to a static string */
2N/A#define MEM_Ephem 0x0040 /* Mem.z points to an ephemeral string */
2N/A#define MEM_Short 0x0080 /* Mem.z points to Mem.zShort */
2N/A
2N/A/* The following MEM_ value appears only in AggElem.aMem.s.flag fields.
2N/A** It indicates that the corresponding AggElem.aMem.z points to a
2N/A** aggregate function context that needs to be finalized.
2N/A*/
2N/A#define MEM_AggCtx 0x0100 /* Mem.z points to an agg function context */
2N/A
2N/A/*
2N/A** The "context" argument for a installable function. A pointer to an
2N/A** instance of this structure is the first argument to the routines used
2N/A** implement the SQL functions.
2N/A**
2N/A** There is a typedef for this structure in sqlite.h. So all routines,
2N/A** even the public interface to SQLite, can use a pointer to this structure.
2N/A** But this file is the only place where the internal details of this
2N/A** structure are known.
2N/A**
2N/A** This structure is defined inside of vdbe.c because it uses substructures
2N/A** (Mem) which are only defined there.
2N/A*/
2N/Astruct sqlite_func {
2N/A FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
2N/A Mem s; /* The return value is stored here */
2N/A void *pAgg; /* Aggregate context */
2N/A u8 isError; /* Set to true for an error */
2N/A u8 isStep; /* Current in the step function */
2N/A int cnt; /* Number of times that the step function has been called */
2N/A};
2N/A
2N/A/*
2N/A** An Agg structure describes an Aggregator. Each Agg consists of
2N/A** zero or more Aggregator elements (AggElem). Each AggElem contains
2N/A** a key and one or more values. The values are used in processing
2N/A** aggregate functions in a SELECT. The key is used to implement
2N/A** the GROUP BY clause of a select.
2N/A*/
2N/Atypedef struct Agg Agg;
2N/Atypedef struct AggElem AggElem;
2N/Astruct Agg {
2N/A int nMem; /* Number of values stored in each AggElem */
2N/A AggElem *pCurrent; /* The AggElem currently in focus */
2N/A HashElem *pSearch; /* The hash element for pCurrent */
2N/A Hash hash; /* Hash table of all aggregate elements */
2N/A FuncDef **apFunc; /* Information about aggregate functions */
2N/A};
2N/Astruct AggElem {
2N/A char *zKey; /* The key to this AggElem */
2N/A int nKey; /* Number of bytes in the key, including '\0' at end */
2N/A Mem aMem[1]; /* The values for this AggElem */
2N/A};
2N/A
2N/A/*
2N/A** A Set structure is used for quick testing to see if a value
2N/A** is part of a small set. Sets are used to implement code like
2N/A** this:
2N/A** x.y IN ('hi','hoo','hum')
2N/A*/
2N/Atypedef struct Set Set;
2N/Astruct Set {
2N/A Hash hash; /* A set is just a hash table */
2N/A HashElem *prev; /* Previously accessed hash elemen */
2N/A};
2N/A
2N/A/*
2N/A** A Keylist is a bunch of keys into a table. The keylist can
2N/A** grow without bound. The keylist stores the ROWIDs of database
2N/A** records that need to be deleted or updated.
2N/A*/
2N/Atypedef struct Keylist Keylist;
2N/Astruct Keylist {
2N/A int nKey; /* Number of slots in aKey[] */
2N/A int nUsed; /* Next unwritten slot in aKey[] */
2N/A int nRead; /* Next unread slot in aKey[] */
2N/A Keylist *pNext; /* Next block of keys */
2N/A int aKey[1]; /* One or more keys. Extra space allocated as needed */
2N/A};
2N/A
2N/A/*
2N/A** A Context stores the last insert rowid, the last statement change count,
2N/A** and the current statement change count (i.e. changes since last statement).
2N/A** Elements of Context structure type make up the ContextStack, which is
2N/A** updated by the ContextPush and ContextPop opcodes (used by triggers)
2N/A*/
2N/Atypedef struct Context Context;
2N/Astruct Context {
2N/A int lastRowid; /* Last insert rowid (from db->lastRowid) */
2N/A int lsChange; /* Last statement change count (from db->lsChange) */
2N/A int csChange; /* Current statement change count (from db->csChange) */
2N/A};
2N/A
2N/A/*
2N/A** An instance of the virtual machine. This structure contains the complete
2N/A** state of the virtual machine.
2N/A**
2N/A** The "sqlite_vm" structure pointer that is returned by sqlite_compile()
2N/A** is really a pointer to an instance of this structure.
2N/A*/
2N/Astruct Vdbe {
2N/A sqlite *db; /* The whole database */
2N/A Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
2N/A FILE *trace; /* Write an execution trace here, if not NULL */
2N/A int nOp; /* Number of instructions in the program */
2N/A int nOpAlloc; /* Number of slots allocated for aOp[] */
2N/A Op *aOp; /* Space to hold the virtual machine's program */
2N/A int nLabel; /* Number of labels used */
2N/A int nLabelAlloc; /* Number of slots allocated in aLabel[] */
2N/A int *aLabel; /* Space to hold the labels */
2N/A Mem *aStack; /* The operand stack, except string values */
2N/A Mem *pTos; /* Top entry in the operand stack */
2N/A char **zArgv; /* Text values used by the callback */
2N/A char **azColName; /* Becomes the 4th parameter to callbacks */
2N/A int nCursor; /* Number of slots in aCsr[] */
2N/A Cursor *aCsr; /* One element of this array for each open cursor */
2N/A Sorter *pSort; /* A linked list of objects to be sorted */
2N/A FILE *pFile; /* At most one open file handler */
2N/A int nField; /* Number of file fields */
2N/A char **azField; /* Data for each file field */
2N/A int nVar; /* Number of entries in azVariable[] */
2N/A char **azVar; /* Values for the OP_Variable opcode */
2N/A int *anVar; /* Length of each value in azVariable[] */
2N/A u8 *abVar; /* TRUE if azVariable[i] needs to be sqliteFree()ed */
2N/A char *zLine; /* A single line from the input file */
2N/A int nLineAlloc; /* Number of spaces allocated for zLine */
2N/A int magic; /* Magic number for sanity checking */
2N/A int nMem; /* Number of memory locations currently allocated */
2N/A Mem *aMem; /* The memory locations */
2N/A Agg agg; /* Aggregate information */
2N/A int nSet; /* Number of sets allocated */
2N/A Set *aSet; /* An array of sets */
2N/A int nCallback; /* Number of callbacks invoked so far */
2N/A Keylist *pList; /* A list of ROWIDs */
2N/A int keylistStackDepth; /* The size of the "keylist" stack */
2N/A Keylist **keylistStack; /* The stack used by opcodes ListPush & ListPop */
2N/A int contextStackDepth; /* The size of the "context" stack */
2N/A Context *contextStack; /* Stack used by opcodes ContextPush & ContextPop*/
2N/A int pc; /* The program counter */
2N/A int rc; /* Value to return */
2N/A unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */
2N/A int errorAction; /* Recovery action to do in case of an error */
2N/A int undoTransOnError; /* If error, either ROLLBACK or COMMIT */
2N/A int inTempTrans; /* True if temp database is transactioned */
2N/A int returnStack[100]; /* Return address stack for OP_Gosub & OP_Return */
2N/A int returnDepth; /* Next unused element in returnStack[] */
2N/A int nResColumn; /* Number of columns in one row of the result set */
2N/A char **azResColumn; /* Values for one row of result */
2N/A int popStack; /* Pop the stack this much on entry to VdbeExec() */
2N/A char *zErrMsg; /* Error message written here */
2N/A u8 explain; /* True if EXPLAIN present on SQL command */
2N/A};
2N/A
2N/A/*
2N/A** The following are allowed values for Vdbe.magic
2N/A*/
2N/A#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */
2N/A#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */
2N/A#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */
2N/A#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
2N/A
2N/A/*
2N/A** Function prototypes
2N/A*/
2N/Avoid sqliteVdbeCleanupCursor(Cursor*);
2N/Avoid sqliteVdbeSorterReset(Vdbe*);
2N/Avoid sqliteVdbeAggReset(Agg*);
2N/Avoid sqliteVdbeKeylistFree(Keylist*);
2N/Avoid sqliteVdbePopStack(Vdbe*,int);
2N/Aint sqliteVdbeCursorMoveto(Cursor*);
2N/Aint sqliteVdbeByteSwap(int);
2N/A#if !defined(NDEBUG) || defined(VDBE_PROFILE)
2N/Avoid sqliteVdbePrintOp(FILE*, int, Op*);
2N/A#endif