/ Changes On Branch coroutine-refactor
Login

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

Changes In Branch coroutine-refactor Excluding Merge-Ins

This is equivalent to a diff from be24fbc221 to a522f364a6

2014-02-07
22:21
Add opcodes OP_InitCoroutine and OP_EndCoroutine. Use these to remove the need for separate boolean registers to record when a co-routine has finished. (check-in: 5a88b6a7ae user: drh tags: trunk)
19:26
In the TCL bindings, make sure Tcl_AppendResult() is always terminated by "(char*)0" and not just "0". (check-in: ea4d23d1c0 user: drh tags: trunk)
19:18
Change the OP_InitCoroutine instruction to jump over the co-routine implementation. (Closed-Leaf check-in: a522f364a6 user: drh tags: coroutine-refactor)
18:27
Get rid of the OP_Undef and OP_IsUndef opcodes in favor of higher-level OP_InitCoroutine and OP_EndCoroutine. (check-in: 1ec0e9dd4b user: drh tags: coroutine-refactor)
13:20
Add the OP_Undef and OP_IsUndef opcodes. With these, use the first register in the result register range as the flag to indicate EOF on an INSERT from a SELECT, rather than allocating a separate boolean register for that task. (check-in: 6fb7448550 user: drh tags: coroutine-refactor)
03:28
More comment updates. No changes to code. (check-in: be24fbc221 user: mistachkin tags: trunk)
02:29
Update comments in vdbe.c. No changes to code. (check-in: 1122b410de user: drh tags: trunk)

Changes to src/insert.c.

   345    345   **
   346    346   ** Registers are allocated as follows:
   347    347   **
   348    348   **   pDest->iSDParm      The register holding the next entry-point of the
   349    349   **                       co-routine.  Run the co-routine to its next breakpoint
   350    350   **                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
   351    351   **
   352         -**   pDest->iSDParm+1    The register holding the "completed" flag for the
   353         -**                       co-routine. This register is 0 if the previous Yield
   354         -**                       generated a new result row, or 1 if the subquery
   355         -**                       has completed.  If the Yield is called again
   356         -**                       after this register becomes 1, then the VDBE will
   357         -**                       halt with an SQLITE_INTERNAL error.
   358         -**
   359    352   **   pDest->iSdst        First result register.
   360    353   **
   361    354   **   pDest->nSdst        Number of result registers.
          355  +**
          356  +** At EOF the first result register will be marked as "undefined" so that
          357  +** the caller can know when to stop reading results.
   362    358   **
   363    359   ** This routine handles all of the register allocation and fills in the
   364    360   ** pDest structure appropriately.
   365    361   **
   366    362   ** Here is a schematic of the generated code assuming that X is the 
   367    363   ** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
   368    364   ** completed flag reg[pDest->iSDParm+1], and R and S are the range of
   369    365   ** registers that hold the result set, reg[pDest->iSdst] through
   370    366   ** reg[pDest->iSdst+pDest->nSdst-1]:
   371    367   **
   372    368   **         X <- A
   373         -**         EOF <- 0
   374    369   **         goto B
   375    370   **      A: setup for the SELECT
   376    371   **         loop rows in the SELECT
   377    372   **           load results into registers R..S
   378    373   **           yield X
   379    374   **         end loop
   380    375   **         cleanup after the SELECT
   381         -**         EOF <- 1
   382         -**         yield X
   383         -**         halt-error
          376  +**         end co-routine R
   384    377   **      B:
   385    378   **
   386    379   ** To use this subroutine, the caller generates code as follows:
   387    380   **
   388    381   **         [ Co-routine generated by this subroutine, shown above ]
   389         -**      S: yield X
   390         -**         if EOF goto E
          382  +**      S: yield X, at EOF goto E
   391    383   **         if skip this row, goto C
   392    384   **         if terminate loop, goto E
   393    385   **         deal with this row
   394    386   **      C: goto S
   395    387   **      E:
   396    388   */
   397    389   int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
   398    390     int regYield;       /* Register holding co-routine entry-point */
   399         -  int regEof;         /* Register holding co-routine completion flag */
   400    391     int addrTop;        /* Top of the co-routine */
   401         -  int j1;             /* Jump instruction */
   402    392     int rc;             /* Result code */
   403    393     Vdbe *v;            /* VDBE under construction */
   404    394   
   405    395     regYield = ++pParse->nMem;
   406         -  regEof = ++pParse->nMem;
   407    396     v = sqlite3GetVdbe(pParse);
   408         -  addrTop = sqlite3VdbeCurrentAddr(v);
   409         -  sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
   410         -  VdbeComment((v, "Co-routine entry point"));
   411         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
   412         -  VdbeComment((v, "Co-routine completion flag"));
          397  +  addrTop = sqlite3VdbeCurrentAddr(v) + 1;
          398  +  sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
   413    399     sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
   414         -  j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
   415    400     rc = sqlite3Select(pParse, pSelect, pDest);
   416    401     assert( pParse->nErr==0 || rc );
   417    402     if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
   418    403     if( rc ) return rc;
   419         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
   420         -  sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
   421         -  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
   422         -  VdbeComment((v, "End of coroutine"));
   423         -  sqlite3VdbeJumpHere(v, j1);                             /* label B: */
          404  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
          405  +  sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
   424    406     return rc;
   425    407   }
   426    408   
   427    409   
   428    410   
   429    411   /* Forward declaration */
   430    412   static int xferOptimization(
................................................................................
   484    466   **           close cursors
   485    467   **         end foreach
   486    468   **
   487    469   ** The 3rd template is for when the second template does not apply
   488    470   ** and the SELECT clause does not read from <table> at any time.
   489    471   ** The generated code follows this template:
   490    472   **
   491         -**         EOF <- 0
   492    473   **         X <- A
   493    474   **         goto B
   494    475   **      A: setup for the SELECT
   495    476   **         loop over the rows in the SELECT
   496    477   **           load values into registers R..R+n
   497    478   **           yield X
   498    479   **         end loop
   499    480   **         cleanup after the SELECT
   500         -**         EOF <- 1
   501         -**         yield X
   502         -**         goto A
          481  +**         end-coroutine X
   503    482   **      B: open write cursor to <table> and its indices
   504         -**      C: yield X
   505         -**         if EOF goto D
          483  +**      C: yield X, at EOF goto D
   506    484   **         insert the select result into <table> from R..R+n
   507    485   **         goto C
   508    486   **      D: cleanup
   509    487   **
   510    488   ** The 4th template is used if the insert statement takes its
   511    489   ** values from a SELECT but the data is being inserted into a table
   512    490   ** that is also read as part of the SELECT.  In the third form,
   513    491   ** we have to use a intermediate table to store the results of
   514    492   ** the select.  The template is like this:
   515    493   **
   516         -**         EOF <- 0
   517    494   **         X <- A
   518    495   **         goto B
   519    496   **      A: setup for the SELECT
   520    497   **         loop over the tables in the SELECT
   521    498   **           load value into register R..R+n
   522    499   **           yield X
   523    500   **         end loop
   524    501   **         cleanup after the SELECT
   525         -**         EOF <- 1
   526         -**         yield X
   527         -**         halt-error
          502  +**         end co-routine R
   528    503   **      B: open temp table
   529         -**      L: yield X
   530         -**         if EOF goto M
          504  +**      L: yield X, at EOF goto M
   531    505   **         insert row from R..R+n into temp table
   532    506   **         goto L
   533    507   **      M: open write cursor to <table> and its indices
   534    508   **         rewind temp table
   535    509   **      C: loop over rows of intermediate table
   536    510   **           transfer values form intermediate table into <table>
   537    511   **         end loop
................................................................................
   572    546     /* Register allocations */
   573    547     int regFromSelect = 0;/* Base register for data coming from SELECT */
   574    548     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
   575    549     int regRowCount = 0;  /* Memory cell used for the row counter */
   576    550     int regIns;           /* Block of regs holding rowid+data being inserted */
   577    551     int regRowid;         /* registers holding insert rowid */
   578    552     int regData;          /* register holding first column to insert */
   579         -  int regEof = 0;       /* Register recording end of SELECT data */
   580    553     int *aRegIdx = 0;     /* One register allocated to each index */
   581    554   
   582    555   #ifndef SQLITE_OMIT_TRIGGER
   583    556     int isView;                 /* True if attempting to insert into a view */
   584    557     Trigger *pTrigger;          /* List of triggers on pTab, if required */
   585    558     int tmask;                  /* Mask of trigger times */
   586    559   #endif
................................................................................
   685    658     ** co-routine is the common header to the 3rd and 4th templates.
   686    659     */
   687    660     if( pSelect ){
   688    661       /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
   689    662       int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
   690    663       if( rc ) goto insert_cleanup;
   691    664   
   692         -    regEof = dest.iSDParm + 1;
   693    665       regFromSelect = dest.iSdst;
   694    666       assert( pSelect->pEList );
   695    667       nColumn = pSelect->pEList->nExpr;
   696    668       assert( dest.nSdst==nColumn );
   697    669   
   698    670       /* Set useTempTable to TRUE if the result of the SELECT statement
   699    671       ** should be written into a temporary table (template 4).  Set to
................................................................................
   710    682   
   711    683       if( useTempTable ){
   712    684         /* Invoke the coroutine to extract information from the SELECT
   713    685         ** and add it to a transient table srcTab.  The code generated
   714    686         ** here is from the 4th template:
   715    687         **
   716    688         **      B: open temp table
   717         -      **      L: yield X
   718         -      **         if EOF goto M
          689  +      **      L: yield X, goto M at EOF
   719    690         **         insert row from R..R+n into temp table
   720    691         **         goto L
   721    692         **      M: ...
   722    693         */
   723    694         int regRec;          /* Register to hold packed record */
   724    695         int regTempRowid;    /* Register to hold temp table ROWID */
   725    696         int addrTop;         /* Label "L" */
   726         -      int addrIf;          /* Address of jump to M */
   727    697   
   728    698         srcTab = pParse->nTab++;
   729    699         regRec = sqlite3GetTempReg(pParse);
   730    700         regTempRowid = sqlite3GetTempReg(pParse);
   731    701         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
   732    702         addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   733         -      addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
   734    703         sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
   735    704         sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
   736    705         sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
   737    706         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
   738         -      sqlite3VdbeJumpHere(v, addrIf);
          707  +      sqlite3VdbeJumpHere(v, addrTop);
   739    708         sqlite3ReleaseTempReg(pParse, regRec);
   740    709         sqlite3ReleaseTempReg(pParse, regTempRowid);
   741    710       }
   742    711     }else{
   743    712       /* This is the case if the data for the INSERT is coming from a VALUES
   744    713       ** clause
   745    714       */
................................................................................
   843    812     }
   844    813   
   845    814     /* This is the top of the main insertion loop */
   846    815     if( useTempTable ){
   847    816       /* This block codes the top of loop only.  The complete loop is the
   848    817       ** following pseudocode (template 4):
   849    818       **
   850         -    **         rewind temp table
          819  +    **         rewind temp table, if empty goto D
   851    820       **      C: loop over rows of intermediate table
   852    821       **           transfer values form intermediate table into <table>
   853    822       **         end loop
   854    823       **      D: ...
   855    824       */
   856    825       addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
   857    826       addrCont = sqlite3VdbeCurrentAddr(v);
   858    827     }else if( pSelect ){
   859    828       /* This block codes the top of loop only.  The complete loop is the
   860    829       ** following pseudocode (template 3):
   861    830       **
   862         -    **      C: yield X
   863         -    **         if EOF goto D
          831  +    **      C: yield X, at EOF goto D
   864    832       **         insert the select result into <table> from R..R+n
   865    833       **         goto C
   866    834       **      D: ...
   867    835       */
   868         -    addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   869         -    addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
          836  +    addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
   870    837     }
   871    838   
   872    839     /* Allocate registers for holding the rowid of the new row,
   873    840     ** the content of the new row, and the assemblied row record.
   874    841     */
   875    842     regRowid = regIns = pParse->nMem+1;
   876    843     pParse->nMem += pTab->nCol + 1;

Changes to src/select.c.

   761    761           sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
   762    762           /* The LIMIT clause will jump out of the loop for us */
   763    763         }
   764    764         break;
   765    765       }
   766    766   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
   767    767   
   768         -    /* Send the data to the callback function or to a subroutine.  In the
   769         -    ** case of a subroutine, the subroutine itself is responsible for
   770         -    ** popping the data from the stack.
   771         -    */
   772         -    case SRT_Coroutine:
   773         -    case SRT_Output: {
          768  +    case SRT_Coroutine:       /* Send data to a co-routine */
          769  +    case SRT_Output: {        /* Return the results */
   774    770         testcase( eDest==SRT_Coroutine );
   775    771         testcase( eDest==SRT_Output );
   776    772         if( pOrderBy ){
   777    773           int r1 = sqlite3GetTempReg(pParse);
   778    774           sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
   779    775           pushOntoSorter(pParse, pOrderBy, p, r1);
   780    776           sqlite3ReleaseTempReg(pParse, r1);
................................................................................
  2568   2564   ){
  2569   2565     int i, j;             /* Loop counters */
  2570   2566     Select *pPrior;       /* Another SELECT immediately to our left */
  2571   2567     Vdbe *v;              /* Generate code to this VDBE */
  2572   2568     SelectDest destA;     /* Destination for coroutine A */
  2573   2569     SelectDest destB;     /* Destination for coroutine B */
  2574   2570     int regAddrA;         /* Address register for select-A coroutine */
  2575         -  int regEofA;          /* Flag to indicate when select-A is complete */
  2576   2571     int regAddrB;         /* Address register for select-B coroutine */
  2577         -  int regEofB;          /* Flag to indicate when select-B is complete */
  2578   2572     int addrSelectA;      /* Address of the select-A coroutine */
  2579   2573     int addrSelectB;      /* Address of the select-B coroutine */
  2580   2574     int regOutA;          /* Address register for the output-A subroutine */
  2581   2575     int regOutB;          /* Address register for the output-B subroutine */
  2582   2576     int addrOutA;         /* Address of the output-A subroutine */
  2583   2577     int addrOutB = 0;     /* Address of the output-B subroutine */
  2584   2578     int addrEofA;         /* Address of the select-A-exhausted subroutine */
         2579  +  int addrEofA_noB;     /* Alternate addrEofA if B is uninitialized */
  2585   2580     int addrEofB;         /* Address of the select-B-exhausted subroutine */
  2586   2581     int addrAltB;         /* Address of the A<B subroutine */
  2587   2582     int addrAeqB;         /* Address of the A==B subroutine */
  2588   2583     int addrAgtB;         /* Address of the A>B subroutine */
  2589   2584     int regLimitA;        /* Limit register for select-A */
  2590   2585     int regLimitB;        /* Limit register for select-A */
  2591   2586     int regPrev;          /* A range of registers to hold previous output */
................................................................................
  2714   2709     }
  2715   2710     sqlite3ExprDelete(db, p->pLimit);
  2716   2711     p->pLimit = 0;
  2717   2712     sqlite3ExprDelete(db, p->pOffset);
  2718   2713     p->pOffset = 0;
  2719   2714   
  2720   2715     regAddrA = ++pParse->nMem;
  2721         -  regEofA = ++pParse->nMem;
  2722   2716     regAddrB = ++pParse->nMem;
  2723         -  regEofB = ++pParse->nMem;
  2724   2717     regOutA = ++pParse->nMem;
  2725   2718     regOutB = ++pParse->nMem;
  2726   2719     sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
  2727   2720     sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
  2728         -
  2729         -  /* Jump past the various subroutines and coroutines to the main
  2730         -  ** merge loop
  2731         -  */
  2732         -  j1 = sqlite3VdbeAddOp0(v, OP_Goto);
  2733         -  addrSelectA = sqlite3VdbeCurrentAddr(v);
  2734         -
  2735   2721   
  2736   2722     /* Generate a coroutine to evaluate the SELECT statement to the
  2737   2723     ** left of the compound operator - the "A" select.
  2738   2724     */
  2739         -  VdbeNoopComment((v, "Begin coroutine for left SELECT"));
         2725  +  addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
         2726  +  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
         2727  +  VdbeComment((v, "left SELECT"));
  2740   2728     pPrior->iLimit = regLimitA;
  2741   2729     explainSetInteger(iSub1, pParse->iNextSelectId);
  2742   2730     sqlite3Select(pParse, pPrior, &destA);
  2743         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
  2744         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
  2745         -  VdbeNoopComment((v, "End coroutine for left SELECT"));
         2731  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
         2732  +  sqlite3VdbeJumpHere(v, j1);
  2746   2733   
  2747   2734     /* Generate a coroutine to evaluate the SELECT statement on 
  2748   2735     ** the right - the "B" select
  2749   2736     */
  2750         -  addrSelectB = sqlite3VdbeCurrentAddr(v);
  2751         -  VdbeNoopComment((v, "Begin coroutine for right SELECT"));
         2737  +  addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
         2738  +  j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
         2739  +  VdbeComment((v, "right SELECT"));
  2752   2740     savedLimit = p->iLimit;
  2753   2741     savedOffset = p->iOffset;
  2754   2742     p->iLimit = regLimitB;
  2755   2743     p->iOffset = 0;  
  2756   2744     explainSetInteger(iSub2, pParse->iNextSelectId);
  2757   2745     sqlite3Select(pParse, p, &destB);
  2758   2746     p->iLimit = savedLimit;
  2759   2747     p->iOffset = savedOffset;
  2760         -  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
  2761         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
  2762         -  VdbeNoopComment((v, "End coroutine for right SELECT"));
         2748  +  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
  2763   2749   
  2764   2750     /* Generate a subroutine that outputs the current row of the A
  2765   2751     ** select as the next output row of the compound select.
  2766   2752     */
  2767   2753     VdbeNoopComment((v, "Output routine for A"));
  2768   2754     addrOutA = generateOutputSubroutine(pParse,
  2769   2755                    p, &destA, pDest, regOutA,
................................................................................
  2779   2765                    regPrev, pKeyDup, labelEnd);
  2780   2766     }
  2781   2767     sqlite3KeyInfoUnref(pKeyDup);
  2782   2768   
  2783   2769     /* Generate a subroutine to run when the results from select A
  2784   2770     ** are exhausted and only data in select B remains.
  2785   2771     */
  2786         -  VdbeNoopComment((v, "eof-A subroutine"));
  2787   2772     if( op==TK_EXCEPT || op==TK_INTERSECT ){
  2788         -    addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
         2773  +    addrEofA_noB = addrEofA = labelEnd;
  2789   2774     }else{  
  2790         -    addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
  2791         -    sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  2792         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
         2775  +    VdbeNoopComment((v, "eof-A subroutine"));
         2776  +    addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
         2777  +    addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
  2793   2778       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
  2794   2779       p->nSelectRow += pPrior->nSelectRow;
  2795   2780     }
  2796   2781   
  2797   2782     /* Generate a subroutine to run when the results from select B
  2798   2783     ** are exhausted and only data in select A remains.
  2799   2784     */
  2800   2785     if( op==TK_INTERSECT ){
  2801   2786       addrEofB = addrEofA;
  2802   2787       if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
  2803   2788     }else{  
  2804   2789       VdbeNoopComment((v, "eof-B subroutine"));
  2805         -    addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
  2806         -    sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  2807         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
         2790  +    addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
         2791  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd);
  2808   2792       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
  2809   2793     }
  2810   2794   
  2811   2795     /* Generate code to handle the case of A<B
  2812   2796     */
  2813   2797     VdbeNoopComment((v, "A-lt-B subroutine"));
  2814   2798     addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  2815         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
  2816         -  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
         2799  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA);
  2817   2800     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2818   2801   
  2819   2802     /* Generate code to handle the case of A==B
  2820   2803     */
  2821   2804     if( op==TK_ALL ){
  2822   2805       addrAeqB = addrAltB;
  2823   2806     }else if( op==TK_INTERSECT ){
  2824   2807       addrAeqB = addrAltB;
  2825   2808       addrAltB++;
  2826   2809     }else{
  2827   2810       VdbeNoopComment((v, "A-eq-B subroutine"));
  2828   2811       addrAeqB =
  2829         -    sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
  2830         -    sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
         2812  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA);
  2831   2813       sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2832   2814     }
  2833   2815   
  2834   2816     /* Generate code to handle the case of A>B
  2835   2817     */
  2836   2818     VdbeNoopComment((v, "A-gt-B subroutine"));
  2837   2819     addrAgtB = sqlite3VdbeCurrentAddr(v);
  2838   2820     if( op==TK_ALL || op==TK_UNION ){
  2839   2821       sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  2840   2822     }
  2841         -  sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
  2842         -  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
         2823  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB);
  2843   2824     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2844   2825   
  2845   2826     /* This code runs once to initialize everything.
  2846   2827     */
  2847   2828     sqlite3VdbeJumpHere(v, j1);
  2848         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
  2849         -  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
  2850         -  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
  2851         -  sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
  2852         -  sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
  2853         -  sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
         2829  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB);
         2830  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB);
  2854   2831   
  2855   2832     /* Implement the main merge loop
  2856   2833     */
  2857   2834     sqlite3VdbeResolveLabel(v, labelCmpr);
  2858   2835     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
  2859   2836     sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
  2860   2837                            (char*)pKeyMerge, P4_KEYINFO);
................................................................................
  4555   4532       }else if( pTabList->nSrc==1
  4556   4533              && OptimizationEnabled(db, SQLITE_SubqCoroutine)
  4557   4534       ){
  4558   4535         /* Implement a co-routine that will return a single row of the result
  4559   4536         ** set on each invocation.
  4560   4537         */
  4561   4538         int addrTop;
  4562         -      int addrEof;
  4563   4539         pItem->regReturn = ++pParse->nMem;
  4564         -      addrEof = ++pParse->nMem;
  4565   4540         /* Before coding the OP_Goto to jump to the start of the main routine,
  4566   4541         ** ensure that the jump to the verify-schema routine has already
  4567   4542         ** been coded. Otherwise, the verify-schema would likely be coded as 
  4568   4543         ** part of the co-routine. If the main routine then accessed the 
  4569   4544         ** database before invoking the co-routine for the first time (for 
  4570   4545         ** example to initialize a LIMIT register from a sub-select), it would 
  4571   4546         ** be doing so without having verified the schema version and obtained 
  4572   4547         ** the required db locks. See ticket d6b36be38.  */
  4573   4548         sqlite3CodeVerifySchema(pParse, -1);
  4574   4549         sqlite3VdbeAddOp0(v, OP_Goto);
  4575   4550         addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
  4576   4551         sqlite3VdbeChangeP5(v, 1);
  4577         -      VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
         4552  +      VdbeComment((v, "coroutine %s", pItem->pTab->zName));
  4578   4553         pItem->addrFillSub = addrTop;
  4579         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
  4580         -      sqlite3VdbeChangeP5(v, 1);
  4581   4554         sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
  4582   4555         explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
  4583   4556         sqlite3Select(pParse, pSub, &dest);
  4584   4557         pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
  4585   4558         pItem->viaCoroutine = 1;
  4586   4559         sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
  4587   4560         sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
  4588         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
  4589         -      sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
  4590         -      VdbeComment((v, "end %s", pItem->pTab->zName));
         4561  +      sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
  4591   4562         sqlite3VdbeJumpHere(v, addrTop-1);
  4592   4563         sqlite3ClearTempRegCache(pParse);
  4593   4564       }else{
  4594   4565         /* Generate a subroutine that will fill an ephemeral table with
  4595   4566         ** the content of this subquery.  pItem->addrFillSub will point
  4596   4567         ** to the address of the generated subroutine.  pItem->regReturn
  4597   4568         ** is a register allocated to hold the subroutine return address

Changes to src/vdbe.c.

   389    389   #endif
   390    390   
   391    391   #ifdef SQLITE_DEBUG
   392    392   /*
   393    393   ** Print the value of a register for tracing purposes:
   394    394   */
   395    395   static void memTracePrint(Mem *p){
   396         -  if( p->flags & MEM_Invalid ){
          396  +  if( p->flags & MEM_Undefined ){
   397    397       printf(" undefined");
   398    398     }else if( p->flags & MEM_Null ){
   399    399       printf(" NULL");
   400    400     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
   401    401       printf(" si:%lld", p->u.i);
   402    402     }else if( p->flags & MEM_Int ){
   403    403       printf(" i:%lld", p->u.i);
................................................................................
   698    698     }
   699    699   #endif
   700    700     
   701    701     break;
   702    702   }
   703    703   
   704    704   /* Opcode:  Gosub P1 P2 * * *
   705         -** Synopsis:  r[P1]=pc; pc=P2
   706    705   **
   707    706   ** Write the current address onto register P1
   708    707   ** and then jump to address P2.
   709    708   */
   710    709   case OP_Gosub: {            /* jump */
   711    710     assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
   712    711     pIn1 = &aMem[pOp->p1];
................................................................................
   716    715     pIn1->u.i = pc;
   717    716     REGISTER_TRACE(pOp->p1, pIn1);
   718    717     pc = pOp->p2 - 1;
   719    718     break;
   720    719   }
   721    720   
   722    721   /* Opcode:  Return P1 * * * *
   723         -** Synopsis:  pc=r[P1]+1
   724    722   **
   725         -** Jump to the next instruction after the address in register P1.
          723  +** Jump to the next instruction after the address in register P1.  After
          724  +** the jump, register P1 becomes undefined.
   726    725   */
   727    726   case OP_Return: {           /* in1 */
   728    727     pIn1 = &aMem[pOp->p1];
   729         -  assert( pIn1->flags & MEM_Int );
          728  +  assert( pIn1->flags==MEM_Int );
   730    729     pc = (int)pIn1->u.i;
          730  +  pIn1->flags = MEM_Undefined;
   731    731     break;
   732    732   }
   733    733   
   734         -/* Opcode:  Yield P1 * * * *
   735         -** Synopsis: swap(pc,r[P1])
          734  +/* Opcode: InitCoroutine P1 P2 P3 * *
          735  +**
          736  +** Set up register P1 so that it will OP_Yield to the co-routine
          737  +** located at address P3.
          738  +**
          739  +** If P2!=0 then the co-routine implementation immediately follows
          740  +** this opcode.  So jump over the co-routine implementation to
          741  +** address P2.
          742  +*/
          743  +case OP_InitCoroutine: {     /* jump */
          744  +  assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
          745  +  assert( pOp->p2>=0 && pOp->p2<p->nOp );
          746  +  assert( pOp->p3>=0 && pOp->p3<p->nOp );
          747  +  pOut = &aMem[pOp->p1];
          748  +  assert( !VdbeMemDynamic(pOut) );
          749  +  pOut->u.i = pOp->p3 - 1;
          750  +  pOut->flags = MEM_Int;
          751  +  if( pOp->p2 ) pc = pOp->p2 - 1;
          752  +  break;
          753  +}
          754  +
          755  +/* Opcode:  EndCoroutine P1 * * * *
          756  +**
          757  +** The instruction at the address in register P1 is an OP_Yield.
          758  +** Jump to the P2 parameter of that OP_Yield.
          759  +** After the jump, register P1 becomes undefined.
          760  +*/
          761  +case OP_EndCoroutine: {           /* in1 */
          762  +  VdbeOp *pCaller;
          763  +  pIn1 = &aMem[pOp->p1];
          764  +  assert( pIn1->flags==MEM_Int );
          765  +  assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
          766  +  pCaller = &aOp[pIn1->u.i];
          767  +  assert( pCaller->opcode==OP_Yield );
          768  +  assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
          769  +  pc = pCaller->p2 - 1;
          770  +  pIn1->flags = MEM_Undefined;
          771  +  break;
          772  +}
          773  +
          774  +/* Opcode:  Yield P1 P2 * * *
   736    775   **
   737    776   ** Swap the program counter with the value in register P1.
          777  +**
          778  +** If the co-routine ends with OP_Yield or OP_Return then continue
          779  +** to the next instruction.  But if the co-routine ends with
          780  +** OP_EndCoroutine, jump immediately to P2.
   738    781   */
   739         -case OP_Yield: {            /* in1 */
          782  +case OP_Yield: {            /* in1, jump */
   740    783     int pcDest;
   741    784     pIn1 = &aMem[pOp->p1];
   742    785     assert( (pIn1->flags & MEM_Dyn)==0 );
   743    786     pIn1->flags = MEM_Int;
   744    787     pcDest = (int)pIn1->u.i;
   745    788     pIn1->u.i = pc;
   746    789     REGISTER_TRACE(pOp->p1, pIn1);
................................................................................
   970   1013       pOut->flags = nullFlag;
   971   1014       cnt--;
   972   1015     }
   973   1016     break;
   974   1017   }
   975   1018   
   976   1019   
   977         -/* Opcode: Blob P1 P2 * P4
         1020  +/* Opcode: Blob P1 P2 * P4 *
   978   1021   ** Synopsis: r[P2]=P4 (len=P1)
   979   1022   **
   980   1023   ** P4 points to a blob of data P1 bytes long.  Store this
   981   1024   ** blob in register P2.
   982   1025   */
   983   1026   case OP_Blob: {                /* out2-prerelease */
   984   1027     assert( pOp->p1 <= SQLITE_MAX_LENGTH );
................................................................................
  5187   5230       pFrame->nOp = p->nOp;
  5188   5231       pFrame->token = pProgram->token;
  5189   5232       pFrame->aOnceFlag = p->aOnceFlag;
  5190   5233       pFrame->nOnceFlag = p->nOnceFlag;
  5191   5234   
  5192   5235       pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
  5193   5236       for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
  5194         -      pMem->flags = MEM_Invalid;
         5237  +      pMem->flags = MEM_Undefined;
  5195   5238         pMem->db = db;
  5196   5239       }
  5197   5240     }else{
  5198   5241       pFrame = pRt->u.pFrame;
  5199   5242       assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
  5200   5243       assert( pProgram->nCsr==pFrame->nChildCsr );
  5201   5244       assert( pc==pFrame->pc );

Changes to src/vdbeInt.h.

   194    194   #define MEM_Null      0x0001   /* Value is NULL */
   195    195   #define MEM_Str       0x0002   /* Value is a string */
   196    196   #define MEM_Int       0x0004   /* Value is an integer */
   197    197   #define MEM_Real      0x0008   /* Value is a real number */
   198    198   #define MEM_Blob      0x0010   /* Value is a BLOB */
   199    199   #define MEM_RowSet    0x0020   /* Value is a RowSet object */
   200    200   #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
   201         -#define MEM_Invalid   0x0080   /* Value is undefined */
          201  +#define MEM_Undefined 0x0080   /* Value is undefined */
   202    202   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
   203    203   #define MEM_TypeMask  0x01ff   /* Mask of type bits */
   204    204   
   205    205   
   206    206   /* Whenever Mem contains a valid string or blob representation, one of
   207    207   ** the following flags must be set to determine the memory management
   208    208   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
................................................................................
   226    226      ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
   227    227   
   228    228   /*
   229    229   ** Return true if a memory cell is not marked as invalid.  This macro
   230    230   ** is for use inside assert() statements only.
   231    231   */
   232    232   #ifdef SQLITE_DEBUG
   233         -#define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
          233  +#define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
   234    234   #endif
   235    235   
   236    236   /*
   237    237   ** Each auxilliary data pointer stored by a user defined function 
   238    238   ** implementation calling sqlite3_set_auxdata() is stored in an instance
   239    239   ** of this structure. All such structures associated with a single VM
   240    240   ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
................................................................................
   421    421   double sqlite3VdbeRealValue(Mem*);
   422    422   void sqlite3VdbeIntegerAffinity(Mem*);
   423    423   int sqlite3VdbeMemRealify(Mem*);
   424    424   int sqlite3VdbeMemNumerify(Mem*);
   425    425   int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
   426    426   void sqlite3VdbeMemRelease(Mem *p);
   427    427   void sqlite3VdbeMemReleaseExternal(Mem *p);
          428  +#define VdbeMemDynamic(X)  \
          429  +  (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
   428    430   #define VdbeMemRelease(X)  \
   429         -  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
   430         -    sqlite3VdbeMemReleaseExternal(X);
          431  +  if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
   431    432   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   432    433   const char *sqlite3OpcodeName(int);
   433    434   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   434    435   int sqlite3VdbeCloseStatement(Vdbe *, int);
   435    436   void sqlite3VdbeFrameDelete(VdbeFrame*);
   436    437   int sqlite3VdbeFrameRestore(VdbeFrame *);
   437    438   void sqlite3VdbeMemStoreType(Mem *pMem);

Changes to src/vdbeaux.c.

  1230   1230         if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
  1231   1231           sqlite3VdbeMemRelease(p);
  1232   1232         }else if( p->zMalloc ){
  1233   1233           sqlite3DbFree(db, p->zMalloc);
  1234   1234           p->zMalloc = 0;
  1235   1235         }
  1236   1236   
  1237         -      p->flags = MEM_Invalid;
         1237  +      p->flags = MEM_Undefined;
  1238   1238       }
  1239   1239       db->mallocFailed = malloc_failed;
  1240   1240     }
  1241   1241   }
  1242   1242   
  1243   1243   /*
  1244   1244   ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
................................................................................
  1698   1698       memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
  1699   1699       memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
  1700   1700     }
  1701   1701     if( p->aMem ){
  1702   1702       p->aMem--;                      /* aMem[] goes from 1..nMem */
  1703   1703       p->nMem = nMem;                 /*       not from 0..nMem-1 */
  1704   1704       for(n=1; n<=nMem; n++){
  1705         -      p->aMem[n].flags = MEM_Invalid;
         1705  +      p->aMem[n].flags = MEM_Undefined;
  1706   1706         p->aMem[n].db = db;
  1707   1707       }
  1708   1708     }
  1709   1709     p->explain = pParse->explain;
  1710   1710     sqlite3VdbeRewind(p);
  1711   1711   }
  1712   1712   
