/ Check-in [c0faa1c6]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Rework the VDBE data structures to combine string representations into the same structure with integer and floating point. This opens the door to significant optimizations. (CVS 1202)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c0faa1c67a967f028cd018e58988fb08bc814d3d
User & Date: drh 2004-01-30 14:49:17
Context
2004-01-31
19:22
Rework internal data structures to make the VDBE about 15% smaller. (CVS 1203) check-in: 8273c74b user: drh tags: trunk
2004-01-30
14:49
Rework the VDBE data structures to combine string representations into the same structure with integer and floating point. This opens the door to significant optimizations. (CVS 1202) check-in: c0faa1c6 user: drh tags: trunk
02:01
Make sure min() and max() optimizations work for subqueries. Ticket #587. (CVS 1201) check-in: af73fbca user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.37 2004/01/19 04:53:25 jplyon Exp $
           19  +** $Id: func.c,v 1.38 2004/01/30 14:49:17 drh Exp $
    20     20   */
    21     21   #include <ctype.h>
    22     22   #include <math.h>
    23     23   #include <stdlib.h>
    24     24   #include <assert.h>
    25     25   #include "sqliteInt.h"
    26     26   #include "os.h"
................................................................................
   491    491   */
   492    492   static void minStep(sqlite_func *context, int argc, const char **argv){
   493    493     MinMaxCtx *p;
   494    494     p = sqlite_aggregate_context(context, sizeof(*p));
   495    495     if( p==0 || argc<1 || argv[0]==0 ) return;
   496    496     if( p->z==0 || sqliteCompare(argv[0],p->z)<0 ){
   497    497       int len;
   498         -    if( p->z && p->z!=p->zBuf ){
          498  +    if( !p->zBuf[0] ){
   499    499         sqliteFree(p->z);
   500    500       }
   501    501       len = strlen(argv[0]);
   502         -    if( len < sizeof(p->zBuf) ){
   503         -      p->z = p->zBuf;
          502  +    if( len < sizeof(p->zBuf)-1 ){
          503  +      p->z = &p->zBuf[1];
          504  +      p->zBuf[0] = 1;
   504    505       }else{
   505    506         p->z = sqliteMalloc( len+1 );
          507  +      p->zBuf[0] = 0;
   506    508         if( p->z==0 ) return;
   507    509       }
   508    510       strcpy(p->z, argv[0]);
   509    511     }
   510    512   }
   511    513   static void maxStep(sqlite_func *context, int argc, const char **argv){
   512    514     MinMaxCtx *p;
   513    515     p = sqlite_aggregate_context(context, sizeof(*p));
   514    516     if( p==0 || argc<1 || argv[0]==0 ) return;
   515    517     if( p->z==0 || sqliteCompare(argv[0],p->z)>0 ){
   516    518       int len;
   517         -    if( p->z && p->z!=p->zBuf ){
          519  +    if( !p->zBuf[0] ){
   518    520         sqliteFree(p->z);
   519    521       }
   520    522       len = strlen(argv[0]);
   521         -    if( len < sizeof(p->zBuf) ){
   522         -      p->z = p->zBuf;
          523  +    if( len < sizeof(p->zBuf)-1 ){
          524  +      p->z = &p->zBuf[1];
          525  +      p->zBuf[0] = 1;
   523    526       }else{
   524    527         p->z = sqliteMalloc( len+1 );
          528  +      p->zBuf[0] = 0;
   525    529         if( p->z==0 ) return;
   526    530       }
   527    531       strcpy(p->z, argv[0]);
   528    532     }
   529    533   }
   530    534   static void minMaxFinalize(sqlite_func *context){
   531    535     MinMaxCtx *p;
   532    536     p = sqlite_aggregate_context(context, sizeof(*p));
   533    537     if( p && p->z ){
   534    538       sqlite_set_result_string(context, p->z, strlen(p->z));
   535    539     }
   536         -  if( p && p->z && p->z!=p->zBuf ){
          540  +  if( p && !p->zBuf[0] ){
   537    541       sqliteFree(p->z);
   538    542     }
   539    543   }
   540    544   
   541    545   /*
   542    546   ** This function registered all of the above C functions as SQL
   543    547   ** functions.  This should be the only routine in this file with

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.251 2004/01/15 02:44:03 drh Exp $
           46  +** $Id: vdbe.c,v 1.252 2004/01/30 14:49:17 drh Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
   155    155     pOld = sqliteHashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem);
   156    156     if( pOld!=0 ){
   157    157       assert( pOld==pElem );  /* Malloc failed on insert */
   158    158       sqliteFree(pOld);
   159    159       return 0;
   160    160     }
   161    161     for(i=0; i<p->nMem; i++){
   162         -    pElem->aMem[i].s.flags = STK_Null;
          162  +    pElem->aMem[i].flags = MEM_Null;
   163    163     }
   164    164     p->pCurrent = pElem;
   165    165     return 0;
   166    166   }
   167    167   
   168    168   /*
   169    169   ** Get the AggElem currently in focus
................................................................................
   178    178     return pElem ? sqliteHashData(pElem) : 0;
   179    179   }
   180    180   
   181    181   /*
   182    182   ** Convert the given stack entity into a string if it isn't one
   183    183   ** already.
   184    184   */
   185         -#define Stringify(P,I) if((aStack[I].flags & STK_Str)==0){hardStringify(P,I);}
          185  +#define Stringify(P,I) if((aStack[I].flags & MEM_Str)==0){hardStringify(P,I);}
   186    186   static int hardStringify(Vdbe *p, int i){
   187         -  Stack *pStack = &p->aStack[i];
          187  +  Mem *pStack = &p->aStack[i];
   188    188     int fg = pStack->flags;
   189         -  if( fg & STK_Real ){
   190         -    sqlite_snprintf(sizeof(pStack->z),pStack->z,"%.15g",pStack->r);
   191         -  }else if( fg & STK_Int ){
   192         -    sqlite_snprintf(sizeof(pStack->z),pStack->z,"%d",pStack->i);
          189  +  if( fg & MEM_Real ){
          190  +    sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%.15g",pStack->r);
          191  +  }else if( fg & MEM_Int ){
          192  +    sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%d",pStack->i);
   193    193     }else{
   194         -    pStack->z[0] = 0;
          194  +    pStack->zShort[0] = 0;
   195    195     }
   196         -  p->zStack[i] = pStack->z;
   197         -  pStack->n = strlen(pStack->z)+1;
   198         -  pStack->flags = STK_Str;
          196  +  p->aStack[i].z = pStack->zShort;
          197  +  pStack->n = strlen(pStack->zShort)+1;
          198  +  pStack->flags = MEM_Str;
   199    199     return 0;
   200    200   }
   201    201   
   202    202   /*
   203    203   ** Convert the given stack entity into a string that has been obtained
   204    204   ** from sqliteMalloc().  This is different from Stringify() above in that
   205    205   ** Stringify() will use the NBFS bytes of static string space if the string
   206    206   ** will fit but this routine always mallocs for space.
   207    207   ** Return non-zero if we run out of memory.
   208    208   */
   209         -#define Dynamicify(P,I) ((aStack[I].flags & STK_Dyn)==0 ? hardDynamicify(P,I):0)
          209  +#define Dynamicify(P,I) ((aStack[I].flags & MEM_Dyn)==0 ? hardDynamicify(P,I):0)
   210    210   static int hardDynamicify(Vdbe *p, int i){
   211         -  Stack *pStack = &p->aStack[i];
          211  +  Mem *pStack = &p->aStack[i];
   212    212     int fg = pStack->flags;
   213    213     char *z;
   214         -  if( (fg & STK_Str)==0 ){
          214  +  if( (fg & MEM_Str)==0 ){
   215    215       hardStringify(p, i);
   216    216     }
   217         -  assert( (fg & STK_Dyn)==0 );
          217  +  assert( (fg & MEM_Dyn)==0 );
   218    218     z = sqliteMallocRaw( pStack->n );
   219    219     if( z==0 ) return 1;
   220         -  memcpy(z, p->zStack[i], pStack->n);
   221         -  p->zStack[i] = z;
   222         -  pStack->flags |= STK_Dyn;
          220  +  memcpy(z, p->aStack[i].z, pStack->n);
          221  +  p->aStack[i].z = z;
          222  +  pStack->flags |= MEM_Dyn;
   223    223     return 0;
   224    224   }
   225    225   
   226    226   /*
   227         -** An ephemeral string value (signified by the STK_Ephem flag) contains
          227  +** An ephemeral string value (signified by the MEM_Ephem flag) contains
   228    228   ** a pointer to a dynamically allocated string where some other entity
   229    229   ** is responsible for deallocating that string.  Because the stack entry
   230    230   ** does not control the string, it might be deleted without the stack
   231    231   ** entry knowing it.
   232    232   **
   233    233   ** This routine converts an ephemeral string into a dynamically allocated
   234    234   ** string that the stack entry itself controls.  In other words, it
   235         -** converts an STK_Ephem string into an STK_Dyn string.
          235  +** converts an MEM_Ephem string into an MEM_Dyn string.
   236    236   */
   237    237   #define Deephemeralize(P,I) \
   238         -   if( ((P)->aStack[I].flags&STK_Ephem)!=0 && hardDeephem(P,I) ){ goto no_mem;}
          238  +   if( ((P)->aStack[I].flags&MEM_Ephem)!=0 && hardDeephem(P,I) ){ goto no_mem;}
   239    239   static int hardDeephem(Vdbe *p, int i){
   240         -  Stack *pStack = &p->aStack[i];
   241         -  char **pzStack = &p->zStack[i];
          240  +  Mem *pStack = &p->aStack[i];
   242    241     char *z;
   243         -  assert( (pStack->flags & STK_Ephem)!=0 );
          242  +  assert( (pStack->flags & MEM_Ephem)!=0 );
   244    243     z = sqliteMallocRaw( pStack->n );
   245    244     if( z==0 ) return 1;
   246         -  memcpy(z, *pzStack, pStack->n);
   247         -  *pzStack = z;
   248         -  pStack->flags &= ~STK_Ephem;
   249         -  pStack->flags |= STK_Dyn;
          245  +  memcpy(z, pStack->z, pStack->n);
          246  +  pStack->z = z;
          247  +  pStack->flags &= ~MEM_Ephem;
          248  +  pStack->flags |= MEM_Dyn;
   250    249     return 0;
   251    250   }
   252    251   
   253    252   /*
   254    253   ** Release the memory associated with the given stack level
   255    254   */
   256         -#define Release(P,I)  if((P)->aStack[I].flags&STK_Dyn){ hardRelease(P,I); }
          255  +#define Release(P,I)  if((P)->aStack[I].flags&MEM_Dyn){ hardRelease(P,I); }
   257    256   static void hardRelease(Vdbe *p, int i){
   258         -  sqliteFree(p->zStack[i]);
   259         -  p->zStack[i] = 0;
   260         -  p->aStack[i].flags &= ~(STK_Str|STK_Dyn|STK_Static|STK_Ephem);
          257  +  sqliteFree(p->aStack[i].z);
          258  +  p->aStack[i].z = 0;
          259  +  p->aStack[i].flags &= ~(MEM_Str|MEM_Dyn|MEM_Static|MEM_Ephem);
   261    260   }
   262    261   
   263    262   /*
   264    263   ** Return TRUE if zNum is a 32-bit signed integer and write
   265    264   ** the value of the integer into *pNum.  If zNum is not an integer
   266    265   ** or is an integer that is too large to be expressed with just 32
   267    266   ** bits, then return false.
................................................................................
   293    292   ** Convert the given stack entity into a integer if it isn't one
   294    293   ** already.
   295    294   **
   296    295   ** Any prior string or real representation is invalidated.  
   297    296   ** NULLs are converted into 0.
   298    297   */
   299    298   #define Integerify(P,I) \
   300         -    if(((P)->aStack[(I)].flags&STK_Int)==0){ hardIntegerify(P,I); }
          299  +    if(((P)->aStack[(I)].flags&MEM_Int)==0){ hardIntegerify(P,I); }
   301    300   static void hardIntegerify(Vdbe *p, int i){
   302         -  if( p->aStack[i].flags & STK_Real ){
          301  +  if( p->aStack[i].flags & MEM_Real ){
   303    302       p->aStack[i].i = (int)p->aStack[i].r;
   304    303       Release(p, i);
   305         -  }else if( p->aStack[i].flags & STK_Str ){
   306         -    toInt(p->zStack[i], &p->aStack[i].i);
          304  +  }else if( p->aStack[i].flags & MEM_Str ){
          305  +    toInt(p->aStack[i].z, &p->aStack[i].i);
   307    306       Release(p, i);
   308    307     }else{
   309    308       p->aStack[i].i = 0;
   310    309     }
   311         -  p->aStack[i].flags = STK_Int;
          310  +  p->aStack[i].flags = MEM_Int;
   312    311   }
   313    312   
   314    313   /*
   315    314   ** Get a valid Real representation for the given stack element.
   316    315   **
   317    316   ** Any prior string or integer representation is retained.
   318    317   ** NULLs are converted into 0.0.
   319    318   */
   320    319   #define Realify(P,I) \
   321         -    if(((P)->aStack[(I)].flags&STK_Real)==0){ hardRealify(P,I); }
          320  +    if(((P)->aStack[(I)].flags&MEM_Real)==0){ hardRealify(P,I); }
   322    321   static void hardRealify(Vdbe *p, int i){
   323         -  if( p->aStack[i].flags & STK_Str ){
   324         -    p->aStack[i].r = sqliteAtoF(p->zStack[i]);
   325         -  }else if( p->aStack[i].flags & STK_Int ){
          322  +  if( p->aStack[i].flags & MEM_Str ){
          323  +    p->aStack[i].r = sqliteAtoF(p->aStack[i].z);
          324  +  }else if( p->aStack[i].flags & MEM_Int ){
   326    325       p->aStack[i].r = p->aStack[i].i;
   327    326     }else{
   328    327       p->aStack[i].r = 0.0;
   329    328     }
   330         -  p->aStack[i].flags |= STK_Real;
          329  +  p->aStack[i].flags |= MEM_Real;
   331    330   }
   332    331   
   333    332   /*
   334    333   ** The parameters are pointers to the head of two sorted lists
   335    334   ** of Sorter structures.  Merge these two lists together and return
   336    335   ** a single sorted list.  This routine forms the core of the merge-sort
   337    336   ** algorithm.
................................................................................
   482    481   int sqliteVdbeExec(
   483    482     Vdbe *p                    /* The VDBE */
   484    483   ){
   485    484     int pc;                    /* The program counter */
   486    485     Op *pOp;                   /* Current operation */
   487    486     int rc = SQLITE_OK;        /* Value to return */
   488    487     sqlite *db = p->db;        /* The database */
   489         -  char **zStack = p->zStack; /* Text stack */
   490         -  Stack *aStack = p->aStack; /* Additional stack information */
          488  +  Mem *aStack = p->aStack;   /* The operand stack */
   491    489     char zBuf[100];            /* Space to sprintf() an integer */
   492    490   #ifdef VDBE_PROFILE
   493    491     unsigned long long start;  /* CPU clock count at start of opcode */
   494    492     int origPc;                /* Program counter at start of opcode */
   495    493   #endif
   496    494   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   497    495     int nProgressOps = 0;      /* Opcodes executed since progress callback. */
................................................................................
   658    656   **
   659    657   ** The integer value P1 is pushed onto the stack.  If P3 is not zero
   660    658   ** then it is assumed to be a string representation of the same integer.
   661    659   */
   662    660   case OP_Integer: {
   663    661     int i = ++p->tos;
   664    662     aStack[i].i = pOp->p1;
   665         -  aStack[i].flags = STK_Int;
          663  +  aStack[i].flags = MEM_Int;
   666    664     if( pOp->p3 ){
   667         -    zStack[i] = pOp->p3;
   668         -    aStack[i].flags |= STK_Str | STK_Static;
          665  +    aStack[i].z = pOp->p3;
          666  +    aStack[i].flags |= MEM_Str | MEM_Static;
   669    667       aStack[i].n = strlen(pOp->p3)+1;
   670    668     }
   671    669     break;
   672    670   }
   673    671   
   674    672   /* Opcode: String * * P3
   675    673   **
................................................................................
   677    675   ** NULL is pushed onto the stack.
   678    676   */
   679    677   case OP_String: {
   680    678     int i = ++p->tos;
   681    679     char *z;
   682    680     z = pOp->p3;
   683    681     if( z==0 ){
   684         -    zStack[i] = 0;
          682  +    aStack[i].z = 0;
   685    683       aStack[i].n = 0;
   686         -    aStack[i].flags = STK_Null;
          684  +    aStack[i].flags = MEM_Null;
   687    685     }else{
   688         -    zStack[i] = z;
          686  +    aStack[i].z = z;
   689    687       aStack[i].n = strlen(z) + 1;
   690         -    aStack[i].flags = STK_Str | STK_Static;
          688  +    aStack[i].flags = MEM_Str | MEM_Static;
   691    689     }
   692    690     break;
   693    691   }
   694    692   
   695    693   /* Opcode: Variable P1 * *
   696    694   **
   697    695   ** Push the value of variable P1 onto the stack.  A variable is
................................................................................
   701    699   ** right beginning with 1.  The values of variables are set using the
   702    700   ** sqlite_bind() API.
   703    701   */
   704    702   case OP_Variable: {
   705    703     int i = ++p->tos;
   706    704     int j = pOp->p1 - 1;
   707    705     if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){
   708         -    zStack[i] = p->azVar[j];
          706  +    aStack[i].z = p->azVar[j];
   709    707       aStack[i].n = p->anVar[j];
   710         -    aStack[i].flags = STK_Str | STK_Static;
          708  +    aStack[i].flags = MEM_Str | MEM_Static;
   711    709     }else{
   712         -    zStack[i] = 0;
          710  +    aStack[i].z = 0;
   713    711       aStack[i].n = 0;
   714         -    aStack[i].flags = STK_Null;
          712  +    aStack[i].flags = MEM_Null;
   715    713     }
   716    714     break;
   717    715   }
   718    716   
   719    717   /* Opcode: Pop P1 * *
   720    718   **
   721    719   ** P1 elements are popped off of the top of stack and discarded.
................................................................................
   742    740   ** Also see the Pull instruction.
   743    741   */
   744    742   case OP_Dup: {
   745    743     int i = p->tos - pOp->p1;
   746    744     int j = ++p->tos;
   747    745     VERIFY( if( i<0 ) goto not_enough_stack; )
   748    746     memcpy(&aStack[j], &aStack[i], sizeof(aStack[i])-NBFS);
   749         -  if( aStack[j].flags & STK_Str ){
   750         -    int isStatic = (aStack[j].flags & STK_Static)!=0;
          747  +  if( aStack[j].flags & MEM_Str ){
          748  +    int isStatic = (aStack[j].flags & MEM_Static)!=0;
   751    749       if( pOp->p2 || isStatic ){
   752         -      zStack[j] = zStack[i];
   753         -      aStack[j].flags &= ~STK_Dyn;
   754         -      if( !isStatic ) aStack[j].flags |= STK_Ephem;
          750  +      aStack[j].z = aStack[i].z;
          751  +      aStack[j].flags &= ~MEM_Dyn;
          752  +      if( !isStatic ) aStack[j].flags |= MEM_Ephem;
   755    753       }else if( aStack[i].n<=NBFS ){
   756         -      memcpy(aStack[j].z, zStack[i], aStack[j].n);
   757         -      zStack[j] = aStack[j].z;
   758         -      aStack[j].flags &= ~(STK_Static|STK_Dyn|STK_Ephem);
          754  +      memcpy(aStack[j].zShort, aStack[i].z, aStack[j].n);
          755  +      aStack[j].z = aStack[j].zShort;
          756  +      aStack[j].flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
   759    757       }else{
   760         -      zStack[j] = sqliteMallocRaw( aStack[j].n );
   761         -      if( zStack[j]==0 ) goto no_mem;
   762         -      memcpy(zStack[j], zStack[i], aStack[j].n);
   763         -      aStack[j].flags &= ~(STK_Static|STK_Ephem);
   764         -      aStack[j].flags |= STK_Dyn;
          758  +      aStack[j].z = sqliteMallocRaw( aStack[j].n );
          759  +      if( aStack[j].z==0 ) goto no_mem;
          760  +      memcpy(aStack[j].z, aStack[i].z, aStack[j].n);
          761  +      aStack[j].flags &= ~(MEM_Static|MEM_Ephem);
          762  +      aStack[j].flags |= MEM_Dyn;
   765    763       }
   766    764     }
   767    765     break;
   768    766   }
   769    767   
   770    768   /* Opcode: Pull P1 * *
   771    769   **
................................................................................
   777    775   **
   778    776   ** See also the Dup instruction.
   779    777   */
   780    778   case OP_Pull: {
   781    779     int from = p->tos - pOp->p1;
   782    780     int to = p->tos;
   783    781     int i;
   784         -  Stack ts;
   785         -  char *tz;
          782  +  Mem ts;
   786    783     VERIFY( if( from<0 ) goto not_enough_stack; )
   787    784     Deephemeralize(p, from);
   788    785     ts = aStack[from];
   789         -  tz = zStack[from];
   790    786     Deephemeralize(p, to);
   791    787     for(i=from; i<to; i++){
   792    788       Deephemeralize(p, i+1);
   793    789       aStack[i] = aStack[i+1];
   794         -    assert( (aStack[i].flags & STK_Ephem)==0 );
   795         -    if( aStack[i].flags & (STK_Dyn|STK_Static) ){
   796         -      zStack[i] = zStack[i+1];
          790  +    assert( (aStack[i].flags & MEM_Ephem)==0 );
          791  +    if( aStack[i].flags & (MEM_Dyn|MEM_Static) ){
          792  +      aStack[i].z = aStack[i+1].z;
   797    793       }else{
   798         -      zStack[i] = aStack[i].z;
          794  +      aStack[i].z = aStack[i].zShort;
   799    795       }
   800    796     }
   801    797     aStack[to] = ts;
   802         -  assert( (aStack[to].flags & STK_Ephem)==0 );
   803         -  if( aStack[to].flags & (STK_Dyn|STK_Static) ){
   804         -    zStack[to] = tz;
   805         -  }else{
   806         -    zStack[to] = aStack[to].z;
          798  +  assert( (aStack[to].flags & MEM_Ephem)==0 );
          799  +  if( (aStack[to].flags & (MEM_Dyn|MEM_Static))==0 ){
          800  +    aStack[to].z = aStack[to].zShort;
   807    801     }
   808    802     break;
   809    803   }
   810    804   
   811    805   /* Opcode: Push P1 * *
   812    806   **
   813    807   ** Overwrite the value of the P1-th element down on the
................................................................................
   815    809   ** of the top of the stack.  Then pop the top of the stack.
   816    810   */
   817    811   case OP_Push: {
   818    812     int from = p->tos;
   819    813     int to = p->tos - pOp->p1;
   820    814   
   821    815     VERIFY( if( to<0 ) goto not_enough_stack; )
   822         -  if( aStack[to].flags & STK_Dyn ){
   823         -    sqliteFree(zStack[to]);
          816  +  if( aStack[to].flags & MEM_Dyn ){
          817  +    sqliteFree(aStack[to].z);
   824    818     }
   825    819     Deephemeralize(p, from);
   826    820     aStack[to] = aStack[from];
   827         -  if( aStack[to].flags & (STK_Dyn|STK_Static|STK_Ephem) ){
   828         -    zStack[to] = zStack[from];
          821  +  if( aStack[to].flags & (MEM_Dyn|MEM_Static|MEM_Ephem) ){
          822  +    aStack[to].z = aStack[from].z;
   829    823     }else{
   830         -    zStack[to] = aStack[to].z;
          824  +    aStack[to].z = aStack[to].zShort;
   831    825     }
   832    826     aStack[from].flags = 0;
   833    827     p->tos--;
   834    828     break;
   835    829   }
   836    830   
   837    831   /* Opcode: ColumnName P1 * P3
................................................................................
   853    847   ** 3rd parameter.
   854    848   */
   855    849   case OP_Callback: {
   856    850     int i = p->tos - pOp->p1 + 1;
   857    851     int j;
   858    852     VERIFY( if( i<0 ) goto not_enough_stack; )
   859    853     for(j=i; j<=p->tos; j++){
   860         -    if( aStack[j].flags & STK_Null ){
   861         -      zStack[j] = 0;
          854  +    if( aStack[j].flags & MEM_Null ){
          855  +      aStack[j].z = 0;
   862    856       }else{
   863    857         Stringify(p, j);
   864    858       }
          859  +    p->zArgv[j] = aStack[j].z;
   865    860     }
   866         -  zStack[p->tos+1] = 0;
          861  +  p->zArgv[p->tos+1] = 0;
   867    862     if( p->xCallback==0 ){
   868         -    p->azResColumn = &zStack[i];
          863  +    p->azResColumn = &p->zArgv[i];
   869    864       p->nResColumn = pOp->p1;
   870    865       p->popStack = pOp->p1;
   871    866       p->pc = pc + 1;
   872    867       return SQLITE_ROW;
   873    868     }
   874    869     if( sqliteSafetyOff(db) ) goto abort_due_to_misuse; 
   875         -  if( p->xCallback(p->pCbArg, pOp->p1, &zStack[i], p->azColName)!=0 ){
          870  +  if( p->xCallback(p->pCbArg, pOp->p1, &p->zArgv[i], p->azColName)!=0 ){
   876    871       rc = SQLITE_ABORT;
   877    872     }
   878    873     if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
   879    874     p->nCallback++;
   880    875     sqliteVdbePopStack(p, pOp->p1);
   881    876     if( sqlite_malloc_failed ) goto no_mem;
   882    877     break;
................................................................................
   935    930     nField = pOp->p1;
   936    931     zSep = pOp->p3;
   937    932     if( zSep==0 ) zSep = "";
   938    933     nSep = strlen(zSep);
   939    934     VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
   940    935     nByte = 1 - nSep;
   941    936     for(i=p->tos-nField+1; i<=p->tos; i++){
   942         -    if( aStack[i].flags & STK_Null ){
          937  +    if( aStack[i].flags & MEM_Null ){
   943    938         nByte = -1;
   944    939         break;
   945    940       }else{
   946    941         Stringify(p, i);
   947    942         nByte += aStack[i].n - 1 + nSep;
   948    943       }
   949    944     }
   950    945     if( nByte<0 ){
   951    946       if( pOp->p2==0 ) sqliteVdbePopStack(p, nField);
   952    947       p->tos++;
   953         -    aStack[p->tos].flags = STK_Null;
   954         -    zStack[p->tos] = 0;
          948  +    aStack[p->tos].flags = MEM_Null;
          949  +    aStack[p->tos].z = 0;
   955    950       break;
   956    951     }
   957    952     zNew = sqliteMallocRaw( nByte );
   958    953     if( zNew==0 ) goto no_mem;
   959    954     j = 0;
   960    955     for(i=p->tos-nField+1; i<=p->tos; i++){
   961         -    if( (aStack[i].flags & STK_Null)==0 ){
   962         -      memcpy(&zNew[j], zStack[i], aStack[i].n-1);
          956  +    if( (aStack[i].flags & MEM_Null)==0 ){
          957  +      memcpy(&zNew[j], aStack[i].z, aStack[i].n-1);
   963    958         j += aStack[i].n-1;
   964    959       }
   965    960       if( nSep>0 && i<p->tos ){
   966    961         memcpy(&zNew[j], zSep, nSep);
   967    962         j += nSep;
   968    963       }
   969    964     }
   970    965     zNew[j] = 0;
   971    966     if( pOp->p2==0 ) sqliteVdbePopStack(p, nField);
   972    967     p->tos++;
   973    968     aStack[p->tos].n = nByte;
   974         -  aStack[p->tos].flags = STK_Str|STK_Dyn;
   975         -  zStack[p->tos] = zNew;
          969  +  aStack[p->tos].flags = MEM_Str|MEM_Dyn;
          970  +  aStack[p->tos].z = zNew;
   976    971     break;
   977    972   }
   978    973   
   979    974   /* Opcode: Add * * *
   980    975   **
   981    976   ** Pop the top two elements from the stack, add them together,
   982    977   ** and push the result back onto the stack.  If either element
................................................................................
  1026   1021   case OP_Subtract:
  1027   1022   case OP_Multiply:
  1028   1023   case OP_Divide:
  1029   1024   case OP_Remainder: {
  1030   1025     int tos = p->tos;
  1031   1026     int nos = tos - 1;
  1032   1027     VERIFY( if( nos<0 ) goto not_enough_stack; )
  1033         -  if( ((aStack[tos].flags | aStack[nos].flags) & STK_Null)!=0 ){
         1028  +  if( ((aStack[tos].flags | aStack[nos].flags) & MEM_Null)!=0 ){
  1034   1029       POPSTACK;
  1035   1030       Release(p, nos);
  1036         -    aStack[nos].flags = STK_Null;
  1037         -  }else if( (aStack[tos].flags & aStack[nos].flags & STK_Int)==STK_Int ){
         1031  +    aStack[nos].flags = MEM_Null;
         1032  +  }else if( (aStack[tos].flags & aStack[nos].flags & MEM_Int)==MEM_Int ){
  1038   1033       int a, b;
  1039   1034       a = aStack[tos].i;
  1040   1035       b = aStack[nos].i;
  1041   1036       switch( pOp->opcode ){
  1042   1037         case OP_Add:         b += a;       break;
  1043   1038         case OP_Subtract:    b -= a;       break;
  1044   1039         case OP_Multiply:    b *= a;       break;
................................................................................
  1052   1047           b %= a;
  1053   1048           break;
  1054   1049         }
  1055   1050       }
  1056   1051       POPSTACK;
  1057   1052       Release(p, nos);
  1058   1053       aStack[nos].i = b;
  1059         -    aStack[nos].flags = STK_Int;
         1054  +    aStack[nos].flags = MEM_Int;
  1060   1055     }else{
  1061   1056       double a, b;
  1062   1057       Realify(p, tos);
  1063   1058       Realify(p, nos);
  1064   1059       a = aStack[tos].r;
  1065   1060       b = aStack[nos].r;
  1066   1061       switch( pOp->opcode ){
................................................................................
  1079   1074           b = ib % ia;
  1080   1075           break;
  1081   1076         }
  1082   1077       }
  1083   1078       POPSTACK;
  1084   1079       Release(p, nos);
  1085   1080       aStack[nos].r = b;
  1086         -    aStack[nos].flags = STK_Real;
         1081  +    aStack[nos].flags = MEM_Real;
  1087   1082     }
  1088   1083     break;
  1089   1084   
  1090   1085   divide_by_zero:
  1091   1086     sqliteVdbePopStack(p, 2);
  1092   1087     p->tos = nos;
  1093         -  aStack[nos].flags = STK_Null;
         1088  +  aStack[nos].flags = MEM_Null;
  1094   1089     break;
  1095   1090   }
  1096   1091   
  1097   1092   /* Opcode: Function P1 * P3
  1098   1093   **
  1099   1094   ** Invoke a user function (P3 is a pointer to a Function structure that
  1100   1095   ** defines the function) with P1 string arguments taken from the stack.
................................................................................
  1106   1101     int n, i;
  1107   1102     sqlite_func ctx;
  1108   1103   
  1109   1104     n = pOp->p1;
  1110   1105     VERIFY( if( n<0 ) goto bad_instruction; )
  1111   1106     VERIFY( if( p->tos+1<n ) goto not_enough_stack; )
  1112   1107     for(i=p->tos-n+1; i<=p->tos; i++){
  1113         -    if( aStack[i].flags & STK_Null ){
  1114         -      zStack[i] = 0;
         1108  +    if( aStack[i].flags & MEM_Null ){
         1109  +      aStack[i].z = 0;
  1115   1110       }else{
  1116   1111         Stringify(p, i);
  1117   1112       }
         1113  +    p->zArgv[i] = aStack[i].z;
  1118   1114     }
  1119   1115     ctx.pFunc = (FuncDef*)pOp->p3;
  1120         -  ctx.s.flags = STK_Null;
  1121         -  ctx.z = 0;
         1116  +  ctx.s.flags = MEM_Null;
         1117  +  ctx.s.z = 0;
  1122   1118     ctx.isError = 0;
  1123   1119     ctx.isStep = 0;
  1124   1120     if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
  1125         -  (*ctx.pFunc->xFunc)(&ctx, n, (const char**)&zStack[p->tos-n+1]);
         1121  +  (*ctx.pFunc->xFunc)(&ctx, n, (const char**)&p->zArgv[p->tos-n+1]);
  1126   1122     if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
  1127   1123     sqliteVdbePopStack(p, n);
  1128   1124     p->tos++;
  1129   1125     aStack[p->tos] = ctx.s;
  1130         -  if( ctx.s.flags & STK_Dyn ){
  1131         -    zStack[p->tos] = ctx.z;
  1132         -  }else if( ctx.s.flags & STK_Str ){
  1133         -    zStack[p->tos] = aStack[p->tos].z;
         1126  +  if( ctx.s.flags & MEM_Dyn ){
         1127  +    aStack[p->tos].z = ctx.s.z;
         1128  +  }else if( ctx.s.flags & MEM_Str ){
         1129  +    aStack[p->tos].z = aStack[p->tos].zShort;
  1134   1130     }else{
  1135         -    zStack[p->tos] = 0;
         1131  +    aStack[p->tos].z = 0;
  1136   1132     }
  1137   1133     if( ctx.isError ){
  1138   1134       sqliteSetString(&p->zErrMsg, 
  1139         -       zStack[p->tos] ? zStack[p->tos] : "user function error", (char*)0);
         1135  +       aStack[p->tos].z ? aStack[p->tos].z : "user function error", (char*)0);
  1140   1136       rc = SQLITE_ERROR;
  1141   1137     }
  1142   1138     break;
  1143   1139   }
  1144   1140   
  1145   1141   /* Opcode: BitAnd * * *
  1146   1142   **
................................................................................
  1174   1170   case OP_BitOr:
  1175   1171   case OP_ShiftLeft:
  1176   1172   case OP_ShiftRight: {
  1177   1173     int tos = p->tos;
  1178   1174     int nos = tos - 1;
  1179   1175     int a, b;
  1180   1176     VERIFY( if( nos<0 ) goto not_enough_stack; )
  1181         -  if( (aStack[tos].flags | aStack[nos].flags) & STK_Null ){
         1177  +  if( (aStack[tos].flags | aStack[nos].flags) & MEM_Null ){
  1182   1178       POPSTACK;
  1183   1179       Release(p,nos);
  1184         -    aStack[nos].flags = STK_Null;
         1180  +    aStack[nos].flags = MEM_Null;
  1185   1181       break;
  1186   1182     }
  1187   1183     Integerify(p, tos);
  1188   1184     Integerify(p, nos);
  1189   1185     a = aStack[tos].i;
  1190   1186     b = aStack[nos].i;
  1191   1187     switch( pOp->opcode ){
................................................................................
  1194   1190       case OP_ShiftLeft:   a <<= b;    break;
  1195   1191       case OP_ShiftRight:  a >>= b;    break;
  1196   1192       default:   /* CANT HAPPEN */     break;
  1197   1193     }
  1198   1194     POPSTACK;
  1199   1195     Release(p, nos);
  1200   1196     aStack[nos].i = a;
  1201         -  aStack[nos].flags = STK_Int;
         1197  +  aStack[nos].flags = MEM_Int;
  1202   1198     break;
  1203   1199   }
  1204   1200   
  1205   1201   /* Opcode: AddImm  P1 * *
  1206   1202   ** 
  1207   1203   ** Add the value P1 to whatever is on top of the stack.  The result
  1208   1204   ** is always an integer.
................................................................................
  1227   1223   ** current value if P1==0, or to the least integer that is strictly
  1228   1224   ** greater than its current value if P1==1.
  1229   1225   */
  1230   1226   case OP_ForceInt: {
  1231   1227     int tos = p->tos;
  1232   1228     int v;
  1233   1229     VERIFY( if( tos<0 ) goto not_enough_stack; )
  1234         -  if( (aStack[tos].flags & (STK_Int|STK_Real))==0
  1235         -         && (zStack[tos]==0 || sqliteIsNumber(zStack[tos])==0) ){
         1230  +  if( (aStack[tos].flags & (MEM_Int|MEM_Real))==0
         1231  +         && (aStack[tos].z==0 || sqliteIsNumber(aStack[tos].z)==0) ){
  1236   1232       POPSTACK;
  1237   1233       pc = pOp->p2 - 1;
  1238   1234       break;
  1239   1235     }
  1240         -  if( aStack[tos].flags & STK_Int ){
         1236  +  if( aStack[tos].flags & MEM_Int ){
  1241   1237       v = aStack[tos].i + (pOp->p1!=0);
  1242   1238     }else{
  1243   1239       Realify(p, tos);
  1244   1240       v = (int)aStack[tos].r;
  1245   1241       if( aStack[tos].r>(double)v ) v++;
  1246   1242       if( pOp->p1 && aStack[tos].r==(double)v ) v++;
  1247   1243     }
  1248         -  if( aStack[tos].flags & STK_Dyn ) sqliteFree(zStack[tos]);
  1249         -  zStack[tos] = 0;
         1244  +  if( aStack[tos].flags & MEM_Dyn ) sqliteFree(aStack[tos].z);
         1245  +  aStack[tos].z = 0;
  1250   1246     aStack[tos].i = v;
  1251         -  aStack[tos].flags = STK_Int;
         1247  +  aStack[tos].flags = MEM_Int;
  1252   1248     break;
  1253   1249   }
  1254   1250   
  1255   1251   /* Opcode: MustBeInt P1 P2 *
  1256   1252   ** 
  1257   1253   ** Force the top of the stack to be an integer.  If the top of the
  1258   1254   ** stack is not an integer and cannot be converted into an integer
................................................................................
  1262   1258   ** If the top of the stack is not an integer and P2 is not zero and
  1263   1259   ** P1 is 1, then the stack is popped.  In all other cases, the depth
  1264   1260   ** of the stack is unchanged.
  1265   1261   */
  1266   1262   case OP_MustBeInt: {
  1267   1263     int tos = p->tos;
  1268   1264     VERIFY( if( tos<0 ) goto not_enough_stack; )
  1269         -  if( aStack[tos].flags & STK_Int ){
         1265  +  if( aStack[tos].flags & MEM_Int ){
  1270   1266       /* Do nothing */
  1271         -  }else if( aStack[tos].flags & STK_Real ){
         1267  +  }else if( aStack[tos].flags & MEM_Real ){
  1272   1268       int i = aStack[tos].r;
  1273   1269       double r = (double)i;
  1274   1270       if( r!=aStack[tos].r ){
  1275   1271         goto mismatch;
  1276   1272       }
  1277   1273       aStack[tos].i = i;
  1278         -  }else if( aStack[tos].flags & STK_Str ){
         1274  +  }else if( aStack[tos].flags & MEM_Str ){
  1279   1275       int v;
  1280         -    if( !toInt(zStack[tos], &v) ){
         1276  +    if( !toInt(aStack[tos].z, &v) ){
  1281   1277         double r;
  1282         -      if( !sqliteIsNumber(zStack[tos]) ){
         1278  +      if( !sqliteIsNumber(aStack[tos].z) ){
  1283   1279           goto mismatch;
  1284   1280         }
  1285   1281         Realify(p, tos);
  1286         -      assert( (aStack[tos].flags & STK_Real)!=0 );
         1282  +      assert( (aStack[tos].flags & MEM_Real)!=0 );
  1287   1283         v = aStack[tos].r;
  1288   1284         r = (double)v;
  1289   1285         if( r!=aStack[tos].r ){
  1290   1286           goto mismatch;
  1291   1287         }
  1292   1288       }
  1293   1289       aStack[tos].i = v;
  1294   1290     }else{
  1295   1291       goto mismatch;
  1296   1292     }
  1297   1293     Release(p, tos);
  1298         -  aStack[tos].flags = STK_Int;
         1294  +  aStack[tos].flags = MEM_Int;
  1299   1295     break;
  1300   1296   
  1301   1297   mismatch:
  1302   1298     if( pOp->p2==0 ){
  1303   1299       rc = SQLITE_MISMATCH;
  1304   1300       goto abort_due_to_error;
  1305   1301     }else{
................................................................................
  1429   1425     int tos = p->tos;
  1430   1426     int nos = tos - 1;
  1431   1427     int c, v;
  1432   1428     int ft, fn;
  1433   1429     VERIFY( if( nos<0 ) goto not_enough_stack; )
  1434   1430     ft = aStack[tos].flags;
  1435   1431     fn = aStack[nos].flags;
  1436         -  if( (ft | fn) & STK_Null ){
         1432  +  if( (ft | fn) & MEM_Null ){
  1437   1433       POPSTACK;
  1438   1434       POPSTACK;
  1439   1435       if( pOp->p2 ){
  1440   1436         if( pOp->p1 ) pc = pOp->p2-1;
  1441   1437       }else{
  1442   1438         p->tos++;
  1443         -      aStack[nos].flags = STK_Null;
         1439  +      aStack[nos].flags = MEM_Null;
  1444   1440       }
  1445   1441       break;
  1446         -  }else if( (ft & fn & STK_Int)==STK_Int ){
         1442  +  }else if( (ft & fn & MEM_Int)==MEM_Int ){
  1447   1443       c = aStack[nos].i - aStack[tos].i;
  1448         -  }else if( (ft & STK_Int)!=0 && (fn & STK_Str)!=0 && toInt(zStack[nos],&v) ){
         1444  +  }else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(aStack[nos].z,&v) ){
  1449   1445       Release(p, nos);
  1450   1446       aStack[nos].i = v;
  1451         -    aStack[nos].flags = STK_Int;
         1447  +    aStack[nos].flags = MEM_Int;
  1452   1448       c = aStack[nos].i - aStack[tos].i;
  1453         -  }else if( (fn & STK_Int)!=0 && (ft & STK_Str)!=0 && toInt(zStack[tos],&v) ){
         1449  +  }else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(aStack[tos].z,&v) ){
  1454   1450       Release(p, tos);
  1455   1451       aStack[tos].i = v;
  1456         -    aStack[tos].flags = STK_Int;
         1452  +    aStack[tos].flags = MEM_Int;
  1457   1453       c = aStack[nos].i - aStack[tos].i;
  1458   1454     }else{
  1459   1455       Stringify(p, tos);
  1460   1456       Stringify(p, nos);
  1461         -    c = sqliteCompare(zStack[nos], zStack[tos]);
         1457  +    c = sqliteCompare(aStack[nos].z, aStack[tos].z);
  1462   1458     }
  1463   1459     switch( pOp->opcode ){
  1464   1460       case OP_Eq:    c = c==0;     break;
  1465   1461       case OP_Ne:    c = c!=0;     break;
  1466   1462       case OP_Lt:    c = c<0;      break;
  1467   1463       case OP_Le:    c = c<=0;     break;
  1468   1464       case OP_Gt:    c = c>0;      break;
................................................................................
  1470   1466     }
  1471   1467     POPSTACK;
  1472   1468     POPSTACK;
  1473   1469     if( pOp->p2 ){
  1474   1470       if( c ) pc = pOp->p2-1;
  1475   1471     }else{
  1476   1472       p->tos++;
  1477         -    aStack[nos].flags = STK_Int;
         1473  +    aStack[nos].flags = MEM_Int;
  1478   1474       aStack[nos].i = c;
  1479   1475     }
  1480   1476     break;
  1481   1477   }
  1482   1478   /* INSERT NO CODE HERE!
  1483   1479   **
  1484   1480   ** The opcode numbers are extracted from this source file by doing
................................................................................
  1592   1588   case OP_StrLe:
  1593   1589   case OP_StrGt:
  1594   1590   case OP_StrGe: {
  1595   1591     int tos = p->tos;
  1596   1592     int nos = tos - 1;
  1597   1593     int c;
  1598   1594     VERIFY( if( nos<0 ) goto not_enough_stack; )
  1599         -  if( (aStack[nos].flags | aStack[tos].flags) & STK_Null ){
         1595  +  if( (aStack[nos].flags | aStack[tos].flags) & MEM_Null ){
  1600   1596       POPSTACK;
  1601   1597       POPSTACK;
  1602   1598       if( pOp->p2 ){
  1603   1599         if( pOp->p1 ) pc = pOp->p2-1;
  1604   1600       }else{
  1605   1601         p->tos++;
  1606         -      aStack[nos].flags = STK_Null;
         1602  +      aStack[nos].flags = MEM_Null;
  1607   1603       }
  1608   1604       break;
  1609   1605     }else{
  1610   1606       Stringify(p, tos);
  1611   1607       Stringify(p, nos);
  1612         -    c = strcmp(zStack[nos], zStack[tos]);
         1608  +    c = strcmp(aStack[nos].z, aStack[tos].z);
  1613   1609     }
  1614   1610     /* The asserts on each case of the following switch are there to verify
  1615   1611     ** that string comparison opcodes are always exactly 6 greater than the
  1616   1612     ** corresponding numeric comparison opcodes.  The code generator depends
  1617   1613     ** on this fact.
  1618   1614     */
  1619   1615     switch( pOp->opcode ){
................................................................................
  1626   1622     }
  1627   1623     POPSTACK;
  1628   1624     POPSTACK;
  1629   1625     if( pOp->p2 ){
  1630   1626       if( c ) pc = pOp->p2-1;
  1631   1627     }else{
  1632   1628       p->tos++;
  1633         -    aStack[nos].flags = STK_Int;
         1629  +    aStack[nos].flags = MEM_Int;
  1634   1630       aStack[nos].i = c;
  1635   1631     }
  1636   1632     break;
  1637   1633   }
  1638   1634   
  1639   1635   /* Opcode: And * * *
  1640   1636   **
................................................................................
  1651   1647   case OP_And:
  1652   1648   case OP_Or: {
  1653   1649     int tos = p->tos;
  1654   1650     int nos = tos - 1;
  1655   1651     int v1, v2;    /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
  1656   1652   
  1657   1653     VERIFY( if( nos<0 ) goto not_enough_stack; )
  1658         -  if( aStack[tos].flags & STK_Null ){
         1654  +  if( aStack[tos].flags & MEM_Null ){
  1659   1655       v1 = 2;
  1660   1656     }else{
  1661   1657       Integerify(p, tos);
  1662   1658       v1 = aStack[tos].i==0;
  1663   1659     }
  1664         -  if( aStack[nos].flags & STK_Null ){
         1660  +  if( aStack[nos].flags & MEM_Null ){
  1665   1661       v2 = 2;
  1666   1662     }else{
  1667   1663       Integerify(p, nos);
  1668   1664       v2 = aStack[nos].i==0;
  1669   1665     }
  1670   1666     if( pOp->opcode==OP_And ){
  1671   1667       static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
................................................................................
  1673   1669     }else{
  1674   1670       static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
  1675   1671       v1 = or_logic[v1*3+v2];
  1676   1672     }
  1677   1673     POPSTACK;
  1678   1674     Release(p, nos);
  1679   1675     if( v1==2 ){
  1680         -    aStack[nos].flags = STK_Null;
         1676  +    aStack[nos].flags = MEM_Null;
  1681   1677     }else{
  1682   1678       aStack[nos].i = v1==0;
  1683         -    aStack[nos].flags = STK_Int;
         1679  +    aStack[nos].flags = MEM_Int;
  1684   1680     }
  1685   1681     break;
  1686   1682   }
  1687   1683   
  1688   1684   /* Opcode: Negative * * *
  1689   1685   **
  1690   1686   ** Treat the top of the stack as a numeric quantity.  Replace it
................................................................................
  1697   1693   ** with its absolute value. If the top of the stack is NULL
  1698   1694   ** its value is unchanged.
  1699   1695   */
  1700   1696   case OP_Negative:
  1701   1697   case OP_AbsValue: {
  1702   1698     int tos = p->tos;
  1703   1699     VERIFY( if( tos<0 ) goto not_enough_stack; )
  1704         -  if( aStack[tos].flags & STK_Real ){
         1700  +  if( aStack[tos].flags & MEM_Real ){
  1705   1701       Release(p, tos);
  1706   1702       if( pOp->opcode==OP_Negative || aStack[tos].r<0.0 ){
  1707   1703         aStack[tos].r = -aStack[tos].r;
  1708   1704       }
  1709         -    aStack[tos].flags = STK_Real;
  1710         -  }else if( aStack[tos].flags & STK_Int ){
         1705  +    aStack[tos].flags = MEM_Real;
         1706  +  }else if( aStack[tos].flags & MEM_Int ){
  1711   1707       Release(p, tos);
  1712   1708       if( pOp->opcode==OP_Negative ||  aStack[tos].i<0 ){
  1713   1709         aStack[tos].i = -aStack[tos].i;
  1714   1710       }
  1715         -    aStack[tos].flags = STK_Int;
  1716         -  }else if( aStack[tos].flags & STK_Null ){
         1711  +    aStack[tos].flags = MEM_Int;
         1712  +  }else if( aStack[tos].flags & MEM_Null ){
  1717   1713       /* Do nothing */
  1718   1714     }else{
  1719   1715       Realify(p, tos);
  1720   1716       Release(p, tos);
  1721   1717       if( pOp->opcode==OP_Negative ||  aStack[tos].r<0.0 ){
  1722   1718         aStack[tos].r = -aStack[tos].r;
  1723   1719       }
  1724         -    aStack[tos].flags = STK_Real;
         1720  +    aStack[tos].flags = MEM_Real;
  1725   1721     }
  1726   1722     break;
  1727   1723   }
  1728   1724   
  1729   1725   /* Opcode: Not * * *
  1730   1726   **
  1731   1727   ** Interpret the top of the stack as a boolean value.  Replace it
  1732   1728   ** with its complement.  If the top of the stack is NULL its value
  1733   1729   ** is unchanged.
  1734   1730   */
  1735   1731   case OP_Not: {
  1736   1732     int tos = p->tos;
  1737   1733     VERIFY( if( p->tos<0 ) goto not_enough_stack; )
  1738         -  if( aStack[tos].flags & STK_Null ) break;  /* Do nothing to NULLs */
         1734  +  if( aStack[tos].flags & MEM_Null ) break;  /* Do nothing to NULLs */
  1739   1735     Integerify(p, tos);
  1740   1736     Release(p, tos);
  1741   1737     aStack[tos].i = !aStack[tos].i;
  1742         -  aStack[tos].flags = STK_Int;
         1738  +  aStack[tos].flags = MEM_Int;
  1743   1739     break;
  1744   1740   }
  1745   1741   
  1746   1742   /* Opcode: BitNot * * *
  1747   1743   **
  1748   1744   ** Interpret the top of the stack as an value.  Replace it
  1749   1745   ** with its ones-complement.  If the top of the stack is NULL its
  1750   1746   ** value is unchanged.
  1751   1747   */
  1752   1748   case OP_BitNot: {
  1753   1749     int tos = p->tos;
  1754   1750     VERIFY( if( p->tos<0 ) goto not_enough_stack; )
  1755         -  if( aStack[tos].flags & STK_Null ) break;  /* Do nothing to NULLs */
         1751  +  if( aStack[tos].flags & MEM_Null ) break;  /* Do nothing to NULLs */
  1756   1752     Integerify(p, tos);
  1757   1753     Release(p, tos);
  1758   1754     aStack[tos].i = ~aStack[tos].i;
  1759         -  aStack[tos].flags = STK_Int;
         1755  +  aStack[tos].flags = MEM_Int;
  1760   1756     break;
  1761   1757   }
  1762   1758   
  1763   1759   /* Opcode: Noop * * *
  1764   1760   **
  1765   1761   ** Do nothing.  This instruction is often useful as a jump
  1766   1762   ** destination.
................................................................................
  1789   1785   ** If the value popped of the stack is NULL, then take the jump if P1
  1790   1786   ** is true and fall through if P1 is false.
  1791   1787   */
  1792   1788   case OP_If:
  1793   1789   case OP_IfNot: {
  1794   1790     int c;
  1795   1791     VERIFY( if( p->tos<0 ) goto not_enough_stack; )
  1796         -  if( aStack[p->tos].flags & STK_Null ){
         1792  +  if( aStack[p->tos].flags & MEM_Null ){
  1797   1793       c = pOp->p1;
  1798   1794     }else{
  1799   1795       Integerify(p, p->tos);
  1800   1796       c = aStack[p->tos].i;
  1801   1797       if( pOp->opcode==OP_IfNot ) c = !c;
  1802   1798     }
  1803   1799     POPSTACK;
................................................................................
  1813   1809   */
  1814   1810   case OP_IsNull: {
  1815   1811     int i, cnt;
  1816   1812     cnt = pOp->p1;
  1817   1813     if( cnt<0 ) cnt = -cnt;
  1818   1814     VERIFY( if( p->tos+1-cnt<0 ) goto not_enough_stack; )
  1819   1815     for(i=0; i<cnt; i++){
  1820         -    if( aStack[p->tos-i].flags & STK_Null ){
         1816  +    if( aStack[p->tos-i].flags & MEM_Null ){
  1821   1817         pc = pOp->p2-1;
  1822   1818         break;
  1823   1819       }
  1824   1820     }
  1825   1821     if( pOp->p1>0 ) sqliteVdbePopStack(p, cnt);
  1826   1822     break;
  1827   1823   }
................................................................................
  1833   1829   ** zero then leave the stack unchanged.
  1834   1830   */
  1835   1831   case OP_NotNull: {
  1836   1832     int i, cnt;
  1837   1833     cnt = pOp->p1;
  1838   1834     if( cnt<0 ) cnt = -cnt;
  1839   1835     VERIFY( if( p->tos+1-cnt<0 ) goto not_enough_stack; )
  1840         -  for(i=0; i<cnt && (aStack[p->tos-i].flags & STK_Null)==0; i++){}
         1836  +  for(i=0; i<cnt && (aStack[p->tos-i].flags & MEM_Null)==0; i++){}
  1841   1837     if( i>=cnt ) pc = pOp->p2-1;
  1842   1838     if( pOp->p1>0 ) sqliteVdbePopStack(p, cnt);
  1843   1839     break;
  1844   1840   }
  1845   1841   
  1846   1842   /* Opcode: MakeRecord P1 P2 *
  1847   1843   **
................................................................................
  1893   1889     ** of data(0).  Idx(k) contains the offset to the start of data(k).
  1894   1890     ** Idx(N) contains the total number of bytes in the record.
  1895   1891     */
  1896   1892     nField = pOp->p1;
  1897   1893     VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
  1898   1894     nByte = 0;
  1899   1895     for(i=p->tos-nField+1; i<=p->tos; i++){
  1900         -    if( (aStack[i].flags & STK_Null) ){
         1896  +    if( (aStack[i].flags & MEM_Null) ){
  1901   1897         addUnique = pOp->p2;
  1902   1898       }else{
  1903   1899         Stringify(p, i);
  1904   1900         nByte += aStack[i].n;
  1905   1901       }
  1906   1902     }
  1907   1903     if( addUnique ) nByte += sizeof(p->uniqueCnt);
................................................................................
  1929   1925       zNewRecord[j++] = addr & 0xff;
  1930   1926       if( idxWidth>1 ){
  1931   1927         zNewRecord[j++] = (addr>>8)&0xff;
  1932   1928         if( idxWidth>2 ){
  1933   1929           zNewRecord[j++] = (addr>>16)&0xff;
  1934   1930         }
  1935   1931       }
  1936         -    if( (aStack[i].flags & STK_Null)==0 ){
         1932  +    if( (aStack[i].flags & MEM_Null)==0 ){
  1937   1933         addr += aStack[i].n;
  1938   1934       }
  1939   1935     }
  1940   1936     zNewRecord[j++] = addr & 0xff;
  1941   1937     if( idxWidth>1 ){
  1942   1938       zNewRecord[j++] = (addr>>8)&0xff;
  1943   1939       if( idxWidth>2 ){
................................................................................
  1946   1942     }
  1947   1943     if( addUnique ){
  1948   1944       memcpy(&zNewRecord[j], &p->uniqueCnt, sizeof(p->uniqueCnt));
  1949   1945       p->uniqueCnt++;
  1950   1946       j += sizeof(p->uniqueCnt);
  1951   1947     }
  1952   1948     for(i=p->tos-nField+1; i<=p->tos; i++){
  1953         -    if( (aStack[i].flags & STK_Null)==0 ){
  1954         -      memcpy(&zNewRecord[j], zStack[i], aStack[i].n);
         1949  +    if( (aStack[i].flags & MEM_Null)==0 ){
         1950  +      memcpy(&zNewRecord[j], aStack[i].z, aStack[i].n);
  1955   1951         j += aStack[i].n;
  1956   1952       }
  1957   1953     }
  1958   1954     sqliteVdbePopStack(p, nField);
  1959   1955     p->tos++;
  1960   1956     aStack[p->tos].n = nByte;
  1961   1957     if( nByte<=NBFS ){
  1962   1958       assert( zNewRecord==zTemp );
  1963         -    memcpy(aStack[p->tos].z, zTemp, nByte);
  1964         -    zStack[p->tos] = aStack[p->tos].z;
  1965         -    aStack[p->tos].flags = STK_Str;
         1959  +    memcpy(aStack[p->tos].zShort, zTemp, nByte);
         1960  +    aStack[p->tos].z = aStack[p->tos].zShort;
         1961  +    aStack[p->tos].flags = MEM_Str;
  1966   1962     }else{
  1967   1963       assert( zNewRecord!=zTemp );
  1968         -    aStack[p->tos].flags = STK_Str | STK_Dyn;
  1969         -    zStack[p->tos] = zNewRecord;
         1964  +    aStack[p->tos].flags = MEM_Str | MEM_Dyn;
         1965  +    aStack[p->tos].z = zNewRecord;
  1970   1966     }
  1971   1967     break;
  1972   1968   }
  1973   1969   
  1974   1970   /* Opcode: MakeKey P1 P2 P3
  1975   1971   **
  1976   1972   ** Convert the top P1 entries of the stack into a single entry suitable
................................................................................
  2057   2053     nField = pOp->p1;
  2058   2054     VERIFY( if( p->tos+1+addRowid<nField ) goto not_enough_stack; )
  2059   2055     nByte = 0;
  2060   2056     for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){
  2061   2057       int flags = aStack[i].flags;
  2062   2058       int len;
  2063   2059       char *z;
  2064         -    if( flags & STK_Null ){
         2060  +    if( flags & MEM_Null ){
  2065   2061         nByte += 2;
  2066   2062         containsNull = 1;
  2067   2063       }else if( pOp->p3 && pOp->p3[j]=='t' ){
  2068   2064         Stringify(p, i);
  2069         -      aStack[i].flags &= ~(STK_Int|STK_Real);
         2065  +      aStack[i].flags &= ~(MEM_Int|MEM_Real);
  2070   2066         nByte += aStack[i].n+1;
  2071         -    }else if( (flags & (STK_Real|STK_Int))!=0 || sqliteIsNumber(zStack[i]) ){
  2072         -      if( (flags & (STK_Real|STK_Int))==STK_Int ){
         2067  +    }else if( (flags & (MEM_Real|MEM_Int))!=0 || sqliteIsNumber(aStack[i].z) ){
         2068  +      if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){
  2073   2069           aStack[i].r = aStack[i].i;
  2074         -      }else if( (flags & (STK_Real|STK_Int))==0 ){
  2075         -        aStack[i].r = sqliteAtoF(zStack[i]);
         2070  +      }else if( (flags & (MEM_Real|MEM_Int))==0 ){
         2071  +        aStack[i].r = sqliteAtoF(aStack[i].z);
  2076   2072         }
  2077   2073         Release(p, i);
  2078         -      z = aStack[i].z;
         2074  +      z = aStack[i].zShort;
  2079   2075         sqliteRealToSortable(aStack[i].r, z);
  2080   2076         len = strlen(z);
  2081         -      zStack[i] = 0;
  2082         -      aStack[i].flags = STK_Real;
         2077  +      aStack[i].z = 0;
         2078  +      aStack[i].flags = MEM_Real;
  2083   2079         aStack[i].n = len+1;
  2084   2080         nByte += aStack[i].n+1;
  2085   2081       }else{
  2086   2082         nByte += aStack[i].n+1;
  2087   2083       }
  2088   2084     }
  2089   2085     if( nByte+sizeof(u32)>MAX_BYTES_PER_ROW ){
................................................................................
  2095   2091       zNewKey = zTemp;
  2096   2092     }else{
  2097   2093       zNewKey = sqliteMallocRaw( nByte );
  2098   2094       if( zNewKey==0 ) goto no_mem;
  2099   2095     }
  2100   2096     j = 0;
  2101   2097     for(i=p->tos-nField+1; i<=p->tos; i++){
  2102         -    if( aStack[i].flags & STK_Null ){
         2098  +    if( aStack[i].flags & MEM_Null ){
  2103   2099         zNewKey[j++] = 'a';
  2104   2100         zNewKey[j++] = 0;
  2105   2101       }else{
  2106         -      if( aStack[i].flags & (STK_Int|STK_Real) ){
         2102  +      if( aStack[i].flags & (MEM_Int|MEM_Real) ){
  2107   2103           zNewKey[j++] = 'b';
  2108   2104         }else{
  2109   2105           zNewKey[j++] = 'c';
  2110   2106         }
  2111         -      memcpy(&zNewKey[j], zStack[i] ? zStack[i] : aStack[i].z, aStack[i].n);
         2107  +      /*** Is this right? ****/
         2108  +      memcpy(&zNewKey[j],aStack[i].z?aStack[i].z:aStack[i].zShort,aStack[i].n);
  2112   2109         j += aStack[i].n;
  2113   2110       }
  2114   2111     }
  2115   2112     if( addRowid ){
  2116   2113       u32 iKey;
  2117   2114       Integerify(p, p->tos-nField);
  2118   2115       iKey = intToKey(aStack[p->tos-nField].i);
................................................................................
  2122   2119     }else{
  2123   2120       if( pOp->p2==0 ) sqliteVdbePopStack(p, nField+addRowid);
  2124   2121     }
  2125   2122     p->tos++;
  2126   2123     aStack[p->tos].n = nByte;
  2127   2124     if( nByte<=NBFS ){
  2128   2125       assert( zNewKey==zTemp );
  2129         -    zStack[p->tos] = aStack[p->tos].z;
  2130         -    memcpy(zStack[p->tos], zTemp, nByte);
  2131         -    aStack[p->tos].flags = STK_Str;
         2126  +    aStack[p->tos].z = aStack[p->tos].zShort;
         2127  +    memcpy(aStack[p->tos].z, zTemp, nByte);
         2128  +    aStack[p->tos].flags = MEM_Str;
  2132   2129     }else{
  2133         -    aStack[p->tos].flags = STK_Str|STK_Dyn;
  2134         -    zStack[p->tos] = zNewKey;
         2130  +    aStack[p->tos].flags = MEM_Str|MEM_Dyn;
         2131  +    aStack[p->tos].z = zNewKey;
  2135   2132     }
  2136   2133     break;
  2137   2134   }
  2138   2135   
  2139   2136   /* Opcode: IncrKey * * *
  2140   2137   **
  2141   2138   ** The top of the stack should contain an index key generated by
................................................................................
  2145   2142   ** the key itself.
  2146   2143   */
  2147   2144   case OP_IncrKey: {
  2148   2145     int tos = p->tos;
  2149   2146   
  2150   2147     VERIFY( if( tos<0 ) goto bad_instruction );
  2151   2148     Stringify(p, tos);
  2152         -  if( aStack[tos].flags & (STK_Static|STK_Ephem) ){
         2149  +  if( aStack[tos].flags & (MEM_Static|MEM_Ephem) ){
  2153   2150       /* CANT HAPPEN.  The IncrKey opcode is only applied to keys
  2154   2151       ** generated by MakeKey or MakeIdxKey and the results of those
  2155   2152       ** operands are always dynamic strings.
  2156   2153       */
  2157   2154       goto abort_due_to_error;
  2158   2155     }
  2159         -  zStack[tos][aStack[tos].n-1]++;
         2156  +  aStack[tos].z[aStack[tos].n-1]++;
  2160   2157     break;
  2161   2158   }
  2162   2159   
  2163   2160   /* Opcode: Checkpoint P1 * *
  2164   2161   **
  2165   2162   ** Begin a checkpoint.  A checkpoint is the beginning of a operation that
  2166   2163   ** is part of a larger transaction but which might need to be rolled back
................................................................................
  2301   2298     int i = ++p->tos;
  2302   2299     int aMeta[SQLITE_N_BTREE_META];
  2303   2300     assert( pOp->p2<SQLITE_N_BTREE_META );
  2304   2301     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  2305   2302     assert( db->aDb[pOp->p1].pBt!=0 );
  2306   2303     rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
  2307   2304     aStack[i].i = aMeta[1+pOp->p2];
  2308         -  aStack[i].flags = STK_Int;
         2305  +  aStack[i].flags = MEM_Int;
  2309   2306     break;
  2310   2307   }
  2311   2308   
  2312   2309   /* Opcode: SetCookie P1 P2 *
  2313   2310   **
  2314   2311   ** Write the top of the stack into cookie number P2 of database P1.
  2315   2312   ** P2==0 is the schema version.  P2==1 is the database format.
................................................................................
  2582   2579   
  2583   2580     VERIFY( if( tos<0 ) goto not_enough_stack; )
  2584   2581     assert( i>=0 && i<p->nCursor );
  2585   2582     pC = &p->aCsr[i];
  2586   2583     if( pC->pCursor!=0 ){
  2587   2584       int res, oc;
  2588   2585       pC->nullRow = 0;
  2589         -    if( aStack[tos].flags & STK_Int ){
         2586  +    if( aStack[tos].flags & MEM_Int ){
  2590   2587         int iKey = intToKey(aStack[tos].i);
  2591   2588         if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){
  2592   2589           pC->movetoTarget = iKey;
  2593   2590           pC->deferredMoveto = 1;
  2594   2591           POPSTACK;
  2595   2592           break;
  2596   2593         }
  2597   2594         sqliteBtreeMoveto(pC->pCursor, (char*)&iKey, sizeof(int), &res);
  2598   2595         pC->lastRecno = aStack[tos].i;
  2599   2596         pC->recnoIsValid = res==0;
  2600   2597       }else{
  2601   2598         Stringify(p, tos);
  2602         -      sqliteBtreeMoveto(pC->pCursor, zStack[tos], aStack[tos].n, &res);
         2599  +      sqliteBtreeMoveto(pC->pCursor, aStack[tos].z, aStack[tos].n, &res);
  2603   2600         pC->recnoIsValid = 0;
  2604   2601       }
  2605   2602       pC->deferredMoveto = 0;
  2606   2603       sqlite_search_count++;
  2607   2604       oc = pOp->opcode;
  2608   2605       if( oc==OP_MoveTo && res<0 ){
  2609   2606         sqliteBtreeNext(pC->pCursor, &res);
................................................................................
  2671   2668     int tos = p->tos;
  2672   2669     int alreadyExists = 0;
  2673   2670     Cursor *pC;
  2674   2671     VERIFY( if( tos<0 ) goto not_enough_stack; )
  2675   2672     if( VERIFY( i>=0 && i<p->nCursor && ) (pC = &p->aCsr[i])->pCursor!=0 ){
  2676   2673       int res, rx;
  2677   2674       Stringify(p, tos);
  2678         -    rx = sqliteBtreeMoveto(pC->pCursor, zStack[tos], aStack[tos].n, &res);
         2675  +    rx = sqliteBtreeMoveto(pC->pCursor, aStack[tos].z, aStack[tos].n, &res);
  2679   2676       alreadyExists = rx==SQLITE_OK && res==0;
  2680   2677       pC->deferredMoveto = 0;
  2681   2678     }
  2682   2679     if( pOp->opcode==OP_Found ){
  2683   2680       if( alreadyExists ) pc = pOp->p2 - 1;
  2684   2681     }else{
  2685   2682       if( !alreadyExists ) pc = pOp->p2 - 1;
................................................................................
  2728   2725       int v;         /* The record number on the P1 entry that matches K */
  2729   2726       char *zKey;    /* The value of K */
  2730   2727       int nKey;      /* Number of bytes in K */
  2731   2728   
  2732   2729       /* Make sure K is a string and make zKey point to K
  2733   2730       */
  2734   2731       Stringify(p, nos);
  2735         -    zKey = zStack[nos];
         2732  +    zKey = aStack[nos].z;
  2736   2733       nKey = aStack[nos].n;
  2737   2734       assert( nKey >= 4 );
  2738   2735   
  2739   2736       /* Search for an entry in P1 where all but the last four bytes match K.
  2740   2737       ** If there is no such entry, jump immediately to P2.
  2741   2738       */
  2742   2739       assert( p->aCsr[i].deferredMoveto==0 );
................................................................................
  2771   2768       /* The last four bytes of the key are different from R.  Convert the
  2772   2769       ** last four bytes of the key into an integer and push it onto the
  2773   2770       ** stack.  (These bytes are the record number of an entry that
  2774   2771       ** violates a UNIQUE constraint.)
  2775   2772       */
  2776   2773       p->tos++;
  2777   2774       aStack[tos].i = v;
  2778         -    aStack[tos].flags = STK_Int;
         2775  +    aStack[tos].flags = MEM_Int;
  2779   2776     }
  2780   2777     break;
  2781   2778   }
  2782   2779   
  2783   2780   /* Opcode: NotExists P1 P2 *
  2784   2781   **
  2785   2782   ** Use the top of the stack as a integer key.  If a record with that key
................................................................................
  2796   2793   case OP_NotExists: {
  2797   2794     int i = pOp->p1;
  2798   2795     int tos = p->tos;
  2799   2796     BtCursor *pCrsr;
  2800   2797     VERIFY( if( tos<0 ) goto not_enough_stack; )
  2801   2798     if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
  2802   2799       int res, rx, iKey;
  2803         -    assert( aStack[tos].flags & STK_Int );
         2800  +    assert( aStack[tos].flags & MEM_Int );
  2804   2801       iKey = intToKey(aStack[tos].i);
  2805   2802       rx = sqliteBtreeMoveto(pCrsr, (char*)&iKey, sizeof(int), &res);
  2806   2803       p->aCsr[i].lastRecno = aStack[tos].i;
  2807   2804       p->aCsr[i].recnoIsValid = res==0;
  2808   2805       p->aCsr[i].nullRow = 0;
  2809   2806       if( rx!=SQLITE_OK || res!=0 ){
  2810   2807         pc = pOp->p2 - 1;
................................................................................
  2908   2905         }
  2909   2906       }
  2910   2907       pC->recnoIsValid = 0;
  2911   2908       pC->deferredMoveto = 0;
  2912   2909     }
  2913   2910     p->tos++;
  2914   2911     aStack[p->tos].i = v;
  2915         -  aStack[p->tos].flags = STK_Int;
         2912  +  aStack[p->tos].flags = MEM_Int;
  2916   2913     break;
  2917   2914   }
  2918   2915   
  2919   2916   /* Opcode: PutIntKey P1 P2 *
  2920   2917   **
  2921   2918   ** Write an entry into the table of cursor P1.  A new entry is
  2922   2919   ** created if it doesn't already exist or the data for an existing
................................................................................
  2948   2945     if( VERIFY( i>=0 && i<p->nCursor && )
  2949   2946         ((pC = &p->aCsr[i])->pCursor!=0 || pC->pseudoTable) ){
  2950   2947       char *zKey;
  2951   2948       int nKey, iKey;
  2952   2949       if( pOp->opcode==OP_PutStrKey ){
  2953   2950         Stringify(p, nos);
  2954   2951         nKey = aStack[nos].n;
  2955         -      zKey = zStack[nos];
         2952  +      zKey = aStack[nos].z;
  2956   2953       }else{
  2957         -      assert( aStack[nos].flags & STK_Int );
         2954  +      assert( aStack[nos].flags & MEM_Int );
  2958   2955         nKey = sizeof(int);
  2959   2956         iKey = intToKey(aStack[nos].i);
  2960   2957         zKey = (char*)&iKey;
  2961   2958         if( pOp->p2 ){
  2962   2959           db->nChange++;
  2963   2960           db->lastRowid = aStack[nos].i;
  2964   2961         }
................................................................................
  2971   2968         ** The following assert makes sure we are not trying to use
  2972   2969         ** PutStrKey on a pseudo-table
  2973   2970         */
  2974   2971         assert( pOp->opcode==OP_PutIntKey );
  2975   2972         sqliteFree(pC->pData);
  2976   2973         pC->iKey = iKey;
  2977   2974         pC->nData = aStack[tos].n;
  2978         -      if( aStack[tos].flags & STK_Dyn ){
  2979         -        pC->pData = zStack[tos];
  2980         -        zStack[tos] = 0;
  2981         -        aStack[tos].flags = STK_Null;
         2975  +      if( aStack[tos].flags & MEM_Dyn ){
         2976  +        pC->pData = aStack[tos].z;
         2977  +        aStack[tos].z = 0;
         2978  +        aStack[tos].flags = MEM_Null;
  2982   2979         }else{
  2983   2980           pC->pData = sqliteMallocRaw( pC->nData );
  2984   2981           if( pC->pData ){
  2985         -          memcpy(pC->pData, zStack[tos], pC->nData);
         2982  +          memcpy(pC->pData, aStack[tos].z, pC->nData);
  2986   2983           }
  2987   2984         }
  2988   2985         pC->nullRow = 0;
  2989   2986       }else{
  2990   2987         rc = sqliteBtreeInsert(pC->pCursor, zKey, nKey,
  2991         -                          zStack[tos], aStack[tos].n);
         2988  +                          aStack[tos].z, aStack[tos].n);
  2992   2989       }
  2993   2990       pC->recnoIsValid = 0;
  2994   2991       pC->deferredMoveto = 0;
  2995   2992     }
  2996   2993     POPSTACK;
  2997   2994     POPSTACK;
  2998   2995     break;
................................................................................
  3064   3061     int tos = ++p->tos;
  3065   3062     Cursor *pC;
  3066   3063     int n;
  3067   3064   
  3068   3065     assert( i>=0 && i<p->nCursor );
  3069   3066     pC = &p->aCsr[i];
  3070   3067     if( pC->nullRow ){
  3071         -    aStack[tos].flags = STK_Null;
         3068  +    aStack[tos].flags = MEM_Null;
  3072   3069     }else if( pC->pCursor!=0 ){
  3073   3070       BtCursor *pCrsr = pC->pCursor;
  3074   3071       sqliteVdbeCursorMoveto(pC);
  3075   3072       if( pC->nullRow ){
  3076         -      aStack[tos].flags = STK_Null;
         3073  +      aStack[tos].flags = MEM_Null;
  3077   3074         break;
  3078   3075       }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){
  3079   3076         sqliteBtreeKeySize(pCrsr, &n);
  3080   3077       }else{
  3081   3078         sqliteBtreeDataSize(pCrsr, &n);
  3082   3079       }
  3083   3080       aStack[tos].n = n;
  3084   3081       if( n<=NBFS ){
  3085         -      aStack[tos].flags = STK_Str;
  3086         -      zStack[tos] = aStack[tos].z;
         3082  +      aStack[tos].flags = MEM_Str;
         3083  +      aStack[tos].z = aStack[tos].zShort;
  3087   3084       }else{
  3088   3085         char *z = sqliteMallocRaw( n );
  3089   3086         if( z==0 ) goto no_mem;
  3090         -      aStack[tos].flags = STK_Str | STK_Dyn;
  3091         -      zStack[tos] = z;
         3087  +      aStack[tos].flags = MEM_Str | MEM_Dyn;
         3088  +      aStack[tos].z = z;
  3092   3089       }
  3093   3090       if( pC->keyAsData || pOp->opcode==OP_RowKey ){
  3094         -      sqliteBtreeKey(pCrsr, 0, n, zStack[tos]);
         3091  +      sqliteBtreeKey(pCrsr, 0, n, aStack[tos].z);
  3095   3092       }else{
  3096         -      sqliteBtreeData(pCrsr, 0, n, zStack[tos]);
         3093  +      sqliteBtreeData(pCrsr, 0, n, aStack[tos].z);
  3097   3094       }
  3098   3095     }else if( pC->pseudoTable ){
  3099   3096       aStack[tos].n = pC->nData;
  3100         -    zStack[tos] = pC->pData;
  3101         -    aStack[tos].flags = STK_Str|STK_Ephem;
         3097  +    aStack[tos].z = pC->pData;
         3098  +    aStack[tos].flags = MEM_Str|MEM_Ephem;
  3102   3099     }else{
  3103         -    aStack[tos].flags = STK_Null;
         3100  +    aStack[tos].flags = MEM_Null;
  3104   3101     }
  3105   3102     break;
  3106   3103   }
  3107   3104   
  3108   3105   /* Opcode: Column P1 P2 *
  3109   3106   **
  3110   3107   ** Interpret the data that cursor P1 points to as
................................................................................
  3134   3131     BtCursor *pCrsr;
  3135   3132     int idxWidth;
  3136   3133     unsigned char aHdr[10];
  3137   3134   
  3138   3135     assert( i<p->nCursor );
  3139   3136     if( i<0 ){
  3140   3137       VERIFY( if( tos+i<0 ) goto bad_instruction; )
  3141         -    VERIFY( if( (aStack[tos+i].flags & STK_Str)==0 ) goto bad_instruction; )
  3142         -    zRec = zStack[tos+i];
         3138  +    VERIFY( if( (aStack[tos+i].flags & MEM_Str)==0 ) goto bad_instruction; )
         3139  +    zRec = aStack[tos+i].z;
  3143   3140       payloadSize = aStack[tos+i].n;
  3144   3141     }else if( (pC = &p->aCsr[i])->pCursor!=0 ){
  3145   3142       sqliteVdbeCursorMoveto(pC);
  3146   3143       zRec = 0;
  3147   3144       pCrsr = pC->pCursor;
  3148   3145       if( pC->nullRow ){
  3149   3146         payloadSize = 0;
................................................................................
  3160   3157       payloadSize = 0;
  3161   3158     }
  3162   3159   
  3163   3160     /* Figure out how many bytes in the column data and where the column
  3164   3161     ** data begins.
  3165   3162     */
  3166   3163     if( payloadSize==0 ){
  3167         -    aStack[tos].flags = STK_Null;
         3164  +    aStack[tos].flags = MEM_Null;
  3168   3165       p->tos = tos;
  3169   3166       break;
  3170   3167     }else if( payloadSize<256 ){
  3171   3168       idxWidth = 1;
  3172   3169     }else if( payloadSize<65536 ){
  3173   3170       idxWidth = 2;
  3174   3171     }else{
................................................................................
  3204   3201       goto abort_due_to_error;
  3205   3202     }
  3206   3203   
  3207   3204     /* amt and offset now hold the offset to the start of data and the
  3208   3205     ** amount of data.  Go get the data and put it on the stack.
  3209   3206     */
  3210   3207     if( amt==0 ){
  3211         -    aStack[tos].flags = STK_Null;
         3208  +    aStack[tos].flags = MEM_Null;
  3212   3209     }else if( zRec ){
  3213         -    aStack[tos].flags = STK_Str | STK_Ephem;
         3210  +    aStack[tos].flags = MEM_Str | MEM_Ephem;
  3214   3211       aStack[tos].n = amt;
  3215         -    zStack[tos] = &zRec[offset];
         3212  +    aStack[tos].z = &zRec[offset];
  3216   3213     }else{
  3217   3214       if( amt<=NBFS ){
  3218         -      aStack[tos].flags = STK_Str;
  3219         -      zStack[tos] = aStack[tos].z;
         3215  +      aStack[tos].flags = MEM_Str;
         3216  +      aStack[tos].z = aStack[tos].zShort;
  3220   3217         aStack[tos].n = amt;
  3221   3218       }else{
  3222   3219         char *z = sqliteMallocRaw( amt );
  3223   3220         if( z==0 ) goto no_mem;
  3224         -      aStack[tos].flags = STK_Str | STK_Dyn;
  3225         -      zStack[tos] = z;
         3221  +      aStack[tos].flags = MEM_Str | MEM_Dyn;
         3222  +      aStack[tos].z = z;
  3226   3223         aStack[tos].n = amt;
  3227   3224       }
  3228   3225       if( pC->keyAsData ){
  3229         -      sqliteBtreeKey(pCrsr, offset, amt, zStack[tos]);
         3226  +      sqliteBtreeKey(pCrsr, offset, amt, aStack[tos].z);
  3230   3227       }else{
  3231         -      sqliteBtreeData(pCrsr, offset, amt, zStack[tos]);
         3228  +      sqliteBtreeData(pCrsr, offset, amt, aStack[tos].z);
  3232   3229       }
  3233   3230     }
  3234   3231     p->tos = tos;
  3235   3232     break;
  3236   3233   }
  3237   3234   
  3238   3235   /* Opcode: Recno P1 * *
................................................................................
  3252   3249     pC = &p->aCsr[i];
  3253   3250     sqliteVdbeCursorMoveto(pC);
  3254   3251     if( pC->recnoIsValid ){
  3255   3252       v = pC->lastRecno;
  3256   3253     }else if( pC->pseudoTable ){
  3257   3254       v = keyToInt(pC->iKey);
  3258   3255     }else if( pC->nullRow || pC->pCursor==0 ){
  3259         -    aStack[tos].flags = STK_Null;
         3256  +    aStack[tos].flags = MEM_Null;
  3260   3257       break;
  3261   3258     }else{
  3262   3259       assert( pC->pCursor!=0 );
  3263   3260       sqliteBtreeKey(pC->pCursor, 0, sizeof(u32), (char*)&v);
  3264   3261       v = keyToInt(v);
  3265   3262     }
  3266   3263     aStack[tos].i = v;
  3267         -  aStack[tos].flags = STK_Int;
         3264  +  aStack[tos].flags = MEM_Int;
  3268   3265     break;
  3269   3266   }
  3270   3267   
  3271   3268   /* Opcode: FullKey P1 * *
  3272   3269   **
  3273   3270   ** Extract the complete key from the record that cursor P1 is currently
  3274   3271   ** pointing to and push the key onto the stack as a string.
................................................................................
  3295   3292       if( amt<=0 ){
  3296   3293         rc = SQLITE_CORRUPT;
  3297   3294         goto abort_due_to_error;
  3298   3295       }
  3299   3296       if( amt>NBFS ){
  3300   3297         z = sqliteMallocRaw( amt );
  3301   3298         if( z==0 ) goto no_mem;
  3302         -      aStack[tos].flags = STK_Str | STK_Dyn;
         3299  +      aStack[tos].flags = MEM_Str | MEM_Dyn;
  3303   3300       }else{
  3304         -      z = aStack[tos].z;
  3305         -      aStack[tos].flags = STK_Str;
         3301  +      z = aStack[tos].zShort;
         3302  +      aStack[tos].flags = MEM_Str;
  3306   3303       }
  3307   3304       sqliteBtreeKey(pCrsr, 0, amt, z);
  3308         -    zStack[tos] = z;
         3305  +    aStack[tos].z = z;
  3309   3306       aStack[tos].n = amt;
  3310   3307     }
  3311   3308     break;
  3312   3309   }
  3313   3310   
  3314   3311   /* Opcode: NullRow P1 * *
  3315   3312   **
................................................................................
  3444   3441   case OP_IdxPut: {
  3445   3442     int i = pOp->p1;
  3446   3443     int tos = p->tos;
  3447   3444     BtCursor *pCrsr;
  3448   3445     VERIFY( if( tos<0 ) goto not_enough_stack; )
  3449   3446     if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
  3450   3447       int nKey = aStack[tos].n;
  3451         -    const char *zKey = zStack[tos];
         3448  +    const char *zKey = aStack[tos].z;
  3452   3449       if( pOp->p2 ){
  3453   3450         int res, n;
  3454   3451         assert( aStack[tos].n >= 4 );
  3455   3452         rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res);
  3456   3453         if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3457   3454         while( res!=0 ){
  3458   3455           int c;
................................................................................
  3490   3487   case OP_IdxDelete: {
  3491   3488     int i = pOp->p1;
  3492   3489     int tos = p->tos;
  3493   3490     BtCursor *pCrsr;
  3494   3491     VERIFY( if( tos<0 ) goto not_enough_stack; )
  3495   3492     if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
  3496   3493       int rx, res;
  3497         -    rx = sqliteBtreeMoveto(pCrsr, zStack[tos], aStack[tos].n, &res);
         3494  +    rx = sqliteBtreeMoveto(pCrsr, aStack[tos].z, aStack[tos].n, &res);
  3498   3495       if( rx==SQLITE_OK && res==0 ){
  3499   3496         rc = sqliteBtreeDelete(pCrsr);
  3500   3497       }
  3501   3498       assert( p->aCsr[i].deferredMoveto==0 );
  3502   3499     }
  3503   3500     POPSTACK;
  3504   3501     break;
................................................................................
  3520   3517   
  3521   3518     if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
  3522   3519       int v;
  3523   3520       int sz;
  3524   3521       assert( p->aCsr[i].deferredMoveto==0 );
  3525   3522       sqliteBtreeKeySize(pCrsr, &sz);
  3526   3523       if( sz<sizeof(u32) ){
  3527         -      aStack[tos].flags = STK_Null;
         3524  +      aStack[tos].flags = MEM_Null;
  3528   3525       }else{
  3529   3526         sqliteBtreeKey(pCrsr, sz - sizeof(u32), sizeof(u32), (char*)&v);
  3530   3527         v = keyToInt(v);
  3531   3528         aStack[tos].i = v;
  3532         -      aStack[tos].flags = STK_Int;
         3529  +      aStack[tos].flags = MEM_Int;
  3533   3530       }
  3534   3531     }
  3535   3532     break;
  3536   3533   }
  3537   3534   
  3538   3535   /* Opcode: IdxGT P1 P2 *
  3539   3536   **
................................................................................
  3568   3565     BtCursor *pCrsr;
  3569   3566   
  3570   3567     if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
  3571   3568       int res, rc;
  3572   3569    
  3573   3570       Stringify(p, tos);
  3574   3571       assert( p->aCsr[i].deferredMoveto==0 );
  3575         -    rc = sqliteBtreeKeyCompare(pCrsr, zStack[tos], aStack[tos].n, 4, &res);
         3572  +    rc = sqliteBtreeKeyCompare(pCrsr, aStack[tos].z, aStack[tos].n, 4, &res);
  3576   3573       if( rc!=SQLITE_OK ){
  3577   3574         break;
  3578   3575       }
  3579   3576       if( pOp->opcode==OP_IdxLT ){
  3580   3577         res = -res;
  3581   3578       }else if( pOp->opcode==OP_IdxGE ){
  3582   3579         res++;
................................................................................
  3601   3598   case OP_IdxIsNull: {
  3602   3599     int i = pOp->p1;
  3603   3600     int tos = p->tos;
  3604   3601     int k, n;
  3605   3602     const char *z;
  3606   3603   
  3607   3604     assert( tos>=0 );
  3608         -  assert( aStack[tos].flags & STK_Str );
  3609         -  z = zStack[tos];
         3605  +  assert( aStack[tos].flags & MEM_Str );
         3606  +  z = aStack[tos].z;
  3610   3607     n = aStack[tos].n;
  3611   3608     for(k=0; k<n && i>0; i--){
  3612   3609       if( z[k]=='a' ){
  3613   3610         pc = pOp->p2-1;
  3614   3611         break;
  3615   3612       }
  3616   3613       while( k<n && z[k] ){ k++; }
................................................................................
  3688   3685     if( pOp->opcode==OP_CreateTable ){
  3689   3686       rc = sqliteBtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno);
  3690   3687     }else{
  3691   3688       rc = sqliteBtreeCreateIndex(db->aDb[pOp->p2].pBt, &pgno);
  3692   3689     }
  3693   3690     if( rc==SQLITE_OK ){
  3694   3691       aStack[i].i = pgno;
  3695         -    aStack[i].flags = STK_Int;
         3692  +    aStack[i].flags = MEM_Int;
  3696   3693       *(u32*)pOp->p3 = pgno;
  3697   3694       pOp->p3 = 0;
  3698   3695     }
  3699   3696     break;
  3700   3697   }
  3701   3698   
  3702   3699   /* Opcode: IntegrityCk P1 P2 *
................................................................................
  3735   3732     }
  3736   3733     aRoot[j] = 0;
  3737   3734     sqliteHashClear(&pSet->hash);
  3738   3735     pSet->prev = 0;
  3739   3736     z = sqliteBtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
  3740   3737     if( z==0 || z[0]==0 ){
  3741   3738       if( z ) sqliteFree(z);
  3742         -    zStack[tos] = "ok";
         3739  +    aStack[tos].z = "ok";
  3743   3740       aStack[tos].n = 3;
  3744         -    aStack[tos].flags = STK_Str | STK_Static;
         3741  +    aStack[tos].flags = MEM_Str | MEM_Static;
  3745   3742     }else{
  3746         -    zStack[tos] = z;
         3743  +    aStack[tos].z = z;
  3747   3744       aStack[tos].n = strlen(z) + 1;
  3748         -    aStack[tos].flags = STK_Str | STK_Dyn;
         3745  +    aStack[tos].flags = MEM_Str | MEM_Dyn;
  3749   3746     }
  3750   3747     sqliteFree(aRoot);
  3751   3748     break;
  3752   3749   }
  3753   3750   
  3754   3751   /* Opcode: ListWrite * * *
  3755   3752   **
................................................................................
  3799   3796       VERIFY(
  3800   3797         if( pKeylist->nRead<0 
  3801   3798           || pKeylist->nRead>=pKeylist->nUsed
  3802   3799           || pKeylist->nRead>=pKeylist->nKey ) goto bad_instruction;
  3803   3800       )
  3804   3801       p->tos++;
  3805   3802       aStack[p->tos].i = pKeylist->aKey[pKeylist->nRead++];
  3806         -    aStack[p->tos].flags = STK_Int;
  3807         -    zStack[p->tos] = 0;
         3803  +    aStack[p->tos].flags = MEM_Int;
         3804  +    aStack[p->tos].z = 0;
  3808   3805       if( pKeylist->nRead>=pKeylist->nUsed ){
  3809   3806         p->pList = pKeylist->pNext;
  3810   3807         sqliteFree(pKeylist);
  3811   3808       }
  3812   3809     }else{
  3813   3810       pc = pOp->p2 - 1;
  3814   3811     }
................................................................................
  3873   3870     Sorter *pSorter;
  3874   3871     VERIFY( if( tos<1 ) goto not_enough_stack; )
  3875   3872     if( Dynamicify(p, tos) || Dynamicify(p, nos) ) goto no_mem;
  3876   3873     pSorter = sqliteMallocRaw( sizeof(Sorter) );
  3877   3874     if( pSorter==0 ) goto no_mem;
  3878   3875     pSorter->pNext = p->pSort;
  3879   3876     p->pSort = pSorter;
  3880         -  assert( aStack[tos].flags & STK_Dyn );
         3877  +  assert( aStack[tos].flags & MEM_Dyn );
  3881   3878     pSorter->nKey = aStack[tos].n;
  3882         -  pSorter->zKey = zStack[tos];
         3879  +  pSorter->zKey = aStack[tos].z;
  3883   3880     pSorter->nData = aStack[nos].n;
  3884         -  if( aStack[nos].flags & STK_Dyn ){
  3885         -    pSorter->pData = zStack[nos];
         3881  +  if( aStack[nos].flags & MEM_Dyn ){
         3882  +    pSorter->pData = aStack[nos].z;
  3886   3883     }else{
  3887         -    pSorter->pData = sqliteStrDup(zStack[nos]);
         3884  +    pSorter->pData = sqliteStrDup(aStack[nos].z);
  3888   3885     }
  3889   3886     aStack[tos].flags = 0;
  3890   3887     aStack[nos].flags = 0;
  3891         -  zStack[tos] = 0;
  3892         -  zStack[nos] = 0;
         3888  +  aStack[tos].z = 0;
         3889  +  aStack[nos].z = 0;
  3893   3890     p->tos -= 2;
  3894   3891     break;
  3895   3892   }
  3896   3893   
  3897   3894   /* Opcode: SortMakeRec P1 * *
  3898   3895   **
  3899   3896   ** The top P1 elements are the arguments to a callback.  Form these
................................................................................
  3907   3904     int nField;
  3908   3905     int i, j;
  3909   3906   
  3910   3907     nField = pOp->p1;
  3911   3908     VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
  3912   3909     nByte = 0;
  3913   3910     for(i=p->tos-nField+1; i<=p->tos; i++){
  3914         -    if( (aStack[i].flags & STK_Null)==0 ){
         3911  +    if( (aStack[i].flags & MEM_Null)==0 ){
  3915   3912         Stringify(p, i);
  3916   3913         nByte += aStack[i].n;
  3917   3914       }
  3918   3915     }
  3919   3916     nByte += sizeof(char*)*(nField+1);
  3920   3917     azArg = sqliteMallocRaw( nByte );
  3921   3918     if( azArg==0 ) goto no_mem;
  3922   3919     z = (char*)&azArg[nField+1];
  3923   3920     for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){
  3924         -    if( aStack[i].flags & STK_Null ){
         3921  +    if( aStack[i].flags & MEM_Null ){
  3925   3922         azArg[j] = 0;
  3926   3923       }else{
  3927   3924         azArg[j] = z;
  3928         -      strcpy(z, zStack[i]);
         3925  +      strcpy(z, aStack[i].z);
  3929   3926         z += aStack[i].n;
  3930   3927       }
  3931   3928     }
  3932   3929     sqliteVdbePopStack(p, nField);
  3933   3930     p->tos++;
  3934   3931     aStack[p->tos].n = nByte;
  3935         -  zStack[p->tos] = (char*)azArg;
  3936         -  aStack[p->tos].flags = STK_Str|STK_Dyn;
         3932  +  aStack[p->tos].z = (char*)azArg;
         3933  +  aStack[p->tos].flags = MEM_Str|MEM_Dyn;
  3937   3934     break;
  3938   3935   }
  3939   3936   
  3940   3937   /* Opcode: SortMakeKey * * P3
  3941   3938   **
  3942   3939   ** Convert the top few entries of the stack into a sort key.  The
  3943   3940   ** number of stack entries consumed is the number of characters in 
................................................................................
  3958   3955     int nField;
  3959   3956     int i, j, k;
  3960   3957   
  3961   3958     nField = strlen(pOp->p3);
  3962   3959     VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
  3963   3960     nByte = 1;
  3964   3961     for(i=p->tos-nField+1; i<=p->tos; i++){
  3965         -    if( (aStack[i].flags & STK_Null)!=0 ){
         3962  +    if( (aStack[i].flags & MEM_Null)!=0 ){
  3966   3963         nByte += 2;
  3967   3964       }else{
  3968   3965         Stringify(p, i);
  3969   3966         nByte += aStack[i].n+2;
  3970   3967       }
  3971   3968     }
  3972   3969     zNewKey = sqliteMallocRaw( nByte );
  3973   3970     if( zNewKey==0 ) goto no_mem;
  3974   3971     j = 0;
  3975   3972     k = 0;
  3976   3973     for(i=p->tos-nField+1; i<=p->tos; i++){
  3977         -    if( (aStack[i].flags & STK_Null)!=0 ){
         3974  +    if( (aStack[i].flags & MEM_Null)!=0 ){
  3978   3975         zNewKey[j++] = 'N';
  3979   3976         zNewKey[j++] = 0;
  3980   3977         k++;
  3981   3978       }else{
  3982   3979         zNewKey[j++] = pOp->p3[k++];
  3983         -      memcpy(&zNewKey[j], zStack[i], aStack[i].n-1);
         3980  +      memcpy(&zNewKey[j], aStack[i].z, aStack[i].n-1);
  3984   3981         j += aStack[i].n-1;
  3985   3982         zNewKey[j++] = 0;
  3986   3983       }
  3987   3984     }
  3988   3985     zNewKey[j] = 0;
  3989   3986     assert( j<nByte );
  3990   3987     sqliteVdbePopStack(p, nField);
  3991   3988     p->tos++;
  3992   3989     aStack[p->tos].n = nByte;
  3993         -  aStack[p->tos].flags = STK_Str|STK_Dyn;
  3994         -  zStack[p->tos] = zNewKey;
         3990  +  aStack[p->tos].flags = MEM_Str|MEM_Dyn;
         3991  +  aStack[p->tos].z = zNewKey;
  3995   3992     break;
  3996   3993   }
  3997   3994   
  3998   3995   /* Opcode: Sort * * *
  3999   3996   **
  4000   3997   ** Sort all elements on the sorter.  The algorithm is a
  4001   3998   ** mergesort.
................................................................................
  4041   4038   */
  4042   4039   case OP_SortNext: {
  4043   4040     Sorter *pSorter = p->pSort;
  4044   4041     CHECK_FOR_INTERRUPT;
  4045   4042     if( pSorter!=0 ){
  4046   4043       p->pSort = pSorter->pNext;
  4047   4044       p->tos++;
  4048         -    zStack[p->tos] = pSorter->pData;
         4045  +    aStack[p->tos].z = pSorter->pData;
  4049   4046       aStack[p->tos].n = pSorter->nData;
  4050         -    aStack[p->tos].flags = STK_Str|STK_Dyn;
         4047  +    aStack[p->tos].flags = MEM_Str|MEM_Dyn;
  4051   4048       sqliteFree(pSorter->zKey);
  4052   4049       sqliteFree(pSorter);
  4053   4050     }else{
  4054   4051       pc = pOp->p2 - 1;
  4055   4052     }
  4056   4053     break;
  4057   4054   }
................................................................................
  4064   4061   ** callback on it.
  4065   4062   */
  4066   4063   case OP_SortCallback: {
  4067   4064     int i = p->tos;
  4068   4065     VERIFY( if( i<0 ) goto not_enough_stack; )
  4069   4066     if( p->xCallback==0 ){
  4070   4067       p->pc = pc+1;
  4071         -    p->azResColumn = (char**)zStack[i];
         4068  +    p->azResColumn = (char**)aStack[i].z;
  4072   4069       p->nResColumn = pOp->p1;
  4073   4070       p->popStack = 1;
  4074   4071       return SQLITE_ROW;
  4075   4072     }else{
  4076   4073       if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
  4077         -    if( p->xCallback(p->pCbArg, pOp->p1, (char**)zStack[i], p->azColName)!=0 ){
         4074  +    if( p->xCallback(p->pCbArg, pOp->p1, (char**)aStack[i].z, p->azColName)!=0){
  4078   4075         rc = SQLITE_ABORT;
  4079   4076       }
  4080   4077       if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
  4081   4078       p->nCallback++;
  4082   4079     }
  4083   4080     POPSTACK;
  4084   4081     if( sqlite_malloc_failed ) goto no_mem;
................................................................................
  4249   4246       z = p->azField[i];
  4250   4247     }else{
  4251   4248       z = 0;
  4252   4249     }
  4253   4250     p->tos++;
  4254   4251     if( z ){
  4255   4252       aStack[p->tos].n = strlen(z) + 1;
  4256         -    zStack[p->tos] = z;
  4257         -    aStack[p->tos].flags = STK_Str;
         4253  +    aStack[p->tos].z = z;
         4254  +    aStack[p->tos].flags = MEM_Str;
  4258   4255     }else{
  4259   4256       aStack[p->tos].n = 0;
  4260         -    zStack[p->tos] = 0;
  4261         -    aStack[p->tos].flags = STK_Null;
         4257  +    aStack[p->tos].z = 0;
         4258  +    aStack[p->tos].flags = MEM_Null;
  4262   4259     }
  4263   4260     break;
  4264   4261   }
  4265   4262   
  4266   4263   /* Opcode: MemStore P1 P2 *
  4267   4264   **
  4268   4265   ** Write the top of the stack into memory location P1.
................................................................................
  4285   4282       Mem *aMem;
  4286   4283       p->nMem = i + 5;
  4287   4284       aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0]));
  4288   4285       if( aMem==0 ) goto no_mem;
  4289   4286       if( aMem!=p->aMem ){
  4290   4287         int j;
  4291   4288         for(j=0; j<nOld; j++){
  4292         -        if( aMem[j].z==p->aMem[j].s.z ){
  4293         -          aMem[j].z = aMem[j].s.z;
         4289  +        if( aMem[j].z==p->aMem[j].zShort ){
         4290  +          aMem[j].z = aMem[j].zShort;
  4294   4291           }
  4295   4292         }
  4296   4293       }
  4297   4294       p->aMem = aMem;
  4298   4295       if( nOld<p->nMem ){
  4299   4296         memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld));
  4300   4297       }
  4301   4298     }
  4302   4299     pMem = &p->aMem[i];
  4303         -  flags = pMem->s.flags;
  4304         -  if( flags & STK_Dyn ){
         4300  +  flags = pMem->flags;
         4301  +  if( flags & MEM_Dyn ){
  4305   4302       zOld = pMem->z;
  4306   4303     }else{
  4307   4304       zOld = 0;
  4308   4305     }
  4309         -  pMem->s = aStack[tos];
  4310         -  flags = pMem->s.flags;
  4311         -  if( flags & (STK_Static|STK_Dyn|STK_Ephem) ){
  4312         -    if( (flags & STK_Static)!=0 || (pOp->p2 && (flags & STK_Dyn)!=0) ){
  4313         -      pMem->z = zStack[tos];
  4314         -    }else if( flags & STK_Str ){
  4315         -      pMem->z = sqliteMallocRaw( pMem->s.n );
         4306  +  *pMem = aStack[tos];
         4307  +  flags = pMem->flags;
         4308  +  if( flags & (MEM_Static|MEM_Dyn|MEM_Ephem) ){
         4309  +    if( (flags & MEM_Static)!=0 || (pOp->p2 && (flags & MEM_Dyn)!=0) ){
         4310  +      /* pMem->z = zStack[tos]; *** do nothing */
         4311  +    }else if( flags & MEM_Str ){
         4312  +      pMem->z = sqliteMallocRaw( pMem->n );
  4316   4313         if( pMem->z==0 ) goto no_mem;
  4317         -      memcpy(pMem->z, zStack[tos], pMem->s.n);
  4318         -      pMem->s.flags |= STK_Dyn;
  4319         -      pMem->s.flags &= ~(STK_Static|STK_Ephem);
         4314  +      memcpy(pMem->z, aStack[tos].z, pMem->n);
         4315  +      pMem->flags |= MEM_Dyn;
         4316  +      pMem->flags &= ~(MEM_Static|MEM_Ephem);
  4320   4317       }
  4321   4318     }else{
  4322         -    pMem->z = pMem->s.z;
         4319  +    pMem->z = pMem->zShort;
  4323   4320     }
  4324   4321     if( zOld ) sqliteFree(zOld);
  4325   4322     if( pOp->p2 ){
  4326         -    zStack[tos] = 0;
         4323  +    aStack[tos].z = 0;
  4327   4324       aStack[tos].flags = 0;
  4328   4325       POPSTACK;
  4329   4326     }
  4330   4327     break;
  4331   4328   }
  4332   4329   
  4333   4330   /* Opcode: MemLoad P1 * *
................................................................................
  4339   4336   ** location is subsequently changed (using OP_MemStore) then the
  4340   4337   ** value pushed onto the stack will change too.
  4341   4338   */
  4342   4339   case OP_MemLoad: {
  4343   4340     int tos = ++p->tos;
  4344   4341     int i = pOp->p1;
  4345   4342     VERIFY( if( i<0 || i>=p->nMem ) goto bad_instruction; )
  4346         -  memcpy(&aStack[tos], &p->aMem[i].s, sizeof(aStack[tos])-NBFS);;
  4347         -  if( aStack[tos].flags & STK_Str ){
  4348         -    zStack[tos] = p->aMem[i].z;
  4349         -    aStack[tos].flags |= STK_Ephem;
  4350         -    aStack[tos].flags &= ~(STK_Dyn|STK_Static);
         4343  +  memcpy(&aStack[tos], &p->aMem[i], sizeof(aStack[tos])-NBFS);;
         4344  +  if( aStack[tos].flags & MEM_Str ){
         4345  +    /* aStack[tos].z = p->aMem[i].z; */
         4346  +    aStack[tos].flags |= MEM_Ephem;
         4347  +    aStack[tos].flags &= ~(MEM_Dyn|MEM_Static);
  4351   4348     }
  4352   4349     break;
  4353   4350   }
  4354   4351   
  4355   4352   /* Opcode: MemIncr P1 P2 *
  4356   4353   **
  4357   4354   ** Increment the integer valued memory cell P1 by 1.  If P2 is not zero
................................................................................
  4362   4359   ** an integer.
  4363   4360   */
  4364   4361   case OP_MemIncr: {
  4365   4362     int i = pOp->p1;
  4366   4363     Mem *pMem;
  4367   4364     VERIFY( if( i<0 || i>=p->nMem ) goto bad_instruction; )
  4368   4365     pMem = &p->aMem[i];
  4369         -  VERIFY( if( pMem->s.flags != STK_Int ) goto bad_instruction; )
  4370         -  pMem->s.i++;
  4371         -  if( pOp->p2>0 && pMem->s.i>0 ){
         4366  +  VERIFY( if( pMem->flags != MEM_Int ) goto bad_instruction; )
         4367  +  pMem->i++;
         4368  +  if( pOp->p2>0 && pMem->i>0 ){
  4372   4369        pc = pOp->p2 - 1;
  4373   4370     }
  4374   4371     break;
  4375   4372   }
  4376   4373   
  4377   4374   /* Opcode: AggReset * P2 *
  4378   4375   **
................................................................................
  4415   4412     int n = pOp->p2;
  4416   4413     int i;
  4417   4414     Mem *pMem;
  4418   4415     sqlite_func ctx;
  4419   4416   
  4420   4417     VERIFY( if( n<0 ) goto bad_instruction; )
  4421   4418     VERIFY( if( p->tos+1<n ) goto not_enough_stack; )
  4422         -  VERIFY( if( aStack[p->tos].flags!=STK_Int ) goto bad_instruction; )
         4419  +  VERIFY( if( aStack[p->tos].flags!=MEM_Int ) goto bad_instruction; )
  4423   4420     for(i=p->tos-n; i<p->tos; i++){
  4424         -    if( aStack[i].flags & STK_Null ){
  4425         -      zStack[i] = 0;
         4421  +    if( aStack[i].flags & MEM_Null ){
         4422  +      aStack[i].z = 0;
  4426   4423       }else{
  4427   4424         Stringify(p, i);
  4428   4425       }
         4426  +    p->zArgv[i] = aStack[i].z;
  4429   4427     }
  4430   4428     i = aStack[p->tos].i;
  4431   4429     VERIFY( if( i<0 || i>=p->agg.nMem ) goto bad_instruction; )
  4432   4430     ctx.pFunc = (FuncDef*)pOp->p3;
  4433   4431     pMem = &p->agg.pCurrent->aMem[i];
  4434         -  ctx.z = pMem->s.z;
         4432  +  ctx.s.z = pMem->zShort;  /* Space used for small aggregate contexts */
  4435   4433     ctx.pAgg = pMem->z;
  4436         -  ctx.cnt = ++pMem->s.i;
         4434  +  ctx.cnt = ++pMem->i;
  4437   4435     ctx.isError = 0;
  4438   4436     ctx.isStep = 1;
  4439         -  (ctx.pFunc->xStep)(&ctx, n, (const char**)&zStack[p->tos-n]);
         4437  +  (ctx.pFunc->xStep)(&ctx, n, (const char**)&p->zArgv[p->tos-n]);
  4440   4438     pMem->z = ctx.pAgg;
  4441         -  pMem->s.flags = STK_AggCtx;
         4439  +  pMem->flags = MEM_AggCtx;
  4442   4440     sqliteVdbePopStack(p, n+1);
  4443   4441     if( ctx.isError ){
  4444   4442       rc = SQLITE_ERROR;
  4445   4443     }
  4446   4444     break;
  4447   4445   }
  4448   4446   
................................................................................
  4464   4462     int tos = p->tos;
  4465   4463     AggElem *pElem;
  4466   4464     char *zKey;
  4467   4465     int nKey;
  4468   4466   
  4469   4467     VERIFY( if( tos<0 ) goto not_enough_stack; )
  4470   4468     Stringify(p, tos);
  4471         -  zKey = zStack[tos]; 
         4469  +  zKey = aStack[tos].z; 
  4472   4470     nKey = aStack[tos].n;
  4473   4471     pElem = sqliteHashFind(&p->agg.hash, zKey, nKey);
  4474   4472     if( pElem ){
  4475   4473       p->agg.pCurrent = pElem;
  4476   4474       pc = pOp->p2 - 1;
  4477   4475     }else{
  4478   4476       AggInsert(&p->agg, zKey, nKey);
................................................................................
  4492   4490     int i = pOp->p2;
  4493   4491     int tos = p->tos;
  4494   4492     VERIFY( if( tos<0 ) goto not_enough_stack; )
  4495   4493     if( pFocus==0 ) goto no_mem;
  4496   4494     if( VERIFY( i>=0 && ) i<p->agg.nMem ){
  4497   4495       Mem *pMem = &pFocus->aMem[i];
  4498   4496       char *zOld;
  4499         -    if( pMem->s.flags & STK_Dyn ){
         4497  +    if( pMem->flags & MEM_Dyn ){
  4500   4498         zOld = pMem->z;
  4501   4499       }else{
  4502   4500         zOld = 0;
  4503   4501       }
  4504   4502       Deephemeralize(p, tos);
  4505         -    pMem->s = aStack[tos];
  4506         -    if( pMem->s.flags & STK_Dyn ){
  4507         -      pMem->z = zStack[tos];
  4508         -      zStack[tos] = 0;
         4503  +    *pMem = aStack[tos];
         4504  +    if( pMem->flags & MEM_Dyn ){
         4505  +      aStack[tos].z = 0;
  4509   4506         aStack[tos].flags = 0;
  4510         -    }else if( pMem->s.flags & (STK_Static|STK_AggCtx) ){
  4511         -      pMem->z = zStack[tos];
  4512         -    }else if( pMem->s.flags & STK_Str ){
  4513         -      pMem->z = pMem->s.z;
         4507  +    }else if( pMem->flags & (MEM_Static|MEM_AggCtx) ){
         4508  +      /* pMem->z = zStack[tos]; *** do nothing */
         4509  +    }else if( pMem->flags & MEM_Str ){
         4510  +      pMem->z = pMem->zShort;
  4514   4511       }
  4515   4512       if( zOld ) sqliteFree(zOld);
  4516   4513     }
  4517   4514     POPSTACK;
  4518   4515     break;
  4519   4516   }
  4520   4517   
................................................................................
  4527   4524   case OP_AggGet: {
  4528   4525     AggElem *pFocus = AggInFocus(p->agg);
  4529   4526     int i = pOp->p2;
  4530   4527     int tos = ++p->tos;
  4531   4528     if( pFocus==0 ) goto no_mem;
  4532   4529     if( VERIFY( i>=0 && ) i<p->agg.nMem ){
  4533   4530       Mem *pMem = &pFocus->aMem[i];
  4534         -    aStack[tos] = pMem->s;
  4535         -    zStack[tos] = pMem->z;
  4536         -    aStack[tos].flags &= ~STK_Dyn;
  4537         -    aStack[tos].flags |= STK_Ephem;
         4531  +    aStack[tos] = *pMem;
         4532  +    aStack[tos].flags &= ~MEM_Dyn;
         4533  +    aStack[tos].flags |= MEM_Ephem;
  4538   4534     }
  4539   4535     break;
  4540   4536   }
  4541   4537   
  4542   4538   /* Opcode: AggNext * P2 *
  4543   4539   **
  4544   4540   ** Make the next aggregate value the current aggregate.  The prior
................................................................................
  4566   4562       Mem *aMem;
  4567   4563       p->agg.pCurrent = sqliteHashData(p->agg.pSearch);
  4568   4564       aMem = p->agg.pCurrent->aMem;
  4569   4565       for(i=0; i<p->agg.nMem; i++){
  4570   4566         int freeCtx;
  4571   4567         if( p->agg.apFunc[i]==0 ) continue;
  4572   4568         if( p->agg.apFunc[i]->xFinalize==0 ) continue;
  4573         -      ctx.s.flags = STK_Null;
  4574         -      ctx.z = 0;
         4569  +      ctx.s.flags = MEM_Null;
         4570  +      ctx.s.z = aMem[i].zShort;
  4575   4571         ctx.pAgg = (void*)aMem[i].z;
  4576         -      freeCtx = aMem[i].z && aMem[i].z!=aMem[i].s.z;
  4577         -      ctx.cnt = aMem[i].s.i;
         4572  +      freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort;
         4573  +      ctx.cnt = aMem[i].i;
  4578   4574         ctx.isStep = 0;
  4579   4575         ctx.pFunc = p->agg.apFunc[i];
  4580   4576         (*p->agg.apFunc[i]->xFinalize)(&ctx);
  4581   4577         if( freeCtx ){
  4582   4578           sqliteFree( aMem[i].z );
  4583   4579         }
  4584         -      aMem[i].s = ctx.s;
  4585         -      aMem[i].z = ctx.z;
  4586         -      if( (aMem[i].s.flags & STK_Str) &&
  4587         -              (aMem[i].s.flags & (STK_Dyn|STK_Static|STK_Ephem))==0 ){
  4588         -        aMem[i].z = aMem[i].s.z;
         4580  +      aMem[i] = ctx.s;
         4581  +      if( (aMem[i].flags & MEM_Str) &&
         4582  +              (aMem[i].flags & (MEM_Dyn|MEM_Static|MEM_Ephem))==0 ){
         4583  +        aMem[i].z = aMem[i].zShort;
  4589   4584         }
  4590   4585       }
  4591   4586     }
  4592   4587     break;
  4593   4588   }
  4594   4589   
  4595   4590   /* Opcode: SetInsert P1 * P3
................................................................................
  4612   4607     }
  4613   4608     if( pOp->p3 ){
  4614   4609       sqliteHashInsert(&p->aSet[i].hash, pOp->p3, strlen(pOp->p3)+1, p);
  4615   4610     }else{
  4616   4611       int tos = p->tos;
  4617   4612       if( tos<0 ) goto not_enough_stack;
  4618   4613       Stringify(p, tos);
  4619         -    sqliteHashInsert(&p->aSet[i].hash, zStack[tos], aStack[tos].n, p);
         4614  +    sqliteHashInsert(&p->aSet[i].hash, aStack[tos].z, aStack[tos].n, p);
  4620   4615       POPSTACK;
  4621   4616     }
  4622   4617     if( sqlite_malloc_failed ) goto no_mem;
  4623   4618     break;
  4624   4619   }
  4625   4620   
  4626   4621   /* Opcode: SetFound P1 P2 *
................................................................................
  4631   4626   */
  4632   4627   case OP_SetFound: {
  4633   4628     int i = pOp->p1;
  4634   4629     int tos = p->tos;
  4635   4630     VERIFY( if( tos<0 ) goto not_enough_stack; )
  4636   4631     Stringify(p, tos);
  4637   4632     if( i>=0 && i<p->nSet &&
  4638         -       sqliteHashFind(&p->aSet[i].hash, zStack[tos], aStack[tos].n)){
         4633  +       sqliteHashFind(&p->aSet[i].hash, aStack[tos].z, aStack[tos].n)){
  4639   4634       pc = pOp->p2 - 1;
  4640   4635     }
  4641   4636     POPSTACK;
  4642   4637     break;
  4643   4638   }
  4644   4639   
  4645   4640   /* Opcode: SetNotFound P1 P2 *
................................................................................
  4650   4645   */
  4651   4646   case OP_SetNotFound: {
  4652   4647     int i = pOp->p1;
  4653   4648     int tos = p->tos;
  4654   4649     VERIFY( if( tos<0 ) goto not_enough_stack; )
  4655   4650     Stringify(p, tos);
  4656   4651     if( i<0 || i>=p->nSet ||
  4657         -       sqliteHashFind(&p->aSet[i].hash, zStack[tos], aStack[tos].n)==0 ){
         4652  +       sqliteHashFind(&p->aSet[i].hash, aStack[tos].z, aStack[tos].n)==0 ){
  4658   4653       pc = pOp->p2 - 1;
  4659   4654     }
  4660   4655     POPSTACK;
  4661   4656     break;
  4662   4657   }
  4663   4658   
  4664   4659   /* Opcode: SetFirst P1 P2 *
................................................................................
  4695   4690       if( pSet->prev==0 ){
  4696   4691         break;
  4697   4692       }else{
  4698   4693         pc = pOp->p2 - 1;
  4699   4694       }
  4700   4695     }
  4701   4696     tos = ++p->tos;
  4702         -  zStack[tos] = sqliteHashKey(pSet->prev);
         4697  +  aStack[tos].z = sqliteHashKey(pSet->prev);
  4703   4698     aStack[tos].n = sqliteHashKeysize(pSet->prev);
  4704         -  aStack[tos].flags = STK_Str | STK_Ephem;
         4699  +  aStack[tos].flags = MEM_Str | MEM_Ephem;
  4705   4700     break;
  4706   4701   }
  4707   4702   
  4708   4703   /* Opcode: Vacuum * * *
  4709   4704   **
  4710   4705   ** Vacuum the entire database.  This opcode will cause other virtual
  4711   4706   ** machines to be created and run.  It may not be called from within
................................................................................
  4757   4752         sqliteSetString(&p->zErrMsg, "jump destination out of range", (char*)0);
  4758   4753         rc = SQLITE_INTERNAL;
  4759   4754       }
  4760   4755       if( p->trace && p->tos>=0 ){
  4761   4756         int i;
  4762   4757         fprintf(p->trace, "Stack:");
  4763   4758         for(i=p->tos; i>=0 && i>p->tos-5; i--){
  4764         -        if( aStack[i].flags & STK_Null ){
         4759  +        if( aStack[i].flags & MEM_Null ){
  4765   4760             fprintf(p->trace, " NULL");
  4766         -        }else if( (aStack[i].flags & (STK_Int|STK_Str))==(STK_Int|STK_Str) ){
         4761  +        }else if( (aStack[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
  4767   4762             fprintf(p->trace, " si:%d", aStack[i].i);
  4768         -        }else if( aStack[i].flags & STK_Int ){
         4763  +        }else if( aStack[i].flags & MEM_Int ){
  4769   4764             fprintf(p->trace, " i:%d", aStack[i].i);
  4770         -        }else if( aStack[i].flags & STK_Real ){
         4765  +        }else if( aStack[i].flags & MEM_Real ){
  4771   4766             fprintf(p->trace, " r:%g", aStack[i].r);
  4772         -        }else if( aStack[i].flags & STK_Str ){
         4767  +        }else if( aStack[i].flags & MEM_Str ){
  4773   4768             int j, k;
  4774   4769             char zBuf[100];
  4775   4770             zBuf[0] = ' ';
  4776         -          if( aStack[i].flags & STK_Dyn ){
         4771  +          if( aStack[i].flags & MEM_Dyn ){
  4777   4772               zBuf[1] = 'z';
  4778         -            assert( (aStack[i].flags & (STK_Static|STK_Ephem))==0 );
  4779         -          }else if( aStack[i].flags & STK_Static ){
         4773  +            assert( (aStack[i].flags & (MEM_Static|MEM_Ephem))==0 );
         4774  +          }else if( aStack[i].flags & MEM_Static ){
  4780   4775               zBuf[1] = 't';
  4781         -            assert( (aStack[i].flags & (STK_Dyn|STK_Ephem))==0 );
  4782         -          }else if( aStack[i].flags & STK_Ephem ){
         4776  +            assert( (aStack[i].flags & (MEM_Dyn|MEM_Ephem))==0 );
         4777  +          }else if( aStack[i].flags & MEM_Ephem ){
  4783   4778               zBuf[1] = 'e';
  4784         -            assert( (aStack[i].flags & (STK_Static|STK_Dyn))==0 );
         4779  +            assert( (aStack[i].flags & (MEM_Static|MEM_Dyn))==0 );
  4785   4780             }else{
  4786   4781               zBuf[1] = 's';
  4787   4782             }
  4788   4783             zBuf[2] = '[';
  4789   4784             k = 3;
  4790   4785             for(j=0; j<20 && j<aStack[i].n; j++){
  4791         -            int c = zStack[i][j];
         4786  +            int c = aStack[i].z[j];
  4792   4787               if( c==0 && j==aStack[i].n-1 ) break;
  4793   4788               if( isprint(c) && !isspace(c) ){
  4794   4789                 zBuf[k++] = c;
  4795   4790               }else{
  4796   4791                 zBuf[k++] = '.';
  4797   4792               }
  4798   4793             }

Changes to src/vdbeInt.h.

   102    102   ** Number of bytes of string storage space available to each stack
   103    103   ** layer without having to malloc.  NBFS is short for Number of Bytes
   104    104   ** For Strings.
   105    105   */
   106    106   #define NBFS 32
   107    107   
   108    108   /*
   109         -** A single level of the stack is an instance of the following
   110         -** structure.  Except, string values are stored on a separate
   111         -** list of of pointers to character.  The reason for storing
   112         -** strings separately is so that they can be easily passed
   113         -** to the callback function.
   114         -*/
   115         -struct Stack {
   116         -  int i;         /* Integer value */
   117         -  int n;         /* Number of characters in string value, including '\0' */
   118         -  int flags;     /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */
   119         -  double r;      /* Real value */
   120         -  char z[NBFS];  /* Space for short strings */
   121         -};
   122         -typedef struct Stack Stack;
   123         -
   124         -/*
   125         -** Memory cells use the same structure as the stack except that space
   126         -** for an arbitrary string is added.
          109  +** A single level of the stack or a single memory cell
          110  +** is an instance of the following structure. 
   127    111   */
   128    112   struct Mem {
   129         -  Stack s;       /* All values of the memory cell besides string */
   130         -  char *z;       /* String value for this memory cell */
          113  +  int i;              /* Integer value */
          114  +  int n;              /* Number of characters in string value, including '\0' */
          115  +  int flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
          116  +  double r;           /* Real value */
          117  +  char *z;            /* String value */
          118  +  char zShort[NBFS];  /* Space for short strings */
   131    119   };
   132    120   typedef struct Mem Mem;
   133    121   
   134    122   /*
   135         -** Allowed values for Stack.flags
          123  +** Allowed values for Mem.flags
   136    124   */
   137         -#define STK_Null      0x0001   /* Value is NULL */
   138         -#define STK_Str       0x0002   /* Value is a string */
   139         -#define STK_Int       0x0004   /* Value is an integer */
   140         -#define STK_Real      0x0008   /* Value is a real number */
   141         -#define STK_Dyn       0x0010   /* Need to call sqliteFree() on zStack[] */
   142         -#define STK_Static    0x0020   /* zStack[] points to a static string */
   143         -#define STK_Ephem     0x0040   /* zStack[] points to an ephemeral string */
          125  +#define MEM_Null      0x0001   /* Value is NULL */
          126  +#define MEM_Str       0x0002   /* Value is a string */
          127  +#define MEM_Int       0x0004   /* Value is an integer */
          128  +#define MEM_Real      0x0008   /* Value is a real number */
          129  +#define MEM_Dyn       0x0010   /* Need to call sqliteFree() on Mem.z */
          130  +#define MEM_Static    0x0020   /* Mem.z points to a static string */
          131  +#define MEM_Ephem     0x0040   /* Mem.z points to an ephemeral string */
   144    132   
   145         -/* The following STK_ value appears only in AggElem.aMem.s.flag fields.
          133  +/* The following MEM_ value appears only in AggElem.aMem.s.flag fields.
   146    134   ** It indicates that the corresponding AggElem.aMem.z points to a
   147    135   ** aggregate function context that needs to be finalized.
   148    136   */
   149         -#define STK_AggCtx    0x0040   /* zStack[] points to an agg function context */
          137  +#define MEM_AggCtx    0x0040   /* Mem.z points to an agg function context */
   150    138   
   151    139   /*
   152    140   ** The "context" argument for a installable function.  A pointer to an
   153    141   ** instance of this structure is the first argument to the routines used
   154    142   ** implement the SQL functions.
   155    143   **
   156    144   ** There is a typedef for this structure in sqlite.h.  So all routines,
   157    145   ** even the public interface to SQLite, can use a pointer to this structure.
   158    146   ** But this file is the only place where the internal details of this
   159    147   ** structure are known.
   160    148   **
   161    149   ** This structure is defined inside of vdbe.c because it uses substructures
   162         -** (Stack) which are only defined there.
          150  +** (Mem) which are only defined there.
   163    151   */
   164    152   struct sqlite_func {
   165    153     FuncDef *pFunc;   /* Pointer to function information.  MUST BE FIRST */
   166         -  Stack s;          /* Small strings, ints, and double values go here */
   167         -  char *z;          /* Space for holding dynamic string results */
          154  +  Mem s;            /* The return value is stored here */
   168    155     void *pAgg;       /* Aggregate context */
   169    156     u8 isError;       /* Set to true for an error */
   170    157     u8 isStep;        /* Current in the step function */
   171    158     int cnt;          /* Number of times that the step function has been called */
   172    159   };
   173    160   
   174    161   /*
................................................................................
   233    220     int nOp;            /* Number of instructions in the program */
   234    221     int nOpAlloc;       /* Number of slots allocated for aOp[] */
   235    222     Op *aOp;            /* Space to hold the virtual machine's program */
   236    223     int nLabel;         /* Number of labels used */
   237    224     int nLabelAlloc;    /* Number of slots allocated in aLabel[] */
   238    225     int *aLabel;        /* Space to hold the labels */
   239    226     int tos;            /* Index of top of stack */
   240         -  Stack *aStack;      /* The operand stack, except string values */
   241         -  char **zStack;      /* Text or binary values of the stack */
          227  +  Mem *aStack;        /* The operand stack, except string values */
          228  +  char **zArgv;       /* Text values used by the callback */
   242    229     char **azColName;   /* Becomes the 4th parameter to callbacks */
   243    230     int nCursor;        /* Number of slots in aCsr[] */
   244    231     Cursor *aCsr;       /* One element of this array for each open cursor */
   245    232     Sorter *pSort;      /* A linked list of objects to be sorted */
   246    233     FILE *pFile;        /* At most one open file handler */
   247    234     int nField;         /* Number of file fields */
   248    235     char **azField;     /* Data for each file field */
................................................................................
   290    277   /*
   291    278   ** Here is a macro to handle the common case of popping the stack
   292    279   ** once.  This macro only works from within the sqliteVdbeExec()
   293    280   ** function.
   294    281   */
   295    282   #define POPSTACK \
   296    283     assert(p->tos>=0); \
   297         -  if( aStack[p->tos].flags & STK_Dyn ) sqliteFree(zStack[p->tos]); \
          284  +  if( aStack[p->tos].flags & MEM_Dyn ) sqliteFree(aStack[p->tos].z); \
   298    285     p->tos--;
   299    286   
   300    287   /*
   301    288   ** Function prototypes
   302    289   */
   303    290   void sqliteVdbeCleanupCursor(Cursor*);
   304    291   void sqliteVdbeSorterReset(Vdbe*);

Changes to src/vdbeaux.c.

   371    371   **
   372    372   ** These routines are defined here in vdbe.c because they depend on knowing
   373    373   ** the internals of the sqlite_func structure which is only defined in 
   374    374   ** this source file.
   375    375   */
   376    376   char *sqlite_set_result_string(sqlite_func *p, const char *zResult, int n){
   377    377     assert( !p->isStep );
   378         -  if( p->s.flags & STK_Dyn ){
   379         -    sqliteFree(p->z);
          378  +  if( p->s.flags & MEM_Dyn ){
          379  +    sqliteFree(p->s.z);
   380    380     }
   381    381     if( zResult==0 ){
   382         -    p->s.flags = STK_Null;
          382  +    p->s.flags = MEM_Null;
   383    383       n = 0;
   384         -    p->z = 0;
          384  +    p->s.z = 0;
   385    385       p->s.n = 0;
   386    386     }else{
   387    387       if( n<0 ) n = strlen(zResult);
   388    388       if( n<NBFS-1 ){
   389         -      memcpy(p->s.z, zResult, n);
   390         -      p->s.z[n] = 0;
   391         -      p->s.flags = STK_Str;
   392         -      p->z = p->s.z;
          389  +      memcpy(p->s.zShort, zResult, n);
          390  +      p->s.zShort[n] = 0;
          391  +      p->s.flags = MEM_Str;
          392  +      p->s.z = p->s.zShort;
   393    393       }else{
   394         -      p->z = sqliteMallocRaw( n+1 );
   395         -      if( p->z ){
   396         -        memcpy(p->z, zResult, n);
   397         -        p->z[n] = 0;
          394  +      p->s.z = sqliteMallocRaw( n+1 );
          395  +      if( p->s.z ){
          396  +        memcpy(p->s.z, zResult, n);
          397  +        p->s.z[n] = 0;
   398    398         }
   399         -      p->s.flags = STK_Str | STK_Dyn;
          399  +      p->s.flags = MEM_Str | MEM_Dyn;
   400    400       }
   401    401       p->s.n = n+1;
   402    402     }
   403         -  return p->z;
          403  +  return p->s.z;
   404    404   }
   405    405   void sqlite_set_result_int(sqlite_func *p, int iResult){
   406    406     assert( !p->isStep );
   407         -  if( p->s.flags & STK_Dyn ){
   408         -    sqliteFree(p->z);
          407  +  if( p->s.flags & MEM_Dyn ){
          408  +    sqliteFree(p->s.z);
   409    409     }
   410    410     p->s.i = iResult;
   411         -  p->s.flags = STK_Int;
          411  +  p->s.flags = MEM_Int;
   412    412   }
   413    413   void sqlite_set_result_double(sqlite_func *p, double rResult){
   414    414     assert( !p->isStep );
   415         -  if( p->s.flags & STK_Dyn ){
   416         -    sqliteFree(p->z);
          415  +  if( p->s.flags & MEM_Dyn ){
          416  +    sqliteFree(p->s.z);
   417    417     }
   418    418     p->s.r = rResult;
   419         -  p->s.flags = STK_Real;
          419  +  p->s.flags = MEM_Real;
   420    420   }
   421    421   void sqlite_set_result_error(sqlite_func *p, const char *zMsg, int n){
   422    422     assert( !p->isStep );
   423    423     sqlite_set_result_string(p, zMsg, n);
   424    424     p->isError = 1;
   425    425   }
   426    426   
................................................................................
   442    442   ** the internals of the sqlite_func structure which is only defined in
   443    443   ** this source file.
   444    444   */
   445    445   void *sqlite_aggregate_context(sqlite_func *p, int nByte){
   446    446     assert( p && p->pFunc && p->pFunc->xStep );
   447    447     if( p->pAgg==0 ){
   448    448       if( nByte<=NBFS ){
   449         -      p->pAgg = (void*)p->z;
          449  +      p->pAgg = (void*)p->s.z;
          450  +      memset(p->pAgg, 0, nByte);
   450    451       }else{
   451    452         p->pAgg = sqliteMalloc( nByte );
   452    453       }
   453    454     }
   454    455     return p->pAgg;
   455    456   }
   456    457   
................................................................................
   504    505        "int",  "text",   "int", "int", "text",
   505    506        0
   506    507     };
   507    508   
   508    509     assert( p->popStack==0 );
   509    510     assert( p->explain );
   510    511     p->azColName = azColumnNames;
   511         -  p->azResColumn = p->zStack;
   512         -  for(i=0; i<5; i++) p->zStack[i] = p->aStack[i].z;
          512  +  p->azResColumn = p->zArgv;
          513  +  for(i=0; i<5; i++) p->zArgv[i] = p->aStack[i].zShort;
   513    514     p->rc = SQLITE_OK;
   514    515     for(i=p->pc; p->rc==SQLITE_OK && i<p->nOp; i++){
   515    516       if( db->flags & SQLITE_Interrupt ){
   516    517         db->flags &= ~SQLITE_Interrupt;
   517    518         if( db->magic!=SQLITE_MAGIC_BUSY ){
   518    519           p->rc = SQLITE_MISUSE;
   519    520         }else{
   520    521           p->rc = SQLITE_INTERRUPT;
   521    522         }
   522    523         sqliteSetString(&p->zErrMsg, sqlite_error_string(p->rc), (char*)0);
   523    524         break;
   524    525       }
   525         -    sprintf(p->zStack[0],"%d",i);
   526         -    sprintf(p->zStack[2],"%d", p->aOp[i].p1);
   527         -    sprintf(p->zStack[3],"%d", p->aOp[i].p2);
          526  +    sprintf(p->zArgv[0],"%d",i);
          527  +    sprintf(p->zArgv[2],"%d", p->aOp[i].p1);
          528  +    sprintf(p->zArgv[3],"%d", p->aOp[i].p2);
   528    529       if( p->aOp[i].p3type==P3_POINTER ){
   529         -      sprintf(p->aStack[4].z, "ptr(%#x)", (int)p->aOp[i].p3);
   530         -      p->zStack[4] = p->aStack[4].z;
          530  +      sprintf(p->aStack[4].zShort, "ptr(%#x)", (int)p->aOp[i].p3);
          531  +      p->zArgv[4] = p->aStack[4].zShort;
   531    532       }else{
   532         -      p->zStack[4] = p->aOp[i].p3;
          533  +      p->zArgv[4] = p->aOp[i].p3;
   533    534       }
   534         -    p->zStack[1] = sqliteOpcodeNames[p->aOp[i].opcode];
          535  +    p->zArgv[1] = sqliteOpcodeNames[p->aOp[i].opcode];
   535    536       if( p->xCallback==0 ){
   536    537         p->pc = i+1;
   537         -      p->azResColumn = p->zStack;
          538  +      p->azResColumn = p->zArgv;
   538    539         p->nResColumn = 5;
   539    540         return SQLITE_ROW;
   540    541       }
   541    542       if( sqliteSafetyOff(db) ){
   542    543         p->rc = SQLITE_MISUSE;
   543    544         break;
   544    545       }
   545         -    if( p->xCallback(p->pCbArg, 5, p->zStack, p->azColName) ){
          546  +    if( p->xCallback(p->pCbArg, 5, p->zArgv, p->azColName) ){
   546    547         p->rc = SQLITE_ABORT;
   547    548       }
   548    549       if( sqliteSafetyOn(db) ){
   549    550         p->rc = SQLITE_MISUSE;
   550    551       }
   551    552     }
   552    553     return p->rc==SQLITE_OK ? SQLITE_DONE : SQLITE_ERROR;
................................................................................
   592    593     ** Allocation all the stack space we will ever need.
   593    594     */
   594    595     if( p->aStack==0 ){
   595    596       p->nVar = nVar;
   596    597       assert( nVar>=0 );
   597    598       n = isExplain ? 10 : p->nOp;
   598    599       p->aStack = sqliteMalloc(
   599         -    n*(sizeof(p->aStack[0]) + 2*sizeof(char*))   /* aStack and zStack */
          600  +    n*(sizeof(p->aStack[0]) + 2*sizeof(char*))     /* aStack and zArgv */
   600    601         + p->nVar*(sizeof(char*)+sizeof(int)+1)      /* azVar, anVar, abVar */
   601    602       );
   602         -    p->zStack = (char**)&p->aStack[n];
   603         -    p->azColName = (char**)&p->zStack[n];
          603  +    p->zArgv = (char**)&p->aStack[n];
          604  +    p->azColName = (char**)&p->zArgv[n];
   604    605       p->azVar = (char**)&p->azColName[n];
   605    606       p->anVar = (int*)&p->azVar[p->nVar];
   606    607       p->abVar = (u8*)&p->anVar[p->nVar];
   607    608     }
   608    609   
   609    610     sqliteHashInit(&p->agg.hash, SQLITE_HASH_BINARY, 0);
   610    611     p->agg.pSearch = 0;
................................................................................
   652    653   
   653    654   /*
   654    655   ** Pop the stack N times.  Free any memory associated with the
   655    656   ** popped stack elements.
   656    657   */
   657    658   void sqliteVdbePopStack(Vdbe *p, int N){
   658    659     assert( N>=0 );
   659         -  if( p->zStack==0 ) return;
   660         -  assert( p->aStack || sqlite_malloc_failed );
   661    660     if( p->aStack==0 ) return;
   662    661     while( N-- > 0 ){
   663         -    if( p->aStack[p->tos].flags & STK_Dyn ){
   664         -      sqliteFree(p->zStack[p->tos]);
          662  +    if( p->aStack[p->tos].flags & MEM_Dyn ){
          663  +      sqliteFree(p->aStack[p->tos].z);
   665    664       }
   666    665       p->aStack[p->tos].flags = 0;
   667         -    p->zStack[p->tos] = 0;
   668    666       p->tos--;
   669    667     }
   670    668   }
   671    669   
   672    670   /*
   673    671   ** Reset an Agg structure.  Delete all its contents. 
   674    672   **
................................................................................
   682    680     int i;
   683    681     HashElem *p;
   684    682     for(p = sqliteHashFirst(&pAgg->hash); p; p = sqliteHashNext(p)){
   685    683       AggElem *pElem = sqliteHashData(p);
   686    684       assert( pAgg->apFunc!=0 );
   687    685       for(i=0; i<pAgg->nMem; i++){
   688    686         Mem *pMem = &pElem->aMem[i];
   689         -      if( pAgg->apFunc[i] && (pMem->s.flags & STK_AggCtx)!=0 ){
          687  +      if( pAgg->apFunc[i] && (pMem->flags & MEM_AggCtx)!=0 ){
   690    688           sqlite_func ctx;
   691    689           ctx.pFunc = pAgg->apFunc[i];
   692         -        ctx.s.flags = STK_Null;
   693         -        ctx.z = 0;
          690  +        ctx.s.flags = MEM_Null;
   694    691           ctx.pAgg = pMem->z;
   695         -        ctx.cnt = pMem->s.i;
          692  +        ctx.cnt = pMem->i;
   696    693           ctx.isStep = 0;
   697    694           ctx.isError = 0;
   698    695           (*pAgg->apFunc[i]->xFinalize)(&ctx);
   699         -        if( pMem->z!=0 && pMem->z!=pMem->s.z ){
          696  +        if( pMem->z!=0 && pMem->z!=pMem->zShort ){
   700    697             sqliteFree(pMem->z);
   701    698           }
   702         -      }else if( pMem->s.flags & STK_Dyn ){
          699  +      }else if( pMem->flags & MEM_Dyn ){
   703    700           sqliteFree(pMem->z);
   704    701         }
   705    702       }
   706    703       sqliteFree(pElem);
   707    704     }
   708    705     sqliteHashClear(&pAgg->hash);
   709    706     sqliteFree(pAgg->apFunc);
................................................................................
   761    758   */
   762    759   static void Cleanup(Vdbe *p){
   763    760     int i;
   764    761     sqliteVdbePopStack(p, p->tos+1);
   765    762     closeAllCursors(p);
   766    763     if( p->aMem ){
   767    764       for(i=0; i<p->nMem; i++){
   768         -      if( p->aMem[i].s.flags & STK_Dyn ){
          765  +      if( p->aMem[i].flags & MEM_Dyn ){
   769    766           sqliteFree(p->aMem[i].z);
   770    767         }
   771    768       }
   772    769     }
   773    770     sqliteFree(p->aMem);
   774    771     p->aMem = 0;
   775    772     p->nMem = 0;