/ Check-in [c8d2bd37]
Login

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

Overview
Comment:Merge the fts3-refactor branch with the trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fts3-refactor
Files: files | file ages | folders
SHA1: c8d2bd37a4c16154912a0831690584011dc230cb
User & Date: dan 2009-11-19 14:52:58
Original User & Date: dan 2009-11-19 02:52:58
Context
2009-11-19
14:57
Merge with [4bd4330709]. check-in: 7a46d1eb user: dan tags: fts3-refactor
14:52
Merge the fts3-refactor branch with the trunk. check-in: c8d2bd37 user: dan tags: fts3-refactor
00:15
Fix problems introduced into fts3 as part of the refactoring. check-in: fa0998e1 user: dan tags: fts3-refactor
2009-11-18
23:01
Performance improvement by avoiding unnecessary calls to memset(). check-in: 85940468 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to mkopcodeh.awk.

    45     45     sub("\r","",name)
    46     46     op[name] = -1
    47     47     jump[name] = 0
    48     48     out2_prerelease[name] = 0
    49     49     in1[name] = 0
    50     50     in2[name] = 0
    51     51     in3[name] = 0
           52  +  out2[name] = 0
    52     53     out3[name] = 0
    53     54     for(i=3; i<NF; i++){
    54     55       if($i=="same" && $(i+1)=="as"){
    55     56         sym = $(i+2)
    56     57         sub(/,/,"",sym)
    57     58         op[name] = tk[sym]
    58     59         used[op[name]] = 1
................................................................................
    66     67         out2_prerelease[name] = 1
    67     68       }else if(x=="in1"){
    68     69         in1[name] = 1
    69     70       }else if(x=="in2"){
    70     71         in2[name] = 1
    71     72       }else if(x=="in3"){
    72     73         in3[name] = 1
           74  +    }else if(x=="out2"){
           75  +      out2[name] = 1
    73     76       }else if(x=="out3"){
    74     77         out3[name] = 1
    75     78       }
    76     79     }
    77     80     order[n_op++] = name;
    78     81   }
    79     82   
................................................................................
   121    124     #  bit 2:     output to p1.  release p1 before opcode runs
   122    125     #
   123    126     for(i=0; i<=max; i++) bv[i] = 0;
   124    127     for(i=0; i<n_op; i++){
   125    128       name = order[i];
   126    129       x = op[name]
   127    130       a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0
   128         -    # a8 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = 0
          131  +    # a7 = a9 = a10 = a11 = a12 = a13 = a14 = a15 = 0
   129    132       if( jump[name] ) a0 = 1;
   130    133       if( out2_prerelease[name] ) a1 = 2;
   131    134       if( in1[name] ) a2 = 4;
   132    135       if( in2[name] ) a3 = 8;
   133    136       if( in3[name] ) a4 = 16;
   134         -    if( out3[name] ) a5 = 32;
          137  +    if( out2[name] ) a5 = 32;
          138  +    if( out3[name] ) a6 = 64;
   135    139       # bv[x] = a0+a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15;
   136    140       bv[x] = a0+a1+a2+a3+a4+a5+a6+a7;
   137    141     }
   138    142     print "\n"
   139    143     print "/* Properties such as \"out2\" or \"jump\" that are specified in"
   140    144     print "** comments following the \"case\" for each opcode in the vdbe.c"
   141    145     print "** are encoded into bitvectors as follows:"
   142    146     print "*/"
   143    147     print "#define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */"
   144    148     print "#define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */"
   145    149     print "#define OPFLG_IN1             0x0004  /* in1:   P1 is an input */"
   146    150     print "#define OPFLG_IN2             0x0008  /* in2:   P2 is an input */"
   147    151     print "#define OPFLG_IN3             0x0010  /* in3:   P3 is an input */"
   148         -  print "#define OPFLG_OUT3            0x0020  /* out3:  P3 is an output */"
          152  +  print "#define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */"
          153  +  print "#define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */"
   149    154     print "#define OPFLG_INITIALIZER {\\"
   150    155     for(i=0; i<=max; i++){
   151    156       if( i%8==0 ) printf("/* %3d */",i)
   152    157       printf " 0x%02x,", bv[i]
   153    158       if( i%8==7 ) printf("\\\n");
   154    159     }
   155    160     print "}"
   156    161   }

Changes to src/btree.c.

  3274   3274   **
  3275   3275   ** 4:  There must be an active transaction.
  3276   3276   **
  3277   3277   ** No checking is done to make sure that page iTable really is the
  3278   3278   ** root page of a b-tree.  If it is not, then the cursor acquired
  3279   3279   ** will not work correctly.
  3280   3280   **
  3281         -** It is assumed that the sqlite3BtreeCursorSize() bytes of memory 
  3282         -** pointed to by pCur have been zeroed by the caller.
         3281  +** It is assumed that the sqlite3BtreeCursorZero() has been called
         3282  +** on pCur to initialize the memory space prior to invoking this routine.
  3283   3283   */
  3284   3284   static int btreeCursor(
  3285   3285     Btree *p,                              /* The btree */
  3286   3286     int iTable,                            /* Root page of table to open */
  3287   3287     int wrFlag,                            /* 1 to write. 0 read-only */
  3288   3288     struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
  3289   3289     BtCursor *pCur                         /* Space for new cursor */
................................................................................
  3350   3350   ** sufficient storage to hold a cursor.  The BtCursor object is opaque
  3351   3351   ** to users so they cannot do the sizeof() themselves - they must call
  3352   3352   ** this routine.
  3353   3353   */
  3354   3354   int sqlite3BtreeCursorSize(void){
  3355   3355     return ROUND8(sizeof(BtCursor));
  3356   3356   }
         3357  +
         3358  +/*
         3359  +** Initialize memory that will be converted into a BtCursor object.
         3360  +**
         3361  +** The simple approach here would be to memset() the entire object
         3362  +** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
         3363  +** do not need to be zeroed and they are large, so we can save a lot
         3364  +** of run-time by skipping the initialization of those elements.
         3365  +*/
         3366  +void sqlite3BtreeCursorZero(BtCursor *p){
         3367  +  memset(p, 0, offsetof(BtCursor, iPage));
         3368  +}
  3357   3369   
  3358   3370   /*
  3359   3371   ** Set the cached rowid value of every cursor in the same database file
  3360   3372   ** as pCur and having the same root page number as pCur.  The value is
  3361   3373   ** set to iRowid.
  3362   3374   **
  3363   3375   ** Only positive rowid values are considered valid for this cache.

Changes to src/btree.h.

   144    144     Btree*,                              /* BTree containing table to open */
   145    145     int iTable,                          /* Index of root page */
   146    146     int wrFlag,                          /* 1 for writing.  0 for read-only */
   147    147     struct KeyInfo*,                     /* First argument to compare function */
   148    148     BtCursor *pCursor                    /* Space to write cursor structure */
   149    149   );
   150    150   int sqlite3BtreeCursorSize(void);
          151  +void sqlite3BtreeCursorZero(BtCursor*);
   151    152   
   152    153   int sqlite3BtreeCloseCursor(BtCursor*);
   153    154   int sqlite3BtreeMovetoUnpacked(
   154    155     BtCursor*,
   155    156     UnpackedRecord *pUnKey,
   156    157     i64 intKey,
   157    158     int bias,

Changes to src/complete.c.

    20     20   #ifndef SQLITE_OMIT_COMPLETE
    21     21   
    22     22   /*
    23     23   ** This is defined in tokenize.c.  We just have to import the definition.
    24     24   */
    25     25   #ifndef SQLITE_AMALGAMATION
    26     26   #ifdef SQLITE_ASCII
    27         -extern const char sqlite3IsAsciiIdChar[];
    28         -#define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
           27  +#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
    29     28   #endif
    30     29   #ifdef SQLITE_EBCDIC
    31     30   extern const char sqlite3IsEbcdicIdChar[];
    32     31   #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
    33     32   #endif
    34     33   #endif /* SQLITE_AMALGAMATION */
    35     34   
................................................................................
   178    177           zSql++;
   179    178           while( *zSql && *zSql!=c ){ zSql++; }
   180    179           if( *zSql==0 ) return 0;
   181    180           token = tkOTHER;
   182    181           break;
   183    182         }
   184    183         default: {
   185         -        int c;
          184  +#ifdef SQLITE_EBCDIC
          185  +        unsigned char c;
          186  +#endif
   186    187           if( IdChar((u8)*zSql) ){
   187    188             /* Keywords and unquoted identifiers */
   188    189             int nId;
   189    190             for(nId=1; IdChar(zSql[nId]); nId++){}
   190    191   #ifdef SQLITE_OMIT_TRIGGER
   191    192             token = tkOTHER;
   192    193   #else

Changes to src/expr.c.

  1253   1253                  || (p->flags2 & EP2_MallocedToken)==0 );
  1254   1254       p->op = TK_INTEGER;
  1255   1255       p->flags |= EP_IntValue;
  1256   1256       p->u.iValue = *pValue;
  1257   1257     }
  1258   1258     return rc;
  1259   1259   }
         1260  +
         1261  +/*
         1262  +** Return FALSE if there is no chance that the expression can be NULL.
         1263  +**
         1264  +** If the expression might be NULL or if the expression is too complex
         1265  +** to tell return TRUE.  
         1266  +**
         1267  +** This routine is used as an optimization, to skip OP_IsNull opcodes
         1268  +** when we know that a value cannot be NULL.  Hence, a false positive
         1269  +** (returning TRUE when in fact the expression can never be NULL) might
         1270  +** be a small performance hit but is otherwise harmless.  On the other
         1271  +** hand, a false negative (returning FALSE when the result could be NULL)
         1272  +** will likely result in an incorrect answer.  So when in doubt, return
         1273  +** TRUE.
         1274  +*/
         1275  +int sqlite3ExprCanBeNull(const Expr *p){
         1276  +  u8 op;
         1277  +  while( p->op==TK_UPLUS || p->op==TK_MINUS ){ p = p->pLeft; }
         1278  +  op = p->op;
         1279  +  if( op==TK_REGISTER ) op = p->op2;
         1280  +  switch( op ){
         1281  +    case TK_INTEGER:
         1282  +    case TK_STRING:
         1283  +    case TK_FLOAT:
         1284  +    case TK_BLOB:
         1285  +      return 0;
         1286  +    default:
         1287  +      return 1;
         1288  +  }
         1289  +}
         1290  +
         1291  +/*
         1292  +** Generate an OP_IsNull instruction that tests register iReg and jumps
         1293  +** to location iDest if the value in iReg is NULL.  The value in iReg 
         1294  +** was computed by pExpr.  If we can look at pExpr at compile-time and
         1295  +** determine that it can never generate a NULL, then the OP_IsNull operation
         1296  +** can be omitted.
         1297  +*/
         1298  +void sqlite3ExprCodeIsNullJump(
         1299  +  Vdbe *v,            /* The VDBE under construction */
         1300  +  const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
         1301  +  int iReg,           /* Test the value in this register for NULL */
         1302  +  int iDest           /* Jump here if the value is null */
         1303  +){
         1304  +  if( sqlite3ExprCanBeNull(pExpr) ){
         1305  +    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
         1306  +  }
         1307  +}
         1308  +
         1309  +/*
         1310  +** Return TRUE if the given expression is a constant which would be
         1311  +** unchanged by OP_Affinity with the affinity given in the second
         1312  +** argument.
         1313  +**
         1314  +** This routine is used to determine if the OP_Affinity operation
         1315  +** can be omitted.  When in doubt return FALSE.  A false negative
         1316  +** is harmless.  A false positive, however, can result in the wrong
         1317  +** answer.
         1318  +*/
         1319  +int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
         1320  +  u8 op;
         1321  +  if( aff==SQLITE_AFF_NONE ) return 1;
         1322  +  while( p->op==TK_UPLUS || p->op==TK_MINUS ){ p = p->pLeft; }
         1323  +  op = p->op;
         1324  +  if( op==TK_REGISTER ) op = p->op2;
         1325  +  switch( op ){
         1326  +    case TK_INTEGER: {
         1327  +      return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
         1328  +    }
         1329  +    case TK_FLOAT: {
         1330  +      return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
         1331  +    }
         1332  +    case TK_STRING: {
         1333  +      return aff==SQLITE_AFF_TEXT;
         1334  +    }
         1335  +    case TK_BLOB: {
         1336  +      return 1;
         1337  +    }
         1338  +    case TK_COLUMN: {
         1339  +      return p->iTable>=0 && p->iColumn<0
         1340  +               && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
         1341  +    }
         1342  +    default: {
         1343  +      return 0;
         1344  +    }
         1345  +  }
         1346  +}
  1260   1347   
  1261   1348   /*
  1262   1349   ** Return TRUE if the given string is a row-id column name.
  1263   1350   */
  1264   1351   int sqlite3IsRowid(const char *z){
  1265   1352     if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
  1266   1353     if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;

Changes to src/global.c.

    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains definitions of global variables and contants.
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   
    17         -
    18     17   /* An array to map all upper-case characters into their corresponding
    19     18   ** lower-case character. 
    20     19   **
    21     20   ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
    22     21   ** handle case conversions for the UTF character set since the tables
    23     22   ** involved are nearly as big or bigger than SQLite itself.
    24     23   */
................................................................................
    66     65   **
    67     66   **   isspace()                        0x01
    68     67   **   isalpha()                        0x02
    69     68   **   isdigit()                        0x04
    70     69   **   isalnum()                        0x06
    71     70   **   isxdigit()                       0x08
    72     71   **   toupper()                        0x20
           72  +**   SQLite identifier character      0x40
    73     73   **
    74     74   ** Bit 0x20 is set if the mapped character requires translation to upper
    75     75   ** case. i.e. if the character is a lower-case ASCII character.
    76     76   ** If x is a lower-case ASCII character, then its upper-case equivalent
    77     77   ** is (x - 0x20). Therefore toupper() can be implemented as:
    78     78   **
    79     79   **   (x & ~(map[x]&0x20))
    80     80   **
    81     81   ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
    82     82   ** array. tolower() is used more often than toupper() by SQLite.
           83  +**
           84  +** Bit 0x40 is set if the character non-alphanumeric and can be used in an 
           85  +** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
           86  +** non-ASCII UTF character. Hence the test for whether or not a character is
           87  +** part of an identifier is 0x46.
    83     88   **
    84     89   ** SQLite's versions are identical to the standard versions assuming a
    85     90   ** locale of "C". They are implemented as macros in sqliteInt.h.
    86     91   */
    87     92   #ifdef SQLITE_ASCII
    88     93   const unsigned char sqlite3CtypeMap[256] = {
    89     94     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
    90     95     0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
    91     96     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
    92     97     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
    93         -  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
           98  +  0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
    94     99     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
    95    100     0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
    96    101     0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
    97    102   
    98    103     0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
    99    104     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
   100    105     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
   101         -  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 58..5f    XYZ[\]^_ */
          106  +  0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
   102    107     0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
   103    108     0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
   104    109     0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
   105    110     0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
   106    111   
   107         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 80..87    ........ */
   108         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 88..8f    ........ */
   109         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 90..97    ........ */
   110         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 98..9f    ........ */
   111         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* a0..a7    ........ */
   112         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* a8..af    ........ */
   113         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* b0..b7    ........ */
   114         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* b8..bf    ........ */
          112  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
          113  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
          114  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
          115  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
          116  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
          117  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
          118  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
          119  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
   115    120   
   116         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* c0..c7    ........ */
   117         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* c8..cf    ........ */
   118         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* d0..d7    ........ */
   119         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* d8..df    ........ */
   120         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* e0..e7    ........ */
   121         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* e8..ef    ........ */
   122         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* f0..f7    ........ */
   123         -  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00   /* f8..ff    ........ */
          121  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
          122  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
          123  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
          124  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
          125  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
          126  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
          127  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
          128  +  0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
   124    129   };
   125    130   #endif
   126    131   
   127    132   
   128    133   
   129    134   /*
   130    135   ** The following singleton contains the global configuration for
................................................................................
   184    189   **
   185    190   ** IMPORTANT:  Changing the pending byte to any value other than
   186    191   ** 0x40000000 results in an incompatible database file format!
   187    192   ** Changing the pending byte during operating results in undefined
   188    193   ** and dileterious behavior.
   189    194   */
   190    195   int sqlite3PendingByte = 0x40000000;
          196  +
          197  +#include "opcodes.h"
          198  +/*
          199  +** Properties of opcodes.  The OPFLG_INITIALIZER macro is
          200  +** created by mkopcodeh.awk during compilation.  Data is obtained
          201  +** from the comments following the "case OP_xxxx:" statements in
          202  +** the vdbe.c file.  
          203  +*/
          204  +const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;

Changes to src/os.c.

   134    134   }
   135    135   int sqlite3OsFullPathname(
   136    136     sqlite3_vfs *pVfs, 
   137    137     const char *zPath, 
   138    138     int nPathOut, 
   139    139     char *zPathOut
   140    140   ){
          141  +  zPathOut[0] = 0;
   141    142     return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
   142    143   }
   143    144   #ifndef SQLITE_OMIT_LOAD_EXTENSION
   144    145   void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
   145    146     return pVfs->xDlOpen(pVfs, zPath);
   146    147   }
   147    148   void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){

Changes to src/sqliteInt.h.

  2670   2670   void sqlite3RollbackTransaction(Parse*);
  2671   2671   void sqlite3Savepoint(Parse*, int, Token*);
  2672   2672   void sqlite3CloseSavepoints(sqlite3 *);
  2673   2673   int sqlite3ExprIsConstant(Expr*);
  2674   2674   int sqlite3ExprIsConstantNotJoin(Expr*);
  2675   2675   int sqlite3ExprIsConstantOrFunction(Expr*);
  2676   2676   int sqlite3ExprIsInteger(Expr*, int*);
         2677  +int sqlite3ExprCanBeNull(const Expr*);
         2678  +void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
         2679  +int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  2677   2680   int sqlite3IsRowid(const char*);
  2678   2681   void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
  2679   2682   void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
  2680   2683   int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
  2681   2684   void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
  2682   2685                                        int*,int,int,int,int,int*);
  2683   2686   void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