................................................................................
  1810   1810   
  1811   1811   #ifdef SQLITE_DEBUG
  1812   1812     /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 
  1813   1813     ** Vdbe.aMem[] arrays have already been cleaned up.  */
  1814   1814     int i;
  1815   1815     if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
  1816   1816     if( p->aMem ){
  1817         -    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
         1817  +    for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
  1818   1818     }
  1819   1819   #endif
  1820   1820   
  1821   1821     sqlite3DbFree(db, p->zErrMsg);
  1822   1822     p->zErrMsg = 0;
  1823   1823     p->pResultSet = 0;
  1824   1824   }

Changes to src/vdbemem.c.

   583    583   ** copies are not misused.
   584    584   */
   585    585   void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
   586    586     int i;
   587    587     Mem *pX;
   588    588     for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
   589    589       if( pX->pScopyFrom==pMem ){
   590         -      pX->flags |= MEM_Invalid;
          590  +      pX->flags |= MEM_Undefined;
   591    591         pX->pScopyFrom = 0;
   592    592       }
   593    593     }
   594    594     pMem->pScopyFrom = 0;
   595    595   }
   596    596   #endif /* SQLITE_DEBUG */
   597    597   

Changes to src/where.c.

  2781   2781       sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
  2782   2782       VdbeComment((v, "init LEFT JOIN no-match flag"));
  2783   2783     }
  2784   2784   
  2785   2785     /* Special case of a FROM clause subquery implemented as a co-routine */
  2786   2786     if( pTabItem->viaCoroutine ){
  2787   2787       int regYield = pTabItem->regReturn;
  2788         -    sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield);
  2789         -    pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
         2788  +    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
         2789  +    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
  2790   2790       VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
  2791         -    sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
  2792   2791       pLevel->op = OP_Goto;
  2793   2792     }else
  2794   2793   
  2795   2794   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2796   2795     if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  2797   2796       /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
  2798   2797       **          to access the data.