From 17c402944adbc06d36b1b972a185f00066a2a22a Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 21 Jul 2004 02:53:29 +0000 Subject: [PATCH] Minor coding enhancements. (CVS 1839) FossilOrigin-Name: 65c3af74c16c9fe0341b1e7e6d029927503f7404 --- manifest | 14 ++--- manifest.uuid | 2 +- src/expr.c | 11 +--- src/vdbe.c | 160 +++++++++++++++++--------------------------------- 4 files changed, 66 insertions(+), 121 deletions(-) diff --git a/manifest b/manifest index 62aa72218f..ff2422192b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Simplify\sthe\swhere.c\slogic\sby\sflipping\sexpression\sover\sso\sthat\sthe\scontrolling\nvariable\sis\salways\son\sthe\sleft.\s(CVS\s1838) -D 2004-07-20T18:23:15 +C Minor\scoding\senhancements.\s(CVS\s1839) +D 2004-07-21T02:53:30 F Makefile.in 4a5e570a9e2d35b09c31b3cf01b78cea764ade4b F Makefile.linux-gcc a9e5a0d309fa7c38e7c14d3ecf7690879d3a5457 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd @@ -33,7 +33,7 @@ F src/build.c a4b1e80b13c570c7c962f500fa58045450b5a0cd F src/date.c e1bb384a7856c18dce9cadb0afbe6934ba5ddb00 F src/delete.c e81545e546f6bc87d7508a93a09ca70695265af3 F src/encode.c a876af473d1d636faa3dca51c7571f2e007eea37 -F src/expr.c df21bde644eca66276eafe0c97734fc8f930f4ab +F src/expr.c a4e8ac69c872f86bc207364be0e32d0638f61e90 F src/func.c b163fb49efec999eb7bf982f7de5b9be388301f3 F src/hash.c f0a2f22c2a7052d67053b5f4690ea3010bb3fb9f F src/hash.h 762d95f1e567664d1eafc1687de755626be962fb @@ -74,7 +74,7 @@ F src/update.c b66b1896c9da54678ba3eff2bf0b4d291a95986a F src/utf.c f03535db72bfa09e24202ccdd245f21d2fc65f0a F src/util.c 2aacc79b7bf5df5859813dafd3bf3258f67a5234 F src/vacuum.c b8546f4921719458cc537b9e736df52a8256399c -F src/vdbe.c a88514694be86eca792dd4d291e766f3c1831876 +F src/vdbe.c 5a7d1ee9d7356df6b53be64c1776824a33f6f2fb F src/vdbe.h 75b241c02431b9c0f16eaa9cdbb34146c6287f52 F src/vdbeInt.h 7160653a006b6d2c4a00d204112a095bdf842ab6 F src/vdbeapi.c c5c6d8f162a9581dde497b1a4034f9a0bf54c355 @@ -237,7 +237,7 @@ F www/tclsqlite.tcl 19191cf2a1010eaeff74c51d83fd5f5a4d899075 F www/vdbe.tcl 59288db1ac5c0616296b26dce071c36cb611dfe9 F www/version3.tcl 092a01f5ef430d2c4acc0ae558d74c4bb89638a0 F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4 -P 3c5aa850eeec5c75c5200a3707852cc5fc9e780b -R 85614d781cba6751b913b133092132cd +P ec8bfa3891dbf0f3172e31cf322974c03f9af59a +R 9e5a169fd77b5347506f033f7c0bac3e U drh -Z 550b1f0e24b844483d3dbfa3a8f89d17 +Z 16e953d85e2aa59a57853642a9ecf96b diff --git a/manifest.uuid b/manifest.uuid index 7c5cd294d7..1c23b86524 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ec8bfa3891dbf0f3172e31cf322974c03f9af59a \ No newline at end of file +65c3af74c16c9fe0341b1e7e6d029927503f7404 \ No newline at end of file diff --git a/src/expr.c b/src/expr.c index 81d4d8bed7..b7f612af2f 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.151 2004/07/19 00:39:45 drh Exp $ +** $Id: expr.c,v 1.152 2004/07/21 02:53:30 drh Exp $ */ #include "sqliteInt.h" #include @@ -1167,16 +1167,11 @@ void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ case TK_REM: case TK_BITAND: case TK_BITOR: - case TK_SLASH: { - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3ExprCode(pParse, pExpr->pRight); - sqlite3VdbeAddOp(v, op, 0, 0); - break; - } + case TK_SLASH: case TK_LSHIFT: case TK_RSHIFT: { - sqlite3ExprCode(pParse, pExpr->pRight); sqlite3ExprCode(pParse, pExpr->pLeft); + sqlite3ExprCode(pParse, pExpr->pRight); sqlite3VdbeAddOp(v, op, 0, 0); break; } diff --git a/src/vdbe.c b/src/vdbe.c index b085fe7af1..beeae2d7c7 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -43,7 +43,7 @@ ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.402 2004/07/19 17:25:25 drh Exp $ +** $Id: vdbe.c,v 1.403 2004/07/21 02:53:30 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -290,54 +290,43 @@ static Cursor *allocateCursor(Vdbe *p, int iCur){ ** */ static void applyAffinity(Mem *pRec, char affinity, u8 enc){ - switch( affinity ){ - case SQLITE_AFF_INTEGER: - case SQLITE_AFF_NUMERIC: - if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){ - /* pRec does not have a valid integer or real representation. - ** Attempt a conversion if pRec has a string representation and - ** it looks like a number. - */ - int realnum; - sqlite3VdbeMemNulTerminate(pRec); - if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum, enc) ){ - if( realnum ){ - Realify(pRec); - }else{ - Integerify(pRec); - } + if( affinity==SQLITE_AFF_NONE ){ + /* do nothing */ + }else if( affinity==SQLITE_AFF_TEXT ){ + /* Only attempt the conversion to TEXT if there is an integer or real + ** representation (blob and NULL do not get converted) but no string + ** representation. + */ + if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ + sqlite3VdbeMemStringify(pRec, enc); + } + pRec->flags &= ~(MEM_Real|MEM_Int); + }else{ + if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){ + /* pRec does not have a valid integer or real representation. + ** Attempt a conversion if pRec has a string representation and + ** it looks like a number. + */ + int realnum; + sqlite3VdbeMemNulTerminate(pRec); + if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum, enc) ){ + if( realnum ){ + Realify(pRec); + }else{ + Integerify(pRec); } } + } - if( affinity==SQLITE_AFF_INTEGER ){ - /* For INTEGER affinity, try to convert a real value to an int */ - if( (pRec->flags&MEM_Real) && !(pRec->flags&MEM_Int) ){ - pRec->i = pRec->r; - if( ((double)pRec->i)==pRec->r ){ - pRec->flags |= MEM_Int; - } + if( affinity==SQLITE_AFF_INTEGER ){ + /* For INTEGER affinity, try to convert a real value to an int */ + if( (pRec->flags&MEM_Real) && !(pRec->flags&MEM_Int) ){ + pRec->i = pRec->r; + if( ((double)pRec->i)==pRec->r ){ + pRec->flags |= MEM_Int; } } - break; - - case SQLITE_AFF_TEXT: - /* Only attempt the conversion if there is an integer or real - ** representation (blob and NULL do not get converted) but no string - ** representation. - */ - if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ - sqlite3VdbeMemStringify(pRec, enc); - } - pRec->flags &= ~(MEM_Real|MEM_Int); - - break; - - case SQLITE_AFF_NONE: - /* Affinity NONE. Do nothing. */ - break; - - default: - assert(0); + } } } @@ -608,11 +597,7 @@ case OP_Goto: { ** with a fatal error. */ case OP_Gosub: { - if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){ - sqlite3SetString(&p->zErrMsg, "return address stack overflow", (char*)0); - p->rc = SQLITE_INTERNAL; - return SQLITE_ERROR; - } + assert( p->returnDepthreturnStack)/sizeof(p->returnStack[0]) ); p->returnStack[p->returnDepth++] = pc+1; pc = pOp->p2 - 1; break; @@ -625,11 +610,7 @@ case OP_Gosub: { ** processing aborts with a fatal error. */ case OP_Return: { - if( p->returnDepth<=0 ){ - sqlite3SetString(&p->zErrMsg, "return address stack underflow", (char*)0); - p->rc = SQLITE_INTERNAL; - return SQLITE_ERROR; - } + assert( p->returnDepth>0 ); p->returnDepth--; pc = p->returnStack[p->returnDepth] - 1; break; @@ -658,9 +639,7 @@ case OP_Halt: { if( pOp->p1!=SQLITE_OK ){ p->rc = pOp->p1; p->errorAction = pOp->p2; - if( pOp->p3 ){ - sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); - } + sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); return SQLITE_ERROR; }else{ p->rc = SQLITE_OK; @@ -813,6 +792,7 @@ case OP_Variable: { assert( j>=0 && jnVar ); pTos++; + /* sqlite3VdbeMemCopyStatic(pTos, &p->apVar[j]); */ memcpy(pTos, &p->apVar[j], sizeof(*pTos)-NBFS); pTos->xDel = 0; if( pTos->flags&(MEM_Str|MEM_Blob) ){ @@ -1297,15 +1277,15 @@ case OP_Function: { /* Opcode: ShiftLeft * * * ** ** Pop the top two elements from the stack. Convert both elements -** to integers. Push back onto the stack the top element shifted -** left by N bits where N is the second element on the stack. +** to integers. Push back onto the stack the second element shifted +** left by N bits where N is the top element on the stack. ** If either operand is NULL, the result is NULL. */ /* Opcode: ShiftRight * * * ** ** Pop the top two elements from the stack. Convert both elements -** to integers. Push back onto the stack the top element shifted -** right by N bits where N is the second element on the stack. +** to integers. Push back onto the stack the second element shifted +** right by N bits where N is the top element on the stack. ** If either operand is NULL, the result is NULL. */ case OP_BitAnd: @@ -1322,8 +1302,8 @@ case OP_ShiftRight: { pTos->flags = MEM_Null; break; } - a = sqlite3VdbeIntValue(pTos); - b = sqlite3VdbeIntValue(pNos); + a = sqlite3VdbeIntValue(pNos); + b = sqlite3VdbeIntValue(pTos); switch( pOp->opcode ){ case OP_BitAnd: a &= b; break; case OP_BitOr: a |= b; break; @@ -1366,8 +1346,8 @@ case OP_AddImm: { case OP_ForceInt: { int v; assert( pTos>=p->aStack ); - if( (pTos->flags & (MEM_Int|MEM_Real))==0 && ((pTos->flags & MEM_Str)==0 - || sqlite3IsNumber(pTos->z, 0, db->enc)==0) ){ + applyAffinity(pTos, SQLITE_AFF_INTEGER, db->enc); + if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){ Release(pTos); pTos--; pc = pOp->p2 - 1; @@ -1400,48 +1380,18 @@ case OP_ForceInt: { */ case OP_MustBeInt: { assert( pTos>=p->aStack ); - if( pTos->flags & MEM_Int ){ - /* Do nothing */ - }else if( pTos->flags & MEM_Real ){ - int i = (int)pTos->r; - double r = (double)i; - if( r!=pTos->r ){ - goto mismatch; - } - pTos->i = i; - }else if( pTos->flags & MEM_Str ){ - i64 v; - if( sqlite3VdbeChangeEncoding(pTos, SQLITE_UTF8) - || sqlite3VdbeMemNulTerminate(pTos) ){ - goto no_mem; - } - if( !sqlite3atoi64(pTos->z, &v) ){ - double r; - if( !sqlite3IsNumber(pTos->z, 0, SQLITE_UTF8) ){ - goto mismatch; - } - Realify(pTos); - v = (int)pTos->r; - r = (double)v; - if( r!=pTos->r ){ - goto mismatch; - } + applyAffinity(pTos, SQLITE_AFF_INTEGER, db->enc); + if( (pTos->flags & MEM_Int)==0 ){ + if( pOp->p2==0 ){ + rc = SQLITE_MISMATCH; + goto abort_due_to_error; + }else{ + if( pOp->p1 ) popStack(&pTos, 1); + pc = pOp->p2 - 1; } - pTos->i = v; - }else{ - goto mismatch; - } - Release(pTos); - pTos->flags = MEM_Int; - break; - -mismatch: - if( pOp->p2==0 ){ - rc = SQLITE_MISMATCH; - goto abort_due_to_error; }else{ - if( pOp->p1 ) popStack(&pTos, 1); - pc = pOp->p2 - 1; + Release(pTos); + pTos->flags = MEM_Int; } break; } -- 2.47.3