Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions
vdbe.h File Reference
#include <stdio.h>
#include "opcodes.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  VdbeOp
struct  VdbeOpList

Defines

#define P3_NOTUSED   0 /* The P3 parameter is not used */
#define P3_DYNAMIC   (-1) /* Pointer to a string obtained from sqliteMalloc() */
#define P3_STATIC   (-2) /* Pointer to a static string */
#define P3_COLLSEQ   (-4) /* P3 is a pointer to a CollSeq structure */
#define P3_FUNCDEF   (-5) /* P3 is a pointer to a FuncDef structure */
#define P3_KEYINFO   (-6) /* P3 is a pointer to a KeyInfo structure */
#define P3_VDBEFUNC   (-7) /* P3 is a pointer to a VdbeFunc structure */
#define P3_MEM   (-8) /* P3 is a pointer to a Mem* structure */
#define P3_TRANSIENT   (-9) /* P3 is a pointer to a transient string */
#define P3_KEYINFO_HANDOFF   (-9)
#define COLNAME_NAME   0
#define COLNAME_DECLTYPE   1
#define COLNAME_DATABASE   2
#define COLNAME_TABLE   3
#define COLNAME_COLUMN   4
#define COLNAME_N   5 /* Number of COLNAME_xxx symbols */
#define ADDR(X)   (-1-(X))
#define VdbeComment(X)   sqlite3VdbeComment X

Typedefs

typedef struct Vdbe
typedef struct VdbeOp
typedef struct VdbeOpList

Functions

Vdbesqlite3VdbeCreate (sqlite3 *)
void sqlite3VdbeCreateCallback (Vdbe *, int *)
int sqlite3VdbeAddOp (Vdbe *, int, int, int)
int sqlite3VdbeOp3 (Vdbe *, int, int, int, const char *zP3, int)
int sqlite3VdbeAddOpList (Vdbe *, int nOp, VdbeOpList const *aOp)
void sqlite3VdbeChangeP1 (Vdbe *, int addr, int P1)
void sqlite3VdbeChangeP2 (Vdbe *, int addr, int P2)
void sqlite3VdbeJumpHere (Vdbe *, int addr)
void sqlite3VdbeChangeToNoop (Vdbe *, int addr, int N)
void sqlite3VdbeChangeP3 (Vdbe *, int addr, const char *zP1, int N)
VdbeOpsqlite3VdbeGetOp (Vdbe *, int)
int sqlite3VdbeMakeLabel (Vdbe *)
void sqlite3VdbeDelete (Vdbe *)
void sqlite3VdbeMakeReady (Vdbe *, int, int, int, int)
int sqlite3VdbeFinalize (Vdbe *)
void sqlite3VdbeResolveLabel (Vdbe *, int)
int sqlite3VdbeCurrentAddr (Vdbe *)
void sqlite3VdbeTrace (Vdbe *, FILE *)
int sqlite3VdbeReset (Vdbe *)
int sqliteVdbeSetVariables (Vdbe *, int, const char **)
void sqlite3VdbeSetNumCols (Vdbe *, int)
int sqlite3VdbeSetColName (Vdbe *, int, int, const char *, int)
void sqlite3VdbeCountChanges (Vdbe *)
sqlite3sqlite3VdbeDb (Vdbe *)
void sqlite3VdbeComment (Vdbe *, const char *,...)

Class Documentation

struct VdbeOp

Definition at line 36 of file vdbe.h.

Collaboration diagram for VdbeOp:
Class Members
u8 opcode
int p1
int p2
char * p3
int p3type
struct VdbeOpList

Definition at line 53 of file vdbe.h.

Class Members
u8 opcode
signed char p1
short int p2
char * p3

Define Documentation

#define ADDR (   X)    (-1-(X))

Definition at line 100 of file vdbe.h.

Definition at line 91 of file vdbe.h.

Definition at line 89 of file vdbe.h.

Definition at line 88 of file vdbe.h.

#define COLNAME_N   5 /* Number of COLNAME_xxx symbols */

Definition at line 92 of file vdbe.h.

Definition at line 87 of file vdbe.h.

Definition at line 90 of file vdbe.h.

#define P3_COLLSEQ   (-4) /* P3 is a pointer to a CollSeq structure */

Definition at line 67 of file vdbe.h.