................................................................................
  2841   2844   char *sqlite3Utf16to8(sqlite3 *, const void*, int);
  2842   2845   #ifdef SQLITE_ENABLE_STAT2
  2843   2846   char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
  2844   2847   #endif
  2845   2848   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  2846   2849   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  2847   2850   #ifndef SQLITE_AMALGAMATION
         2851  +extern const unsigned char sqlite3OpcodeProperty[];
  2848   2852   extern const unsigned char sqlite3UpperToLower[];
  2849   2853   extern const unsigned char sqlite3CtypeMap[];
  2850   2854   extern SQLITE_WSD struct Sqlite3Config sqlite3Config;
  2851   2855   extern SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  2852   2856   extern int sqlite3PendingByte;
  2853   2857   #endif
  2854   2858   void sqlite3RootPageMoved(Db*, int, int);

Changes to src/tokenize.c.

    78     78   **
    79     79   ** Ticket #1066.  the SQL standard does not allow '$' in the
    80     80   ** middle of identfiers.  But many SQL implementations do. 
    81     81   ** SQLite will allow '$' in identifiers for compatibility.
    82     82   ** But the feature is undocumented.
    83     83   */
    84     84   #ifdef SQLITE_ASCII
    85         -const char sqlite3IsAsciiIdChar[] = {
    86         -/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
    87         -    0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
    88         -    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
    89         -    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
    90         -    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
    91         -    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
    92         -    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
    93         -};
    94         -#define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
           85  +#define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
    95     86   #endif
    96     87   #ifdef SQLITE_EBCDIC
    97     88   const char sqlite3IsEbcdicIdChar[] = {
    98     89   /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
    99     90       0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
   100     91       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
   101     92       0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */

Changes to src/vdbe.c.

   166    166     else if( flags & MEM_Str ){
   167    167       pMem->type = SQLITE_TEXT;
   168    168     }else{
   169    169       pMem->type = SQLITE_BLOB;
   170    170     }
   171    171   }
   172    172   
   173         -/*
   174         -** Properties of opcodes.  The OPFLG_INITIALIZER macro is
   175         -** created by mkopcodeh.awk during compilation.  Data is obtained
   176         -** from the comments following the "case OP_xxxx:" statements in
   177         -** this file.  
   178         -*/
   179         -static const unsigned char opcodeProperty[] = OPFLG_INITIALIZER;
   180         -
   181         -/*
   182         -** Return true if an opcode has any of the OPFLG_xxx properties
   183         -** specified by mask.
   184         -*/
   185         -int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
   186         -  assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) );
   187         -  return (opcodeProperty[opcode]&mask)!=0;
   188         -}
   189         -
   190    173   /*
   191    174   ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
   192    175   ** if we run out of memory.
   193    176   */
   194    177   static VdbeCursor *allocateCursor(
   195    178     Vdbe *p,              /* The virtual machine */
   196    179     int iCur,             /* Index of the new VdbeCursor */
................................................................................
   228    211     assert( iCur<p->nCursor );
   229    212     if( p->apCsr[iCur] ){
   230    213       sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   231    214       p->apCsr[iCur] = 0;
   232    215     }
   233    216     if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
   234    217       p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
   235         -    memset(pCx, 0, nByte);
          218  +    memset(pCx, 0, sizeof(VdbeCursor));
   236    219       pCx->iDb = iDb;
   237    220       pCx->nField = nField;
   238    221       if( nField ){
   239    222         pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
   240    223       }
   241    224       if( isBtreeCursor ){
   242    225         pCx->pCursor = (BtCursor*)
   243    226             &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
          227  +      sqlite3BtreeCursorZero(pCx->pCursor);
   244    228       }
   245    229     }
   246    230     return pCx;
   247    231   }
   248    232   
   249    233   /*
   250    234   ** Try to convert a value into a numeric representation if we can
................................................................................
   551    535   ** After this routine has finished, sqlite3VdbeFinalize() should be
   552    536   ** used to clean up the mess that was left behind.
   553    537   */
   554    538   int sqlite3VdbeExec(
   555    539     Vdbe *p                    /* The VDBE */
   556    540   ){
   557    541     int pc;                    /* The program counter */
          542  +  Op *aOp = p->aOp;          /* Copy of p->aOp */
   558    543     Op *pOp;                   /* Current operation */
   559    544     int rc = SQLITE_OK;        /* Value to return */
   560    545     sqlite3 *db = p->db;       /* The database */
   561    546     u8 encoding = ENC(db);     /* The database encoding */
          547  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
          548  +  u8 checkProgress;          /* True if progress callbacks are enabled */
          549  +  int nProgressOps = 0;      /* Opcodes executed since progress callback. */
          550  +#endif
          551  +  Mem *aMem = p->aMem;       /* Copy of p->aMem */
   562    552     Mem *pIn1 = 0;             /* 1st input operand */
   563    553     Mem *pIn2 = 0;             /* 2nd input operand */
   564    554     Mem *pIn3 = 0;             /* 3rd input operand */
   565    555     Mem *pOut = 0;             /* Output operand */
   566         -  u8 opProperty;
   567    556     int iCompare = 0;          /* Result of last OP_Compare operation */
   568    557     int *aPermute = 0;         /* Permutation of columns for OP_Compare */
   569    558   #ifdef VDBE_PROFILE
   570    559     u64 start;                 /* CPU clock count at start of opcode */
   571    560     int origPc;                /* Program counter at start of opcode */
   572    561   #endif
   573         -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   574         -  int nProgressOps = 0;      /* Opcodes executed since progress callback. */
   575         -#endif
   576    562     /*** INSERT STACK UNION HERE ***/
   577    563   
   578    564     assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
   579    565     assert( db->magic==SQLITE_MAGIC_BUSY );
   580    566     sqlite3VdbeMutexArrayEnter(p);
   581    567     if( p->rc==SQLITE_NOMEM ){
   582    568       /* This happens if a malloc() inside a call to sqlite3_column_text() or
................................................................................
   586    572     assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
   587    573     p->rc = SQLITE_OK;
   588    574     assert( p->explain==0 );
   589    575     p->pResultSet = 0;
   590    576     db->busyHandler.nBusy = 0;
   591    577     CHECK_FOR_INTERRUPT;
   592    578     sqlite3VdbeIOTraceSql(p);
          579  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
          580  +  checkProgress = db->xProgress!=0;
          581  +#endif
   593    582   #ifdef SQLITE_DEBUG
   594    583     sqlite3BeginBenignMalloc();
   595    584     if( p->pc==0 
   596    585      && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
   597    586     ){
   598    587       int i;
   599    588       printf("VDBE Program Listing:\n");
   600    589       sqlite3VdbePrintSql(p);
   601    590       for(i=0; i<p->nOp; i++){
   602         -      sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
          591  +      sqlite3VdbePrintOp(stdout, i, &aOp[i]);
   603    592       }
   604    593     }
   605    594     if( fileExists(db, "vdbe_trace") ){
   606    595       p->trace = stdout;
   607    596     }
   608    597     sqlite3EndBenignMalloc();
   609    598   #endif
................................................................................
   610    599     for(pc=p->pc; rc==SQLITE_OK; pc++){
   611    600       assert( pc>=0 && pc<p->nOp );
   612    601       if( db->mallocFailed ) goto no_mem;
   613    602   #ifdef VDBE_PROFILE
   614    603       origPc = pc;
   615    604       start = sqlite3Hwtime();
   616    605   #endif
   617         -    pOp = &p->aOp[pc];
          606  +    pOp = &aOp[pc];
   618    607   
   619    608       /* Only allow tracing if SQLITE_DEBUG is defined.
   620    609       */
   621    610   #ifdef SQLITE_DEBUG
   622    611       if( p->trace ){
   623    612         if( pc==0 ){
   624    613           printf("VDBE Execution Trace:\n");
................................................................................
   651    640   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   652    641       /* Call the progress callback if it is configured and the required number
   653    642       ** of VDBE ops have been executed (either since this invocation of
   654    643       ** sqlite3VdbeExec() or since last time the progress callback was called).
   655    644       ** If the progress callback returns non-zero, exit the virtual machine with
   656    645       ** a return code SQLITE_ABORT.
   657    646       */
   658         -    if( db->xProgress ){
          647  +    if( checkProgress ){
   659    648         if( db->nProgressOps==nProgressOps ){
   660    649           int prc;
   661    650           if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
   662    651           prc =db->xProgress(db->pProgressArg);
   663    652           if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
   664    653           if( prc!=0 ){
   665    654             rc = SQLITE_INTERRUPT;
................................................................................
   667    656           }
   668    657           nProgressOps = 0;
   669    658         }
   670    659         nProgressOps++;
   671    660       }
   672    661   #endif
   673    662   
   674         -    /* Do common setup processing for any opcode that is marked
   675         -    ** with the "out2-prerelease" tag.  Such opcodes have a single
   676         -    ** output which is specified by the P2 parameter.  The P2 register
   677         -    ** is initialized to a NULL.
          663  +    /* On any opcode with the "out2-prerelase" tag, free any
          664  +    ** external allocations out of mem[p2] and set mem[p2] to be
          665  +    ** an undefined integer.  Opcodes will either fill in the integer
          666  +    ** value or convert mem[p2] to a different type.
   678    667       */
   679         -    opProperty = opcodeProperty[pOp->opcode];
   680         -    if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
          668  +    assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
          669  +    if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   681    670         assert( pOp->p2>0 );
   682    671         assert( pOp->p2<=p->nMem );
   683         -      pOut = &p->aMem[pOp->p2];
          672  +      pOut = &aMem[pOp->p2];
   684    673         sqlite3VdbeMemReleaseExternal(pOut);
   685         -      pOut->flags = MEM_Null;
   686         -      pOut->n = 0;
   687         -    }else
   688         - 
   689         -    /* Do common setup for opcodes marked with one of the following
   690         -    ** combinations of properties.
   691         -    **
   692         -    **           in1
   693         -    **           in1 in2
   694         -    **           in1 in2 out3
   695         -    **           in1 in3
   696         -    **
   697         -    ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
   698         -    ** registers for inputs.  Variable pOut points to the output register.
   699         -    */
   700         -    if( (opProperty & OPFLG_IN1)!=0 ){
          674  +      pOut->flags = MEM_Int;
          675  +    }
          676  +
          677  +    /* Sanity checking on other operands */
          678  +#ifdef SQLITE_DEBUG
          679  +    if( (pOp->opflags & OPFLG_IN1)!=0 ){
   701    680         assert( pOp->p1>0 );
   702    681         assert( pOp->p1<=p->nMem );
   703         -      pIn1 = &p->aMem[pOp->p1];
   704         -      REGISTER_TRACE(pOp->p1, pIn1);
   705         -      if( (opProperty & OPFLG_IN2)!=0 ){
   706         -        assert( pOp->p2>0 );
   707         -        assert( pOp->p2<=p->nMem );
   708         -        pIn2 = &p->aMem[pOp->p2];
   709         -        REGISTER_TRACE(pOp->p2, pIn2);
   710         -        /* As currently implemented, in2 implies out3.  There is no reason
   711         -        ** why this has to be, it just worked out that way. */
   712         -        assert( (opProperty & OPFLG_OUT3)!=0 );
   713         -        assert( pOp->p3>0 );
   714         -        assert( pOp->p3<=p->nMem );
   715         -        pOut = &p->aMem[pOp->p3];
   716         -      }else if( (opProperty & OPFLG_IN3)!=0 ){
   717         -        assert( pOp->p3>0 );
   718         -        assert( pOp->p3<=p->nMem );
   719         -        pIn3 = &p->aMem[pOp->p3];
   720         -        REGISTER_TRACE(pOp->p3, pIn3);
   721         -      }
   722         -    }else if( (opProperty & OPFLG_IN2)!=0 ){
          682  +      REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
          683  +    }
          684  +    if( (pOp->opflags & OPFLG_IN2)!=0 ){
          685  +      assert( pOp->p2>0 );
          686  +      assert( pOp->p2<=p->nMem );
          687  +      REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
          688  +    }
          689  +    if( (pOp->opflags & OPFLG_IN3)!=0 ){
          690  +      assert( pOp->p3>0 );
          691  +      assert( pOp->p3<=p->nMem );
          692  +      REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
          693  +    }
          694  +    if( (pOp->opflags & OPFLG_OUT2)!=0 ){
   723    695         assert( pOp->p2>0 );
   724    696         assert( pOp->p2<=p->nMem );
   725         -      pIn2 = &p->aMem[pOp->p2];
   726         -      REGISTER_TRACE(pOp->p2, pIn2);
   727         -    }else if( (opProperty & OPFLG_IN3)!=0 ){
          697  +    }
          698  +    if( (pOp->opflags & OPFLG_OUT3)!=0 ){
   728    699         assert( pOp->p3>0 );
   729    700         assert( pOp->p3<=p->nMem );
   730         -      pIn3 = &p->aMem[pOp->p3];
   731         -      REGISTER_TRACE(pOp->p3, pIn3);
   732    701       }
   733         -
          702  +#endif
          703  +  
   734    704       switch( pOp->opcode ){
   735    705   
   736    706   /*****************************************************************************
   737    707   ** What follows is a massive switch statement where each case implements a
   738    708   ** separate instruction in the virtual machine.  If we follow the usual
   739    709   ** indentation conventions, each case should be indented by 6 spaces.  But
   740    710   ** that is a lot of wasted space on the left margin.  So the code within
................................................................................
   782    752   }
   783    753   
   784    754   /* Opcode:  Gosub P1 P2 * * *
   785    755   **
   786    756   ** Write the current address onto register P1
   787    757   ** and then jump to address P2.
   788    758   */
   789         -case OP_Gosub: {            /* jump */
   790         -  assert( pOp->p1>0 );
   791         -  assert( pOp->p1<=p->nMem );
   792         -  pIn1 = &p->aMem[pOp->p1];
          759  +case OP_Gosub: {            /* jump, in1 */
          760  +  pIn1 = &aMem[pOp->p1];
   793    761     assert( (pIn1->flags & MEM_Dyn)==0 );
   794    762     pIn1->flags = MEM_Int;
   795    763     pIn1->u.i = pc;
   796    764     REGISTER_TRACE(pOp->p1, pIn1);
   797    765     pc = pOp->p2 - 1;
   798    766     break;
   799    767   }
   800    768   
   801    769   /* Opcode:  Return P1 * * * *
   802    770   **
   803    771   ** Jump to the next instruction after the address in register P1.
   804    772   */
   805    773   case OP_Return: {           /* in1 */
          774  +  pIn1 = &aMem[pOp->p1];
   806    775     assert( pIn1->flags & MEM_Int );
   807    776     pc = (int)pIn1->u.i;
   808    777     break;
   809    778   }
   810    779   
   811    780   /* Opcode:  Yield P1 * * * *
   812    781   **
   813    782   ** Swap the program counter with the value in register P1.
   814    783   */
   815    784   case OP_Yield: {            /* in1 */
   816    785     int pcDest;
          786  +  pIn1 = &aMem[pOp->p1];
   817    787     assert( (pIn1->flags & MEM_Dyn)==0 );
   818    788     pIn1->flags = MEM_Int;
   819    789     pcDest = (int)pIn1->u.i;
   820    790     pIn1->u.i = pc;
   821    791     REGISTER_TRACE(pOp->p1, pIn1);
   822    792     pc = pcDest;
   823    793     break;
................................................................................
   826    796   /* Opcode:  HaltIfNull  P1 P2 P3 P4 *
   827    797   **
   828    798   ** Check the value in register P3.  If is is NULL then Halt using
   829    799   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
   830    800   ** value in register P3 is not NULL, then this routine is a no-op.
   831    801   */
   832    802   case OP_HaltIfNull: {      /* in3 */
          803  +  pIn3 = &aMem[pOp->p3];
   833    804     if( (pIn3->flags & MEM_Null)==0 ) break;
   834    805     /* Fall through into OP_Halt */
   835    806   }
   836    807   
   837    808   /* Opcode:  Halt P1 P2 * P4 *
   838    809   **
   839    810   ** Exit immediately.  All open cursors, etc are closed
................................................................................
   865    836         /* Instruction pc is the OP_Program that invoked the sub-program 
   866    837         ** currently being halted. If the p2 instruction of this OP_Halt
   867    838         ** instruction is set to OE_Ignore, then the sub-program is throwing
   868    839         ** an IGNORE exception. In this case jump to the address specified
   869    840         ** as the p2 of the calling OP_Program.  */
   870    841         pc = p->aOp[pc].p2-1;
   871    842       }
          843  +    aOp = p->aOp;
          844  +    aMem = p->aMem;
   872    845       break;
   873    846     }
   874    847   
   875    848     p->rc = pOp->p1;
   876    849     p->errorAction = (u8)pOp->p2;
   877    850     p->pc = pc;
   878    851     if( pOp->p4.z ){
................................................................................
   891    864   }
   892    865   
   893    866   /* Opcode: Integer P1 P2 * * *
   894    867   **
   895    868   ** The 32-bit integer value P1 is written into register P2.
   896    869   */
   897    870   case OP_Integer: {         /* out2-prerelease */
   898         -  pOut->flags = MEM_Int;
   899    871     pOut->u.i = pOp->p1;
   900    872     break;
   901    873   }
   902    874   
   903    875   /* Opcode: Int64 * P2 * P4 *
   904    876   **
   905    877   ** P4 is a pointer to a 64-bit integer value.
   906    878   ** Write that value into register P2.
   907    879   */
   908    880   case OP_Int64: {           /* out2-prerelease */
   909    881     assert( pOp->p4.pI64!=0 );
   910         -  pOut->flags = MEM_Int;
   911    882     pOut->u.i = *pOp->p4.pI64;
   912    883     break;
   913    884   }
   914    885   
   915    886   /* Opcode: Real * P2 * P4 *
   916    887   **
   917    888   ** P4 is a pointer to a 64-bit floating point value.
................................................................................
   973    944   }
   974    945   
   975    946   /* Opcode: Null * P2 * * *
   976    947   **
   977    948   ** Write a NULL into register P2.
   978    949   */
   979    950   case OP_Null: {           /* out2-prerelease */
          951  +  pOut->flags = MEM_Null;
   980    952     break;
   981    953   }
   982    954   
   983    955   
   984    956   /* Opcode: Blob P1 P2 * P4
   985    957   **
   986    958   ** P4 points to a blob of data P1 bytes long.  Store this
................................................................................
  1020    992     assert( pOp->p4.z==0 || pOp->p3==1 || pOp->p3==0 );
  1021    993   
  1022    994     while( n-- > 0 ){
  1023    995       pVar = &p->aVar[p1++];
  1024    996       if( sqlite3VdbeMemTooBig(pVar) ){
  1025    997         goto too_big;
  1026    998       }
  1027         -    pOut = &p->aMem[p2++];
          999  +    pOut = &aMem[p2++];
  1028   1000       sqlite3VdbeMemReleaseExternal(pOut);
  1029   1001       pOut->flags = MEM_Null;
  1030   1002       sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
  1031   1003       UPDATE_MAX_BLOBSIZE(pOut);
  1032   1004     }
  1033   1005     break;
  1034   1006   }
................................................................................
  1048   1020   
  1049   1021     n = pOp->p3;
  1050   1022     p1 = pOp->p1;
  1051   1023     p2 = pOp->p2;
  1052   1024     assert( n>0 && p1>0 && p2>0 );
  1053   1025     assert( p1+n<=p2 || p2+n<=p1 );
  1054   1026   
  1055         -  pIn1 = &p->aMem[p1];
  1056         -  pOut = &p->aMem[p2];
         1027  +  pIn1 = &aMem[p1];
         1028  +  pOut = &aMem[p2];
  1057   1029     while( n-- ){
  1058         -    assert( pOut<=&p->aMem[p->nMem] );
  1059         -    assert( pIn1<=&p->aMem[p->nMem] );
         1030  +    assert( pOut<=&aMem[p->nMem] );
         1031  +    assert( pIn1<=&aMem[p->nMem] );
  1060   1032       zMalloc = pOut->zMalloc;
  1061   1033       pOut->zMalloc = 0;
  1062   1034       sqlite3VdbeMemMove(pOut, pIn1);
  1063   1035       pIn1->zMalloc = zMalloc;
  1064   1036       REGISTER_TRACE(p2++, pOut);
  1065   1037       pIn1++;
  1066   1038       pOut++;
................................................................................
  1071   1043   /* Opcode: Copy P1 P2 * * *
  1072   1044   **
  1073   1045   ** Make a copy of register P1 into register P2.
  1074   1046   **
  1075   1047   ** This instruction makes a deep copy of the value.  A duplicate
  1076   1048   ** is made of any string or blob constant.  See also OP_SCopy.
  1077   1049   */
  1078         -case OP_Copy: {             /* in1 */
  1079         -  assert( pOp->p2>0 );
  1080         -  assert( pOp->p2<=p->nMem );
  1081         -  pOut = &p->aMem[pOp->p2];
         1050  +case OP_Copy: {             /* in1, out2 */
         1051  +  pIn1 = &aMem[pOp->p1];
         1052  +  pOut = &aMem[pOp->p2];
  1082   1053     assert( pOut!=pIn1 );
  1083   1054     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1084   1055     Deephemeralize(pOut);
  1085   1056     REGISTER_TRACE(pOp->p2, pOut);
  1086   1057     break;
  1087   1058   }
  1088   1059   
................................................................................
  1094   1065   ** is a string or blob, then the copy is only a pointer to the
  1095   1066   ** original and hence if the original changes so will the copy.
  1096   1067   ** Worse, if the original is deallocated, the copy becomes invalid.
  1097   1068   ** Thus the program must guarantee that the original will not change
  1098   1069   ** during the lifetime of the copy.  Use OP_Copy to make a complete
  1099   1070   ** copy.
  1100   1071   */
  1101         -case OP_SCopy: {            /* in1 */
  1102         -  REGISTER_TRACE(pOp->p1, pIn1);
  1103         -  assert( pOp->p2>0 );
  1104         -  assert( pOp->p2<=p->nMem );
  1105         -  pOut = &p->aMem[pOp->p2];
         1072  +case OP_SCopy: {            /* in1, out2 */
         1073  +  pIn1 = &aMem[pOp->p1];
         1074  +  pOut = &aMem[pOp->p2];
  1106   1075     assert( pOut!=pIn1 );
  1107   1076     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1108   1077     REGISTER_TRACE(pOp->p2, pOut);
  1109   1078     break;
  1110   1079   }
  1111   1080   
  1112   1081   /* Opcode: ResultRow P1 P2 * * *
................................................................................
  1157   1126     /* Invalidate all ephemeral cursor row caches */
  1158   1127     p->cacheCtr = (p->cacheCtr + 2)|1;
  1159   1128   
  1160   1129     /* Make sure the results of the current row are \000 terminated
  1161   1130     ** and have an assigned type.  The results are de-ephemeralized as
  1162   1131     ** as side effect.
  1163   1132     */
  1164         -  pMem = p->pResultSet = &p->aMem[pOp->p1];
         1133  +  pMem = p->pResultSet = &aMem[pOp->p1];
  1165   1134     for(i=0; i<pOp->p2; i++){
  1166   1135       sqlite3VdbeMemNulTerminate(&pMem[i]);
  1167   1136       sqlite3VdbeMemStoreType(&pMem[i]);
  1168   1137       REGISTER_TRACE(pOp->p1+i, &pMem[i]);
  1169   1138     }
  1170   1139     if( db->mallocFailed ) goto no_mem;
  1171   1140   
................................................................................
  1187   1156   ** It is illegal for P1 and P3 to be the same register. Sometimes,
  1188   1157   ** if P3 is the same register as P2, the implementation is able
  1189   1158   ** to avoid a memcpy().
  1190   1159   */
  1191   1160   case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
  1192   1161     i64 nByte;
  1193   1162   
         1163  +  pIn1 = &aMem[pOp->p1];
         1164  +  pIn2 = &aMem[pOp->p2];
         1165  +  pOut = &aMem[pOp->p3];
  1194   1166     assert( pIn1!=pOut );
  1195   1167     if( (pIn1->flags | pIn2->flags) & MEM_Null ){
  1196   1168       sqlite3VdbeMemSetNull(pOut);
  1197   1169       break;
  1198   1170     }
  1199   1171     if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
  1200   1172     Stringify(pIn1, encoding);
................................................................................
  1260   1232   case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
  1261   1233     int flags;      /* Combined MEM_* flags from both inputs */
  1262   1234     i64 iA;         /* Integer value of left operand */
  1263   1235     i64 iB;         /* Integer value of right operand */
  1264   1236     double rA;      /* Real value of left operand */
  1265   1237     double rB;      /* Real value of right operand */
  1266   1238   
         1239  +  pIn1 = &aMem[pOp->p1];
  1267   1240     applyNumericAffinity(pIn1);
         1241  +  pIn2 = &aMem[pOp->p2];
  1268   1242     applyNumericAffinity(pIn2);
         1243  +  pOut = &aMem[pOp->p3];
  1269   1244     flags = pIn1->flags | pIn2->flags;
  1270   1245     if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
  1271   1246     if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
  1272   1247       iA = pIn1->u.i;
  1273   1248       iB = pIn2->u.i;
  1274   1249       switch( pOp->opcode ){
  1275   1250         case OP_Add:         iB += iA;       break;
................................................................................
  1376   1351   
  1377   1352     n = pOp->p5;
  1378   1353     apVal = p->apArg;
  1379   1354     assert( apVal || n==0 );
  1380   1355   
  1381   1356     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) );
  1382   1357     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  1383         -  pArg = &p->aMem[pOp->p2];
         1358  +  pArg = &aMem[pOp->p2];
  1384   1359     for(i=0; i<n; i++, pArg++){
  1385   1360       apVal[i] = pArg;
  1386   1361       sqlite3VdbeMemStoreType(pArg);
  1387   1362       REGISTER_TRACE(pOp->p2, pArg);
  1388   1363     }
  1389   1364   
  1390   1365     assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
................................................................................
  1393   1368       ctx.pVdbeFunc = 0;
  1394   1369     }else{
  1395   1370       ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
  1396   1371       ctx.pFunc = ctx.pVdbeFunc->pFunc;
  1397   1372     }
  1398   1373   
  1399   1374     assert( pOp->p3>0 && pOp->p3<=p->nMem );
  1400         -  pOut = &p->aMem[pOp->p3];
         1375  +  pOut = &aMem[pOp->p3];
  1401   1376     ctx.s.flags = MEM_Null;
  1402   1377     ctx.s.db = db;
  1403   1378     ctx.s.xDel = 0;
  1404   1379     ctx.s.zMalloc = 0;
  1405   1380   
  1406   1381     /* The output cell may already have a buffer allocated. Move
  1407   1382     ** the pointer to ctx.s so in case the user-function can use
................................................................................
  1408   1383     ** the already allocated buffer instead of allocating a new one.
  1409   1384     */
  1410   1385     sqlite3VdbeMemMove(&ctx.s, pOut);
  1411   1386     MemSetTypeFlag(&ctx.s, MEM_Null);
  1412   1387   
  1413   1388     ctx.isError = 0;
  1414   1389     if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
  1415         -    assert( pOp>p->aOp );
         1390  +    assert( pOp>aOp );
  1416   1391       assert( pOp[-1].p4type==P4_COLLSEQ );
  1417   1392       assert( pOp[-1].opcode==OP_CollSeq );
  1418   1393       ctx.pColl = pOp[-1].p4.pColl;
  1419   1394     }
  1420   1395     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  1421   1396     (*ctx.pFunc->xFunc)(&ctx, n, apVal);
  1422   1397     if( sqlite3SafetyOn(db) ){
................................................................................
  1492   1467   case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
  1493   1468   case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
  1494   1469   case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
  1495   1470   case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
  1496   1471     i64 a;
  1497   1472     i64 b;
  1498   1473   
         1474  +  pIn1 = &aMem[pOp->p1];
         1475  +  pIn2 = &aMem[pOp->p2];
         1476  +  pOut = &aMem[pOp->p3];
  1499   1477     if( (pIn1->flags | pIn2->flags) & MEM_Null ){
  1500   1478       sqlite3VdbeMemSetNull(pOut);
  1501   1479       break;
  1502   1480     }
  1503   1481     a = sqlite3VdbeIntValue(pIn2);
  1504   1482     b = sqlite3VdbeIntValue(pIn1);
  1505   1483     switch( pOp->opcode ){
................................................................................
  1518   1496   ** 
  1519   1497   ** Add the constant P2 to the value in register P1.
  1520   1498   ** The result is always an integer.
  1521   1499   **
  1522   1500   ** To force any register to be an integer, just add 0.
  1523   1501   */
  1524   1502   case OP_AddImm: {            /* in1 */
         1503  +  pIn1 = &aMem[pOp->p1];
  1525   1504     sqlite3VdbeMemIntegerify(pIn1);
  1526   1505     pIn1->u.i += pOp->p2;
  1527   1506     break;
  1528   1507   }
  1529   1508   
  1530   1509   /* Opcode: MustBeInt P1 P2 * * *
  1531   1510   ** 
  1532   1511   ** Force the value in register P1 to be an integer.  If the value
  1533   1512   ** in P1 is not an integer and cannot be converted into an integer
  1534   1513   ** without data loss, then jump immediately to P2, or if P2==0
  1535   1514   ** raise an SQLITE_MISMATCH exception.
  1536   1515   */
  1537   1516   case OP_MustBeInt: {            /* jump, in1 */
         1517  +  pIn1 = &aMem[pOp->p1];
  1538   1518     applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
  1539   1519     if( (pIn1->flags & MEM_Int)==0 ){
  1540   1520       if( pOp->p2==0 ){
  1541   1521         rc = SQLITE_MISMATCH;
  1542   1522         goto abort_due_to_error;
  1543   1523       }else{
  1544   1524         pc = pOp->p2 - 1;
................................................................................
  1555   1535   **
  1556   1536   ** This opcode is used when extracting information from a column that
  1557   1537   ** has REAL affinity.  Such column values may still be stored as
  1558   1538   ** integers, for space efficiency, but after extraction we want them
  1559   1539   ** to have only a real value.
  1560   1540   */
  1561   1541   case OP_RealAffinity: {                  /* in1 */
         1542  +  pIn1 = &aMem[pOp->p1];
  1562   1543     if( pIn1->flags & MEM_Int ){
  1563   1544       sqlite3VdbeMemRealify(pIn1);
  1564   1545     }
  1565   1546     break;
  1566   1547   }
  1567   1548   
  1568   1549   #ifndef SQLITE_OMIT_CAST
................................................................................
  1572   1553   ** If the value is numeric, convert it to a string using the
  1573   1554   ** equivalent of printf().  Blob values are unchanged and
  1574   1555   ** are afterwards simply interpreted as text.
  1575   1556   **
  1576   1557   ** A NULL value is not changed by this routine.  It remains NULL.
  1577   1558   */
  1578   1559   case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
         1560  +  pIn1 = &aMem[pOp->p1];
  1579   1561     if( pIn1->flags & MEM_Null ) break;
  1580   1562     assert( MEM_Str==(MEM_Blob>>3) );
  1581   1563     pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
  1582   1564     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
  1583   1565     rc = ExpandBlob(pIn1);
  1584   1566     assert( pIn1->flags & MEM_Str || db->mallocFailed );
  1585   1567     pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
................................................................................
  1593   1575   ** If the value is numeric, convert it to a string first.
  1594   1576   ** Strings are simply reinterpreted as blobs with no change
  1595   1577   ** to the underlying data.
  1596   1578   **
  1597   1579   ** A NULL value is not changed by this routine.  It remains NULL.
  1598   1580   */
  1599   1581   case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
         1582  +  pIn1 = &aMem[pOp->p1];
  1600   1583     if( pIn1->flags & MEM_Null ) break;
  1601   1584     if( (pIn1->flags & MEM_Blob)==0 ){
  1602   1585       applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
  1603   1586       assert( pIn1->flags & MEM_Str || db->mallocFailed );
  1604   1587       MemSetTypeFlag(pIn1, MEM_Blob);
  1605   1588     }else{
  1606   1589       pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
................................................................................
  1616   1599   ** If the value is text or blob, try to convert it to an using the
  1617   1600   ** equivalent of atoi() or atof() and store 0 if no such conversion 
  1618   1601   ** is possible.
  1619   1602   **
  1620   1603   ** A NULL value is not changed by this routine.  It remains NULL.
  1621   1604   */
  1622   1605   case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
         1606  +  pIn1 = &aMem[pOp->p1];
  1623   1607     if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
  1624   1608       sqlite3VdbeMemNumerify(pIn1);
  1625   1609     }
  1626   1610     break;
  1627   1611   }
  1628   1612   #endif /* SQLITE_OMIT_CAST */
  1629   1613   
................................................................................
  1633   1617   ** The value is currently a real number, drop its fractional part.
  1634   1618   ** If the value is text or blob, try to convert it to an integer using the
  1635   1619   ** equivalent of atoi() and store 0 if no such conversion is possible.
  1636   1620   **
  1637   1621   ** A NULL value is not changed by this routine.  It remains NULL.
  1638   1622   */
  1639   1623   case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
         1624  +  pIn1 = &aMem[pOp->p1];
  1640   1625     if( (pIn1->flags & MEM_Null)==0 ){
  1641   1626       sqlite3VdbeMemIntegerify(pIn1);
  1642   1627     }
  1643   1628     break;
  1644   1629   }
  1645   1630   
  1646   1631   #ifndef SQLITE_OMIT_CAST
................................................................................
  1650   1635   ** If The value is currently an integer, convert it.
  1651   1636   ** If the value is text or blob, try to convert it to an integer using the
  1652   1637   ** equivalent of atoi() and store 0.0 if no such conversion is possible.
  1653   1638   **
  1654   1639   ** A NULL value is not changed by this routine.  It remains NULL.
  1655   1640   */
  1656   1641   case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
         1642  +  pIn1 = &aMem[pOp->p1];
  1657   1643     if( (pIn1->flags & MEM_Null)==0 ){
  1658   1644       sqlite3VdbeMemRealify(pIn1);
  1659   1645     }
  1660   1646     break;
  1661   1647   }
  1662   1648   #endif /* SQLITE_OMIT_CAST */
  1663   1649   
................................................................................
  1738   1724   case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
  1739   1725   case OP_Le:               /* same as TK_LE, jump, in1, in3 */
  1740   1726   case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
  1741   1727   case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
  1742   1728     int res;            /* Result of the comparison of pIn1 against pIn3 */
  1743   1729     char affinity;      /* Affinity to use for comparison */
  1744   1730   
         1731  +  pIn1 = &aMem[pOp->p1];
         1732  +  pIn3 = &aMem[pOp->p3];
  1745   1733     if( (pIn1->flags | pIn3->flags)&MEM_Null ){
  1746   1734       /* One or both operands are NULL */
  1747   1735       if( pOp->p5 & SQLITE_NULLEQ ){
  1748   1736         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
  1749   1737         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
  1750   1738         ** or not both operands are null.
  1751   1739         */
................................................................................
  1753   1741         res = (pIn1->flags & pIn3->flags & MEM_Null)==0;
  1754   1742       }else{
  1755   1743         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
  1756   1744         ** then the result is always NULL.
  1757   1745         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
  1758   1746         */
  1759   1747         if( pOp->p5 & SQLITE_STOREP2 ){
  1760         -        pOut = &p->aMem[pOp->p2];
         1748  +        pOut = &aMem[pOp->p2];
  1761   1749           MemSetTypeFlag(pOut, MEM_Null);
  1762   1750           REGISTER_TRACE(pOp->p2, pOut);
  1763   1751         }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
  1764   1752           pc = pOp->p2-1;
  1765   1753         }
  1766   1754         break;
  1767   1755       }
................................................................................
  1785   1773       case OP_Lt:    res = res<0;      break;
  1786   1774       case OP_Le:    res = res<=0;     break;
  1787   1775       case OP_Gt:    res = res>0;      break;
  1788   1776       default:       res = res>=0;     break;
  1789   1777     }
  1790   1778   
  1791   1779     if( pOp->p5 & SQLITE_STOREP2 ){
  1792         -    pOut = &p->aMem[pOp->p2];
         1780  +    pOut = &aMem[pOp->p2];
  1793   1781       MemSetTypeFlag(pOut, MEM_Int);
  1794   1782       pOut->u.i = res;
  1795   1783       REGISTER_TRACE(pOp->p2, pOut);
  1796   1784     }else if( res ){
  1797   1785       pc = pOp->p2-1;
  1798   1786     }
  1799   1787     break;
................................................................................
  1854   1842     }else{
  1855   1843       assert( p1>0 && p1+n<=p->nMem+1 );
  1856   1844       assert( p2>0 && p2+n<=p->nMem+1 );
  1857   1845     }
  1858   1846   #endif /* SQLITE_DEBUG */
  1859   1847     for(i=0; i<n; i++){
  1860   1848       idx = aPermute ? aPermute[i] : i;
  1861         -    REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]);
  1862         -    REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]);
         1849  +    REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
         1850  +    REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
  1863   1851       assert( i<pKeyInfo->nField );
  1864   1852       pColl = pKeyInfo->aColl[i];
  1865   1853       bRev = pKeyInfo->aSortOrder[i];
  1866         -    iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl);
         1854  +    iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
  1867   1855       if( iCompare ){
  1868   1856         if( bRev ) iCompare = -iCompare;
  1869   1857         break;
  1870   1858       }
  1871   1859     }
  1872   1860     aPermute = 0;
  1873   1861     break;
