⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vdbe.c

📁 sqlite 嵌入式数据库的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
** 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 */  char zBuf[100];            /* Space to sprintf() an integer */#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;  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 ***** Exit immediately.  All open cursors, Lists, Sorts, 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. **** 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 * P3**** The integer value P1 is pushed onto the stack.  If P3 is not zero** then it is assumed to be a string representation of the same integer.** If P1 is zero and P3 is not zero, then the value is derived from P3.**** If the value cannot be represented as a 32-bits then its value** will be in P3.*/case OP_Integer: {  pTos++;  if( pOp->p3==0 ){    pTos->flags = MEM_Int;    pTos->i = pOp->p1;  }else{    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;  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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -