/ Check-in [356cdd64]
Login

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

Overview
Comment:documentation and speed updates (CVS 164)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:356cdd64860b714f52529159fada799dca7bb1c0
User & Date: drh 2000-10-23 13:16:32
Context
2000-10-23
13:20
Version 1.0.15 (CVS 488) check-in: d2ad3d2b user: drh tags: trunk
13:16
documentation and speed updates (CVS 164) check-in: 356cdd64 user: drh tags: trunk
01:08
remove unnecessary code when NDEBUG is defined (CVS 163) check-in: 738e3e49 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to VERSION.

1
1.0.14
|
1
1.0.15

Changes to src/vdbe.c.

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
...
951
952
953
954
955
956
957


958
959
960
961
962
963
964
965
966
967
968


969
970
971
972
973
974
975
....
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
....
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
....
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
....
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
....
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
....
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
....
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
....
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
....
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
....
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
....
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
....
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
....
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
....
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
....
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
....
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
....
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
....
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
....
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
....
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
....
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
....
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
....
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
....
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
....
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
....
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
....
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
....
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
....
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
....
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
....
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
....
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
....
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
....
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
....
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
....
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
....
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
....
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
....
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
....
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
....
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
....
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
....
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
....
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
....
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
....
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
....
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
....
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
....
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
....
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
....
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
....
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
....
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
** inplicit conversion from one type to the other occurs as necessary.
** 
** Most of the code in this file is taken up by the sqliteVdbeExec()
** function which does the work of interpreting a VDBE program.
** But other routines are also provided to help in building up
** a program instruction by instruction.
**
** $Id: vdbe.c,v 1.46 2000/10/23 01:08:00 drh Exp $
*/
#include "sqliteInt.h"
#include <unistd.h>
#include <ctype.h>

/*
** SQL is translated into a sequence of instructions to be
................................................................................
  int (*xBusy)(void*,const char*,int)  /* Called when a file is busy */
){
  int pc;                    /* The program counter */
  Op *pOp;                   /* Current operation */
  int rc;                    /* Value to return */
  Dbbe *pBe = p->pBe;        /* The backend driver */
  sqlite *db = p->db;        /* The database */


  char zBuf[100];            /* Space to sprintf() and integer */


  /* No instruction ever pushes more than a single element onto the
  ** stack.  And the stack never grows on successive executions of the
  ** same loop.  So the total number of instructions is an upper bound
  ** on the maximum stack depth required.
  **
  ** Allocation all the stack space we will ever need.
  */
  NeedStack(p, p->nOp);


  p->tos = -1;

  rc = SQLITE_OK;
#ifdef MEMORY_DEBUG
  if( access("vdbe_trace",0)==0 ){
    p->trace = stderr;
  }
................................................................................
      /* Opcode: Integer P1 * *
      **
      ** The integer value P1 is pushed onto the stack.
      */
      case OP_Integer: {
        int i = ++p->tos;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        p->aStack[i].i = pOp->p1;
        p->aStack[i].flags = STK_Int;
        break;
      }

      /* Opcode: String * * P3
      **
      ** The string value P3 is pushed onto the stack.
      */
      case OP_String: {
        int i = ++p->tos;
        char *z;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        z = pOp->p3;
        if( z==0 ) z = "";
        p->zStack[i] = z;
        p->aStack[i].n = strlen(z) + 1;
        p->aStack[i].flags = STK_Str;
        break;
      }

      /* Opcode: Null * * *
      **
      ** Push a NULL value onto the stack.
      */
      case OP_Null: {
        int i = ++p->tos;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        p->zStack[i] = 0;
        p->aStack[i].flags = STK_Null;
        break;
      }

      /* Opcode: Pop P1 * *
      **
      ** P1 elements are popped off of the top of stack and discarded.
      */
................................................................................
      ** top of the stack.
      */
      case OP_Dup: {
        int i = p->tos - pOp->p1;
        int j = ++p->tos;
        VERIFY( if( i<0 ) goto not_enough_stack; )
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        p->aStack[j] = p->aStack[i];
        if( p->aStack[i].flags & STK_Dyn ){
          p->zStack[j] = sqliteMalloc( p->aStack[j].n );
          if( p->zStack[j]==0 ) goto no_mem;
          memcpy(p->zStack[j], p->zStack[i], p->aStack[j].n);
        }else{
          p->zStack[j] = p->zStack[i];
        }
        break;
      }

      /* Opcode: Pull P1 * *
      **
      ** The P1-th element is removed from its current location on 
................................................................................
      case OP_Pull: {
        int from = p->tos - pOp->p1;
        int to = p->tos;
        int i;
        Stack ts;
        char *tz;
        VERIFY( if( from<0 ) goto not_enough_stack; )
        ts = p->aStack[from];
        tz = p->zStack[from];
        for(i=from; i<to; i++){
          p->aStack[i] = p->aStack[i+1];
          p->zStack[i] = p->zStack[i+1];
        }
        p->aStack[to] = ts;
        p->zStack[to] = tz;
        break;
      }

      /* Opcode: ColumnCount P1 * *
      **
      ** Specify the number of column values that will appear in the
      ** array passed as the 4th parameter to the callback.  No checking
................................................................................
      */
      case OP_Callback: {
        int i = p->tos - pOp->p1 + 1;
        int j;
        VERIFY( if( i<0 ) goto not_enough_stack; )
        VERIFY( if( NeedStack(p, p->tos+2) ) goto no_mem; )
        for(j=i; j<=p->tos; j++){
          if( (p->aStack[j].flags & STK_Null)==0 ){
            if( Stringify(p, j) ) goto no_mem;
          }
        }
        p->zStack[p->tos+1] = 0;
        if( xCallback!=0 ){
          if( xCallback(pArg, pOp->p1, &p->zStack[i], p->azColName)!=0 ){
            rc = SQLITE_ABORT;
          }
        }
        PopStack(p, pOp->p1);
        break;
      }

................................................................................
        nField = pOp->p1;
        zSep = pOp->p3;
        if( zSep==0 ) zSep = "";
        nSep = strlen(zSep);
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 1 - nSep;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( p->aStack[i].flags & STK_Null ){
            nByte += nSep;
          }else{
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n - 1 + nSep;
          }
        }
        zNew = sqliteMalloc( nByte );
        if( zNew==0 ) goto no_mem;
        j = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNew[j], p->zStack[i], p->aStack[i].n-1);
            j += p->aStack[i].n-1;
          }
          if( nSep>0 && i<p->tos ){
            memcpy(&zNew[j], zSep, nSep);
            j += nSep;
          }
        }
        zNew[j] = 0;
        if( pOp->p2==0 ) PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        p->zStack[p->tos] = zNew;
        break;
      }

      /* Opcode: Add * * *
      **
      ** Pop the top two elements from the stack, add them together,
      ** and push the result back onto the stack.  If either element
................................................................................
      case OP_Add:
      case OP_Subtract:
      case OP_Multiply:
      case OP_Divide: {
        int tos = p->tos;
        int nos = tos - 1;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        if( (p->aStack[tos].flags & p->aStack[nos].flags & STK_Int)==STK_Int ){
          int a, b;
          a = p->aStack[tos].i;
          b = p->aStack[nos].i;
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0 ) goto divide_by_zero;
              b /= a;
              break;
            }
          }
          PopStack(p, 2);
          p->tos = nos;
          p->aStack[nos].i = b;
          p->aStack[nos].flags = STK_Int;
        }else{
          double a, b;
          Realify(p, tos);
          Realify(p, nos);
          a = p->aStack[tos].r;
          b = p->aStack[nos].r;
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0.0 ) goto divide_by_zero;
              b /= a;
              break;
            }
          }
          PopStack(p, 1);
          Release(p, nos);
          p->aStack[nos].r = b;
          p->aStack[nos].flags = STK_Real;
        }
        break;

      divide_by_zero:
        PopStack(p, 2);
        p->tos = nos;
        p->aStack[nos].flags = STK_Null;
        break;
      }

      /* Opcode: Max * * *
      **
      ** Pop the top two elements from the stack then push back the
      ** largest of the two.
................................................................................
      */
      case OP_Max: {
        int tos = p->tos;
        int nos = tos - 1;
        int ft, fn;
        int copy = 0;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        ft = p->aStack[tos].flags;
        fn = p->aStack[nos].flags;
        if( fn & STK_Null ){
          copy = 1;
        }else if( (ft & fn & STK_Int)==STK_Int ){
          copy = p->aStack[nos].i<p->aStack[tos].i;
        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
          Realify(p, tos);
          Realify(p, nos);
          copy = p->aStack[tos].r>p->aStack[nos].r;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          copy = sqliteCompare(p->zStack[tos],p->zStack[nos])>0;
        }
        if( copy ){
          Release(p, nos);
          p->aStack[nos] = p->aStack[tos];
          p->zStack[nos] = p->zStack[tos];
          p->zStack[tos] = 0;
          p->aStack[tos].flags = 0;
        }else{
          Release(p, tos);
        }
        p->tos = nos;
        break;
      }

................................................................................
      */
      case OP_Min: {
        int tos = p->tos;
        int nos = tos - 1;
        int ft, fn;
        int copy = 0;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        ft = p->aStack[tos].flags;
        fn = p->aStack[nos].flags;
        if( fn & STK_Null ){
          copy = 1;
        }else if( ft & STK_Null ){
          copy = 0;
        }else if( (ft & fn & STK_Int)==STK_Int ){
          copy = p->aStack[nos].i>p->aStack[tos].i;
        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
          Realify(p, tos);
          Realify(p, nos);
          copy = p->aStack[tos].r<p->aStack[nos].r;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          copy = sqliteCompare(p->zStack[tos],p->zStack[nos])<0;
        }
        if( copy ){
          Release(p, nos);
          p->aStack[nos] = p->aStack[tos];
          p->zStack[nos] = p->zStack[tos];
          p->zStack[tos] = 0;
          p->aStack[tos].flags = 0;
        }else{
          Release(p, tos);
        }
        p->tos = nos;
        break;
      }