................................................................................
  1909   1897   ** give a NULL output.
  1910   1898   */
  1911   1899   case OP_And:              /* same as TK_AND, in1, in2, out3 */
  1912   1900   case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
  1913   1901     int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1914   1902     int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
  1915   1903   
         1904  +  pIn1 = &aMem[pOp->p1];
  1916   1905     if( pIn1->flags & MEM_Null ){
  1917   1906       v1 = 2;
  1918   1907     }else{
  1919   1908       v1 = sqlite3VdbeIntValue(pIn1)!=0;
  1920   1909     }
         1910  +  pIn2 = &aMem[pOp->p2];
  1921   1911     if( pIn2->flags & MEM_Null ){
  1922   1912       v2 = 2;
  1923   1913     }else{
  1924   1914       v2 = sqlite3VdbeIntValue(pIn2)!=0;
  1925   1915     }
  1926   1916     if( pOp->opcode==OP_And ){
  1927   1917       static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
  1928   1918       v1 = and_logic[v1*3+v2];
  1929   1919     }else{
  1930   1920       static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
  1931   1921       v1 = or_logic[v1*3+v2];
  1932   1922     }
         1923  +  pOut = &aMem[pOp->p3];
  1933   1924     if( v1==2 ){
  1934   1925       MemSetTypeFlag(pOut, MEM_Null);
  1935   1926     }else{
  1936   1927       pOut->u.i = v1;
  1937   1928       MemSetTypeFlag(pOut, MEM_Int);
  1938   1929     }
  1939   1930     break;
................................................................................
  1941   1932   
  1942   1933   /* Opcode: Not P1 P2 * * *
  1943   1934   **
  1944   1935   ** Interpret the value in register P1 as a boolean value.  Store the
  1945   1936   ** boolean complement in register P2.  If the value in register P1 is 
  1946   1937   ** NULL, then a NULL is stored in P2.
  1947   1938   */
  1948         -case OP_Not: {                /* same as TK_NOT, in1 */
  1949         -  pOut = &p->aMem[pOp->p2];
         1939  +case OP_Not: {                /* same as TK_NOT, in1, out2 */
         1940  +  pIn1 = &aMem[pOp->p1];
         1941  +  pOut = &aMem[pOp->p2];
  1950   1942     if( pIn1->flags & MEM_Null ){
  1951   1943       sqlite3VdbeMemSetNull(pOut);
  1952   1944     }else{
  1953   1945       sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
  1954   1946     }
  1955   1947     break;
  1956   1948   }
................................................................................
  1957   1949   
  1958   1950   /* Opcode: BitNot P1 P2 * * *
  1959   1951   **
  1960   1952   ** Interpret the content of register P1 as an integer.  Store the
  1961   1953   ** ones-complement of the P1 value into register P2.  If P1 holds
  1962   1954   ** a NULL then store a NULL in P2.
  1963   1955   */
  1964         -case OP_BitNot: {             /* same as TK_BITNOT, in1 */
  1965         -  pOut = &p->aMem[pOp->p2];
         1956  +case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
         1957  +  pIn1 = &aMem[pOp->p1];
         1958  +  pOut = &aMem[pOp->p2];
  1966   1959     if( pIn1->flags & MEM_Null ){
  1967   1960       sqlite3VdbeMemSetNull(pOut);
  1968   1961     }else{
  1969   1962       sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
  1970   1963     }
  1971   1964     break;
  1972   1965   }
................................................................................
  1982   1975   ** Jump to P2 if the value in register P1 is False.  The value is
  1983   1976   ** is considered true if it has a numeric value of zero.  If the value
  1984   1977   ** in P1 is NULL then take the jump if P3 is true.
  1985   1978   */
  1986   1979   case OP_If:                 /* jump, in1 */
  1987   1980   case OP_IfNot: {            /* jump, in1 */
  1988   1981     int c;
         1982  +  pIn1 = &aMem[pOp->p1];
  1989   1983     if( pIn1->flags & MEM_Null ){
  1990   1984       c = pOp->p3;
  1991   1985     }else{
  1992   1986   #ifdef SQLITE_OMIT_FLOATING_POINT
  1993   1987       c = sqlite3VdbeIntValue(pIn1)!=0;
  1994   1988   #else
  1995   1989       c = sqlite3VdbeRealValue(pIn1)!=0.0;
................................................................................
  2003   1997   }
  2004   1998   
  2005   1999   /* Opcode: IsNull P1 P2 * * *
  2006   2000   **
  2007   2001   ** Jump to P2 if the value in register P1 is NULL.
  2008   2002   */
  2009   2003   case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
         2004  +  pIn1 = &aMem[pOp->p1];
  2010   2005     if( (pIn1->flags & MEM_Null)!=0 ){
  2011   2006       pc = pOp->p2 - 1;
  2012   2007     }
  2013   2008     break;
  2014   2009   }
  2015   2010   
  2016   2011   /* Opcode: NotNull P1 P2 * * *
  2017   2012   **
  2018   2013   ** Jump to P2 if the value in register P1 is not NULL.  
  2019   2014   */
  2020   2015   case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
         2016  +  pIn1 = &aMem[pOp->p1];
  2021   2017     if( (pIn1->flags & MEM_Null)==0 ){
  2022   2018       pc = pOp->p2 - 1;
  2023   2019     }
  2024   2020     break;
  2025   2021   }
  2026   2022   
  2027   2023   /* Opcode: Column P1 P2 P3 P4 P5
................................................................................
  2070   2066   
  2071   2067     p1 = pOp->p1;
  2072   2068     p2 = pOp->p2;
  2073   2069     pC = 0;
  2074   2070     memset(&sMem, 0, sizeof(sMem));
  2075   2071     assert( p1<p->nCursor );
  2076   2072     assert( pOp->p3>0 && pOp->p3<=p->nMem );
  2077         -  pDest = &p->aMem[pOp->p3];
         2073  +  pDest = &aMem[pOp->p3];
  2078   2074     MemSetTypeFlag(pDest, MEM_Null);
  2079   2075     zRec = 0;
  2080   2076   
  2081   2077     /* This block sets the variable payloadSize to be the total number of
  2082   2078     ** bytes in the record.
  2083   2079     **
  2084   2080     ** zRec is set to be the complete text of the record if it is available.
................................................................................
  2116   2112         payloadSize = (u32)payloadSize64;
  2117   2113       }else{
  2118   2114         assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2119   2115         rc = sqlite3BtreeDataSize(pCrsr, &payloadSize);
  2120   2116         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  2121   2117       }
  2122   2118     }else if( pC->pseudoTableReg>0 ){
  2123         -    pReg = &p->aMem[pC->pseudoTableReg];
         2119  +    pReg = &aMem[pC->pseudoTableReg];
  2124   2120       assert( pReg->flags & MEM_Blob );
  2125   2121       payloadSize = pReg->n;
  2126   2122       zRec = pReg->z;
  2127   2123       pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  2128   2124       assert( payloadSize==0 || zRec!=0 );
  2129   2125     }else{
  2130   2126       /* Consider the row to be NULL */
................................................................................
  2327   2323   ** Apply affinities to a range of P2 registers starting with P1.
  2328   2324   **
  2329   2325   ** P4 is a string that is P2 characters long. The nth character of the
  2330   2326   ** string indicates the column affinity that should be used for the nth
  2331   2327   ** memory cell in the range.
  2332   2328   */
  2333   2329   case OP_Affinity: {
  2334         -  char *zAffinity;   /* The affinity to be applied */
  2335         -  Mem *pData0;       /* First register to which to apply affinity */
  2336         -  Mem *pLast;        /* Last register to which to apply affinity */
  2337         -  Mem *pRec;         /* Current register */
         2330  +  const char *zAffinity;   /* The affinity to be applied */
         2331  +  char cAff;               /* A single character of affinity */
  2338   2332   
  2339   2333     zAffinity = pOp->p4.z;
  2340         -  pData0 = &p->aMem[pOp->p1];
  2341         -  pLast = &pData0[pOp->p2-1];
  2342         -  for(pRec=pData0; pRec<=pLast; pRec++){
  2343         -    ExpandBlob(pRec);
  2344         -    applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
         2334  +  assert( zAffinity!=0 );
         2335  +  assert( zAffinity[pOp->p2]==0 );
         2336  +  pIn1 = &aMem[pOp->p1];
         2337  +  while( (cAff = *(zAffinity++))!=0 ){
         2338  +    assert( pIn1 <= &p->aMem[p->nMem] );
         2339  +    ExpandBlob(pIn1);
         2340  +    applyAffinity(pIn1, cAff, encoding);
         2341  +    pIn1++;
  2345   2342     }
  2346   2343     break;
  2347   2344   }
  2348   2345   
  2349   2346   /* Opcode: MakeRecord P1 P2 P3 P4 *
  2350   2347   **
  2351   2348   ** Convert P2 registers beginning with P1 into a single entry
................................................................................
  2399   2396     nData = 0;         /* Number of bytes of data space */
  2400   2397     nHdr = 0;          /* Number of bytes of header space */
  2401   2398     nByte = 0;         /* Data space required for this record */
  2402   2399     nZero = 0;         /* Number of zero bytes at the end of the record */
  2403   2400     nField = pOp->p1;
  2404   2401     zAffinity = pOp->p4.z;
  2405   2402     assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 );
  2406         -  pData0 = &p->aMem[nField];
         2403  +  pData0 = &aMem[nField];
  2407   2404     nField = pOp->p2;
  2408   2405     pLast = &pData0[nField-1];
  2409   2406     file_format = p->minWriteFileFormat;
  2410   2407   
  2411   2408     /* Loop through the elements that will make up the record to figure
  2412   2409     ** out how much space is required for the new record.
  2413   2410     */
