Below is the file 'sqlite/vdbe.c' from this revision. You can also download the file.
/* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** The code in this file implements execution method of the ** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c") ** handles housekeeping details such as creating and deleting ** VDBE instances. This file is solely interested in executing ** the VDBE program. ** ** In the external interface, an "sqlite3_stmt*" is an opaque pointer ** to a VDBE. ** ** The SQL parser generates a program which is then executed by ** the VDBE to do the work of the SQL statement. VDBE programs are ** similar in form to assembly language. The program consists of ** a linear sequence of operations. Each operation has an opcode ** and 3 operands. Operands P1 and P2 are integers. Operand P3 ** is a null-terminated string. The P2 operand must be non-negative. ** Opcodes will typically ignore one or more operands. Many opcodes ** ignore all three operands. ** ** Computation results are stored on a stack. Each entry on the ** stack is either an integer, a null-terminated string, a floating point ** number, or the SQL "NULL" value. An inplicit conversion from one ** type to the other occurs as necessary. ** ** Most of the code in this file is taken up by the sqlite3VdbeExec() ** function which does the work of interpreting a VDBE program. ** But other routines are also provided to help in building up ** a program instruction by instruction. ** ** Various scripts scan this source file in order to generate HTML ** documentation, headers files, or other derived files. The formatting ** of the code in this file is, therefore, important. See other comments ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** ** $Id: vdbe.c,v 1.491 2005/09/20 17:42:23 drh Exp $ */ #include "sqliteInt.h" #include "os.h" #include <ctype.h> #include "vdbeInt.h" /* ** The following global variable is incremented every time a cursor ** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test ** procedures use this information to make sure that indices are ** working correctly. This variable has no function other than to ** help verify the correct operation of the library. */ int sqlite3_search_count = 0; /* ** When this global variable is positive, it gets decremented once before ** each instruction in the VDBE. When reaches zero, the SQLITE_Interrupt ** of the db.flags field is set in order to simulate and interrupt. ** ** This facility is used for testing purposes only. It does not function ** in an ordinary build. */ int sqlite3_interrupt_count = 0; /* ** The next global variable is incremented each type the OP_Sort opcode ** is executed. The test procedures use this information to make sure that ** sorting is occurring or not occuring at appropriate times. This variable ** has no function other than to help verify the correct operation of the ** library. */ int sqlite3_sort_count = 0; /* ** Release the memory associated with the given stack level. This ** leaves the Mem.flags field in an inconsistent state. */ #define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); } /* ** Convert the given stack entity into a string if it isn't one ** already. Return non-zero if a malloc() fails. */ #define Stringify(P, enc) \ if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \ { goto no_mem; } /* ** Convert the given stack entity into a string that has been obtained ** from sqliteMalloc(). This is different from Stringify() above in that ** Stringify() will use the NBFS bytes of static string space if the string ** will fit but this routine always mallocs for space. ** Return non-zero if we run out of memory. */ #define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P) /* ** An ephemeral string value (signified by the MEM_Ephem flag) contains ** a pointer to a dynamically allocated string where some other entity ** is responsible for deallocating that string. Because the stack entry ** does not control the string, it might be deleted without the stack ** entry knowing it. ** ** This routine converts an ephemeral string into a dynamically allocated ** string that the stack entry itself controls. In other words, it ** converts an MEM_Ephem string into an MEM_Dyn string. */ #define Deephemeralize(P) \ if( ((P)->flags&MEM_Ephem)!=0 \ && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} /* ** Convert the given stack entity into a integer if it isn't one ** already. ** ** Any prior string or real representation is invalidated. ** NULLs are converted into 0. */ #define Integerify(P) sqlite3VdbeMemIntegerify(P) /* ** Convert P so that it has type MEM_Real. ** ** Any prior string or integer representation is invalidated. ** NULLs are converted into 0.0. */ #define Realify(P) sqlite3VdbeMemRealify(P) /* ** Argument pMem points at a memory cell that will be passed to a ** user-defined function or returned to the user as the result of a query. ** The second argument, 'db_enc' is the text encoding used by the vdbe for ** stack variables. This routine sets the pMem->enc and pMem->type ** variables used by the sqlite3_value_*() routines. */ #define storeTypeInfo(A,B) _storeTypeInfo(A) static void _storeTypeInfo(Mem *pMem){ int flags = pMem->flags; if( flags & MEM_Null ){ pMem->type = SQLITE_NULL; } else if( flags & MEM_Int ){ pMem->type = SQLITE_INTEGER; } else if( flags & MEM_Real ){ pMem->type = SQLITE_FLOAT; } else if( flags & MEM_Str ){ pMem->type = SQLITE_TEXT; }else{ pMem->type = SQLITE_BLOB; } } /* ** Pop the stack N times. */ static void popStack(Mem **ppTos, int N){ Mem *pTos = *ppTos; while( N>0 ){ N--; Release(pTos); pTos--; } *ppTos = pTos; } /* ** Allocate cursor number iCur. Return a pointer to it. Return NULL ** if we run out of memory. */ static Cursor *allocateCursor(Vdbe *p, int iCur){ Cursor *pCx; assert( iCur<p->nCursor ); if( p->apCsr[iCur] ){ sqlite3VdbeFreeCursor(p->apCsr[iCur]); } p->apCsr[iCur] = pCx = sqliteMalloc( sizeof(Cursor) ); return pCx; } /* ** Apply any conversion required by the supplied column affinity to ** memory cell pRec. affinity may be one of: ** ** SQLITE_AFF_NUMERIC ** SQLITE_AFF_TEXT ** SQLITE_AFF_NONE ** SQLITE_AFF_INTEGER ** */ static void applyAffinity(Mem *pRec, char affinity, u8 enc){ if( affinity==SQLITE_AFF_NONE ){ /* do nothing */ }else if( affinity==SQLITE_AFF_TEXT ){ /* Only attempt the conversion to TEXT if there is an integer or real ** representation (blob and NULL do not get converted) but no string ** representation. */ if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ sqlite3VdbeMemStringify(pRec, enc); } pRec->flags &= ~(MEM_Real|MEM_Int); }else{ if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){ /* pRec does not have a valid integer or real representation. ** Attempt a conversion if pRec has a string representation and ** it looks like a number. */ int realnum; sqlite3VdbeMemNulTerminate(pRec); if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum, enc) ){ if( realnum ){ Realify(pRec); }else{ Integerify(pRec); } } } if( affinity==SQLITE_AFF_INTEGER ){ /* For INTEGER affinity, try to convert a real value to an int */ if( (pRec->flags&MEM_Real) && !(pRec->flags&MEM_Int) ){ pRec->i = pRec->r; if( ((double)pRec->i)==pRec->r ){ pRec->flags |= MEM_Int; } } } } } /* ** Exported version of applyAffinity(). This one works on sqlite3_value*, ** not the internal Mem* type. */ void sqlite3ValueApplyAffinity(sqlite3_value *pVal, u8 affinity, u8 enc){ applyAffinity((Mem *)pVal, affinity, enc); } #ifdef SQLITE_DEBUG /* ** Write a nice string representation of the contents of cell pMem ** into buffer zBuf, length nBuf. */ void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf, int nBuf){ char *zCsr = zBuf; int f = pMem->flags; static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; if( f&MEM_Blob ){ int i; char c; if( f & MEM_Dyn ){ c = 'z'; assert( (f & (MEM_Static|MEM_Ephem))==0 ); }else if( f & MEM_Static ){ c = 't'; assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); }else if( f & MEM_Ephem ){ c = 'e'; assert( (f & (MEM_Static|MEM_Dyn))==0 ); }else{ c = 's'; } zCsr += sprintf(zCsr, "%c", c); zCsr += sprintf(zCsr, "%d[", pMem->n); for(i=0; i<16 && i<pMem->n; i++){ zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF)); } for(i=0; i<16 && i<pMem->n; i++){ char z = pMem->z[i]; if( z<32 || z>126 ) *zCsr++ = '.'; else *zCsr++ = z; } zCsr += sprintf(zCsr, "]"); *zCsr = '\0'; }else if( f & MEM_Str ){ int j, k; zBuf[0] = ' '; if( f & MEM_Dyn ){ zBuf[1] = 'z'; assert( (f & (MEM_Static|MEM_Ephem))==0 ); }else if( f & MEM_Static ){ zBuf[1] = 't'; assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); }else if( f & MEM_Ephem ){ zBuf[1] = 'e'; assert( (f & (MEM_Static|MEM_Dyn))==0 ); }else{ zBuf[1] = 's'; } k = 2; k += sprintf(&zBuf[k], "%d", pMem->n); zBuf[k++] = '['; for(j=0; j<15 && j<pMem->n; j++){ u8 c = pMem->z[j]; if( c>=0x20 && c<0x7f ){ zBuf[k++] = c; }else{ zBuf[k++] = '.'; } } zBuf[k++] = ']'; k += sprintf(&zBuf[k], encnames[pMem->enc]); zBuf[k++] = 0; } } #endif #ifdef VDBE_PROFILE /* ** The following routine only works on pentium-class processors. ** It uses the RDTSC opcode to read the cycle count value out of the ** processor and returns that value. This can be used for high-res ** profiling. */ __inline__ unsigned long long int hwtime(void){ unsigned long long int x; __asm__("rdtsc\n\t" "mov %%edx, %%ecx\n\t" :"=A" (x)); return x; } #endif /* ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the ** sqlite3_interrupt() routine has been called. If it has been, then ** processing of the VDBE program is interrupted. ** ** This macro added to every instruction that does a jump in order to ** implement a loop. This test used to be on every single instruction, ** but that meant we more testing that we needed. By only testing the ** flag on jump instructions, we get a (small) speed improvement. */ #define CHECK_FOR_INTERRUPT \ if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt; /* ** Execute as much of a VDBE program as we can then return. ** ** sqlite3VdbeMakeReady() must be called before this routine in order to ** close the program with a final OP_Halt and to set up the callbacks ** and the error message pointer. ** ** Whenever a row or result data is available, this routine will either ** invoke the result callback (if there is one) or return with ** SQLITE_ROW. ** ** If an attempt is made to open a locked database, then this routine ** will either invoke the busy callback (if there is one) or it will ** return SQLITE_BUSY. ** ** If an error occurs, an error message is written to memory obtained ** from sqliteMalloc() and p->zErrMsg is made to point to that memory. ** The error code is stored in p->rc and this routine returns SQLITE_ERROR. ** ** If the callback ever returns non-zero, then the program exits ** immediately. There will be no error message but the p->rc field is ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR. ** ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this ** routine to return SQLITE_ERROR. ** ** Other fatal errors return SQLITE_ERROR. ** ** After this routine has finished, sqlite3VdbeFinalize() should be ** used to clean up the mess that was left behind. */ int sqlite3VdbeExec( Vdbe *p /* The VDBE */ ){ int pc; /* The program counter */ Op *pOp; /* Current operation */ int rc = SQLITE_OK; /* Value to return */ sqlite3 *db = p->db; /* The database */ Mem *pTos; /* Top entry in the operand stack */ #ifdef VDBE_PROFILE unsigned long long start; /* CPU clock count at start of opcode */ int origPc; /* Program counter at start of opcode */ #endif #ifndef SQLITE_OMIT_PROGRESS_CALLBACK int nProgressOps = 0; /* Opcodes executed since progress callback. */ #endif #ifndef NDEBUG Mem *pStackLimit; #endif if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; assert( db->magic==SQLITE_MAGIC_BUSY ); assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); p->rc = SQLITE_OK; assert( p->explain==0 ); pTos = p->pTos; if( sqlite3_malloc_failed ) goto no_mem; if( p->popStack ){ popStack(&pTos, p->popStack); p->popStack = 0; } p->resOnStack = 0; db->busyHandler.nBusy = 0; CHECK_FOR_INTERRUPT; for(pc=p->pc; rc==SQLITE_OK; pc++){ assert( pc>=0 && pc<p->nOp ); assert( pTos<=&p->aStack[pc] ); if( sqlite3_malloc_failed ) goto no_mem; #ifdef VDBE_PROFILE origPc = pc; start = hwtime(); #endif pOp = &p->aOp[pc]; /* Only allow tracing if SQLITE_DEBUG is defined. */ #ifdef SQLITE_DEBUG if( p->trace ){ if( pc==0 ){ printf("VDBE Execution Trace:\n"); sqlite3VdbePrintSql(p); } sqlite3VdbePrintOp(p->trace, pc, pOp); } if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){ sqlite3VdbePrintSql(p); } #endif /* Check to see if we need to simulate an interrupt. This only happens ** if we have a special test build. */ #ifdef SQLITE_TEST if( sqlite3_interrupt_count>0 ){ sqlite3_interrupt_count--; if( sqlite3_interrupt_count==0 ){ sqlite3_interrupt(db); } } #endif #ifndef SQLITE_OMIT_PROGRESS_CALLBACK /* Call the progress callback if it is configured and the required number ** of VDBE ops have been executed (either since this invocation of ** sqlite3VdbeExec() or since last time the progress callback was called). ** If the progress callback returns non-zero, exit the virtual machine with ** a return code SQLITE_ABORT. */ if( db->xProgress ){ if( db->nProgressOps==nProgressOps ){ if( db->xProgress(db->pProgressArg)!=0 ){ rc = SQLITE_ABORT; continue; /* skip to the next iteration of the for loop */ } nProgressOps = 0; } nProgressOps++; } #endif #ifndef NDEBUG /* This is to check that the return value of static function ** opcodeNoPush() (see vdbeaux.c) returns values that match the ** implementation of the virtual machine in this file. If ** opcodeNoPush() returns non-zero, then the stack is guarenteed ** not to grow when the opcode is executed. If it returns zero, then ** the stack may grow by at most 1. ** ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not ** available if NDEBUG is defined at build time. */ pStackLimit = pTos; if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){ pStackLimit++; } #endif switch( pOp->opcode ){ /***************************************************************************** ** What follows is a massive switch statement where each case implements a ** separate instruction in the virtual machine. If we follow the usual ** indentation conventions, each case should be indented by 6 spaces. But ** that is a lot of wasted space on the left margin. So the code within ** the switch statement will break with convention and be flush-left. Another ** big comment (similar to this one) will mark the point in the code where ** we transition back to normal indentation. ** ** The formatting of each case is important. The makefile for SQLite ** generates two C files "opcodes.h" and "opcodes.c" by scanning this ** file looking for lines that begin with "case OP_". The opcodes.h files ** will be filled with #defines that give unique integer values to each ** opcode and the opcodes.c file is filled with an array of strings where ** each string is the symbolic name for the corresponding opcode. If the ** case statement is followed by a comment of the form "/# same as ... #/" ** that comment is used to determine the particular value of the opcode. ** ** If a comment on the same line as the "case OP_" construction contains ** the word "no-push", then the opcode is guarenteed not to grow the ** vdbe stack when it is executed. See function opcode() in ** vdbeaux.c for details. ** ** Documentation about VDBE opcodes is generated by scanning this file ** for lines of that contain "Opcode:". That line and all subsequent ** comment lines are used in the generation of the opcode.html documentation ** file. ** ** SUMMARY: ** ** Formatting is important to scripts that scan this file. ** Do not deviate from the formatting style currently in use. ** *****************************************************************************/ /* Opcode: Goto * P2 * ** ** An unconditional jump to address P2. ** The next instruction executed will be ** the one at index P2 from the beginning of ** the program. */ case OP_Goto: { /* no-push */ CHECK_FOR_INTERRUPT; pc = pOp->p2 - 1; break; } /* Opcode: Gosub * P2 * ** ** Push the current address plus 1 onto the return address stack ** and then jump to address P2. ** ** The return address stack is of limited depth. If too many ** OP_Gosub operations occur without intervening OP_Returns, then ** the return address stack will fill up and processing will abort ** with a fatal error. */ case OP_Gosub: { /* no-push */ assert( p->returnDepth<sizeof(p->returnStack)/sizeof(p->returnStack[0]) ); p->returnStack[p->returnDepth++] = pc+1; pc = pOp->p2 - 1; break; } /* Opcode: Return * * * ** ** Jump immediately to the next instruction after the last unreturned ** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then ** processing aborts with a fatal error. */ case OP_Return: { /* no-push */ assert( p->returnDepth>0 ); p->returnDepth--; pc = p->returnStack[p->returnDepth] - 1; break; } /* Opcode: Halt P1 P2 P3 ** ** Exit immediately. All open cursors, Fifos, etc are closed ** automatically. ** ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). ** For errors, it can be some other value. If P1!=0 then P2 will determine ** whether or not to rollback the current transaction. Do not rollback ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, ** then back out all changes that have occurred during this execution of the ** VDBE, but do not rollback the transaction. ** ** If P3 is not null then it is an error message string. ** ** There is an implied "Halt 0 0 0" instruction inserted at the very end of ** every program. So a jump past the last instruction of the program ** is the same as executing Halt. */ case OP_Halt: { /* no-push */ p->pTos = pTos; p->rc = pOp->p1; p->pc = pc; p->errorAction = pOp->p2; if( pOp->p3 ){ sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); } rc = sqlite3VdbeHalt(p); assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); if( rc==SQLITE_BUSY ){ p->rc = SQLITE_BUSY; return SQLITE_BUSY; } return p->rc ? SQLITE_ERROR : SQLITE_DONE; } /* Opcode: Integer P1 * * ** ** The 32-bit integer value P1 is pushed onto the stack. */ case OP_Integer: { pTos++; pTos->flags = MEM_Int; pTos->i = pOp->p1; break; } /* Opcode: Int64 * * P3 ** ** P3 is a string representation of an integer. Convert that integer ** to a 64-bit value and push it onto the stack. */ case OP_Int64: { pTos++; assert( pOp->p3!=0 ); pTos->flags = MEM_Str|MEM_Static|MEM_Term; pTos->z = pOp->p3; pTos->n = strlen(pTos->z); pTos->enc = SQLITE_UTF8; pTos->i = sqlite3VdbeIntValue(pTos); pTos->flags |= MEM_Int; break; } /* Opcode: Real * * P3 ** ** The string value P3 is converted to a real and pushed on to the stack. */ case OP_Real: { /* same as TK_FLOAT, */ pTos++; pTos->flags = MEM_Str|MEM_Static|MEM_Term; pTos->z = pOp->p3; pTos->n = strlen(pTos->z); pTos->enc = SQLITE_UTF8; pTos->r = sqlite3VdbeRealValue(pTos); pTos->flags |= MEM_Real; sqlite3VdbeChangeEncoding(pTos, db->enc); break; } /* Opcode: String8 * * P3 ** ** P3 points to a nul terminated UTF-8 string. This opcode is transformed ** into an OP_String before it is executed for the first time. */ case OP_String8: { /* same as TK_STRING */ #ifndef SQLITE_OMIT_UTF16 pOp->opcode = OP_String; assert( pOp->p3!=0 ); if( db->enc!=SQLITE_UTF8 ){ pTos++; sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC); if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, db->enc) ) goto no_mem; if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem; pTos->flags &= ~(MEM_Dyn); pTos->flags |= MEM_Static; if( pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); } pOp->p3type = P3_DYNAMIC; pOp->p3 = pTos->z; break; } #endif /* Otherwise fall through to the next case, OP_String */ } /* Opcode: String * * P3 ** ** The string value P3 is pushed onto the stack. If P3==0 then a ** NULL is pushed onto the stack. P3 is assumed to be a nul terminated ** string encoded with the database native encoding. */ case OP_String: { pTos++; assert( pOp->p3!=0 ); pTos->flags = MEM_Str|MEM_Static|MEM_Term; pTos->z = pOp->p3; #ifndef SQLITE_OMIT_UTF16 if( db->enc==SQLITE_UTF8 ){ pTos->n = strlen(pTos->z); }else{ pTos->n = sqlite3utf16ByteLen(pTos->z, -1); } #else assert( db->enc==SQLITE_UTF8 ); pTos->n = strlen(pTos->z); #endif pTos->enc = db->enc; break; } /* Opcode: Null * * * ** ** Push a NULL onto the stack. */ case OP_Null: { pTos++; pTos->flags = MEM_Null; pTos->n = 0; break; } #ifndef SQLITE_OMIT_BLOB_LITERAL /* Opcode: HexBlob * * P3 ** ** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the ** vdbe stack. ** ** The first time this instruction executes, in transforms itself into a ** 'Blob' opcode with a binary blob as P3. */ case OP_HexBlob: { /* same as TK_BLOB */ pOp->opcode = OP_Blob; pOp->p1 = strlen(pOp->p3)/2; if( pOp->p1 ){ char *zBlob = sqlite3HexToBlob(pOp->p3); if( !zBlob ) goto no_mem; if( pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); } pOp->p3 = zBlob; pOp->p3type = P3_DYNAMIC; }else{ if( pOp->p3type==P3_DYNAMIC ){ sqliteFree(pOp->p3); } pOp->p3type = P3_STATIC; pOp->p3 = ""; } /* Fall through to the next case, OP_Blob. */ } /* Opcode: Blob P1 * P3 ** ** P3 points to a blob of data P1 bytes long. Push this ** value onto the stack. This instruction is not coded directly ** by the compiler. Instead, the compiler layer specifies ** an OP_HexBlob opcode, with the hex string representation of ** the blob as P3. This opcode is transformed to an OP_Blob ** the first time it is executed. */ case OP_Blob: { pTos++; sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0); break; } #endif /* SQLITE_OMIT_BLOB_LITERAL */ /* Opcode: Variable P1 * * ** ** Push the value of variable P1 onto the stack. A variable is ** an unknown in the original SQL string as handed to sqlite3_compile(). ** Any occurance of the '?' character in the original SQL is considered ** a variable. Variables in the SQL string are number from left to ** right beginning with 1. The values of variables are set using the ** sqlite3_bind() API. */ case OP_Variable: { int j = pOp->p1 - 1; assert( j>=0 && j<p->nVar ); pTos++; sqlite3VdbeMemShallowCopy(pTos, &p->aVar[j], MEM_Static); break; } /* Opcode: Pop P1 * * ** ** P1 elements are popped off of the top of stack and discarded. */ case OP_Pop: { /* no-push */ assert( pOp->p1>=0 ); popStack(&pTos, pOp->p1); assert( pTos>=&p->aStack[-1] ); break; } /* Opcode: Dup P1 P2 * ** ** A copy of the P1-th element of the stack ** is made and pushed onto the top of the stack. ** The top of the stack is element 0. So the ** instruction "Dup 0 0 0" will make a copy of the ** top of the stack. ** ** If the content of the P1-th element is a dynamically ** allocated string, then a new copy of that string ** is made if P2==0. If P2!=0, then just a pointer ** to the string is copied. ** ** Also see the Pull instruction. */ case OP_Dup: { Mem *pFrom = &pTos[-pOp->p1]; assert( pFrom<=pTos && pFrom>=p->aStack ); pTos++; sqlite3VdbeMemShallowCopy(pTos, pFrom, MEM_Ephem); if( pOp->p2 ){ Deephemeralize(pTos); } break; } /* Opcode: Pull P1 * * ** ** The P1-th element is removed from its current location on ** the stack and pushed back on top of the stack. The ** top of the stack is element 0, so "Pull 0 0 0" is ** a no-op. "Pull 1 0 0" swaps the top two elements of ** the stack. ** ** See also the Dup instruction. */ case OP_Pull: { /* no-push */ Mem *pFrom = &pTos[-pOp->p1]; int i; Mem ts; ts = *pFrom; Deephemeralize(pTos); for(i=0; i<pOp->p1; i++, pFrom++){ Deephemeralize(&pFrom[1]); assert( (pFrom->flags & MEM_Ephem)==0 ); *pFrom = pFrom[1]; if( pFrom->flags & MEM_Short ){ assert( pFrom->flags & (MEM_Str|MEM_Blob) ); assert( pFrom->z==pFrom[1].zShort ); pFrom->z = pFrom->zShort; } } *pTos = ts; if( pTos->flags & MEM_Short ){ assert( pTos->flags & (MEM_Str|MEM_Blob) ); assert( pTos->z==pTos[-pOp->p1].zShort ); pTos->z = pTos->zShort; } break; } /* Opcode: Push P1 * * ** ** Overwrite the value of the P1-th element down on the ** stack (P1==0 is the top of the stack) with the value ** of the top of the stack. Then pop the top of the stack. */ case OP_Push: { /* no-push */ Mem *pTo = &pTos[-pOp->p1]; assert( pTo>=p->aStack ); sqlite3VdbeMemMove(pTo, pTos); pTos--; break; } /* Opcode: Callback P1 * * ** ** Pop P1 values off the stack and form them into an array. Then ** invoke the callback function using the newly formed array as the ** 3rd parameter. */ case OP_Callback: { /* no-push */ int i; assert( p->nResColumn==pOp->p1 ); for(i=0; i<pOp->p1; i++){ Mem *pVal = &pTos[0-i]; sqlite3VdbeMemNulTerminate(pVal); storeTypeInfo(pVal, db->enc); } p->resOnStack = 1; p->nCallback++; p->popStack = pOp->p1; p->pc = pc + 1; p->pTos = pTos; return SQLITE_ROW; } /* Opcode: Concat P1 P2 * ** ** Look at the first P1+2 elements of the stack. Append them all ** together with the lowest element first. The original P1+2 elements ** are popped from the stack if P2==0 and retained if P2==1. If ** any element of the stack is NULL, then the result is NULL. ** ** When P1==1, this routine makes a copy of the top stack element ** into memory obtained from sqliteMalloc(). */ case OP_Concat: { /* same as TK_CONCAT */ char *zNew; int nByte; int nField; int i, j; Mem *pTerm; /* Loop through the stack elements to see how long the result will be. */ nField = pOp->p1 + 2; pTerm = &pTos[1-nField]; nByte = 0; for(i=0; i<nField; i++, pTerm++){ assert( pOp->p2==0 || (pTerm->flags&MEM_Str) ); if( pTerm->flags&MEM_Null ){ nByte = -1; break; } Stringify(pTerm, db->enc); nByte += pTerm->n; } if( nByte<0 ){ /* If nByte is less than zero, then there is a NULL value on the stack. ** In this case just pop the values off the stack (if required) and ** push on a NULL. */ if( pOp->p2==0 ){ popStack(&pTos, nField); } pTos++; pTos->flags = MEM_Null; }else{ /* Otherwise malloc() space for the result and concatenate all the ** stack values. */ zNew = sqliteMallocRaw( nByte+2 ); if( zNew==0 ) goto no_mem; j = 0; pTerm = &pTos[1-nField]; for(i=j=0; i<nField; i++, pTerm++){ int n = pTerm->n; assert( pTerm->flags & (MEM_Str|MEM_Blob) ); memcpy(&zNew[j], pTerm->z, n); j += n; } zNew[j] = 0; zNew[j+1] = 0; assert( j==nByte ); if( pOp->p2==0 ){ popStack(&pTos, nField); } pTos++; pTos->n = j; pTos->flags = MEM_Str|MEM_Dyn|MEM_Term; pTos->xDel = 0; pTos->enc = db->enc; pTos->z = zNew; } break; } /* Opcode: Add * * * ** ** Pop the top two elements from the stack, add them together, ** and push the result back onto the stack. If either element ** is a string then it is converted to a double using the atof() ** function before the addition. ** If either operand is NULL, the result is NULL. */ /* Opcode: Multiply * * * ** ** Pop the top two elements from the stack, multiply them together, ** and push the result back onto the stack. If either element ** is a string then it is converted to a double using the atof() ** function before the multiplication. ** If either operand is NULL, the result is NULL. */ /* Opcode: Subtract * * * ** ** Pop the top two elements from the stack, subtract the ** first (what was on top of the stack) from the second (the ** next on stack) ** and push the result back onto the stack. If either element ** is a string then it is converted to a double using the atof() ** function before the subtraction. ** If either operand is NULL, the result is NULL. */ /* Opcode: Divide * * * ** ** Pop the top two elements from the stack, divide the ** first (what was on top of the stack) from the second (the ** next on stack) ** and push the result back onto the stack. If either element ** is a string then it is converted to a double using the atof() ** function before the division. Division by zero returns NULL. ** If either operand is NULL, the result is NULL. */ /* Opcode: Remainder * * * ** ** Pop the top two elements from the stack, divide the ** first (what was on top of the stack) from the second (the ** next on stack) ** and push the remainder after division onto the stack. If either element ** is a string then it is converted to a double using the atof() ** function before the division. Division by zero returns NULL. ** If either operand is NULL, the result is NULL. */ case OP_Add: /* same as TK_PLUS, no-push */ case OP_Subtract: /* same as TK_MINUS, no-push */ case OP_Multiply: /* same as TK_STAR, no-push */ case OP_Divide: /* same as TK_SLASH, no-push */ case OP_Remainder: { /* same as TK_REM, no-push */ Mem *pNos = &pTos[-1]; assert( pNos>=p->aStack ); if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){ Release(pTos); pTos--; Release(pTos); pTos->flags = MEM_Null; }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){ i64 a, b; a = pTos->i; b = pNos->i; switch( pOp->opcode ){ case OP_Add: b += a; break; case OP_Subtract: b -= a; break; case OP_Multiply: b *= a; break; case OP_Divide: { if( a==0 ) goto divide_by_zero; b /= a; break; } default: { if( a==0 ) goto divide_by_zero; b %= a; break; } } Release(pTos); pTos--; Release(pTos); pTos->i = b; pTos->flags = MEM_Int; }else{ double a, b; a = sqlite3VdbeRealValue(pTos); b = sqlite3VdbeRealValue(pNos); switch( pOp->opcode ){ case OP_Add: b += a; break; case OP_Subtract: b -= a; break; case OP_Multiply: b *= a; break; case OP_Divide: { if( a==0.0 ) goto divide_by_zero; b /= a; break; } default: { int ia = (int)a; int ib = (int)b; if( ia==0.0 ) goto divide_by_zero; b = ib % ia; break; } } Release(pTos); pTos--; Release(pTos); pTos->r = b; pTos->flags = MEM_Real; } break; divide_by_zero: Release(pTos); pTos--; Release(pTos); pTos->flags = MEM_Null; break; } /* Opcode: CollSeq * * P3 ** ** P3 is a pointer to a CollSeq struct. If the next call to a user function ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will ** be returned. This is used by the built-in min(), max() and nullif() ** functions. ** ** The interface used by the implementation of the aforementioned functions ** to retrieve the collation sequence set by this opcode is not available ** publicly, only to user functions defined in func.c. */ case OP_CollSeq: { /* no-push */ assert( pOp->p3type==P3_COLLSEQ ); break; } /* Opcode: Function P1 P2 P3 ** ** Invoke a user function (P3 is a pointer to a Function structure that ** defines the function) with P2 arguments taken from the stack. Pop all ** arguments from the stack and push back the result. ** ** P1 is a 32-bit bitmask indicating whether or not each argument to the ** function was determined to be constant at compile time. If the first ** argument was constant then bit 0 of P1 is set. This is used to determine ** whether meta data associated with a user function argument using the ** sqlite3_set_auxdata() API may be safely retained until the next ** invocation of this opcode. ** ** See also: AggStep and AggFinal */ case OP_Function: { int i; Mem *pArg; sqlite3_context ctx; sqlite3_value **apVal; int n = pOp->p2; apVal = p->apArg; assert( apVal || n==0 ); pArg = &pTos[1-n]; for(i=0; i<n; i++, pArg++){ apVal[i] = pArg; storeTypeInfo(pArg, db->enc); } assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC ); if( pOp->p3type==P3_FUNCDEF ){ ctx.pFunc = (FuncDef*)pOp->p3; ctx.pVdbeFunc = 0; }else{ ctx.pVdbeFunc = (VdbeFunc*)pOp->p3; ctx.pFunc = ctx.pVdbeFunc->pFunc; } ctx.s.flags = MEM_Null; ctx.s.z = 0; ctx.s.xDel = 0; ctx.isError = 0; if( ctx.pFunc->needCollSeq ){ assert( pOp>p->aOp ); assert( pOp[-1].p3type==P3_COLLSEQ ); assert( pOp[-1].opcode==OP_CollSeq ); ctx.pColl = (CollSeq *)pOp[-1].p3; } if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; (*ctx.pFunc->xFunc)(&ctx, n, apVal); if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; if( sqlite3_malloc_failed ) goto no_mem; popStack(&pTos, n); /* If any auxilary data functions have been called by this user function, ** immediately call the destructor for any non-static values. */ if( ctx.pVdbeFunc ){ sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); pOp->p3 = (char *)ctx.pVdbeFunc; pOp->p3type = P3_VDBEFUNC; } /* Copy the result of the function to the top of the stack */ sqlite3VdbeChangeEncoding(&ctx.s, db->enc); pTos++; pTos->flags = 0; sqlite3VdbeMemMove(pTos, &ctx.s); /* If the function returned an error, throw an exception */ if( ctx.isError ){ if( !(pTos->flags&MEM_Str) ){ sqlite3SetString(&p->zErrMsg, "user function error", (char*)0); }else{ sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pTos), (char*)0); sqlite3VdbeChangeEncoding(pTos, db->enc); } rc = SQLITE_ERROR; } break; } /* Opcode: BitAnd * * * ** ** Pop the top two elements from the stack. Convert both elements ** to integers. Push back onto the stack the bit-wise AND of the ** two elements. ** If either operand is NULL, the result is NULL. */ /* Opcode: BitOr * * * ** ** Pop the top two elements from the stack. Convert both elements ** to integers. Push back onto the stack the bit-wise OR of the ** two elements. ** If either operand is NULL, the result is NULL. */ /* Opcode: ShiftLeft * * * ** ** Pop the top two elements from the stack. Convert both elements ** to integers. Push back onto the stack the second element shifted ** left by N bits where N is the top element on the stack. ** If either operand is NULL, the result is NULL. */ /* Opcode: ShiftRight * * * ** ** Pop the top two elements from the stack. Convert both elements ** to integers. Push back onto the stack the second element shifted ** right by N bits where N is the top element on the stack. ** If either operand is NULL, the result is NULL. */ case OP_BitAnd: /* same as TK_BITAND, no-push */ case OP_BitOr: /* same as TK_BITOR, no-push */ case OP_ShiftLeft: /* same as TK_LSHIFT, no-push */ case OP_ShiftRight: { /* same as TK_RSHIFT, no-push */ Mem *pNos = &pTos[-1]; int a, b; assert( pNos>=p->aStack ); if( (pTos->flags | pNos->flags) & MEM_Null ){ popStack(&pTos, 2); pTos++; pTos->flags = MEM_Null; break; } a = sqlite3VdbeIntValue(pNos); b = sqlite3VdbeIntValue(pTos); switch( pOp->opcode ){ case OP_BitAnd: a &= b; break; case OP_BitOr: a |= b; break; case OP_ShiftLeft: a <<= b; break; case OP_ShiftRight: a >>= b; break; default: /* CANT HAPPEN */ break; } Release(pTos); pTos--; Release(pTos); pTos->i = a; pTos->flags = MEM_Int; break; } /* Opcode: AddImm P1 * * ** ** Add the value P1 to whatever is on top of the stack. The result ** is always an integer. ** ** To force the top of the stack to be an integer, just add 0. */ case OP_AddImm: { /* no-push */ assert( pTos>=p->aStack ); Integerify(pTos); pTos->i += pOp->p1; break; } /* Opcode: ForceInt P1 P2 * ** ** Convert the top of the stack into an integer. If the current top of ** the stack is not numeric (meaning that is is a NULL or a string that ** does not look like an integer or floating point number) then pop the ** stack and jump to P2. If the top of the stack is numeric then ** convert it into the least integer that is greater than or equal to its ** current value if P1==0, or to the least integer that is strictly ** greater than its current value if P1==1. */ case OP_ForceInt: { /* no-push */ i64 v; assert( pTos>=p->aStack ); applyAffinity(pTos, SQLITE_AFF_INTEGER, db->enc); if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){ Release(pTos); pTos--; pc = pOp->p2 - 1; break; } if( pTos->flags & MEM_Int ){ v = pTos->i + (pOp->p1!=0); }else{ Realify(pTos); v = (int)pTos->r; if( pTos->r>(double)v ) v++; if( pOp->p1 && pTos->r==(double)v ) v++; } Release(pTos); pTos->i = v; pTos->flags = MEM_Int; break; } /* Opcode: MustBeInt P1 P2 * ** ** Force the top of the stack to be an integer. If the top of the ** stack is not an integer and cannot be converted into an integer ** with out data loss, then jump immediately to P2, or if P2==0 ** raise an SQLITE_MISMATCH exception. ** ** If the top of the stack is not an integer and P2 is not zero and ** P1 is 1, then the stack is popped. In all other cases, the depth ** of the stack is unchanged. */ case OP_MustBeInt: { /* no-push */ assert( pTos>=p->aStack ); applyAffinity(pTos, SQLITE_AFF_INTEGER, db->enc); if( (pTos->flags & MEM_Int)==0 ){ if( pOp->p2==0 ){ rc = SQLITE_MISMATCH; goto abort_due_to_error; }else{ if( pOp->p1 ) popStack(&pTos, 1); pc = pOp->p2 - 1; } }else{ Release(pTos); pTos->flags = MEM_Int; } break; } #ifndef SQLITE_OMIT_CAST /* Opcode: ToInt * * * ** ** Force the value on the top of the stack to be an integer. If ** The value is currently a real number, drop its fractional part. ** If the value is text or blob, try to convert it to an integer using the ** equivalent of atoi() and store 0 if no such conversion is possible. ** ** A NULL value is not changed by this routine. It remains NULL. */ case OP_ToInt: { /* no-push */ assert( pTos>=p->aStack ); if( pTos->flags & MEM_Null ) break; assert( MEM_Str==(MEM_Blob>>3) ); pTos->flags |= (pTos->flags&MEM_Blob)>>3; applyAffinity(pTos, SQLITE_AFF_INTEGER, db->enc); sqlite3VdbeMemIntegerify(pTos); break; } /* Opcode: ToNumeric * * * ** ** Force the value on the top of the stack to be numeric (either an ** integer or a floating-point number. ** If the value is text or blob, try to convert it to an using the ** equivalent of atoi() or atof() and store 0 if no such conversion ** is possible. ** ** A NULL value is not changed by this routine. It remains NULL. */ case OP_ToNumeric: { /* no-push */ assert( pTos>=p->aStack ); if( pTos->flags & MEM_Null ) break; assert( MEM_Str==(MEM_Blob>>3) ); pTos->flags |= (pTos->flags&MEM_Blob)>>3; applyAffinity(pTos, SQLITE_AFF_NUMERIC, db->enc); if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){ sqlite3VdbeMemRealify(pTos); }else{ sqlite3VdbeMemRelease(pTos); } assert( (pTos->flags & MEM_Dyn)==0 ); pTos->flags &= (MEM_Int|MEM_Real); break; } /* Opcode: ToText * * * ** ** Force the value on the top of the stack to be text. ** If the value is numeric, convert it to an using the ** equivalent of printf(). Blob values are unchanged and ** are afterwards simply interpreted as text. ** ** A NULL value is not changed by this routine. It remains NULL. */ case OP_ToText: { /* no-push */ assert( pTos>=p->aStack ); if( pTos->flags & MEM_Null ) break; assert( MEM_Str==(MEM_Blob>>3) ); pTos->flags |= (pTos->flags&MEM_Blob)>>3; applyAffinity(pTos, SQLITE_AFF_TEXT, db->enc); assert( pTos->flags & MEM_Str ); pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob); break; } /* Opcode: ToBlob * * * ** ** Force the value on the top of the stack to be a BLOB. ** If the value is numeric, convert it to a string first. ** Strings are simply reinterpreted as blobs with no change ** to the underlying data. ** ** A NULL value is not changed by this routine. It remains NULL. */ case OP_ToBlob: { /* no-push */ assert( pTos>=p->aStack ); if( pTos->flags & MEM_Null ) break; if( (pTos->flags & MEM_Blob)==0 ){ applyAffinity(pTos, SQLITE_AFF_TEXT, db->enc); assert( pTos->flags & MEM_Str ); pTos->flags |= MEM_Blob; } pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str); break; } #endif /* SQLITE_OMIT_CAST */ /* Opcode: Eq P1 P2 P3 ** ** Pop the top two elements from the stack. If they are equal, then ** jump to instruction P2. Otherwise, continue to the next instruction. ** ** If the 0x100 bit of P1 is true and either operand is NULL then take the ** jump. If the 0x100 bit of P1 is clear then fall thru if either operand ** is NULL. ** ** If the 0x200 bit of P1 is set and either operand is NULL then ** both operands are converted to integers prior to comparison. ** NULL operands are converted to zero and non-NULL operands are ** converted to 1. Thus, for example, with 0x200 set, NULL==NULL is true ** whereas it would normally be NULL. Similarly, NULL==123 is false when ** 0x200 is set but is NULL when the 0x200 bit of P1 is clear. ** ** The least significant byte of P1 (mask 0xff) must be an affinity character - ** 'n', 't', 'i' or 'o' - or 0x00. An attempt is made to coerce both values ** according to the affinity before the comparison is made. If the byte is ** 0x00, then numeric affinity is used. ** ** Once any conversions have taken place, and neither value is NULL, ** the values are compared. If both values are blobs, or both are text, ** then memcmp() is used to determine the results of the comparison. If ** both values are numeric, then a numeric comparison is used. If the