................................................................................
      ** 
      ** Add the value P1 to whatever is on top of the stack.
      */
      case OP_AddImm: {
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Integerify(p, tos);
        p->aStack[tos].i += pOp->p1;
        break;
      }

      /* Opcode: Eq * P2 *
      **
      ** Pop the top two elements from the stack.  If they are equal, then
      ** jump to instruction P2.  Otherwise, continue to the next instruction.
................................................................................
      case OP_Gt:
      case OP_Ge: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        int ft, fn;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        ft = p->aStack[tos].flags;
        fn = p->aStack[nos].flags;
        if( (ft & fn)==STK_Int ){
          c = p->aStack[nos].i - p->aStack[tos].i;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          c = sqliteCompare(p->zStack[nos], p->zStack[tos]);
        }
        switch( pOp->opcode ){
          case OP_Eq:    c = c==0;     break;
          case OP_Ne:    c = c!=0;     break;
          case OP_Lt:    c = c<0;      break;
          case OP_Le:    c = c<=0;     break;
          case OP_Gt:    c = c>0;      break;
................................................................................
      case OP_Like: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        Stringify(p, nos);
        c = sqliteLikeCompare(p->zStack[tos], p->zStack[nos]);
        PopStack(p, 2);
        if( pOp->p1 ) c = !c;
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: Glob P1 P2 *
................................................................................
      case OP_Glob: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        Stringify(p, nos);
        c = sqliteGlobCompare(p->zStack[tos], p->zStack[nos]);
        PopStack(p, 2);
        if( pOp->p1 ) c = !c;
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: And * * *
................................................................................
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        Integerify(p, tos);
        Integerify(p, nos);
        if( pOp->opcode==OP_And ){
          c = p->aStack[tos].i && p->aStack[nos].i;
        }else{
          c = p->aStack[tos].i || p->aStack[nos].i;
        }
        PopStack(p, 2);
        p->tos++;
        p->aStack[nos].i = c;
        p->aStack[nos].flags = STK_Int;
        break;
      }

      /* Opcode: Negative * * *
      **
      ** Treat the top of the stack as a numeric quantity.  Replace it
      ** with its additive inverse.
      */
      case OP_Negative: {
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( p->aStack[tos].flags & STK_Real ){
          Release(p, tos);
          p->aStack[tos].r = -p->aStack[tos].r;
          p->aStack[tos].flags = STK_Real;
        }else if( p->aStack[tos].flags & STK_Int ){
          Release(p, tos);
          p->aStack[tos].i = -p->aStack[tos].i;
          p->aStack[tos].flags = STK_Int;
        }else{
          Realify(p, tos);
          Release(p, tos);
          p->aStack[tos].r = -p->aStack[tos].r;
          p->aStack[tos].flags = STK_Real;
        }
        break;
      }

      /* Opcode: Not * * *
      **
      ** Interpret the top of the stack as a boolean value.  Replace it
................................................................................
      ** with its complement.
      */
      case OP_Not: {
        int tos = p->tos;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        Integerify(p, tos);
        Release(p, tos);
        p->aStack[tos].i = !p->aStack[tos].i;
        p->aStack[tos].flags = STK_Int;
        break;
      }

      /* Opcode: Noop * * *
      **
      ** Do nothing.  This instruction is often useful as a jump
      ** destination.
................................................................................
      ** An integer is false if zero and true otherwise.  A string is
      ** false if it has zero length and true otherwise.
      */
      case OP_If: {
        int c;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        Integerify(p, p->tos);
        c = p->aStack[p->tos].i;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: IsNull * P2 *
      **
................................................................................
      ** Pop a single value from the stack.  If the value popped is NULL
      ** then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_IsNull: {
        int c;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        c = (p->aStack[p->tos].flags & STK_Null)!=0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: NotNull * P2 *
      **
................................................................................
      ** Pop a single value from the stack.  If the value popped is not an
      ** empty string, then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_NotNull: {
        int c;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        c = (p->aStack[p->tos].flags & STK_Null)==0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: MakeRecord P1 * *
      **
................................................................................
        int i, j;
        int addr;

        nField = pOp->p1;
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n;
          }
        }
        nByte += sizeof(int)*nField;
        zNewRecord = sqliteMalloc( nByte );
        if( zNewRecord==0 ) goto no_mem;
        j = 0;
        addr = sizeof(int)*nField;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( p->aStack[i].flags & STK_Null ){
            int zero = 0;
            memcpy(&zNewRecord[j], (char*)&zero, sizeof(int));
          }else{
            memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
            addr += p->aStack[i].n;
          }
          j += sizeof(int);
        }
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNewRecord[j], p->zStack[i], p->aStack[i].n);
            j += p->aStack[i].n;
          }
        }
        PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str | STK_Dyn;
        p->zStack[p->tos] = zNewRecord;
        break;
      }

      /* Opcode: MakeKey P1 P2 *
      **
      ** Convert the top P1 entries of the stack into a single entry suitable
      ** for use as the key in an index or a sort.  The top P1 records are
................................................................................
        int nField;
        int i, j;

        nField = pOp->p1;
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( p->aStack[i].flags & STK_Null ){
            nByte++;
          }else{
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n;
          }
        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNewKey[j], p->zStack[i], p->aStack[i].n-1);
            j += p->aStack[i].n-1;
          }
          if( i<p->tos ) zNewKey[j++] = '\t';
        }
        zNewKey[j] = 0;
        if( pOp->p2==0 ) PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        p->zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Open P1 P2 P3
      **
      ** Open a new cursor for the database file named P3.  Give the
      ** cursor an identifier P1.  The P1 values need not be
................................................................................
      ** in the P1 cursor until needed.
      */
      case OP_Fetch: {
        int i = pOp->p1;
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( i>=0 && i<p->nCursor && p->aCsr[i].pCursor ){
          if( p->aStack[tos].flags & STK_Int ){
            pBe->Fetch(p->aCsr[i].pCursor, sizeof(int), 
                           (char*)&p->aStack[tos].i);
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            pBe->Fetch(p->aCsr[i].pCursor, p->aStack[tos].n, 
                           p->zStack[tos]);
          }
          p->nFetch++;
        }
        PopStack(p, 1);
        break;
      }

................................................................................
      ** This instruction is used to implement the special fcnt() function
      ** in the SQL dialect that SQLite understands.  fcnt() is used for
      ** testing purposes.
      */
      case OP_Fcnt: {
        int i = ++p->tos;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        p->aStack[i].i = p->nFetch;
        p->aStack[i].flags = STK_Int;
        break;
      }

      /* Opcode: Distinct P1 P2 *
      **
      ** Use the top of the stack as a key.  If a record with that key
      ** does not exist in file P1, then jump to P2.  If the record
................................................................................
      case OP_NotFound:
      case OP_Found: {
        int i = pOp->p1;
        int tos = p->tos;
        int alreadyExists = 0;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor ){
          if( p->aStack[tos].flags & STK_Int ){
            alreadyExists = pBe->Test(p->aCsr[i].pCursor, sizeof(int), 
                                          (char*)&p->aStack[tos].i);
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            alreadyExists = pBe->Test(p->aCsr[i].pCursor,p->aStack[tos].n, 
                                           p->zStack[tos]);
          }
        }
        if( pOp->opcode==OP_Found ){
          if( alreadyExists ) pc = pOp->p2 - 1;
        }else{
          if( !alreadyExists ) pc = pOp->p2 - 1;
        }
................................................................................
        if( VERIFY( i<0 || i>=p->nCursor || ) p->aCsr[i].pCursor==0 ){
          v = 0;
        }else{
          v = pBe->New(p->aCsr[i].pCursor);
        }
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        p->aStack[p->tos].i = v;
        p->aStack[p->tos].flags = STK_Int;
        break;
      }

      /* Opcode: Put P1 * *
      **
      ** Write an entry into the database file P1.  A new entry is
      ** created if it doesn't already exist, or the data for an existing
................................................................................
        int tos = p->tos;
        int nos = p->tos-1;
        int i = pOp->p1;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor!=0 ){
          char *zKey;
          int nKey;
          if( (p->aStack[nos].flags & STK_Int)==0 ){
            if( Stringify(p, nos) ) goto no_mem;
            nKey = p->aStack[nos].n;
            zKey = p->zStack[nos];
          }else{
            nKey = sizeof(int);
            zKey = (char*)&p->aStack[nos].i;
          }
          pBe->Put(p->aCsr[i].pCursor, nKey, zKey,
                        p->aStack[tos].n, p->zStack[tos]);
        }
        PopStack(p, 2);
        break;
      }

      /* Opcode: Delete P1 * *
      **
................................................................................
      case OP_Delete: {
        int tos = p->tos;
        int i = pOp->p1;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor!=0 ){
          char *zKey;
          int nKey;
          if( p->aStack[tos].flags & STK_Int ){
            nKey = sizeof(int);
            zKey = (char*)&p->aStack[tos].i;
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            nKey = p->aStack[tos].n;
            zKey = p->zStack[tos];
          }
          pBe->Delete(p->aCsr[i].pCursor, nKey, zKey);
        }
        PopStack(p, 1);
        break;
      }

................................................................................
        char *z;

        VERIFY( if( NeedStack(p, tos) ) goto no_mem; )
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          if( p->aCsr[i].keyAsData ){
            amt = pBe->KeyLength(pCrsr);
            if( amt<=sizeof(int)*(p2+1) ){
              p->aStack[tos].flags = STK_Null;
              break;
            }
            pAddr = (int*)pBe->ReadKey(pCrsr, sizeof(int)*p2);
            if( *pAddr==0 ){
              p->aStack[tos].flags = STK_Null;
              break;
            }
            z = pBe->ReadKey(pCrsr, *pAddr);
          }else{
            amt = pBe->DataLength(pCrsr);
            if( amt<=sizeof(int)*(p2+1) ){
              p->aStack[tos].flags = STK_Null;
              break;
            }
            pAddr = (int*)pBe->ReadData(pCrsr, sizeof(int)*p2);
            if( *pAddr==0 ){
              p->aStack[tos].flags = STK_Null;
              break;
            }
            z = pBe->ReadData(pCrsr, *pAddr);
          }
          p->zStack[tos] = z;
          p->aStack[tos].n = strlen(z) + 1;
          p->aStack[tos].flags = STK_Str;
        }
        break;
      }

      /* Opcode: Key P1 * *
      **
      ** Push onto the stack an integer which is the first 4 bytes of the
................................................................................
        int tos = ++p->tos;
        DbbeCursor *pCrsr;

        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          char *z = pBe->ReadKey(pCrsr, 0);
          if( p->aCsr[i].keyAsData ){
            p->zStack[tos] = z;
            p->aStack[tos].flags = STK_Str;
            p->aStack[tos].n = pBe->KeyLength(pCrsr);
          }else{
            memcpy(&p->aStack[tos].i, z, sizeof(int));
            p->aStack[tos].flags = STK_Int;
          }
        }
        break;
      }

      /* Opcode: Rewind P1 * *
      **
................................................................................
      */
      case OP_NextIdx: {
        int i = pOp->p1;
        int tos = ++p->tos;
        DbbeCursor *pCrsr;

        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        p->zStack[tos] = 0;
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          int *aIdx;
          int nIdx;
          int j, k;
          nIdx = pBe->DataLength(pCrsr)/sizeof(int);
          aIdx = (int*)pBe->ReadData(pCrsr, 0);
          if( nIdx>1 ){
................................................................................
            k = *(aIdx++);
            if( k>nIdx-1 ) k = nIdx-1;
          }else{
            k = nIdx;
          }
          for(j=p->aCsr[i].index; j<k; j++){
            if( aIdx[j]!=0 ){
              p->aStack[tos].i = aIdx[j];
              p->aStack[tos].flags = STK_Int;
              break;
            }
          }
          if( j>=k ){
            j = -1;
            pc = pOp->p2 - 1;
            PopStack(p, 1);
................................................................................
        int nos = tos - 1;
        DbbeCursor *pCrsr;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          int r;
          int newVal;
          Integerify(p, nos);
          newVal = p->aStack[nos].i;
          if( Stringify(p, tos) ) goto no_mem;
          r = pBe->Fetch(pCrsr, p->aStack[tos].n, p->zStack[tos]);
          if( r==0 ){
            /* Create a new record for this index */
            pBe->Put(pCrsr, p->aStack[tos].n, p->zStack[tos],
                          sizeof(int), (char*)&newVal);
          }else{
            /* Extend the existing record */
            int nIdx;
            int *aIdx;
            int k;
            
................................................................................
            nIdx = pBe->DataLength(pCrsr)/sizeof(int);
            if( nIdx==1 ){
              aIdx = sqliteMalloc( sizeof(int)*4 );
              if( aIdx==0 ) goto no_mem;
              aIdx[0] = 2;
              pBe->CopyData(pCrsr, 0, sizeof(int), (char*)&aIdx[1]);
              aIdx[2] = newVal;
              pBe->Put(pCrsr, p->aStack[tos].n, p->zStack[tos],
                    sizeof(int)*4, (char*)aIdx);
              sqliteFree(aIdx);
            }else{
              aIdx = (int*)pBe->ReadData(pCrsr, 0);
              k = aIdx[0];
              if( k<nIdx-1 ){
                aIdx[k+1] = newVal;
                aIdx[0]++;
                pBe->Put(pCrsr, p->aStack[tos].n, p->zStack[tos],
                    sizeof(int)*nIdx, (char*)aIdx);
              }else{
                nIdx *= 2;
                aIdx = sqliteMalloc( sizeof(int)*nIdx );
                if( aIdx==0 ) goto no_mem;
                pBe->CopyData(pCrsr, 0, sizeof(int)*(k+1), (char*)aIdx);
                aIdx[k+1] = newVal;
                aIdx[0]++;
                pBe->Put(pCrsr, p->aStack[tos].n, p->zStack[tos],
                      sizeof(int)*nIdx, (char*)aIdx);
                sqliteFree(aIdx);
              }
            }              
          }
        }
        PopStack(p, 2);
................................................................................
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          int *aIdx;
          int nIdx;
          int j, k;
          int r;
          int oldVal;
          Integerify(p, nos);
          oldVal = p->aStack[nos].i;
          if( Stringify(p, tos) ) goto no_mem;
          r = pBe->Fetch(pCrsr, p->aStack[tos].n, p->zStack[tos]);
          if( r==0 ) break;
          nIdx = pBe->DataLength(pCrsr)/sizeof(int);
          aIdx = (int*)pBe->ReadData(pCrsr, 0);
          if( (nIdx==1 && aIdx[0]==oldVal) || (aIdx[0]==1 && aIdx[1]==oldVal) ){
            pBe->Delete(pCrsr, p->aStack[tos].n, p->zStack[tos]);
          }else{
            k = aIdx[0];
            for(j=1; j<=k && aIdx[j]!=oldVal; j++){}
            if( j>k ) break;
            aIdx[j] = aIdx[k];
            aIdx[k] = 0;
            aIdx[0]--;
            if( aIdx[0]*3 + 1 < nIdx ){
              nIdx /= 2;
            }
            pBe->Put(pCrsr, p->aStack[tos].n, p->zStack[tos], 
                          sizeof(int)*nIdx, (char*)aIdx);
          }
        }
        PopStack(p, 2);
        break;
      }