................................................................................
  2443   2440   
  2444   2441     /* Make sure the output register has a buffer large enough to store 
  2445   2442     ** the new record. The output register (pOp->p3) is not allowed to
  2446   2443     ** be one of the input registers (because the following call to
  2447   2444     ** sqlite3VdbeMemGrow() could clobber the value before it is used).
  2448   2445     */
  2449   2446     assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
  2450         -  pOut = &p->aMem[pOp->p3];
         2447  +  pOut = &aMem[pOp->p3];
  2451   2448     if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
  2452   2449       goto no_mem;
  2453   2450     }
  2454   2451     zNewRecord = (u8 *)pOut->z;
  2455   2452   
  2456   2453     /* Write the record */
  2457   2454     i = putVarint32(zNewRecord, nHdr);
................................................................................
  2490   2487   
  2491   2488     pCrsr = p->apCsr[pOp->p1]->pCursor;
  2492   2489     if( pCrsr ){
  2493   2490       rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2494   2491     }else{
  2495   2492       nEntry = 0;
  2496   2493     }
  2497         -  pOut->flags = MEM_Int;
  2498   2494     pOut->u.i = nEntry;
  2499   2495     break;
  2500   2496   }
  2501   2497   #endif
  2502   2498   
  2503   2499   /* Opcode: Savepoint P1 * * P4 *
  2504   2500   **
................................................................................
  2811   2807     assert( pOp->p3<SQLITE_N_BTREE_META );
  2812   2808     assert( iDb>=0 && iDb<db->nDb );
  2813   2809     assert( db->aDb[iDb].pBt!=0 );
  2814   2810     assert( (p->btreeMask & (1<<iDb))!=0 );
  2815   2811   
  2816   2812     sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
  2817   2813     pOut->u.i = iMeta;
  2818         -  MemSetTypeFlag(pOut, MEM_Int);
  2819   2814     break;
  2820   2815   }
  2821   2816   
  2822   2817   /* Opcode: SetCookie P1 P2 P3 * *
  2823   2818   **
  2824   2819   ** Write the content of register P3 (interpreted as an integer)
  2825   2820   ** into cookie number P2 of database P1.  P2==1 is the schema version.  
................................................................................
  2832   2827   case OP_SetCookie: {       /* in3 */
  2833   2828     Db *pDb;
  2834   2829     assert( pOp->p2<SQLITE_N_BTREE_META );
  2835   2830     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  2836   2831     assert( (p->btreeMask & (1<<pOp->p1))!=0 );
  2837   2832     pDb = &db->aDb[pOp->p1];
  2838   2833     assert( pDb->pBt!=0 );
         2834  +  pIn3 = &aMem[pOp->p3];
  2839   2835     sqlite3VdbeMemIntegerify(pIn3);
  2840   2836     /* See note about index shifting on OP_ReadCookie */
  2841   2837     rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
  2842   2838     if( pOp->p2==BTREE_SCHEMA_VERSION ){
  2843   2839       /* When the schema cookie changes, record the new cookie internally */
  2844   2840       pDb->pSchema->schema_cookie = (int)pIn3->u.i;
  2845   2841       db->flags |= SQLITE_InternChanges;
................................................................................
  2990   2986       }
  2991   2987     }else{
  2992   2988       wrFlag = 0;
  2993   2989     }
  2994   2990     if( pOp->p5 ){
  2995   2991       assert( p2>0 );
  2996   2992       assert( p2<=p->nMem );
  2997         -    pIn2 = &p->aMem[p2];
         2993  +    pIn2 = &aMem[p2];
  2998   2994       sqlite3VdbeMemIntegerify(pIn2);
  2999   2995       p2 = (int)pIn2->u.i;
  3000   2996       /* The p2 value always comes from a prior OP_CreateTable opcode and
  3001   2997       ** that opcode will always set the p2 value to 2 or more or else fail.
  3002   2998       ** If there were a failure, the prepared statement would have halted
  3003   2999       ** before reaching this instruction. */
  3004   3000       if( NEVER(p2<2) ) {
................................................................................
  3206   3202     i64 iKey;      /* The rowid we are to seek to */
  3207   3203   
  3208   3204     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3209   3205     assert( pOp->p2!=0 );
  3210   3206     pC = p->apCsr[pOp->p1];
  3211   3207     assert( pC!=0 );
  3212   3208     assert( pC->pseudoTableReg==0 );
         3209  +  assert( OP_SeekLe == OP_SeekLt+1 );
         3210  +  assert( OP_SeekGe == OP_SeekLt+2 );
         3211  +  assert( OP_SeekGt == OP_SeekLt+3 );
  3213   3212     if( pC->pCursor!=0 ){
  3214   3213       oc = pOp->opcode;
  3215   3214       pC->nullRow = 0;
  3216   3215       if( pC->isTable ){
  3217   3216         /* The input value in P3 might be of any type: integer, real, string,
  3218   3217         ** blob, or NULL.  But it needs to be an integer before we can do
  3219   3218         ** the seek, so covert it. */
         3219  +      pIn3 = &aMem[pOp->p3];
  3220   3220         applyNumericAffinity(pIn3);
  3221   3221         iKey = sqlite3VdbeIntValue(pIn3);
  3222   3222         pC->rowidIsValid = 0;
  3223   3223   
  3224   3224         /* If the P3 value could not be converted into an integer without
  3225   3225         ** loss of information, then special processing is required... */
  3226   3226         if( (pIn3->flags & MEM_Int)==0 ){
................................................................................
  3235   3235           assert( (pIn3->flags & MEM_Real)!=0 );
  3236   3236   
  3237   3237           if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
  3238   3238             /* The P3 value is too large in magnitude to be expressed as an
  3239   3239             ** integer. */
  3240   3240             res = 1;
  3241   3241             if( pIn3->r<0 ){
  3242         -            if( oc==OP_SeekGt || oc==OP_SeekGe ){
         3242  +            if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3243   3243                 rc = sqlite3BtreeFirst(pC->pCursor, &res);
  3244   3244                 if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3245   3245               }
  3246   3246             }else{
  3247         -            if( oc==OP_SeekLt || oc==OP_SeekLe ){
         3247  +            if( oc<=OP_SeekLe ){  assert( oc==OP_SeekLt || oc==OP_SeekLe );
  3248   3248                 rc = sqlite3BtreeLast(pC->pCursor, &res);
  3249   3249                 if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3250   3250               }
  3251   3251             }
  3252   3252             if( res ){
  3253   3253               pc = pOp->p2 - 1;
  3254   3254             }
................................................................................
  3272   3272         }
  3273   3273       }else{
  3274   3274         nField = pOp->p4.i;
  3275   3275         assert( pOp->p4type==P4_INT32 );
  3276   3276         assert( nField>0 );
  3277   3277         r.pKeyInfo = pC->pKeyInfo;
  3278   3278         r.nField = (u16)nField;
  3279         -      if( oc==OP_SeekGt || oc==OP_SeekLe ){
  3280         -        r.flags = UNPACKED_INCRKEY;
  3281         -      }else{
  3282         -        r.flags = 0;
  3283         -      }
  3284         -      r.aMem = &p->aMem[pOp->p3];
         3279  +
         3280  +      /* The next line of code computes as follows, only faster:
         3281  +      **   if( oc==OP_SeekGt || oc==OP_SeekLe ){
         3282  +      **     r.flags = UNPACKED_INCRKEY;
         3283  +      **   }else{
         3284  +      **     r.flags = 0;
         3285  +      **   }
         3286  +      */
         3287  +      r.flags = UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt));
         3288  +      assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
         3289  +      assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
         3290  +      assert( oc!=OP_SeekGe || r.flags==0 );
         3291  +      assert( oc!=OP_SeekLt || r.flags==0 );
         3292  +
         3293  +      r.aMem = &aMem[pOp->p3];
         3294  +      ExpandBlob(r.aMem);
  3285   3295         rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
  3286   3296         if( rc!=SQLITE_OK ){
  3287   3297           goto abort_due_to_error;
  3288   3298         }
  3289   3299         pC->rowidIsValid = 0;
  3290   3300       }
  3291   3301       pC->deferredMoveto = 0;
  3292   3302       pC->cacheStatus = CACHE_STALE;
  3293   3303   #ifdef SQLITE_TEST
  3294   3304       sqlite3_search_count++;
  3295   3305   #endif
  3296         -    if( oc==OP_SeekGe || oc==OP_SeekGt ){
         3306  +    if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3297   3307         if( res<0 || (res==0 && oc==OP_SeekGt) ){
  3298   3308           rc = sqlite3BtreeNext(pC->pCursor, &res);
  3299   3309           if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3300   3310           pC->rowidIsValid = 0;
  3301   3311         }else{
  3302   3312           res = 0;
  3303   3313         }
................................................................................
  3342   3352   
  3343   3353     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3344   3354     pC = p->apCsr[pOp->p1];
  3345   3355     assert( pC!=0 );
  3346   3356     if( ALWAYS(pC->pCursor!=0) ){
  3347   3357       assert( pC->isTable );
  3348   3358       pC->nullRow = 0;
         3359  +    pIn2 = &aMem[pOp->p2];
  3349   3360       pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
  3350   3361       pC->rowidIsValid = 0;
  3351   3362       pC->deferredMoveto = 1;
  3352   3363     }
  3353   3364     break;
  3354   3365   }
  3355   3366     
................................................................................
  3392   3403   #endif
  3393   3404   
  3394   3405     alreadyExists = 0;
  3395   3406     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3396   3407     assert( pOp->p4type==P4_INT32 );
  3397   3408     pC = p->apCsr[pOp->p1];
  3398   3409     assert( pC!=0 );
         3410  +  pIn3 = &aMem[pOp->p3];
  3399   3411     if( ALWAYS(pC->pCursor!=0) ){
  3400   3412   
  3401   3413       assert( pC->isTable==0 );
  3402   3414       if( pOp->p4.i>0 ){
  3403   3415         r.pKeyInfo = pC->pKeyInfo;
  3404   3416         r.nField = pOp->p4.i;
  3405   3417         r.aMem = pIn3;
................................................................................
  3461   3473   ** See also: NotFound, NotExists, Found
  3462   3474   */
  3463   3475   case OP_IsUnique: {        /* jump, in3 */
  3464   3476     u16 ii;
  3465   3477     VdbeCursor *pCx;
  3466   3478     BtCursor *pCrsr;
  3467   3479     u16 nField;
  3468         -  Mem *aMem;
         3480  +  Mem *aMx;
  3469   3481     UnpackedRecord r;                  /* B-Tree index search key */
  3470   3482     i64 R;                             /* Rowid stored in register P3 */
  3471   3483   
  3472         -  aMem = &p->aMem[pOp->p4.i];
         3484  +  pIn3 = &aMem[pOp->p3];
         3485  +  aMx = &aMem[pOp->p4.i];
  3473   3486     /* Assert that the values of parameters P1 and P4 are in range. */
  3474   3487     assert( pOp->p4type==P4_INT32 );
  3475   3488     assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
  3476   3489     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3477   3490   
  3478   3491     /* Find the index cursor. */
  3479   3492     pCx = p->apCsr[pOp->p1];
................................................................................
  3481   3494     pCx->seekResult = 0;
  3482   3495     pCx->cacheStatus = CACHE_STALE;
  3483   3496     pCrsr = pCx->pCursor;
  3484   3497   
  3485   3498     /* If any of the values are NULL, take the jump. */
  3486   3499     nField = pCx->pKeyInfo->nField;
  3487   3500     for(ii=0; ii<nField; ii++){
  3488         -    if( aMem[ii].flags & MEM_Null ){
         3501  +    if( aMx[ii].flags & MEM_Null ){
  3489   3502         pc = pOp->p2 - 1;
  3490   3503         pCrsr = 0;
  3491   3504         break;
  3492   3505       }
  3493   3506     }
  3494         -  assert( (aMem[nField].flags & MEM_Null)==0 );
         3507  +  assert( (aMx[nField].flags & MEM_Null)==0 );
  3495   3508   
  3496   3509     if( pCrsr!=0 ){
  3497   3510       /* Populate the index search key. */
  3498   3511       r.pKeyInfo = pCx->pKeyInfo;
  3499   3512       r.nField = nField + 1;
  3500   3513       r.flags = UNPACKED_PREFIX_SEARCH;
  3501         -    r.aMem = aMem;
         3514  +    r.aMem = aMx;
  3502   3515   
  3503   3516       /* Extract the value of R from register P3. */
  3504   3517       sqlite3VdbeMemIntegerify(pIn3);
  3505   3518       R = pIn3->u.i;
  3506   3519   
  3507   3520       /* Search the B-Tree index. If no conflicting record is found, jump
  3508   3521       ** to P2. Otherwise, copy the rowid of the conflicting record to
................................................................................
  3533   3546   */
  3534   3547   case OP_NotExists: {        /* jump, in3 */
  3535   3548     VdbeCursor *pC;
  3536   3549     BtCursor *pCrsr;
  3537   3550     int res;
  3538   3551     u64 iKey;
  3539   3552   
         3553  +  pIn3 = &aMem[pOp->p3];
  3540   3554     assert( pIn3->flags & MEM_Int );
  3541   3555     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3542   3556     pC = p->apCsr[pOp->p1];
  3543   3557     assert( pC!=0 );
  3544   3558     assert( pC->isTable );
  3545   3559     assert( pC->pseudoTableReg==0 );
  3546   3560     pCrsr = pC->pCursor;
................................................................................
  3576   3590   ** The sequence number on the cursor is incremented after this
  3577   3591   ** instruction.  
  3578   3592   */
  3579   3593   case OP_Sequence: {           /* out2-prerelease */
  3580   3594     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3581   3595     assert( p->apCsr[pOp->p1]!=0 );
  3582   3596     pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
  3583         -  MemSetTypeFlag(pOut, MEM_Int);
  3584   3597     break;
  3585   3598   }
  3586   3599   
  3587   3600   
  3588   3601   /* Opcode: NewRowid P1 P2 P3 * *
  3589   3602   **
  3590   3603   ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
................................................................................
  3670   3683             for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
  3671   3684             /* Assert that P3 is a valid memory cell. */
  3672   3685             assert( pOp->p3<=pFrame->nMem );
  3673   3686             pMem = &pFrame->aMem[pOp->p3];
  3674   3687           }else{
  3675   3688             /* Assert that P3 is a valid memory cell. */
  3676   3689             assert( pOp->p3<=p->nMem );
  3677         -          pMem = &p->aMem[pOp->p3];
         3690  +          pMem = &aMem[pOp->p3];
  3678   3691           }
  3679   3692   
  3680   3693           REGISTER_TRACE(pOp->p3, pMem);
  3681   3694           sqlite3VdbeMemIntegerify(pMem);
  3682   3695           assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
  3683   3696           if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
  3684   3697             rc = SQLITE_FULL;
................................................................................
  3713   3726           goto abort_due_to_error;
  3714   3727         }
  3715   3728       }
  3716   3729       pC->rowidIsValid = 0;
  3717   3730       pC->deferredMoveto = 0;
  3718   3731       pC->cacheStatus = CACHE_STALE;
  3719   3732     }
  3720         -  MemSetTypeFlag(pOut, MEM_Int);
  3721   3733     pOut->u.i = v;
  3722   3734     break;
  3723   3735   }
  3724   3736   
  3725   3737   /* Opcode: Insert P1 P2 P3 P4 P5
  3726   3738   **
  3727   3739   ** Write an entry into the table of cursor P1.  A new entry is
................................................................................
  3774   3786     VdbeCursor *pC;   /* Cursor to table into which insert is written */
  3775   3787     int nZero;        /* Number of zero-bytes to append */
  3776   3788     int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
  3777   3789     const char *zDb;  /* database name - used by the update hook */
  3778   3790     const char *zTbl; /* Table name - used by the opdate hook */
  3779   3791     int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
  3780   3792   
  3781         -  pData = &p->aMem[pOp->p2];
         3793  +  pData = &aMem[pOp->p2];
  3782   3794     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3783   3795     pC = p->apCsr[pOp->p1];
  3784   3796     assert( pC!=0 );
  3785   3797     assert( pC->pCursor!=0 );
  3786   3798     assert( pC->pseudoTableReg==0 );
  3787   3799     assert( pC->isTable );
  3788   3800     REGISTER_TRACE(pOp->p2, pData);
  3789   3801   
  3790   3802     if( pOp->opcode==OP_Insert ){
  3791         -    pKey = &p->aMem[pOp->p3];
         3803  +    pKey = &aMem[pOp->p3];
  3792   3804       assert( pKey->flags & MEM_Int );
  3793   3805       REGISTER_TRACE(pOp->p3, pKey);
  3794   3806       iKey = pKey->u.i;
  3795   3807     }else{
  3796   3808       assert( pOp->opcode==OP_InsertInt );
  3797   3809       iKey = pOp->p3;
  3798   3810     }
................................................................................
  3932   3944   case OP_RowKey:
  3933   3945   case OP_RowData: {
  3934   3946     VdbeCursor *pC;
  3935   3947     BtCursor *pCrsr;
  3936   3948     u32 n;
  3937   3949     i64 n64;
  3938   3950   
  3939         -  pOut = &p->aMem[pOp->p2];
         3951  +  pOut = &aMem[pOp->p2];
  3940   3952   
  3941   3953     /* Note that RowKey and RowData are really exactly the same instruction */
  3942   3954     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3943   3955     pC = p->apCsr[pOp->p1];
  3944   3956     assert( pC->isTable || pOp->opcode==OP_RowKey );
  3945   3957     assert( pC->isIndex || pOp->opcode==OP_RowData );
  3946   3958     assert( pC!=0 );
................................................................................
  4005   4017     const sqlite3_module *pModule;
  4006   4018   
  4007   4019     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4008   4020     pC = p->apCsr[pOp->p1];
  4009   4021     assert( pC!=0 );
  4010   4022     assert( pC->pseudoTableReg==0 );
  4011   4023     if( pC->nullRow ){
  4012         -    /* Do nothing so that reg[P2] remains NULL */
         4024  +    pOut->flags = MEM_Null;
  4013   4025       break;
  4014   4026     }else if( pC->deferredMoveto ){
  4015   4027       v = pC->movetoTarget;
  4016   4028   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4017   4029     }else if( pC->pVtabCursor ){
  4018   4030       pVtab = pC->pVtabCursor->pVtab;
  4019   4031       pModule = pVtab->pModule;
................................................................................
  4033   4045         v = pC->lastRowid;
  4034   4046       }else{
  4035   4047         rc = sqlite3BtreeKeySize(pC->pCursor, &v);
  4036   4048         assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
  4037   4049       }
  4038   4050     }
  4039   4051     pOut->u.i = v;
  4040         -  MemSetTypeFlag(pOut, MEM_Int);
  4041   4052     break;
  4042   4053   }
  4043   4054   
  4044   4055   /* Opcode: NullRow P1 * * * *
  4045   4056   **
  4046   4057   ** Move the cursor P1 to a null row.  Any OP_Column operations
  4047   4058   ** that occur while the cursor is on the null row will always
................................................................................
  4218   4229     BtCursor *pCrsr;
  4219   4230     int nKey;
  4220   4231     const char *zKey;
  4221   4232   
  4222   4233     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4223   4234     pC = p->apCsr[pOp->p1];
  4224   4235     assert( pC!=0 );
         4236  +  pIn2 = &aMem[pOp->p2];
  4225   4237     assert( pIn2->flags & MEM_Blob );
  4226   4238     pCrsr = pC->pCursor;
  4227   4239     if( ALWAYS(pCrsr!=0) ){
  4228   4240       assert( pC->isTable==0 );
  4229   4241       rc = ExpandBlob(pIn2);
  4230   4242       if( rc==SQLITE_OK ){
  4231   4243         nKey = pIn2->n;
................................................................................
  4258   4270     pC = p->apCsr[pOp->p1];
  4259   4271     assert( pC!=0 );
  4260   4272     pCrsr = pC->pCursor;
  4261   4273     if( ALWAYS(pCrsr!=0) ){
  4262   4274       r.pKeyInfo = pC->pKeyInfo;
  4263   4275       r.nField = (u16)pOp->p3;
  4264   4276       r.flags = 0;
  4265         -    r.aMem = &p->aMem[pOp->p2];
         4277  +    r.aMem = &aMem[pOp->p2];
  4266   4278       rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
  4267   4279       if( rc==SQLITE_OK && res==0 ){
  4268   4280         rc = sqlite3BtreeDelete(pCrsr);
  4269   4281       }
  4270   4282       assert( pC->deferredMoveto==0 );
  4271   4283       pC->cacheStatus = CACHE_STALE;
  4272   4284     }
................................................................................
  4286   4298     VdbeCursor *pC;
  4287   4299     i64 rowid;
  4288   4300   
  4289   4301     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4290   4302     pC = p->apCsr[pOp->p1];
  4291   4303     assert( pC!=0 );
  4292   4304     pCrsr = pC->pCursor;
         4305  +  pOut->flags = MEM_Null;
  4293   4306     if( ALWAYS(pCrsr!=0) ){
  4294   4307       rc = sqlite3VdbeCursorMoveto(pC);
  4295   4308       if( NEVER(rc) ) goto abort_due_to_error;
  4296   4309       assert( pC->deferredMoveto==0 );
  4297   4310       assert( pC->isTable==0 );
  4298   4311       if( !pC->nullRow ){
  4299   4312         rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
  4300   4313         if( rc!=SQLITE_OK ){
  4301   4314           goto abort_due_to_error;
  4302   4315         }
  4303         -      MemSetTypeFlag(pOut, MEM_Int);
  4304   4316         pOut->u.i = rowid;
         4317  +      pOut->flags = MEM_Int;
  4305   4318       }
  4306   4319     }
  4307   4320     break;
  4308   4321   }
  4309   4322   
  4310   4323   /* Opcode: IdxGE P1 P2 P3 P4 P5
  4311   4324   **
................................................................................
  4329   4342   **
  4330   4343   ** If the P1 index entry is less than the key value then jump to P2.
  4331   4344   ** Otherwise fall through to the next instruction.
  4332   4345   **
  4333   4346   ** If P5 is non-zero then the key value is increased by an epsilon prior 
  4334   4347   ** to the comparison.  This makes the opcode work like IdxLE.
  4335   4348   */
  4336         -case OP_IdxLT:          /* jump, in3 */
  4337         -case OP_IdxGE: {        /* jump, in3 */
         4349  +case OP_IdxLT:          /* jump */
         4350  +case OP_IdxGE: {        /* jump */
  4338   4351     VdbeCursor *pC;
  4339   4352     int res;
  4340   4353     UnpackedRecord r;
  4341   4354   
  4342   4355     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4343   4356     pC = p->apCsr[pOp->p1];
  4344   4357     assert( pC!=0 );
................................................................................
  4349   4362       r.pKeyInfo = pC->pKeyInfo;
  4350   4363       r.nField = (u16)pOp->p4.i;
  4351   4364       if( pOp->p5 ){
  4352   4365         r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
  4353   4366       }else{
  4354   4367         r.flags = UNPACKED_IGNORE_ROWID;
  4355   4368       }
  4356         -    r.aMem = &p->aMem[pOp->p3];
         4369  +    r.aMem = &aMem[pOp->p3];
  4357   4370       rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
  4358   4371       if( pOp->opcode==OP_IdxLT ){
  4359   4372         res = -res;
  4360   4373       }else{
  4361   4374         assert( pOp->opcode==OP_IdxGE );
  4362   4375         res++;
  4363   4376       }
................................................................................
  4399   4412       if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
  4400   4413         iCnt++;
  4401   4414       }
  4402   4415     }
  4403   4416   #else
  4404   4417     iCnt = db->activeVdbeCnt;
  4405   4418   #endif
         4419  +  pOut->flags = MEM_Null;
  4406   4420     if( iCnt>1 ){
  4407   4421       rc = SQLITE_LOCKED;
  4408   4422       p->errorAction = OE_Abort;
  4409   4423     }else{
  4410   4424       iDb = pOp->p3;
  4411   4425       assert( iCnt==1 );
  4412   4426       assert( (p->btreeMask & (1<<iDb))!=0 );
  4413   4427       rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
  4414         -    MemSetTypeFlag(pOut, MEM_Int);
         4428  +    pOut->flags = MEM_Int;
  4415   4429       pOut->u.i = iMoved;
  4416   4430   #ifndef SQLITE_OMIT_AUTOVACUUM
  4417   4431       if( rc==SQLITE_OK && iMoved!=0 ){
  4418   4432         sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
  4419   4433       }
  4420   4434   #endif
  4421   4435     }
................................................................................
  4447   4461     assert( (p->btreeMask & (1<<pOp->p2))!=0 );
  4448   4462     rc = sqlite3BtreeClearTable(
  4449   4463         db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
  4450   4464     );
  4451   4465     if( pOp->p3 ){
  4452   4466       p->nChange += nChange;
  4453   4467       if( pOp->p3>0 ){
  4454         -      p->aMem[pOp->p3].u.i += nChange;
         4468  +      aMem[pOp->p3].u.i += nChange;
  4455   4469       }
  4456   4470     }
  4457   4471     break;
  4458   4472   }
  4459   4473   
  4460   4474   /* Opcode: CreateTable P1 P2 * * *
  4461   4475   **
................................................................................
  4494   4508       /* flags = BTREE_INTKEY; */
  4495   4509       flags = BTREE_LEAFDATA|BTREE_INTKEY;
  4496   4510     }else{
  4497   4511       flags = BTREE_ZERODATA;
  4498   4512     }
  4499   4513     rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
  4500   4514     pOut->u.i = pgno;
  4501         -  MemSetTypeFlag(pOut, MEM_Int);
  4502   4515     break;
  4503   4516   }
  4504   4517   
  4505   4518   /* Opcode: ParseSchema P1 P2 * P4 *
  4506   4519   **
  4507   4520   ** Read and parse all entries from the SQLITE_MASTER table of database P1
  4508   4521   ** that match the WHERE clause P4.  P2 is the "force" flag.   Always do
................................................................................
  4657   4670     Mem *pnErr;     /* Register keeping track of errors remaining */
  4658   4671     
  4659   4672     nRoot = pOp->p2;
  4660   4673     assert( nRoot>0 );
  4661   4674     aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
  4662   4675     if( aRoot==0 ) goto no_mem;
  4663   4676     assert( pOp->p3>0 && pOp->p3<=p->nMem );
  4664         -  pnErr = &p->aMem[pOp->p3];
         4677  +  pnErr = &aMem[pOp->p3];
  4665   4678     assert( (pnErr->flags & MEM_Int)!=0 );
  4666   4679     assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
  4667         -  pIn1 = &p->aMem[pOp->p1];
         4680  +  pIn1 = &aMem[pOp->p1];
  4668   4681     for(j=0; j<nRoot; j++){
  4669   4682       aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
  4670   4683     }
  4671   4684     aRoot[j] = 0;
  4672   4685     assert( pOp->p5<db->nDb );
  4673   4686     assert( (p->btreeMask & (1<<pOp->p5))!=0 );
  4674   4687     z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
