#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 2003 September 6
**
** 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.
**
*************************************************************************
** This file contains code used for creating, destroying, and populating
** a VDBE (or an "sqlite_vm" as it is known to the outside world.) Prior
** to version 2.8.7, all this code was combined into the vdbe.c source file.
** But that file was getting too big so this subroutines were split out.
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"
/*
** When debugging the code generator in a symbolic debugger, one can
** set the sqlite_vdbe_addop_trace to 1 and all opcodes will be printed
** as they are added to the instruction stream.
*/
#ifndef NDEBUG
int sqlite_vdbe_addop_trace = 0;
#endif
/*
** Create a new virtual database engine.
*/
Vdbe *p;
p = sqliteMalloc( sizeof(Vdbe) );
if( p==0 ) return 0;
}
p->pPrev = 0;
p->magic = VDBE_MAGIC_INIT;
return p;
}
/*
** Turn tracing on or off
*/
}
/*
** Add a new instruction to the list of instructions current in the
** VDBE. Return the address of the new instruction.
**
** Parameters:
**
** p Pointer to the VDBE
**
** op The opcode for this instruction
**
** p1, p2 First two of the three possible operands.
**
** Use the sqliteVdbeResolveLabel() function to fix an address and
** the sqliteVdbeChangeP3() function to change the value of the P3
** operand.
*/
int i;
i = p->nOp;
p->nOp++;
if( i>=p->nOpAlloc ){
if( aNew==0 ){
return 0;
}
}
}
#ifndef NDEBUG
#endif
return i;
}
/*
** Add an opcode that includes the p3 value.
*/
return addr;
}
/*
** Add multiple opcodes. The list is terminated by an opcode of 0.
*/
int addr;
}
return addr;
}
/*
** Create a new symbolic label for an instruction that has yet to be
** coded. The symbolic label is really just a negative number. The
** label can be used as the P2 value of an operation. Later, when
** the label is resolved to a specific address, the VDBE will scan
** through its operation list and change all values of P2 which match
** the label into the resolved address.
**
** The VDBE knows that a P2 value is a label because labels are
** always negative and P2 values are suppose to be non-negative.
** Hence, a negative P2 value is a label that has yet to be resolved.
*/
int i;
i = p->nLabel++;
if( i>=p->nLabelAlloc ){
int *aNew;
if( aNew==0 ){
sqliteFree(p->aLabel);
}
}
if( p->aLabel==0 ){
p->nLabel = 0;
p->nLabelAlloc = 0;
return 0;
}
p->aLabel[i] = -1;
return -1-i;
}
/*
** Resolve label "x" to be the address of the next instruction to
** be inserted. The parameter "x" must have been obtained from
** a prior call to sqliteVdbeMakeLabel().
*/
int j;
for(j=0; j<p->nOp; j++){
}
}
}
/*
** Return the address of the next instruction to be inserted.
*/
return p->nOp;
}
/*
** Add a whole list of operations to the operation stack. Return the
** address of the first operation added.
*/
int addr;
if( aNew==0 ){
return 0;
}
}
if( nOp>0 ){
int i;
#ifndef NDEBUG
if( sqlite_vdbe_addop_trace ){
}
#endif
}
}
return addr;
}
/*
** Change the value of the P1 operand for a specific instruction.
** This routine is useful when a large program is loaded from a
** static array using sqliteVdbeAddOpList but we want to make a
** few minor changes to the program.
*/
}
}
/*
** Change the value of the P2 operand for a specific instruction.
** This routine is useful for setting a jump destination.
*/
}
}
/*
** Change the value of the P3 operand for a specific instruction.
** This routine is useful when a large program is loaded from a
** static array using sqliteVdbeAddOpList but we want to make a
** few minor changes to the program.
**
** If n>=0 then the P3 operand is dynamic, meaning that a copy of
** the string is made into memory obtained from sqliteMalloc().
** A value of n==0 means copy bytes of zP3 up to and including the
** first null byte. If n>0 then copy n+1 bytes of zP3.
**
** If n==P3_STATIC it means that zP3 is a pointer to a constant static
** string and we can just copy the pointer. n==P3_POINTER means zP3 is
** a pointer to some object other than a string.
**
** If addr<0 then change P3 on the most recently inserted instruction.
*/
if( p==0 || p->aOp==0 ) return;
if( addr<0 ) return;
}
}
if( zP3==0 ){
}else if( n<0 ){
}else{
}
}
/*
** If the P3 operand to the specified instruction appears
** to be a quoted string token, then this procedure removes
** the quotes.
**
** The quoting operator can be either a grave ascent (ASCII 0x27)
** or a double quote character (ASCII 0x22). Two quotes in a row
** resolve to be a single actual quote character within the string.
*/
if( p->aOp==0 ) return;
if( addr<0 ) return;
}
}
}
/*
** On the P3 argument of the given instruction, change all
** strings of whitespace characters into a single space and
** delete leading and trailing whitespace.
*/
unsigned char *z;
int i, j;
return;
}
}
if( z==0 ) return;
i = j = 0;
while( isspace(z[i]) ){ i++; }
while( z[i] ){
if( isspace(z[i]) ){
z[j++] = ' ';
while( isspace(z[++i]) ){}
}else{
z[j++] = z[i++];
}
}
while( j>0 && isspace(z[j-1]) ){ j--; }
z[j] = 0;
}
/*
** Search for the current program for the given opcode and P2
** value. Return the address plus 1 if found and 0 if not found.
*/
int i;
for(i=0; i<p->nOp; i++){
}
return 0;
}
/*
** Return the opcode for a given address.
*/
}
/*
** The following group or routines are employed by installable functions
** to return their results.
**
** The sqlite_set_result_string() routine can be used to return a string
** value or to return a NULL. To return a NULL, pass in NULL for zResult.
** A copy is made of the string before this routine returns so it is safe
** to pass in an ephemeral string.
**
** sqlite_set_result_error() works like sqlite_set_result_string() except
** that it signals a fatal error. The string argument, if any, is the
** error message. If the argument is NULL a generic substitute error message
** is used.
**
** The sqlite_set_result_int() and sqlite_set_result_double() set the return
** value of the user function to an integer or a double.
**
** These routines are defined here in vdbe.c because they depend on knowing
** the internals of the sqlite_func structure which is only defined in
** this source file.
*/
sqliteFree(p->s.z);
}
if( zResult==0 ){
n = 0;
p->s.z = 0;
p->s.n = 0;
}else{
if( n<NBFS-1 ){
p->s.zShort[n] = 0;
p->s.z = p->s.zShort;
}else{
p->s.z = sqliteMallocRaw( n+1 );
if( p->s.z ){
p->s.z[n] = 0;
}
}
p->s.n = n+1;
}
return p->s.z;
}
sqliteFree(p->s.z);
}
p->s.i = iResult;
}
sqliteFree(p->s.z);
}
p->s.r = rResult;
}
sqlite_set_result_string(p, zMsg, n);
p->isError = 1;
}
/*
** Extract the user data from a sqlite_func structure and return a
** pointer to it.
*/
}
/*
** Allocate or return the aggregate context for a user function. A new
** context is allocated on the first call. Subsequent calls return the
** same context that was returned on prior calls.
**
** This routine is defined here in vdbe.c because it depends on knowing
** the internals of the sqlite_func structure which is only defined in
** this source file.
*/
if( p->pAgg==0 ){
p->pAgg = (void*)p->s.z;
}else{
}
}
return p->pAgg;
}
/*
** Return the number of times the Step function of a aggregate has been
** called.
**
** This routine is defined here in vdbe.c because it depends on knowing
** the internals of the sqlite_func structure which is only defined in
** this source file.
*/
return p->cnt;
}
#if !defined(NDEBUG) || defined(VDBE_PROFILE)
/*
** Print a single opcode. This routine is used for debugging only.
*/
char *zP3;
}else{
}
}
#endif
/*
** Give a listing of the program in the virtual machine.
**
** The interface is the same as sqliteVdbeExec(). But instead of
** running the code, it invokes the callback once for each instruction.
** This feature is used to implement "EXPLAIN".
*/
int sqliteVdbeList(
Vdbe *p /* The VDBE */
){
int i;
static char *azColumnNames[] = {
"addr", "opcode", "p1", "p2", "p3",
"int", "text", "int", "int", "text",
0
};
p->azColName = azColumnNames;
p->azResColumn = p->zArgv;
i = p->pc;
if( i>=p->nOp ){
rc = SQLITE_DONE;
p->rc = SQLITE_MISUSE;
}else{
p->rc = SQLITE_INTERRUPT;
}
rc = SQLITE_ERROR;
}else{
}else{
}
p->pc = i+1;
p->azResColumn = p->zArgv;
p->nResColumn = 5;
rc = SQLITE_ROW;
}
return rc;
}
/*
** Prepare a virtual machine for execution. This involves things such
** as allocating stack space and initializing the program counter.
** After the VDBE has be prepped, it can be executed by one or more
** calls to sqliteVdbeExec().
*/
void sqliteVdbeMakeReady(
Vdbe *p, /* The VDBE */
int nVar, /* Number of '?' see in the SQL statement */
int isExplain /* True if the EXPLAIN keywords is present */
){
int n;
assert( p!=0 );
/* Add a HALT instruction to the very end of the program.
*/
sqliteVdbeAddOp(p, OP_Halt, 0, 0);
}
/* No instruction ever pushes more than a single element onto the
** stack. And the stack never grows on successive executions of the
** same loop. So the total number of instructions is an upper bound
** on the maximum stack depth required.
**
** Allocation all the stack space we will ever need.
*/
if( p->aStack==0 ){
p->aStack = sqliteMalloc(
);
}
#ifdef MEMORY_DEBUG
if( sqliteOsFileExists("vdbe_trace") ){
}
#endif
p->pc = 0;
p->uniqueCnt = 0;
p->returnDepth = 0;
p->errorAction = OE_Abort;
p->undoTransOnError = 0;
p->popStack = 0;
p->magic = VDBE_MAGIC_RUN;
#ifdef VDBE_PROFILE
{
int i;
for(i=0; i<p->nOp; i++){
}
}
#endif
}
/*
** Remove any elements that remain on the sorter for the VDBE given.
*/
while( p->pSort ){
}
}
/*
** Reset an Agg structure. Delete all its contents.
**
** For installable aggregate functions, if the step function has been
** called, make sure the finalizer function has also been called. The
** finalizer might need to free memory that was allocated as part of its
** private context. If the finalizer has not been called yet, call it
** now.
*/
int i;
HashElem *p;
sqliteFree(pMem->z);
}
sqliteFree(ctx.s.z);
}
sqliteFree(pMem->z);
}
}
}
}
/*
** Delete a keylist
*/
while( p ){
sqliteFree(p);
p = pNext;
}
}
/*
** Close a cursor and release all the resources that cursor happens
** to hold.
*/
}
}
}
/*
** Close all cursors
*/
int i;
for(i=0; i<p->nCursor; i++){
sqliteVdbeCleanupCursor(&p->aCsr[i]);
}
sqliteFree(p->aCsr);
p->aCsr = 0;
p->nCursor = 0;
}
/*
** Clean up the VM after execution.
**
** sorters that were left open. It also deletes the values of
** variables in the azVariable[] array.
*/
int i;
if( p->aStack ){
sqliteFree(pTos->z);
}
pTos--;
}
}
closeAllCursors(p);
if( p->aMem ){
for(i=0; i<p->nMem; i++){
sqliteFree(p->aMem[i].z);
}
}
}
sqliteFree(p->aMem);
p->aMem = 0;
p->nMem = 0;
if( p->pList ){
p->pList = 0;
}
if( p->pFile ){
p->pFile = 0;
}
if( p->azField ){
sqliteFree(p->azField);
p->azField = 0;
}
p->nField = 0;
if( p->zLine ){
sqliteFree(p->zLine);
p->zLine = 0;
}
p->nLineAlloc = 0;
sqliteVdbeAggReset(&p->agg);
if( p->aSet ){
for(i=0; i<p->nSet; i++){
}
}
sqliteFree(p->aSet);
p->aSet = 0;
p->nSet = 0;
if( p->keylistStack ){
int ii;
}
sqliteFree(p->keylistStack);
p->keylistStackDepth = 0;
p->keylistStack = 0;
}
sqliteFree(p->contextStack);
p->contextStack = 0;
sqliteFree(p->zErrMsg);
p->zErrMsg = 0;
}
/*
** Clean up a VDBE after execution but do not delete the VDBE just yet.
** Write any error messages into *pzErrMsg. Return the result code.
**
** After this routine is run, the VDBE should be ready to be executed
** again.
*/
int i;
return SQLITE_MISUSE;
}
if( p->zErrMsg ){
}else{
sqliteFree(p->zErrMsg);
}
p->zErrMsg = 0;
}else if( p->rc ){
}
Cleanup(p);
switch( p->errorAction ){
case OE_Abort: {
if( !p->undoTransOnError ){
}
}
break;
}
/* Fall through to ROLLBACK */
}
case OE_Rollback: {
break;
}
default: {
if( p->undoTransOnError ){
}
break;
}
}
}
}
}
#ifdef VDBE_PROFILE
{
if( out ){
int i;
for(i=0; i<p->nOp; i++){
}
for(i=0; i<p->nOp; i++){
);
}
}
}
#endif
p->magic = VDBE_MAGIC_INIT;
return p->rc;
}
/*
** Clean up and delete a VDBE after execution. Return an integer which is
** the result code. Write any error message text into *pzErrMsg.
*/
int rc;
return SQLITE_MISUSE;
}
sqliteVdbeDelete(p);
}
if( rc==SQLITE_SCHEMA ){
}
return rc;
}
/*
** Set the values of all variables. Variable $1 in the original SQL will
** be the string azValue[0]. $2 will have the value azValue[1]. And
** so forth. If a value is out of range (for example $3 when nValue==2)
** then its value will be NULL.
**
** This routine overrides any prior call.
*/
return SQLITE_MISUSE;
}
if( i<1 || i>p->nVar ){
return SQLITE_RANGE;
}
i--;
if( p->abVar[i] ){
sqliteFree(p->azVar[i]);
}
if( zVal==0 ){
copy = 0;
len = 0;
}
if( len<0 ){
}
if( copy ){
}else{
}
return SQLITE_OK;
}
/*
** Delete an entire VDBE.
*/
int i;
if( p==0 ) return;
Cleanup(p);
if( p->pPrev ){
}else{
}
if( p->pNext ){
}
if( p->nOpAlloc==0 ){
p->aOp = 0;
p->nOp = 0;
}
for(i=0; i<p->nOp; i++){
}
}
for(i=0; i<p->nVar; i++){
}
sqliteFree(p->aOp);
sqliteFree(p->aLabel);
sqliteFree(p->aStack);
p->magic = VDBE_MAGIC_DEAD;
sqliteFree(p);
}
/*
** Convert an integer in between the native integer format and
** the bigEndian format used as the record number for tables.
**
** The bigEndian format (most significant byte first) is used for
** record numbers so that records will sort into the correct order
** even though memcmp() is used to compare the keys. On machines
** whose native integer format is little endian (ex: i486) the
** order of bytes is reversed. On native big-endian machines
** (ex: Alpha, Sparc, Motorola) the byte order is the same.
**
** This function is its own inverse. In other words
**
** X == byteSwap(byteSwap(X))
*/
int sqliteVdbeByteSwap(int x){
union {
char zBuf[sizeof(int)];
int i;
} ux;
return ux.i;
}
/*
** If a MoveTo operation is pending on the given cursor, then do that
** MoveTo now. Return an error code. If no MoveTo is pending, this
** routine does nothing and returns SQLITE_OK.
*/
if( p->deferredMoveto ){
int res;
extern int sqlite_search_count;
p->recnoIsValid = res==0;
if( res<0 ){
}
p->deferredMoveto = 0;
}
return SQLITE_OK;
}