/ Check-in [6ea5cebf]
Login

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

Overview
Comment:rework the VDBE engine. NULL is now distinct from "" (CVS 49)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:6ea5cebf05562de00d2cf0b9e2aac5f3857638ee
User & Date: drh 2000-06-04 12:58:37
Context
2000-06-05
02:07
:-) (CVS 50) check-in: 1cf2873d user: drh tags: trunk
2000-06-04
12:58
rework the VDBE engine. NULL is now distinct from "" (CVS 49) check-in: 6ea5cebf user: drh tags: trunk
2000-06-03
19:28
:-) (CVS 48) check-in: f7665641 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/expr.c.

    19     19   ** Author contact information:
    20     20   **   drh@hwaci.com
    21     21   **   http://www.hwaci.com/drh/
    22     22   **
    23     23   *************************************************************************
    24     24   ** This file contains C code routines used for processing expressions
    25     25   **
    26         -** $Id: expr.c,v 1.4 2000/06/03 19:19:41 drh Exp $
           26  +** $Id: expr.c,v 1.5 2000/06/04 12:58:37 drh Exp $
    27     27   */
    28     28   #include "sqliteInt.h"
    29     29   
    30     30   /*
    31     31   ** This routine walks an expression tree and resolves references to
    32     32   ** table fields.  Nodes of the form ID.ID or ID resolve into an
    33     33   ** index to the table in the table list and a field offset.  The opcode
................................................................................
   320    320       case TK_STRING: {
   321    321         int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
   322    322         sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
   323    323         sqliteVdbeDequoteP3(v, addr);
   324    324         break;
   325    325       }
   326    326       case TK_NULL: {
   327         -      sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
          327  +      sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
   328    328         break;
   329    329       }
   330    330       case TK_AND:
   331    331       case TK_OR:
   332    332       case TK_PLUS:
   333    333       case TK_STAR:
   334    334       case TK_MINUS:

Changes to src/insert.c.

    20     20   **   drh@hwaci.com
    21     21   **   http://www.hwaci.com/drh/
    22     22   **
    23     23   *************************************************************************
    24     24   ** This file contains C code routines that are called by the parser
    25     25   ** to handle INSERT statements.
    26     26   **
    27         -** $Id: insert.c,v 1.4 2000/06/03 18:06:53 drh Exp $
           27  +** $Id: insert.c,v 1.5 2000/06/04 12:58:38 drh Exp $
    28     28   */
    29     29   #include "sqliteInt.h"
    30     30   
    31     31   /*
    32     32   ** This routine is call to handle SQL of the following form:
    33     33   **
    34     34   **    insert into TABLE (IDLIST) values(EXPRLIST)
................................................................................
   118    118         }else{
   119    119           for(j=0; j<pField->nId; j++){
   120    120             if( pField->a[j].idx==i ) break;
   121    121           }
   122    122         }
   123    123         if( pField && j>=pField->nId ){
   124    124           char *zDflt = pTab->aCol[i].zDflt;
   125         -        if( zDflt==0 ) zDflt = "";
   126         -        sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
          125  +        if( zDflt==0 ){
          126  +          sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
          127  +        }else{
          128  +          sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
          129  +        }
   127    130         }else{
   128    131           sqliteExprCode(pParse, pList->a[j].pExpr);
   129    132         }
   130    133       }
   131    134       sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
   132    135       sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
   133    136       sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
................................................................................
   142    145           }else{
   143    146             for(j=0; j<pField->nId; j++){
   144    147               if( pField->a[j].idx==idx ) break;
   145    148             }
   146    149           }
   147    150           if( pField && j>=pField->nId ){
   148    151             char *zDflt = pTab->aCol[idx].zDflt;
   149         -          if( zDflt==0 ) zDflt = "";
   150         -          sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
          152  +          if( zDflt==0 ){
          153  +            sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
          154  +          }else{
          155  +            sqliteVdbeAddOp(v, OP_String, 0, 0, zDflt, 0);
          156  +          }
   151    157           }else{
   152    158             sqliteExprCode(pParse, pList->a[j].pExpr);
   153    159           }
   154    160         }
   155    161         sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
   156    162         sqliteVdbeAddOp(v, OP_PutIdx, idx, 0, 0, 0);
   157    163         sqliteVdbeAddOp(v, OP_Close, idx, 0, 0, 0);

Changes to src/select.c.

    20     20   **   drh@hwaci.com
    21     21   **   http://www.hwaci.com/drh/
    22     22   **
    23     23   *************************************************************************
    24     24   ** This file contains C code routines that are called by the parser
    25     25   ** to handle SELECT statements.
    26     26   **
    27         -** $Id: select.c,v 1.5 2000/06/03 18:06:53 drh Exp $
           27  +** $Id: select.c,v 1.6 2000/06/04 12:58:38 drh Exp $
    28     28   */
    29     29   #include "sqliteInt.h"
    30     30   
    31     31   
    32     32   /*
    33     33   ** Process a SELECT statement.
    34     34   */
