aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-15 16:17:33 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-15 16:17:33 -0300
commiteadf2aaaffa7a35e7f67b150ce0d57f2c17b9231 (patch)
tree281c96989a0f2bf9181551304ae8aa804935da04 /lvm.c
parentae19b2f51e21cef8ede07ce4e0b1546f2deefe91 (diff)
downloadlua-eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231.tar.gz
lua-eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231.tar.bz2
lua-eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231.zip
small optimizations
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c72
1 files changed, 40 insertions, 32 deletions
diff --git a/lvm.c b/lvm.c
index 5cadb252..3b17a0ad 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.183 2001/06/08 19:20:02 roberto Exp roberto $ 2** $Id: lvm.c,v 1.184 2001/06/11 14:56:42 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -106,17 +106,17 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
106} 106}
107 107
108 108
109static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { 109/* maximum stack used by a call to a tag method (func + args) */
110#define MAXSTACK_TM 4
111
112static StkId callTM (lua_State *L, Closure *f, const l_char *fmt, ...) {
110 va_list argp; 113 va_list argp;
111 StkId base = L->top; 114 StkId base = L->top;
112 StkId result = NULL; /* result position */ 115 lua_assert(strlen(fmt)+1 <= MAXSTACK_TM);
116 luaD_checkstack(L, MAXSTACK_TM);
113 va_start(argp, fmt); 117 va_start(argp, fmt);
114 setclvalue(L->top, f); /* push function */ 118 setclvalue(L->top, f); /* push function */
115 incr_top; 119 L->top++;
116 if (*fmt == l_c('r')) {
117 fmt++;
118 result = va_arg(argp, TObject *); /* result position */
119 }
120 while (*fmt) { 120 while (*fmt) {
121 if (*fmt++ == l_c('o')) { 121 if (*fmt++ == l_c('o')) {
122 setobj(L->top, va_arg(argp, TObject *)); 122 setobj(L->top, va_arg(argp, TObject *));
@@ -125,17 +125,24 @@ static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) {
125 lua_assert(*(fmt-1) == l_c('s')); 125 lua_assert(*(fmt-1) == l_c('s'));
126 setsvalue(L->top, va_arg(argp, TString *)); 126 setsvalue(L->top, va_arg(argp, TString *));
127 } 127 }
128 incr_top; 128 L->top++;
129 } 129 }
130 luaD_call(L, base); 130 luaD_call(L, base);
131 if (result) { /* need result? */ 131 va_end(argp);
132 if (L->top == base) /* are there valid results? */ 132 return base;
133 setnilvalue(result); /* function had no results */ 133}
134 else 134
135 setobj(result, base); /* get first result */ 135
136#define setTM(L, base) (L->top = (base))
137
138static void setTMresult (lua_State *L, TObject *result, StkId base) {
139 if (L->top == base) { /* are there valid results? */
140 setnilvalue(result); /* function had no results */
141 }
142 else {
143 setobj(result, base); /* get first result */
136 } 144 }
137 L->top = base; /* restore top */ 145 L->top = base; /* restore top */
138 va_end(argp);
139} 146}
140 147
141 148
@@ -164,7 +171,7 @@ void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) {
164 if (tm == NULL) /* no tag method? */ 171 if (tm == NULL) /* no tag method? */
165 luaG_typeerror(L, t, l_s("index")); 172 luaG_typeerror(L, t, l_s("index"));
166 } 173 }
167 callTM(L, tm, l_s("roo"), res, t, key); 174 setTMresult(L, res, callTM(L, tm, l_s("oo"), t, key));
168} 175}
169 176
170 177
@@ -187,7 +194,7 @@ void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) {
187 if (tm == NULL) /* no tag method? */ 194 if (tm == NULL) /* no tag method? */
188 luaG_typeerror(L, t, l_s("index")); 195 luaG_typeerror(L, t, l_s("index"));
189 } 196 }
190 callTM(L, tm, l_s("ooo"), t, key, val); 197 setTM(L, callTM(L, tm, l_s("ooo"), t, key, val));
191} 198}
192 199
193 200
@@ -197,8 +204,9 @@ void luaV_getglobal (lua_State *L, TString *name, StkId res) {
197 if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */ 204 if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */
198 (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { 205 (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) {
199 setobj(res, value); /* default behavior */ 206 setobj(res, value); /* default behavior */
200 } else 207 }
201 callTM(L, tm, l_s("rso"), res, name, value); 208 else
209 setTMresult(L, res, callTM(L, tm, l_s("so"), name, value));
202} 210}
203 211
204 212
@@ -208,8 +216,9 @@ void luaV_setglobal (lua_State *L, TString *name, StkId val) {
208 if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */ 216 if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */
209 (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { 217 (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) {
210 setobj(oldvalue, val); /* raw set */ 218 setobj(oldvalue, val); /* raw set */
211 } else 219 }
212 callTM(L, tm, l_s("soo"), name, oldvalue, val); 220 else
221 setTM(L, callTM(L, tm, l_s("soo"), name, oldvalue, val));
213} 222}
214 223
215 224
@@ -226,7 +235,7 @@ static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
226 } 235 }
227 } 236 }
228 opname = luaS_new(L, luaT_eventname[event]); 237 opname = luaS_new(L, luaT_eventname[event]);
229 callTM(L, tm, l_s("roos"), res, p1, p2, opname); 238 setTMresult(L, res, callTM(L, tm, l_s("oos"), p1, p2, opname));
230 return 1; 239 return 1;
231} 240}
232 241
@@ -566,15 +575,16 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
566 return ra; 575 return ra;
567 } 576 }
568 case OP_FORPREP: { 577 case OP_FORPREP: {
569 int jmp = GETARG_sBc(i);
570 if (luaV_tonumber(ra, ra) == NULL) 578 if (luaV_tonumber(ra, ra) == NULL)
571 luaD_error(L, l_s("`for' initial value must be a number")); 579 luaD_error(L, l_s("`for' initial value must be a number"));
572 if (luaV_tonumber(ra+1, ra+1) == NULL) 580 if (luaV_tonumber(ra+1, ra+1) == NULL)
573 luaD_error(L, l_s("`for' limit must be a number")); 581 luaD_error(L, l_s("`for' limit must be a number"));
574 if (luaV_tonumber(ra+2, ra+2) == NULL) 582 if (luaV_tonumber(ra+2, ra+2) == NULL)
575 luaD_error(L, l_s("`for' step must be a number")); 583 luaD_error(L, l_s("`for' step must be a number"));
576 pc += -jmp; /* `jump' to loop end (delta is negated here) */
577 nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */ 584 nvalue(ra) -= nvalue(ra+2);/* decrement index (to be incremented) */
585 pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */
586 /* store in `ra+1' total number of repetitions */
587 nvalue(ra+1) = ((nvalue(ra+1)-nvalue(ra))/nvalue(ra+2));
578 /* go through */ 588 /* go through */
579 } 589 }
580 case OP_FORLOOP: { 590 case OP_FORLOOP: {
@@ -582,28 +592,26 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
582 ttype(ra+2) == LUA_TNUMBER); 592 ttype(ra+2) == LUA_TNUMBER);
583 if (ttype(ra) != LUA_TNUMBER) 593 if (ttype(ra) != LUA_TNUMBER)
584 luaD_error(L, l_s("`for' index must be a number")); 594 luaD_error(L, l_s("`for' index must be a number"));
585 nvalue(ra) += nvalue(ra+2); /* increment index */ 595 if (--nvalue(ra+1) >= 0) {
586 if (nvalue(ra+2) > 0 ? 596 nvalue(ra) += nvalue(ra+2); /* increment index */
587 nvalue(ra) <= nvalue(ra+1) :
588 nvalue(ra) >= nvalue(ra+1))
589 dojump(pc, i); /* repeat loop */ 597 dojump(pc, i); /* repeat loop */
598 }
590 break; 599 break;
591 } 600 }
592 case OP_TFORPREP: { 601 case OP_TFORPREP: {
593 int jmp = GETARG_sBc(i);
594 if (ttype(ra) != LUA_TTABLE) 602 if (ttype(ra) != LUA_TTABLE)
595 luaD_error(L, l_s("`for' table must be a table")); 603 luaD_error(L, l_s("`for' table must be a table"));
596 setnvalue(ra+1, -1); /* initial index */ 604 setnvalue(ra+1, -1); /* initial index */
597 setnilvalue(ra+2); 605 setnilvalue(ra+2);
598 setnilvalue(ra+3); 606 setnilvalue(ra+3);
599 pc += -jmp; /* `jump' to loop end (delta is negated here) */ 607 pc += -GETARG_sBc(i); /* `jump' to loop end (delta is negated here) */
600 /* go through */ 608 /* go through */
601 } 609 }
602 case OP_TFORLOOP: { 610 case OP_TFORLOOP: {
603 Hash *t; 611 Hash *t;
604 int n; 612 int n;
605 runtime_check(L, ttype(ra) == LUA_TTABLE); 613 runtime_check(L, ttype(ra) == LUA_TTABLE &&
606 runtime_check(L, ttype(ra+1) == LUA_TNUMBER); 614 ttype(ra+1) == LUA_TNUMBER);
607 t = hvalue(ra); 615 t = hvalue(ra);
608 n = (int)nvalue(ra+1); 616 n = (int)nvalue(ra+1);
609 n = luaH_nexti(t, n); 617 n = luaH_nexti(t, n);