General code cleanup resulting in smaller footprint. (CVS 2595)

FossilOrigin-Name: 98338abf9e8cfbf8efa81cff0e40ea37e34fd9b2
diff --git a/src/expr.c b/src/expr.c
index 4bab5ea..a7a8f33 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -12,7 +12,7 @@
 ** This file contains routines used for analyzing expressions and
 ** for generating VDBE code that evaluates expressions in SQLite.
 **
-** $Id: expr.c,v 1.216 2005/08/12 22:58:53 drh Exp $
+** $Id: expr.c,v 1.217 2005/08/14 20:47:16 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -1789,11 +1789,9 @@
 ){
   struct ExprList_item *pItem;
   int i, n;
-  Vdbe *v;
   if( pList==0 ) return 0;
-  v = sqlite3GetVdbe(pParse);
   n = pList->nExpr;
-  for(pItem=pList->a, i=0; i<n; i++, pItem++){
+  for(pItem=pList->a, i=n; i>0; i--, pItem++){
     sqlite3ExprCode(pParse, pItem->pExpr);
   }
   return n;
diff --git a/src/select.c b/src/select.c
index dd09445..ba633d6 100644
--- a/src/select.c
+++ b/src/select.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.253 2005/07/08 17:13:47 drh Exp $
+** $Id: select.c,v 1.254 2005/08/14 20:47:16 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -85,7 +85,7 @@
   Token *apAll[3];
   Token *p;
   static const struct {
-    const char *zKeyword;
+    const char zKeyword[8];
     u8 nChar;
     u8 code;
   } keywords[] = {
@@ -155,6 +155,15 @@
   p->dyn = 0;
 }
 
+/*
+** Create an expression node for an identifier with the name of zName
+*/
+static Expr *createIdExpr(const char *zName){
+  Token dummy;
+  setToken(&dummy, zName);
+  return sqlite3Expr(TK_ID, 0, 0, &dummy);
+}
+
 
 /*
 ** Add a term to the WHERE expression in *ppExpr that requires the
@@ -168,24 +177,20 @@
   const char *zAlias2,     /* Alias for second table.  May be NULL */
   Expr **ppExpr            /* Add the equality term to this expression */
 ){
-  Token dummy;
   Expr *pE1a, *pE1b, *pE1c;
   Expr *pE2a, *pE2b, *pE2c;
   Expr *pE;
 
-  setToken(&dummy, zCol);
-  pE1a = sqlite3Expr(TK_ID, 0, 0, &dummy);
-  pE2a = sqlite3Expr(TK_ID, 0, 0, &dummy);
+  pE1a = createIdExpr(zCol);
+  pE2a = createIdExpr(zCol);
   if( zAlias1==0 ){
     zAlias1 = pTab1->zName;
   }
-  setToken(&dummy, zAlias1);
-  pE1b = sqlite3Expr(TK_ID, 0, 0, &dummy);
+  pE1b = createIdExpr(zAlias1);
   if( zAlias2==0 ){
     zAlias2 = pTab2->zName;
   }
-  setToken(&dummy, zAlias2);
-  pE2b = sqlite3Expr(TK_ID, 0, 0, &dummy);
+  pE2b = createIdExpr(zAlias2);
   pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0);
   pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0);
   pE = sqlite3Expr(TK_EQ, pE1c, pE2c, 0);
@@ -321,10 +326,7 @@
 ** stack into the sorter.
 */
 static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
-  int i;
-  for(i=0; i<pOrderBy->nExpr; i++){
-    sqlite3ExprCode(pParse, pOrderBy->a[i].pExpr);
-  }
+  sqlite3ExprCodeExprList(pParse, pOrderBy);
   sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr, 0);
   sqlite3VdbeAddOp(v, OP_SortInsert, 0, 0);
 }
@@ -402,9 +404,7 @@
     }
   }else{
     nColumn = pEList->nExpr;
-    for(i=0; i<pEList->nExpr; i++){
-      sqlite3ExprCode(pParse, pEList->a[i].pExpr);
-    }
+    sqlite3ExprCodeExprList(pParse, pEList);
   }
 
   /* If the DISTINCT keyword was present on the SELECT statement
@@ -412,14 +412,15 @@
   ** part of the result.
   */
   if( hasDistinct ){
+    int n = pEList->nExpr;
 #if NULL_ALWAYS_DISTINCT
     sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7);
 #endif
     /* Deliberately leave the affinity string off of the following
     ** OP_MakeRecord */
-    sqlite3VdbeAddOp(v, OP_MakeRecord, pEList->nExpr * -1, 0);
+    sqlite3VdbeAddOp(v, OP_MakeRecord, -n, 0);
     sqlite3VdbeAddOp(v, OP_Distinct, distinct, sqlite3VdbeCurrentAddr(v)+3);
-    sqlite3VdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
+    sqlite3VdbeAddOp(v, OP_Pop, n+1, 0);
     sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
     VdbeComment((v, "# skip indistinct records"));
     sqlite3VdbeAddOp(v, OP_IdxInsert, distinct, 0);
@@ -511,29 +512,21 @@
     }
 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
 
-    /* Send the data to the callback function.
+    /* Send the data to the callback function or to a subroutine.  In the
+    ** case of a subroutine, the subroutine itself is responsible for
+    ** popping the data from the stack.
     */
+    case SRT_Subroutine:
     case SRT_Callback:
     case SRT_Sorter: {
       if( pOrderBy ){
         sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
         pushOntoSorter(pParse, v, pOrderBy);
-      }else{
-        assert( eDest==SRT_Callback );
-        sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
-      }
-      break;
-    }
-
-    /* Invoke a subroutine to handle the results.  The subroutine itself
-    ** is responsible for popping the results off of the stack.
-    */
-    case SRT_Subroutine: {
-      if( pOrderBy ){
-        sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
-        pushOntoSorter(pParse, v, pOrderBy);
-      }else{
+      }else if( eDest==SRT_Subroutine ){
         sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
+      }else{
+        assert( eDest!=SRT_Sorter );
+        sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
       }
       break;
     }
@@ -2766,9 +2759,7 @@
     int lbl1 = 0;
     pParse->fillAgg = 1;
     if( pGroupBy ){
-      for(i=0; i<pGroupBy->nExpr; i++){
-        sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr);
-      }
+      sqlite3ExprCodeExprList(pParse, pGroupBy);
       /* No affinity string is attached to the following OP_MakeRecord 
       ** because we do not need to do any coercion of datatypes. */
       sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0);