................................................................................
  4692   4705   /* Opcode: RowSetAdd P1 P2 * * *
  4693   4706   **
  4694   4707   ** Insert the integer value held by register P2 into a boolean index
  4695   4708   ** held in register P1.
  4696   4709   **
  4697   4710   ** An assertion fails if P2 is not an integer.
  4698   4711   */
  4699         -case OP_RowSetAdd: {       /* in2 */
  4700         -  Mem *pIdx;
  4701         -  Mem *pVal;
  4702         -  assert( pOp->p1>0 && pOp->p1<=p->nMem );
  4703         -  pIdx = &p->aMem[pOp->p1];
  4704         -  assert( pOp->p2>0 && pOp->p2<=p->nMem );
  4705         -  pVal = &p->aMem[pOp->p2];
  4706         -  assert( (pVal->flags & MEM_Int)!=0 );
  4707         -  if( (pIdx->flags & MEM_RowSet)==0 ){
  4708         -    sqlite3VdbeMemSetRowSet(pIdx);
  4709         -    if( (pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
         4712  +case OP_RowSetAdd: {       /* in1, in2 */
         4713  +  pIn1 = &aMem[pOp->p1];
         4714  +  pIn2 = &aMem[pOp->p2];
         4715  +  assert( (pIn2->flags & MEM_Int)!=0 );
         4716  +  if( (pIn1->flags & MEM_RowSet)==0 ){
         4717  +    sqlite3VdbeMemSetRowSet(pIn1);
         4718  +    if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
  4710   4719     }
  4711         -  sqlite3RowSetInsert(pIdx->u.pRowSet, pVal->u.i);
         4720  +  sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
  4712   4721     break;
  4713   4722   }
  4714   4723   
  4715   4724   /* Opcode: RowSetRead P1 P2 P3 * *
  4716   4725   **
  4717   4726   ** Extract the smallest value from boolean index P1 and put that value into
  4718   4727   ** register P3.  Or, if boolean index P1 is initially empty, leave P3
  4719   4728   ** unchanged and jump to instruction P2.
  4720   4729   */
  4721         -case OP_RowSetRead: {       /* jump, out3 */
  4722         -  Mem *pIdx;
         4730  +case OP_RowSetRead: {       /* jump, in1, out3 */
  4723   4731     i64 val;
  4724         -  assert( pOp->p1>0 && pOp->p1<=p->nMem );
  4725   4732     CHECK_FOR_INTERRUPT;
  4726         -  pIdx = &p->aMem[pOp->p1];
  4727         -  pOut = &p->aMem[pOp->p3];
  4728         -  if( (pIdx->flags & MEM_RowSet)==0 
  4729         -   || sqlite3RowSetNext(pIdx->u.pRowSet, &val)==0
         4733  +  pIn1 = &aMem[pOp->p1];
         4734  +  if( (pIn1->flags & MEM_RowSet)==0 
         4735  +   || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
  4730   4736     ){
  4731   4737       /* The boolean index is empty */
  4732         -    sqlite3VdbeMemSetNull(pIdx);
         4738  +    sqlite3VdbeMemSetNull(pIn1);
  4733   4739       pc = pOp->p2 - 1;
  4734   4740     }else{
  4735   4741       /* A value was pulled from the index */
  4736         -    assert( pOp->p3>0 && pOp->p3<=p->nMem );
  4737         -    sqlite3VdbeMemSetInt64(pOut, val);
         4742  +    sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
  4738   4743     }
  4739   4744     break;
  4740   4745   }
  4741   4746   
  4742   4747   /* Opcode: RowSetTest P1 P2 P3 P4
  4743   4748   **
  4744   4749   ** Register P3 is assumed to hold a 64-bit integer value. If register P1
................................................................................
  4762   4767   ** previously inserted as part of set X (only if it was previously
  4763   4768   ** inserted as part of some other set).
  4764   4769   */
  4765   4770   case OP_RowSetTest: {                     /* jump, in1, in3 */
  4766   4771     int iSet;
  4767   4772     int exists;
  4768   4773   
         4774  +  pIn1 = &aMem[pOp->p1];
         4775  +  pIn3 = &aMem[pOp->p3];
  4769   4776     iSet = pOp->p4.i;
  4770   4777     assert( pIn3->flags&MEM_Int );
  4771   4778   
  4772   4779     /* If there is anything other than a rowset object in memory cell P1,
  4773   4780     ** delete it now and initialize P1 with an empty rowset
  4774   4781     */
  4775   4782     if( (pIn1->flags & MEM_RowSet)==0 ){
................................................................................
  4817   4824     Mem *pMem;              /* Used to iterate through memory cells */
  4818   4825     Mem *pEnd;              /* Last memory cell in new array */
  4819   4826     VdbeFrame *pFrame;      /* New vdbe frame to execute in */
  4820   4827     SubProgram *pProgram;   /* Sub-program to execute */
  4821   4828     void *t;                /* Token identifying trigger */
  4822   4829   
  4823   4830     pProgram = pOp->p4.pProgram;
  4824         -  pRt = &p->aMem[pOp->p3];
         4831  +  pRt = &aMem[pOp->p3];
  4825   4832     assert( pProgram->nOp>0 );
  4826   4833     
  4827   4834     /* If the p5 flag is clear, then recursive invocation of triggers is 
  4828   4835     ** disabled for backwards compatibility (p5 is set if this sub-program
  4829   4836     ** is really a trigger, not a foreign key action, and the flag set
  4830   4837     ** and cleared by the "PRAGMA recursive_triggers" command is clear).
  4831   4838     ** 
................................................................................
  4895   4902   
  4896   4903     p->nFrame++;
  4897   4904     pFrame->pParent = p->pFrame;
  4898   4905     pFrame->lastRowid = db->lastRowid;
  4899   4906     pFrame->nChange = p->nChange;
  4900   4907     p->nChange = 0;
  4901   4908     p->pFrame = pFrame;
  4902         -  p->aMem = &VdbeFrameMem(pFrame)[-1];
         4909  +  p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
  4903   4910     p->nMem = pFrame->nChildMem;
  4904   4911     p->nCursor = (u16)pFrame->nChildCsr;
  4905         -  p->apCsr = (VdbeCursor **)&p->aMem[p->nMem+1];
  4906         -  p->aOp = pProgram->aOp;
         4912  +  p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
         4913  +  p->aOp = aOp = pProgram->aOp;
  4907   4914     p->nOp = pProgram->nOp;
  4908   4915     pc = -1;
  4909   4916   
  4910   4917     break;
  4911   4918   }
  4912   4919   
  4913   4920   /* Opcode: Param P1 P2 * * *
................................................................................
  4985   4992   case OP_MemMax: {        /* in2 */
  4986   4993     Mem *pIn1;
  4987   4994     VdbeFrame *pFrame;
  4988   4995     if( p->pFrame ){
  4989   4996       for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
  4990   4997       pIn1 = &pFrame->aMem[pOp->p1];
  4991   4998     }else{
  4992         -    pIn1 = &p->aMem[pOp->p1];
         4999  +    pIn1 = &aMem[pOp->p1];
  4993   5000     }
  4994   5001     sqlite3VdbeMemIntegerify(pIn1);
         5002  +  pIn2 = &aMem[pOp->p2];
  4995   5003     sqlite3VdbeMemIntegerify(pIn2);
  4996   5004     if( pIn1->u.i<pIn2->u.i){
  4997   5005       pIn1->u.i = pIn2->u.i;
  4998   5006     }
  4999   5007     break;
  5000   5008   }
  5001   5009   #endif /* SQLITE_OMIT_AUTOINCREMENT */
................................................................................
  5004   5012   **
  5005   5013   ** If the value of register P1 is 1 or greater, jump to P2.
  5006   5014   **
  5007   5015   ** It is illegal to use this instruction on a register that does
  5008   5016   ** not contain an integer.  An assertion fault will result if you try.
  5009   5017   */
  5010   5018   case OP_IfPos: {        /* jump, in1 */
         5019  +  pIn1 = &aMem[pOp->p1];
  5011   5020     assert( pIn1->flags&MEM_Int );
  5012   5021     if( pIn1->u.i>0 ){
  5013   5022        pc = pOp->p2 - 1;
  5014   5023     }
  5015   5024     break;
  5016   5025   }
  5017   5026   
................................................................................
  5019   5028   **
  5020   5029   ** If the value of register P1 is less than zero, jump to P2. 
  5021   5030   **
  5022   5031   ** It is illegal to use this instruction on a register that does
  5023   5032   ** not contain an integer.  An assertion fault will result if you try.
  5024   5033   */
  5025   5034   case OP_IfNeg: {        /* jump, in1 */
         5035  +  pIn1 = &aMem[pOp->p1];
  5026   5036     assert( pIn1->flags&MEM_Int );
  5027   5037     if( pIn1->u.i<0 ){
  5028   5038        pc = pOp->p2 - 1;
  5029   5039     }
  5030   5040     break;
  5031   5041   }
  5032   5042   
................................................................................
  5035   5045   ** The register P1 must contain an integer.  Add literal P3 to the
  5036   5046   ** value in register P1.  If the result is exactly 0, jump to P2. 
  5037   5047   **
  5038   5048   ** It is illegal to use this instruction on a register that does
  5039   5049   ** not contain an integer.  An assertion fault will result if you try.
  5040   5050   */
  5041   5051   case OP_IfZero: {        /* jump, in1 */
         5052  +  pIn1 = &aMem[pOp->p1];
  5042   5053     assert( pIn1->flags&MEM_Int );
  5043   5054     pIn1->u.i += pOp->p3;
  5044   5055     if( pIn1->u.i==0 ){
  5045   5056        pc = pOp->p2 - 1;
  5046   5057     }
  5047   5058     break;
  5048   5059   }
................................................................................
  5063   5074     Mem *pMem;
  5064   5075     Mem *pRec;
  5065   5076     sqlite3_context ctx;
  5066   5077     sqlite3_value **apVal;
  5067   5078   
  5068   5079     n = pOp->p5;
  5069   5080     assert( n>=0 );
  5070         -  pRec = &p->aMem[pOp->p2];
         5081  +  pRec = &aMem[pOp->p2];
  5071   5082     apVal = p->apArg;
  5072   5083     assert( apVal || n==0 );
  5073   5084     for(i=0; i<n; i++, pRec++){
  5074   5085       apVal[i] = pRec;
  5075   5086       sqlite3VdbeMemStoreType(pRec);
  5076   5087     }
  5077   5088     ctx.pFunc = pOp->p4.pFunc;
  5078   5089     assert( pOp->p3>0 && pOp->p3<=p->nMem );
  5079         -  ctx.pMem = pMem = &p->aMem[pOp->p3];
         5090  +  ctx.pMem = pMem = &aMem[pOp->p3];
  5080   5091     pMem->n++;
  5081   5092     ctx.s.flags = MEM_Null;
  5082   5093     ctx.s.z = 0;
  5083   5094     ctx.s.zMalloc = 0;
  5084   5095     ctx.s.xDel = 0;
  5085   5096     ctx.s.db = db;
  5086   5097     ctx.isError = 0;
................................................................................
  5111   5122   ** functions that can take varying numbers of arguments.  The
  5112   5123   ** P4 argument is only needed for the degenerate case where
  5113   5124   ** the step function was not previously called.
  5114   5125   */
  5115   5126   case OP_AggFinal: {
  5116   5127     Mem *pMem;
  5117   5128     assert( pOp->p1>0 && pOp->p1<=p->nMem );
  5118         -  pMem = &p->aMem[pOp->p1];
         5129  +  pMem = &aMem[pOp->p1];
  5119   5130     assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  5120   5131     rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
  5121   5132     if( rc ){
  5122   5133       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
  5123   5134     }
  5124   5135     sqlite3VdbeChangeEncoding(pMem, encoding);
  5125   5136     UPDATE_MAX_BLOBSIZE(pMem);
................................................................................
  5336   5347     sqlite3_vtab_cursor *pVtabCursor;
  5337   5348     sqlite3_vtab *pVtab;
  5338   5349     VdbeCursor *pCur;
  5339   5350     int res;
  5340   5351     int i;
  5341   5352     Mem **apArg;
  5342   5353   
  5343         -  pQuery = &p->aMem[pOp->p3];
         5354  +  pQuery = &aMem[pOp->p3];
  5344   5355     pArgc = &pQuery[1];
  5345   5356     pCur = p->apCsr[pOp->p1];
  5346   5357     REGISTER_TRACE(pOp->p3, pQuery);
  5347   5358     assert( pCur->pVtabCursor );
  5348   5359     pVtabCursor = pCur->pVtabCursor;
  5349   5360     pVtab = pVtabCursor->pVtab;
  5350   5361     pModule = pVtab->pModule;
................................................................................
  5397   5408     const sqlite3_module *pModule;
  5398   5409     Mem *pDest;
  5399   5410     sqlite3_context sContext;
  5400   5411   
  5401   5412     VdbeCursor *pCur = p->apCsr[pOp->p1];
  5402   5413     assert( pCur->pVtabCursor );
  5403   5414     assert( pOp->p3>0 && pOp->p3<=p->nMem );
  5404         -  pDest = &p->aMem[pOp->p3];
         5415  +  pDest = &aMem[pOp->p3];
  5405   5416     if( pCur->nullRow ){
  5406   5417       sqlite3VdbeMemSetNull(pDest);
  5407   5418       break;
  5408   5419     }
  5409   5420     pVtab = pCur->pVtabCursor->pVtab;
  5410   5421     pModule = pVtab->pModule;
  5411   5422     assert( pModule->xColumn );
................................................................................
  5504   5515   ** in register P1 is passed as the zName argument to the xRename method.
  5505   5516   */
  5506   5517   case OP_VRename: {
  5507   5518     sqlite3_vtab *pVtab;
  5508   5519     Mem *pName;
  5509   5520   
  5510   5521     pVtab = pOp->p4.pVtab->pVtab;
  5511         -  pName = &p->aMem[pOp->p1];
         5522  +  pName = &aMem[pOp->p1];
  5512   5523     assert( pVtab->pModule->xRename );
  5513   5524     REGISTER_TRACE(pOp->p1, pName);
  5514   5525     assert( pName->flags & MEM_Str );
  5515   5526     if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5516   5527     rc = pVtab->pModule->xRename(pVtab, pName->z);
  5517   5528     sqlite3DbFree(db, p->zErrMsg);
  5518   5529     p->zErrMsg = pVtab->zErrMsg;
................................................................................
  5558   5569   
  5559   5570     pVtab = pOp->p4.pVtab->pVtab;
  5560   5571     pModule = (sqlite3_module *)pVtab->pModule;
  5561   5572     nArg = pOp->p2;
  5562   5573     assert( pOp->p4type==P4_VTAB );
  5563   5574     if( ALWAYS(pModule->xUpdate) ){
  5564   5575       apArg = p->apArg;
  5565         -    pX = &p->aMem[pOp->p3];
         5576  +    pX = &aMem[pOp->p3];
  5566   5577       for(i=0; i<nArg; i++){
  5567   5578         sqlite3VdbeMemStoreType(pX);
  5568   5579         apArg[i] = pX;
  5569   5580         pX++;
  5570   5581       }
  5571   5582       if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  5572   5583       rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
................................................................................
  5597   5608     p1 = pOp->p1; 
  5598   5609     pPager = sqlite3BtreePager(db->aDb[p1].pBt);
  5599   5610     rc = sqlite3PagerPagecount(pPager, &nPage);
  5600   5611     /* OP_Pagecount is always called from within a read transaction.  The
  5601   5612     ** page count has already been successfully read and cached.  So the
  5602   5613     ** sqlite3PagerPagecount() call above cannot fail. */
  5603   5614     if( ALWAYS(rc==SQLITE_OK) ){
  5604         -    pOut->flags = MEM_Int;
  5605   5615       pOut->u.i = nPage;
  5606   5616     }
  5607   5617     break;
  5608   5618   }
  5609   5619   #endif
  5610   5620   
  5611   5621   #ifndef SQLITE_OMIT_TRACE
................................................................................
  5659   5669   #ifdef VDBE_PROFILE
  5660   5670       {
  5661   5671         u64 elapsed = sqlite3Hwtime() - start;
  5662   5672         pOp->cycles += elapsed;
  5663   5673         pOp->cnt++;
  5664   5674   #if 0
  5665   5675           fprintf(stdout, "%10llu ", elapsed);
  5666         -        sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
         5676  +        sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
  5667   5677   #endif
  5668   5678       }
  5669   5679   #endif
  5670   5680   
  5671   5681       /* The following code adds nothing to the actual functionality
  5672   5682       ** of the program.  It is only here for testing and debugging.
  5673   5683       ** On the other hand, it does burn CPU cycles every time through
................................................................................
  5675   5685       */
  5676   5686   #ifndef NDEBUG
  5677   5687       assert( pc>=-1 && pc<p->nOp );
  5678   5688   
  5679   5689   #ifdef SQLITE_DEBUG
  5680   5690       if( p->trace ){
  5681   5691         if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
  5682         -      if( opProperty & OPFLG_OUT2_PRERELEASE ){
  5683         -        registerTrace(p->trace, pOp->p2, pOut);
         5692  +      if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
         5693  +        registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
  5684   5694         }
  5685         -      if( opProperty & OPFLG_OUT3 ){
  5686         -        registerTrace(p->trace, pOp->p3, pOut);
         5695  +      if( pOp->opflags & OPFLG_OUT3 ){
         5696  +        registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
  5687   5697         }
  5688   5698       }
  5689   5699   #endif  /* SQLITE_DEBUG */
  5690   5700   #endif  /* NDEBUG */
  5691   5701     }  /* The end of the for(;;) loop the loops through opcodes */
  5692   5702   
  5693   5703     /* If we reach this point, it means that execution is finished with

Changes to src/vdbe.h.

    38     38   ** A single instruction of the virtual machine has an opcode
    39     39   ** and as many as three operands.  The instruction is recorded
    40     40   ** as an instance of the following structure:
    41     41   */
    42     42   struct VdbeOp {
    43     43     u8 opcode;          /* What operation to perform */
    44     44     signed char p4type; /* One of the P4_xxx constants for p4 */
    45         -  u8 opflags;         /* Not currently used */
           45  +  u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
    46     46     u8 p5;              /* Fifth parameter is an unsigned character */
    47     47     int p1;             /* First operand */
    48     48     int p2;             /* Second parameter (often the jump destination) */
    49     49     int p3;             /* The third parameter */
    50     50     union {             /* fourth parameter */
    51     51       int i;                 /* Integer value if p4type==P4_INT32 */
    52     52       void *p;               /* Generic pointer */

Changes to src/vdbeInt.h.

   375    375   int sqlite3VdbeMemRealify(Mem*);
   376    376   int sqlite3VdbeMemNumerify(Mem*);
   377    377   int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
   378    378   void sqlite3VdbeMemRelease(Mem *p);
   379    379   void sqlite3VdbeMemReleaseExternal(Mem *p);
   380    380   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   381    381   const char *sqlite3OpcodeName(int);
   382         -int sqlite3VdbeOpcodeHasProperty(int, int);
   383    382   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   384    383   int sqlite3VdbeCloseStatement(Vdbe *, int);
   385    384   void sqlite3VdbeFrameDelete(VdbeFrame*);
   386    385   int sqlite3VdbeFrameRestore(VdbeFrame *);
   387    386   void sqlite3VdbeMemStoreType(Mem *pMem);
   388    387   
   389    388   #ifndef SQLITE_OMIT_FOREIGN_KEY

Changes to src/vdbeaux.c.

   382    382   ** label by setting the P2 value to its correct non-zero value.
   383    383   **
   384    384   ** This routine is called once after all opcodes have been inserted.
   385    385   **
   386    386   ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument 
   387    387   ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by 
   388    388   ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
          389  +**
          390  +** The Op.opflags field is set on all opcodes.
   389    391   */
   390    392   static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
   391    393     int i;
   392    394     int nMaxArgs = *pMaxFuncArgs;
   393    395     Op *pOp;
   394    396     int *aLabel = p->aLabel;
   395    397     p->readOnly = 1;
   396    398     for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
   397    399       u8 opcode = pOp->opcode;
   398    400   
          401  +    pOp->opflags = sqlite3OpcodeProperty[opcode];
   399    402       if( opcode==OP_Function || opcode==OP_AggStep ){
   400    403         if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
          404  +    }else if( opcode==OP_Transaction && pOp->p2!=0 ){
          405  +      p->readOnly = 0;
   401    406   #ifndef SQLITE_OMIT_VIRTUALTABLE
   402    407       }else if( opcode==OP_VUpdate ){
   403    408         if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
   404         -#endif
   405         -    }else if( opcode==OP_Transaction && pOp->p2!=0 ){
   406         -      p->readOnly = 0;
   407         -#ifndef SQLITE_OMIT_VIRTUALTABLE
   408    409       }else if( opcode==OP_VFilter ){
   409    410         int n;
   410    411         assert( p->nOp - i >= 3 );
   411    412         assert( pOp[-1].opcode==OP_Integer );
   412    413         n = pOp[-1].p1;
   413    414         if( n>nMaxArgs ) nMaxArgs = n;
   414    415   #endif
   415    416       }
   416    417   
   417         -    if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){
          418  +    if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
   418    419         assert( -1-pOp->p2<p->nLabel );
   419    420         pOp->p2 = aLabel[-1-pOp->p2];
   420    421       }
   421    422     }
   422    423     sqlite3DbFree(p->db, p->aLabel);
   423    424     p->aLabel = 0;
   424    425   
................................................................................
   472    473       int i;
   473    474       VdbeOpList const *pIn = aOp;
   474    475       for(i=0; i<nOp; i++, pIn++){
   475    476         int p2 = pIn->p2;
   476    477         VdbeOp *pOut = &p->aOp[i+addr];
   477    478         pOut->opcode = pIn->opcode;
   478    479         pOut->p1 = pIn->p1;
   479         -      if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){
          480  +      if( p2<0 && (sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP)!=0 ){
   480    481           pOut->p2 = addr + ADDR(p2);
   481    482         }else{
   482    483           pOut->p2 = p2;
   483    484         }
   484    485         pOut->p3 = pIn->p3;
   485    486         pOut->p4type = P4_NOTUSED;
   486    487         pOut->p4.p = 0;
................................................................................
  2782   2783     const unsigned char *aKey1 = (const unsigned char *)pKey1;
  2783   2784     KeyInfo *pKeyInfo;
  2784   2785     Mem mem1;
  2785   2786   
  2786   2787     pKeyInfo = pPKey2->pKeyInfo;
  2787   2788     mem1.enc = pKeyInfo->enc;
  2788   2789     mem1.db = pKeyInfo->db;
  2789         -  mem1.flags = 0;
  2790         -  mem1.u.i = 0;  /* not needed, here to silence compiler warning */
  2791         -  mem1.zMalloc = 0;
         2790  +  /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
         2791  +  VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
         2792  +
         2793  +  /* Compilers may complain that mem1.u.i is potentially uninitialized.
         2794  +  ** We could initialize it, as shown here, to silence those complaints.
         2795  +  ** But in fact, mem1.u.i will never actually be used initialized, and doing 
         2796  +  ** the unnecessary initialization has a measurable negative performance
         2797  +  ** impact, since this routine is a very high runner.  And so, we choose
         2798  +  ** to ignore the compiler warnings and leave this variable uninitialized.
         2799  +  */
         2800  +  /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
  2792   2801     
  2793   2802     idx1 = getVarint32(aKey1, szHdr1);
  2794   2803     d1 = szHdr1;
  2795   2804     if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
  2796   2805       szHdr1--;
  2797   2806     }
  2798   2807     nField = pKeyInfo->nField;
................................................................................
  2808   2817       d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
  2809   2818   
  2810   2819       /* Do the comparison
  2811   2820       */
  2812   2821       rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
  2813   2822                              i<nField ? pKeyInfo->aColl[i] : 0);
  2814   2823       if( rc!=0 ){
  2815         -      break;
         2824  +      assert( mem1.zMalloc==0 );  /* See comment below */
         2825  +
         2826  +      /* Invert the result if we are using DESC sort order. */
         2827  +      if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
         2828  +        rc = -rc;
         2829  +      }
         2830  +    
         2831  +      /* If the PREFIX_SEARCH flag is set and all fields except the final
         2832  +      ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
         2833  +      ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
         2834  +      ** This is used by the OP_IsUnique opcode.
         2835  +      */
         2836  +      if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
         2837  +        assert( idx1==szHdr1 && rc );
         2838  +        assert( mem1.flags & MEM_Int );
         2839  +        pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
         2840  +        pPKey2->rowid = mem1.u.i;
         2841  +      }
         2842  +    
         2843  +      return rc;
  2816   2844       }
  2817   2845       i++;
  2818   2846     }
  2819   2847   
  2820         -  /* No memory allocation is ever used on mem1. */
  2821         -  if( NEVER(mem1.zMalloc) ) sqlite3VdbeMemRelease(&mem1);
         2848  +  /* No memory allocation is ever used on mem1.  Prove this using
         2849  +  ** the following assert().  If the assert() fails, it indicates a
         2850  +  ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
         2851  +  */
         2852  +  assert( mem1.zMalloc==0 );
  2822   2853   
  2823         -  /* If the PREFIX_SEARCH flag is set and all fields except the final
  2824         -  ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
  2825         -  ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
  2826         -  ** This is used by the OP_IsUnique opcode.
         2854  +  /* rc==0 here means that one of the keys ran out of fields and
         2855  +  ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
         2856  +  ** flag is set, then break the tie by treating key2 as larger.
         2857  +  ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
         2858  +  ** are considered to be equal.  Otherwise, the longer key is the 
         2859  +  ** larger.  As it happens, the pPKey2 will always be the longer
         2860  +  ** if there is a difference.
  2827   2861     */
  2828         -  if( (pPKey2->flags & UNPACKED_PREFIX_SEARCH) && i==(pPKey2->nField-1) ){
  2829         -    assert( idx1==szHdr1 && rc );
  2830         -    assert( mem1.flags & MEM_Int );
  2831         -    pPKey2->flags &= ~UNPACKED_PREFIX_SEARCH;
  2832         -    pPKey2->rowid = mem1.u.i;
         2862  +  assert( rc==0 );
         2863  +  if( pPKey2->flags & UNPACKED_INCRKEY ){
         2864  +    rc = -1;
         2865  +  }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
         2866  +    /* Leave rc==0 */
         2867  +  }else if( idx1<szHdr1 ){
         2868  +    rc = 1;
  2833   2869     }
  2834         -
  2835         -  if( rc==0 ){
  2836         -    /* rc==0 here means that one of the keys ran out of fields and
  2837         -    ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
  2838         -    ** flag is set, then break the tie by treating key2 as larger.
  2839         -    ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
  2840         -    ** are considered to be equal.  Otherwise, the longer key is the 
  2841         -    ** larger.  As it happens, the pPKey2 will always be the longer
  2842         -    ** if there is a difference.
  2843         -    */
  2844         -    if( pPKey2->flags & UNPACKED_INCRKEY ){
  2845         -      rc = -1;
  2846         -    }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
  2847         -      /* Leave rc==0 */
  2848         -    }else if( idx1<szHdr1 ){
  2849         -      rc = 1;
  2850         -    }
  2851         -  }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
  2852         -               && pKeyInfo->aSortOrder[i] ){
  2853         -    rc = -rc;
  2854         -  }
  2855         -
  2856   2870     return rc;
  2857   2871   }
  2858   2872    
  2859   2873   
  2860   2874   /*
  2861   2875   ** pCur points at an index entry created using the OP_MakeRecord opcode.
  2862   2876   ** Read the rowid (the last field in the record) and store it in *rowid.

Changes to src/where.c.

  2590   2590     }
  2591   2591   }
  2592   2592   
  2593   2593   /*
  2594   2594   ** Code an OP_Affinity opcode to apply the column affinity string zAff
  2595   2595   ** to the n registers starting at base. 
  2596   2596   **
  2597         -** Buffer zAff was allocated using sqlite3DbMalloc(). It is the 
  2598         -** responsibility of this function to arrange for it to be eventually
  2599         -** freed using sqlite3DbFree().
         2597  +** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
         2598  +** beginning and end of zAff are ignored.  If all entries in zAff are
         2599  +** SQLITE_AFF_NONE, then no code gets generated.
         2600  +**
         2601  +** This routine makes its own copy of zAff so that the caller is free
         2602  +** to modify zAff after this routine returns.
  2600   2603   */
  2601   2604   static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
  2602   2605     Vdbe *v = pParse->pVdbe;
         2606  +  if( zAff==0 ){
         2607  +    assert( pParse->db->mallocFailed );
         2608  +    return;
         2609  +  }
  2603   2610     assert( v!=0 );
  2604         -  sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
  2605         -  sqlite3VdbeChangeP4(v, -1, zAff, P4_DYNAMIC);
  2606         -  sqlite3ExprCacheAffinityChange(pParse, base, n);
         2611  +
         2612  +  /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
         2613  +  ** and end of the affinity string.
         2614  +  */
         2615  +  while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
         2616  +    n--;
         2617  +    base++;
         2618  +    zAff++;
         2619  +  }
         2620  +  while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
         2621  +    n--;
         2622  +  }
         2623  +
         2624  +  /* Code the OP_Affinity opcode if there is anything left to do. */
         2625  +  if( n>0 ){
         2626  +    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
         2627  +    sqlite3VdbeChangeP4(v, -1, zAff, n);
         2628  +    sqlite3ExprCacheAffinityChange(pParse, base, n);
         2629  +  }
  2607   2630   }
  2608   2631   
  2609   2632   
  2610   2633   /*
  2611   2634   ** Generate code for a single equality term of the WHERE clause.  An equality
  2612   2635   ** term can be either X=expr or X IN (...).   pTerm is the term to be 
  2613   2636   ** coded.
................................................................................
  2670   2693     }
  2671   2694     disableTerm(pLevel, pTerm);
  2672   2695     return iReg;
  2673   2696   }
  2674   2697   
  2675   2698   /*
  2676   2699   ** Generate code that will evaluate all == and IN constraints for an
  2677         -** index.  The values for all constraints are left on the stack.
         2700  +** index.
  2678   2701   **
  2679   2702   ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
  2680   2703   ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
  2681   2704   ** The index has as many as three equality constraints, but in this
  2682   2705   ** example, the third "c" value is an inequality.  So only two 
  2683   2706   ** constraints are coded.  This routine will generate code to evaluate
  2684   2707   ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
  2685   2708   ** in consecutive registers and the index of the first register is returned.
  2686   2709   **
  2687   2710   ** In the example above nEq==2.  But this subroutine works for any value
  2688   2711   ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
  2689         -** The only thing it does is allocate the pLevel->iMem memory cell.
         2712  +** The only thing it does is allocate the pLevel->iMem memory cell and
         2713  +** compute the affinity string.
  2690   2714   **
  2691   2715   ** This routine always allocates at least one memory cell and returns
  2692   2716   ** the index of that memory cell. The code that
  2693   2717   ** calls this routine will use that memory cell to store the termination
  2694   2718   ** key value of the loop.  If one or more IN operators appear, then
  2695   2719   ** this routine allocates an additional nEq memory cells for internal
  2696   2720   ** use.
................................................................................
  2760   2784         }else{
  2761   2785           sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
  2762   2786         }
  2763   2787       }
  2764   2788       testcase( pTerm->eOperator & WO_ISNULL );
  2765   2789       testcase( pTerm->eOperator & WO_IN );
  2766   2790       if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
  2767         -      sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
  2768         -      if( zAff 
  2769         -       && sqlite3CompareAffinity(pTerm->pExpr->pRight, zAff[j])==SQLITE_AFF_NONE
  2770         -      ){
  2771         -        zAff[j] = SQLITE_AFF_NONE;
         2791  +      Expr *pRight = pTerm->pExpr->pRight;
         2792  +      sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
         2793  +      if( zAff ){
         2794  +        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
         2795  +          zAff[j] = SQLITE_AFF_NONE;
         2796  +        }
         2797  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
         2798  +          zAff[j] = SQLITE_AFF_NONE;
         2799  +        }
  2772   2800         }
  2773   2801       }
  2774   2802     }
  2775   2803     *pzAff = zAff;
  2776   2804     return regBase;
  2777   2805   }
  2778   2806   
................................................................................
  3090   3118       start_constraints = pRangeStart || nEq>0;
  3091   3119   
  3092   3120       /* Seek the index cursor to the start of the range. */
  3093   3121       nConstraint = nEq;
  3094   3122       if( pRangeStart ){
  3095   3123         Expr *pRight = pRangeStart->pExpr->pRight;
  3096   3124         sqlite3ExprCode(pParse, pRight, regBase+nEq);
  3097         -      sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  3098         -      if( zAff 
  3099         -       && sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE
  3100         -      ){
  3101         -        /* Since the comparison is to be performed with no conversions applied
  3102         -        ** to the operands, set the affinity to apply to pRight to 
  3103         -        ** SQLITE_AFF_NONE.  */
  3104         -        zAff[nConstraint] = SQLITE_AFF_NONE;
  3105         -      }
         3125  +      sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
         3126  +      if( zAff ){
         3127  +        if( sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE){
         3128  +          /* Since the comparison is to be performed with no conversions
         3129  +          ** applied to the operands, set the affinity to apply to pRight to 
         3130  +          ** SQLITE_AFF_NONE.  */
         3131  +          zAff[nConstraint] = SQLITE_AFF_NONE;
         3132  +        }
         3133  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[nConstraint]) ){
         3134  +          zAff[nConstraint] = SQLITE_AFF_NONE;
         3135  +        }
         3136  +      }  
  3106   3137         nConstraint++;
  3107   3138       }else if( isMinQuery ){
  3108   3139         sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  3109   3140         nConstraint++;
  3110   3141         startEq = 0;
  3111   3142         start_constraints = 1;
  3112   3143       }