................................................................................
      case OP_ListWrite: {
        int i = pOp->p1;
        VERIFY( if( i<0 ) goto bad_instruction; )
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        if( VERIFY( i<p->nList && ) p->apList[i]!=0 ){
          int val;
          Integerify(p, p->tos);
          val = p->aStack[p->tos].i;
          PopStack(p, 1);
          fwrite(&val, sizeof(int), 1, p->apList[i]);
        }
        break;
      }

      /* Opcode: ListRewind P1 * *
................................................................................
        int i = pOp->p1;
        int val, amt;
        VERIFY(if( i<0 || i>=p->nList || p->apList[i]==0 )goto bad_instruction;)
        amt = fread(&val, sizeof(int), 1, p->apList[i]);
        if( amt==1 ){
          p->tos++;
          if( NeedStack(p, p->tos) ) goto no_mem;
          p->aStack[p->tos].i = val;
          p->aStack[p->tos].flags = STK_Int;
          p->zStack[p->tos] = 0;
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }

      /* Opcode: ListClose P1 * *
................................................................................
        VERIFY( if( i<0 || i>=p->nSort ) goto bad_instruction; )
        VERIFY( if( tos<1 ) goto not_enough_stack; )
        if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem;
        pSorter = sqliteMalloc( sizeof(Sorter) );
        if( pSorter==0 ) goto no_mem;
        pSorter->pNext = p->apSort[i];
        p->apSort[i] = pSorter;
        pSorter->nKey = p->aStack[tos].n;
        pSorter->zKey = p->zStack[tos];
        pSorter->nData = p->aStack[nos].n;
        pSorter->pData = p->zStack[nos];
        p->aStack[tos].flags = 0;
        p->aStack[nos].flags = 0;
        p->zStack[tos] = 0;
        p->zStack[nos] = 0;
        p->tos -= 2;
        break;
      }

      /* Opcode: SortMakeRec P1 * *
      **
      ** The top P1 elements are the arguments to a callback.  Form these
................................................................................
        int nField;
        int i, j;

        nField = pOp->p1;
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (p->aStack[i].flags & STK_Null)==0 ){
            if( Stringify(p, i) ) goto no_mem;
            nByte += p->aStack[i].n;
          }
        }
        nByte += sizeof(char*)*(nField+1);
        azArg = sqliteMalloc( nByte );
        if( azArg==0 ) goto no_mem;
        z = (char*)&azArg[nField+1];
        for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){
          if( p->aStack[i].flags & STK_Null ){
            azArg[j] = 0;
          }else{
            azArg[j] = z;
            strcpy(z, p->zStack[i]);
            z += p->aStack[i].n;
          }
        }
        PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->zStack[p->tos] = (char*)azArg;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        break;
      }

      /* Opcode: SortMakeKey P1 * P3
      **
      ** Convert the top few entries of the stack into a sort key.  The
      ** number of stack entries consumed is the number of characters in 
................................................................................
        int i, j, k;

        nField = strlen(pOp->p3);
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 1;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( Stringify(p, i) ) goto no_mem;
          nByte += p->aStack[i].n+2;
        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        k = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          zNewKey[j++] = pOp->p3[k++];
          memcpy(&zNewKey[j], p->zStack[i], p->aStack[i].n-1);
          j += p->aStack[i].n-1;
          zNewKey[j++] = 0;
        }
        zNewKey[j] = 0;
        PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        p->aStack[p->tos].n = nByte;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        p->zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Sort P1 * *
      **
      ** Sort all elements on the given sorter.  The algorithm is a
      ** mergesort.
................................................................................
        int i = pOp->p1;
        VERIFY( if( i<0 ) goto bad_instruction; )
        if( VERIFY( i<p->nSort && ) p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->apSort[i] = pSorter->pNext;
          p->tos++;
          VERIFY( NeedStack(p, p->tos); )
          p->zStack[p->tos] = pSorter->pData;
          p->aStack[p->tos].n = pSorter->nData;
          p->aStack[p->tos].flags = STK_Str|STK_Dyn;
          sqliteFree(pSorter->zKey);
          sqliteFree(pSorter);
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }
................................................................................
      case OP_SortKey: {
        int i = pOp->p1;
        VERIFY( if( i<0 ) goto bad_instruction; )
        if( i<p->nSort && p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->tos++;
          VERIFY( NeedStack(p, p->tos); )
          sqliteSetString(&p->zStack[p->tos], pSorter->zKey, 0);
          p->aStack[p->tos].n = pSorter->nKey;
          p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        }
        break;
      }

      /* Opcode: SortCallback P1 P2 *
      **
      ** The top of the stack contains a callback record built using
................................................................................
      ** instruction.  Pop this record from the stack and invoke the
      ** callback on it.
      */
      case OP_SortCallback: {
        int i = p->tos;
        VERIFY( if( i<0 ) goto not_enough_stack; )
        if( xCallback!=0 ){
          if( xCallback(pArg, pOp->p1, (char**)p->zStack[i], p->azColName) ){
            rc = SQLITE_ABORT;
          }
        }
        PopStack(p, 1);
        break;
      }

