blob: daa25052d4eabd5a8c166c50eac34bb062a37cac [file] [log] [blame]
drh9a324642003-09-06 20:12:01 +00001/*
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/*
drha11846b2004-01-07 18:52:56 +000020** 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/*
drh9a324642003-09-06 20:12:01 +000029** 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*/
34extern 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*/
41typedef struct VdbeOp Op;
42
43/*
44** Boolean values
45*/
46typedef 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*/
63struct 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 */
drha11846b2004-01-07 18:52:56 +000074 Bool deferredMoveto; /* A call to sqliteBtreeMoveto() is needed */
75 int movetoTarget; /* Argument to the deferred sqliteBtreeMoveto() */
drh9a324642003-09-06 20:12:01 +000076 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};
81typedef 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*/
87typedef struct Sorter Sorter;
88struct 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/*
drh00706be2004-01-30 14:49:16 +0000109** A single level of the stack or a single memory cell
110** is an instance of the following structure.
drh9a324642003-09-06 20:12:01 +0000111*/
112struct Mem {
drh00706be2004-01-30 14:49:16 +0000113 int i; /* Integer value */
114 int n; /* Number of characters in string value, including '\0' */
115 int flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
116 double r; /* Real value */
117 char *z; /* String value */
118 char zShort[NBFS]; /* Space for short strings */
drh9a324642003-09-06 20:12:01 +0000119};
120typedef struct Mem Mem;
121
122/*
drh00706be2004-01-30 14:49:16 +0000123** Allowed values for Mem.flags
drh9a324642003-09-06 20:12:01 +0000124*/
drh00706be2004-01-30 14:49:16 +0000125#define MEM_Null 0x0001 /* Value is NULL */
126#define MEM_Str 0x0002 /* Value is a string */
127#define MEM_Int 0x0004 /* Value is an integer */
128#define MEM_Real 0x0008 /* Value is a real number */
129#define MEM_Dyn 0x0010 /* Need to call sqliteFree() on Mem.z */
130#define MEM_Static 0x0020 /* Mem.z points to a static string */
131#define MEM_Ephem 0x0040 /* Mem.z points to an ephemeral string */
drh9a324642003-09-06 20:12:01 +0000132
drh00706be2004-01-30 14:49:16 +0000133/* The following MEM_ value appears only in AggElem.aMem.s.flag fields.
drh9a324642003-09-06 20:12:01 +0000134** It indicates that the corresponding AggElem.aMem.z points to a
135** aggregate function context that needs to be finalized.
136*/
drh00706be2004-01-30 14:49:16 +0000137#define MEM_AggCtx 0x0040 /* Mem.z points to an agg function context */
drh9a324642003-09-06 20:12:01 +0000138
139/*
140** The "context" argument for a installable function. A pointer to an
141** instance of this structure is the first argument to the routines used
142** implement the SQL functions.
143**
144** There is a typedef for this structure in sqlite.h. So all routines,
145** even the public interface to SQLite, can use a pointer to this structure.
146** But this file is the only place where the internal details of this
147** structure are known.
148**
149** This structure is defined inside of vdbe.c because it uses substructures
drh00706be2004-01-30 14:49:16 +0000150** (Mem) which are only defined there.
drh9a324642003-09-06 20:12:01 +0000151*/
152struct sqlite_func {
153 FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
drh00706be2004-01-30 14:49:16 +0000154 Mem s; /* The return value is stored here */
drh9a324642003-09-06 20:12:01 +0000155 void *pAgg; /* Aggregate context */
156 u8 isError; /* Set to true for an error */
157 u8 isStep; /* Current in the step function */
158 int cnt; /* Number of times that the step function has been called */
159};
160
161/*
162** An Agg structure describes an Aggregator. Each Agg consists of
163** zero or more Aggregator elements (AggElem). Each AggElem contains
164** a key and one or more values. The values are used in processing
165** aggregate functions in a SELECT. The key is used to implement
166** the GROUP BY clause of a select.
167*/
168typedef struct Agg Agg;
169typedef struct AggElem AggElem;
170struct Agg {
171 int nMem; /* Number of values stored in each AggElem */
172 AggElem *pCurrent; /* The AggElem currently in focus */
173 HashElem *pSearch; /* The hash element for pCurrent */
174 Hash hash; /* Hash table of all aggregate elements */
175 FuncDef **apFunc; /* Information about aggregate functions */
176};
177struct AggElem {
178 char *zKey; /* The key to this AggElem */
179 int nKey; /* Number of bytes in the key, including '\0' at end */
180 Mem aMem[1]; /* The values for this AggElem */
181};
182
183/*
184** A Set structure is used for quick testing to see if a value
185** is part of a small set. Sets are used to implement code like
186** this:
187** x.y IN ('hi','hoo','hum')
188*/
189typedef struct Set Set;
190struct Set {
191 Hash hash; /* A set is just a hash table */
192 HashElem *prev; /* Previously accessed hash elemen */
193};
194
195/*
196** A Keylist is a bunch of keys into a table. The keylist can
197** grow without bound. The keylist stores the ROWIDs of database
198** records that need to be deleted or updated.
199*/
200typedef struct Keylist Keylist;
201struct Keylist {
202 int nKey; /* Number of slots in aKey[] */
203 int nUsed; /* Next unwritten slot in aKey[] */
204 int nRead; /* Next unread slot in aKey[] */
205 Keylist *pNext; /* Next block of keys */
206 int aKey[1]; /* One or more keys. Extra space allocated as needed */
207};
208
209/*
210** An instance of the virtual machine. This structure contains the complete
211** state of the virtual machine.
212**
213** The "sqlite_vm" structure pointer that is returned by sqlite_compile()
214** is really a pointer to an instance of this structure.
215*/
216struct Vdbe {
217 sqlite *db; /* The whole database */
218 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
219 FILE *trace; /* Write an execution trace here, if not NULL */
220 int nOp; /* Number of instructions in the program */
221 int nOpAlloc; /* Number of slots allocated for aOp[] */
222 Op *aOp; /* Space to hold the virtual machine's program */
223 int nLabel; /* Number of labels used */
224 int nLabelAlloc; /* Number of slots allocated in aLabel[] */
225 int *aLabel; /* Space to hold the labels */
226 int tos; /* Index of top of stack */
drh00706be2004-01-30 14:49:16 +0000227 Mem *aStack; /* The operand stack, except string values */
228 char **zArgv; /* Text values used by the callback */
drh9a324642003-09-06 20:12:01 +0000229 char **azColName; /* Becomes the 4th parameter to callbacks */
230 int nCursor; /* Number of slots in aCsr[] */
231 Cursor *aCsr; /* One element of this array for each open cursor */
232 Sorter *pSort; /* A linked list of objects to be sorted */
233 FILE *pFile; /* At most one open file handler */
234 int nField; /* Number of file fields */
235 char **azField; /* Data for each file field */
drh7c972de2003-09-06 22:18:07 +0000236 int nVar; /* Number of entries in azVariable[] */
237 char **azVar; /* Values for the OP_Variable opcode */
238 int *anVar; /* Length of each value in azVariable[] */
239 u8 *abVar; /* TRUE if azVariable[i] needs to be sqliteFree()ed */
drh9a324642003-09-06 20:12:01 +0000240 char *zLine; /* A single line from the input file */
241 int nLineAlloc; /* Number of spaces allocated for zLine */
242 int magic; /* Magic number for sanity checking */
243 int nMem; /* Number of memory locations currently allocated */
244 Mem *aMem; /* The memory locations */
245 Agg agg; /* Aggregate information */
246 int nSet; /* Number of sets allocated */
247 Set *aSet; /* An array of sets */
248 int nCallback; /* Number of callbacks invoked so far */
249 Keylist *pList; /* A list of ROWIDs */
250 int keylistStackDepth; /* The size of the "keylist" stack */
251 Keylist **keylistStack; /* The stack used by opcodes ListPush & ListPop */
252 int pc; /* The program counter */
253 int rc; /* Value to return */
254 unsigned uniqueCnt; /* Used by OP_MakeRecord when P2!=0 */
255 int errorAction; /* Recovery action to do in case of an error */
256 int undoTransOnError; /* If error, either ROLLBACK or COMMIT */
257 int inTempTrans; /* True if temp database is transactioned */
258 int returnStack[100]; /* Return address stack for OP_Gosub & OP_Return */
259 int returnDepth; /* Next unused element in returnStack[] */
260 int nResColumn; /* Number of columns in one row of the result set */
261 char **azResColumn; /* Values for one row of result */
262 int (*xCallback)(void*,int,char**,char**); /* Callback for SELECT results */
263 void *pCbArg; /* First argument to xCallback() */
264 int popStack; /* Pop the stack this much on entry to VdbeExec() */
265 char *zErrMsg; /* Error message written here */
266 u8 explain; /* True if EXPLAIN present on SQL command */
267};
268
269/*
270** The following are allowed values for Vdbe.magic
271*/
272#define VDBE_MAGIC_INIT 0x26bceaa5 /* Building a VDBE program */
273#define VDBE_MAGIC_RUN 0xbdf20da3 /* VDBE is ready to execute */
274#define VDBE_MAGIC_HALT 0x519c2973 /* VDBE has completed execution */
275#define VDBE_MAGIC_DEAD 0xb606c3c8 /* The VDBE has been deallocated */
276
277/*
278** Here is a macro to handle the common case of popping the stack
279** once. This macro only works from within the sqliteVdbeExec()
280** function.
281*/
282#define POPSTACK \
283 assert(p->tos>=0); \
drh00706be2004-01-30 14:49:16 +0000284 if( aStack[p->tos].flags & MEM_Dyn ) sqliteFree(aStack[p->tos].z); \
drh9a324642003-09-06 20:12:01 +0000285 p->tos--;
286
287/*
288** Function prototypes
289*/
290void sqliteVdbeCleanupCursor(Cursor*);
291void sqliteVdbeSorterReset(Vdbe*);
292void sqliteVdbeAggReset(Agg*);
293void sqliteVdbeKeylistFree(Keylist*);
294void sqliteVdbePopStack(Vdbe*,int);
drha11846b2004-01-07 18:52:56 +0000295int sqliteVdbeCursorMoveto(Cursor*);
296int sqliteVdbeByteSwap(int);
drh9a324642003-09-06 20:12:01 +0000297#if !defined(NDEBUG) || defined(VDBE_PROFILE)
298void sqliteVdbePrintOp(FILE*, int, Op*);
299#endif