diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-01-06 11:38:31 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-01-06 11:38:31 -0300 |
commit | 5ff408d2189c6c24fdf8908db4a31432bbdd6f15 (patch) | |
tree | bcd83d7547dab0d5418116eb10f9c601f2f2d3b9 /lcode.c | |
parent | e3c83835e7b396ab7db538fb3b052f02d7807dee (diff) | |
download | lua-5ff408d2189c6c24fdf8908db4a31432bbdd6f15.tar.gz lua-5ff408d2189c6c24fdf8908db4a31432bbdd6f15.tar.bz2 lua-5ff408d2189c6c24fdf8908db4a31432bbdd6f15.zip |
Changed internal representation of booleans
Instead of an explicit value (field 'b'), true and false use different
tag variants. This avoids reading an extra field and results in more
direct code. (Most code that uses booleans needs to distinguish between
true and false anyway.)
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 50 |
1 files changed, 35 insertions, 15 deletions
@@ -84,8 +84,11 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { | |||
84 | if (hasjumps(e)) | 84 | if (hasjumps(e)) |
85 | return 0; /* not a constant */ | 85 | return 0; /* not a constant */ |
86 | switch (e->k) { | 86 | switch (e->k) { |
87 | case VFALSE: case VTRUE: | 87 | case VFALSE: |
88 | setbvalue(v, e->k == VTRUE); | 88 | setbfvalue(v); |
89 | return 1; | ||
90 | case VTRUE: | ||
91 | setbtvalue(v); | ||
89 | return 1; | 92 | return 1; |
90 | case VNIL: | 93 | case VNIL: |
91 | setnilvalue(v); | 94 | setnilvalue(v); |
@@ -604,11 +607,21 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { | |||
604 | 607 | ||
605 | 608 | ||
606 | /* | 609 | /* |
607 | ** Add a boolean to list of constants and return its index. | 610 | ** Add a false to list of constants and return its index. |
608 | */ | 611 | */ |
609 | static int boolK (FuncState *fs, int b) { | 612 | static int boolF (FuncState *fs) { |
610 | TValue o; | 613 | TValue o; |
611 | setbvalue(&o, b); | 614 | setbfvalue(&o); |
615 | return addk(fs, &o, &o); /* use boolean itself as key */ | ||
616 | } | ||
617 | |||
618 | |||
619 | /* | ||
620 | ** Add a true to list of constants and return its index. | ||
621 | */ | ||
622 | static int boolT (FuncState *fs) { | ||
623 | TValue o; | ||
624 | setbtvalue(&o); | ||
612 | return addk(fs, &o, &o); /* use boolean itself as key */ | 625 | return addk(fs, &o, &o); /* use boolean itself as key */ |
613 | } | 626 | } |
614 | 627 | ||
@@ -671,8 +684,11 @@ static void const2exp (TValue *v, expdesc *e) { | |||
671 | case LUA_TNUMFLT: | 684 | case LUA_TNUMFLT: |
672 | e->k = VKFLT; e->u.nval = fltvalue(v); | 685 | e->k = VKFLT; e->u.nval = fltvalue(v); |
673 | break; | 686 | break; |
674 | case LUA_TBOOLEAN: | 687 | case LUA_TFALSE: |
675 | e->k = bvalue(v) ? VTRUE : VFALSE; | 688 | e->k = VFALSE; |
689 | break; | ||
690 | case LUA_TTRUE: | ||
691 | e->k = VTRUE; | ||
676 | break; | 692 | break; |
677 | case LUA_TNIL: | 693 | case LUA_TNIL: |
678 | e->k = VNIL; | 694 | e->k = VNIL; |
@@ -801,8 +817,12 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { | |||
801 | luaK_nil(fs, reg, 1); | 817 | luaK_nil(fs, reg, 1); |
802 | break; | 818 | break; |
803 | } | 819 | } |
804 | case VFALSE: case VTRUE: { | 820 | case VFALSE: { |
805 | luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); | 821 | luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0); |
822 | break; | ||
823 | } | ||
824 | case VTRUE: { | ||
825 | luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0); | ||
806 | break; | 826 | break; |
807 | } | 827 | } |
808 | case VKSTR: { | 828 | case VKSTR: { |
@@ -852,9 +872,9 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { | |||
852 | } | 872 | } |
853 | 873 | ||
854 | 874 | ||
855 | static int code_loadbool (FuncState *fs, int A, int b, int jump) { | 875 | static int code_loadbool (FuncState *fs, int A, OpCode op, int jump) { |
856 | luaK_getlabel(fs); /* those instructions may be jump targets */ | 876 | luaK_getlabel(fs); /* those instructions may be jump targets */ |
857 | return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); | 877 | return luaK_codeABC(fs, op, A, jump, 0); |
858 | } | 878 | } |
859 | 879 | ||
860 | 880 | ||
@@ -888,8 +908,8 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { | |||
888 | int p_t = NO_JUMP; /* position of an eventual LOAD true */ | 908 | int p_t = NO_JUMP; /* position of an eventual LOAD true */ |
889 | if (need_value(fs, e->t) || need_value(fs, e->f)) { | 909 | if (need_value(fs, e->t) || need_value(fs, e->f)) { |
890 | int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); | 910 | int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); |
891 | p_f = code_loadbool(fs, reg, 0, 1); /* load false and skip next i. */ | 911 | p_f = code_loadbool(fs, reg, OP_LOADFALSE, 1); /* skip next inst. */ |
892 | p_t = code_loadbool(fs, reg, 1, 0); /* load true */ | 912 | p_t = code_loadbool(fs, reg, OP_LOADTRUE, 0); |
893 | /* jump around these booleans if 'e' is not a test */ | 913 | /* jump around these booleans if 'e' is not a test */ |
894 | luaK_patchtohere(fs, fj); | 914 | luaK_patchtohere(fs, fj); |
895 | } | 915 | } |
@@ -963,8 +983,8 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) { | |||
963 | if (!hasjumps(e)) { | 983 | if (!hasjumps(e)) { |
964 | int info; | 984 | int info; |
965 | switch (e->k) { /* move constants to 'k' */ | 985 | switch (e->k) { /* move constants to 'k' */ |
966 | case VTRUE: info = boolK(fs, 1); break; | 986 | case VTRUE: info = boolT(fs); break; |
967 | case VFALSE: info = boolK(fs, 0); break; | 987 | case VFALSE: info = boolF(fs); break; |
968 | case VNIL: info = nilK(fs); break; | 988 | case VNIL: info = nilK(fs); break; |
969 | case VKINT: info = luaK_intK(fs, e->u.ival); break; | 989 | case VKINT: info = luaK_intK(fs, e->u.ival); break; |
970 | case VKFLT: info = luaK_numberK(fs, e->u.nval); break; | 990 | case VKFLT: info = luaK_numberK(fs, e->u.nval); break; |