................................................................................
        if( VERIFY( i>=0 && i<p->nField && ) p->azField ){
          z = p->azField[i];
        }else{
          z = 0;
        }
        if( z==0 ) z = "";
        p->tos++;
        p->aStack[p->tos].n = strlen(z) + 1;
        p->zStack[p->tos] = z;
        p->aStack[p->tos].flags = STK_Str;
        break;
      }

      /* Opcode: MemStore P1 * *
      **
      ** Pop a single value of the stack and store that value into memory
      ** location P1.  P1 should be a small integer since space is allocated
................................................................................
        }
        pMem = &p->aMem[i];
        if( pMem->s.flags & STK_Dyn ){
          zOld = pMem->z;
        }else{
          zOld = 0;
        }
        pMem->s = p->aStack[tos];
        if( pMem->s.flags & STK_Str ){
          pMem->z = sqliteStrNDup(p->zStack[tos], pMem->s.n);
          pMem->s.flags |= STK_Dyn;
        }
        if( zOld ) sqliteFree(zOld);
        PopStack(p, 1);
        break;
      }

................................................................................
      ** Push a copy of the value in memory location P1 onto the stack.
      */
      case OP_MemLoad: {
        int tos = ++p->tos;
        int i = pOp->p1;
        VERIFY( if( NeedStack(p, tos) ) goto no_mem; )
        if( i<0 || i>=p->nMem ){
          p->aStack[tos].flags = STK_Null;
          p->zStack[tos] = 0;
        }else{
          p->aStack[tos] = p->aMem[i].s;
          if( p->aStack[tos].flags & STK_Str ){
            char *z = sqliteMalloc(p->aStack[tos].n);
            if( z==0 ) goto no_mem;
            memcpy(z, p->aMem[i].z, p->aStack[tos].n);
            p->zStack[tos] = z;
            p->aStack[tos].flags |= STK_Dyn;
          }
        }
        break;
      }

      /* Opcode: AggReset * P2 *
      **
................................................................................
        int tos = p->tos;
        AggElem *pElem;
        char *zKey;
        int nKey;

        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        zKey = p->zStack[tos]; 
        nKey = p->aStack[tos].n;
        if( p->agg.nHash<=0 ){
          pElem = 0;
        }else{
          int h = sqliteHashNoCase(zKey, nKey-1) % p->agg.nHash;
          for(pElem=p->agg.apHash[h]; pElem; pElem=pElem->pHash){
            if( strcmp(pElem->zKey, zKey)==0 ) break;
          }
................................................................................
          Mem *pMem = &pFocus->aMem[i];
          char *zOld;
          if( pMem->s.flags & STK_Dyn ){
            zOld = pMem->z;
          }else{
            zOld = 0;
          }
          pMem->s = p->aStack[tos];
          if( pMem->s.flags & STK_Str ){
            pMem->z = sqliteMalloc( p->aStack[tos].n );
            if( pMem->z==0 ) goto no_mem;
            memcpy(pMem->z, p->zStack[tos], pMem->s.n);
            pMem->s.flags |= STK_Str|STK_Dyn;
          }
          if( zOld ) sqliteFree(zOld);
        }
        PopStack(p, 1);
        break;
      }
................................................................................
        AggElem *pFocus = AggInFocus(p->agg);
        int i = pOp->p2;
        int tos = ++p->tos;
        VERIFY( if( NeedStack(p, tos) ) goto no_mem; )
        if( pFocus==0 ) goto no_mem;
        if( VERIFY( i>=0 && ) i<p->agg.nMem ){
          Mem *pMem = &pFocus->aMem[i];
          p->aStack[tos] = pMem->s;
          p->zStack[tos] = pMem->z;
          p->aStack[tos].flags &= ~STK_Dyn;
        }
        break;
      }

      /* Opcode: AggNext * P2 *
      **
      ** Make the next aggregate value the current aggregate.  The prior
................................................................................
        }
        if( pOp->p3 ){
          SetInsert(&p->aSet[i], pOp->p3);
        }else{
          int tos = p->tos;
          if( tos<0 ) goto not_enough_stack;
          Stringify(p, tos);
          SetInsert(&p->aSet[i], p->zStack[tos]);
          PopStack(p, 1);
        }
        break;
      }

      /* Opcode: SetFound P1 P2 *
      **
................................................................................
      ** then jump to P2.  Otherwise fall through.
      */
      case OP_SetFound: {
        int i = pOp->p1;
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        if( VERIFY( i>=0 && i<p->nSet &&) SetTest(&p->aSet[i], p->zStack[tos])){
          pc = pOp->p2 - 1;
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: SetNotFound P1 P2 *
................................................................................
      ** set P1, then jump to P2.  Otherwise fall through.
      */
      case OP_SetNotFound: {
        int i = pOp->p1;
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        if(VERIFY( i>=0 && i<p->nSet &&) !SetTest(&p->aSet[i], p->zStack[tos])){
          pc = pOp->p2 - 1;
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: Length * * *
................................................................................
      ** stack with an integer which is the length of the string.
      */
      case OP_Strlen: {
        int tos = p->tos;
        int len;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        len = p->aStack[tos].n-1;
        PopStack(p, 1);
        p->tos++;
        p->aStack[tos].i = len;
        p->aStack[tos].flags = STK_Int;
        break;
      }

      /* Opcode: Substr P1 P2 *
      **
      ** This operation pops between 1 and 3 elements from the stack and
      ** pushes back a single element.  The bottom-most element popped from
................................................................................
        int start;
        int n;
        char *z;

        if( pOp->p2==0 ){
          VERIFY( if( p->tos<0 ) goto not_enough_stack; )
          Integerify(p, p->tos);
          cnt = p->aStack[p->tos].i;
          PopStack(p, 1);
        }else{
          cnt = pOp->p2;
        }
        if( pOp->p1==0 ){
          VERIFY( if( p->tos<0 ) goto not_enough_stack; )
          Integerify(p, p->tos);
          start = p->aStack[p->tos].i - 1;
          PopStack(p, 1);
        }else{
          start = pOp->p1 - 1;
        }
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        Stringify(p, p->tos);
        n = p->aStack[p->tos].n - 1;
        if( start<0 ){
          start += n + 1;
          if( start<0 ){
            cnt += start;
            start = 0;
          }
        }
................................................................................
        }
        if( cnt<0 ) cnt = 0;
        if( cnt > n ){
          cnt = n;
        }
        z = sqliteMalloc( cnt+1 );
        if( z==0 ) goto no_mem;
        strncpy(z, &p->zStack[p->tos][start], cnt);
        z[cnt] = 0;
        PopStack(p, 1);
        p->tos++;
        p->zStack[p->tos] = z;
        p->aStack[p->tos].n = cnt + 1;
        p->aStack[p->tos].flags = STK_Str|STK_Dyn;
        break;
      }

      /* An other opcode is illegal...
      */
      default: {
        sprintf(zBuf,"%d",pOp->opcode);
................................................................................
      sqliteSetString(pzErrMsg, "jump destination out of range", 0);
      rc = SQLITE_INTERNAL;
    }
    if( p->trace && p->tos>=0 ){
      int i;
      fprintf(p->trace, "Stack:");
      for(i=p->tos; i>=0 && i>p->tos-5; i--){
        if( p->aStack[i].flags & STK_Null ){
          fprintf(p->trace, " NULL");
        }else if( p->aStack[i].flags & STK_Int ){
          fprintf(p->trace, " i:%d", p->aStack[i].i);
        }else if( p->aStack[i].flags & STK_Real ){
          fprintf(p->trace, " r:%g", p->aStack[i].r);
        }else if( p->aStack[i].flags & STK_Str ){
          if( p->aStack[i].flags & STK_Dyn ){
            fprintf(p->trace, " z:[%.11s]", p->zStack[i]);
          }else{
            fprintf(p->trace, " s:[%.11s]", p->zStack[i]);
          }
        }else{
          fprintf(p->trace, " ???");
        }
      }
      fprintf(p->trace,"\n");
    }







|







 







>
>











>
>







 







|
|













|
|
|










|
|







 







|
|
|
|
|

|







 







|
|

|
|

|
|







 







|



|

|







 







|



|






|
|
|










|
|
|







 







|

|
|












|
|




|
|












|
|






|







 







|
|



|



|



|



|
|
|
|







 







|
|





|



|



|



|
|
|
|







 







|







 







|
|

|



|







 







|







 







|







 







|

|



|
|











|

|
|
|

|
|



|
|







 







|
|







 







|







 







|







 







|







 







|

|








|




|




|
|
|





|
|
|







 







|



|






|
|
|







|
|
|







 







|

|


|
|







 







|
|







 







|

|


|
|







 







|
|







 







|

|
|


|


|







 







|

|


|
|







 







|




|






|




|




|
|
|







 







|
|
|

|
|







 







|







 







|
|







 







|

|


|







 







|








|








|







 







|

|




|










|







 







|







 







|
|
|







 







|
|
|
|
|
|
|
|







 







|

|







|



|
|





|
|
|







 







|







|
|






|
|
|







 







|
|
|







 







|
|
|







 







|







 







|
|
|







 







|

|







 







|
|

|
|
|

|
|
|







 







|
|







 







|

|

|







 







|
|
|







 







|







 







|







 







|







 







|


|
|







 







|







|






|







 







|



|
|
|







 







|

|
|
|
|
|
|
|

|







37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
...
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
....
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
....
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
....
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
....
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
....
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
....
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
....
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
....
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
....
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
....
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
....
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
....
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
....
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
....
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
....
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
....
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
....
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
....
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
....
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
....
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
....
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
....
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
....
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
....
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
....
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
....
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
....
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
....
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
....
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
....
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
....
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
....
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
....
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
....
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
....
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
....
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
....
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
....
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
....
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
....
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
....
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
....
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
....
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
....
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
....
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
....
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
....
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
....
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
....
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
....
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
....
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
....
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
....
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
** inplicit conversion from one type to the other occurs as necessary.
** 
** Most of the code in this file is taken up by the sqliteVdbeExec()
** function which does the work of interpreting a VDBE program.
** But other routines are also provided to help in building up
** a program instruction by instruction.
**
** $Id: vdbe.c,v 1.47 2000/10/23 13:16:33 drh Exp $
*/
#include "sqliteInt.h"
#include <unistd.h>
#include <ctype.h>

/*
** SQL is translated into a sequence of instructions to be
................................................................................
  int (*xBusy)(void*,const char*,int)  /* Called when a file is busy */
){
  int pc;                    /* The program counter */
  Op *pOp;                   /* Current operation */
  int rc;                    /* Value to return */
  Dbbe *pBe = p->pBe;        /* The backend driver */
  sqlite *db = p->db;        /* The database */
  char **zStack;
  Stack *aStack;
  char zBuf[100];            /* Space to sprintf() and integer */


  /* No instruction ever pushes more than a single element onto the
  ** stack.  And the stack never grows on successive executions of the
  ** same loop.  So the total number of instructions is an upper bound
  ** on the maximum stack depth required.
  **
  ** Allocation all the stack space we will ever need.
  */
  NeedStack(p, p->nOp);
  zStack = p->zStack;
  aStack = p->aStack;
  p->tos = -1;

  rc = SQLITE_OK;
#ifdef MEMORY_DEBUG
  if( access("vdbe_trace",0)==0 ){
    p->trace = stderr;
  }
................................................................................
      /* Opcode: Integer P1 * *
      **
      ** The integer value P1 is pushed onto the stack.
      */
      case OP_Integer: {
        int i = ++p->tos;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        aStack[i].i = pOp->p1;
        aStack[i].flags = STK_Int;
        break;
      }

      /* Opcode: String * * P3
      **
      ** The string value P3 is pushed onto the stack.
      */
      case OP_String: {
        int i = ++p->tos;
        char *z;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        z = pOp->p3;
        if( z==0 ) z = "";
        zStack[i] = z;
        aStack[i].n = strlen(z) + 1;
        aStack[i].flags = STK_Str;
        break;
      }

      /* Opcode: Null * * *
      **
      ** Push a NULL value onto the stack.
      */
      case OP_Null: {
        int i = ++p->tos;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        zStack[i] = 0;
        aStack[i].flags = STK_Null;
        break;
      }

      /* Opcode: Pop P1 * *
      **
      ** P1 elements are popped off of the top of stack and discarded.
      */
................................................................................
      ** top of the stack.
      */
      case OP_Dup: {
        int i = p->tos - pOp->p1;
        int j = ++p->tos;
        VERIFY( if( i<0 ) goto not_enough_stack; )
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        aStack[j] = aStack[i];
        if( aStack[i].flags & STK_Dyn ){
          zStack[j] = sqliteMalloc( aStack[j].n );
          if( zStack[j]==0 ) goto no_mem;
          memcpy(zStack[j], zStack[i], aStack[j].n);
        }else{
          zStack[j] = zStack[i];
        }
        break;
      }

      /* Opcode: Pull P1 * *
      **
      ** The P1-th element is removed from its current location on 
................................................................................
      case OP_Pull: {
        int from = p->tos - pOp->p1;
        int to = p->tos;
        int i;
        Stack ts;
        char *tz;
        VERIFY( if( from<0 ) goto not_enough_stack; )
        ts = aStack[from];
        tz = zStack[from];
        for(i=from; i<to; i++){
          aStack[i] = aStack[i+1];
          zStack[i] = zStack[i+1];
        }
        aStack[to] = ts;
        zStack[to] = tz;
        break;
      }

      /* Opcode: ColumnCount P1 * *
      **
      ** Specify the number of column values that will appear in the
      ** array passed as the 4th parameter to the callback.  No checking
................................................................................
      */
      case OP_Callback: {
        int i = p->tos - pOp->p1 + 1;
        int j;
        VERIFY( if( i<0 ) goto not_enough_stack; )
        VERIFY( if( NeedStack(p, p->tos+2) ) goto no_mem; )
        for(j=i; j<=p->tos; j++){
          if( (aStack[j].flags & STK_Null)==0 ){
            if( Stringify(p, j) ) goto no_mem;
          }
        }
        zStack[p->tos+1] = 0;
        if( xCallback!=0 ){
          if( xCallback(pArg, pOp->p1, &zStack[i], p->azColName)!=0 ){
            rc = SQLITE_ABORT;
          }
        }
        PopStack(p, pOp->p1);
        break;
      }

................................................................................
        nField = pOp->p1;
        zSep = pOp->p3;
        if( zSep==0 ) zSep = "";
        nSep = strlen(zSep);
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 1 - nSep;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( aStack[i].flags & STK_Null ){
            nByte += nSep;
          }else{
            if( Stringify(p, i) ) goto no_mem;
            nByte += aStack[i].n - 1 + nSep;
          }
        }
        zNew = sqliteMalloc( nByte );
        if( zNew==0 ) goto no_mem;
        j = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNew[j], zStack[i], aStack[i].n-1);
            j += aStack[i].n-1;
          }
          if( nSep>0 && i<p->tos ){
            memcpy(&zNew[j], zSep, nSep);
            j += nSep;
          }
        }
        zNew[j] = 0;
        if( pOp->p2==0 ) PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        aStack[p->tos].n = nByte;
        aStack[p->tos].flags = STK_Str|STK_Dyn;
        zStack[p->tos] = zNew;
        break;
      }

      /* Opcode: Add * * *
      **
      ** Pop the top two elements from the stack, add them together,
      ** and push the result back onto the stack.  If either element
................................................................................
      case OP_Add:
      case OP_Subtract:
      case OP_Multiply:
      case OP_Divide: {
        int tos = p->tos;
        int nos = tos - 1;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        if( (aStack[tos].flags & aStack[nos].flags & STK_Int)==STK_Int ){
          int a, b;
          a = aStack[tos].i;
          b = aStack[nos].i;
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0 ) goto divide_by_zero;
              b /= a;
              break;
            }
          }
          PopStack(p, 2);
          p->tos = nos;
          aStack[nos].i = b;
          aStack[nos].flags = STK_Int;
        }else{
          double a, b;
          Realify(p, tos);
          Realify(p, nos);
          a = aStack[tos].r;
          b = aStack[nos].r;
          switch( pOp->opcode ){
            case OP_Add:         b += a;       break;
            case OP_Subtract:    b -= a;       break;
            case OP_Multiply:    b *= a;       break;
            default: {
              if( a==0.0 ) goto divide_by_zero;
              b /= a;
              break;
            }
          }
          PopStack(p, 1);
          Release(p, nos);
          aStack[nos].r = b;
          aStack[nos].flags = STK_Real;
        }
        break;

      divide_by_zero:
        PopStack(p, 2);
        p->tos = nos;
        aStack[nos].flags = STK_Null;
        break;
      }

      /* Opcode: Max * * *
      **
      ** Pop the top two elements from the stack then push back the
      ** largest of the two.
................................................................................
      */
      case OP_Max: {
        int tos = p->tos;
        int nos = tos - 1;
        int ft, fn;
        int copy = 0;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        ft = aStack[tos].flags;
        fn = aStack[nos].flags;
        if( fn & STK_Null ){
          copy = 1;
        }else if( (ft & fn & STK_Int)==STK_Int ){
          copy = aStack[nos].i<aStack[tos].i;
        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
          Realify(p, tos);
          Realify(p, nos);
          copy = aStack[tos].r>aStack[nos].r;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          copy = sqliteCompare(zStack[tos],zStack[nos])>0;
        }
        if( copy ){
          Release(p, nos);
          aStack[nos] = aStack[tos];
          zStack[nos] = zStack[tos];
          zStack[tos] = 0;
          aStack[tos].flags = 0;
        }else{
          Release(p, tos);
        }
        p->tos = nos;
        break;
      }

