vdbe.c.svn-base
上传用户:sunhongbo
上传日期:2022-01-25
资源大小:3010k
文件大小:151k
- /*
- ** 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 5 operands. Operands P1, P2, and P3 are integers. Operand P4
- ** is a null-terminated string. Operand P5 is an unsigned character.
- ** Few opcodes use all 5 operands.
- **
- ** Computation results are stored on a set of registers numbered beginning
- ** with 1 and going up to Vdbe.nMem. Each register can store
- ** 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.730 2008/04/15 12:14:22 drh Exp $
- */
- #include "sqliteInt.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.
- */
- #ifdef SQLITE_TEST
- int sqlite3_search_count = 0;
- #endif
- /*
- ** When this global variable is positive, it gets decremented once before
- ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
- ** field of the sqlite3 structure is set in order to simulate and interrupt.
- **
- ** This facility is used for testing purposes only. It does not function
- ** in an ordinary build.
- */
- #ifdef SQLITE_TEST
- int sqlite3_interrupt_count = 0;
- #endif
- /*
- ** 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.
- */
- #ifdef SQLITE_TEST
- int sqlite3_sort_count = 0;
- #endif
- /*
- ** The next global variable records the size of the largest MEM_Blob
- ** or MEM_Str that has been used by a VDBE opcode. The test procedures
- ** use this information to make sure that the zero-blob functionality
- ** is working correctly. This variable has no function other than to
- ** help verify the correct operation of the library.
- */
- #ifdef SQLITE_TEST
- int sqlite3_max_blobsize = 0;
- static void updateMaxBlobsize(Mem *p){
- if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
- sqlite3_max_blobsize = p->n;
- }
- }
- #endif
- /*
- ** Test a register to see if it exceeds the current maximum blob size.
- ** If it does, record the new maximum blob size.
- */
- #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
- # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
- #else
- # define UPDATE_MAX_BLOBSIZE(P)
- #endif
- /*
- ** Release the memory associated with a register. This
- ** leaves the Mem.flags field in an inconsistent state.
- */
- #define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); }
- /*
- ** Convert the given register 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; }
- /*
- ** The header of a record consists of a sequence variable-length integers.
- ** These integers are almost always small and are encoded as a single byte.
- ** The following macro takes advantage this fact to provide a fast decode
- ** of the integers in a record header. It is faster for the common case
- ** where the integer is a single byte. It is a little slower when the
- ** integer is two or more bytes. But overall it is faster.
- **
- ** The following expressions are equivalent:
- **
- ** x = sqlite3GetVarint32( A, &B );
- **
- ** x = GetVarint( A, B );
- **
- */
- #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
- /*
- ** 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 register
- ** does not control the string, it might be deleted without the register
- ** knowing it.
- **
- ** This routine converts an ephemeral string into a dynamically allocated
- ** string that the register 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;}
- /*
- ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
- ** P if required.
- */
- #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
- /*
- ** Argument pMem points at a regiser 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
- ** register 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;
- }
- }
- /*
- ** Properties of opcodes. The OPFLG_INITIALIZER macro is
- ** created by mkopcodeh.awk during compilation. Data is obtained
- ** from the comments following the "case OP_xxxx:" statements in
- ** this file.
- */
- static unsigned char opcodeProperty[] = OPFLG_INITIALIZER;
- /*
- ** Return true if an opcode has any of the OPFLG_xxx properties
- ** specified by mask.
- */
- int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
- assert( opcode>0 && opcode<sizeof(opcodeProperty) );
- return (opcodeProperty[opcode]&mask)!=0;
- }
- /*
- ** Allocate cursor number iCur. Return a pointer to it. Return NULL
- ** if we run out of memory.
- */
- static Cursor *allocateCursor(
- Vdbe *p,
- int iCur,
- Op *pOp,
- int iDb,
- int isBtreeCursor
- ){
- /* Find the memory cell that will be used to store the blob of memory
- ** required for this Cursor structure. It is convenient to use a
- ** vdbe memory cell to manage the memory allocation required for a
- ** Cursor structure for the following reasons:
- **
- ** * Sometimes cursor numbers are used for a couple of different
- ** purposes in a vdbe program. The different uses might require
- ** different sized allocations. Memory cells provide growable
- ** allocations.
- **
- ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
- ** be freed lazily via the sqlite3_release_memory() API. This
- ** minimizes the number of malloc calls made by the system.
- **
- ** Memory cells for cursors are allocated at the top of the address
- ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
- ** cursor 1 is managed by memory cell (p->nMem-1), etc.
- */
- Mem *pMem = &p->aMem[p->nMem-iCur];
- int nByte;
- Cursor *pCx = 0;
- /* If the opcode of pOp is OP_SetNumColumns, then pOp->p2 contains
- ** the number of fields in the records contained in the table or
- ** index being opened. Use this to reserve space for the
- ** Cursor.aType[] array.
- */
- int nField = 0;
- if( pOp->opcode==OP_SetNumColumns || pOp->opcode==OP_OpenEphemeral ){
- nField = pOp->p2;
- }
- nByte =
- sizeof(Cursor) +
- (isBtreeCursor?sqlite3BtreeCursorSize():0) +
- 2*nField*sizeof(u32);
- assert( iCur<p->nCursor );
- if( p->apCsr[iCur] ){
- sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
- p->apCsr[iCur] = 0;
- }
- if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
- p->apCsr[iCur] = pCx = (Cursor *)pMem->z;
- memset(pMem->z, 0, nByte);
- pCx->iDb = iDb;
- pCx->nField = nField;
- if( nField ){
- pCx->aType = (u32 *)&pMem->z[sizeof(Cursor)];
- }
- if( isBtreeCursor ){
- pCx->pCursor = (BtCursor *)&pMem->z[sizeof(Cursor)+2*nField*sizeof(u32)];
- }
- }
- return pCx;
- }
- /*
- ** Try to convert a value into a numeric representation if we can
- ** do so without loss of information. In other words, if the string
- ** looks like a number, convert it into a number. If it does not
- ** look like a number, leave it alone.
- */
- static void applyNumericAffinity(Mem *pRec){
- if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
- int realnum;
- sqlite3VdbeMemNulTerminate(pRec);
- if( (pRec->flags&MEM_Str)
- && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
- i64 value;
- sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
- if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
- pRec->u.i = value;
- MemSetTypeFlag(pRec, MEM_Int);
- }else{
- sqlite3VdbeMemRealify(pRec);
- }
- }
- }
- }
- /*
- ** Processing is determine by the affinity parameter:
- **
- ** SQLITE_AFF_INTEGER:
- ** SQLITE_AFF_REAL:
- ** SQLITE_AFF_NUMERIC:
- ** Try to convert pRec to an integer representation or a
- ** floating-point representation if an integer representation
- ** is not possible. Note that the integer representation is
- ** always preferred, even if the affinity is REAL, because
- ** an integer representation is more space efficient on disk.
- **
- ** SQLITE_AFF_TEXT:
- ** Convert pRec to a text representation.
- **
- ** SQLITE_AFF_NONE:
- ** No-op. pRec is unchanged.
- */
- static void applyAffinity(
- Mem *pRec, /* The value to apply affinity to */
- char affinity, /* The affinity to be applied */
- u8 enc /* Use this text encoding */
- ){
- 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( affinity!=SQLITE_AFF_NONE ){
- assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
- || affinity==SQLITE_AFF_NUMERIC );
- applyNumericAffinity(pRec);
- if( pRec->flags & MEM_Real ){
- sqlite3VdbeIntegerAffinity(pRec);
- }
- }
- }
- /*
- ** Try to convert the type of a function argument or a result column
- ** into a numeric representation. Use either INTEGER or REAL whichever
- ** is appropriate. But only do the conversion if it is possible without
- ** loss of information and return the revised type of the argument.
- **
- ** This is an EXPERIMENTAL api and is subject to change or removal.
- */
- int sqlite3_value_numeric_type(sqlite3_value *pVal){
- Mem *pMem = (Mem*)pVal;
- applyNumericAffinity(pMem);
- storeTypeInfo(pMem, 0);
- return pMem->type;
- }
- /*
- ** 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){
- 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';
- }
- sqlite3_snprintf(100, zCsr, "%c", c);
- zCsr += strlen(zCsr);
- sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
- zCsr += strlen(zCsr);
- for(i=0; i<16 && i<pMem->n; i++){
- sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
- zCsr += strlen(zCsr);
- }
- for(i=0; i<16 && i<pMem->n; i++){
- char z = pMem->z[i];
- if( z<32 || z>126 ) *zCsr++ = '.';
- else *zCsr++ = z;
- }
- sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
- zCsr += strlen(zCsr);
- if( f & MEM_Zero ){
- sqlite3_snprintf(100, zCsr,"+%lldz",pMem->u.i);
- zCsr += strlen(zCsr);
- }
- *zCsr = ' ';
- }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;
- sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
- k += strlen(&zBuf[k]);
- 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++] = ']';
- sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
- k += strlen(&zBuf[k]);
- zBuf[k++] = 0;
- }
- }
- #endif
- #ifdef SQLITE_DEBUG
- /*
- ** Print the value of a register for tracing purposes:
- */
- static void memTracePrint(FILE *out, Mem *p){
- if( p->flags & MEM_Null ){
- fprintf(out, " NULL");
- }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
- fprintf(out, " si:%lld", p->u.i);
- }else if( p->flags & MEM_Int ){
- fprintf(out, " i:%lld", p->u.i);
- }else if( p->flags & MEM_Real ){
- fprintf(out, " r:%g", p->r);
- }else{
- char zBuf[200];
- sqlite3VdbeMemPrettyPrint(p, zBuf);
- fprintf(out, " ");
- fprintf(out, "%s", zBuf);
- }
- }
- static void registerTrace(FILE *out, int iReg, Mem *p){
- fprintf(out, "REG[%d] = ", iReg);
- memTracePrint(out, p);
- fprintf(out, "n");
- }
- #endif
- #ifdef SQLITE_DEBUG
- # define REGISTER_TRACE(R,M) if(p->trace&&R>0)registerTrace(p->trace,R,M)
- #else
- # define REGISTER_TRACE(R,M)
- #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 int lo, hi;
- /* We cannot use "=A", since this would use %rax on x86_64 */
- __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
- return (unsigned long long int)hi << 32 | lo;
- }
- #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->u1.isInterrupted ) 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 sqlite3_malloc() 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 */
- u8 encoding = ENC(db); /* The database encoding */
- Mem *pIn1, *pIn2, *pIn3; /* Input operands */
- Mem *pOut; /* Output operand */
- u8 opProperty;
- #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
- assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
- assert( db->magic==SQLITE_MAGIC_BUSY );
- sqlite3BtreeMutexArrayEnter(&p->aMutex);
- if( p->rc==SQLITE_NOMEM ){
- /* This happens if a malloc() inside a call to sqlite3_column_text() or
- ** sqlite3_column_text16() failed. */
- goto no_mem;
- }
- assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
- p->rc = SQLITE_OK;
- assert( p->explain==0 );
- p->pResultSet = 0;
- db->busyHandler.nBusy = 0;
- CHECK_FOR_INTERRUPT;
- sqlite3VdbeIOTraceSql(p);
- #ifdef SQLITE_DEBUG
- sqlite3FaultBenign(-1, 1);
- if( p->pc==0 && ((p->db->flags & SQLITE_VdbeListing)!=0
- || sqlite3OsAccess(db->pVfs, "vdbe_explain", SQLITE_ACCESS_EXISTS)==1 )
- ){
- int i;
- printf("VDBE Program Listing:n");
- sqlite3VdbePrintSql(p);
- for(i=0; i<p->nOp; i++){
- sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
- }
- }
- if( sqlite3OsAccess(db->pVfs, "vdbe_trace", SQLITE_ACCESS_EXISTS)==1 ){
- p->trace = stdout;
- }
- sqlite3FaultBenign(-1, 0);
- #endif
- for(pc=p->pc; rc==SQLITE_OK; pc++){
- assert( pc>=0 && pc<p->nOp );
- if( db->mallocFailed ) 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 ){
- sqlite3FaultBenign(-1, 1);
- if( sqlite3OsAccess(db->pVfs, "vdbe_sqltrace", SQLITE_ACCESS_EXISTS)==1 ){
- sqlite3VdbePrintSql(p);
- }
- sqlite3FaultBenign(-1, 0);
- }
- #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 ){
- int prc;
- if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
- prc =db->xProgress(db->pProgressArg);
- if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
- if( prc!=0 ){
- rc = SQLITE_INTERRUPT;
- goto vdbe_error_halt;
- }
- nProgressOps = 0;
- }
- nProgressOps++;
- }
- #endif
- /* Do common setup processing for any opcode that is marked
- ** with the "out2-prerelease" tag. Such opcodes have a single
- ** output which is specified by the P2 parameter. The P2 register
- ** is initialized to a NULL.
- */
- opProperty = opcodeProperty[pOp->opcode];
- if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
- assert( pOp->p2>0 );
- assert( pOp->p2<=p->nMem );
- pOut = &p->aMem[pOp->p2];
- sqlite3VdbeMemReleaseExternal(pOut);
- pOut->flags = MEM_Null;
- }else
-
- /* Do common setup for opcodes marked with one of the following
- ** combinations of properties.
- **
- ** in1
- ** in1 in2
- ** in1 in2 out3
- ** in1 in3
- **
- ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
- ** registers for inputs. Variable pOut points to the output register.
- */
- if( (opProperty & OPFLG_IN1)!=0 ){
- assert( pOp->p1>0 );
- assert( pOp->p1<=p->nMem );
- pIn1 = &p->aMem[pOp->p1];
- REGISTER_TRACE(pOp->p1, pIn1);
- if( (opProperty & OPFLG_IN2)!=0 ){
- assert( pOp->p2>0 );
- assert( pOp->p2<=p->nMem );
- pIn2 = &p->aMem[pOp->p2];
- REGISTER_TRACE(pOp->p2, pIn2);
- if( (opProperty & OPFLG_OUT3)!=0 ){
- assert( pOp->p3>0 );
- assert( pOp->p3<=p->nMem );
- pOut = &p->aMem[pOp->p3];
- }
- }else if( (opProperty & OPFLG_IN3)!=0 ){
- assert( pOp->p3>0 );
- assert( pOp->p3<=p->nMem );
- pIn3 = &p->aMem[pOp->p3];
- REGISTER_TRACE(pOp->p3, pIn3);
- }
- }else if( (opProperty & OPFLG_IN2)!=0 ){
- assert( pOp->p2>0 );
- assert( pOp->p2<=p->nMem );
- pIn2 = &p->aMem[pOp->p2];
- REGISTER_TRACE(pOp->p2, pIn2);
- }else if( (opProperty & OPFLG_IN3)!=0 ){
- assert( pOp->p3>0 );
- assert( pOp->p3<=p->nMem );
- pIn3 = &p->aMem[pOp->p3];
- REGISTER_TRACE(pOp->p3, pIn3);
- }
- 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.
- **
- ** Other keywords in the comment that follows each case are used to
- ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
- ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
- ** the mkopcodeh.awk script for additional information.
- **
- ** 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: { /* jump */
- 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: { /* jump */
- 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: {
- assert( p->returnDepth>0 );
- p->returnDepth--;
- pc = p->returnStack[p->returnDepth] - 1;
- break;
- }
- /* Opcode: Halt P1 P2 * P4 *
- **
- ** 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 P4 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: {
- p->rc = pOp->p1;
- p->pc = pc;
- p->errorAction = pOp->p2;
- if( pOp->p4.z ){
- sqlite3SetString(&p->zErrMsg, pOp->p4.z, (char*)0);
- }
- rc = sqlite3VdbeHalt(p);
- assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
- if( rc==SQLITE_BUSY ){
- p->rc = rc = SQLITE_BUSY;
- }else{
- rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
- }
- goto vdbe_return;
- }
- /* Opcode: Integer P1 P2 * * *
- **
- ** The 32-bit integer value P1 is written into register P2.
- */
- case OP_Integer: { /* out2-prerelease */
- pOut->flags = MEM_Int;
- pOut->u.i = pOp->p1;
- break;
- }
- /* Opcode: Int64 * P2 * P4 *
- **
- ** P4 is a pointer to a 64-bit integer value.
- ** Write that value into register P2.
- */
- case OP_Int64: { /* out2-prerelease */
- assert( pOp->p4.pI64!=0 );
- pOut->flags = MEM_Int;
- pOut->u.i = *pOp->p4.pI64;
- break;
- }
- /* Opcode: Real * P2 * P4 *
- **
- ** P4 is a pointer to a 64-bit floating point value.
- ** Write that value into register P2.
- */
- case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
- pOut->flags = MEM_Real;
- pOut->r = *pOp->p4.pReal;
- break;
- }
- /* Opcode: String8 * P2 * P4 *
- **
- ** P4 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, out2-prerelease */
- assert( pOp->p4.z!=0 );
- pOp->opcode = OP_String;
- pOp->p1 = strlen(pOp->p4.z);
- #ifndef SQLITE_OMIT_UTF16
- if( encoding!=SQLITE_UTF8 ){
- sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
- if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
- if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pOut) ) goto no_mem;
- pOut->zMalloc = 0;
- pOut->flags |= MEM_Static;
- pOut->flags &= ~MEM_Dyn;
- if( pOp->p4type==P4_DYNAMIC ){
- sqlite3_free(pOp->p4.z);
- }
- pOp->p4type = P4_DYNAMIC;
- pOp->p4.z = pOut->z;
- pOp->p1 = pOut->n;
- if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
- goto too_big;
- }
- UPDATE_MAX_BLOBSIZE(pOut);
- break;
- }
- #endif
- if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
- goto too_big;
- }
- /* Fall through to the next case, OP_String */
- }
-
- /* Opcode: String P1 P2 * P4 *
- **
- ** The string value P4 of length P1 (bytes) is stored in register P2.
- */
- case OP_String: { /* out2-prerelease */
- assert( pOp->p4.z!=0 );
- pOut->flags = MEM_Str|MEM_Static|MEM_Term;
- pOut->z = pOp->p4.z;
- pOut->n = pOp->p1;
- pOut->enc = encoding;
- UPDATE_MAX_BLOBSIZE(pOut);
- break;
- }
- /* Opcode: Null * P2 * * *
- **
- ** Write a NULL into register P2.
- */
- case OP_Null: { /* out2-prerelease */
- break;
- }
- #ifndef SQLITE_OMIT_BLOB_LITERAL
- /* Opcode: Blob P1 P2 * P4
- **
- ** P4 points to a blob of data P1 bytes long. Store this
- ** blob in register P2. 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 P4. This opcode is transformed to an OP_Blob
- ** the first time it is executed.
- */
- case OP_Blob: { /* out2-prerelease */
- assert( pOp->p1 <= SQLITE_MAX_LENGTH );
- sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
- pOut->enc = encoding;
- UPDATE_MAX_BLOBSIZE(pOut);
- break;
- }
- #endif /* SQLITE_OMIT_BLOB_LITERAL */
- /* Opcode: Variable P1 P2 * * *
- **
- ** The value of variable P1 is written into register P2. 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: { /* out2-prerelease */
- int j = pOp->p1 - 1;
- Mem *pVar;
- assert( j>=0 && j<p->nVar );
- pVar = &p->aVar[j];
- if( sqlite3VdbeMemTooBig(pVar) ){
- goto too_big;
- }
- sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static);
- UPDATE_MAX_BLOBSIZE(pOut);
- break;
- }
- /* Opcode: Move P1 P2 * * *
- **
- ** Move the value in register P1 over into register P2. Register P1
- ** is left holding a NULL. It is an error for P1 and P2 to be the
- ** same register.
- */
- case OP_Move: {
- char *zMalloc;
- assert( pOp->p1>0 );
- assert( pOp->p1<=p->nMem );
- pIn1 = &p->aMem[pOp->p1];
- REGISTER_TRACE(pOp->p1, pIn1);
- assert( pOp->p2>0 );
- assert( pOp->p2<=p->nMem );
- pOut = &p->aMem[pOp->p2];
- assert( pOut!=pIn1 );
- zMalloc = pOut->zMalloc;
- pOut->zMalloc = 0;
- sqlite3VdbeMemMove(pOut, pIn1);
- pIn1->zMalloc = zMalloc;
- REGISTER_TRACE(pOp->p2, pOut);
- break;
- }
- /* Opcode: Copy P1 P2 * * *
- **
- ** Make a copy of register P1 into register P2.
- **
- ** This instruction makes a deep copy of the value. A duplicate
- ** is made of any string or blob constant. See also OP_SCopy.
- */
- case OP_Copy: {
- assert( pOp->p1>0 );
- assert( pOp->p1<=p->nMem );
- pIn1 = &p->aMem[pOp->p1];
- REGISTER_TRACE(pOp->p1, pIn1);
- assert( pOp->p2>0 );
- assert( pOp->p2<=p->nMem );
- pOut = &p->aMem[pOp->p2];
- assert( pOut!=pIn1 );
- sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
- Deephemeralize(pOut);
- REGISTER_TRACE(pOp->p2, pOut);
- break;
- }
- /* Opcode: SCopy P1 P2 * * *
- **
- ** Make a shallow copy of register P1 into register P2.
- **
- ** This instruction makes a shallow copy of the value. If the value
- ** is a string or blob, then the copy is only a pointer to the
- ** original and hence if the original changes so will the copy.
- ** Worse, if the original is deallocated, the copy becomes invalid.
- ** Thus the program must guarantee that the original will not change
- ** during the lifetime of the copy. Use OP_Copy to make a complete
- ** copy.
- */
- case OP_SCopy: {
- assert( pOp->p1>0 );
- assert( pOp->p1<=p->nMem );
- pIn1 = &p->aMem[pOp->p1];
- REGISTER_TRACE(pOp->p1, pIn1);
- assert( pOp->p2>0 );
- assert( pOp->p2<=p->nMem );
- pOut = &p->aMem[pOp->p2];
- assert( pOut!=pIn1 );
- sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
- REGISTER_TRACE(pOp->p2, pOut);
- break;
- }
- /* Opcode: ResultRow P1 P2 * * *
- **
- ** The registers P1 throught P1+P2-1 contain a single row of
- ** results. This opcode causes the sqlite3_step() call to terminate
- ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
- ** structure to provide access to the top P1 values as the result
- ** row.
- */
- case OP_ResultRow: {
- Mem *pMem;
- int i;
- assert( p->nResColumn==pOp->p2 );
- assert( pOp->p1>0 );
- assert( pOp->p1+pOp->p2<=p->nMem );
- /* Invalidate all ephemeral cursor row caches */
- p->cacheCtr = (p->cacheCtr + 2)|1;
- /* Make sure the results of the current row are