#define P3_DYNAMIC   (-1) /* Pointer to a string obtained from sqliteMalloc() */

Definition at line 65 of file vdbe.h.

#define P3_FUNCDEF   (-5) /* P3 is a pointer to a FuncDef structure */

Definition at line 68 of file vdbe.h.

#define P3_KEYINFO   (-6) /* P3 is a pointer to a KeyInfo structure */

Definition at line 69 of file vdbe.h.

Definition at line 81 of file vdbe.h.

#define P3_MEM   (-8) /* P3 is a pointer to a Mem* structure */

Definition at line 71 of file vdbe.h.

#define P3_NOTUSED   0 /* The P3 parameter is not used */

Definition at line 64 of file vdbe.h.

#define P3_STATIC   (-2) /* Pointer to a static string */

Definition at line 66 of file vdbe.h.

#define P3_TRANSIENT   (-9) /* P3 is a pointer to a transient string */

Definition at line 72 of file vdbe.h.

#define P3_VDBEFUNC   (-7) /* P3 is a pointer to a VdbeFunc structure */

Definition at line 70 of file vdbe.h.

Definition at line 139 of file vdbe.h.


Typedef Documentation

typedef struct Vdbe

Definition at line 29 of file vdbe.h.

typedef struct VdbeOp

Definition at line 47 of file vdbe.h.

typedef struct VdbeOpList

Definition at line 59 of file vdbe.h.


Function Documentation

int sqlite3VdbeAddOp ( Vdbe ,
int  ,
int  ,
int   
)

Definition at line 103 of file vdbeaux.c.

                                                     {
  int i;
  VdbeOp *pOp;

  i = p->nOp;
  p->nOp++;
  assert( p->magic==VDBE_MAGIC_INIT );
  if( p->nOpAlloc<=i ){
    resizeOpArray(p, i+1);
    if( sqlite3MallocFailed() ){
      return 0;
    }
  }
  pOp = &p->aOp[i];
  pOp->opcode = op;
  pOp->p1 = p1;
  pOp->p2 = p2;
  pOp->p3 = 0;
  pOp->p3type = P3_NOTUSED;
  p->expired = 0;
#ifdef SQLITE_DEBUG
  if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
#endif
  return i;
}

Here is the call graph for this function:

int sqlite3VdbeAddOpList ( Vdbe ,
int  nOp,
VdbeOpList const aOp 
)