................................................................................
  3125   3156       ** range (if any).
  3126   3157       */
  3127   3158       nConstraint = nEq;
  3128   3159       if( pRangeEnd ){
  3129   3160         Expr *pRight = pRangeEnd->pExpr->pRight;
  3130   3161         sqlite3ExprCacheRemove(pParse, regBase+nEq);
  3131   3162         sqlite3ExprCode(pParse, pRight, regBase+nEq);
  3132         -      sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  3133         -      zAff = sqlite3DbStrDup(pParse->db, zAff);
  3134         -      if( zAff 
  3135         -       && sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE
  3136         -      ){
  3137         -        /* Since the comparison is to be performed with no conversions applied
  3138         -        ** to the operands, set the affinity to apply to pRight to 
  3139         -        ** SQLITE_AFF_NONE.  */
  3140         -        zAff[nConstraint] = SQLITE_AFF_NONE;
  3141         -      }
         3163  +      sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
         3164  +      if( zAff ){
         3165  +        if( sqlite3CompareAffinity(pRight, zAff[nConstraint])==SQLITE_AFF_NONE){
         3166  +          /* Since the comparison is to be performed with no conversions
         3167  +          ** applied to the operands, set the affinity to apply to pRight to 
         3168  +          ** SQLITE_AFF_NONE.  */
         3169  +          zAff[nConstraint] = SQLITE_AFF_NONE;
         3170  +        }
         3171  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[nConstraint]) ){
         3172  +          zAff[nConstraint] = SQLITE_AFF_NONE;
         3173  +        }
         3174  +      }  
  3142   3175         codeApplyAffinity(pParse, regBase, nEq+1, zAff);
  3143   3176         nConstraint++;
  3144   3177       }
         3178  +    sqlite3DbFree(pParse->db, zAff);
  3145   3179   
  3146   3180       /* Top of the loop body */
  3147   3181       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3148   3182   
  3149   3183       /* Check if the index cursor is past the end of the range. */
  3150   3184       op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
  3151   3185       testcase( op==OP_Noop );