................................................................................
   179    179     */
   180    180     if( isAgg ){
   181    181       for(i=0; i<pEList->nExpr; i++){
   182    182         Expr *p = pEList->a[i].pExpr;
   183    183         switch( sqliteFuncId(&p->token) ){
   184    184           case FN_Min:
   185    185           case FN_Max: {
   186         -          sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
          186  +          sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
   187    187             break;
   188    188           }
   189    189           default: {
   190    190             sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
   191    191             break;
   192    192           }
   193    193         }
................................................................................
   247    247         int id = sqliteFuncId(&p->token);
   248    248         int op, p1;
   249    249         if( n>1 ){
   250    250           sqliteVdbeAddOp(v, OP_Pull, n-1, 0, 0, 0);
   251    251         }
   252    252         if( id!=FN_Count && p->pList && p->pList->nExpr>=1 ){
   253    253           sqliteExprCode(pParse, p->pList->a[0].pExpr);
          254  +        sqliteVdbeAddOp(v, OP_Concat, 1, 0, 0, 0);
   254    255         }
   255    256         switch( sqliteFuncId(&p->token) ){
   256    257           case FN_Count: op = OP_AddImm; p1 = 1; break;
   257    258           case FN_Sum:   op = OP_Add;    p1 = 0; break;
   258    259           case FN_Min:   op = OP_Min;    p1 = 1; break;
   259    260           case FN_Max:   op = OP_Max;    p1 = 0; break;
   260    261         }

Changes to src/shell.c.

    20     20   **   drh@hwaci.com
    21     21   **   http://www.hwaci.com/drh/
    22     22   **
    23     23   *************************************************************************
    24     24   ** This file contains code to implement the "sqlite" command line
    25     25   ** utility for accessing SQLite databases.
    26     26   **
    27         -** $Id: shell.c,v 1.6 2000/06/02 13:27:59 drh Exp $
           27  +** $Id: shell.c,v 1.7 2000/06/04 12:58:38 drh Exp $
    28     28   */
    29     29   #include <stdlib.h>
    30     30   #include <string.h>
    31     31   #include <stdio.h>
    32     32   #include "sqlite.h"
    33     33   #include <unistd.h>
    34     34   #include <ctype.h>
................................................................................
   150    150   static int callback(void *pArg, int nArg, char **azArg, char **azCol){
   151    151     int i;
   152    152     struct callback_data *p = (struct callback_data*)pArg;
   153    153     switch( p->mode ){
   154    154       case MODE_Line: {
   155    155         if( p->cnt++>0 ) fprintf(p->out,"\n");
   156    156         for(i=0; i<nArg; i++){
   157         -        fprintf(p->out,"%s = %s\n", azCol[i], azArg[i]);
          157  +        fprintf(p->out,"%s = %s\n", azCol[i], azArg[i] ? azArg[i] : 0);
   158    158         }
   159    159         break;
   160    160       }
   161    161       case MODE_Column: {
   162    162         if( p->cnt++==0 && p->showHeader ){
   163    163           for(i=0; i<nArg; i++){
   164    164             int w;
................................................................................
   184    184         for(i=0; i<nArg; i++){
   185    185           int w;
   186    186           if( i<ArraySize(p->colWidth) && p->colWidth[i]>0 ){
   187    187              w = p->colWidth[i];
   188    188           }else{
   189    189              w = 10;
   190    190           }
   191         -        fprintf(p->out,"%-*.*s%s",w,w,azArg[i], i==nArg-1 ? "\n": "  ");
          191  +        fprintf(p->out,"%-*.*s%s",w,w,
          192  +            azArg[i] ? azArg[i] : "", i==nArg-1 ? "\n": "  ");
   192    193         }
   193    194         break;
   194    195       }
   195    196       case MODE_List: {
   196    197         if( p->cnt++==0 && p->showHeader ){
   197    198           for(i=0; i<nArg; i++){
   198    199             fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
   199    200           }
   200    201         }
   201    202         for(i=0; i<nArg; i++){
   202         -        fprintf(p->out,"%s%s",azArg[i], i==nArg-1 ? "\n" : p->separator);
          203  +        fprintf(p->out,"%s%s",azArg[i] ? azArg[i] : "",
          204  +             i==nArg-1 ? "\n" : p->separator);
   203    205         }
   204    206         break;
   205    207       }
   206    208       case MODE_Html: {
   207    209         if( p->cnt++==0 && p->showHeader ){
   208    210           fprintf(p->out,"<TR>");
   209    211           for(i=0; i<nArg; i++){
................................................................................
   210    212             fprintf(p->out,"<TH>%s</TH>",azCol[i]);
   211    213           }
   212    214           fprintf(p->out,"</TR>\n");
   213    215         }
   214    216         for(i=0; i<nArg; i++){
   215    217           fprintf(p->out,"<TR>");
   216    218           for(i=0; i<nArg; i++){
   217         -          fprintf(p->out,"<TD>%s</TD>",azArg[i]);
          219  +          fprintf(p->out,"<TD>%s</TD>",azArg[i] ? azArg[i] : "");
   218    220           }
   219    221           fprintf(p->out,"</TD>\n");
   220    222         }
   221    223         break;
   222    224       }
   223    225     }      
   224    226     return 0;

Changes to src/sqliteInt.h.

    19     19   ** Author contact information:
    20     20   **   drh@hwaci.com
    21     21   **   http://www.hwaci.com/drh/
    22     22   **
    23     23   *************************************************************************
    24     24   ** Internal interface definitions for SQLite.
    25     25   **
    26         -** @(#) $Id: sqliteInt.h,v 1.11 2000/06/03 18:06:53 drh Exp $
           26  +** @(#) $Id: sqliteInt.h,v 1.12 2000/06/04 12:58:38 drh Exp $
    27     27   */
    28     28   #include "sqlite.h"
    29     29   #include "dbbe.h"
    30     30   #include "vdbe.h"
    31     31   #include "parse.h"
    32     32   #include <gdbm.h>
    33     33   #include <stdio.h>
    34     34   #include <stdlib.h>
    35     35   #include <string.h>
    36     36   #include <assert.h>
    37     37   
    38         -/* #define MEMORY_DEBUG 2 */
           38  +#define MEMORY_DEBUG 1
    39     39   #ifdef MEMORY_DEBUG
    40     40   # define sqliteMalloc(X)    sqliteMalloc_(X,__FILE__,__LINE__)
    41     41   # define sqliteFree(X)      sqliteFree_(X,__FILE__,__LINE__)
    42     42   # define sqliteRealloc(X,Y) sqliteRealloc_(X,Y,__FILE__,__LINE__)
    43     43     void sqliteStrRealloc(char**);
    44     44   #else
    45     45   # define sqliteStrRealloc(X)

Changes to src/tclsqlite.c.

    19     19   ** Author contact information:
    20     20   **   drh@hwaci.com
    21     21   **   http://www.hwaci.com/drh/
    22     22   **
    23     23   *************************************************************************
    24     24   ** A TCL Interface to SQLite
    25     25   **
    26         -** $Id: tclsqlite.c,v 1.4 2000/06/02 01:17:38 drh Exp $
           26  +** $Id: tclsqlite.c,v 1.5 2000/06/04 12:58:38 drh Exp $
    27     27   */
    28     28   #include "sqlite.h"
    29     29   #include <tcl.h>
    30     30   #include <stdlib.h>
    31     31   #include <string.h>
    32     32   
    33     33   /*
................................................................................
    57     57       if( cbData->once ){
    58     58         for(i=0; i<nCol; i++){
    59     59           Tcl_SetVar2(cbData->interp, cbData->zArray, "*", azN[i],
    60     60              TCL_LIST_ELEMENT|TCL_APPEND_VALUE);
    61     61         }
    62     62       }
    63     63       for(i=0; i<nCol; i++){
    64         -      Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], azCol[i], 0);
           64  +      char *z = azCol[i];
           65  +      if( z==0 ) z = "";
           66  +      Tcl_SetVar2(cbData->interp, cbData->zArray, azN[i], z, 0);
    65     67       }
    66     68     }else{
    67     69       for(i=0; i<nCol; i++){
    68         -      Tcl_SetVar(cbData->interp, azN[i], azCol[i], 0);
           70  +      char *z = azCol[i];
           71  +      if( z==0 ) z = "";
           72  +      Tcl_SetVar(cbData->interp, azN[i], z, 0);
    69     73       }
    70     74     }
    71     75     cbData->once = 0;
    72     76     rc = Tcl_Eval(cbData->interp, cbData->zCode);
    73     77     return rc;
    74     78   }
    75     79   
................................................................................
   279    283       Tcl_SetVar(interp,"argv0",argv[1],TCL_GLOBAL_ONLY);
   280    284       Tcl_SetVar(interp,"argv", "", TCL_GLOBAL_ONLY);
   281    285       for(i=2; i<argc; i++){
   282    286         Tcl_SetVar(interp, "argv", argv[i],
   283    287             TCL_GLOBAL_ONLY | TCL_LIST_ELEMENT | TCL_APPEND_VALUE);
   284    288       }
   285    289       if( Tcl_EvalFile(interp, argv[1])!=TCL_OK ){
   286         -      fprintf(stderr,"%s: %s\n", *argv, 
   287         -         Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY)
   288         -      );
          290  +      char *zInfo = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
          291  +      if( zInfo==0 ) zInfo = interp->result;
          292  +      fprintf(stderr,"%s: %s\n", *argv, zInfo);
   289    293         return 1;
   290    294       }
   291    295     }else{
   292    296       Tcl_GlobalEval(interp, zMainloop);
   293    297     }
   294    298     return 0;
   295    299   }
   296    300   #endif /* TCLSH */

Changes to src/util.c.

    22     22   **
    23     23   *************************************************************************
    24     24   ** Utility functions used throughout sqlite.
    25     25   **
    26     26   ** This file contains functions for allocating memory, comparing
    27     27   ** strings, and stuff like that.
    28     28   **
    29         -** $Id: util.c,v 1.9 2000/06/02 13:28:00 drh Exp $
           29  +** $Id: util.c,v 1.10 2000/06/04 12:58:38 drh Exp $
    30     30   */
    31     31   #include "sqliteInt.h"
    32     32   #include <stdarg.h>
    33     33   #include <ctype.h>
    34     34   
    35     35   #ifdef MEMORY_DEBUG
    36     36   
................................................................................
   628    628             while( *zString && sqliteGlobCompare(&zPattern[1],zString)==0 ){
   629    629               zString++;
   630    630             }
   631    631             return *zString!=0;
   632    632           }else{
   633    633             while( (c2 = *zString)!=0 ){
   634    634               while( c2 != 0 && c2 != c ){ c2 = *++zString; }
          635  +            if( c2==0 ) return 0;
   635    636               if( sqliteGlobCompare(&zPattern[1],zString) ) return 1;
   636    637               zString++;
   637    638             }
   638    639             return 0;
   639    640           }
   640    641         case '?':
   641    642           if( *zString==0 ) return 0;
................................................................................
   695    696             while( *zString && sqliteLikeCompare(&zPattern[1],zString)==0 ){
   696    697               zString++;
   697    698             }
   698    699             return *zString!=0;
   699    700           }else{
   700    701             while( (c2 = UpperToLower[*zString])!=0 ){
   701    702               while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
          703  +            if( c2==0 ) return 0;
   702    704               if( sqliteLikeCompare(&zPattern[1],zString) ) return 1;
   703    705               zString++;
   704    706             }
   705    707             return 0;
   706    708           }
   707    709         case '_':
   708    710           if( *zString==0 ) return 0;

Changes to src/vdbe.c.

    37     37   ** inplicit conversion from one type to the other occurs as necessary.
    38     38   ** 
    39     39   ** Most of the code in this file is taken up by the sqliteVdbeExec()
    40     40   ** function which does the work of interpreting a VDBE program.
    41     41   ** But other routines are also provided to help in building up
    42     42   ** a program instruction by instruction.
    43     43   **
    44         -** $Id: vdbe.c,v 1.12 2000/06/02 23:22:40 drh Exp $
           44  +** $Id: vdbe.c,v 1.13 2000/06/04 12:58:38 drh Exp $
    45     45   */
    46     46   #include "sqliteInt.h"
    47     47   
    48     48   /*
    49     49   ** SQL is translated into a sequence of instructions to be
    50     50   ** executed by a virtual machine.  Each instruction is an instance
    51     51   ** of the following structure.
................................................................................
    76     76   };
    77     77   
    78     78   /* 
    79     79   ** Number of buckets used for merge-sort.  
    80     80   */
    81     81   #define NSORT 30
    82     82   
           83  +/*
           84  +** A single level of the stack is an instance of the following
           85  +** structure.  Except, string values are stored on a separate
           86  +** list of of pointers to character.  The reason for storing
           87  +** strings separately is so that they can be easily passed
           88  +** to the callback function.
           89  +*/
           90  +struct Stack {
           91  +  int i;         /* Integer value */
           92  +  int n;         /* Number of characters in string value, including '\0' */
           93  +  int flags;     /* Some combination of STK_Null, STK_Str, STK_Dyn, etc. */
           94  +  double r;      /* Real value */      
           95  +};
           96  +typedef struct Stack Stack;
           97  +
           98  +/*
           99  +** Allowed values for Stack.flags
          100  +*/
          101  +#define STK_Null      0x0001   /* Value is NULL */
          102  +#define STK_Str       0x0002   /* Value is a string */
          103  +#define STK_Int       0x0004   /* Value is an integer */
          104  +#define STK_Real      0x0008   /* Value is a real number */
          105  +#define STK_Dyn       0x0010   /* Need to call sqliteFree() on zStack[*] */
          106  +
    83    107   /*
    84    108   ** An instance of the virtual machine
    85    109   */
    86    110   struct Vdbe {
    87    111     Dbbe *pBe;          /* Opaque context structure used by DB backend */
    88    112     FILE *trace;        /* Write an execution trace here, if not NULL */
    89    113     int nOp;            /* Number of instructions in the program */
................................................................................
    90    114     int nOpAlloc;       /* Number of slots allocated for aOp[] */
    91    115     Op *aOp;            /* Space to hold the virtual machine's program */
    92    116     int nLabel;         /* Number of labels used */
    93    117     int nLabelAlloc;    /* Number of slots allocated in aLabel[] */
    94    118     int *aLabel;        /* Space to hold the labels */
    95    119     int tos;            /* Index of top of stack */
    96    120     int nStackAlloc;    /* Size of the stack */
    97         -  int *iStack;        /* Integer values of the stack */
          121  +  Stack *aStack;      /* The operand stack, except string values */
    98    122     char **zStack;      /* Text or binary values of the stack */
    99    123     char **azColName;   /* Becomes the 4th parameter to callbacks */
   100    124     int nTable;         /* Number of slots in aTab[] */
   101    125     VdbeTable *aTab;    /* On element of this array for each open table */
   102    126     int nList;          /* Number of slots in apList[] */
   103    127     FILE **apList;      /* An open file for each list */
   104    128     int nSort;          /* Number of slots in apSort[] */
................................................................................
   285    309       p->nLabel = 0;
   286    310       p->nLabelAlloc = 0;
   287    311       return 0;
   288    312     }
   289    313     p->aLabel[i] = -1;
   290    314     return -1-i;
   291    315   }
          316  +
          317  +/*
          318  +** Convert the given stack entity into a string if it isn't one
          319  +** already.  Return non-zero if we run out of memory.
          320  +**
          321  +** NULLs are converted into an empty string.
          322  +*/
          323  +#define Stringify(P,I) \
          324  +   ((P->aStack[I].flags & STK_Str)==0 ? hardStringify(P,I) : 0)
          325  +static int hardStringify(Vdbe *p, int i){
          326  +  char zBuf[30];
          327  +  int fg = p->aStack[i].flags;
          328  +  if( fg & STK_Real ){
          329  +    sprintf(zBuf,"%g",p->aStack[i].r);
          330  +  }else if( fg & STK_Int ){
          331  +    sprintf(zBuf,"%d",p->aStack[i].i);
          332  +  }else{
          333  +    p->zStack[i] = "";
          334  +    p->aStack[i].n = 1;
          335  +    p->aStack[i].flags |= STK_Str;
          336  +    return 0;
          337  +  }
          338  +  p->zStack[i] = 0;
          339  +  sqliteSetString(&p->zStack[i], zBuf, 0);
          340  +  if( p->zStack[i]==0 ) return 1;
          341  +  p->aStack[i].n = strlen(p->zStack[i])+1;
          342  +  p->aStack[i].flags |= STK_Str|STK_Dyn;
          343  +  return 0;
          344  +}
          345  +
          346  +/*
          347  +** Release the memory associated with the given stack level
          348  +*/
          349  +#define Release(P,I)  if((P)->aStack[I].flags&STK_Dyn){ hardRelease(P,I); }
          350  +static void hardRelease(Vdbe *p, int i){
          351  +  sqliteFree(p->zStack[i]);
          352  +  p->zStack[i] = 0;
          353  +  p->aStack[i].flags &= ~(STK_Str|STK_Dyn);
          354  +}
          355  +
          356  +/*
          357  +** Convert the given stack entity into a integer if it isn't one
          358  +** already.
          359  +**
          360  +** Any prior string or real representation is invalidated.  
          361  +** NULLs are converted into 0.
          362  +*/
          363  +#define Integerify(P,I) \
          364  +    if(((P)->aStack[(I)].flags&STK_Int)==0){ hardIntegerify(P,I); }
          365  +static void hardIntegerify(Vdbe *p, int i){
          366  +  if( p->aStack[i].flags & STK_Real ){
          367  +    p->aStack[i].i = p->aStack[i].r;
          368  +    Release(p, i);
          369  +  }else if( p->aStack[i].flags & STK_Str ){
          370  +    p->aStack[i].i = atoi(p->zStack[i]);
          371  +    Release(p, i);
          372  +  }else{
          373  +    p->aStack[i].i = 0;
          374  +  }
          375  +  p->aStack[i].flags = STK_Int;
          376  +}
          377  +
          378  +/*
          379  +** Get a valid Real representation for the given stack element.
          380  +**
          381  +** Any prior string or integer representation is retained.
          382  +** NULLs are converted into 0.0.
          383  +*/
          384  +#define Realify(P,I) \
          385  +    if(((P)->aStack[(I)].flags&STK_Real)==0){ hardRealify(P,I); }
          386  +static void hardRealify(Vdbe *p, int i){
          387  +  if( p->aStack[i].flags & STK_Str ){
          388  +    p->aStack[i].r = atof(p->zStack[i]);
          389  +  }else if( p->aStack[i].flags & STK_Int ){
          390  +    p->aStack[i].r = p->aStack[i].i;
          391  +  }else{
          392  +    p->aStack[i].r = 0.0;
          393  +  }
          394  +  p->aStack[i].flags |= STK_Real;
          395  +}
   292    396   
   293    397   /*
   294    398   ** Pop the stack N times.  Free any memory associated with the
   295    399   ** popped stack elements.
   296    400   */
   297    401   static void PopStack(Vdbe *p, int N){
   298    402     if( p->zStack==0 ) return;
   299    403     while( p->tos>=0 && N-->0 ){
   300    404       int i = p->tos--;
   301         -    sqliteFree(p->zStack[i]);
          405  +    if( p->aStack[i].flags & STK_Dyn ){
          406  +      sqliteFree(p->zStack[i]);
          407  +    }
          408  +    p->aStack[i].flags = 0;
   302    409       p->zStack[i] = 0;
   303    410     }    
   304    411   }
          412  +
          413  +/*
          414  +** Make sure space has been allocated to hold at least N
          415  +** stack elements.  Allocate additional stack space if
          416  +** necessary.
          417  +**
          418  +** Return 0 on success and non-zero if there are memory
          419  +** allocation errors.
          420  +*/
          421  +#define NeedStack(P,N) (((P)->nStackAlloc<=(N)) ? hardNeedStack(P,N) : 0)
          422  +static int hardNeedStack(Vdbe *p, int N){
          423  +  int oldAlloc;
          424  +  int i;
          425  +  if( N>=p->nStackAlloc ){
          426  +    oldAlloc = p->nStackAlloc;
          427  +    p->nStackAlloc = N + 20;
          428  +    p->aStack = sqliteRealloc(p->aStack, p->nStackAlloc*sizeof(p->aStack[0]));
          429  +    p->zStack = sqliteRealloc(p->zStack, p->nStackAlloc*sizeof(char*));
          430  +    if( p->aStack==0 || p->zStack==0 ){
          431  +      sqliteFree(p->aStack);
          432  +      sqliteFree(p->zStack);
          433  +      p->aStack = 0;
          434  +      p->zStack = 0;
          435  +      p->nStackAlloc = 0;
          436  +      return 1;
          437  +    }
          438  +    for(i=oldAlloc; i<p->nStackAlloc; i++){
          439  +      p->zStack[i] = 0;
          440  +      p->aStack[i].flags = 0;
          441  +    }
          442  +  }
          443  +  return 0;
          444  +}
   305    445   
   306    446   /*
   307    447   ** Clean up the VM after execution.
   308    448   **
   309    449   ** This routine will automatically close any tables, list, and/or
   310    450   ** sorters that were left open.
   311    451   */
................................................................................
   372    512       p->nOp = 0;
   373    513     }
   374    514     for(i=0; i<p->nOp; i++){
   375    515       sqliteFree(p->aOp[i].p3);
   376    516     }
   377    517     sqliteFree(p->aOp);
   378    518     sqliteFree(p->aLabel);
   379         -  sqliteFree(p->iStack);
          519  +  sqliteFree(p->aStack);
   380    520     sqliteFree(p->zStack);
   381    521     sqliteFree(p);
   382    522   }
   383    523   
   384    524   /*
   385    525   ** A translation from opcode numbers to opcode names.  Used for testing
   386    526   ** and debugging only.
................................................................................
   398    538     "DeleteIdx",      "ListOpen",       "ListWrite",      "ListRewind",
   399    539     "ListRead",       "ListClose",      "SortOpen",       "SortPut",
   400    540     "SortMakeRec",    "SortMakeKey",    "Sort",           "SortNext",
   401    541     "SortKey",        "SortCallback",   "SortClose",      "FileOpen",
   402    542     "FileRead",       "FileField",      "FileClose",      "MakeRecord",
   403    543     "MakeKey",        "Goto",           "If",             "Halt",
   404    544     "ColumnCount",    "ColumnName",     "Callback",       "Integer",
   405         -  "String",         "Pop",            "Dup",            "Pull",
   406         -  "Add",            "AddImm",         "Subtract",       "Multiply",
   407         -  "Divide",         "Min",            "Max",            "Like",
   408         -  "Glob",           "Eq",             "Ne",             "Lt",
   409         -  "Le",             "Gt",             "Ge",             "IsNull",
   410         -  "NotNull",        "Negative",       "And",            "Or",
   411         -  "Not",            "Concat",         "Noop",         
          545  +  "String",         "Null",           "Pop",            "Dup",
          546  +  "Pull",           "Add",            "AddImm",         "Subtract",
          547  +  "Multiply",       "Divide",         "Min",            "Max",
          548  +  "Like",           "Glob",           "Eq",             "Ne",
          549  +  "Lt",             "Le",             "Gt",             "Ge",
          550  +  "IsNull",         "NotNull",        "Negative",       "And",
          551  +  "Or",             "Not",            "Concat",         "Noop",
   412    552   };
   413    553   
   414    554   /*
   415    555   ** Given the name of an opcode, return its number.  Return 0 if
   416    556   ** there is no match.
   417    557   **
   418    558   ** This routine is used for testing and debugging.
................................................................................
   455    595     rc = SQLITE_OK;
   456    596     if( pzErrMsg ){ *pzErrMsg = 0; }
   457    597     for(i=0; rc==SQLITE_OK && i<p->nOp; i++){
   458    598       sprintf(zAddr,"%d",i);
   459    599       sprintf(zP1,"%d", p->aOp[i].p1);
   460    600       sprintf(zP2,"%d", p->aOp[i].p2);
   461    601       azField[4] = p->aOp[i].p3;
   462         -    if( azField[4]==0 ) azField[4] = "";
   463    602       azField[1] = zOpName[p->aOp[i].opcode];
   464    603       if( xCallback(pArg, 5, azField, azColumnNames) ){
   465    604         rc = SQLITE_ABORT;
   466    605       }
   467    606     }
   468    607     return rc;
   469    608   }
   470    609   
   471         -/*
   472         -** Make sure space has been allocated to hold at least N
   473         -** stack elements.  Allocate additional stack space if
   474         -** necessary.
   475         -**
   476         -** Return 0 on success and non-zero if there are memory
   477         -** allocation errors.
   478         -*/
   479         -static int NeedStack(Vdbe *p, int N){
   480         -  int oldAlloc;
   481         -  int i;
   482         -  if( N>=p->nStackAlloc ){
   483         -    oldAlloc = p->nStackAlloc;
   484         -    p->nStackAlloc = N + 20;
   485         -    p->iStack = sqliteRealloc(p->iStack, p->nStackAlloc*sizeof(int));
   486         -    p->zStack = sqliteRealloc(p->zStack, p->nStackAlloc*sizeof(char*));
   487         -    if( p->iStack==0 || p->zStack==0 ){
   488         -      sqliteFree(p->iStack);
   489         -      sqliteFree(p->zStack);
   490         -      p->iStack = 0;
   491         -      p->zStack = 0;
   492         -      p->nStackAlloc = 0;
   493         -      return 1;
   494         -    }
   495         -    for(i=oldAlloc; i<p->nStackAlloc; i++){
   496         -      p->zStack[i] = 0;
   497         -    }
   498         -  }
   499         -  return 0;
   500         -}
   501         -
   502         -/*
   503         -** Convert the given stack entity into a string if it isn't one
   504         -** already.  Return non-zero if we run out of memory.
   505         -*/
   506         -static int Stringify(Vdbe *p, int i){
   507         -  if( p->zStack[i]==0 ){
   508         -    char zBuf[30];
   509         -    sprintf(zBuf,"%d",p->iStack[i]);
   510         -    sqliteSetString(&p->zStack[i], zBuf, 0);
   511         -    if( p->zStack[i]==0 ) return 1;
   512         -    p->iStack[i] = strlen(p->zStack[i])+1;
   513         -  }
   514         -  return 0;
   515         -}
   516         -
   517         -/*
   518         -** Convert the given stack entity into a integer if it isn't one
   519         -** already.
   520         -*/
   521         -static int Integerify(Vdbe *p, int i){
   522         -  if( p->zStack[i]!=0 ){
   523         -    p->iStack[i] = atoi(p->zStack[i]);
   524         -    sqliteFree(p->zStack[i]);
   525         -    p->zStack[i] = 0;
   526         -  }
   527         -  return p->iStack[i];
   528         -}
   529         -
   530    610   /*
   531    611   ** The parameters are pointers to the head of two sorted lists
   532    612   ** of Sorter structures.  Merge these two lists together and return
   533    613   ** a single sorted list.  This routine forms the core of the merge-sort
   534    614   ** algorithm.
   535    615   **
   536    616   ** In the case of a tie, left sorts in front of right.
................................................................................
   625    705         /* Opcode: Integer P1 * *
   626    706         **
   627    707         ** The integer value P1 is pushed onto the stack.
   628    708         */
   629    709         case OP_Integer: {
   630    710           int i = ++p->tos;
   631    711           if( NeedStack(p, p->tos) ) goto no_mem;
   632         -        p->iStack[i] = pOp->p1;
   633         -        p->zStack[i] = 0;
          712  +        p->aStack[i].i = pOp->p1;
          713  +        p->aStack[i].flags = STK_Int;
   634    714           break;
   635    715         }
   636    716   
   637    717         /* Opcode: String * * P3
   638    718         **
   639    719         ** The string value P3 is pushed onto the stack.
   640    720         */
   641    721         case OP_String: {
   642    722           int i = ++p->tos;
   643    723           char *z;
   644    724           if( NeedStack(p, p->tos) ) goto no_mem;
   645    725           z = pOp->p3;
   646    726           if( z==0 ) z = "";
   647         -        p->iStack[i] = strlen(z) + 1;
   648         -        sqliteSetString(&p->zStack[i], z, 0);
          727  +        p->zStack[i] = z;
          728  +        p->aStack[i].n = strlen(z) + 1;
          729  +        p->aStack[i].flags = STK_Str;
          730  +        break;
          731  +      }
          732  +
          733  +      /* Opcode: NULL * * *
          734  +      **
          735  +      ** Push a NULL value onto the stack.
          736  +      */
          737  +      case OP_Null: {
          738  +        int i = ++p->tos;
          739  +        char *z;
          740  +        if( NeedStack(p, p->tos) ) goto no_mem;
          741  +        p->zStack[i] = 0;
          742  +        p->aStack[i].flags = STK_Null;
   649    743           break;
   650    744         }
   651    745   
   652    746         /* Opcode: Pop P1 * *
   653    747         **
   654    748         ** P1 elements are popped off of the top of stack and discarded.
   655    749         */
................................................................................
   667    761         ** top of the stack.
   668    762         */
   669    763         case OP_Dup: {
   670    764           int i = p->tos - pOp->p1;
   671    765           int j = ++p->tos;
   672    766           if( i<0 ) goto not_enough_stack;
   673    767           if( NeedStack(p, p->tos) ) goto no_mem;
   674         -        p->iStack[j] = p->iStack[i];
   675         -        if( p->zStack[i] ){
   676         -          p->zStack[j] = sqliteMalloc( p->iStack[j] );
   677         -          if( p->zStack[j] ) memcpy(p->zStack[j], p->zStack[i], p->iStack[j]);
          768  +        p->aStack[j] = p->aStack[i];
          769  +        if( p->aStack[i].flags & STK_Dyn ){
          770  +          p->zStack[j] = sqliteMalloc( p->aStack[j].n );
          771  +          if( p->zStack[j]==0 ) goto no_mem;
          772  +          memcpy(p->zStack[j], p->zStack[i], p->aStack[j].n);
   678    773           }else{
   679         -          p->zStack[j] = 0;
          774  +          p->zStack[j] = p->zStack[i];
   680    775           }
   681    776           break;
   682    777         }
   683    778   
   684    779         /* Opcode: Pull P1 * *
   685    780         **
   686         -      ** The P1-th element is removed its current location on 
          781  +      ** The P1-th element is removed from its current location on 
   687    782         ** the stack and pushed back on top of the stack.  The
   688    783         ** top of the stack is element 0, so "Pull 0 0 0" is
   689    784         ** a no-op.
   690    785         */
   691    786         case OP_Pull: {
   692    787           int from = p->tos - pOp->p1;
   693    788           int to = p->tos;
   694    789           int i;
   695         -        int ti;
          790  +        Stack ts;
   696    791           char *tz;
   697    792           if( from<0 ) goto not_enough_stack;
   698         -        ti = p->iStack[from];
          793  +        ts = p->aStack[from];
   699    794           tz = p->zStack[from];
   700    795           for(i=from; i<to; i++){
   701         -          p->iStack[i] = p->iStack[i+1];
          796  +          p->aStack[i] = p->aStack[i+1];
   702    797             p->zStack[i] = p->zStack[i+1];
   703    798           }
   704         -        p->iStack[to] = ti;
          799  +        p->aStack[to] = ts;
   705    800           p->zStack[to] = tz;
   706    801           break;
   707    802         }
   708    803   
   709    804         /* Opcode: ColumnCount P1 * *
   710    805         **
   711    806         ** Specify the number of column values that will appear in the
................................................................................
   740    835         */
   741    836         case OP_Callback: {
   742    837           int i = p->tos - pOp->p1 + 1;
   743    838           int j;
   744    839           if( i<0 ) goto not_enough_stack;
   745    840           if( NeedStack(p, p->tos+2) ) goto no_mem;
   746    841           for(j=i; j<=p->tos; j++){
   747         -          if( Stringify(p, j) ) goto no_mem;
          842  +          if( (p->aStack[j].flags & STK_Null)==0 ){
          843  +            if( Stringify(p, j) ) goto no_mem;
          844  +          }
   748    845           }
   749    846           p->zStack[p->tos+1] = 0;
   750    847           if( xCallback!=0 ){
   751    848             if( xCallback(pArg, pOp->p1, &p->zStack[i], p->azColName)!=0 ){
   752    849               rc = SQLITE_ABORT;
   753    850             }
   754    851           }
   755    852           PopStack(p, pOp->p1);
   756    853           break;
   757    854         }
   758    855   
   759         -      /* Opcode: Concat * * *
          856  +      /* Opcode: Concat P1 P2 P3
   760    857         **
   761         -      ** Pop two elements from the stack.  Append the first (what used
   762         -      ** to be the top of stack) to the second (the next on stack) to 
   763         -      ** form a new string.  Push the new string back onto the stack.
          858  +      ** Look at the first P1 elements of the stack.  Append them all 
          859  +      ** together with the lowest element first.  Use P3 as a separator.  
          860  +      ** Put the result on the top of the stack.  The original P1 elements
          861  +      ** are popped from the stack if P2==0 and retained if P2==1.
          862  +      **
          863  +      ** If P3 is NULL, then use no separator.  When P1==1, this routine
          864  +      ** makes a copy of the top stack element into memory obtained
          865  +      ** from sqliteMalloc().
   764    866         */
   765    867         case OP_Concat: {
   766         -        int tos = p->tos;
   767         -        int nos = tos - 1;
   768         -        char *z;
   769         -        if( nos<0 ) goto not_enough_stack;
   770         -        Stringify(p, tos);
   771         -        Stringify(p, nos);
   772         -        z = 0;
   773         -        sqliteSetString(&z, p->zStack[nos], p->zStack[tos], 0);
   774         -        PopStack(p, 1);
   775         -        sqliteFree(p->zStack[nos]);
   776         -        p->zStack[nos] = z;
   777         -        p->iStack[nos] = strlen(p->zStack[nos])+1;
          868  +        char *zNew;
          869  +        int nByte;
          870  +        int nField;
          871  +        int i, j;
          872  +        char *zSep;
          873  +        int nSep;
          874  +
          875  +        nField = pOp->p1;
          876  +        zSep = pOp->p3;
          877  +        if( zSep==0 ) zSep = "";
          878  +        nSep = strlen(zSep);
          879  +        if( p->tos+1<nField ) goto not_enough_stack;
          880  +        nByte = 1 - nSep;
          881  +        for(i=p->tos-nField+1; i<=p->tos; i++){
          882  +          if( p->aStack[i].flags & STK_Null ){
          883  +            nByte += nSep;
          884  +          }else{
          885  +            if( Stringify(p, i) ) goto no_mem;
          886  +            nByte += p->aStack[i].n - 1 + nSep;
          887  +          }
          888  +        }
          889  +        zNew = sqliteMalloc( nByte );
          890  +        if( zNew==0 ) goto no_mem;
          891  +        j = 0;
          892  +        for(i=p->tos-nField+1; i<=p->tos; i++){
          893  +          if( (p->aStack[i].flags & STK_Null)==0 ){
          894  +            memcpy(&zNew[j], p->zStack[i], p->aStack[i].n-1);
          895  +            j += p->aStack[i].n-1;
          896  +          }
          897  +          if( nSep>0 && i<p->tos ){
          898  +            memcpy(&zNew[j], zSep, nSep);
          899  +            j += nSep;
          900  +          }
          901  +        }
          902  +        zNew[j] = 0;
          903  +        if( pOp->p2==0 ) PopStack(p, nField);
          904  +        NeedStack(p, p->tos+1);
          905  +        p->tos++;
          906  +        p->aStack[p->tos].n = nByte;
          907  +        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
          908  +        p->zStack[p->tos] = zNew;
   778    909           break;
   779    910         }
   780    911   
   781    912         /* Opcode: Add * * *
   782    913         **
   783    914         ** Pop the top two elements from the stack, add them together,
   784    915         ** and push the result back onto the stack.  If either element
................................................................................
   814    945         case OP_Add:
   815    946         case OP_Subtract:
   816    947         case OP_Multiply:
   817    948         case OP_Divide: {
   818    949           int tos = p->tos;
   819    950           int nos = tos - 1;
   820    951           if( nos<0 ) goto not_enough_stack;
   821         -        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
          952  +        if( (p->aStack[tos].flags & p->aStack[nos].flags & STK_Int)==STK_Int ){
   822    953             int a, b;
   823         -          a = p->iStack[tos];
   824         -          b = p->iStack[nos];
          954  +          a = p->aStack[tos].i;
          955  +          b = p->aStack[nos].i;
   825    956             switch( pOp->opcode ){
   826    957               case OP_Add:         b += a;       break;
   827    958               case OP_Subtract:    b -= a;       break;
   828    959               case OP_Multiply:    b *= a;       break;
   829    960               default: {
   830    961                 if( a==0 ){ 
   831    962                   sqliteSetString(pzErrMsg, "division by zero", 0);
................................................................................
   832    963                   rc = SQLITE_ERROR;
   833    964                   goto cleanup;
   834    965                 }
   835    966                 b /= a;
   836    967                 break;
   837    968               }
   838    969             }
   839         -          PopStack(p, 1);
   840         -          p->iStack[nos] = b;
          970  +          PopStack(p, 2);
          971  +          p->tos = nos;
          972  +          p->aStack[nos].i = b;
          973  +          p->aStack[nos].flags = STK_Int;
   841    974           }else{
   842    975             double a, b;
   843         -          Stringify(p, tos);
   844         -          Stringify(p, nos);
   845         -          a = atof(p->zStack[tos]);
   846         -          b = atof(p->zStack[nos]);
          976  +          Realify(p, tos);
          977  +          Realify(p, nos);
          978  +          a = p->aStack[tos].r;
          979  +          b = p->aStack[nos].r;
   847    980             switch( pOp->opcode ){
   848    981               case OP_Add:         b += a;       break;
   849    982               case OP_Subtract:    b -= a;       break;
   850    983               case OP_Multiply:    b *= a;       break;
   851    984               default: {
   852    985                 if( a==0.0 ){ 
   853    986                   sqliteSetString(pzErrMsg, "division by zero", 0);
................................................................................
   854    987                   rc = SQLITE_ERROR;
   855    988                   goto cleanup;
   856    989                 }
   857    990                 b /= a;
   858    991                 break;
   859    992               }
   860    993             }
   861         -          sprintf(zBuf,"%g",b);
   862    994             PopStack(p, 1);
   863         -          sqliteSetString(&p->zStack[nos], zBuf, 0);
   864         -          if( p->zStack[nos]==0 ) goto no_mem;
   865         -          p->iStack[nos] = strlen(p->zStack[nos]) + 1;
          995  +          Release(p, nos);
          996  +          p->aStack[nos].r = b;
          997  +          p->aStack[nos].flags = STK_Real;
   866    998           }
   867    999           break;
   868   1000         }
   869   1001   
   870   1002         /* Opcode: Max * * *
   871   1003         **
   872   1004         ** Pop the top two elements from the stack then push back the
   873   1005         ** largest of the two.
   874   1006         */
   875   1007         case OP_Max: {
   876   1008           int tos = p->tos;
   877   1009           int nos = tos - 1;
         1010  +        int ft, fn;
         1011  +        int copy = 0;
   878   1012           if( nos<0 ) goto not_enough_stack;
   879         -        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
   880         -          if( p->iStack[nos]<p->iStack[tos] ){
   881         -            p->iStack[nos] = p->iStack[tos];
   882         -          }
         1013  +        ft = p->aStack[tos].flags;
         1014  +        fn = p->aStack[nos].flags;
         1015  +        if( fn & STK_Null ){
         1016  +          copy = 1;
         1017  +        }else if( (ft & fn & STK_Int)==STK_Int ){
         1018  +          copy = p->aStack[nos].i<p->aStack[tos].i;
         1019  +        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
         1020  +          Realify(p, tos);
         1021  +          Realify(p, nos);
         1022  +          copy = p->aStack[tos].r>p->aStack[nos].r;
   883   1023           }else{
   884   1024             Stringify(p, tos);
   885   1025             Stringify(p, nos);
   886         -          if( sqliteCompare(p->zStack[nos], p->zStack[tos])<0 ){
   887         -            sqliteFree(p->zStack[nos]);
   888         -            p->zStack[nos] = p->zStack[tos];
   889         -            p->iStack[nos] = p->iStack[tos];
   890         -            p->zStack[tos] = 0;
   891         -          }
         1026  +          copy = sqliteCompare(p->zStack[tos],p->zStack[nos])>0;
   892   1027           }
   893         -        p->tos--;
         1028  +        if( copy ){
         1029  +          Release(p, nos);
         1030  +          p->aStack[nos] = p->aStack[tos];
         1031  +          p->zStack[nos] = p->zStack[tos];
         1032  +          p->zStack[tos] = 0;
         1033  +          p->aStack[tos].flags = 0;
         1034  +        }else{
         1035  +          Release(p, tos);
         1036  +        }
         1037  +        p->tos = nos;
   894   1038           break;
   895   1039         }
   896   1040   
   897   1041         /* Opcode: Min * * *
   898   1042         **
   899   1043         ** Pop the top two elements from the stack then push back the
   900   1044         ** smaller of the two. 
................................................................................
   901   1045         **
   902   1046         ** If P1==1, always choose TOS for the min and decrement P1.
   903   1047         ** This is self-altering code...
   904   1048         */
   905   1049         case OP_Min: {
   906   1050           int tos = p->tos;
   907   1051           int nos = tos - 1;
         1052  +        int ft, fn;
         1053  +        int copy = 0;
   908   1054           if( nos<0 ) goto not_enough_stack;
   909         -        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
   910         -          if( p->iStack[nos]>p->iStack[tos] ){
   911         -            p->iStack[nos] = p->iStack[tos];
   912         -          }
         1055  +        ft = p->aStack[tos].flags;
         1056  +        fn = p->aStack[nos].flags;
         1057  +        if( pOp->p1 ){
         1058  +          copy = 1;
         1059  +          pOp->p1 = 0;
         1060  +        }else if( fn & STK_Null ){
         1061  +          copy = 1;
         1062  +        }else if( (ft & fn & STK_Int)==STK_Int ){
         1063  +          copy = p->aStack[nos].i>p->aStack[tos].i;
         1064  +        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
         1065  +          Realify(p, tos);
         1066  +          Realify(p, nos);
         1067  +          copy = p->aStack[tos].r<p->aStack[nos].r;
   913   1068           }else{
   914   1069             Stringify(p, tos);
   915   1070             Stringify(p, nos);
   916         -          if( pOp->p1==1 ){
   917         -            sqliteFree(p->zStack[nos]);
   918         -            p->zStack[nos] = p->zStack[tos];
   919         -            p->iStack[nos] = p->iStack[tos];
   920         -            p->zStack[tos] = 0;
   921         -            pOp->p1 = 0;
   922         -          }else if( sqliteCompare(p->zStack[nos], p->zStack[tos])>0 ){
   923         -            sqliteFree(p->zStack[nos]);
   924         -            p->zStack[nos] = p->zStack[tos];
   925         -            p->iStack[nos] = p->iStack[tos];
   926         -            p->zStack[tos] = 0;
   927         -          }
         1071  +          copy = sqliteCompare(p->zStack[tos],p->zStack[nos])<0;
   928   1072           }
   929         -        p->tos--;
         1073  +        if( copy ){
         1074  +          Release(p, nos);
         1075  +          p->aStack[nos] = p->aStack[tos];
         1076  +          p->zStack[nos] = p->zStack[tos];
         1077  +          p->zStack[tos] = 0;
         1078  +          p->aStack[tos].flags = 0;
         1079  +        }else{
         1080  +          Release(p, tos);
         1081  +        }
         1082  +        p->tos = nos;
   930   1083           break;
   931   1084         }
   932   1085   
   933   1086         /* Opcode: AddImm  P1 * *
   934   1087         ** 
   935   1088         ** Add the value P1 to whatever is on top of the stack.
   936   1089         */
   937   1090         case OP_AddImm: {
   938   1091           int tos = p->tos;
   939   1092           if( tos<0 ) goto not_enough_stack;
   940   1093           Integerify(p, tos);
   941         -        p->iStack[tos] += pOp->p1;
         1094  +        p->aStack[tos].i += pOp->p1;
   942   1095           break;
   943   1096         }
   944   1097   
   945   1098         /* Opcode: Eq * P2 *
   946   1099         **
   947   1100         ** Pop the top two elements from the stack.  If they are equal, then
   948   1101         ** jump to instruction P2.  Otherwise, continue to the next instruction.
................................................................................
   982   1135         case OP_Lt:
   983   1136         case OP_Le:
   984   1137         case OP_Gt:
   985   1138         case OP_Ge: {
   986   1139           int tos = p->tos;
   987   1140           int nos = tos - 1;
   988   1141           int c;
         1142  +        int ft, fn;
   989   1143           if( nos<0 ) goto not_enough_stack;
   990         -        if( p->zStack[tos]==0 && p->zStack[nos]==0 ){
   991         -          int a, b;
   992         -          a = p->iStack[tos];
   993         -          b = p->iStack[nos];
   994         -          switch( pOp->opcode ){
   995         -            case OP_Eq:    c = b==a;     break;
   996         -            case OP_Ne:    c = b!=a;     break;
   997         -            case OP_Lt:    c = b<a;      break;
   998         -            case OP_Le:    c = b<=a;     break;
   999         -            case OP_Gt:    c = b>a;      break;
  1000         -            default:       c = b>=a;     break;
  1001         -          }
         1144  +        ft = p->aStack[tos].flags;
         1145  +        fn = p->aStack[nos].flags;
         1146  +        if( (ft & fn)==STK_Int ){
         1147  +          c = p->aStack[nos].i - p->aStack[tos].i;
  1002   1148           }else{
  1003   1149             Stringify(p, tos);
  1004   1150             Stringify(p, nos);
  1005   1151             c = sqliteCompare(p->zStack[nos], p->zStack[tos]);
  1006         -          switch( pOp->opcode ){
  1007         -            case OP_Eq:    c = c==0;     break;
  1008         -            case OP_Ne:    c = c!=0;     break;
  1009         -            case OP_Lt:    c = c<0;      break;
  1010         -            case OP_Le:    c = c<=0;     break;
  1011         -            case OP_Gt:    c = c>0;      break;
  1012         -            default:       c = c>=0;     break;
  1013         -          }
         1152  +        }
         1153  +        switch( pOp->opcode ){
         1154  +          case OP_Eq:    c = c==0;     break;
         1155  +          case OP_Ne:    c = c!=0;     break;
         1156  +          case OP_Lt:    c = c<0;      break;
         1157  +          case OP_Le:    c = c<=0;     break;
         1158  +          case OP_Gt:    c = c>0;      break;
         1159  +          default:       c = c>=0;     break;
  1014   1160           }
  1015   1161           PopStack(p, 2);
  1016   1162           if( c ) pc = pOp->p2-1;
  1017   1163           break;
  1018   1164         }
  1019   1165   
  1020   1166         /* Opcode: Like P1 P2 *
................................................................................
  1078   1224           break;
  1079   1225         }
  1080   1226   
  1081   1227         /* Opcode: And * * *
  1082   1228         **
  1083   1229         ** Pop two values off the stack.  Take the logical AND of the
  1084   1230         ** two values and push the resulting boolean value back onto the
  1085         -      ** stack.  Integers are considered false if zero and true otherwise.
  1086         -      ** Strings are considered false if their length is zero and true
  1087         -      ** otherwise.
         1231  +      ** stack. 
  1088   1232         */
  1089   1233         /* Opcode: Or * * *
  1090   1234         **
  1091   1235         ** Pop two values off the stack.  Take the logical OR of the
  1092   1236         ** two values and push the resulting boolean value back onto the
  1093         -      ** stack.  Integers are considered false if zero and true otherwise.
  1094         -      ** Strings are considered false if their length is zero and true
  1095         -      ** otherwise.
         1237  +      ** stack. 
  1096   1238         */
  1097   1239         case OP_And:
  1098   1240         case OP_Or: {
  1099   1241           int tos = p->tos;
  1100   1242           int nos = tos - 1;
  1101         -        int x, y, c;
         1243  +        int c;
  1102   1244           if( nos<0 ) goto not_enough_stack;
  1103         -        x = p->zStack[nos] ? p->zStack[nos][0] : p->iStack[nos];
  1104         -        y = p->zStack[tos] ? p->zStack[tos][0] : p->iStack[tos];
         1245  +        Integerify(p, tos);
         1246  +        Integerify(p, nos);
  1105   1247           if( pOp->opcode==OP_And ){
  1106         -          c = x && y;
         1248  +          c = p->aStack[tos].i && p->aStack[nos].i;
  1107   1249           }else{
  1108         -          c = x || y;
         1250  +          c = p->aStack[tos].i || p->aStack[nos].i;
  1109   1251           }
  1110   1252           PopStack(p, 2);
  1111   1253           p->tos++;
  1112         -        p->iStack[nos] = c;
         1254  +        p->aStack[nos].i = c;
         1255  +        p->aStack[nos].flags = STK_Int;
  1113   1256           break;
  1114   1257         }
  1115   1258   
  1116   1259         /* Opcode: Negative * * *
  1117   1260         **
  1118   1261         ** Treat the top of the stack as a numeric quantity.  Replace it
  1119         -      ** with its additive inverse.  If the top of stack is a string,
  1120         -      ** then it is converted into a number using atof().
         1262  +      ** with its additive inverse.
  1121   1263         */
  1122   1264         case OP_Negative: {
  1123   1265           int tos;
  1124   1266           if( (tos = p->tos)<0 ) goto not_enough_stack;
  1125         -        if( p->zStack[tos] ){
  1126         -          double r = atof(p->zStack[tos]);
  1127         -          sprintf(zBuf, "%g", -r);
  1128         -          sqliteSetString(&p->zStack[tos], zBuf, 0);
  1129         -          p->iStack[tos] = strlen(zBuf) + 1;
         1267  +        if( p->aStack[tos].flags & STK_Real ){
         1268  +          Release(p, tos);
         1269  +          p->aStack[tos].r = -p->aStack[tos].r;
         1270  +          p->aStack[tos].flags = STK_Real;
         1271  +        }else if( p->aStack[tos].flags & STK_Int ){
         1272  +          Release(p, tos);
         1273  +          p->aStack[tos].i = -p->aStack[tos].i;
         1274  +          p->aStack[tos].flags = STK_Int;
  1130   1275           }else{
  1131         -          p->iStack[tos] = -p->iStack[tos];
         1276  +          Realify(p, tos);
         1277  +          Release(p, tos);
         1278  +          p->aStack[tos].r = -p->aStack[tos].r;
         1279  +          p->aStack[tos].flags = STK_Real;
  1132   1280           }
  1133   1281           break;
  1134   1282         }
  1135   1283   
  1136   1284         /* Opcode: Not * * *
  1137   1285         **
  1138   1286         ** Treat the top of the stack as a boolean value.  Replace it
  1139         -      ** with its complement.  Integers are false if zero and true
  1140         -      ** otherwise.  Strings are false if zero-length and true otherwise.
         1287  +      ** with its complement.
  1141   1288         */
  1142   1289         case OP_Not: {
  1143         -        int c;
         1290  +        int tos = p->tos;
  1144   1291           if( p->tos<0 ) goto not_enough_stack;
  1145         -        c = p->zStack[p->tos] ? p->zStack[p->tos][0] : p->iStack[p->tos];
  1146         -        PopStack(p, 1);
  1147         -        p->tos++;
  1148         -        p->iStack[p->tos] = !c;
         1292  +        Integerify(p, tos);
         1293  +        Release(p, tos);
         1294  +        p->aStack[tos].i = !p->aStack[tos].i;
         1295  +        p->aStack[tos].flags = STK_Int;
  1149   1296           break;
  1150   1297         }
  1151   1298   
  1152   1299         /* Opcode: Noop * * *
  1153   1300         **
  1154   1301         ** Do nothing.  This instruction is often useful as a jump
  1155   1302         ** destination.
................................................................................
  1164   1311         ** true, then jump to p2.  Otherwise continue to the next instruction.
  1165   1312         ** An integer is false if zero and true otherwise.  A string is
  1166   1313         ** false if it has zero length and true otherwise.
  1167   1314         */
  1168   1315         case OP_If: {
  1169   1316           int c;
  1170   1317           if( p->tos<0 ) goto not_enough_stack;
  1171         -        c = p->zStack[p->tos] ? p->zStack[p->tos][0] : p->iStack[p->tos];
         1318  +        Integerify(p, p->tos);
         1319  +        c = p->aStack[p->tos].i;
  1172   1320           PopStack(p, 1);
  1173   1321           if( c ) pc = pOp->p2-1;
  1174   1322           break;
  1175   1323         }
  1176   1324   
  1177   1325         /* Opcode: IsNull * P2 *
  1178   1326         **
  1179         -      ** Pop a single value from the stack.  If the value popped is the
  1180         -      ** empty string, then jump to p2.  Otherwise continue to the next 
         1327  +      ** Pop a single value from the stack.  If the value popped is NULL
         1328  +      ** then jump to p2.  Otherwise continue to the next 
  1181   1329         ** instruction.
  1182   1330         */
  1183   1331         case OP_IsNull: {
  1184   1332           int c;
  1185   1333           if( p->tos<0 ) goto not_enough_stack;
  1186         -        c = p->zStack[p->tos]!=0 && p->zStack[p->tos][0]==0;
         1334  +        c = (p->aStack[p->tos].flags & STK_Null)!=0;
  1187   1335           PopStack(p, 1);
  1188   1336           if( c ) pc = pOp->p2-1;
  1189   1337           break;
  1190   1338         }
  1191   1339   
  1192   1340         /* Opcode: NotNull * P2 *
  1193   1341         **
................................................................................
  1194   1342         ** Pop a single value from the stack.  If the value popped is not an
  1195   1343         ** empty string, then jump to p2.  Otherwise continue to the next 
  1196   1344         ** instruction.
  1197   1345         */
  1198   1346         case OP_NotNull: {
  1199   1347           int c;
  1200   1348           if( p->tos<0 ) goto not_enough_stack;
  1201         -        c = p->zStack[p->tos]==0 || p->zStack[p->tos][0]!=0;
         1349  +        c = (p->aStack[p->tos].flags & STK_Null)==0;
  1202   1350           PopStack(p, 1);
  1203   1351           if( c ) pc = pOp->p2-1;
  1204   1352           break;
  1205   1353         }
  1206   1354   
  1207   1355         /* Opcode: MakeRecord P1 * *
  1208   1356         **
  1209   1357         ** Convert the top P1 entries of the stack into a single entry
  1210   1358         ** suitable for use as a data record in the database.  To do this
  1211         -      ** each entry is converted to a string and all the strings are
         1359  +      ** all entries (except NULLs) are converted to strings and 
  1212   1360         ** concatenated.  The null-terminators are preserved by the concatation
  1213   1361         ** and serve as a boundry marker between fields.  The lowest entry
  1214   1362         ** on the stack is the first in the concatenation and the top of
  1215   1363         ** the stack is the last.  After all fields are concatenated, an
  1216   1364         ** index header is added.  The index header consists of P1 integers
  1217   1365         ** which hold the offset of the beginning of each field from the
  1218         -      ** beginning of the completed record including the header.
         1366  +      ** beginning of the completed record including the header.  The
         1367  +      ** index for NULL entries is 0.
  1219   1368         */
  1220   1369         case OP_MakeRecord: {
  1221   1370           char *zNewRecord;
  1222   1371           int nByte;
  1223   1372           int nField;
  1224   1373           int i, j;
  1225   1374           int addr;
  1226   1375   
  1227   1376           nField = pOp->p1;
  1228   1377           if( p->tos+1<nField ) goto not_enough_stack;
  1229   1378           nByte = 0;
  1230   1379           for(i=p->tos-nField+1; i<=p->tos; i++){
  1231         -          if( Stringify(p, i) ) goto no_mem;
  1232         -          nByte += p->iStack[i];
         1380  +          if( (p->aStack[i].flags & STK_Null)==0 ){
         1381  +            if( Stringify(p, i) ) goto no_mem;
         1382  +            nByte += p->aStack[i].n;
         1383  +          }
  1233   1384           }
  1234   1385           nByte += sizeof(int)*nField;
  1235   1386           zNewRecord = sqliteMalloc( nByte );
  1236   1387           if( zNewRecord==0 ) goto no_mem;
  1237   1388           j = 0;
  1238   1389           addr = sizeof(int)*nField;
  1239         -        for(i=p->tos-nField+1; i<p->tos; i++){
  1240         -          memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
  1241         -          addr += p->iStack[i];
         1390  +        for(i=p->tos-nField+1; i<=p->tos; i++){
         1391  +          if( p->aStack[i].flags & STK_Null ){
         1392  +            int zero = 0;
         1393  +            memcpy(&zNewRecord[j], (char*)&zero, sizeof(int));
         1394  +          }else{
         1395  +            memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
         1396  +            addr += p->aStack[i].n;
         1397  +          }
  1242   1398             j += sizeof(int);
  1243   1399           }
  1244         -        memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
  1245         -        j += sizeof(int);
  1246   1400           for(i=p->tos-nField+1; i<=p->tos; i++){
  1247         -          memcpy(&zNewRecord[j], p->zStack[i], p->iStack[i]);
  1248         -          j += p->iStack[i];
         1401  +          if( (p->aStack[i].flags & STK_Null)==0 ){
         1402  +            memcpy(&zNewRecord[j], p->zStack[i], p->aStack[i].n);
         1403  +            j += p->aStack[i].n;
         1404  +          }
  1249   1405           }
  1250   1406           PopStack(p, nField);
  1251   1407           NeedStack(p, p->tos+1);
  1252   1408           p->tos++;
  1253         -        p->iStack[p->tos] = nByte;
         1409  +        p->aStack[p->tos].n = nByte;
         1410  +        p->aStack[p->tos].flags = STK_Str | STK_Dyn;
  1254   1411           p->zStack[p->tos] = zNewRecord;
  1255   1412           break;
  1256   1413         }
  1257   1414   
  1258   1415         /* Opcode: MakeKey P1 P2 *
  1259   1416         **
  1260   1417         ** Convert the top P1 entries of the stack into a single entry suitable
................................................................................
  1277   1434           int nField;
  1278   1435           int i, j;
  1279   1436   
  1280   1437           nField = pOp->p1;
  1281   1438           if( p->tos+1<nField ) goto not_enough_stack;
  1282   1439           nByte = 0;
  1283   1440           for(i=p->tos-nField+1; i<=p->tos; i++){
  1284         -          if( Stringify(p, i) ) goto no_mem;
  1285         -          nByte += p->iStack[i]+1;
         1441  +          if( p->aStack[i].flags & STK_Null ){
         1442  +            nByte++;
         1443  +          }else{
         1444  +            if( Stringify(p, i) ) goto no_mem;
         1445  +            nByte += p->aStack[i].n;
         1446  +          }
  1286   1447           }
  1287   1448           zNewKey = sqliteMalloc( nByte );
  1288   1449           if( zNewKey==0 ) goto no_mem;
  1289   1450           j = 0;
  1290   1451           for(i=p->tos-nField+1; i<=p->tos; i++){
  1291         -          memcpy(&zNewKey[j], p->zStack[i], p->iStack[i]-1);
  1292         -          j += p->iStack[i]-1;
         1452  +          if( (p->aStack[i].flags & STK_Null)==0 ){
         1453  +            memcpy(&zNewKey[j], p->zStack[i], p->aStack[i].n-1);
         1454  +            j += p->aStack[i].n-1;
         1455  +          }
  1293   1456             if( i<p->tos ) zNewKey[j++] = '\t';
  1294   1457           }
  1295   1458           zNewKey[j] = 0;
  1296   1459           if( pOp->p2==0 ) PopStack(p, nField);
  1297   1460           NeedStack(p, p->tos+1);
  1298   1461           p->tos++;
  1299         -        p->iStack[p->tos] = nByte;
         1462  +        p->aStack[p->tos].n = nByte;
         1463  +        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
  1300   1464           p->zStack[p->tos] = zNewKey;
  1301   1465           break;
  1302   1466         }
  1303   1467   
  1304   1468         /* Opcode: Open P1 P2 P3
  1305   1469         **
  1306   1470         ** Open a new database table named P3.  Give it an identifier P1.
................................................................................
  1307   1471         ** Open readonly if P2==0 and for reading and writing if P2!=0.
  1308   1472         ** The table is created if it does not already exist and P2!=0.
  1309   1473         ** If there is already another table opened on P1, then the old
  1310   1474         ** table is closed first.  All tables are automatically closed when
  1311   1475         ** the VDBE finishes execution.  The P1 values need not be
  1312   1476         ** contiguous but all P1 values should be small integers.  It is
  1313   1477         ** an error for P1 to be negative.
         1478  +      **
         1479  +      ** If P3 is null or an empty string, a temporary table is opened.
  1314   1480         */
  1315   1481         case OP_Open: {
  1316   1482           int i = pOp->p1;
  1317   1483           if( i<0 ) goto bad_instruction;
  1318   1484           if( i>=p->nTable ){
  1319   1485             int j;
  1320   1486             p->aTab = sqliteRealloc( p->aTab, (i+1)*sizeof(VdbeTable) );
................................................................................
  1370   1536         ** stack or anything like that.
  1371   1537         */
  1372   1538         case OP_Fetch: {
  1373   1539           int i = pOp->p1;
  1374   1540           int tos = p->tos;
  1375   1541           if( tos<0 ) goto not_enough_stack;
  1376   1542           if( i>=0 && i<p->nTable && p->aTab[i].pTable ){
  1377         -          if( p->zStack[tos]==0 ){
         1543  +          if( p->aStack[tos].flags & STK_Int ){
  1378   1544               sqliteDbbeFetch(p->aTab[i].pTable, sizeof(int), 
  1379         -                           (char*)&p->iStack[tos]);
         1545  +                           (char*)&p->aStack[tos].i);
  1380   1546             }else{
  1381         -            sqliteDbbeFetch(p->aTab[i].pTable, p->iStack[tos], 
         1547  +            if( Stringify(p, tos) ) goto no_mem;
         1548  +            sqliteDbbeFetch(p->aTab[i].pTable, p->aStack[tos].n, 
  1382   1549                              p->zStack[tos]);
  1383   1550             }
  1384   1551           }
  1385   1552           PopStack(p, 1);
  1386   1553           break;
  1387   1554         }
  1388   1555   
................................................................................
  1395   1562         */
  1396   1563         case OP_Distinct: {
  1397   1564           int i = pOp->p1;
  1398   1565           int tos = p->tos;
  1399   1566           int alreadyExists = 0;
  1400   1567           if( tos<0 ) goto not_enough_stack;
  1401   1568           if( i>=0 && i<p->nTable && p->aTab[i].pTable ){
  1402         -          if( p->zStack[tos]==0 ){
         1569  +          if( p->aStack[tos].flags & STK_Int ){
  1403   1570               alreadyExists = sqliteDbbeTest(p->aTab[i].pTable, sizeof(int), 
  1404         -                                          (char*)&p->iStack[tos]);
         1571  +                                          (char*)&p->aStack[tos].i);
  1405   1572             }else{
  1406         -            alreadyExists = sqliteDbbeTest(p->aTab[i].pTable, p->iStack[tos], 
         1573  +            if( Stringify(p, tos) ) goto no_mem;
         1574  +            alreadyExists = sqliteDbbeTest(p->aTab[i].pTable, p->aStack[tos].n, 
  1407   1575                                              p->zStack[tos]);
  1408   1576             }
  1409   1577           }
  1410   1578           if( !alreadyExists ){
  1411   1579             pc = pOp->p2 - 1;
  1412   1580           }
  1413   1581           break;
................................................................................
  1424   1592           if( i<0 || i>=p->nTable || p->aTab[i].pTable==0 ){
  1425   1593             v = 0;
  1426   1594           }else{
  1427   1595             v = sqliteDbbeNew(p->aTab[i].pTable);
  1428   1596           }
  1429   1597           NeedStack(p, p->tos+1);
  1430   1598           p->tos++;
  1431         -        p->iStack[p->tos] = v;
         1599  +        p->aStack[p->tos].i = v;
         1600  +        p->aStack[p->tos].flags = STK_Int;
  1432   1601           break;
  1433   1602         }
  1434   1603   
  1435   1604         /* Opcode: Put P1 * *
  1436   1605         **
  1437   1606         ** Write an entry into the database table P1.  A new entry is
  1438   1607         ** created if it doesn't already exist, or the data for an existing
................................................................................
  1444   1613           int tos = p->tos;
  1445   1614           int nos = p->tos-1;
  1446   1615           int i = pOp->p1;
  1447   1616           if( nos<0 ) goto not_enough_stack;
  1448   1617           if( i>=0 && i<p->nTable && p->aTab[i].pTable!=0 ){
  1449   1618             char *zKey;
  1450   1619             int nKey;
  1451         -          Stringify(p, tos);
  1452         -          if( p->zStack[nos]!=0 ){
  1453         -            nKey = p->iStack[nos];
         1620  +          if( (p->aStack[nos].flags & STK_Int)==0 ){
         1621  +            if( Stringify(p, nos) ) goto no_mem;
         1622  +            nKey = p->aStack[nos].n;
  1454   1623               zKey = p->zStack[nos];
  1455   1624             }else{
  1456   1625               nKey = sizeof(int);
  1457         -            zKey = (char*)&p->iStack[nos];
         1626  +            zKey = (char*)&p->aStack[nos].i;
  1458   1627             }
  1459   1628             sqliteDbbePut(p->aTab[i].pTable, nKey, zKey,
  1460         -                        p->iStack[tos], p->zStack[tos]);
         1629  +                        p->aStack[tos].n, p->zStack[tos]);
  1461   1630           }
  1462   1631           PopStack(p, 2);
  1463   1632           break;
  1464   1633         }
  1465   1634   
  1466   1635         /* Opcode: Delete P1 * *
  1467   1636         **
................................................................................
  1471   1640         case OP_Delete: {
  1472   1641           int tos = p->tos;
  1473   1642           int i = pOp->p1;
  1474   1643           if( tos<0 ) goto not_enough_stack;
  1475   1644           if( i>=0 && i<p->nTable && p->aTab[i].pTable!=0 ){
  1476   1645             char *zKey;
  1477   1646             int nKey;
  1478         -          if( p->zStack[tos]!=0 ){
  1479         -            nKey = p->iStack[tos];
         1647  +          if( (p->aStack[tos].flags & STK_Int)==0 ){
         1648  +            if( Stringify(p, tos) ) goto no_mem;
         1649  +            nKey = p->aStack[tos].n;
  1480   1650               zKey = p->zStack[tos];
  1481   1651             }else{
  1482   1652               nKey = sizeof(int);
  1483         -            zKey = (char*)&p->iStack[tos];
         1653  +            zKey = (char*)&p->aStack[tos].n;
  1484   1654             }
  1485   1655             sqliteDbbeDelete(p->aTab[i].pTable, nKey, zKey);
  1486   1656           }
  1487   1657           PopStack(p, 1);
  1488   1658           break;
  1489   1659         }
  1490   1660   
  1491   1661         /* Opcode: Field P1 P2 *
  1492   1662         **
  1493   1663         ** Push onto the stack the value of the P2-th field from the
  1494   1664         ** most recent Fetch from table P1.
         1665  +      ** 
         1666  +      ** The value pushed is just a pointer to the data in the cursor.
         1667  +      ** The value will go away the next time a record is fetched from P1,
         1668  +      ** or when P1 is closed.  Make a copy of the string if it needs
         1669  +      ** to persist longer than that.
  1495   1670         */
  1496   1671         case OP_Field: {
  1497   1672           int *pAddr;
  1498   1673           int amt;
  1499   1674           int i = pOp->p1;
  1500   1675           int p2 = pOp->p2;
  1501   1676           int tos = ++p->tos;
  1502   1677           DbbeTable *pTab;
  1503   1678           char *z;
  1504   1679   
  1505         -        if( NeedStack(p, p->tos) ) goto no_mem;
         1680  +        if( NeedStack(p, tos) ) goto no_mem;
  1506   1681           if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
  1507   1682             amt = sqliteDbbeDataLength(pTab);
  1508   1683             if( amt<=sizeof(int)*(p2+1) ){
  1509         -            sqliteSetString(&p->zStack[tos], "", 0);
         1684  +            p->aStack[tos].flags = STK_Null;
  1510   1685               break;
  1511   1686             }
  1512   1687             pAddr = (int*)sqliteDbbeReadData(pTab, sizeof(int)*p2);
  1513         -          z = sqliteDbbeReadData(pTab, *pAddr);
  1514         -          sqliteSetString(&p->zStack[tos], z, 0);
  1515         -          p->iStack[tos] = strlen(z)+1;
         1688  +          if( *pAddr==0 ){
         1689  +            p->aStack[tos].flags = STK_Null;
         1690  +            break;
         1691  +          }
         1692  +          p->zStack[tos] = z = sqliteDbbeReadData(pTab, *pAddr);
         1693  +          p->aStack[tos].n = strlen(z) + 1;
         1694  +          p->aStack[tos].flags = STK_Str;
  1516   1695           }
  1517   1696           break;
  1518   1697         }
  1519   1698   
  1520   1699         /* Opcode: Key P1 * *
  1521   1700         **
  1522   1701         ** Push onto the stack an integer which is the first 4 bytes of the
................................................................................
  1527   1706           int i = pOp->p1;
  1528   1707           int tos = ++p->tos;
  1529   1708           DbbeTable *pTab;
  1530   1709   
  1531   1710           if( NeedStack(p, p->tos) ) goto no_mem;
  1532   1711           if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
  1533   1712             char *z = sqliteDbbeReadKey(pTab, 0);
  1534         -          memcpy(&p->iStack[tos], z, sizeof(int));
  1535         -          p->zStack[tos] = 0;
         1713  +          memcpy(&p->aStack[tos].i, z, sizeof(int));
         1714  +          p->aStack[tos].flags = STK_Int;
  1536   1715           }
  1537   1716           break;
  1538   1717         }
  1539   1718   
  1540   1719         /* Opcode: Rewind P1 * *
  1541   1720         **
  1542   1721         ** The next use of the Key or Field or Next instruction for P1 
................................................................................
  1595   1774             int *aIdx;
  1596   1775             int nIdx;
  1597   1776             int j;
  1598   1777             nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
  1599   1778             aIdx = (int*)sqliteDbbeReadData(pTab, 0);
  1600   1779             for(j=p->aTab[i].index; j<nIdx; j++){
  1601   1780               if( aIdx[j]!=0 ){
  1602         -              p->iStack[tos] = aIdx[j];
         1781  +              p->aStack[tos].i = aIdx[j];
         1782  +              p->aStack[tos].flags = STK_Int;
  1603   1783                 break;
  1604   1784               }
  1605   1785             }
  1606   1786             if( j>=nIdx ){
  1607   1787               j = -1;
  1608   1788               pc = pOp->p2 - 1;
  1609   1789               PopStack(p, 1);
................................................................................
  1626   1806           int i = pOp->p1;
  1627   1807           int tos = p->tos;
  1628   1808           int nos = tos - 1;
  1629   1809           DbbeTable *pTab;
  1630   1810           if( nos<0 ) goto not_enough_stack;
  1631   1811           if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
  1632   1812             int r;
  1633         -          int newVal = Integerify(p, nos);
  1634         -          Stringify(p, tos);
  1635         -          r = sqliteDbbeFetch(pTab, p->iStack[tos], p->zStack[tos]);
         1813  +          int newVal;
         1814  +          Integerify(p, nos);
         1815  +          newVal = p->aStack[nos].i;
         1816  +          if( Stringify(p, tos) ) goto no_mem;
         1817  +          r = sqliteDbbeFetch(pTab, p->aStack[tos].n, p->zStack[tos]);
  1636   1818             if( r==0 ){
  1637   1819               /* Create a new record for this index */
  1638         -            sqliteDbbePut(pTab, p->iStack[tos], p->zStack[tos],
         1820  +            sqliteDbbePut(pTab, p->aStack[tos].n, p->zStack[tos],
  1639   1821                             sizeof(int), (char*)&newVal);
  1640   1822             }else{
  1641   1823               /* Extend the existing record */
  1642   1824               int nIdx;
  1643   1825               int *aIdx;
  1644   1826               nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
  1645   1827               aIdx = sqliteMalloc( sizeof(int)*(nIdx+1) );
  1646   1828               if( aIdx==0 ) goto no_mem;
  1647   1829               sqliteDbbeCopyData(pTab, 0, nIdx*sizeof(int), (char*)aIdx);
  1648   1830               aIdx[nIdx] = newVal;
  1649         -            sqliteDbbePut(pTab, p->iStack[tos], p->zStack[tos],
         1831  +            sqliteDbbePut(pTab, p->aStack[tos].n, p->zStack[tos],
  1650   1832                             sizeof(int)*(nIdx+1), (char*)aIdx);
  1651   1833               sqliteFree(aIdx);
  1652   1834             }
  1653   1835           }
  1654   1836           PopStack(p, 2);
  1655   1837           break;
  1656   1838         }
................................................................................
  1671   1853           DbbeTable *pTab;
  1672   1854           if( nos<0 ) goto not_enough_stack;
  1673   1855           if( i>=0 && i<p->nTable && (pTab = p->aTab[i].pTable)!=0 ){
  1674   1856             int *aIdx;
  1675   1857             int nIdx;
  1676   1858             int j;
  1677   1859             int r;
  1678         -          int oldVal = Integerify(p, nos);
  1679         -          Stringify(p, tos);
  1680         -          r = sqliteDbbeFetch(pTab, p->iStack[tos], p->zStack[tos]);
         1860  +          int oldVal;
         1861  +          Integerify(p, nos);
         1862  +          oldVal = p->aStack[nos].i;
         1863  +          if( Stringify(p, tos) ) goto no_mem;
         1864  +          r = sqliteDbbeFetch(pTab, p->aStack[tos].n, p->zStack[tos]);
  1681   1865             if( r==0 ) break;
  1682   1866             nIdx = sqliteDbbeDataLength(pTab)/sizeof(int);
  1683   1867             aIdx = (int*)sqliteDbbeReadData(pTab, 0);
  1684   1868             for(j=0; j<nIdx && aIdx[j]!=oldVal; j++){}
  1685   1869             if( j>=nIdx ) break;
  1686   1870             aIdx[j] = aIdx[nIdx-1];
  1687   1871             if( nIdx==1 ){
  1688         -            sqliteDbbeDelete(pTab, p->iStack[tos], p->zStack[tos]);
         1872  +            sqliteDbbeDelete(pTab, p->aStack[tos].n, p->zStack[tos]);
  1689   1873             }else{
  1690         -            sqliteDbbePut(pTab, p->iStack[tos], p->zStack[tos], 
         1874  +            sqliteDbbePut(pTab, p->aStack[tos].n, p->zStack[tos], 
  1691   1875                             sizeof(int)*(nIdx-1), (char*)aIdx);
  1692   1876             }
  1693   1877           }
  1694   1878           PopStack(p, 2);
  1695   1879           break;
  1696   1880         }
  1697   1881   
................................................................................
  1747   1931         ** into the temporary storage file P1.
  1748   1932         */
  1749   1933         case OP_ListWrite: {
  1750   1934           int i = pOp->p1;
  1751   1935           if( i<0 ) goto bad_instruction;
  1752   1936           if( p->tos<0 ) goto not_enough_stack;
  1753   1937           if( i<p->nList && p->apList[i]!=0 ){
  1754         -          int val = Integerify(p, p->tos);
         1938  +          int val;
         1939  +          Integerify(p, p->tos);
         1940  +          val = p->aStack[p->tos].i;
  1755   1941             PopStack(p, 1);
  1756   1942             fwrite(&val, sizeof(int), 1, p->apList[i]);
  1757   1943           }
  1758   1944           break;
  1759   1945         }
  1760   1946   
  1761   1947         /* Opcode: ListRewind P1 * *
................................................................................
  1781   1967           int i = pOp->p1;
  1782   1968           int val, amt;
  1783   1969           if( i<0 || i>=p->nList || p->apList[i]==0 ) goto bad_instruction;
  1784   1970           amt = fread(&val, sizeof(int), 1, p->apList[i]);
  1785   1971           if( amt==1 ){
  1786   1972             p->tos++;
  1787   1973             if( NeedStack(p, p->tos) ) goto no_mem;
  1788         -          p->iStack[p->tos] = val;
  1789         -          p->zStack[p->tos] = 0;
         1974  +          p->aStack[p->tos].n = val;
         1975  +          p->aStack[p->tos].flags = STK_Int;
  1790   1976           }else{
  1791   1977             pc = pOp->p2 - 1;
  1792   1978           }
  1793   1979           break;
  1794   1980         }
  1795   1981   
  1796   1982         /* Opcode: ListClose P1 * *
................................................................................
  1827   2013         /* Opcode: SortPut P1 * *
  1828   2014         **
  1829   2015         ** The TOS is the key and the NOS is the data.  Pop both from the stack
  1830   2016         ** and put them on the sorter.
  1831   2017         */
  1832   2018         case OP_SortPut: {
  1833   2019           int i = pOp->p1;
         2020  +        int tos = p->tos;
         2021  +        int nos = tos - 1;
  1834   2022           Sorter *pSorter;
  1835   2023           if( i<0 || i>=p->nSort ) goto bad_instruction;
  1836         -        if( p->tos<1 ) goto not_enough_stack;
  1837         -        Stringify(p, p->tos);
  1838         -        Stringify(p, p->tos-1);
         2024  +        if( tos<1 ) goto not_enough_stack;
         2025  +        if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem;
  1839   2026           pSorter = sqliteMalloc( sizeof(Sorter) );
  1840   2027           if( pSorter==0 ) goto no_mem;
  1841   2028           pSorter->pNext = p->apSort[i];
  1842   2029           p->apSort[i] = pSorter;
  1843         -        pSorter->nKey = p->iStack[p->tos];
  1844         -        pSorter->zKey = p->zStack[p->tos];
  1845         -        pSorter->nData = p->iStack[p->tos-1];
  1846         -        pSorter->pData = p->zStack[p->tos-1];
  1847         -        p->zStack[p->tos] = p->zStack[p->tos-1] = 0;
  1848         -        PopStack(p, 2);
         2030  +        pSorter->nKey = p->aStack[tos].n;
         2031  +        pSorter->zKey = p->zStack[tos];
         2032  +        pSorter->nData = p->aStack[nos].n;
         2033  +        pSorter->pData = p->zStack[nos];
         2034  +        p->aStack[tos].flags = 0;
         2035  +        p->aStack[nos].flags = 0;
         2036  +        p->zStack[tos] = 0;
         2037  +        p->zStack[nos] = 0;
         2038  +        p->tos -= 2;
  1849   2039           break;
  1850   2040         }
  1851   2041   
  1852   2042         /* Opcode: SortMakeRec P1 * *
  1853   2043         **
  1854   2044         ** The top P1 elements are the arguments to a callback.  Form these
  1855   2045         ** elements into a single data entry that can be stored on a sorter
................................................................................
  1862   2052           int nField;
  1863   2053           int i, j;
  1864   2054   
  1865   2055           nField = pOp->p1;
  1866   2056           if( p->tos+1<nField ) goto not_enough_stack;
  1867   2057           nByte = 0;
  1868   2058           for(i=p->tos-nField+1; i<=p->tos; i++){
  1869         -          if( Stringify(p, i) ) goto no_mem;
  1870         -          nByte += p->iStack[i];
         2059  +          if( (p->aStack[i].flags & STK_Null)==0 ){
         2060  +            if( Stringify(p, i) ) goto no_mem;
         2061  +            nByte += p->aStack[i].n;
         2062  +          }
  1871   2063           }
  1872   2064           nByte += sizeof(char*)*(nField+1);
  1873   2065           azArg = sqliteMalloc( nByte );
  1874   2066           if( azArg==0 ) goto no_mem;
  1875   2067           z = (char*)&azArg[nField+1];
  1876   2068           for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){
  1877         -          azArg[j] = z;
  1878         -          strcpy(z, p->zStack[i]);
  1879         -          z += p->iStack[i];
         2069  +          if( p->aStack[i].flags & STK_Null ){
         2070  +            azArg[j] = 0;
         2071  +          }else{
         2072  +            azArg[j] = z;
         2073  +            strcpy(z, p->zStack[i]);
         2074  +            z += p->aStack[i].n;
         2075  +          }
  1880   2076           }
  1881   2077           PopStack(p, nField);
  1882   2078           NeedStack(p, p->tos+1);
  1883   2079           p->tos++;
  1884         -        p->iStack[p->tos] = nByte;
         2080  +        p->aStack[p->tos].n = nByte;
  1885   2081           p->zStack[p->tos] = (char*)azArg;
         2082  +        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
  1886   2083           break;
  1887   2084         }
  1888   2085   
  1889   2086         /* Opcode: SortMakeKey P1 * P3
  1890   2087         **
  1891   2088         ** Convert the top few entries of the stack into a sort key.  The
  1892   2089         ** number of stack entries consumed is the number of characters in 
................................................................................
  1906   2103           int i, j, k;
  1907   2104   
  1908   2105           nField = strlen(pOp->p3);
  1909   2106           if( p->tos+1<nField ) goto not_enough_stack;
  1910   2107           nByte = 1;
  1911   2108           for(i=p->tos-nField+1; i<=p->tos; i++){
  1912   2109             if( Stringify(p, i) ) goto no_mem;
  1913         -          nByte += p->iStack[i]+2;
         2110  +          nByte += p->aStack[i].n+2;
  1914   2111           }
  1915   2112           zNewKey = sqliteMalloc( nByte );
  1916   2113           if( zNewKey==0 ) goto no_mem;
  1917   2114           j = 0;
  1918   2115           k = nField-1;
  1919   2116           for(i=p->tos-nField+1; i<=p->tos; i++){
  1920   2117             zNewKey[j++] = pOp->p3[k--];
  1921         -          memcpy(&zNewKey[j], p->zStack[i], p->iStack[i]-1);
  1922         -          j += p->iStack[i]-1;
         2118  +          memcpy(&zNewKey[j], p->zStack[i], p->aStack[i].n-1);
         2119  +          j += p->aStack[i].n-1;
  1923   2120             zNewKey[j++] = 0;
  1924   2121           }
  1925   2122           zNewKey[j] = 0;
  1926   2123           PopStack(p, nField);
  1927   2124           NeedStack(p, p->tos+1);
  1928   2125           p->tos++;
  1929         -        p->iStack[p->tos] = nByte;
         2126  +        p->aStack[p->tos].n = nByte;
         2127  +        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
  1930   2128           p->zStack[p->tos] = zNewKey;
  1931   2129           break;
  1932   2130         }
  1933   2131   
  1934   2132         /* Opcode: Sort P1 * *
  1935   2133         **
  1936   2134         ** Sort all elements on the given sorter.  The algorithm is a
................................................................................
  1983   2181           if( i<0 ) goto bad_instruction;
  1984   2182           if( i<p->nSort && p->apSort[i]!=0 ){
  1985   2183             Sorter *pSorter = p->apSort[i];
  1986   2184             p->apSort[i] = pSorter->pNext;
  1987   2185             p->tos++;
  1988   2186             NeedStack(p, p->tos);
  1989   2187             p->zStack[p->tos] = pSorter->pData;
  1990         -          p->iStack[p->tos] = pSorter->nData;
         2188  +          p->aStack[p->tos].n = pSorter->nData;
         2189  +          p->aStack[p->tos].flags = STK_Str|STK_Dyn;
  1991   2190             sqliteFree(pSorter->zKey);
  1992   2191             sqliteFree(pSorter);
  1993   2192           }else{
  1994   2193             pc = pOp->p2 - 1;
  1995   2194           }
  1996   2195           break;
  1997   2196         }
................................................................................
  2005   2204           int i = pOp->p1;
  2006   2205           if( i<0 ) goto bad_instruction;
  2007   2206           if( i<p->nSort && p->apSort[i]!=0 ){
  2008   2207             Sorter *pSorter = p->apSort[i];
  2009   2208             p->tos++;
  2010   2209             NeedStack(p, p->tos);
  2011   2210             sqliteSetString(&p->zStack[p->tos], pSorter->zKey, 0);
  2012         -          p->iStack[p->tos] = pSorter->nKey;
         2211  +          p->aStack[p->tos].n = pSorter->nKey;
         2212  +          p->aStack[p->tos].flags = STK_Str|STK_Dyn;
  2013   2213           }
  2014   2214           break;
  2015   2215         }
  2016   2216   
  2017   2217         /* Opcode: SortCallback P1 P2 *
  2018   2218         **
  2019   2219         ** The top of the stack contains a callback record built using
................................................................................
  2186   2386           if( i>=0 && i<p->nField && p->azField ){
  2187   2387             z = p->azField[i];
  2188   2388           }else{
  2189   2389             z = 0;
  2190   2390           }
  2191   2391           if( z==0 ) z = "";
  2192   2392           p->tos++;
  2193         -        p->iStack[p->tos] = strlen(z) + 1;
  2194         -        sqliteSetString(&p->zStack[p->tos], z, 0);
         2393  +        p->aStack[p->tos].n = strlen(z) + 1;
         2394  +        p->zStack[p->tos] = z;
         2395  +        p->aStack[p->tos].flags = STK_Str;
  2195   2396           break;
  2196   2397         }
  2197   2398   
  2198   2399         /* An other opcode is illegal...
  2199   2400         */
  2200   2401         default: {
  2201   2402           sprintf(zBuf,"%d",pOp->opcode);
................................................................................
  2208   2409         sqliteSetString(pzErrMsg, "jump destination out of range", 0);
  2209   2410         rc = SQLITE_INTERNAL;
  2210   2411       }
  2211   2412       if( p->trace && p->tos>=0 ){
  2212   2413         int i;
  2213   2414         fprintf(p->trace, "Stack:");
  2214   2415         for(i=p->tos; i>=0 && i>p->tos-5; i--){
  2215         -        if( p->zStack[i] ){
  2216         -          fprintf(p->trace, " [%.11s]", p->zStack[i]);
         2416  +        if( p->aStack[i].flags & STK_Null ){
         2417  +          fprintf(p->trace, " NULL");
         2418  +        }else if( p->aStack[i].flags & STK_Int ){
         2419  +          fprintf(p->trace, " i:%d", p->aStack[i].i);
         2420  +        }else if( p->aStack[i].flags & STK_Real ){
         2421  +          fprintf(p->trace, " r:%g", p->aStack[i].r);
         2422  +        }else if( p->aStack[i].flags & STK_Str ){
         2423  +          if( p->aStack[i].flags & STK_Dyn ){
         2424  +            fprintf(p->trace, " z:[%.11s]", p->zStack[i]);
         2425  +          }else{
         2426  +            fprintf(p->trace, " s:[%.11s]", p->zStack[i]);
         2427  +          }
  2217   2428           }else{
  2218         -          fprintf(p->trace, " [%d]", p->iStack[i]);
         2429  +          fprintf(p->trace, " ???");
  2219   2430           }
  2220   2431         }
  2221   2432         fprintf(p->trace,"\n");
  2222   2433       }
  2223   2434     }
  2224   2435   
  2225   2436   cleanup:

Changes to src/vdbe.h.

    23     23   *************************************************************************
    24     24   ** Header file for the Virtual DataBase Engine (VDBE)
    25     25   **
    26     26   ** This header defines the interface to the virtual database engine
    27     27   ** or VDBE.  The VDBE implements an abstract machine that runs a
    28     28   ** simple program to access and modify the underlying database.
    29     29   **
    30         -** $Id: vdbe.h,v 1.4 2000/05/31 20:00:53 drh Exp $
           30  +** $Id: vdbe.h,v 1.5 2000/06/04 12:58:39 drh Exp $
    31     31   */
    32     32   #ifndef _SQLITE_VDBE_H_
    33     33   #define _SQLITE_VDBE_H_
    34     34   #include <stdio.h>
    35     35   
    36     36   /*
    37     37   ** A single VDBE is an opaque structure named "Vdbe".  Only routines
................................................................................
   121    121   
   122    122   #define OP_ColumnCount        41
   123    123   #define OP_ColumnName         42
   124    124   #define OP_Callback           43
   125    125   
   126    126   #define OP_Integer            44
   127    127   #define OP_String             45
   128         -#define OP_Pop                46
   129         -#define OP_Dup                47
   130         -#define OP_Pull               48
          128  +#define OP_Null               46
          129  +#define OP_Pop                47
          130  +#define OP_Dup                48
          131  +#define OP_Pull               49
   131    132   
   132         -#define OP_Add                49
   133         -#define OP_AddImm             50
   134         -#define OP_Subtract           51
   135         -#define OP_Multiply           52
   136         -#define OP_Divide             53
   137         -#define OP_Min                54
   138         -#define OP_Max                55
   139         -#define OP_Like               56
   140         -#define OP_Glob               57
   141         -#define OP_Eq                 58
   142         -#define OP_Ne                 59
   143         -#define OP_Lt                 60
   144         -#define OP_Le                 61
   145         -#define OP_Gt                 62
   146         -#define OP_Ge                 63
   147         -#define OP_IsNull             64
   148         -#define OP_NotNull            65
   149         -#define OP_Negative           66
   150         -#define OP_And                67
   151         -#define OP_Or                 68
   152         -#define OP_Not                69
   153         -#define OP_Concat             70
   154         -#define OP_Noop               71
          133  +#define OP_Add                50
          134  +#define OP_AddImm             51
          135  +#define OP_Subtract           52
          136  +#define OP_Multiply           53
          137  +#define OP_Divide             54
          138  +#define OP_Min                55
          139  +#define OP_Max                56
          140  +#define OP_Like               57
          141  +#define OP_Glob               58
          142  +#define OP_Eq                 59
          143  +#define OP_Ne                 60
          144  +#define OP_Lt                 61
          145  +#define OP_Le                 62
          146  +#define OP_Gt                 63
          147  +#define OP_Ge                 64
          148  +#define OP_IsNull             65
          149  +#define OP_NotNull            66
          150  +#define OP_Negative           67
          151  +#define OP_And                68
          152  +#define OP_Or                 69
          153  +#define OP_Not                70
          154  +#define OP_Concat             71
          155  +#define OP_Noop               72
   155    156   
   156         -#define OP_MAX                71
          157  +#define OP_MAX                72
   157    158   
   158    159   /*
   159    160   ** Prototypes for the VDBE interface.  See comments on the implementation
   160    161   ** for a description of what each of these routines does.
   161    162   */
   162    163   Vdbe *sqliteVdbeCreate(Dbbe*);
   163    164   int sqliteVdbeAddOp(Vdbe*,int,int,int,const char*,int);