diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-01-19 14:50:30 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-01-19 14:50:30 -0200 |
| commit | 512b15b6012c14b0b58c4531078d27794a583f84 (patch) | |
| tree | 57618feea448e8a59975d96234af365072a5661e | |
| parent | 33d35048892fe745bc5b1f16ff7ff2baf5102c0d (diff) | |
| download | lua-512b15b6012c14b0b58c4531078d27794a583f84.tar.gz lua-512b15b6012c14b0b58c4531078d27794a583f84.tar.bz2 lua-512b15b6012c14b0b58c4531078d27794a583f84.zip | |
small optimizations (relational operators)
| -rw-r--r-- | lbuiltin.c | 9 | ||||
| -rw-r--r-- | lvm.c | 106 | ||||
| -rw-r--r-- | lvm.h | 5 |
3 files changed, 80 insertions, 40 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.90 1999/12/28 19:23:41 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.91 1999/12/30 18:27:03 roberto Exp roberto $ |
| 3 | ** Built-in functions | 3 | ** Built-in functions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -541,10 +541,9 @@ static int sort_comp (lua_State *L, lua_Object f, const TObject *a, | |||
| 541 | luaD_call(L, L->top-3, 1); | 541 | luaD_call(L, L->top-3, 1); |
| 542 | } | 542 | } |
| 543 | else { /* a < b? */ | 543 | else { /* a < b? */ |
| 544 | *(L->top) = *a; | 544 | *(L->top++) = *a; |
| 545 | *(L->top+1) = *b; | 545 | *(L->top++) = *b; |
| 546 | luaV_comparison(L, L->top+2, LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT); | 546 | luaV_comparison(L); |
| 547 | L->top++; /* result of comparison */ | ||
| 548 | } | 547 | } |
| 549 | return ttype(--(L->top)) != LUA_T_NIL; | 548 | return ttype(--(L->top)) != LUA_T_NIL; |
| 550 | } | 549 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.79 2000/01/13 15:56:03 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.80 2000/01/19 12:00:45 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 | */ |
| @@ -236,7 +236,11 @@ static void call_arith (lua_State *L, StkId top, IMS event) { | |||
| 236 | } | 236 | } |
| 237 | 237 | ||
| 238 | 238 | ||
| 239 | static int luaV_strcomp (const char *l, long ll, const char *r, long lr) { | 239 | static int luaV_strcomp (const TaggedString *ls, const TaggedString *rs) { |
| 240 | const char *l = ls->str; | ||
| 241 | long ll = ls->u.s.len; | ||
| 242 | const char *r = rs->str; | ||
| 243 | long lr = rs->u.s.len; | ||
| 240 | for (;;) { | 244 | for (;;) { |
| 241 | long temp = strcoll(l, r); | 245 | long temp = strcoll(l, r); |
| 242 | if (temp != 0) return temp; | 246 | if (temp != 0) return temp; |
| @@ -252,23 +256,25 @@ static int luaV_strcomp (const char *l, long ll, const char *r, long lr) { | |||
| 252 | } | 256 | } |
| 253 | } | 257 | } |
| 254 | 258 | ||
| 255 | void luaV_comparison (lua_State *L, StkId top, lua_Type ttype_less, | 259 | void luaV_comparison (lua_State *L) { |
| 256 | lua_Type ttype_equal, lua_Type ttype_great, IMS op) { | 260 | const TObject *l = L->top-2; |
| 257 | const TObject *l = top-2; | 261 | const TObject *r = L->top-1; |
| 258 | const TObject *r = top-1; | 262 | int result; |
| 259 | real result; | ||
| 260 | if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER) | 263 | if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER) |
| 261 | result = nvalue(l)-nvalue(r); | 264 | result = nvalue(l) < nvalue(r); |
| 262 | else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING) | 265 | else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING) |
| 263 | result = luaV_strcomp(svalue(l), tsvalue(l)->u.s.len, | 266 | result = luaV_strcomp(tsvalue(l), tsvalue(r)) < 0; |
| 264 | svalue(r), tsvalue(r)->u.s.len); | ||
| 265 | else { | 267 | else { |
| 266 | call_binTM(L, top, op, "unexpected type in comparison"); | 268 | call_binTM(L, L->top, IM_LT, "unexpected type in comparison"); |
| 267 | return; | 269 | return; |
| 268 | } | 270 | } |
| 269 | nvalue(top-2) = 1; | 271 | L->top--; |
| 270 | ttype(top-2) = (result < 0) ? ttype_less : | 272 | if (result) { |
| 271 | (result == 0) ? ttype_equal : ttype_great; | 273 | nvalue(L->top-1) = 1.0; |
| 274 | ttype(L->top-1) = LUA_T_NUMBER; | ||
| 275 | } | ||
| 276 | else | ||
| 277 | ttype(L->top-1) = LUA_T_NIL; | ||
| 272 | } | 278 | } |
| 273 | 279 | ||
| 274 | 280 | ||
| @@ -380,6 +386,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 380 | case GETGLOBALW: aux += highbyte(L, *pc++); | 386 | case GETGLOBALW: aux += highbyte(L, *pc++); |
| 381 | case GETGLOBAL: aux += *pc++; | 387 | case GETGLOBAL: aux += *pc++; |
| 382 | L->top = top; | 388 | L->top = top; |
| 389 | LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type"); | ||
| 383 | luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv); | 390 | luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv); |
| 384 | top++; | 391 | top++; |
| 385 | LUA_ASSERT(L, top==L->top, "top's not synchronized"); | 392 | LUA_ASSERT(L, top==L->top, "top's not synchronized"); |
| @@ -394,7 +401,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 394 | 401 | ||
| 395 | case GETDOTTEDW: aux += highbyte(L, *pc++); | 402 | case GETDOTTEDW: aux += highbyte(L, *pc++); |
| 396 | case GETDOTTED: aux += *pc++; | 403 | case GETDOTTED: aux += *pc++; |
| 397 | *top++ = consts[aux]; | 404 | LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type"); |
| 405 | ttype(top) = LUA_T_STRING; | ||
| 406 | tsvalue(top++) = tsvalue(&consts[aux]); | ||
| 398 | L->top = top; | 407 | L->top = top; |
| 399 | luaV_gettable(L); | 408 | luaV_gettable(L); |
| 400 | top--; | 409 | top--; |
| @@ -405,7 +414,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 405 | case PUSHSELF: aux += *pc++; { | 414 | case PUSHSELF: aux += *pc++; { |
| 406 | TObject receiver; | 415 | TObject receiver; |
| 407 | receiver = *(top-1); | 416 | receiver = *(top-1); |
| 408 | *top++ = consts[aux]; | 417 | LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type"); |
| 418 | ttype(top) = LUA_T_STRING; | ||
| 419 | tsvalue(top++) = tsvalue(&consts[aux]); | ||
| 409 | L->top = top; | 420 | L->top = top; |
| 410 | luaV_gettable(L); | 421 | luaV_gettable(L); |
| 411 | *(top-1) = receiver; | 422 | *(top-1) = receiver; |
| @@ -427,6 +438,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 427 | 438 | ||
| 428 | case SETGLOBALW: aux += highbyte(L, *pc++); | 439 | case SETGLOBALW: aux += highbyte(L, *pc++); |
| 429 | case SETGLOBAL: aux += *pc++; | 440 | case SETGLOBAL: aux += *pc++; |
| 441 | LUA_ASSERT(L, ttype(&consts[aux]) == LUA_T_STRING, "unexpected type"); | ||
| 430 | L->top = top; | 442 | L->top = top; |
| 431 | luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv); | 443 | luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv); |
| 432 | top--; | 444 | top--; |
| @@ -469,34 +481,64 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 469 | } | 481 | } |
| 470 | 482 | ||
| 471 | case NEQOP: aux = 1; | 483 | case NEQOP: aux = 1; |
| 472 | case EQOP: { | 484 | case EQOP: |
| 473 | int res = luaO_equalObj(top-2, top-1); | ||
| 474 | if (aux) res = !res; | ||
| 475 | top--; | 485 | top--; |
| 476 | ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL; | 486 | aux = (luaO_equalObj(top-1, top) != aux); |
| 477 | nvalue(top-1) = 1; | 487 | booleanresult: |
| 488 | if (aux) { | ||
| 489 | ttype(top-1) = LUA_T_NUMBER; | ||
| 490 | nvalue(top-1) = 1.0; | ||
| 491 | } | ||
| 492 | else ttype(top-1) = LUA_T_NIL; | ||
| 478 | break; | 493 | break; |
| 479 | } | ||
| 480 | 494 | ||
| 481 | case LTOP: | 495 | case LTOP: |
| 482 | luaV_comparison(L, top, LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT); | 496 | top--; |
| 483 | top--; | 497 | if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) |
| 484 | break; | 498 | aux = nvalue(top-1) < nvalue(top); |
| 499 | else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING) | ||
| 500 | aux = luaV_strcomp(tsvalue(top-1), tsvalue(top)) < 0; | ||
| 501 | else { | ||
| 502 | call_binTM(L, top+1, IM_LT, "unexpected type in comparison"); | ||
| 503 | break; | ||
| 504 | } | ||
| 505 | goto booleanresult; | ||
| 485 | 506 | ||
| 486 | case LEOP: | 507 | case LEOP: |
| 487 | luaV_comparison(L, top, LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE); | ||
| 488 | top--; | 508 | top--; |
| 489 | break; | 509 | if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) |
| 510 | aux = nvalue(top-1) <= nvalue(top); | ||
| 511 | else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING) | ||
| 512 | aux = luaV_strcomp(tsvalue(top-1), tsvalue(top)) <= 0; | ||
| 513 | else { | ||
| 514 | call_binTM(L, top+1, IM_LE, "unexpected type in comparison"); | ||
| 515 | break; | ||
| 516 | } | ||
| 517 | goto booleanresult; | ||
| 490 | 518 | ||
| 491 | case GTOP: | 519 | case GTOP: |
| 492 | luaV_comparison(L, top, LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT); | ||
| 493 | top--; | 520 | top--; |
| 494 | break; | 521 | if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) |
| 522 | aux = nvalue(top-1) > nvalue(top); | ||
| 523 | else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING) | ||
| 524 | aux = luaV_strcomp(tsvalue(top-1), tsvalue(top)) > 0; | ||
| 525 | else { | ||
| 526 | call_binTM(L, top+1, IM_GT, "unexpected type in comparison"); | ||
| 527 | break; | ||
| 528 | } | ||
| 529 | goto booleanresult; | ||
| 495 | 530 | ||
| 496 | case GEOP: | 531 | case GEOP: |
| 497 | luaV_comparison(L, top, LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE); | ||
| 498 | top--; | 532 | top--; |
| 499 | break; | 533 | if (ttype(top-1) == LUA_T_NUMBER && ttype(top) == LUA_T_NUMBER) |
| 534 | aux = nvalue(top-1) >= nvalue(top); | ||
| 535 | else if (ttype(top-1) == LUA_T_STRING && ttype(top) == LUA_T_STRING) | ||
| 536 | aux = luaV_strcomp(tsvalue(top-1), tsvalue(top)) >= 0; | ||
| 537 | else { | ||
| 538 | call_binTM(L, top+1, IM_GE, "unexpected type in comparison"); | ||
| 539 | break; | ||
| 540 | } | ||
| 541 | goto booleanresult; | ||
| 500 | 542 | ||
| 501 | case ADDOP: | 543 | case ADDOP: |
| 502 | if (tonumber(top-1) || tonumber(top-2)) | 544 | if (tonumber(top-1) || tonumber(top-2)) |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.h,v 1.12 1999/11/22 13:12:07 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.13 1999/12/01 19:50:08 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 | */ |
| @@ -28,7 +28,6 @@ void luaV_getglobal (lua_State *L, GlobalVar *gv); | |||
| 28 | void luaV_setglobal (lua_State *L, GlobalVar *gv); | 28 | void luaV_setglobal (lua_State *L, GlobalVar *gv); |
| 29 | StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base); | 29 | StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId base); |
| 30 | void luaV_closure (lua_State *L, int nelems); | 30 | void luaV_closure (lua_State *L, int nelems); |
| 31 | void luaV_comparison (lua_State *L, StkId top, lua_Type ttype_less, | 31 | void luaV_comparison (lua_State *L); |
| 32 | lua_Type ttype_equal, lua_Type ttype_great, IMS op); | ||
| 33 | 32 | ||
| 34 | #endif | 33 | #endif |