................................................................................
      */
      case OP_Min: {
        int tos = p->tos;
        int nos = tos - 1;
        int ft, fn;
        int copy = 0;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        ft = aStack[tos].flags;
        fn = aStack[nos].flags;
        if( fn & STK_Null ){
          copy = 1;
        }else if( ft & STK_Null ){
          copy = 0;
        }else if( (ft & fn & STK_Int)==STK_Int ){
          copy = aStack[nos].i>aStack[tos].i;
        }else if( ( (ft|fn) & (STK_Int|STK_Real) ) !=0 ){
          Realify(p, tos);
          Realify(p, nos);
          copy = aStack[tos].r<aStack[nos].r;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          copy = sqliteCompare(zStack[tos],zStack[nos])<0;
        }
        if( copy ){
          Release(p, nos);
          aStack[nos] = aStack[tos];
          zStack[nos] = zStack[tos];
          zStack[tos] = 0;
          aStack[tos].flags = 0;
        }else{
          Release(p, tos);
        }
        p->tos = nos;
        break;
      }

................................................................................
      ** 
      ** Add the value P1 to whatever is on top of the stack.
      */
      case OP_AddImm: {
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Integerify(p, tos);
        aStack[tos].i += pOp->p1;
        break;
      }

      /* Opcode: Eq * P2 *
      **
      ** Pop the top two elements from the stack.  If they are equal, then
      ** jump to instruction P2.  Otherwise, continue to the next instruction.
................................................................................
      case OP_Gt:
      case OP_Ge: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        int ft, fn;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        ft = aStack[tos].flags;
        fn = aStack[nos].flags;
        if( (ft & fn)==STK_Int ){
          c = aStack[nos].i - aStack[tos].i;
        }else{
          Stringify(p, tos);
          Stringify(p, nos);
          c = sqliteCompare(zStack[nos], zStack[tos]);
        }
        switch( pOp->opcode ){
          case OP_Eq:    c = c==0;     break;
          case OP_Ne:    c = c!=0;     break;
          case OP_Lt:    c = c<0;      break;
          case OP_Le:    c = c<=0;     break;
          case OP_Gt:    c = c>0;      break;
................................................................................
      case OP_Like: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        Stringify(p, nos);
        c = sqliteLikeCompare(zStack[tos], zStack[nos]);
        PopStack(p, 2);
        if( pOp->p1 ) c = !c;
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: Glob P1 P2 *
................................................................................
      case OP_Glob: {
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        Stringify(p, nos);
        c = sqliteGlobCompare(zStack[tos], zStack[nos]);
        PopStack(p, 2);
        if( pOp->p1 ) c = !c;
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: And * * *
................................................................................
        int tos = p->tos;
        int nos = tos - 1;
        int c;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        Integerify(p, tos);
        Integerify(p, nos);
        if( pOp->opcode==OP_And ){
          c = aStack[tos].i && aStack[nos].i;
        }else{
          c = aStack[tos].i || aStack[nos].i;
        }
        PopStack(p, 2);
        p->tos++;
        aStack[nos].i = c;
        aStack[nos].flags = STK_Int;
        break;
      }

      /* Opcode: Negative * * *
      **
      ** Treat the top of the stack as a numeric quantity.  Replace it
      ** with its additive inverse.
      */
      case OP_Negative: {
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( aStack[tos].flags & STK_Real ){
          Release(p, tos);
          aStack[tos].r = -aStack[tos].r;
          aStack[tos].flags = STK_Real;
        }else if( aStack[tos].flags & STK_Int ){
          Release(p, tos);
          aStack[tos].i = -aStack[tos].i;
          aStack[tos].flags = STK_Int;
        }else{
          Realify(p, tos);
          Release(p, tos);
          aStack[tos].r = -aStack[tos].r;
          aStack[tos].flags = STK_Real;
        }
        break;
      }

      /* Opcode: Not * * *
      **
      ** Interpret the top of the stack as a boolean value.  Replace it
................................................................................
      ** with its complement.
      */
      case OP_Not: {
        int tos = p->tos;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        Integerify(p, tos);
        Release(p, tos);
        aStack[tos].i = !aStack[tos].i;
        aStack[tos].flags = STK_Int;
        break;
      }

      /* Opcode: Noop * * *
      **
      ** Do nothing.  This instruction is often useful as a jump
      ** destination.
................................................................................
      ** An integer is false if zero and true otherwise.  A string is
      ** false if it has zero length and true otherwise.
      */
      case OP_If: {
        int c;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        Integerify(p, p->tos);
        c = aStack[p->tos].i;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: IsNull * P2 *
      **
................................................................................
      ** Pop a single value from the stack.  If the value popped is NULL
      ** then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_IsNull: {
        int c;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        c = (aStack[p->tos].flags & STK_Null)!=0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: NotNull * P2 *
      **
................................................................................
      ** Pop a single value from the stack.  If the value popped is not an
      ** empty string, then jump to p2.  Otherwise continue to the next 
      ** instruction.
      */
      case OP_NotNull: {
        int c;
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        c = (aStack[p->tos].flags & STK_Null)==0;
        PopStack(p, 1);
        if( c ) pc = pOp->p2-1;
        break;
      }

      /* Opcode: MakeRecord P1 * *
      **
................................................................................
        int i, j;
        int addr;

        nField = pOp->p1;
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (aStack[i].flags & STK_Null)==0 ){
            if( Stringify(p, i) ) goto no_mem;
            nByte += aStack[i].n;
          }
        }
        nByte += sizeof(int)*nField;
        zNewRecord = sqliteMalloc( nByte );
        if( zNewRecord==0 ) goto no_mem;
        j = 0;
        addr = sizeof(int)*nField;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( aStack[i].flags & STK_Null ){
            int zero = 0;
            memcpy(&zNewRecord[j], (char*)&zero, sizeof(int));
          }else{
            memcpy(&zNewRecord[j], (char*)&addr, sizeof(int));
            addr += aStack[i].n;
          }
          j += sizeof(int);
        }
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNewRecord[j], zStack[i], aStack[i].n);
            j += aStack[i].n;
          }
        }
        PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        aStack[p->tos].n = nByte;
        aStack[p->tos].flags = STK_Str | STK_Dyn;
        zStack[p->tos] = zNewRecord;
        break;
      }

      /* Opcode: MakeKey P1 P2 *
      **
      ** Convert the top P1 entries of the stack into a single entry suitable
      ** for use as the key in an index or a sort.  The top P1 records are
................................................................................
        int nField;
        int i, j;

        nField = pOp->p1;
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( aStack[i].flags & STK_Null ){
            nByte++;
          }else{
            if( Stringify(p, i) ) goto no_mem;
            nByte += aStack[i].n;
          }
        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (aStack[i].flags & STK_Null)==0 ){
            memcpy(&zNewKey[j], zStack[i], aStack[i].n-1);
            j += aStack[i].n-1;
          }
          if( i<p->tos ) zNewKey[j++] = '\t';
        }
        zNewKey[j] = 0;
        if( pOp->p2==0 ) PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        aStack[p->tos].n = nByte;
        aStack[p->tos].flags = STK_Str|STK_Dyn;
        zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Open P1 P2 P3
      **
      ** Open a new cursor for the database file named P3.  Give the
      ** cursor an identifier P1.  The P1 values need not be
................................................................................
      ** in the P1 cursor until needed.
      */
      case OP_Fetch: {
        int i = pOp->p1;
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( i>=0 && i<p->nCursor && p->aCsr[i].pCursor ){
          if( aStack[tos].flags & STK_Int ){
            pBe->Fetch(p->aCsr[i].pCursor, sizeof(int), 
                           (char*)&aStack[tos].i);
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            pBe->Fetch(p->aCsr[i].pCursor, aStack[tos].n, 
                           zStack[tos]);
          }
          p->nFetch++;
        }
        PopStack(p, 1);
        break;
      }

................................................................................
      ** This instruction is used to implement the special fcnt() function
      ** in the SQL dialect that SQLite understands.  fcnt() is used for
      ** testing purposes.
      */
      case OP_Fcnt: {
        int i = ++p->tos;
        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        aStack[i].i = p->nFetch;
        aStack[i].flags = STK_Int;
        break;
      }

      /* Opcode: Distinct P1 P2 *
      **
      ** Use the top of the stack as a key.  If a record with that key
      ** does not exist in file P1, then jump to P2.  If the record
................................................................................
      case OP_NotFound:
      case OP_Found: {
        int i = pOp->p1;
        int tos = p->tos;
        int alreadyExists = 0;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor ){
          if( aStack[tos].flags & STK_Int ){
            alreadyExists = pBe->Test(p->aCsr[i].pCursor, sizeof(int), 
                                          (char*)&aStack[tos].i);
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            alreadyExists = pBe->Test(p->aCsr[i].pCursor,aStack[tos].n, 
                                           zStack[tos]);
          }
        }
        if( pOp->opcode==OP_Found ){
          if( alreadyExists ) pc = pOp->p2 - 1;
        }else{
          if( !alreadyExists ) pc = pOp->p2 - 1;
        }
................................................................................
        if( VERIFY( i<0 || i>=p->nCursor || ) p->aCsr[i].pCursor==0 ){
          v = 0;
        }else{
          v = pBe->New(p->aCsr[i].pCursor);
        }
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        aStack[p->tos].i = v;
        aStack[p->tos].flags = STK_Int;
        break;
      }

      /* Opcode: Put P1 * *
      **
      ** Write an entry into the database file P1.  A new entry is
      ** created if it doesn't already exist, or the data for an existing
................................................................................
        int tos = p->tos;
        int nos = p->tos-1;
        int i = pOp->p1;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor!=0 ){
          char *zKey;
          int nKey;
          if( (aStack[nos].flags & STK_Int)==0 ){
            if( Stringify(p, nos) ) goto no_mem;
            nKey = aStack[nos].n;
            zKey = zStack[nos];
          }else{
            nKey = sizeof(int);
            zKey = (char*)&aStack[nos].i;
          }
          pBe->Put(p->aCsr[i].pCursor, nKey, zKey,
                        aStack[tos].n, zStack[tos]);
        }
        PopStack(p, 2);
        break;
      }

      /* Opcode: Delete P1 * *
      **
................................................................................
      case OP_Delete: {
        int tos = p->tos;
        int i = pOp->p1;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) p->aCsr[i].pCursor!=0 ){
          char *zKey;
          int nKey;
          if( aStack[tos].flags & STK_Int ){
            nKey = sizeof(int);
            zKey = (char*)&aStack[tos].i;
          }else{
            if( Stringify(p, tos) ) goto no_mem;
            nKey = aStack[tos].n;
            zKey = zStack[tos];
          }
          pBe->Delete(p->aCsr[i].pCursor, nKey, zKey);
        }
        PopStack(p, 1);
        break;
      }

................................................................................
        char *z;

        VERIFY( if( NeedStack(p, tos) ) goto no_mem; )
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          if( p->aCsr[i].keyAsData ){
            amt = pBe->KeyLength(pCrsr);
            if( amt<=sizeof(int)*(p2+1) ){
              aStack[tos].flags = STK_Null;
              break;
            }
            pAddr = (int*)pBe->ReadKey(pCrsr, sizeof(int)*p2);
            if( *pAddr==0 ){
              aStack[tos].flags = STK_Null;
              break;
            }
            z = pBe->ReadKey(pCrsr, *pAddr);
          }else{
            amt = pBe->DataLength(pCrsr);
            if( amt<=sizeof(int)*(p2+1) ){
              aStack[tos].flags = STK_Null;
              break;
            }
            pAddr = (int*)pBe->ReadData(pCrsr, sizeof(int)*p2);
            if( *pAddr==0 ){
              aStack[tos].flags = STK_Null;
              break;
            }
            z = pBe->ReadData(pCrsr, *pAddr);
          }
          zStack[tos] = z;
          aStack[tos].n = strlen(z) + 1;
          aStack[tos].flags = STK_Str;
        }
        break;
      }

      /* Opcode: Key P1 * *
      **
      ** Push onto the stack an integer which is the first 4 bytes of the
................................................................................
        int tos = ++p->tos;
        DbbeCursor *pCrsr;

        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          char *z = pBe->ReadKey(pCrsr, 0);
          if( p->aCsr[i].keyAsData ){
            zStack[tos] = z;
            aStack[tos].flags = STK_Str;
            aStack[tos].n = pBe->KeyLength(pCrsr);
          }else{
            memcpy(&aStack[tos].i, z, sizeof(int));
            aStack[tos].flags = STK_Int;
          }
        }
        break;
      }

      /* Opcode: Rewind P1 * *
      **
................................................................................
      */
      case OP_NextIdx: {
        int i = pOp->p1;
        int tos = ++p->tos;
        DbbeCursor *pCrsr;

        VERIFY( if( NeedStack(p, p->tos) ) goto no_mem; )
        zStack[tos] = 0;
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          int *aIdx;
          int nIdx;
          int j, k;
          nIdx = pBe->DataLength(pCrsr)/sizeof(int);
          aIdx = (int*)pBe->ReadData(pCrsr, 0);
          if( nIdx>1 ){
................................................................................
            k = *(aIdx++);
            if( k>nIdx-1 ) k = nIdx-1;
          }else{
            k = nIdx;
          }
          for(j=p->aCsr[i].index; j<k; j++){
            if( aIdx[j]!=0 ){
              aStack[tos].i = aIdx[j];
              aStack[tos].flags = STK_Int;
              break;
            }
          }
          if( j>=k ){
            j = -1;
            pc = pOp->p2 - 1;
            PopStack(p, 1);
................................................................................
        int nos = tos - 1;
        DbbeCursor *pCrsr;
        VERIFY( if( nos<0 ) goto not_enough_stack; )
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          int r;
          int newVal;
          Integerify(p, nos);
          newVal = aStack[nos].i;
          if( Stringify(p, tos) ) goto no_mem;
          r = pBe->Fetch(pCrsr, aStack[tos].n, zStack[tos]);
          if( r==0 ){
            /* Create a new record for this index */
            pBe->Put(pCrsr, aStack[tos].n, zStack[tos],
                          sizeof(int), (char*)&newVal);
          }else{
            /* Extend the existing record */
            int nIdx;
            int *aIdx;
            int k;
            
................................................................................
            nIdx = pBe->DataLength(pCrsr)/sizeof(int);
            if( nIdx==1 ){
              aIdx = sqliteMalloc( sizeof(int)*4 );
              if( aIdx==0 ) goto no_mem;
              aIdx[0] = 2;
              pBe->CopyData(pCrsr, 0, sizeof(int), (char*)&aIdx[1]);
              aIdx[2] = newVal;
              pBe->Put(pCrsr, aStack[tos].n, zStack[tos],
                    sizeof(int)*4, (char*)aIdx);
              sqliteFree(aIdx);
            }else{
              aIdx = (int*)pBe->ReadData(pCrsr, 0);
              k = aIdx[0];
              if( k<nIdx-1 ){
                aIdx[k+1] = newVal;
                aIdx[0]++;
                pBe->Put(pCrsr, aStack[tos].n, zStack[tos],
                    sizeof(int)*nIdx, (char*)aIdx);
              }else{
                nIdx *= 2;
                aIdx = sqliteMalloc( sizeof(int)*nIdx );
                if( aIdx==0 ) goto no_mem;
                pBe->CopyData(pCrsr, 0, sizeof(int)*(k+1), (char*)aIdx);
                aIdx[k+1] = newVal;
                aIdx[0]++;
                pBe->Put(pCrsr, aStack[tos].n, zStack[tos],
                      sizeof(int)*nIdx, (char*)aIdx);
                sqliteFree(aIdx);
              }
            }              
          }
        }
        PopStack(p, 2);
................................................................................
        if( VERIFY( i>=0 && i<p->nCursor && ) (pCrsr = p->aCsr[i].pCursor)!=0 ){
          int *aIdx;
          int nIdx;
          int j, k;
          int r;
          int oldVal;
          Integerify(p, nos);
          oldVal = aStack[nos].i;
          if( Stringify(p, tos) ) goto no_mem;
          r = pBe->Fetch(pCrsr, aStack[tos].n, zStack[tos]);
          if( r==0 ) break;
          nIdx = pBe->DataLength(pCrsr)/sizeof(int);
          aIdx = (int*)pBe->ReadData(pCrsr, 0);
          if( (nIdx==1 && aIdx[0]==oldVal) || (aIdx[0]==1 && aIdx[1]==oldVal) ){
            pBe->Delete(pCrsr, aStack[tos].n, zStack[tos]);
          }else{
            k = aIdx[0];
            for(j=1; j<=k && aIdx[j]!=oldVal; j++){}
            if( j>k ) break;
            aIdx[j] = aIdx[k];
            aIdx[k] = 0;
            aIdx[0]--;
            if( aIdx[0]*3 + 1 < nIdx ){
              nIdx /= 2;
            }
            pBe->Put(pCrsr, aStack[tos].n, zStack[tos], 
                          sizeof(int)*nIdx, (char*)aIdx);
          }
        }
        PopStack(p, 2);
        break;
      }

................................................................................
      case OP_ListWrite: {
        int i = pOp->p1;
        VERIFY( if( i<0 ) goto bad_instruction; )
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        if( VERIFY( i<p->nList && ) p->apList[i]!=0 ){
          int val;
          Integerify(p, p->tos);
          val = aStack[p->tos].i;
          PopStack(p, 1);
          fwrite(&val, sizeof(int), 1, p->apList[i]);
        }
        break;
      }

      /* Opcode: ListRewind P1 * *
................................................................................
        int i = pOp->p1;
        int val, amt;
        VERIFY(if( i<0 || i>=p->nList || p->apList[i]==0 )goto bad_instruction;)
        amt = fread(&val, sizeof(int), 1, p->apList[i]);
        if( amt==1 ){
          p->tos++;
          if( NeedStack(p, p->tos) ) goto no_mem;
          aStack[p->tos].i = val;
          aStack[p->tos].flags = STK_Int;
          zStack[p->tos] = 0;
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }

      /* Opcode: ListClose P1 * *
................................................................................
        VERIFY( if( i<0 || i>=p->nSort ) goto bad_instruction; )
        VERIFY( if( tos<1 ) goto not_enough_stack; )
        if( Stringify(p, tos) || Stringify(p, nos) ) goto no_mem;
        pSorter = sqliteMalloc( sizeof(Sorter) );
        if( pSorter==0 ) goto no_mem;
        pSorter->pNext = p->apSort[i];
        p->apSort[i] = pSorter;
        pSorter->nKey = aStack[tos].n;
        pSorter->zKey = zStack[tos];
        pSorter->nData = aStack[nos].n;
        pSorter->pData = zStack[nos];
        aStack[tos].flags = 0;
        aStack[nos].flags = 0;
        zStack[tos] = 0;
        zStack[nos] = 0;
        p->tos -= 2;
        break;
      }

      /* Opcode: SortMakeRec P1 * *
      **
      ** The top P1 elements are the arguments to a callback.  Form these
................................................................................
        int nField;
        int i, j;

        nField = pOp->p1;
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( (aStack[i].flags & STK_Null)==0 ){
            if( Stringify(p, i) ) goto no_mem;
            nByte += aStack[i].n;
          }
        }
        nByte += sizeof(char*)*(nField+1);
        azArg = sqliteMalloc( nByte );
        if( azArg==0 ) goto no_mem;
        z = (char*)&azArg[nField+1];
        for(j=0, i=p->tos-nField+1; i<=p->tos; i++, j++){
          if( aStack[i].flags & STK_Null ){
            azArg[j] = 0;
          }else{
            azArg[j] = z;
            strcpy(z, zStack[i]);
            z += aStack[i].n;
          }
        }
        PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        aStack[p->tos].n = nByte;
        zStack[p->tos] = (char*)azArg;
        aStack[p->tos].flags = STK_Str|STK_Dyn;
        break;
      }

      /* Opcode: SortMakeKey P1 * P3
      **
      ** Convert the top few entries of the stack into a sort key.  The
      ** number of stack entries consumed is the number of characters in 
................................................................................
        int i, j, k;

        nField = strlen(pOp->p3);
        VERIFY( if( p->tos+1<nField ) goto not_enough_stack; )
        nByte = 1;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          if( Stringify(p, i) ) goto no_mem;
          nByte += aStack[i].n+2;
        }
        zNewKey = sqliteMalloc( nByte );
        if( zNewKey==0 ) goto no_mem;
        j = 0;
        k = 0;
        for(i=p->tos-nField+1; i<=p->tos; i++){
          zNewKey[j++] = pOp->p3[k++];
          memcpy(&zNewKey[j], zStack[i], aStack[i].n-1);
          j += aStack[i].n-1;
          zNewKey[j++] = 0;
        }
        zNewKey[j] = 0;
        PopStack(p, nField);
        VERIFY( NeedStack(p, p->tos+1); )
        p->tos++;
        aStack[p->tos].n = nByte;
        aStack[p->tos].flags = STK_Str|STK_Dyn;
        zStack[p->tos] = zNewKey;
        break;
      }

      /* Opcode: Sort P1 * *
      **
      ** Sort all elements on the given sorter.  The algorithm is a
      ** mergesort.
................................................................................
        int i = pOp->p1;
        VERIFY( if( i<0 ) goto bad_instruction; )
        if( VERIFY( i<p->nSort && ) p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->apSort[i] = pSorter->pNext;
          p->tos++;
          VERIFY( NeedStack(p, p->tos); )
          zStack[p->tos] = pSorter->pData;
          aStack[p->tos].n = pSorter->nData;
          aStack[p->tos].flags = STK_Str|STK_Dyn;
          sqliteFree(pSorter->zKey);
          sqliteFree(pSorter);
        }else{
          pc = pOp->p2 - 1;
        }
        break;
      }
................................................................................
      case OP_SortKey: {
        int i = pOp->p1;
        VERIFY( if( i<0 ) goto bad_instruction; )
        if( i<p->nSort && p->apSort[i]!=0 ){
          Sorter *pSorter = p->apSort[i];
          p->tos++;
          VERIFY( NeedStack(p, p->tos); )
          sqliteSetString(&zStack[p->tos], pSorter->zKey, 0);
          aStack[p->tos].n = pSorter->nKey;
          aStack[p->tos].flags = STK_Str|STK_Dyn;
        }
        break;
      }

      /* Opcode: SortCallback P1 P2 *
      **
      ** The top of the stack contains a callback record built using
................................................................................
      ** instruction.  Pop this record from the stack and invoke the
      ** callback on it.
      */
      case OP_SortCallback: {
        int i = p->tos;
        VERIFY( if( i<0 ) goto not_enough_stack; )
        if( xCallback!=0 ){
          if( xCallback(pArg, pOp->p1, (char**)zStack[i], p->azColName) ){
            rc = SQLITE_ABORT;
          }
        }
        PopStack(p, 1);
        break;
      }

................................................................................
        if( VERIFY( i>=0 && i<p->nField && ) p->azField ){
          z = p->azField[i];
        }else{
          z = 0;
        }
        if( z==0 ) z = "";
        p->tos++;
        aStack[p->tos].n = strlen(z) + 1;
        zStack[p->tos] = z;
        aStack[p->tos].flags = STK_Str;
        break;
      }

      /* Opcode: MemStore P1 * *
      **
      ** Pop a single value of the stack and store that value into memory
      ** location P1.  P1 should be a small integer since space is allocated
................................................................................
        }
        pMem = &p->aMem[i];
        if( pMem->s.flags & STK_Dyn ){
          zOld = pMem->z;
        }else{
          zOld = 0;
        }
        pMem->s = aStack[tos];
        if( pMem->s.flags & STK_Str ){
          pMem->z = sqliteStrNDup(zStack[tos], pMem->s.n);
          pMem->s.flags |= STK_Dyn;
        }
        if( zOld ) sqliteFree(zOld);
        PopStack(p, 1);
        break;
      }

................................................................................
      ** Push a copy of the value in memory location P1 onto the stack.
      */
      case OP_MemLoad: {
        int tos = ++p->tos;
        int i = pOp->p1;
        VERIFY( if( NeedStack(p, tos) ) goto no_mem; )
        if( i<0 || i>=p->nMem ){
          aStack[tos].flags = STK_Null;
          zStack[tos] = 0;
        }else{
          aStack[tos] = p->aMem[i].s;
          if( aStack[tos].flags & STK_Str ){
            char *z = sqliteMalloc(aStack[tos].n);
            if( z==0 ) goto no_mem;
            memcpy(z, p->aMem[i].z, aStack[tos].n);
            zStack[tos] = z;
            aStack[tos].flags |= STK_Dyn;
          }
        }
        break;
      }

      /* Opcode: AggReset * P2 *
      **
................................................................................
        int tos = p->tos;
        AggElem *pElem;
        char *zKey;
        int nKey;

        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        zKey = zStack[tos]; 
        nKey = aStack[tos].n;
        if( p->agg.nHash<=0 ){
          pElem = 0;
        }else{
          int h = sqliteHashNoCase(zKey, nKey-1) % p->agg.nHash;
          for(pElem=p->agg.apHash[h]; pElem; pElem=pElem->pHash){
            if( strcmp(pElem->zKey, zKey)==0 ) break;
          }
................................................................................
          Mem *pMem = &pFocus->aMem[i];
          char *zOld;
          if( pMem->s.flags & STK_Dyn ){
            zOld = pMem->z;
          }else{
            zOld = 0;
          }
          pMem->s = aStack[tos];
          if( pMem->s.flags & STK_Str ){
            pMem->z = sqliteMalloc( aStack[tos].n );
            if( pMem->z==0 ) goto no_mem;
            memcpy(pMem->z, zStack[tos], pMem->s.n);
            pMem->s.flags |= STK_Str|STK_Dyn;
          }
          if( zOld ) sqliteFree(zOld);
        }
        PopStack(p, 1);
        break;
      }
................................................................................
        AggElem *pFocus = AggInFocus(p->agg);
        int i = pOp->p2;
        int tos = ++p->tos;
        VERIFY( if( NeedStack(p, tos) ) goto no_mem; )
        if( pFocus==0 ) goto no_mem;
        if( VERIFY( i>=0 && ) i<p->agg.nMem ){
          Mem *pMem = &pFocus->aMem[i];
          aStack[tos] = pMem->s;
          zStack[tos] = pMem->z;
          aStack[tos].flags &= ~STK_Dyn;
        }
        break;
      }

      /* Opcode: AggNext * P2 *
      **
      ** Make the next aggregate value the current aggregate.  The prior
................................................................................
        }
        if( pOp->p3 ){
          SetInsert(&p->aSet[i], pOp->p3);
        }else{
          int tos = p->tos;
          if( tos<0 ) goto not_enough_stack;
          Stringify(p, tos);
          SetInsert(&p->aSet[i], zStack[tos]);
          PopStack(p, 1);
        }
        break;
      }

      /* Opcode: SetFound P1 P2 *
      **
................................................................................
      ** then jump to P2.  Otherwise fall through.
      */
      case OP_SetFound: {
        int i = pOp->p1;
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        if( VERIFY( i>=0 && i<p->nSet &&) SetTest(&p->aSet[i], zStack[tos])){
          pc = pOp->p2 - 1;
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: SetNotFound P1 P2 *
................................................................................
      ** set P1, then jump to P2.  Otherwise fall through.
      */
      case OP_SetNotFound: {
        int i = pOp->p1;
        int tos = p->tos;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        if(VERIFY( i>=0 && i<p->nSet &&) !SetTest(&p->aSet[i], zStack[tos])){
          pc = pOp->p2 - 1;
        }
        PopStack(p, 1);
        break;
      }

      /* Opcode: Length * * *
................................................................................
      ** stack with an integer which is the length of the string.
      */
      case OP_Strlen: {
        int tos = p->tos;
        int len;
        VERIFY( if( tos<0 ) goto not_enough_stack; )
        Stringify(p, tos);
        len = aStack[tos].n-1;
        PopStack(p, 1);
        p->tos++;
        aStack[tos].i = len;
        aStack[tos].flags = STK_Int;
        break;
      }

      /* Opcode: Substr P1 P2 *
      **
      ** This operation pops between 1 and 3 elements from the stack and
      ** pushes back a single element.  The bottom-most element popped from
................................................................................
        int start;
        int n;
        char *z;

        if( pOp->p2==0 ){
          VERIFY( if( p->tos<0 ) goto not_enough_stack; )
          Integerify(p, p->tos);
          cnt = aStack[p->tos].i;
          PopStack(p, 1);
        }else{
          cnt = pOp->p2;
        }
        if( pOp->p1==0 ){
          VERIFY( if( p->tos<0 ) goto not_enough_stack; )
          Integerify(p, p->tos);
          start = aStack[p->tos].i - 1;
          PopStack(p, 1);
        }else{
          start = pOp->p1 - 1;
        }
        VERIFY( if( p->tos<0 ) goto not_enough_stack; )
        Stringify(p, p->tos);
        n = aStack[p->tos].n - 1;
        if( start<0 ){
          start += n + 1;
          if( start<0 ){
            cnt += start;
            start = 0;
          }
        }
................................................................................
        }
        if( cnt<0 ) cnt = 0;
        if( cnt > n ){
          cnt = n;
        }
        z = sqliteMalloc( cnt+1 );
        if( z==0 ) goto no_mem;
        strncpy(z, &zStack[p->tos][start], cnt);
        z[cnt] = 0;
        PopStack(p, 1);
        p->tos++;
        zStack[p->tos] = z;
        aStack[p->tos].n = cnt + 1;
        aStack[p->tos].flags = STK_Str|STK_Dyn;
        break;
      }

      /* An other opcode is illegal...
      */
      default: {
        sprintf(zBuf,"%d",pOp->opcode);
................................................................................
      sqliteSetString(pzErrMsg, "jump destination out of range", 0);
      rc = SQLITE_INTERNAL;
    }
    if( p->trace && p->tos>=0 ){
      int i;
      fprintf(p->trace, "Stack:");
      for(i=p->tos; i>=0 && i>p->tos-5; i--){
        if( aStack[i].flags & STK_Null ){
          fprintf(p->trace, " NULL");
        }else if( aStack[i].flags & STK_Int ){
          fprintf(p->trace, " i:%d", aStack[i].i);
        }else if( aStack[i].flags & STK_Real ){
          fprintf(p->trace, " r:%g", aStack[i].r);
        }else if( aStack[i].flags & STK_Str ){
          if( aStack[i].flags & STK_Dyn ){
            fprintf(p->trace, " z:[%.11s]", zStack[i]);
          }else{
            fprintf(p->trace, " s:[%.11s]", zStack[i]);
          }
        }else{
          fprintf(p->trace, " ???");
        }
      }
      fprintf(p->trace,"\n");
    }

Changes to www/c_interface.tcl.

1
2
3
4
5
6
7
8
9
10
11
..
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
...
130
131
132
133
134
135
136
137

138
139
140
141
142
143
144
145
146













147
148
149
150
151
152
153
#
# Run this Tcl script to generate the sqlite.html file.
#
set rcsid {$Id: c_interface.tcl,v 1.11 2000/10/16 22:06:43 drh Exp $}

puts {<html>
<head>
  <title>The C language interface to the SQLite library</title>
</head>
<body bgcolor=white>
<h1 align=center>
................................................................................
<p>The interface to the SQLite library consists of three core functions,
one opaque data structure, and some constants used as return values.
The core interface is as follows:</p>

<blockquote><pre>
typedef struct sqlite sqlite;

sqlite *sqlite_open(const char *filename, int mode, char **errmsg);

void sqlite_close(sqlite*);

int sqlite_exec(
  sqlite*,
  char *sql,
  int (*)(void*,int,char**,char**),
................................................................................
database read only.  The third argument is a pointer to a string
pointer.  If the third argument is not NULL and an error occurs
while trying to open the database, then an error message will be
written to memory obtained from malloc() and *errmsg will be made
to point to this error message.  The calling function is responsible
for freeing the memory when it has finished with it.</p>

<p>An SQLite database is just a directory containing a collection of

GDBM files.  There is one GDBM file for each table and index in the
database.  All GDBM files end with the ".tbl" suffix.  Every SQLite
database also contains a special database table named <b>sqlite_master</b>
stored in its own GDBM file.  This special table records the database
schema.</p>

<p>To create a new SQLite database, all you have to do is call
<b>sqlite_open()</b> with the first parameter set to the name of
an empty directory and the second parameter set to 0666.</p>














<p>The return value of the <b>sqlite_open()</b> function is a
pointer to an opaque <b>sqlite</b> structure.  This pointer will
be the first argument to all subsequent SQLite function calls that
deal with the same database.  NULL is returned if the open fails
for any reason.</p>




|







 







|







 







|
>
|







|
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
..
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#
# Run this Tcl script to generate the sqlite.html file.
#
set rcsid {$Id: c_interface.tcl,v 1.12 2000/10/23 13:16:33 drh Exp $}

puts {<html>
<head>
  <title>The C language interface to the SQLite library</title>
</head>
<body bgcolor=white>
<h1 align=center>
................................................................................
<p>The interface to the SQLite library consists of three core functions,
one opaque data structure, and some constants used as return values.
The core interface is as follows:</p>

<blockquote><pre>
typedef struct sqlite sqlite;

sqlite *sqlite_open(const char *dbname, int mode, char **errmsg);

void sqlite_close(sqlite*);

int sqlite_exec(
  sqlite*,
  char *sql,
  int (*)(void*,int,char**,char**),
................................................................................
database read only.  The third argument is a pointer to a string
pointer.  If the third argument is not NULL and an error occurs
while trying to open the database, then an error message will be
written to memory obtained from malloc() and *errmsg will be made
to point to this error message.  The calling function is responsible
for freeing the memory when it has finished with it.</p>

<p>The name of an SQLite database is normally the name of a directory
that contains a collection of GDBM files that comprise the database.
There is one GDBM file for each table and index in the
database.  All GDBM files end with the ".tbl" suffix.  Every SQLite
database also contains a special database table named <b>sqlite_master</b>
stored in its own GDBM file.  This special table records the database
schema.</p>

<p>To create a new SQLite database, all you have to do is call
<b>sqlite_open()</b> with the first parameter set to the name of
an empty directory and the second parameter set to 0666.  The
directory is created automatically if it does not already exist.</p>

<p>Beginning with SQLite version 1.0.14, SQLite supports database
backends other than GDBM.  The only backends currently supported 
are the default GDBM driver and an in-memory hash table database.
You may anticipate additional backends in future versions of SQLite.</p>

<p>An alternative database backend is specified by prepending the
backend name and a colon to the database name argument of the
<b>sqlite_open()</b> function.  For the GDBM backend, you can
prepend "<b>gdbm:</b>" to the directory name.  To select the in-memory
hash table backend, prepend "<b>memory:</b>" to the database name.
Future database drivers will be selected by a similar mechanism.</p>

<p>The return value of the <b>sqlite_open()</b> function is a
pointer to an opaque <b>sqlite</b> structure.  This pointer will
be the first argument to all subsequent SQLite function calls that
deal with the same database.  NULL is returned if the open fails
for any reason.</p>

Changes to www/changes.tcl.

12
13
14
15
16
17
18







19
20
21
22
23
24
25
}


proc chng {date desc} {
  puts "<DT><B>$date</B></DT>"
  puts "<DD><P><UL>$desc</UL></P></DD>"
}








chng {2000 Oct 19 (1.0.14)} {
<li>Added a "memory:" backend driver that stores its database in an
    in-memory hash table.</li>
}

chng {2000 Oct 18 (1.0.13)} {







>
>
>
>
>
>
>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
}


proc chng {date desc} {
  puts "<DT><B>$date</B></DT>"
  puts "<DD><P><UL>$desc</UL></P></DD>"
}

chng {2000 Oct 23 (1.0.15)} {
<li>Documentation updates</li>
<li>Some sanity checking code was removed from the inner loop of vdbe.c
    to help the library to run a little faster.  The code is only
    removed if you compile with -DNDEBUG.</li>
}

chng {2000 Oct 19 (1.0.14)} {
<li>Added a "memory:" backend driver that stores its database in an
    in-memory hash table.</li>
}

chng {2000 Oct 18 (1.0.13)} {

Changes to www/tclsqlite.tcl.

1
2
3
4
5
6
7
8
9
10
11
..
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
























46
47
48
49
50
51
52
...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#
# Run this Tcl script to generate the tclsqlite.html file.
#
set rcsid {$Id: tclsqlite.tcl,v 1.2 2000/10/08 22:20:58 drh Exp $}

puts {<html>
<head>
  <title>The Tcl interface to the SQLite library</title>
</head>
<body bgcolor=white>
<h1 align=center>
................................................................................
tcl command named <b>sqlite</b>.  Because there is only this
one interface command, the interface is not placed in a separate
namespace.</p>

<p>The <b>sqlite</b> command is used as follows:</p>

<blockquote>
<b>sqlite</b>&nbsp;&nbsp;<i>dbcmd&nbsp;&nbsp;database-directory-name</i>
</blockquote>

<p>
The <b>sqlite</b> command opens the SQLite database located in the
directory named by the second argument.  If the database or directory
does not exist, it is created.  The <b>sqlite</b> command 
also creates a new Tcl
command to control the database.  The name of the new Tcl command
is given by the first argument.  This approach is similar to the
way widgets are created in Tk.
</p>

























<p>
Once an SQLite database is open, it can be controlled using 
methods of the <i>dbcmd</i>.  There are currently 5 methods
defined:</p>

<p>
<ul>
................................................................................
<h2>The "timeout" method</h2>

<p>The "timeout" method is used to control how long the SQLite library
will wait for locks to clear before giving up on a database transaction.
The default timeout is 0 millisecond.  (In other words, the default behavior
is not to wait at all.)</p>

<p>The GDBM library the underlies SQLite allows multiple simultaneous
readers or a single writer but not both.  If any process is writing to
the database no other process is allows to read or write.  If any process
is reading the database other processes are allowed to read but not write.
Each GDBM file is locked separately.  Because each SQL table is stored as
a separate file, it is possible for different processes to write to different
database tables at the same time, just not the same table.</p>




|







 







|



|
|
|
|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







1
2
3
4
5
6
7
8
9
10
11
..
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#
# Run this Tcl script to generate the tclsqlite.html file.
#
set rcsid {$Id: tclsqlite.tcl,v 1.3 2000/10/23 13:16:33 drh Exp $}

puts {<html>
<head>
  <title>The Tcl interface to the SQLite library</title>
</head>
<body bgcolor=white>
<h1 align=center>
................................................................................
tcl command named <b>sqlite</b>.  Because there is only this
one interface command, the interface is not placed in a separate
namespace.</p>

<p>The <b>sqlite</b> command is used as follows:</p>

<blockquote>
<b>sqlite</b>&nbsp;&nbsp;<i>dbcmd&nbsp;&nbsp;database-name</i>
</blockquote>

<p>
The <b>sqlite</b> command opens the database named in the second
argument.  If the database does not already exist, it is
automatically created.
The <b>sqlite</b> command also creates a new Tcl
command to control the database.  The name of the new Tcl command
is given by the first argument.  This approach is similar to the
way widgets are created in Tk.
</p>

<p>
The name of the database is usually either the name of a directory
that will contain the GDBM files that comprise the database, or it is the
name of the directory prefaced by "<b>gdbm:</b>".  The second form
of the name is a new feature beginning in SQLite version 1.0.14 that
allows you to select alternative database backends.  The default
backend is GDBM.  But you can also select to store the database in
a hash table in memory by using the prefix "<b>memory:</b>". 
Other backends may be added in the future.
</p>

<p>
Every time you open an SQLite database with the <b>memory:</b> prefix
on the database name, you get a new in-memory database.  This is true
even if you open two databases with the same name.  Furthermore,
an in-memory database is automatically deleted when the database is
closed and so is not useful for persistant storage like a normal
database.  But the use of an in-memory SQL database does give Tcl/Tk
a powerful new data storage mechanism that can do things that are
difficult to do with only Tcl array variables.  In fact, the
hash-table backend for SQLite was created for the sole purpose of
providing better data structure support to the Tcl language.
</p>

<p>
Once an SQLite database is open, it can be controlled using 
methods of the <i>dbcmd</i>.  There are currently 5 methods
defined:</p>

<p>
<ul>
................................................................................
<h2>The "timeout" method</h2>

<p>The "timeout" method is used to control how long the SQLite library
will wait for locks to clear before giving up on a database transaction.
The default timeout is 0 millisecond.  (In other words, the default behavior
is not to wait at all.)</p>

<p>The GDBM backend allows multiple simultaneous
readers or a single writer but not both.  If any process is writing to
the database no other process is allows to read or write.  If any process
is reading the database other processes are allowed to read but not write.
Each GDBM file is locked separately.  Because each SQL table is stored as
a separate file, it is possible for different processes to write to different
database tables at the same time, just not the same table.</p>