diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-01 12:42:31 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-01 12:42:31 -0300 |
| commit | 8eca21c2e85625390a2a3b08c231e75e315980b0 (patch) | |
| tree | 47106e7a62cf00091594302629c7157fed81f738 /lparser.c | |
| parent | 924bed7297d5ea16a78ec07e7acc64afad951aa8 (diff) | |
| download | lua-8eca21c2e85625390a2a3b08c231e75e315980b0.tar.gz lua-8eca21c2e85625390a2a3b08c231e75e315980b0.tar.bz2 lua-8eca21c2e85625390a2a3b08c231e75e315980b0.zip | |
First take on constant propagation
Diffstat (limited to 'lparser.c')
| -rw-r--r-- | lparser.c | 34 |
1 files changed, 22 insertions, 12 deletions
| @@ -206,6 +206,7 @@ static Vardesc *new_localvar (LexState *ls, TString *name) { | |||
| 206 | var = &dyd->actvar.arr[dyd->actvar.n++]; | 206 | var = &dyd->actvar.arr[dyd->actvar.n++]; |
| 207 | var->idx = cast(short, reg); | 207 | var->idx = cast(short, reg); |
| 208 | var->ro = 0; | 208 | var->ro = 0; |
| 209 | setnilvalue(&var->val); | ||
| 209 | return var; | 210 | return var; |
| 210 | } | 211 | } |
| 211 | 212 | ||
| @@ -238,7 +239,7 @@ static LocVar *getlocvar (FuncState *fs, int i) { | |||
| 238 | ** where that variable was defined. Return NULL if expression | 239 | ** where that variable was defined. Return NULL if expression |
| 239 | ** is neither a local variable nor an upvalue. | 240 | ** is neither a local variable nor an upvalue. |
| 240 | */ | 241 | */ |
| 241 | static Vardesc *getvardesc (FuncState **fs, expdesc *e) { | 242 | Vardesc *luaY_getvardesc (FuncState **fs, const expdesc *e) { |
| 242 | if (e->k == VLOCAL) | 243 | if (e->k == VLOCAL) |
| 243 | return getlocalvardesc(*fs, e->u.var.idx); | 244 | return getlocalvardesc(*fs, e->u.var.idx); |
| 244 | else if (e->k != VUPVAL) | 245 | else if (e->k != VUPVAL) |
| @@ -261,7 +262,7 @@ static Vardesc *getvardesc (FuncState **fs, expdesc *e) { | |||
| 261 | 262 | ||
| 262 | static void check_readonly (LexState *ls, expdesc *e) { | 263 | static void check_readonly (LexState *ls, expdesc *e) { |
| 263 | FuncState *fs = ls->fs; | 264 | FuncState *fs = ls->fs; |
| 264 | Vardesc *vardesc = getvardesc(&fs, e); | 265 | Vardesc *vardesc = luaY_getvardesc(&fs, e); |
| 265 | if (vardesc && vardesc->ro) { /* is variable local and const? */ | 266 | if (vardesc && vardesc->ro) { /* is variable local and const? */ |
| 266 | const char *msg = luaO_pushfstring(ls->L, | 267 | const char *msg = luaO_pushfstring(ls->L, |
| 267 | "attempt to assign to const variable '%s'", | 268 | "attempt to assign to const variable '%s'", |
| @@ -1678,20 +1679,13 @@ static void commonlocalstat (LexState *ls) { | |||
| 1678 | static void tocloselocalstat (LexState *ls, Vardesc *var) { | 1679 | static void tocloselocalstat (LexState *ls, Vardesc *var) { |
| 1679 | FuncState *fs = ls->fs; | 1680 | FuncState *fs = ls->fs; |
| 1680 | var->ro = 1; /* to-be-closed variables are always read-only */ | 1681 | var->ro = 1; /* to-be-closed variables are always read-only */ |
| 1681 | markupval(fs, fs->nactvar); | 1682 | markupval(fs, fs->nactvar + 1); |
| 1682 | fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ | 1683 | fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ |
| 1683 | luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); | 1684 | luaK_codeABC(fs, OP_TBC, fs->nactvar, 0, 0); |
| 1684 | } | 1685 | } |
| 1685 | 1686 | ||
| 1686 | 1687 | ||
| 1687 | static void attriblocalstat (LexState *ls) { | 1688 | static void checkattrib (LexState *ls, TString *attr, Vardesc *var) { |
| 1688 | Vardesc *var; | ||
| 1689 | TString *attr = str_checkname(ls); | ||
| 1690 | testnext(ls, '>'); | ||
| 1691 | var = new_localvar(ls, str_checkname(ls)); | ||
| 1692 | checknext(ls, '='); | ||
| 1693 | exp1(ls); | ||
| 1694 | adjustlocalvars(ls, 1); | ||
| 1695 | if (strcmp(getstr(attr), "const") == 0) | 1689 | if (strcmp(getstr(attr), "const") == 0) |
| 1696 | var->ro = 1; /* set variable as read-only */ | 1690 | var->ro = 1; /* set variable as read-only */ |
| 1697 | else if (strcmp(getstr(attr), "toclose") == 0) | 1691 | else if (strcmp(getstr(attr), "toclose") == 0) |
| @@ -1702,6 +1696,22 @@ static void attriblocalstat (LexState *ls) { | |||
| 1702 | } | 1696 | } |
| 1703 | 1697 | ||
| 1704 | 1698 | ||
| 1699 | static void attriblocalstat (LexState *ls) { | ||
| 1700 | FuncState *fs = ls->fs; | ||
| 1701 | Vardesc *var; | ||
| 1702 | expdesc e; | ||
| 1703 | TString *attr = str_checkname(ls); | ||
| 1704 | testnext(ls, '>'); | ||
| 1705 | var = new_localvar(ls, str_checkname(ls)); | ||
| 1706 | checknext(ls, '='); | ||
| 1707 | expr(ls, &e); | ||
| 1708 | checkattrib(ls, attr, var); | ||
| 1709 | luaK_tonumeral(fs, &e, &var->val); | ||
| 1710 | luaK_exp2nextreg(fs, &e); | ||
| 1711 | adjustlocalvars(ls, 1); | ||
| 1712 | } | ||
| 1713 | |||
| 1714 | |||
| 1705 | static void localstat (LexState *ls) { | 1715 | static void localstat (LexState *ls) { |
| 1706 | /* stat -> LOCAL NAME {',' NAME} ['=' explist] | 1716 | /* stat -> LOCAL NAME {',' NAME} ['=' explist] |
| 1707 | | LOCAL *toclose NAME '=' exp */ | 1717 | | LOCAL *toclose NAME '=' exp */ |
