xref: /sqlite-3.40.0/src/vdbeaux.c (revision 85b623f2)
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 file contains code used for creating, destroying, and populating
13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
14 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
15 ** But that file was getting too big so this subroutines were split out.
16 */
17 #include "sqliteInt.h"
18 #include <ctype.h>
19 #include "vdbeInt.h"
20 
21 
22 
23 /*
24 ** When debugging the code generator in a symbolic debugger, one can
25 ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
26 ** as they are added to the instruction stream.
27 */
28 #ifdef SQLITE_DEBUG
29 int sqlite3_vdbe_addop_trace = 0;
30 #endif
31 
32 
33 /*
34 ** Create a new virtual database engine.
35 */
36 Vdbe *sqlite3VdbeCreate(sqlite3 *db){
37   Vdbe *p;
38   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
39   if( p==0 ) return 0;
40   p->db = db;
41   if( db->pVdbe ){
42     db->pVdbe->pPrev = p;
43   }
44   p->pNext = db->pVdbe;
45   p->pPrev = 0;
46   db->pVdbe = p;
47   p->magic = VDBE_MAGIC_INIT;
48   return p;
49 }
50 
51 /*
52 ** Remember the SQL string for a prepared statement.
53 */
54 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
55   if( p==0 ) return;
56   assert( p->zSql==0 );
57   p->zSql = sqlite3DbStrNDup(p->db, z, n);
58 }
59 
60 /*
61 ** Return the SQL associated with a prepared statement
62 */
63 const char *sqlite3_sql(sqlite3_stmt *pStmt){
64   return ((Vdbe *)pStmt)->zSql;
65 }
66 
67 /*
68 ** Swap all content between two VDBE structures.
69 */
70 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
71   Vdbe tmp, *pTmp;
72   char *zTmp;
73   int nTmp;
74   tmp = *pA;
75   *pA = *pB;
76   *pB = tmp;
77   pTmp = pA->pNext;
78   pA->pNext = pB->pNext;
79   pB->pNext = pTmp;
80   pTmp = pA->pPrev;
81   pA->pPrev = pB->pPrev;
82   pB->pPrev = pTmp;
83   zTmp = pA->zSql;
84   pA->zSql = pB->zSql;
85   pB->zSql = zTmp;
86   nTmp = pA->nSql;
87   pA->nSql = pB->nSql;
88   pB->nSql = nTmp;
89 }
90 
91 #ifdef SQLITE_DEBUG
92 /*
93 ** Turn tracing on or off
94 */
95 void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
96   p->trace = trace;
97 }
98 #endif
99 
100 /*
101 ** Resize the Vdbe.aOp array so that it contains at least N
102 ** elements.
103 **
104 ** If an out-of-memory error occurs while resizing the array,
105 ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
106 ** any opcodes already allocated can be correctly deallocated
107 ** along with the rest of the Vdbe).
108 */
109 static void resizeOpArray(Vdbe *p, int N){
110   VdbeOp *pNew;
111   int oldSize = p->nOpAlloc;
112   pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op));
113   if( pNew ){
114     p->nOpAlloc = N;
115     p->aOp = pNew;
116     if( N>oldSize ){
117       memset(&p->aOp[oldSize], 0, (N-oldSize)*sizeof(Op));
118     }
119   }
120 }
121 
122 /*
123 ** Add a new instruction to the list of instructions current in the
124 ** VDBE.  Return the address of the new instruction.
125 **
126 ** Parameters:
127 **
128 **    p               Pointer to the VDBE
129 **
130 **    op              The opcode for this instruction
131 **
132 **    p1, p2          First two of the three possible operands.
133 **
134 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
135 ** the sqlite3VdbeChangeP3() function to change the value of the P3
136 ** operand.
137 */
138 int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
139   int i;
140   VdbeOp *pOp;
141 
142   i = p->nOp;
143   assert( p->magic==VDBE_MAGIC_INIT );
144   if( p->nOpAlloc<=i ){
145     resizeOpArray(p, p->nOpAlloc*2 + 100);
146     if( p->db->mallocFailed ){
147       return 0;
148     }
149   }
150   p->nOp++;
151   pOp = &p->aOp[i];
152   pOp->opcode = op;
153   pOp->p1 = p1;
154   pOp->p2 = p2;
155   pOp->p3 = 0;
156   pOp->p3type = P3_NOTUSED;
157   p->expired = 0;
158 #ifdef SQLITE_DEBUG
159   if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
160 #endif
161   return i;
162 }
163 
164 /*
165 ** Add an opcode that includes the p3 value.
166 */
167 int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
168   int addr = sqlite3VdbeAddOp(p, op, p1, p2);
169   sqlite3VdbeChangeP3(p, addr, zP3, p3type);
170   return addr;
171 }
172 
173 /*
174 ** Create a new symbolic label for an instruction that has yet to be
175 ** coded.  The symbolic label is really just a negative number.  The
176 ** label can be used as the P2 value of an operation.  Later, when
177 ** the label is resolved to a specific address, the VDBE will scan
178 ** through its operation list and change all values of P2 which match
179 ** the label into the resolved address.
180 **
181 ** The VDBE knows that a P2 value is a label because labels are
182 ** always negative and P2 values are suppose to be non-negative.
183 ** Hence, a negative P2 value is a label that has yet to be resolved.
184 **
185 ** Zero is returned if a malloc() fails.
186 */
187 int sqlite3VdbeMakeLabel(Vdbe *p){
188   int i;
189   i = p->nLabel++;
190   assert( p->magic==VDBE_MAGIC_INIT );
191   if( i>=p->nLabelAlloc ){
192     p->nLabelAlloc = p->nLabelAlloc*2 + 10;
193     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
194                                     p->nLabelAlloc*sizeof(p->aLabel[0]));
195   }
196   if( p->aLabel ){
197     p->aLabel[i] = -1;
198   }
199   return -1-i;
200 }
201 
202 /*
203 ** Resolve label "x" to be the address of the next instruction to
204 ** be inserted.  The parameter "x" must have been obtained from
205 ** a prior call to sqlite3VdbeMakeLabel().
206 */
207 void sqlite3VdbeResolveLabel(Vdbe *p, int x){
208   int j = -1-x;
209   assert( p->magic==VDBE_MAGIC_INIT );
210   assert( j>=0 && j<p->nLabel );
211   if( p->aLabel ){
212     p->aLabel[j] = p->nOp;
213   }
214 }
215 
216 /*
217 ** Return non-zero if opcode 'op' is guarenteed not to push more values
218 ** onto the VDBE stack than it pops off.
219 */
220 static int opcodeNoPush(u8 op){
221   /* The 10 NOPUSH_MASK_n constants are defined in the automatically
222   ** generated header file opcodes.h. Each is a 16-bit bitmask, one
223   ** bit corresponding to each opcode implemented by the virtual
224   ** machine in vdbe.c. The bit is true if the word "no-push" appears
225   ** in a comment on the same line as the "case OP_XXX:" in
226   ** sqlite3VdbeExec() in vdbe.c.
227   **
228   ** If the bit is true, then the corresponding opcode is guarenteed not
229   ** to grow the stack when it is executed. Otherwise, it may grow the
230   ** stack by at most one entry.
231   **
232   ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
233   ** one bit for opcodes 16 to 31, and so on.
234   **
235   ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
236   ** because the file is generated by an awk program. Awk manipulates
237   ** all numbers as floating-point and we don't want to risk a rounding
238   ** error if someone builds with an awk that uses (for example) 32-bit
239   ** IEEE floats.
240   */
241   static const u32 masks[5] = {
242     NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
243     NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
244     NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
245     NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
246     NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
247   };
248   assert( op<32*5 );
249   return (masks[op>>5] & (1<<(op&0x1F)));
250 }
251 
252 #ifndef NDEBUG
253 int sqlite3VdbeOpcodeNoPush(u8 op){
254   return opcodeNoPush(op);
255 }
256 #endif
257 
258 /*
259 ** Loop through the program looking for P2 values that are negative.
260 ** Each such value is a label.  Resolve the label by setting the P2
261 ** value to its correct non-zero value.
262 **
263 ** This routine is called once after all opcodes have been inserted.
264 **
265 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
266 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
267 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
268 **
269 ** The integer *pMaxStack is set to the maximum number of vdbe stack
270 ** entries that static analysis reveals this program might need.
271 **
272 ** This routine also does the following optimization:  It scans for
273 ** instructions that might cause a statement rollback.  Such instructions
274 ** are:
275 **
276 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
277 **   *  OP_Destroy
278 **   *  OP_VUpdate
279 **   *  OP_VRename
280 **
281 ** If no such instruction is found, then every Statement instruction
282 ** is changed to a Noop.  In this way, we avoid creating the statement
283 ** journal file unnecessarily.
284 */
285 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
286   int i;
287   int nMaxArgs = 0;
288   int nMaxStack = p->nOp;
289   Op *pOp;
290   int *aLabel = p->aLabel;
291   int doesStatementRollback = 0;
292   int hasStatementBegin = 0;
293   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
294     u8 opcode = pOp->opcode;
295 
296     if( opcode==OP_Function || opcode==OP_AggStep
297 #ifndef SQLITE_OMIT_VIRTUALTABLE
298         || opcode==OP_VUpdate
299 #endif
300     ){
301       if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
302     }
303     if( opcode==OP_Halt ){
304       if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
305         doesStatementRollback = 1;
306       }
307     }else if( opcode==OP_Statement ){
308       hasStatementBegin = 1;
309     }else if( opcode==OP_Destroy ){
310       doesStatementRollback = 1;
311 #ifndef SQLITE_OMIT_VIRTUALTABLE
312     }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
313       doesStatementRollback = 1;
314     }else if( opcode==OP_VFilter ){
315       int n;
316       assert( p->nOp - i >= 3 );
317       assert( pOp[-2].opcode==OP_Integer );
318       n = pOp[-2].p1;
319       if( n>nMaxArgs ) nMaxArgs = n;
320 #endif
321     }
322     if( opcodeNoPush(opcode) ){
323       nMaxStack--;
324     }
325 
326     if( pOp->p2>=0 ) continue;
327     assert( -1-pOp->p2<p->nLabel );
328     pOp->p2 = aLabel[-1-pOp->p2];
329   }
330   sqlite3_free(p->aLabel);
331   p->aLabel = 0;
332 
333   *pMaxFuncArgs = nMaxArgs;
334   *pMaxStack = nMaxStack;
335 
336   /* If we never rollback a statement transaction, then statement
337   ** transactions are not needed.  So change every OP_Statement
338   ** opcode into an OP_Noop.  This avoid a call to sqlite3OsOpenExclusive()
339   ** which can be expensive on some platforms.
340   */
341   if( hasStatementBegin && !doesStatementRollback ){
342     for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
343       if( pOp->opcode==OP_Statement ){
344         pOp->opcode = OP_Noop;
345       }
346     }
347   }
348 }
349 
350 /*
351 ** Return the address of the next instruction to be inserted.
352 */
353 int sqlite3VdbeCurrentAddr(Vdbe *p){
354   assert( p->magic==VDBE_MAGIC_INIT );
355   return p->nOp;
356 }
357 
358 /*
359 ** Add a whole list of operations to the operation stack.  Return the
360 ** address of the first operation added.
361 */
362 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
363   int addr;
364   assert( p->magic==VDBE_MAGIC_INIT );
365   if( p->nOp + nOp > p->nOpAlloc ){
366     resizeOpArray(p, p->nOp*2 + nOp);
367   }
368   if( p->db->mallocFailed ){
369     return 0;
370   }
371   addr = p->nOp;
372   if( nOp>0 ){
373     int i;
374     VdbeOpList const *pIn = aOp;
375     for(i=0; i<nOp; i++, pIn++){
376       int p2 = pIn->p2;
377       VdbeOp *pOut = &p->aOp[i+addr];
378       pOut->opcode = pIn->opcode;
379       pOut->p1 = pIn->p1;
380       pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
381       pOut->p3 = pIn->p3;
382       pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
383 #ifdef SQLITE_DEBUG
384       if( sqlite3_vdbe_addop_trace ){
385         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
386       }
387 #endif
388     }
389     p->nOp += nOp;
390   }
391   return addr;
392 }
393 
394 /*
395 ** Change the value of the P1 operand for a specific instruction.
396 ** This routine is useful when a large program is loaded from a
397 ** static array using sqlite3VdbeAddOpList but we want to make a
398 ** few minor changes to the program.
399 */
400 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
401   assert( p==0 || p->magic==VDBE_MAGIC_INIT );
402   if( p && addr>=0 && p->nOp>addr && p->aOp ){
403     p->aOp[addr].p1 = val;
404   }
405 }
406 
407 /*
408 ** Change the value of the P2 operand for a specific instruction.
409 ** This routine is useful for setting a jump destination.
410 */
411 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
412   assert( val>=0 );
413   assert( p==0 || p->magic==VDBE_MAGIC_INIT );
414   if( p && addr>=0 && p->nOp>addr && p->aOp ){
415     p->aOp[addr].p2 = val;
416   }
417 }
418 
419 /*
420 ** Change the P2 operand of instruction addr so that it points to
421 ** the address of the next instruction to be coded.
422 */
423 void sqlite3VdbeJumpHere(Vdbe *p, int addr){
424   sqlite3VdbeChangeP2(p, addr, p->nOp);
425 }
426 
427 
428 /*
429 ** If the input FuncDef structure is ephemeral, then free it.  If
430 ** the FuncDef is not ephermal, then do nothing.
431 */
432 static void freeEphemeralFunction(FuncDef *pDef){
433   if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
434     sqlite3_free(pDef);
435   }
436 }
437 
438 /*
439 ** Delete a P3 value if necessary.
440 */
441 static void freeP3(int p3type, void *p3){
442   if( p3 ){
443     switch( p3type ){
444       case P3_REAL:
445       case P3_INT64:
446       case P3_MPRINTF:
447       case P3_DYNAMIC:
448       case P3_KEYINFO:
449       case P3_KEYINFO_HANDOFF: {
450         sqlite3_free(p3);
451         break;
452       }
453       case P3_VDBEFUNC: {
454         VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
455         freeEphemeralFunction(pVdbeFunc->pFunc);
456         sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
457         sqlite3_free(pVdbeFunc);
458         break;
459       }
460       case P3_FUNCDEF: {
461         freeEphemeralFunction((FuncDef*)p3);
462         break;
463       }
464       case P3_MEM: {
465         sqlite3ValueFree((sqlite3_value*)p3);
466         break;
467       }
468     }
469   }
470 }
471 
472 
473 /*
474 ** Change N opcodes starting at addr to No-ops.
475 */
476 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
477   if( p && p->aOp ){
478     VdbeOp *pOp = &p->aOp[addr];
479     while( N-- ){
480       freeP3(pOp->p3type, pOp->p3);
481       memset(pOp, 0, sizeof(pOp[0]));
482       pOp->opcode = OP_Noop;
483       pOp++;
484     }
485   }
486 }
487 
488 /*
489 ** Change the value of the P3 operand for a specific instruction.
490 ** This routine is useful when a large program is loaded from a
491 ** static array using sqlite3VdbeAddOpList but we want to make a
492 ** few minor changes to the program.
493 **
494 ** If n>=0 then the P3 operand is dynamic, meaning that a copy of
495 ** the string is made into memory obtained from sqlite3_malloc().
496 ** A value of n==0 means copy bytes of zP3 up to and including the
497 ** first null byte.  If n>0 then copy n+1 bytes of zP3.
498 **
499 ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
500 ** A copy is made of the KeyInfo structure into memory obtained from
501 ** sqlite3_malloc, to be freed when the Vdbe is finalized.
502 ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
503 ** stored in memory that the caller has obtained from sqlite3_malloc. The
504 ** caller should not free the allocation, it will be freed when the Vdbe is
505 ** finalized.
506 **
507 ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
508 ** to a string or structure that is guaranteed to exist for the lifetime of
509 ** the Vdbe. In these cases we can just copy the pointer.
510 **
511 ** If addr<0 then change P3 on the most recently inserted instruction.
512 */
513 void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
514   Op *pOp;
515   assert( p==0 || p->magic==VDBE_MAGIC_INIT );
516   if( p==0 || p->aOp==0 || p->db->mallocFailed ){
517     if (n != P3_KEYINFO) {
518       freeP3(n, (void*)*(char**)&zP3);
519     }
520     return;
521   }
522   if( addr<0 || addr>=p->nOp ){
523     addr = p->nOp - 1;
524     if( addr<0 ) return;
525   }
526   pOp = &p->aOp[addr];
527   freeP3(pOp->p3type, pOp->p3);
528   pOp->p3 = 0;
529   if( zP3==0 ){
530     pOp->p3 = 0;
531     pOp->p3type = P3_NOTUSED;
532   }else if( n==P3_KEYINFO ){
533     KeyInfo *pKeyInfo;
534     int nField, nByte;
535 
536     nField = ((KeyInfo*)zP3)->nField;
537     nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
538     pKeyInfo = sqlite3_malloc( nByte );
539     pOp->p3 = (char*)pKeyInfo;
540     if( pKeyInfo ){
541       unsigned char *aSortOrder;
542       memcpy(pKeyInfo, zP3, nByte);
543       aSortOrder = pKeyInfo->aSortOrder;
544       if( aSortOrder ){
545         pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
546         memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
547       }
548       pOp->p3type = P3_KEYINFO;
549     }else{
550       p->db->mallocFailed = 1;
551       pOp->p3type = P3_NOTUSED;
552     }
553   }else if( n==P3_KEYINFO_HANDOFF ){
554     pOp->p3 = (char*)zP3;
555     pOp->p3type = P3_KEYINFO;
556   }else if( n<0 ){
557     pOp->p3 = (char*)zP3;
558     pOp->p3type = n;
559   }else{
560     if( n==0 ) n = strlen(zP3);
561     pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n);
562     pOp->p3type = P3_DYNAMIC;
563   }
564 }
565 
566 #ifndef NDEBUG
567 /*
568 ** Replace the P3 field of the most recently coded instruction with
569 ** comment text.
570 */
571 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
572   va_list ap;
573   assert( p->nOp>0 || p->aOp==0 );
574   assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed );
575   va_start(ap, zFormat);
576   sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC);
577   va_end(ap);
578 }
579 #endif
580 
581 /*
582 ** Return the opcode for a given address.
583 */
584 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
585   assert( p->magic==VDBE_MAGIC_INIT );
586   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
587   return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
588 }
589 
590 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
591      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
592 /*
593 ** Compute a string that describes the P3 parameter for an opcode.
594 ** Use zTemp for any required temporary buffer space.
595 */
596 static char *displayP3(Op *pOp, char *zTemp, int nTemp){
597   char *zP3;
598   assert( nTemp>=20 );
599   switch( pOp->p3type ){
600     case P3_KEYINFO: {
601       int i, j;
602       KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
603       sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
604       i = strlen(zTemp);
605       for(j=0; j<pKeyInfo->nField; j++){
606         CollSeq *pColl = pKeyInfo->aColl[j];
607         if( pColl ){
608           int n = strlen(pColl->zName);
609           if( i+n>nTemp-6 ){
610             memcpy(&zTemp[i],",...",4);
611             break;
612           }
613           zTemp[i++] = ',';
614           if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
615             zTemp[i++] = '-';
616           }
617           memcpy(&zTemp[i], pColl->zName,n+1);
618           i += n;
619         }else if( i+4<nTemp-6 ){
620           memcpy(&zTemp[i],",nil",4);
621           i += 4;
622         }
623       }
624       zTemp[i++] = ')';
625       zTemp[i] = 0;
626       assert( i<nTemp );
627       zP3 = zTemp;
628       break;
629     }
630     case P3_COLLSEQ: {
631       CollSeq *pColl = (CollSeq*)pOp->p3;
632       sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
633       zP3 = zTemp;
634       break;
635     }
636     case P3_FUNCDEF: {
637       FuncDef *pDef = (FuncDef*)pOp->p3;
638       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
639       zP3 = zTemp;
640       break;
641     }
642     case P3_INT64: {
643       sqlite3_snprintf(nTemp, zTemp, "%lld", *(sqlite3_int64*)pOp->p3);
644       zP3 = zTemp;
645       break;
646     }
647     case P3_REAL: {
648       sqlite3_snprintf(nTemp, zTemp, "%.16g", *(double*)pOp->p3);
649       zP3 = zTemp;
650       break;
651     }
652 #ifndef SQLITE_OMIT_VIRTUALTABLE
653     case P3_VTAB: {
654       sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
655       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
656       zP3 = zTemp;
657       break;
658     }
659 #endif
660     default: {
661       zP3 = pOp->p3;
662       if( zP3==0 || pOp->opcode==OP_Noop ){
663         zP3 = "";
664       }
665     }
666   }
667   assert( zP3!=0 );
668   return zP3;
669 }
670 #endif
671 
672 /*
673 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
674 **
675 */
676 void sqlite3VdbeUsesBtree(Vdbe *p, int i){
677   int mask;
678   assert( i>=0 && i<p->db->nDb );
679   assert( i<sizeof(p->btreeMask)*8 );
680   mask = 1<<i;
681   if( (p->btreeMask & mask)==0 ){
682     p->btreeMask |= mask;
683     sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
684   }
685 }
686 
687 
688 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
689 /*
690 ** Print a single opcode.  This routine is used for debugging only.
691 */
692 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
693   char *zP3;
694   char zPtr[50];
695   static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
696   if( pOut==0 ) pOut = stdout;
697   zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
698   fprintf(pOut, zFormat1,
699       pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3);
700   fflush(pOut);
701 }
702 #endif
703 
704 /*
705 ** Release an array of N Mem elements
706 */
707 static void releaseMemArray(Mem *p, int N){
708   if( p ){
709     while( N-->0 ){
710       assert( N<2 || p[0].db==p[1].db );
711       sqlite3VdbeMemRelease(p++);
712     }
713   }
714 }
715 
716 #ifndef SQLITE_OMIT_EXPLAIN
717 /*
718 ** Give a listing of the program in the virtual machine.
719 **
720 ** The interface is the same as sqlite3VdbeExec().  But instead of
721 ** running the code, it invokes the callback once for each instruction.
722 ** This feature is used to implement "EXPLAIN".
723 */
724 int sqlite3VdbeList(
725   Vdbe *p                   /* The VDBE */
726 ){
727   sqlite3 *db = p->db;
728   int i;
729   int rc = SQLITE_OK;
730 
731   assert( p->explain );
732   if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
733   assert( db->magic==SQLITE_MAGIC_BUSY );
734   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
735 
736   /* Even though this opcode does not put dynamic strings onto the
737   ** the stack, they may become dynamic if the user calls
738   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
739   */
740   if( p->pTos==&p->aStack[4] ){
741     releaseMemArray(p->aStack, 5);
742   }
743   p->resOnStack = 0;
744 
745   do{
746     i = p->pc++;
747   }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
748   if( i>=p->nOp ){
749     p->rc = SQLITE_OK;
750     rc = SQLITE_DONE;
751   }else if( db->u1.isInterrupted ){
752     p->rc = SQLITE_INTERRUPT;
753     rc = SQLITE_ERROR;
754     sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
755   }else{
756     Op *pOp = &p->aOp[i];
757     Mem *pMem = p->aStack;
758     pMem->flags = MEM_Int;
759     pMem->type = SQLITE_INTEGER;
760     pMem->u.i = i;                                /* Program counter */
761     pMem++;
762 
763     pMem->flags = MEM_Static|MEM_Str|MEM_Term;
764     pMem->z = (char*)sqlite3OpcodeName(pOp->opcode);  /* Opcode */
765     assert( pMem->z!=0 );
766     pMem->n = strlen(pMem->z);
767     pMem->type = SQLITE_TEXT;
768     pMem->enc = SQLITE_UTF8;
769     pMem++;
770 
771     pMem->flags = MEM_Int;
772     pMem->u.i = pOp->p1;                          /* P1 */
773     pMem->type = SQLITE_INTEGER;
774     pMem++;
775 
776     pMem->flags = MEM_Int;
777     pMem->u.i = pOp->p2;                          /* P2 */
778     pMem->type = SQLITE_INTEGER;
779     pMem++;
780 
781     pMem->flags = MEM_Ephem|MEM_Str|MEM_Term;   /* P3 */
782     pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
783     assert( pMem->z!=0 );
784     pMem->n = strlen(pMem->z);
785     pMem->type = SQLITE_TEXT;
786     pMem->enc = SQLITE_UTF8;
787 
788     p->nResColumn = 5 - 2*(p->explain-1);
789     p->pTos = pMem;
790     p->rc = SQLITE_OK;
791     p->resOnStack = 1;
792     rc = SQLITE_ROW;
793   }
794   return rc;
795 }
796 #endif /* SQLITE_OMIT_EXPLAIN */
797 
798 #ifdef SQLITE_DEBUG
799 /*
800 ** Print the SQL that was used to generate a VDBE program.
801 */
802 void sqlite3VdbePrintSql(Vdbe *p){
803   int nOp = p->nOp;
804   VdbeOp *pOp;
805   if( nOp<1 ) return;
806   pOp = &p->aOp[nOp-1];
807   if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
808     const char *z = pOp->p3;
809     while( isspace(*(u8*)z) ) z++;
810     printf("SQL: [%s]\n", z);
811   }
812 }
813 #endif
814 
815 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
816 /*
817 ** Print an IOTRACE message showing SQL content.
818 */
819 void sqlite3VdbeIOTraceSql(Vdbe *p){
820   int nOp = p->nOp;
821   VdbeOp *pOp;
822   if( sqlite3_io_trace==0 ) return;
823   if( nOp<1 ) return;
824   pOp = &p->aOp[nOp-1];
825   if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
826     int i, j;
827     char z[1000];
828     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3);
829     for(i=0; isspace((unsigned char)z[i]); i++){}
830     for(j=0; z[i]; i++){
831       if( isspace((unsigned char)z[i]) ){
832         if( z[i-1]!=' ' ){
833           z[j++] = ' ';
834         }
835       }else{
836         z[j++] = z[i];
837       }
838     }
839     z[j] = 0;
840     sqlite3_io_trace("SQL %s\n", z);
841   }
842 }
843 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
844 
845 
846 /*
847 ** Prepare a virtual machine for execution.  This involves things such
848 ** as allocating stack space and initializing the program counter.
849 ** After the VDBE has be prepped, it can be executed by one or more
850 ** calls to sqlite3VdbeExec().
851 **
852 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
853 ** VDBE_MAGIC_RUN.
854 */
855 void sqlite3VdbeMakeReady(
856   Vdbe *p,                       /* The VDBE */
857   int nVar,                      /* Number of '?' see in the SQL statement */
858   int nMem,                      /* Number of memory cells to allocate */
859   int nCursor,                   /* Number of cursors to allocate */
860   int isExplain                  /* True if the EXPLAIN keywords is present */
861 ){
862   int n;
863   sqlite3 *db = p->db;
864 
865   assert( p!=0 );
866   assert( p->magic==VDBE_MAGIC_INIT );
867 
868   /* There should be at least one opcode.
869   */
870   assert( p->nOp>0 );
871 
872   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
873    * is because the call to resizeOpArray() below may shrink the
874    * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
875    * state.
876    */
877   p->magic = VDBE_MAGIC_RUN;
878 
879   /* No instruction ever pushes more than a single element onto the
880   ** stack.  And the stack never grows on successive executions of the
881   ** same loop.  So the total number of instructions is an upper bound
882   ** on the maximum stack depth required.  (Added later:)  The
883   ** resolveP2Values() call computes a tighter upper bound on the
884   ** stack size.
885   **
886   ** Allocation all the stack space we will ever need.
887   */
888   if( p->aStack==0 ){
889     int nArg;       /* Maximum number of args passed to a user function. */
890     int nStack;     /* Maximum number of stack entries required */
891     resolveP2Values(p, &nArg, &nStack);
892     resizeOpArray(p, p->nOp);
893     assert( nVar>=0 );
894     assert( nStack<p->nOp );
895     if( isExplain ){
896       nStack = 10;
897     }
898     p->aStack = sqlite3DbMallocZero(db,
899         nStack*sizeof(p->aStack[0])    /* aStack */
900       + nArg*sizeof(Mem*)              /* apArg */
901       + nVar*sizeof(Mem)               /* aVar */
902       + nVar*sizeof(char*)             /* azVar */
903       + nMem*sizeof(Mem)               /* aMem */
904       + nCursor*sizeof(Cursor*)        /* apCsr */
905     );
906     if( !db->mallocFailed ){
907       p->aMem = &p->aStack[nStack];
908       p->nMem = nMem;
909       p->aVar = &p->aMem[nMem];
910       p->nVar = nVar;
911       p->okVar = 0;
912       p->apArg = (Mem**)&p->aVar[nVar];
913       p->azVar = (char**)&p->apArg[nArg];
914       p->apCsr = (Cursor**)&p->azVar[nVar];
915       p->nCursor = nCursor;
916       for(n=0; n<nVar; n++){
917         p->aVar[n].flags = MEM_Null;
918         p->aVar[n].db = db;
919       }
920       for(n=0; n<nStack; n++){
921         p->aStack[n].db = db;
922       }
923     }
924   }
925   for(n=0; n<p->nMem; n++){
926     p->aMem[n].flags = MEM_Null;
927     p->aMem[n].db = db;
928   }
929 
930   p->pTos = &p->aStack[-1];
931   p->pc = -1;
932   p->rc = SQLITE_OK;
933   p->uniqueCnt = 0;
934   p->returnDepth = 0;
935   p->errorAction = OE_Abort;
936   p->popStack =  0;
937   p->explain |= isExplain;
938   p->magic = VDBE_MAGIC_RUN;
939   p->nChange = 0;
940   p->cacheCtr = 1;
941   p->minWriteFileFormat = 255;
942   p->openedStatement = 0;
943 #ifdef VDBE_PROFILE
944   {
945     int i;
946     for(i=0; i<p->nOp; i++){
947       p->aOp[i].cnt = 0;
948       p->aOp[i].cycles = 0;
949     }
950   }
951 #endif
952 }
953 
954 /*
955 ** Close a VDBE cursor and release all the resources that cursor happens
956 ** to hold.
957 */
958 void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
959   if( pCx==0 ){
960     return;
961   }
962   if( pCx->pCursor ){
963     sqlite3BtreeCloseCursor(pCx->pCursor);
964   }
965   if( pCx->pBt ){
966     sqlite3BtreeClose(pCx->pBt);
967   }
968 #ifndef SQLITE_OMIT_VIRTUALTABLE
969   if( pCx->pVtabCursor ){
970     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
971     const sqlite3_module *pModule = pCx->pModule;
972     p->inVtabMethod = 1;
973     sqlite3SafetyOff(p->db);
974     pModule->xClose(pVtabCursor);
975     sqlite3SafetyOn(p->db);
976     p->inVtabMethod = 0;
977   }
978 #endif
979   sqlite3_free(pCx->pData);
980   sqlite3_free(pCx->aType);
981   sqlite3_free(pCx);
982 }
983 
984 /*
985 ** Close all cursors except for VTab cursors that are currently
986 ** in use.
987 */
988 static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
989   int i;
990   if( p->apCsr==0 ) return;
991   for(i=0; i<p->nCursor; i++){
992     Cursor *pC = p->apCsr[i];
993     if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
994       sqlite3VdbeFreeCursor(p, pC);
995       p->apCsr[i] = 0;
996     }
997   }
998 }
999 
1000 /*
1001 ** Clean up the VM after execution.
1002 **
1003 ** This routine will automatically close any cursors, lists, and/or
1004 ** sorters that were left open.  It also deletes the values of
1005 ** variables in the aVar[] array.
1006 */
1007 static void Cleanup(Vdbe *p){
1008   int i;
1009   if( p->aStack ){
1010     releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
1011     p->pTos = &p->aStack[-1];
1012   }
1013   closeAllCursorsExceptActiveVtabs(p);
1014   releaseMemArray(p->aMem, p->nMem);
1015   sqlite3VdbeFifoClear(&p->sFifo);
1016   if( p->contextStack ){
1017     for(i=0; i<p->contextStackTop; i++){
1018       sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
1019     }
1020     sqlite3_free(p->contextStack);
1021   }
1022   p->contextStack = 0;
1023   p->contextStackDepth = 0;
1024   p->contextStackTop = 0;
1025   sqlite3_free(p->zErrMsg);
1026   p->zErrMsg = 0;
1027   p->resOnStack = 0;
1028 }
1029 
1030 /*
1031 ** Set the number of result columns that will be returned by this SQL
1032 ** statement. This is now set at compile time, rather than during
1033 ** execution of the vdbe program so that sqlite3_column_count() can
1034 ** be called on an SQL statement before sqlite3_step().
1035 */
1036 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
1037   Mem *pColName;
1038   int n;
1039 
1040   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1041   sqlite3_free(p->aColName);
1042   n = nResColumn*COLNAME_N;
1043   p->nResColumn = nResColumn;
1044   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
1045   if( p->aColName==0 ) return;
1046   while( n-- > 0 ){
1047     pColName->flags = MEM_Null;
1048     pColName->db = p->db;
1049     pColName++;
1050   }
1051 }
1052 
1053 /*
1054 ** Set the name of the idx'th column to be returned by the SQL statement.
1055 ** zName must be a pointer to a nul terminated string.
1056 **
1057 ** This call must be made after a call to sqlite3VdbeSetNumCols().
1058 **
1059 ** If N==P3_STATIC  it means that zName is a pointer to a constant static
1060 ** string and we can just copy the pointer. If it is P3_DYNAMIC, then
1061 ** the string is freed using sqlite3_free() when the vdbe is finished with
1062 ** it. Otherwise, N bytes of zName are copied.
1063 */
1064 int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
1065   int rc;
1066   Mem *pColName;
1067   assert( idx<p->nResColumn );
1068   assert( var<COLNAME_N );
1069   if( p->db->mallocFailed ) return SQLITE_NOMEM;
1070   assert( p->aColName!=0 );
1071   pColName = &(p->aColName[idx+var*p->nResColumn]);
1072   if( N==P3_DYNAMIC || N==P3_STATIC ){
1073     rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
1074   }else{
1075     rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
1076   }
1077   if( rc==SQLITE_OK && N==P3_DYNAMIC ){
1078     pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
1079     pColName->xDel = 0;
1080   }
1081   return rc;
1082 }
1083 
1084 /*
1085 ** A read or write transaction may or may not be active on database handle
1086 ** db. If a transaction is active, commit it. If there is a
1087 ** write-transaction spanning more than one database file, this routine
1088 ** takes care of the master journal trickery.
1089 */
1090 static int vdbeCommit(sqlite3 *db){
1091   int i;
1092   int nTrans = 0;  /* Number of databases with an active write-transaction */
1093   int rc = SQLITE_OK;
1094   int needXcommit = 0;
1095 
1096   /* Before doing anything else, call the xSync() callback for any
1097   ** virtual module tables written in this transaction. This has to
1098   ** be done before determining whether a master journal file is
1099   ** required, as an xSync() callback may add an attached database
1100   ** to the transaction.
1101   */
1102   rc = sqlite3VtabSync(db, rc);
1103   if( rc!=SQLITE_OK ){
1104     return rc;
1105   }
1106 
1107   /* This loop determines (a) if the commit hook should be invoked and
1108   ** (b) how many database files have open write transactions, not
1109   ** including the temp database. (b) is important because if more than
1110   ** one database file has an open write transaction, a master journal
1111   ** file is required for an atomic commit.
1112   */
1113   for(i=0; i<db->nDb; i++){
1114     Btree *pBt = db->aDb[i].pBt;
1115     if( sqlite3BtreeIsInTrans(pBt) ){
1116       needXcommit = 1;
1117       if( i!=1 ) nTrans++;
1118     }
1119   }
1120 
1121   /* If there are any write-transactions at all, invoke the commit hook */
1122   if( needXcommit && db->xCommitCallback ){
1123     sqlite3SafetyOff(db);
1124     rc = db->xCommitCallback(db->pCommitArg);
1125     sqlite3SafetyOn(db);
1126     if( rc ){
1127       return SQLITE_CONSTRAINT;
1128     }
1129   }
1130 
1131   /* The simple case - no more than one database file (not counting the
1132   ** TEMP database) has a transaction active.   There is no need for the
1133   ** master-journal.
1134   **
1135   ** If the return value of sqlite3BtreeGetFilename() is a zero length
1136   ** string, it means the main database is :memory:.  In that case we do
1137   ** not support atomic multi-file commits, so use the simple case then
1138   ** too.
1139   */
1140   if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
1141     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1142       Btree *pBt = db->aDb[i].pBt;
1143       if( pBt ){
1144         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
1145       }
1146     }
1147 
1148     /* Do the commit only if all databases successfully complete phase 1.
1149     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1150     ** IO error while deleting or truncating a journal file. It is unlikely,
1151     ** but could happen. In this case abandon processing and return the error.
1152     */
1153     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1154       Btree *pBt = db->aDb[i].pBt;
1155       if( pBt ){
1156         rc = sqlite3BtreeCommitPhaseTwo(pBt);
1157       }
1158     }
1159     if( rc==SQLITE_OK ){
1160       sqlite3VtabCommit(db);
1161     }
1162   }
1163 
1164   /* The complex case - There is a multi-file write-transaction active.
1165   ** This requires a master journal file to ensure the transaction is
1166   ** committed atomicly.
1167   */
1168 #ifndef SQLITE_OMIT_DISKIO
1169   else{
1170     sqlite3_vfs *pVfs = db->pVfs;
1171     int needSync = 0;
1172     char *zMaster = 0;   /* File-name for the master journal */
1173     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
1174     sqlite3_file *pMaster = 0;
1175     i64 offset = 0;
1176 
1177     /* Select a master journal file name */
1178     do {
1179       u32 random;
1180       sqlite3_free(zMaster);
1181       sqlite3Randomness(sizeof(random), &random);
1182       zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
1183       if( !zMaster ){
1184         return SQLITE_NOMEM;
1185       }
1186     }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
1187 
1188     /* Open the master journal. */
1189     rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1190         SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1191         SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1192     );
1193     if( rc!=SQLITE_OK ){
1194       sqlite3_free(zMaster);
1195       return rc;
1196     }
1197 
1198     /* Write the name of each database file in the transaction into the new
1199     ** master journal file. If an error occurs at this point close
1200     ** and delete the master journal file. All the individual journal files
1201     ** still have 'null' as the master journal pointer, so they will roll
1202     ** back independently if a failure occurs.
1203     */
1204     for(i=0; i<db->nDb; i++){
1205       Btree *pBt = db->aDb[i].pBt;
1206       if( i==1 ) continue;   /* Ignore the TEMP database */
1207       if( sqlite3BtreeIsInTrans(pBt) ){
1208         char const *zFile = sqlite3BtreeGetJournalname(pBt);
1209         if( zFile[0]==0 ) continue;  /* Ignore :memory: databases */
1210         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1211           needSync = 1;
1212         }
1213         rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
1214         offset += strlen(zFile)+1;
1215         if( rc!=SQLITE_OK ){
1216           sqlite3OsCloseFree(pMaster);
1217           sqlite3OsDelete(pVfs, zMaster, 0);
1218           sqlite3_free(zMaster);
1219           return rc;
1220         }
1221       }
1222     }
1223 
1224     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1225     ** flag is set this is not required.
1226     */
1227     zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1228     if( (needSync
1229      && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
1230      && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
1231       sqlite3OsCloseFree(pMaster);
1232       sqlite3OsDelete(pVfs, zMaster, 0);
1233       sqlite3_free(zMaster);
1234       return rc;
1235     }
1236 
1237     /* Sync all the db files involved in the transaction. The same call
1238     ** sets the master journal pointer in each individual journal. If
1239     ** an error occurs here, do not delete the master journal file.
1240     **
1241     ** If the error occurs during the first call to
1242     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1243     ** master journal file will be orphaned. But we cannot delete it,
1244     ** in case the master journal file name was written into the journal
1245     ** file before the failure occured.
1246     */
1247     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1248       Btree *pBt = db->aDb[i].pBt;
1249       if( pBt ){
1250         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
1251       }
1252     }
1253     sqlite3OsCloseFree(pMaster);
1254     if( rc!=SQLITE_OK ){
1255       sqlite3_free(zMaster);
1256       return rc;
1257     }
1258 
1259     /* Delete the master journal file. This commits the transaction. After
1260     ** doing this the directory is synced again before any individual
1261     ** transaction files are deleted.
1262     */
1263     rc = sqlite3OsDelete(pVfs, zMaster, 1);
1264     sqlite3_free(zMaster);
1265     zMaster = 0;
1266     if( rc ){
1267       return rc;
1268     }
1269 
1270     /* All files and directories have already been synced, so the following
1271     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1272     ** deleting or truncating journals. If something goes wrong while
1273     ** this is happening we don't really care. The integrity of the
1274     ** transaction is already guaranteed, but some stray 'cold' journals
1275     ** may be lying around. Returning an error code won't help matters.
1276     */
1277     disable_simulated_io_errors();
1278     for(i=0; i<db->nDb; i++){
1279       Btree *pBt = db->aDb[i].pBt;
1280       if( pBt ){
1281         sqlite3BtreeCommitPhaseTwo(pBt);
1282       }
1283     }
1284     enable_simulated_io_errors();
1285 
1286     sqlite3VtabCommit(db);
1287   }
1288 #endif
1289 
1290   return rc;
1291 }
1292 
1293 /*
1294 ** This routine checks that the sqlite3.activeVdbeCnt count variable
1295 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
1296 ** currently active. An assertion fails if the two counts do not match.
1297 ** This is an internal self-check only - it is not an essential processing
1298 ** step.
1299 **
1300 ** This is a no-op if NDEBUG is defined.
1301 */
1302 #ifndef NDEBUG
1303 static void checkActiveVdbeCnt(sqlite3 *db){
1304   Vdbe *p;
1305   int cnt = 0;
1306   p = db->pVdbe;
1307   while( p ){
1308     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
1309       cnt++;
1310     }
1311     p = p->pNext;
1312   }
1313   assert( cnt==db->activeVdbeCnt );
1314 }
1315 #else
1316 #define checkActiveVdbeCnt(x)
1317 #endif
1318 
1319 /*
1320 ** For every Btree that in database connection db which
1321 ** has been modified, "trip" or invalidate each cursor in
1322 ** that Btree might have been modified so that the cursor
1323 ** can never be used again.  This happens when a rollback
1324 *** occurs.  We have to trip all the other cursors, even
1325 ** cursor from other VMs in different database connections,
1326 ** so that none of them try to use the data at which they
1327 ** were pointing and which now may have been changed due
1328 ** to the rollback.
1329 **
1330 ** Remember that a rollback can delete tables complete and
1331 ** reorder rootpages.  So it is not sufficient just to save
1332 ** the state of the cursor.  We have to invalidate the cursor
1333 ** so that it is never used again.
1334 */
1335 static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
1336   int i;
1337   for(i=0; i<db->nDb; i++){
1338     Btree *p = db->aDb[i].pBt;
1339     if( p && sqlite3BtreeIsInTrans(p) ){
1340       sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1341     }
1342   }
1343 }
1344 
1345 /*
1346 ** This routine is called the when a VDBE tries to halt.  If the VDBE
1347 ** has made changes and is in autocommit mode, then commit those
1348 ** changes.  If a rollback is needed, then do the rollback.
1349 **
1350 ** This routine is the only way to move the state of a VM from
1351 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
1352 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
1353 **
1354 ** Return an error code.  If the commit could not complete because of
1355 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
1356 ** means the close did not happen and needs to be repeated.
1357 */
1358 int sqlite3VdbeHalt(Vdbe *p){
1359   sqlite3 *db = p->db;
1360   int i;
1361   int (*xFunc)(Btree *pBt) = 0;  /* Function to call on each btree backend */
1362   int isSpecialError;            /* Set to true if SQLITE_NOMEM or IOERR */
1363 
1364   /* This function contains the logic that determines if a statement or
1365   ** transaction will be committed or rolled back as a result of the
1366   ** execution of this virtual machine.
1367   **
1368   ** If any of the following errors occur:
1369   **
1370   **     SQLITE_NOMEM
1371   **     SQLITE_IOERR
1372   **     SQLITE_FULL
1373   **     SQLITE_INTERRUPT
1374   **
1375   ** Then the internal cache might have been left in an inconsistent
1376   ** state.  We need to rollback the statement transaction, if there is
1377   ** one, or the complete transaction if there is no statement transaction.
1378   */
1379 
1380   if( p->db->mallocFailed ){
1381     p->rc = SQLITE_NOMEM;
1382   }
1383   closeAllCursorsExceptActiveVtabs(p);
1384   if( p->magic!=VDBE_MAGIC_RUN ){
1385     return SQLITE_OK;
1386   }
1387   checkActiveVdbeCnt(db);
1388 
1389   /* No commit or rollback needed if the program never started */
1390   if( p->pc>=0 ){
1391     int mrc;   /* Primary error code from p->rc */
1392 
1393     /* Lock all btrees used by the statement */
1394     sqlite3BtreeMutexArrayEnter(&p->aMutex);
1395 
1396     /* Check for one of the special errors */
1397     mrc = p->rc & 0xff;
1398     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
1399                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
1400     if( isSpecialError ){
1401       /* This loop does static analysis of the query to see which of the
1402       ** following three categories it falls into:
1403       **
1404       **     Read-only
1405       **     Query with statement journal
1406       **     Query without statement journal
1407       **
1408       ** We could do something more elegant than this static analysis (i.e.
1409       ** store the type of query as part of the compliation phase), but
1410       ** handling malloc() or IO failure is a fairly obscure edge case so
1411       ** this is probably easier. Todo: Might be an opportunity to reduce
1412       ** code size a very small amount though...
1413       */
1414       int notReadOnly = 0;
1415       int isStatement = 0;
1416       assert(p->aOp || p->nOp==0);
1417       for(i=0; i<p->nOp; i++){
1418         switch( p->aOp[i].opcode ){
1419           case OP_Transaction:
1420             notReadOnly |= p->aOp[i].p2;
1421             break;
1422           case OP_Statement:
1423             isStatement = 1;
1424             break;
1425         }
1426       }
1427 
1428 
1429       /* If the query was read-only, we need do no rollback at all. Otherwise,
1430       ** proceed with the special handling.
1431       */
1432       if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
1433         if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1434           xFunc = sqlite3BtreeRollbackStmt;
1435           p->rc = SQLITE_BUSY;
1436         } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
1437           xFunc = sqlite3BtreeRollbackStmt;
1438         }else{
1439           /* We are forced to roll back the active transaction. Before doing
1440           ** so, abort any other statements this handle currently has active.
1441           */
1442           invalidateCursorsOnModifiedBtrees(db);
1443           sqlite3RollbackAll(db);
1444           db->autoCommit = 1;
1445         }
1446       }
1447     }
1448 
1449     /* If the auto-commit flag is set and this is the only active vdbe, then
1450     ** we do either a commit or rollback of the current transaction.
1451     **
1452     ** Note: This block also runs if one of the special errors handled
1453     ** above has occured.
1454     */
1455     if( db->autoCommit && db->activeVdbeCnt==1 ){
1456       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
1457         /* The auto-commit flag is true, and the vdbe program was
1458         ** successful or hit an 'OR FAIL' constraint. This means a commit
1459         ** is required.
1460         */
1461         int rc = vdbeCommit(db);
1462         if( rc==SQLITE_BUSY ){
1463           sqlite3BtreeMutexArrayLeave(&p->aMutex);
1464           return SQLITE_BUSY;
1465         }else if( rc!=SQLITE_OK ){
1466           p->rc = rc;
1467           sqlite3RollbackAll(db);
1468         }else{
1469           sqlite3CommitInternalChanges(db);
1470         }
1471       }else{
1472         sqlite3RollbackAll(db);
1473       }
1474     }else if( !xFunc ){
1475       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1476         if( p->openedStatement ){
1477           xFunc = sqlite3BtreeCommitStmt;
1478         }
1479       }else if( p->errorAction==OE_Abort ){
1480         xFunc = sqlite3BtreeRollbackStmt;
1481       }else{
1482         invalidateCursorsOnModifiedBtrees(db);
1483         sqlite3RollbackAll(db);
1484         db->autoCommit = 1;
1485       }
1486     }
1487 
1488     /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1489     ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1490     ** and the return code is still SQLITE_OK, set the return code to the new
1491     ** error value.
1492     */
1493     assert(!xFunc ||
1494       xFunc==sqlite3BtreeCommitStmt ||
1495       xFunc==sqlite3BtreeRollbackStmt
1496     );
1497     for(i=0; xFunc && i<db->nDb; i++){
1498       int rc;
1499       Btree *pBt = db->aDb[i].pBt;
1500       if( pBt ){
1501         rc = xFunc(pBt);
1502         if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1503           p->rc = rc;
1504           sqlite3SetString(&p->zErrMsg, 0);
1505         }
1506       }
1507     }
1508 
1509     /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1510     ** set the change counter.
1511     */
1512     if( p->changeCntOn && p->pc>=0 ){
1513       if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1514         sqlite3VdbeSetChanges(db, p->nChange);
1515       }else{
1516         sqlite3VdbeSetChanges(db, 0);
1517       }
1518       p->nChange = 0;
1519     }
1520 
1521     /* Rollback or commit any schema changes that occurred. */
1522     if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1523       sqlite3ResetInternalSchema(db, 0);
1524       db->flags = (db->flags | SQLITE_InternChanges);
1525     }
1526 
1527     /* Release the locks */
1528     sqlite3BtreeMutexArrayLeave(&p->aMutex);
1529   }
1530 
1531   /* We have successfully halted and closed the VM.  Record this fact. */
1532   if( p->pc>=0 ){
1533     db->activeVdbeCnt--;
1534   }
1535   p->magic = VDBE_MAGIC_HALT;
1536   checkActiveVdbeCnt(db);
1537   if( p->db->mallocFailed ){
1538     p->rc = SQLITE_NOMEM;
1539   }
1540   checkActiveVdbeCnt(db);
1541 
1542   return SQLITE_OK;
1543 }
1544 
1545 
1546 /*
1547 ** Each VDBE holds the result of the most recent sqlite3_step() call
1548 ** in p->rc.  This routine sets that result back to SQLITE_OK.
1549 */
1550 void sqlite3VdbeResetStepResult(Vdbe *p){
1551   p->rc = SQLITE_OK;
1552 }
1553 
1554 /*
1555 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
1556 ** Write any error messages into *pzErrMsg.  Return the result code.
1557 **
1558 ** After this routine is run, the VDBE should be ready to be executed
1559 ** again.
1560 **
1561 ** To look at it another way, this routine resets the state of the
1562 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1563 ** VDBE_MAGIC_INIT.
1564 */
1565 int sqlite3VdbeReset(Vdbe *p){
1566   sqlite3 *db;
1567   db = p->db;
1568 
1569   /* If the VM did not run to completion or if it encountered an
1570   ** error, then it might not have been halted properly.  So halt
1571   ** it now.
1572   */
1573   sqlite3SafetyOn(db);
1574   sqlite3VdbeHalt(p);
1575   sqlite3SafetyOff(db);
1576 
1577   /* If the VDBE has be run even partially, then transfer the error code
1578   ** and error message from the VDBE into the main database structure.  But
1579   ** if the VDBE has just been set to run but has not actually executed any
1580   ** instructions yet, leave the main database error information unchanged.
1581   */
1582   if( p->pc>=0 ){
1583     if( p->zErrMsg ){
1584       sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
1585       db->errCode = p->rc;
1586       p->zErrMsg = 0;
1587     }else if( p->rc ){
1588       sqlite3Error(db, p->rc, 0);
1589     }else{
1590       sqlite3Error(db, SQLITE_OK, 0);
1591     }
1592   }else if( p->rc && p->expired ){
1593     /* The expired flag was set on the VDBE before the first call
1594     ** to sqlite3_step(). For consistency (since sqlite3_step() was
1595     ** called), set the database error in this case as well.
1596     */
1597     sqlite3Error(db, p->rc, 0);
1598     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3_free);
1599     p->zErrMsg = 0;
1600   }
1601 
1602   /* Reclaim all memory used by the VDBE
1603   */
1604   Cleanup(p);
1605 
1606   /* Save profiling information from this VDBE run.
1607   */
1608   assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
1609 #ifdef VDBE_PROFILE
1610   {
1611     FILE *out = fopen("vdbe_profile.out", "a");
1612     if( out ){
1613       int i;
1614       fprintf(out, "---- ");
1615       for(i=0; i<p->nOp; i++){
1616         fprintf(out, "%02x", p->aOp[i].opcode);
1617       }
1618       fprintf(out, "\n");
1619       for(i=0; i<p->nOp; i++){
1620         fprintf(out, "%6d %10lld %8lld ",
1621            p->aOp[i].cnt,
1622            p->aOp[i].cycles,
1623            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1624         );
1625         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
1626       }
1627       fclose(out);
1628     }
1629   }
1630 #endif
1631   p->magic = VDBE_MAGIC_INIT;
1632   p->aborted = 0;
1633   return p->rc & db->errMask;
1634 }
1635 
1636 /*
1637 ** Clean up and delete a VDBE after execution.  Return an integer which is
1638 ** the result code.  Write any error message text into *pzErrMsg.
1639 */
1640 int sqlite3VdbeFinalize(Vdbe *p){
1641   int rc = SQLITE_OK;
1642   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1643     rc = sqlite3VdbeReset(p);
1644     assert( (rc & p->db->errMask)==rc );
1645   }else if( p->magic!=VDBE_MAGIC_INIT ){
1646     return SQLITE_MISUSE;
1647   }
1648   sqlite3VdbeDelete(p);
1649   return rc;
1650 }
1651 
1652 /*
1653 ** Call the destructor for each auxdata entry in pVdbeFunc for which
1654 ** the corresponding bit in mask is clear.  Auxdata entries beyond 31
1655 ** are always destroyed.  To destroy all auxdata entries, call this
1656 ** routine with mask==0.
1657 */
1658 void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1659   int i;
1660   for(i=0; i<pVdbeFunc->nAux; i++){
1661     struct AuxData *pAux = &pVdbeFunc->apAux[i];
1662     if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1663       if( pAux->xDelete ){
1664         pAux->xDelete(pAux->pAux);
1665       }
1666       pAux->pAux = 0;
1667     }
1668   }
1669 }
1670 
1671 /*
1672 ** Delete an entire VDBE.
1673 */
1674 void sqlite3VdbeDelete(Vdbe *p){
1675   int i;
1676   if( p==0 ) return;
1677   Cleanup(p);
1678   if( p->pPrev ){
1679     p->pPrev->pNext = p->pNext;
1680   }else{
1681     assert( p->db->pVdbe==p );
1682     p->db->pVdbe = p->pNext;
1683   }
1684   if( p->pNext ){
1685     p->pNext->pPrev = p->pPrev;
1686   }
1687   if( p->aOp ){
1688     for(i=0; i<p->nOp; i++){
1689       Op *pOp = &p->aOp[i];
1690       freeP3(pOp->p3type, pOp->p3);
1691     }
1692     sqlite3_free(p->aOp);
1693   }
1694   releaseMemArray(p->aVar, p->nVar);
1695   sqlite3_free(p->aLabel);
1696   sqlite3_free(p->aStack);
1697   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1698   sqlite3_free(p->aColName);
1699   sqlite3_free(p->zSql);
1700   p->magic = VDBE_MAGIC_DEAD;
1701   sqlite3_free(p);
1702 }
1703 
1704 /*
1705 ** If a MoveTo operation is pending on the given cursor, then do that
1706 ** MoveTo now.  Return an error code.  If no MoveTo is pending, this
1707 ** routine does nothing and returns SQLITE_OK.
1708 */
1709 int sqlite3VdbeCursorMoveto(Cursor *p){
1710   if( p->deferredMoveto ){
1711     int res, rc;
1712 #ifdef SQLITE_TEST
1713     extern int sqlite3_search_count;
1714 #endif
1715     assert( p->isTable );
1716     rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
1717     if( rc ) return rc;
1718     *p->pIncrKey = 0;
1719     p->lastRowid = keyToInt(p->movetoTarget);
1720     p->rowidIsValid = res==0;
1721     if( res<0 ){
1722       rc = sqlite3BtreeNext(p->pCursor, &res);
1723       if( rc ) return rc;
1724     }
1725 #ifdef SQLITE_TEST
1726     sqlite3_search_count++;
1727 #endif
1728     p->deferredMoveto = 0;
1729     p->cacheStatus = CACHE_STALE;
1730   }
1731   return SQLITE_OK;
1732 }
1733 
1734 /*
1735 ** The following functions:
1736 **
1737 ** sqlite3VdbeSerialType()
1738 ** sqlite3VdbeSerialTypeLen()
1739 ** sqlite3VdbeSerialRead()
1740 ** sqlite3VdbeSerialLen()
1741 ** sqlite3VdbeSerialWrite()
1742 **
1743 ** encapsulate the code that serializes values for storage in SQLite
1744 ** data and index records. Each serialized value consists of a
1745 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1746 ** integer, stored as a varint.
1747 **
1748 ** In an SQLite index record, the serial type is stored directly before
1749 ** the blob of data that it corresponds to. In a table record, all serial
1750 ** types are stored at the start of the record, and the blobs of data at
1751 ** the end. Hence these functions allow the caller to handle the
1752 ** serial-type and data blob seperately.
1753 **
1754 ** The following table describes the various storage classes for data:
1755 **
1756 **   serial type        bytes of data      type
1757 **   --------------     ---------------    ---------------
1758 **      0                     0            NULL
1759 **      1                     1            signed integer
1760 **      2                     2            signed integer
1761 **      3                     3            signed integer
1762 **      4                     4            signed integer
1763 **      5                     6            signed integer
1764 **      6                     8            signed integer
1765 **      7                     8            IEEE float
1766 **      8                     0            Integer constant 0
1767 **      9                     0            Integer constant 1
1768 **     10,11                               reserved for expansion
1769 **    N>=12 and even       (N-12)/2        BLOB
1770 **    N>=13 and odd        (N-13)/2        text
1771 **
1772 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
1773 ** of SQLite will not understand those serial types.
1774 */
1775 
1776 /*
1777 ** Return the serial-type for the value stored in pMem.
1778 */
1779 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
1780   int flags = pMem->flags;
1781   int n;
1782 
1783   if( flags&MEM_Null ){
1784     return 0;
1785   }
1786   if( flags&MEM_Int ){
1787     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
1788 #   define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
1789     i64 i = pMem->u.i;
1790     u64 u;
1791     if( file_format>=4 && (i&1)==i ){
1792       return 8+i;
1793     }
1794     u = i<0 ? -i : i;
1795     if( u<=127 ) return 1;
1796     if( u<=32767 ) return 2;
1797     if( u<=8388607 ) return 3;
1798     if( u<=2147483647 ) return 4;
1799     if( u<=MAX_6BYTE ) return 5;
1800     return 6;
1801   }
1802   if( flags&MEM_Real ){
1803     return 7;
1804   }
1805   assert( flags&(MEM_Str|MEM_Blob) );
1806   n = pMem->n;
1807   if( flags & MEM_Zero ){
1808     n += pMem->u.i;
1809   }
1810   assert( n>=0 );
1811   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
1812 }
1813 
1814 /*
1815 ** Return the length of the data corresponding to the supplied serial-type.
1816 */
1817 int sqlite3VdbeSerialTypeLen(u32 serial_type){
1818   if( serial_type>=12 ){
1819     return (serial_type-12)/2;
1820   }else{
1821     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
1822     return aSize[serial_type];
1823   }
1824 }
1825 
1826 /*
1827 ** If we are on an architecture with mixed-endian floating
1828 ** points (ex: ARM7) then swap the lower 4 bytes with the
1829 ** upper 4 bytes.  Return the result.
1830 **
1831 ** For most architectures, this is a no-op.
1832 **
1833 ** (later):  It is reported to me that the mixed-endian problem
1834 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
1835 ** that early versions of GCC stored the two words of a 64-bit
1836 ** float in the wrong order.  And that error has been propagated
1837 ** ever since.  The blame is not necessarily with GCC, though.
1838 ** GCC might have just copying the problem from a prior compiler.
1839 ** I am also told that newer versions of GCC that follow a different
1840 ** ABI get the byte order right.
1841 **
1842 ** Developers using SQLite on an ARM7 should compile and run their
1843 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
1844 ** enabled, some asserts below will ensure that the byte order of
1845 ** floating point values is correct.
1846 **
1847 ** (2007-08-30)  Frank van Vugt has studied this problem closely
1848 ** and has send his findings to the SQLite developers.  Frank
1849 ** writes that some Linux kernels offer floating point hardware
1850 ** emulation that uses only 32-bit mantissas instead of a full
1851 ** 48-bits as required by the IEEE standard.  (This is the
1852 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
1853 ** byte swapping becomes very complicated.  To avoid problems,
1854 ** the necessary byte swapping is carried out using a 64-bit integer
1855 ** rather than a 64-bit float.  Frank assures us that the code here
1856 ** works for him.  We, the developers, have no way to independently
1857 ** verify this, but Frank seems to know what he is talking about
1858 ** so we trust him.
1859 */
1860 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
1861 static u64 floatSwap(u64 in){
1862   union {
1863     u64 r;
1864     u32 i[2];
1865   } u;
1866   u32 t;
1867 
1868   u.r = in;
1869   t = u.i[0];
1870   u.i[0] = u.i[1];
1871   u.i[1] = t;
1872   return u.r;
1873 }
1874 # define swapMixedEndianFloat(X)  X = floatSwap(X)
1875 #else
1876 # define swapMixedEndianFloat(X)
1877 #endif
1878 
1879 /*
1880 ** Write the serialized data blob for the value stored in pMem into
1881 ** buf. It is assumed that the caller has allocated sufficient space.
1882 ** Return the number of bytes written.
1883 **
1884 ** nBuf is the amount of space left in buf[].  nBuf must always be
1885 ** large enough to hold the entire field.  Except, if the field is
1886 ** a blob with a zero-filled tail, then buf[] might be just the right
1887 ** size to hold everything except for the zero-filled tail.  If buf[]
1888 ** is only big enough to hold the non-zero prefix, then only write that
1889 ** prefix into buf[].  But if buf[] is large enough to hold both the
1890 ** prefix and the tail then write the prefix and set the tail to all
1891 ** zeros.
1892 **
1893 ** Return the number of bytes actually written into buf[].  The number
1894 ** of bytes in the zero-filled tail is included in the return value only
1895 ** if those bytes were zeroed in buf[].
1896 */
1897 int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
1898   u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
1899   int len;
1900 
1901   /* Integer and Real */
1902   if( serial_type<=7 && serial_type>0 ){
1903     u64 v;
1904     int i;
1905     if( serial_type==7 ){
1906       assert( sizeof(v)==sizeof(pMem->r) );
1907       memcpy(&v, &pMem->r, sizeof(v));
1908       swapMixedEndianFloat(v);
1909     }else{
1910       v = pMem->u.i;
1911     }
1912     len = i = sqlite3VdbeSerialTypeLen(serial_type);
1913     assert( len<=nBuf );
1914     while( i-- ){
1915       buf[i] = (v&0xFF);
1916       v >>= 8;
1917     }
1918     return len;
1919   }
1920 
1921   /* String or blob */
1922   if( serial_type>=12 ){
1923     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
1924              == sqlite3VdbeSerialTypeLen(serial_type) );
1925     assert( pMem->n<=nBuf );
1926     len = pMem->n;
1927     memcpy(buf, pMem->z, len);
1928     if( pMem->flags & MEM_Zero ){
1929       len += pMem->u.i;
1930       if( len>nBuf ){
1931         len = nBuf;
1932       }
1933       memset(&buf[pMem->n], 0, len-pMem->n);
1934     }
1935     return len;
1936   }
1937 
1938   /* NULL or constants 0 or 1 */
1939   return 0;
1940 }
1941 
1942 /*
1943 ** Deserialize the data blob pointed to by buf as serial type serial_type
1944 ** and store the result in pMem.  Return the number of bytes read.
1945 */
1946 int sqlite3VdbeSerialGet(
1947   const unsigned char *buf,     /* Buffer to deserialize from */
1948   u32 serial_type,              /* Serial type to deserialize */
1949   Mem *pMem                     /* Memory cell to write value into */
1950 ){
1951   switch( serial_type ){
1952     case 10:   /* Reserved for future use */
1953     case 11:   /* Reserved for future use */
1954     case 0: {  /* NULL */
1955       pMem->flags = MEM_Null;
1956       break;
1957     }
1958     case 1: { /* 1-byte signed integer */
1959       pMem->u.i = (signed char)buf[0];
1960       pMem->flags = MEM_Int;
1961       return 1;
1962     }
1963     case 2: { /* 2-byte signed integer */
1964       pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
1965       pMem->flags = MEM_Int;
1966       return 2;
1967     }
1968     case 3: { /* 3-byte signed integer */
1969       pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1970       pMem->flags = MEM_Int;
1971       return 3;
1972     }
1973     case 4: { /* 4-byte signed integer */
1974       pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1975       pMem->flags = MEM_Int;
1976       return 4;
1977     }
1978     case 5: { /* 6-byte signed integer */
1979       u64 x = (((signed char)buf[0])<<8) | buf[1];
1980       u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1981       x = (x<<32) | y;
1982       pMem->u.i = *(i64*)&x;
1983       pMem->flags = MEM_Int;
1984       return 6;
1985     }
1986     case 6:   /* 8-byte signed integer */
1987     case 7: { /* IEEE floating point */
1988       u64 x;
1989       u32 y;
1990 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
1991       /* Verify that integers and floating point values use the same
1992       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
1993       ** defined that 64-bit floating point values really are mixed
1994       ** endian.
1995       */
1996       static const u64 t1 = ((u64)0x3ff00000)<<32;
1997       static const double r1 = 1.0;
1998       u64 t2 = t1;
1999       swapMixedEndianFloat(t2);
2000       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
2001 #endif
2002 
2003       x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
2004       y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
2005       x = (x<<32) | y;
2006       if( serial_type==6 ){
2007         pMem->u.i = *(i64*)&x;
2008         pMem->flags = MEM_Int;
2009       }else{
2010         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
2011         swapMixedEndianFloat(x);
2012         memcpy(&pMem->r, &x, sizeof(x));
2013         pMem->flags = MEM_Real;
2014       }
2015       return 8;
2016     }
2017     case 8:    /* Integer 0 */
2018     case 9: {  /* Integer 1 */
2019       pMem->u.i = serial_type-8;
2020       pMem->flags = MEM_Int;
2021       return 0;
2022     }
2023     default: {
2024       int len = (serial_type-12)/2;
2025       pMem->z = (char *)buf;
2026       pMem->n = len;
2027       pMem->xDel = 0;
2028       if( serial_type&0x01 ){
2029         pMem->flags = MEM_Str | MEM_Ephem;
2030       }else{
2031         pMem->flags = MEM_Blob | MEM_Ephem;
2032       }
2033       return len;
2034     }
2035   }
2036   return 0;
2037 }
2038 
2039 /*
2040 ** The header of a record consists of a sequence variable-length integers.
2041 ** These integers are almost always small and are encoded as a single byte.
2042 ** The following macro takes advantage this fact to provide a fast decode
2043 ** of the integers in a record header.  It is faster for the common case
2044 ** where the integer is a single byte.  It is a little slower when the
2045 ** integer is two or more bytes.  But overall it is faster.
2046 **
2047 ** The following expressions are equivalent:
2048 **
2049 **     x = sqlite3GetVarint32( A, &B );
2050 **
2051 **     x = GetVarint( A, B );
2052 **
2053 */
2054 #define GetVarint(A,B)  ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
2055 
2056 /*
2057 ** This function compares the two table rows or index records specified by
2058 ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
2059 ** or positive integer if {nKey1, pKey1} is less than, equal to or
2060 ** greater than {nKey2, pKey2}.  Both Key1 and Key2 must be byte strings
2061 ** composed by the OP_MakeRecord opcode of the VDBE.
2062 */
2063 int sqlite3VdbeRecordCompare(
2064   void *userData,
2065   int nKey1, const void *pKey1,
2066   int nKey2, const void *pKey2
2067 ){
2068   KeyInfo *pKeyInfo = (KeyInfo*)userData;
2069   u32 d1, d2;          /* Offset into aKey[] of next data element */
2070   u32 idx1, idx2;      /* Offset into aKey[] of next header element */
2071   u32 szHdr1, szHdr2;  /* Number of bytes in header */
2072   int i = 0;
2073   int nField;
2074   int rc = 0;
2075   const unsigned char *aKey1 = (const unsigned char *)pKey1;
2076   const unsigned char *aKey2 = (const unsigned char *)pKey2;
2077 
2078   Mem mem1;
2079   Mem mem2;
2080   mem1.enc = pKeyInfo->enc;
2081   mem1.db = pKeyInfo->db;
2082   mem2.enc = pKeyInfo->enc;
2083   mem2.db = pKeyInfo->db;
2084 
2085   idx1 = GetVarint(aKey1, szHdr1);
2086   d1 = szHdr1;
2087   idx2 = GetVarint(aKey2, szHdr2);
2088   d2 = szHdr2;
2089   nField = pKeyInfo->nField;
2090   while( idx1<szHdr1 && idx2<szHdr2 ){
2091     u32 serial_type1;
2092     u32 serial_type2;
2093 
2094     /* Read the serial types for the next element in each key. */
2095     idx1 += GetVarint( aKey1+idx1, serial_type1 );
2096     if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
2097     idx2 += GetVarint( aKey2+idx2, serial_type2 );
2098     if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
2099 
2100     /* Extract the values to be compared.
2101     */
2102     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
2103     d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
2104 
2105     /* Do the comparison
2106     */
2107     rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
2108     if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
2109     if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
2110     if( rc!=0 ){
2111       break;
2112     }
2113     i++;
2114   }
2115 
2116   /* One of the keys ran out of fields, but all the fields up to that point
2117   ** were equal. If the incrKey flag is true, then the second key is
2118   ** treated as larger.
2119   */
2120   if( rc==0 ){
2121     if( pKeyInfo->incrKey ){
2122       rc = -1;
2123     }else if( !pKeyInfo->prefixIsEqual ){
2124       if( d1<nKey1 ){
2125         rc = 1;
2126       }else if( d2<nKey2 ){
2127         rc = -1;
2128       }
2129     }
2130   }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
2131                && pKeyInfo->aSortOrder[i] ){
2132     rc = -rc;
2133   }
2134 
2135   return rc;
2136 }
2137 
2138 /*
2139 ** The argument is an index entry composed using the OP_MakeRecord opcode.
2140 ** The last entry in this record should be an integer (specifically
2141 ** an integer rowid).  This routine returns the number of bytes in
2142 ** that integer.
2143 */
2144 int sqlite3VdbeIdxRowidLen(const u8 *aKey){
2145   u32 szHdr;        /* Size of the header */
2146   u32 typeRowid;    /* Serial type of the rowid */
2147 
2148   sqlite3GetVarint32(aKey, &szHdr);
2149   sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
2150   return sqlite3VdbeSerialTypeLen(typeRowid);
2151 }
2152 
2153 
2154 /*
2155 ** pCur points at an index entry created using the OP_MakeRecord opcode.
2156 ** Read the rowid (the last field in the record) and store it in *rowid.
2157 ** Return SQLITE_OK if everything works, or an error code otherwise.
2158 */
2159 int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
2160   i64 nCellKey = 0;
2161   int rc;
2162   u32 szHdr;        /* Size of the header */
2163   u32 typeRowid;    /* Serial type of the rowid */
2164   u32 lenRowid;     /* Size of the rowid */
2165   Mem m, v;
2166 
2167   sqlite3BtreeKeySize(pCur, &nCellKey);
2168   if( nCellKey<=0 ){
2169     return SQLITE_CORRUPT_BKPT;
2170   }
2171   rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
2172   if( rc ){
2173     return rc;
2174   }
2175   sqlite3GetVarint32((u8*)m.z, &szHdr);
2176   sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
2177   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
2178   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
2179   *rowid = v.u.i;
2180   sqlite3VdbeMemRelease(&m);
2181   return SQLITE_OK;
2182 }
2183 
2184 /*
2185 ** Compare the key of the index entry that cursor pC is point to against
2186 ** the key string in pKey (of length nKey).  Write into *pRes a number
2187 ** that is negative, zero, or positive if pC is less than, equal to,
2188 ** or greater than pKey.  Return SQLITE_OK on success.
2189 **
2190 ** pKey is either created without a rowid or is truncated so that it
2191 ** omits the rowid at the end.  The rowid at the end of the index entry
2192 ** is ignored as well.
2193 */
2194 int sqlite3VdbeIdxKeyCompare(
2195   Cursor *pC,                 /* The cursor to compare against */
2196   int nKey, const u8 *pKey,   /* The key to compare */
2197   int *res                    /* Write the comparison result here */
2198 ){
2199   i64 nCellKey = 0;
2200   int rc;
2201   BtCursor *pCur = pC->pCursor;
2202   int lenRowid;
2203   Mem m;
2204 
2205   sqlite3BtreeKeySize(pCur, &nCellKey);
2206   if( nCellKey<=0 ){
2207     *res = 0;
2208     return SQLITE_OK;
2209   }
2210   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
2211   if( rc ){
2212     return rc;
2213   }
2214   lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
2215   *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
2216   sqlite3VdbeMemRelease(&m);
2217   return SQLITE_OK;
2218 }
2219 
2220 /*
2221 ** This routine sets the value to be returned by subsequent calls to
2222 ** sqlite3_changes() on the database handle 'db'.
2223 */
2224 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
2225   assert( sqlite3_mutex_held(db->mutex) );
2226   db->nChange = nChange;
2227   db->nTotalChange += nChange;
2228 }
2229 
2230 /*
2231 ** Set a flag in the vdbe to update the change counter when it is finalised
2232 ** or reset.
2233 */
2234 void sqlite3VdbeCountChanges(Vdbe *v){
2235   v->changeCntOn = 1;
2236 }
2237 
2238 /*
2239 ** Mark every prepared statement associated with a database connection
2240 ** as expired.
2241 **
2242 ** An expired statement means that recompilation of the statement is
2243 ** recommend.  Statements expire when things happen that make their
2244 ** programs obsolete.  Removing user-defined functions or collating
2245 ** sequences, or changing an authorization function are the types of
2246 ** things that make prepared statements obsolete.
2247 */
2248 void sqlite3ExpirePreparedStatements(sqlite3 *db){
2249   Vdbe *p;
2250   for(p = db->pVdbe; p; p=p->pNext){
2251     p->expired = 1;
2252   }
2253 }
2254 
2255 /*
2256 ** Return the database associated with the Vdbe.
2257 */
2258 sqlite3 *sqlite3VdbeDb(Vdbe *v){
2259   return v->db;
2260 }
2261