Definition at line 308 of file vdbeaux.c.

                                                                 {
  int addr;
  assert( p->magic==VDBE_MAGIC_INIT );
  resizeOpArray(p, p->nOp + nOp);
  if( sqlite3MallocFailed() ){
    return 0;
  }
  addr = p->nOp;
  if( nOp>0 ){
    int i;
    VdbeOpList const *pIn = aOp;
    for(i=0; i<nOp; i++, pIn++){
      int p2 = pIn->p2;
      VdbeOp *pOut = &p->aOp[i+addr];
      pOut->opcode = pIn->opcode;
      pOut->p1 = pIn->p1;
      pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
      pOut->p3 = pIn->p3;
      pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
#ifdef SQLITE_DEBUG
      if( sqlite3_vdbe_addop_trace ){
        sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
      }
#endif
    }
    p->nOp += nOp;
  }
  return addr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqlite3VdbeChangeP1 ( Vdbe ,
int  addr,
int  P1 
)

Definition at line 344 of file vdbeaux.c.

                                                    {
  assert( p==0 || p->magic==VDBE_MAGIC_INIT );
  if( p && addr>=0 && p->nOp>addr && p->aOp ){
    p->aOp[addr].p1 = val;
  }
}

Here is the caller graph for this function:

void sqlite3VdbeChangeP2 ( Vdbe ,
int  addr,
int  P2 
)

Definition at line 355 of file vdbeaux.c.

                                                    {
  assert( val>=0 );
  assert( p==0 || p->magic==VDBE_MAGIC_INIT );
  if( p && addr>=0 && p->nOp>addr && p->aOp ){
    p->aOp[addr].p2 = val;
  }
}

Here is the caller graph for this function:

void sqlite3VdbeChangeP3 ( Vdbe ,
int  addr,
const char *  zP1,
int  N 
)

Definition at line 436 of file vdbeaux.c.

                                                                   {
  Op *pOp;
  assert( p==0 || p->magic==VDBE_MAGIC_INIT );
  if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){
    if (n != P3_KEYINFO) {
      freeP3(n, (void*)*(char**)&zP3);
    }
    return;
  }
  if( addr<0 || addr>=p->nOp ){
    addr = p->nOp - 1;
    if( addr<0 ) return;
  }
  pOp = &p->aOp[addr];
  freeP3(pOp->p3type, pOp->p3);
  pOp->p3 = 0;
  if( zP3==0 ){
    pOp->p3 = 0;
    pOp->p3type = P3_NOTUSED;
  }else if( n==P3_KEYINFO ){
    KeyInfo *pKeyInfo;
    int nField, nByte;

    nField = ((KeyInfo*)zP3)->nField;
    nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
    pKeyInfo = sqliteMallocRaw( nByte );
    pOp->p3 = (char*)pKeyInfo;
    if( pKeyInfo ){
      unsigned char *aSortOrder;
      memcpy(pKeyInfo, zP3, nByte);
      aSortOrder = pKeyInfo->aSortOrder;
      if( aSortOrder ){
        pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
        memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
      }
      pOp->p3type = P3_KEYINFO;
    }else{
      pOp->p3type = P3_NOTUSED;
    }
  }else if( n==P3_KEYINFO_HANDOFF ){
    pOp->p3 = (char*)zP3;
    pOp->p3type = P3_KEYINFO;
  }else if( n<0 ){
    pOp->p3 = (char*)zP3;
    pOp->p3type = n;
  }else{
    if( n==0 ) n = strlen(zP3);
    pOp->p3 = sqliteStrNDup(zP3, n);
    pOp->p3type = P3_DYNAMIC;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqlite3VdbeChangeToNoop ( Vdbe ,
int  addr,
int  N 
)

Definition at line 401 of file vdbeaux.c.

                                                      {
  VdbeOp *pOp = &p->aOp[addr];
  while( N-- ){
    freeP3(pOp->p3type, pOp->p3);
    memset(pOp, 0, sizeof(pOp[0]));
    pOp->opcode = OP_Noop;
    pOp++;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqlite3VdbeComment ( Vdbe ,
const char *  ,
  ... 
)

Definition at line 1949 of file vdbeaux.c.

                                     {
  v->changeCntOn = 1;
}

Here is the caller graph for this function:

Definition at line 36 of file vdbeaux.c.

                                    {
  Vdbe *p;
  p = sqliteMalloc( sizeof(Vdbe) );
  if( p==0 ) return 0;
  p->db = db;
  if( db->pVdbe ){
    db->pVdbe->pPrev = p;
  }
  p->pNext = db->pVdbe;
  p->pPrev = 0;
  db->pVdbe = p;
  p->magic = VDBE_MAGIC_INIT;
  return p;
}

Here is the caller graph for this function:

Definition at line 299 of file vdbeaux.c.

                                   {
  assert( p->magic==VDBE_MAGIC_INIT );
  return p->nOp;
}

Here is the caller graph for this function:

Definition at line 1973 of file vdbeaux.c.

                               {
  return v->db;
}

Here is the caller graph for this function:

Definition at line 1478 of file vdbeaux.c.

                               {
  int i;
  if( p==0 ) return;
  Cleanup(p);
  if( p->pPrev ){
    p->pPrev->pNext = p->pNext;
  }else{
    assert( p->db->pVdbe==p );
    p->db->pVdbe = p->pNext;
  }
  if( p->pNext ){
    p->pNext->pPrev = p->pPrev;
  }
  if( p->aOp ){
    for(i=0; i<p->nOp; i++){
      Op *pOp = &p->aOp[i];
      freeP3(pOp->p3type, pOp->p3);
    }
    sqliteFree(p->aOp);
  }
  releaseMemArray(p->aVar, p->nVar);
  sqliteFree(p->aLabel);
  sqliteFree(p->aStack);
  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  sqliteFree(p->aColName);
  p->magic = VDBE_MAGIC_DEAD;
  sqliteFree(p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1444 of file vdbeaux.c.

                                {
  int rc = SQLITE_OK;

  if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
    rc = sqlite3VdbeReset(p);
  }else if( p->magic!=VDBE_MAGIC_INIT ){
    return SQLITE_MISUSE;
  }
  sqlite3VdbeDelete(p);
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

VdbeOp* sqlite3VdbeGetOp ( Vdbe ,
int   
)

Definition at line 507 of file vdbeaux.c.

                                           {
  assert( p->magic==VDBE_MAGIC_INIT );
  assert( addr>=0 && addr<p->nOp );
  return &p->aOp[addr];
}

Here is the caller graph for this function:

void sqlite3VdbeJumpHere ( Vdbe ,
int  addr 
)

Definition at line 367 of file vdbeaux.c.

                                           {
  sqlite3VdbeChangeP2(p, addr, p->nOp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 152 of file vdbeaux.c.

                                 {
  int i;
  i = p->nLabel++;
  assert( p->magic==VDBE_MAGIC_INIT );
  if( i>=p->nLabelAlloc ){
    p->nLabelAlloc = p->nLabelAlloc*2 + 10;
    sqliteReallocOrFree((void**)&p->aLabel,
                          p->nLabelAlloc*sizeof(p->aLabel[0]));
  }
  if( p->aLabel ){
    p->aLabel[i] = -1;
  }
  return -1-i;
}

Here is the caller graph for this function:

void sqlite3VdbeMakeReady ( Vdbe ,
int  ,
int  ,
int  ,
int   
)

Definition at line 716 of file vdbeaux.c.

 {
  int n;

  assert( p!=0 );
  assert( p->magic==VDBE_MAGIC_INIT );

  /* There should be at least one opcode.
  */
  assert( p->nOp>0 );

  /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
   * is because the call to resizeOpArray() below may shrink the
   * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN 
   * state.
   */
  p->magic = VDBE_MAGIC_RUN;

  /* 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.  (Added later:)  The
  ** resolveP2Values() call computes a tighter upper bound on the
  ** stack size.
  **
  ** Allocation all the stack space we will ever need.
  */
  if( p->aStack==0 ){
    int nArg;       /* Maximum number of args passed to a user function. */
    int nStack;     /* Maximum number of stack entries required */
    resolveP2Values(p, &nArg, &nStack);
    resizeOpArray(p, p->nOp);
    assert( nVar>=0 );
    assert( nStack<p->nOp );
    nStack = isExplain ? 10 : nStack;
    p->aStack = sqliteMalloc(
        nStack*sizeof(p->aStack[0])    /* aStack */
      + nArg*sizeof(Mem*)              /* apArg */
      + nVar*sizeof(Mem)               /* aVar */
      + nVar*sizeof(char*)             /* azVar */
      + nMem*sizeof(Mem)               /* aMem */
      + nCursor*sizeof(Cursor*)        /* apCsr */
    );
    if( !sqlite3MallocFailed() ){
      p->aMem = &p->aStack[nStack];
      p->nMem = nMem;
      p->aVar = &p->aMem[nMem];
      p->nVar = nVar;
      p->okVar = 0;
      p->apArg = (Mem**)&p->aVar[nVar];
      p->azVar = (char**)&p->apArg[nArg];
      p->apCsr = (Cursor**)&p->azVar[nVar];
      p->nCursor = nCursor;
      for(n=0; n<nVar; n++){
        p->aVar[n].flags = MEM_Null;
      }
    }
  }
  for(n=0; n<p->nMem; n++){
    p->aMem[n].flags = MEM_Null;
  }

#ifdef SQLITE_DEBUG
  if( (p->db->flags & SQLITE_VdbeListing)!=0
    || sqlite3OsFileExists("vdbe_explain")
  ){
    int i;
    printf("VDBE Program Listing:\n");
    sqlite3VdbePrintSql(p);
    for(i=0; i<p->nOp; i++){
      sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
    }
  }
  if( sqlite3OsFileExists("vdbe_trace") ){
    p->trace = stdout;
  }
#endif
  p->pTos = &p->aStack[-1];
  p->pc = -1;
  p->rc = SQLITE_OK;
  p->uniqueCnt = 0;
  p->returnDepth = 0;
  p->errorAction = OE_Abort;
  p->popStack =  0;
  p->explain |= isExplain;
  p->magic = VDBE_MAGIC_RUN;
  p->nChange = 0;
  p->cacheCtr = 1;
  p->minWriteFileFormat = 255;
#ifdef VDBE_PROFILE
  {
    int i;
    for(i=0; i<p->nOp; i++){
      p->aOp[i].cnt = 0;
      p->aOp[i].cycles = 0;
    }
  }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite3VdbeOp3 ( Vdbe ,
int  ,
int  ,
int  ,
const char *  zP3,
int   
)

Definition at line 132 of file vdbeaux.c.

                                                                               {
  int addr = sqlite3VdbeAddOp(p, op, p1, p2);
  sqlite3VdbeChangeP3(p, addr, zP3, p3type);
  return addr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1367 of file vdbeaux.c.

                             {
  if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
    sqlite3Error(p->db, SQLITE_MISUSE, 0);
    return SQLITE_MISUSE;
  }

  /* If the VM did not run to completion or if it encountered an
  ** error, then it might not have been halted properly.  So halt
  ** it now.
  */
  sqlite3VdbeHalt(p);

  /* If the VDBE has be run even partially, then transfer the error code
  ** and error message from the VDBE into the main database structure.  But
  ** if the VDBE has just been set to run but has not actually executed any
  ** instructions yet, leave the main database error information unchanged.
  */
  if( p->pc>=0 ){
    if( p->zErrMsg ){
      sqlite3* db = p->db;
      sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX);
      db->errCode = p->rc;
      p->zErrMsg = 0;
    }else if( p->rc ){
      sqlite3Error(p->db, p->rc, 0);
    }else{
      sqlite3Error(p->db, SQLITE_OK, 0);
    }
  }else if( p->rc && p->expired ){
    /* The expired flag was set on the VDBE before the first call
    ** to sqlite3_step(). For consistency (since sqlite3_step() was
    ** called), set the database error in this case as well.
    */
    sqlite3Error(p->db, p->rc, 0);
  }

  /* Reclaim all memory used by the VDBE
  */
  Cleanup(p);

  /* Save profiling information from this VDBE run.
  */
  assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
#ifdef VDBE_PROFILE
  {
    FILE *out = fopen("vdbe_profile.out", "a");
    if( out ){
      int i;
      fprintf(out, "---- ");
      for(i=0; i<p->nOp; i++){
        fprintf(out, "%02x", p->aOp[i].opcode);
      }
      fprintf(out, "\n");
      for(i=0; i<p->nOp; i++){
        fprintf(out, "%6d %10lld %8lld ",
           p->aOp[i].cnt,
           p->aOp[i].cycles,
           p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
        );
        sqlite3VdbePrintOp(out, i, &p->aOp[i]);
      }
      fclose(out);
    }
  }
#endif
  p->magic = VDBE_MAGIC_INIT;
  p->aborted = 0;
  if( p->rc==SQLITE_SCHEMA ){
    sqlite3ResetInternalSchema(p->db, 0);
  }
  return p->rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 172 of file vdbeaux.c.

                                            {
  int j = -1-x;
  assert( p->magic==VDBE_MAGIC_INIT );
  assert( j>=0 && j<p->nLabel );
  if( p->aLabel ){
    p->aLabel[j] = p->nOp;
  }
}

Here is the caller graph for this function:

int sqlite3VdbeSetColName ( Vdbe ,
int  ,
int  ,
const char *  ,
int   
)

Definition at line 912 of file vdbeaux.c.

                                                                              {
  int rc;
  Mem *pColName;
  assert( idx<p->nResColumn );
  assert( var<COLNAME_N );
  if( sqlite3MallocFailed() ) return SQLITE_NOMEM;
  assert( p->aColName!=0 );
  pColName = &(p->aColName[idx+var*p->nResColumn]);
  if( N==P3_DYNAMIC || N==P3_STATIC ){
    rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
  }else{
    rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
  }
  if( rc==SQLITE_OK && N==P3_DYNAMIC ){
    pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
    pColName->xDel = 0;
  }
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 887 of file vdbeaux.c.

                                                   {
  Mem *pColName;
  int n;
  releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
  sqliteFree(p->aColName);
  n = nResColumn*COLNAME_N;
  p->nResColumn = nResColumn;
  p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n );
  if( p->aColName==0 ) return;
  while( n-- > 0 ){
    (pColName++)->flags = MEM_Null;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqlite3VdbeTrace ( Vdbe ,
FILE  
)

Definition at line 54 of file vdbeaux.c.

                                           {
  p->trace = trace;
}

Here is the caller graph for this function:

int sqliteVdbeSetVariables ( Vdbe ,
int  ,
const char **   
)