aboutsummaryrefslogtreecommitdiff
path: root/lcode.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-17 14:26:56 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-17 14:26:56 -0300
commitd6af81084df569bc8e3bd0949ad6fc0b40c8468d (patch)
tree103b92a3fd9b1164763500054f7979f51f9aa4b4 /lcode.c
parent4846f7e3bb1397142ab0de808ae59c08db9832a6 (diff)
downloadlua-d6af81084df569bc8e3bd0949ad6fc0b40c8468d.tar.gz
lua-d6af81084df569bc8e3bd0949ad6fc0b40c8468d.tar.bz2
lua-d6af81084df569bc8e3bd0949ad6fc0b40c8468d.zip
New kind of expression VKSTR
String literal expressions have their own kind VKSTR, instead of the generic VK. This allows strings to "cross" functions without entering their constant tables (e.g., if they are used only by some nested function).
Diffstat (limited to 'lcode.c')
-rw-r--r--lcode.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/lcode.c b/lcode.c
index e57ad284..40efcff3 100644
--- a/lcode.c
+++ b/lcode.c
@@ -81,9 +81,8 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
81 case VNIL: 81 case VNIL:
82 setnilvalue(v); 82 setnilvalue(v);
83 return 1; 83 return 1;
84 case VK: { 84 case VKSTR: {
85 TValue *k = &fs->f->k[e->u.info]; 85 setsvalue(fs->ls->L, v, e->u.strval);
86 setobj(fs->ls->L, v, k);
87 return 1; 86 return 1;
88 } 87 }
89 default: return tonumeral(e, v); 88 default: return tonumeral(e, v);
@@ -561,7 +560,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
561/* 560/*
562** Add a string to list of constants and return its index. 561** Add a string to list of constants and return its index.
563*/ 562*/
564int luaK_stringK (FuncState *fs, TString *s) { 563static int stringK (FuncState *fs, TString *s) {
565 TValue o; 564 TValue o;
566 setsvalue(fs->ls->L, &o, s); 565 setsvalue(fs->ls->L, &o, s);
567 return addk(fs, &o, &o); /* use string itself as key */ 566 return addk(fs, &o, &o); /* use string itself as key */
@@ -656,7 +655,7 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) {
656/* 655/*
657** Convert a constant in 'v' into an expression description 'e' 656** Convert a constant in 'v' into an expression description 'e'
658*/ 657*/
659static void const2exp (FuncState *fs, TValue *v, expdesc *e) { 658static void const2exp (TValue *v, expdesc *e) {
660 switch (ttypetag(v)) { 659 switch (ttypetag(v)) {
661 case LUA_TNUMINT: 660 case LUA_TNUMINT:
662 e->k = VKINT; e->u.ival = ivalue(v); 661 e->k = VKINT; e->u.ival = ivalue(v);
@@ -671,7 +670,7 @@ static void const2exp (FuncState *fs, TValue *v, expdesc *e) {
671 e->k = VNIL; 670 e->k = VNIL;
672 break; 671 break;
673 case LUA_TSHRSTR: case LUA_TLNGSTR: 672 case LUA_TSHRSTR: case LUA_TLNGSTR:
674 e->k = VK; e->u.info = luaK_stringK(fs, tsvalue(v)); 673 e->k = VKSTR; e->u.strval = tsvalue(v);
675 break; 674 break;
676 default: lua_assert(0); 675 default: lua_assert(0);
677 } 676 }
@@ -697,6 +696,16 @@ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
697 696
698 697
699/* 698/*
699** Convert a VKSTR to a VK
700*/
701static void str2K (FuncState *fs, expdesc *e) {
702 lua_assert(e->k == VKSTR);
703 e->u.info = stringK(fs, e->u.strval);
704 e->k = VK;
705}
706
707
708/*
700** Fix an expression to return one result. 709** Fix an expression to return one result.
701** If expression is not a multi-ret expression (function call or 710** If expression is not a multi-ret expression (function call or
702** vararg), it already returns one result, so nothing needs to be done. 711** vararg), it already returns one result, so nothing needs to be done.
@@ -728,7 +737,7 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
728 switch (e->k) { 737 switch (e->k) {
729 case VCONST: { 738 case VCONST: {
730 TValue *val = &fs->ls->dyd->actvar.arr[e->u.info].k; 739 TValue *val = &fs->ls->dyd->actvar.arr[e->u.info].k;
731 const2exp(fs, val, e); 740 const2exp(val, e);
732 break; 741 break;
733 } 742 }
734 case VLOCAL: { /* already in a register */ 743 case VLOCAL: { /* already in a register */
@@ -789,6 +798,9 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
789 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); 798 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
790 break; 799 break;
791 } 800 }
801 case VKSTR: {
802 str2K(fs, e);
803 } /* FALLTHROUGH */
792 case VK: { 804 case VK: {
793 luaK_codek(fs, reg, e->u.info); 805 luaK_codek(fs, reg, e->u.info);
794 break; 806 break;
@@ -949,6 +961,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) {
949 case VNIL: info = nilK(fs); break; 961 case VNIL: info = nilK(fs); break;
950 case VKINT: info = luaK_intK(fs, e->u.ival); break; 962 case VKINT: info = luaK_intK(fs, e->u.ival); break;
951 case VKFLT: info = luaK_numberK(fs, e->u.nval); break; 963 case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
964 case VKSTR: info = stringK(fs, e->u.strval); break;
952 case VK: info = e->u.info; break; 965 case VK: info = e->u.info; break;
953 default: return 0; /* not a constant */ 966 default: return 0; /* not a constant */
954 } 967 }
@@ -1083,7 +1096,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) {
1083 pc = e->u.info; /* save jump position */ 1096 pc = e->u.info; /* save jump position */
1084 break; 1097 break;
1085 } 1098 }
1086 case VK: case VKFLT: case VKINT: case VTRUE: { 1099 case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1087 pc = NO_JUMP; /* always true; do nothing */ 1100 pc = NO_JUMP; /* always true; do nothing */
1088 break; 1101 break;
1089 } 1102 }
@@ -1133,7 +1146,7 @@ static void codenot (FuncState *fs, expdesc *e) {
1133 e->k = VTRUE; /* true == not nil == not false */ 1146 e->k = VTRUE; /* true == not nil == not false */
1134 break; 1147 break;
1135 } 1148 }
1136 case VK: case VKFLT: case VKINT: case VTRUE: { 1149 case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
1137 e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ 1150 e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
1138 break; 1151 break;
1139 } 1152 }
@@ -1219,9 +1232,11 @@ static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) {
1219** values in registers. 1232** values in registers.
1220*/ 1233*/
1221void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { 1234void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
1235 if (k->k == VKSTR)
1236 str2K(fs, k);
1222 lua_assert(!hasjumps(t) && 1237 lua_assert(!hasjumps(t) &&
1223 (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); 1238 (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
1224 if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ 1239 if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
1225 luaK_exp2anyreg(fs, t); /* put it in a register */ 1240 luaK_exp2anyreg(fs, t); /* put it in a register */
1226 if (t->k == VUPVAL) { 1241 if (t->k == VUPVAL) {
1227 t->u.ind.t = t->u.info; /* upvalue index */ 1242 t->u.ind.t = t->u.info; /* upvalue index */