diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-06 12:51:41 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-06 12:51:41 -0300 |
| commit | 0dbf0c5953a3d72deebc7e41840a0e73b46de8bc (patch) | |
| tree | b4a525660cba017d7985c37393f50d6b8bee9e4b | |
| parent | 85dcb411a8454de0bc1c2c60a24af1588e436c23 (diff) | |
| download | lua-0dbf0c5953a3d72deebc7e41840a0e73b46de8bc.tar.gz lua-0dbf0c5953a3d72deebc7e41840a0e73b46de8bc.tar.bz2 lua-0dbf0c5953a3d72deebc7e41840a0e73b46de8bc.zip | |
new format for test intructions (handle NaN correctly)
| -rw-r--r-- | lapi.c | 4 | ||||
| -rw-r--r-- | lcode.c | 148 | ||||
| -rw-r--r-- | lobject.c | 8 | ||||
| -rw-r--r-- | lobject.h | 11 | ||||
| -rw-r--r-- | lopcodes.c | 24 | ||||
| -rw-r--r-- | lopcodes.h | 26 | ||||
| -rw-r--r-- | lvm.c | 68 | ||||
| -rw-r--r-- | lvm.h | 4 |
8 files changed, 147 insertions, 146 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.186 2002/05/01 20:48:12 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.187 2002/05/02 16:55:55 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -231,7 +231,7 @@ LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { | |||
| 231 | o1 = luaA_indexAcceptable(L, index1); | 231 | o1 = luaA_indexAcceptable(L, index1); |
| 232 | o2 = luaA_indexAcceptable(L, index2); | 232 | o2 = luaA_indexAcceptable(L, index2); |
| 233 | i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */ | 233 | i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */ |
| 234 | : luaV_lessthan(L, o1, o2); | 234 | : luaV_cmp(L, o1, o2, CMP_LT); |
| 235 | lua_unlock(L); | 235 | lua_unlock(L); |
| 236 | return i; | 236 | return i; |
| 237 | } | 237 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 1.96 2002/04/22 14:37:09 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 1.97 2002/04/24 20:07:46 roberto Exp roberto $ |
| 3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -107,16 +107,21 @@ static Instruction *getjumpcontrol (FuncState *fs, int pc) { | |||
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | 109 | ||
| 110 | static int need_value (FuncState *fs, int list, OpCode op) { | 110 | /* |
| 111 | /* check whether list has any jump different from `op' */ | 111 | ** check whether list has any jump that do not produce a value |
| 112 | for (; list != NO_JUMP; list = luaK_getjump(fs, list)) | 112 | ** (or produce an inverted value) |
| 113 | if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1; | 113 | */ |
| 114 | static int need_value (FuncState *fs, int list, int cond) { | ||
| 115 | for (; list != NO_JUMP; list = luaK_getjump(fs, list)) { | ||
| 116 | Instruction i = *getjumpcontrol(fs, list); | ||
| 117 | if (GET_OPCODE(i) != OP_TEST || GETARG_B(i) != cond) return 1; | ||
| 118 | } | ||
| 114 | return 0; /* not found */ | 119 | return 0; /* not found */ |
| 115 | } | 120 | } |
| 116 | 121 | ||
| 117 | 122 | ||
| 118 | static void patchtestreg (Instruction *i, int reg) { | 123 | static void patchtestreg (Instruction *i, int reg) { |
| 119 | if (reg == NO_REG) reg = GETARG_B(*i); | 124 | if (reg == NO_REG) reg = GETARG_C(*i); |
| 120 | SETARG_A(*i, reg); | 125 | SETARG_A(*i, reg); |
| 121 | } | 126 | } |
| 122 | 127 | ||
| @@ -126,20 +131,20 @@ static void luaK_patchlistaux (FuncState *fs, int list, | |||
| 126 | while (list != NO_JUMP) { | 131 | while (list != NO_JUMP) { |
| 127 | int next = luaK_getjump(fs, list); | 132 | int next = luaK_getjump(fs, list); |
| 128 | Instruction *i = getjumpcontrol(fs, list); | 133 | Instruction *i = getjumpcontrol(fs, list); |
| 129 | switch (GET_OPCODE(*i)) { | 134 | if (GET_OPCODE(*i) != OP_TEST) { |
| 130 | case OP_TESTT: { | 135 | lua_assert(dtarget != NO_JUMP); |
| 136 | luaK_fixjump(fs, list, dtarget); /* jump to default target */ | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | if (GETARG_B(*i)) { | ||
| 140 | lua_assert(ttarget != NO_JUMP); | ||
| 131 | patchtestreg(i, treg); | 141 | patchtestreg(i, treg); |
| 132 | luaK_fixjump(fs, list, ttarget); | 142 | luaK_fixjump(fs, list, ttarget); |
| 133 | break; | ||
| 134 | } | 143 | } |
| 135 | case OP_TESTF: { | 144 | else { |
| 145 | lua_assert(ftarget != NO_JUMP); | ||
| 136 | patchtestreg(i, freg); | 146 | patchtestreg(i, freg); |
| 137 | luaK_fixjump(fs, list, ftarget); | 147 | luaK_fixjump(fs, list, ftarget); |
| 138 | break; | ||
| 139 | } | ||
| 140 | default: { | ||
| 141 | luaK_fixjump(fs, list, dtarget); /* jump to default target */ | ||
| 142 | break; | ||
| 143 | } | 148 | } |
| 144 | } | 149 | } |
| 145 | list = next; | 150 | list = next; |
| @@ -342,9 +347,8 @@ static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) { | |||
| 342 | int final; /* position after whole expression */ | 347 | int final; /* position after whole expression */ |
| 343 | int p_f = NO_JUMP; /* position of an eventual PUSH false */ | 348 | int p_f = NO_JUMP; /* position of an eventual PUSH false */ |
| 344 | int p_t = NO_JUMP; /* position of an eventual PUSH true */ | 349 | int p_t = NO_JUMP; /* position of an eventual PUSH true */ |
| 345 | if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) || | 350 | if (e->k == VJMP || need_value(fs, e->t, 1) |
| 346 | need_value(fs, e->t, OP_TESTT)) { | 351 | || need_value(fs, e->f, 0)) { |
| 347 | /* expression needs values */ | ||
| 348 | if (e->k != VJMP) { | 352 | if (e->k != VJMP) { |
| 349 | luaK_getlabel(fs); /* these instruction may be jump target */ | 353 | luaK_getlabel(fs); /* these instruction may be jump target */ |
| 350 | luaK_codeAsBx(fs, OP_JMP, 0, 2); /* to jump over both pushes */ | 354 | luaK_codeAsBx(fs, OP_JMP, 0, 2); /* to jump over both pushes */ |
| @@ -463,40 +467,36 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { | |||
| 463 | } | 467 | } |
| 464 | 468 | ||
| 465 | 469 | ||
| 466 | static OpCode invertoperator (OpCode op) { | ||
| 467 | switch (op) { | ||
| 468 | case OP_TESTNE: return OP_TESTEQ; | ||
| 469 | case OP_TESTEQ: return OP_TESTNE; | ||
| 470 | case OP_TESTLT: return OP_TESTGE; | ||
| 471 | case OP_TESTLE: return OP_TESTGT; | ||
| 472 | case OP_TESTGT: return OP_TESTLE; | ||
| 473 | case OP_TESTGE: return OP_TESTLT; | ||
| 474 | case OP_TESTT: return OP_TESTF; | ||
| 475 | case OP_TESTF: return OP_TESTT; | ||
| 476 | default: lua_assert(0); return op; /* invalid jump instruction */ | ||
| 477 | } | ||
| 478 | } | ||
| 479 | |||
| 480 | |||
| 481 | static void invertjump (FuncState *fs, expdesc *e) { | 470 | static void invertjump (FuncState *fs, expdesc *e) { |
| 482 | Instruction *pc = getjumpcontrol(fs, e->info); | 471 | Instruction *pc = getjumpcontrol(fs, e->info); |
| 483 | SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc))); | 472 | OpCode op = GET_OPCODE(*pc); |
| 473 | switch (op) { | ||
| 474 | case OP_EQ: { | ||
| 475 | SETARG_B(*pc, !(GETARG_B(*pc))); | ||
| 476 | return; | ||
| 477 | } | ||
| 478 | case OP_CMP: { | ||
| 479 | SETARG_B(*pc, ~(GETARG_B(*pc))); | ||
| 480 | return; | ||
| 481 | } | ||
| 482 | default: lua_assert(0); /* invalid jump instruction */ | ||
| 483 | } | ||
| 484 | SET_OPCODE(*pc, op); | ||
| 484 | } | 485 | } |
| 485 | 486 | ||
| 486 | 487 | ||
| 487 | static int jumponcond (FuncState *fs, expdesc *e, OpCode op) { | 488 | static int jumponcond (FuncState *fs, expdesc *e, int cond) { |
| 488 | if (e->k == VRELOCABLE) { | 489 | if (e->k == VRELOCABLE) { |
| 489 | Instruction ie = getcode(fs, e); | 490 | Instruction ie = getcode(fs, e); |
| 490 | if (GET_OPCODE(ie) == OP_NOT) { | 491 | if (GET_OPCODE(ie) == OP_NOT) { |
| 491 | op = invertoperator(op); | ||
| 492 | fs->pc--; /* remove previous OP_NOT */ | 492 | fs->pc--; /* remove previous OP_NOT */ |
| 493 | return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0); | 493 | return luaK_condjump(fs, OP_TEST, NO_REG, !cond ,GETARG_B(ie)); |
| 494 | } | 494 | } |
| 495 | /* else go through */ | 495 | /* else go through */ |
| 496 | } | 496 | } |
| 497 | discharge2anyreg(fs, e); | 497 | discharge2anyreg(fs, e); |
| 498 | freeexp(fs, e); | 498 | freeexp(fs, e); |
| 499 | return luaK_condjump(fs, op, NO_REG, e->info, 0); | 499 | return luaK_condjump(fs, OP_TEST, NO_REG, cond, e->info); |
| 500 | } | 500 | } |
| 501 | 501 | ||
| 502 | 502 | ||
| @@ -518,7 +518,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { | |||
| 518 | break; | 518 | break; |
| 519 | } | 519 | } |
| 520 | default: { | 520 | default: { |
| 521 | pc = jumponcond(fs, e, OP_TESTF); | 521 | pc = jumponcond(fs, e, 0); |
| 522 | break; | 522 | break; |
| 523 | } | 523 | } |
| 524 | } | 524 | } |
| @@ -545,7 +545,7 @@ static void luaK_goiffalse (FuncState *fs, expdesc *e) { | |||
| 545 | break; | 545 | break; |
| 546 | } | 546 | } |
| 547 | default: { | 547 | default: { |
| 548 | pc = jumponcond(fs, e, OP_TESTT); | 548 | pc = jumponcond(fs, e, 1); |
| 549 | break; | 549 | break; |
| 550 | } | 550 | } |
| 551 | } | 551 | } |
| @@ -639,23 +639,46 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { | |||
| 639 | 639 | ||
| 640 | 640 | ||
| 641 | 641 | ||
| 642 | /* opcode for each binary operator */ | 642 | static const OpCode cmp_masks[] = { /* ORDER OPR */ |
| 643 | static const OpCode codes[] = { /* ORDER OPR */ | 643 | CMP_LT, (CMP_LT | CMP_EQ), CMP_GT, (CMP_GT | CMP_EQ) |
| 644 | OP_ADD, OP_SUB, OP_MUL, OP_DIV, | ||
| 645 | OP_POW, OP_CONCAT, | ||
| 646 | OP_TESTNE, OP_TESTEQ, | ||
| 647 | OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE | ||
| 648 | }; | 644 | }; |
| 649 | 645 | ||
| 650 | 646 | ||
| 651 | /* `inverted' opcode for each binary operator */ | 647 | static void codebinop (FuncState *fs, expdesc *res, BinOpr op, |
| 652 | /* ( -1 means operator has no inverse) */ | 648 | int o1, int o2, int ic) { |
| 653 | static const OpCode invcodes[] = { /* ORDER OPR */ | 649 | switch (op) { |
| 654 | OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1, | 650 | case OPR_SUB: |
| 655 | (OpCode)-1, (OpCode)-1, | 651 | case OPR_DIV: |
| 656 | OP_TESTNE, OP_TESTEQ, | 652 | case OPR_POW: |
| 657 | OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE | 653 | lua_assert(!ic); |
| 658 | }; | 654 | /* go through */ |
| 655 | case OPR_ADD: | ||
| 656 | case OPR_MULT: { | ||
| 657 | OpCode opc = cast(OpCode, (op - OPR_ADD) + OP_ADD); | ||
| 658 | res->info = luaK_codeABC(fs, opc, 0, o1, o2); | ||
| 659 | res->k = VRELOCABLE; | ||
| 660 | break; | ||
| 661 | } | ||
| 662 | case OPR_NE: | ||
| 663 | case OPR_EQ: { | ||
| 664 | res->info = luaK_condjump(fs, OP_EQ, o1, (op == OPR_EQ), o2); | ||
| 665 | res->k = VJMP; | ||
| 666 | break; | ||
| 667 | } | ||
| 668 | case OPR_LT: | ||
| 669 | case OPR_LE: | ||
| 670 | case OPR_GT: | ||
| 671 | case OPR_GE: { | ||
| 672 | int mask = cmp_masks[op - OPR_LT]; | ||
| 673 | if (ic) /* operands were interchanged? */ | ||
| 674 | mask ^= (CMP_LT | CMP_GT); /* correct condition */ | ||
| 675 | res->info = luaK_condjump(fs, OP_CMP, o1, mask, o2); | ||
| 676 | res->k = VJMP; | ||
| 677 | break; | ||
| 678 | } | ||
| 679 | default: lua_assert(0); | ||
| 680 | } | ||
| 681 | } | ||
| 659 | 682 | ||
| 660 | 683 | ||
| 661 | void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { | 684 | void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { |
| @@ -693,27 +716,20 @@ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { | |||
| 693 | } | 716 | } |
| 694 | default: { | 717 | default: { |
| 695 | int o1, o2; | 718 | int o1, o2; |
| 696 | OpCode opc; | 719 | int ic; /* interchange flag */ |
| 697 | if (e1->k != VK) { /* not a constant operator? */ | 720 | if (e1->k != VK) { /* not a constant operator? */ |
| 698 | o1 = e1->info; | 721 | o1 = e1->info; |
| 699 | o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */ | 722 | o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */ |
| 700 | opc = codes[op]; | 723 | ic = 0; |
| 701 | } | 724 | } |
| 702 | else { /* invert operands */ | 725 | else { /* interchange operands */ |
| 703 | o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */ | 726 | o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */ |
| 704 | o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */ | 727 | o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */ |
| 705 | opc = invcodes[op]; /* use inverted operator */ | 728 | ic = 1; |
| 706 | } | 729 | } |
| 707 | freeexp(fs, e2); | 730 | freeexp(fs, e2); |
| 708 | freeexp(fs, e1); | 731 | freeexp(fs, e1); |
| 709 | if (op < OPR_NE) { /* ORDER OPR */ | 732 | codebinop(fs, e1, op, o1, o2, ic); |
| 710 | e1->info = luaK_codeABC(fs, opc, 0, o1, o2); | ||
| 711 | e1->k = VRELOCABLE; | ||
| 712 | } | ||
| 713 | else { /* jump */ | ||
| 714 | e1->info = luaK_condjump(fs, opc, o1, 0, o2); | ||
| 715 | e1->k = VJMP; | ||
| 716 | } | ||
| 717 | } | 733 | } |
| 718 | } | 734 | } |
| 719 | } | 735 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 1.76 2002/04/05 18:54:31 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 1.77 2002/04/22 14:40:23 roberto Exp roberto $ |
| 3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -49,7 +49,9 @@ int luaO_log2 (unsigned int x) { | |||
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | 51 | ||
| 52 | 52 | /* | |
| 53 | ** warning: this function must return 1 for true (see opcode OP_TESTEQ) | ||
| 54 | */ | ||
| 53 | int luaO_equalObj (const TObject *t1, const TObject *t2) { | 55 | int luaO_equalObj (const TObject *t1, const TObject *t2) { |
| 54 | if (ttype(t1) != ttype(t2)) return 0; | 56 | if (ttype(t1) != ttype(t2)) return 0; |
| 55 | switch (ttype(t1)) { | 57 | switch (ttype(t1)) { |
| @@ -58,7 +60,7 @@ int luaO_equalObj (const TObject *t1, const TObject *t2) { | |||
| 58 | case LUA_TNIL: | 60 | case LUA_TNIL: |
| 59 | return 1; | 61 | return 1; |
| 60 | case LUA_TBOOLEAN: | 62 | case LUA_TBOOLEAN: |
| 61 | return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ | 63 | return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ |
| 62 | case LUA_TUDATAVAL: | 64 | case LUA_TUDATAVAL: |
| 63 | return pvalue(t1) == pvalue(t2); | 65 | return pvalue(t1) == pvalue(t2); |
| 64 | default: /* other types are equal if struct pointers are equal */ | 66 | default: /* other types are equal if struct pointers are equal */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 1.128 2002/03/25 17:47:14 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 1.129 2002/04/05 18:54:31 roberto Exp roberto $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -230,6 +230,15 @@ typedef struct Table { | |||
| 230 | #define sizearray(t) ((t)->sizearray) | 230 | #define sizearray(t) ((t)->sizearray) |
| 231 | 231 | ||
| 232 | 232 | ||
| 233 | /* | ||
| 234 | ** masks for comparison results | ||
| 235 | */ | ||
| 236 | #define CMP_EQ 1 | ||
| 237 | #define CMP_LT 2 | ||
| 238 | #define CMP_GT 4 | ||
| 239 | #define CMP_N 8 /* not comparable values (e.g. NaN) */ | ||
| 240 | |||
| 241 | |||
| 233 | extern const TObject luaO_nilobject; | 242 | extern const TObject luaO_nilobject; |
| 234 | 243 | ||
| 235 | int luaO_log2 (unsigned int x); | 244 | int luaO_log2 (unsigned int x); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lopcodes.c,v 1.16 2002/04/10 18:05:08 roberto Exp roberto $ | 2 | ** $Id: lopcodes.c,v 1.17 2002/04/24 20:07:46 roberto Exp roberto $ |
| 3 | ** extracted automatically from lopcodes.h by mkprint.lua | 3 | ** extracted automatically from lopcodes.h by mkprint.lua |
| 4 | ** DO NOT EDIT | 4 | ** DO NOT EDIT |
| 5 | ** See Copyright Notice in lua.h | 5 | ** See Copyright Notice in lua.h |
| @@ -36,14 +36,9 @@ const char *const luaP_opnames[] = { | |||
| 36 | "NOT", | 36 | "NOT", |
| 37 | "CONCAT", | 37 | "CONCAT", |
| 38 | "JMP", | 38 | "JMP", |
| 39 | "TESTEQ", | 39 | "EQ", |
| 40 | "TESTNE", | 40 | "CMP", |
| 41 | "TESTLT", | 41 | "TEST", |
| 42 | "TESTLE", | ||
| 43 | "TESTGT", | ||
| 44 | "TESTGE", | ||
| 45 | "TESTT", | ||
| 46 | "TESTF", | ||
| 47 | "CALL", | 42 | "CALL", |
| 48 | "TAILCALL", | 43 | "TAILCALL", |
| 49 | "RETURN", | 44 | "RETURN", |
| @@ -86,14 +81,9 @@ const lu_byte luaP_opmodes[NUM_OPCODES] = { | |||
| 86 | ,opmode(0,0,1,0, 1,0,iABC) /* OP_NOT */ | 81 | ,opmode(0,0,1,0, 1,0,iABC) /* OP_NOT */ |
| 87 | ,opmode(0,0,1,1, 1,0,iABC) /* OP_CONCAT */ | 82 | ,opmode(0,0,1,1, 1,0,iABC) /* OP_CONCAT */ |
| 88 | ,opmode(0,0,0,0, 0,0,iAsBx) /* OP_JMP */ | 83 | ,opmode(0,0,0,0, 0,0,iAsBx) /* OP_JMP */ |
| 89 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTEQ */ | 84 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_EQ */ |
| 90 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTNE */ | 85 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_CMP */ |
| 91 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTLT */ | 86 | ,opmode(1,0,0,1, 1,0,iABC) /* OP_TEST */ |
| 92 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTLE */ | ||
| 93 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTGT */ | ||
| 94 | ,opmode(1,0,0,1, 0,0,iABC) /* OP_TESTGE */ | ||
| 95 | ,opmode(1,0,1,0, 1,0,iABC) /* OP_TESTT */ | ||
| 96 | ,opmode(1,0,1,0, 1,0,iABC) /* OP_TESTF */ | ||
| 97 | ,opmode(0,0,0,0, 0,0,iABC) /* OP_CALL */ | 87 | ,opmode(0,0,0,0, 0,0,iABC) /* OP_CALL */ |
| 98 | ,opmode(0,0,0,0, 0,0,iABC) /* OP_TAILCALL */ | 88 | ,opmode(0,0,0,0, 0,0,iABC) /* OP_TAILCALL */ |
| 99 | ,opmode(0,0,0,0, 0,0,iABC) /* OP_RETURN */ | 89 | ,opmode(0,0,0,0, 0,0,iABC) /* OP_RETURN */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lopcodes.h,v 1.94 2002/04/09 19:47:44 roberto Exp roberto $ | 2 | ** $Id: lopcodes.h,v 1.95 2002/04/24 20:07:46 roberto Exp roberto $ |
| 3 | ** Opcodes for Lua virtual machine | 3 | ** Opcodes for Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -81,19 +81,19 @@ enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ | |||
| 81 | 81 | ||
| 82 | #define GETARG_A(i) (cast(int, (i)>>POS_A)) | 82 | #define GETARG_A(i) (cast(int, (i)>>POS_A)) |
| 83 | #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ | 83 | #define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ |
| 84 | (cast(Instruction, u)<<POS_A))) | 84 | ((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A)))) |
| 85 | 85 | ||
| 86 | #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0))) | 86 | #define GETARG_B(i) (cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0))) |
| 87 | #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ | 87 | #define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ |
| 88 | (cast(Instruction, b)<<POS_B))) | 88 | ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B)))) |
| 89 | 89 | ||
| 90 | #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0))) | 90 | #define GETARG_C(i) (cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0))) |
| 91 | #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ | 91 | #define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ |
| 92 | (cast(Instruction, b)<<POS_C))) | 92 | ((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C)))) |
| 93 | 93 | ||
| 94 | #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0))) | 94 | #define GETARG_Bx(i) (cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0))) |
| 95 | #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ | 95 | #define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ |
| 96 | (cast(Instruction, b)<<POS_Bx))) | 96 | ((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx)))) |
| 97 | 97 | ||
| 98 | #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx) | 98 | #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx) |
| 99 | #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) | 99 | #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) |
| @@ -157,19 +157,14 @@ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ | |||
| 157 | 157 | ||
| 158 | OP_JMP,/* sBx PC += sBx */ | 158 | OP_JMP,/* sBx PC += sBx */ |
| 159 | 159 | ||
| 160 | OP_TESTEQ,/* A C if not (R(A) == R/K(C)) then pc++ */ | 160 | OP_EQ,/* A B C if ((R(A) == R/K(C)) ~= B) then pc++ */ |
| 161 | OP_TESTNE,/* A C if not (R(A) ~= R/K(C)) then pc++ */ | 161 | OP_CMP,/* A B C if not (R(A) <B> R/K(C)) then pc++ (see note) */ |
| 162 | OP_TESTLT,/* A C if not (R(A) < R/K(C)) then pc++ */ | ||
| 163 | OP_TESTLE,/* A C if not (R(A) <= R/K(C)) then pc++ */ | ||
| 164 | OP_TESTGT,/* A C if not (R(A) > R/K(C)) then pc++ */ | ||
| 165 | OP_TESTGE,/* A C if not (R(A) >= R/K(C)) then pc++ */ | ||
| 166 | 162 | ||
| 167 | OP_TESTT,/* A B if (R(B)) then R(A) := R(B) else pc++ */ | 163 | OP_TEST,/* A B C if (R(C) <=> B) then R(A) := R(C) else pc++ */ |
| 168 | OP_TESTF,/* A B if not (R(B)) then R(A) := R(B) else pc++ */ | ||
| 169 | 164 | ||
| 170 | OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ | 165 | OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ |
| 171 | OP_TAILCALL,/* A B return R(A)(R(A+1), ... ,R(A+B-1)) */ | 166 | OP_TAILCALL,/* A B return R(A)(R(A+1), ... ,R(A+B-1)) */ |
| 172 | OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see (3)) */ | 167 | OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ |
| 173 | 168 | ||
| 174 | OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx */ | 169 | OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx */ |
| 175 | 170 | ||
| @@ -196,8 +191,9 @@ OP_CLOSURE/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ | |||
| 196 | next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. | 191 | next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. |
| 197 | 192 | ||
| 198 | (2) In OP_RETURN, if (B == 0) then return up to `top' | 193 | (2) In OP_RETURN, if (B == 0) then return up to `top' |
| 199 | ===========================================================================*/ | ||
| 200 | 194 | ||
| 195 | (3) For comparisons, B specifies what conditions the test should accept. | ||
| 196 | ===========================================================================*/ | ||
| 201 | 197 | ||
| 202 | 198 | ||
| 203 | /* | 199 | /* |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.227 2002/04/24 20:07:46 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.228 2002/05/02 13:06:20 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 | */ |
| @@ -189,20 +189,21 @@ static void call_arith (lua_State *L, StkId p1, const TObject *p2, | |||
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | 191 | ||
| 192 | static int luaV_strlessthan (const TString *ls, const TString *rs) { | 192 | static int luaV_strcmp (const TString *ls, const TString *rs) { |
| 193 | const char *l = getstr(ls); | 193 | const char *l = getstr(ls); |
| 194 | size_t ll = ls->tsv.len; | 194 | size_t ll = ls->tsv.len; |
| 195 | const char *r = getstr(rs); | 195 | const char *r = getstr(rs); |
| 196 | size_t lr = rs->tsv.len; | 196 | size_t lr = rs->tsv.len; |
| 197 | for (;;) { | 197 | for (;;) { |
| 198 | int temp = strcoll(l, r); | 198 | int temp = strcoll(l, r); |
| 199 | if (temp != 0) return (temp < 0); | 199 | if (temp < 0) return CMP_LT; |
| 200 | else if (temp > 0) return CMP_GT; | ||
| 200 | else { /* strings are equal up to a `\0' */ | 201 | else { /* strings are equal up to a `\0' */ |
| 201 | size_t len = strlen(l); /* index of first `\0' in both strings */ | 202 | size_t len = strlen(l); /* index of first `\0' in both strings */ |
| 202 | if (len == lr) /* r is finished? */ | 203 | if (len == lr) /* r is finished? */ |
| 203 | return 0; /* l is equal or greater than r */ | 204 | return (len == ll) ? CMP_EQ : CMP_GT; /* l is eq. or gt. than r */ |
| 204 | else if (len == ll) /* l is finished? */ | 205 | else if (len == ll) /* l is finished? */ |
| 205 | return 1; /* l is smaller than r (because r is not finished) */ | 206 | return CMP_LT; /* l is smaller than r (because r is not finished) */ |
| 206 | /* both strings longer than `len'; go on comparing (after the `\0') */ | 207 | /* both strings longer than `len'; go on comparing (after the `\0') */ |
| 207 | len++; | 208 | len++; |
| 208 | l += len; ll -= len; r += len; lr -= len; | 209 | l += len; ll -= len; r += len; lr -= len; |
| @@ -211,15 +212,24 @@ static int luaV_strlessthan (const TString *ls, const TString *rs) { | |||
| 211 | } | 212 | } |
| 212 | 213 | ||
| 213 | 214 | ||
| 214 | int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) { | 215 | int luaV_cmp (lua_State *L, const TObject *l, const TObject *r, int cond) { |
| 215 | if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER) | 216 | if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER) { |
| 216 | return (nvalue(l) < nvalue(r)); | 217 | lua_Number n1 = nvalue(l); |
| 218 | lua_Number n2 = nvalue(r); | ||
| 219 | if (n1 < n2) return (cond & CMP_LT); | ||
| 220 | else if (n1 > n2) return (cond & CMP_GT); | ||
| 221 | else if (n1 == n2) return (cond & CMP_EQ); | ||
| 222 | else return (cond & CMP_N); | ||
| 223 | } | ||
| 217 | else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING) | 224 | else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING) |
| 218 | return luaV_strlessthan(tsvalue(l), tsvalue(r)); | 225 | return luaV_strcmp(tsvalue(l), tsvalue(r)) & cond; |
| 219 | else { /* try TM */ | 226 | else { /* try TM */ |
| 227 | if (cond & CMP_EQ ? cond & CMP_LT : cond & CMP_GT) { /* `<=' or `>' ? */ | ||
| 228 | const TObject *temp = l; l = r; r = temp; /* exchange terms */ | ||
| 229 | } | ||
| 220 | if (!call_binTM(L, l, r, L->top, TM_LT)) | 230 | if (!call_binTM(L, l, r, L->top, TM_LT)) |
| 221 | luaG_ordererror(L, l, r); | 231 | luaG_ordererror(L, l, r); |
| 222 | return !l_isfalse(L->top); | 232 | return (cond & CMP_EQ) ? l_isfalse(L->top) : !l_isfalse(L->top); |
| 223 | } | 233 | } |
| 224 | } | 234 | } |
| 225 | 235 | ||
| @@ -437,40 +447,18 @@ StkId luaV_execute (lua_State *L) { | |||
| 437 | dojump(pc, GETARG_sBx(i)); | 447 | dojump(pc, GETARG_sBx(i)); |
| 438 | break; | 448 | break; |
| 439 | } | 449 | } |
| 440 | case OP_TESTEQ: { /* skip next instruction if test fails */ | 450 | case OP_EQ: { /* skip next instruction if test fails */ |
| 441 | if (!luaO_equalObj(ra, RKC(i))) pc++; | 451 | if (luaO_equalObj(ra, RKC(i)) != GETARG_B(i)) pc++; |
| 442 | break; | ||
| 443 | } | ||
| 444 | case OP_TESTNE: { | ||
| 445 | if (luaO_equalObj(ra, RKC(i))) pc++; | ||
| 446 | break; | ||
| 447 | } | ||
| 448 | case OP_TESTLT: { | ||
| 449 | if (!luaV_lessthan(L, ra, RKC(i))) pc++; | ||
| 450 | break; | ||
| 451 | } | ||
| 452 | case OP_TESTLE: { /* b <= c === !(c<b) */ | ||
| 453 | if (luaV_lessthan(L, RKC(i), ra)) pc++; | ||
| 454 | break; | ||
| 455 | } | ||
| 456 | case OP_TESTGT: { /* b > c === (c<b) */ | ||
| 457 | if (!luaV_lessthan(L, RKC(i), ra)) pc++; | ||
| 458 | break; | 452 | break; |
| 459 | } | 453 | } |
| 460 | case OP_TESTGE: { /* b >= c === !(b<c) */ | 454 | case OP_CMP: { |
| 461 | if (luaV_lessthan(L, ra, RKC(i))) pc++; | 455 | if (!(luaV_cmp(L, ra, RKC(i), GETARG_B(i)))) pc++; |
| 462 | break; | 456 | break; |
| 463 | } | 457 | } |
| 464 | case OP_TESTT: { | 458 | case OP_TEST: { |
| 465 | StkId rb = RB(i); | 459 | StkId rc = RKC(i); |
| 466 | if (l_isfalse(rb)) pc++; | 460 | if (l_isfalse(rc) == GETARG_B(i)) pc++; |
| 467 | else setobj(ra, rb); | 461 | else setobj(ra, rc); |
| 468 | break; | ||
| 469 | } | ||
| 470 | case OP_TESTF: { | ||
| 471 | StkId rb = RB(i); | ||
| 472 | if (!l_isfalse(rb)) pc++; | ||
| 473 | else setobj(ra, rb); | ||
| 474 | break; | 462 | break; |
| 475 | } | 463 | } |
| 476 | case OP_CALL: { | 464 | case OP_CALL: { |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.h,v 1.37 2002/03/04 21:33:09 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.38 2002/03/19 12:45:25 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 | */ |
| @@ -19,12 +19,12 @@ | |||
| 19 | (((o) = luaV_tonumber(o,n)) != NULL)) | 19 | (((o) = luaV_tonumber(o,n)) != NULL)) |
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | int luaV_cmp (lua_State *L, const TObject *l, const TObject *r, int cond); | ||
| 22 | const TObject *luaV_tonumber (const TObject *obj, TObject *n); | 23 | const TObject *luaV_tonumber (const TObject *obj, TObject *n); |
| 23 | int luaV_tostring (lua_State *L, TObject *obj); | 24 | int luaV_tostring (lua_State *L, TObject *obj); |
| 24 | void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res); | 25 | void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res); |
| 25 | void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val); | 26 | void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val); |
| 26 | StkId luaV_execute (lua_State *L); | 27 | StkId luaV_execute (lua_State *L); |
| 27 | int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r); | ||
| 28 | void luaV_strconc (lua_State *L, int total, int last); | 28 | void luaV_strconc (lua_State *L, int total, int last); |
| 29 | 29 | ||
| 30 | #endif | 30 | #endif |
