diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-01-25 11:57:18 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-01-25 11:57:18 -0200 |
commit | d11e5adf55b11a446671775a6c7803e066fc94e8 (patch) | |
tree | b7881e03e792bcc46f497577e8141b5ecc2e5b17 /lparser.c | |
parent | 99e340b2ba08226f4f7b457b170296af8d82959b (diff) | |
download | lua-d11e5adf55b11a446671775a6c7803e066fc94e8.tar.gz lua-d11e5adf55b11a446671775a6c7803e066fc94e8.tar.bz2 lua-d11e5adf55b11a446671775a6c7803e066fc94e8.zip |
`const' array in protos breaked in 3 arrays (for strings, numbers, and
prototypes).
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 83 |
1 files changed, 35 insertions, 48 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 1.53 2000/01/10 17:34:38 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.54 2000/01/12 16:24:39 roberto Exp roberto $ |
3 | ** LL(1) Parser and code generator for Lua | 3 | ** LL(1) Parser and code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -209,33 +209,24 @@ static void code_opcode (LexState *ls, OpCode op, int delta) { | |||
209 | } | 209 | } |
210 | 210 | ||
211 | 211 | ||
212 | static void code_constant (LexState *ls, int c) { | 212 | static void code_strcnst (LexState *ls, int c) { |
213 | code_oparg(ls, PUSHCONSTANT, c, 1); | 213 | code_oparg(ls, PUSHSTRCNST, c, 1); |
214 | } | 214 | } |
215 | 215 | ||
216 | 216 | ||
217 | static void assertglobal (LexState *ls, int index) { | 217 | static void assertglobal (LexState *ls, int index) { |
218 | TObject *o = &ls->fs->f->consts[index]; | 218 | luaS_assertglobal(ls->L, ls->fs->f->strcnst[index]); |
219 | LUA_ASSERT(ls->L, ttype(o) == LUA_T_STRING, "global name is not a string"); | ||
220 | luaS_assertglobal(ls->L, tsvalue(o)); | ||
221 | } | ||
222 | |||
223 | |||
224 | static int next_constant (LexState *ls, TProtoFunc *f) { | ||
225 | luaM_growvector(ls->L, f->consts, f->nconsts, 1, | ||
226 | TObject, constantEM, MAX_ARG); | ||
227 | return f->nconsts++; | ||
228 | } | 219 | } |
229 | 220 | ||
230 | 221 | ||
231 | static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) { | 222 | static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) { |
232 | TProtoFunc *f = fs->f; | 223 | TProtoFunc *f = fs->f; |
233 | int c = s->constindex; | 224 | int c = s->constindex; |
234 | if (!(c < f->nconsts && | 225 | if (c >= f->nstrcnst || f->strcnst[c] != s) { |
235 | ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { | 226 | luaM_growvector(ls->L, f->strcnst, f->nstrcnst, 1, |
236 | c = next_constant(ls, f); | 227 | TaggedString *, constantEM, MAX_ARG); |
237 | ttype(&f->consts[c]) = LUA_T_STRING; | 228 | c = f->nstrcnst++; |
238 | tsvalue(&f->consts[c]) = s; | 229 | f->strcnst[c] = s; |
239 | s->constindex = c; /* hint for next time */ | 230 | s->constindex = c; /* hint for next time */ |
240 | } | 231 | } |
241 | return c; | 232 | return c; |
@@ -243,7 +234,7 @@ static int string_constant (LexState *ls, FuncState *fs, TaggedString *s) { | |||
243 | 234 | ||
244 | 235 | ||
245 | static void code_string (LexState *ls, TaggedString *s) { | 236 | static void code_string (LexState *ls, TaggedString *s) { |
246 | code_constant(ls, string_constant(ls, ls->fs, s)); | 237 | code_strcnst(ls, string_constant(ls, ls->fs, s)); |
247 | } | 238 | } |
248 | 239 | ||
249 | 240 | ||
@@ -251,18 +242,15 @@ static void code_string (LexState *ls, TaggedString *s) { | |||
251 | static int real_constant (LexState *ls, real r) { | 242 | static int real_constant (LexState *ls, real r) { |
252 | /* check whether `r' has appeared within the last LIM entries */ | 243 | /* check whether `r' has appeared within the last LIM entries */ |
253 | TProtoFunc *f = ls->fs->f; | 244 | TProtoFunc *f = ls->fs->f; |
254 | TObject *cnt = f->consts; | 245 | int c = f->nnumcnst; |
255 | int c = f->nconsts; | ||
256 | int lim = c < LIM ? 0 : c-LIM; | 246 | int lim = c < LIM ? 0 : c-LIM; |
257 | while (--c >= lim) { | 247 | while (--c >= lim) |
258 | if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r) | 248 | if (f->numcnst[c] == r) return c; |
259 | return c; | ||
260 | } | ||
261 | /* not found; create a new entry */ | 249 | /* not found; create a new entry */ |
262 | c = next_constant(ls, f); | 250 | luaM_growvector(ls->L, f->numcnst, f->nnumcnst, 1, |
263 | cnt = f->consts; /* `next_constant' may reallocate this vector */ | 251 | real, constantEM, MAX_ARG); |
264 | ttype(&cnt[c]) = LUA_T_NUMBER; | 252 | c = f->nnumcnst++; |
265 | nvalue(&cnt[c]) = r; | 253 | f->numcnst[c] = r; |
266 | return c; | 254 | return c; |
267 | } | 255 | } |
268 | 256 | ||
@@ -274,7 +262,7 @@ static void code_number (LexState *ls, real f) { | |||
274 | code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1); | 262 | code_oparg(ls, (f<0) ? PUSHNUMBERNEG : PUSHNUMBER, (int)af, 1); |
275 | } | 263 | } |
276 | else | 264 | else |
277 | code_constant(ls, real_constant(ls, f)); | 265 | code_oparg(ls, PUSHNUMCNST, real_constant(ls, f), 1); |
278 | } | 266 | } |
279 | 267 | ||
280 | 268 | ||
@@ -476,7 +464,7 @@ static void code_args (LexState *ls, int nparams, int dots) { | |||
476 | static void unloaddot (LexState *ls, vardesc *v) { | 464 | static void unloaddot (LexState *ls, vardesc *v) { |
477 | /* dotted variables <a.x> must be stored as regular indexed vars <a["x"]> */ | 465 | /* dotted variables <a.x> must be stored as regular indexed vars <a["x"]> */ |
478 | if (v->k == VDOT) { | 466 | if (v->k == VDOT) { |
479 | code_constant(ls, v->info); | 467 | code_strcnst(ls, v->info); |
480 | v->k = VINDEXED; | 468 | v->k = VINDEXED; |
481 | } | 469 | } |
482 | } | 470 | } |
@@ -556,19 +544,16 @@ static void codeIf (LexState *ls, int thenAdd, int elseAdd) { | |||
556 | 544 | ||
557 | static void func_onstack (LexState *ls, FuncState *func) { | 545 | static void func_onstack (LexState *ls, FuncState *func) { |
558 | FuncState *fs = ls->fs; | 546 | FuncState *fs = ls->fs; |
547 | TProtoFunc *f = fs->f; | ||
559 | int i; | 548 | int i; |
560 | int c = next_constant(ls, fs->f); | 549 | luaM_growvector(ls->L, f->protocnst, f->nprotocnst, 1, |
561 | ttype(&fs->f->consts[c]) = LUA_T_LPROTO; | 550 | TProtoFunc *, constantEM, MAX_ARG); |
562 | fs->f->consts[c].value.tf = func->f; | 551 | f->protocnst[f->nprotocnst] = func->f; |
563 | if (func->nupvalues == 0) | 552 | for (i=0; i<func->nupvalues; i++) |
564 | code_constant(ls, c); | 553 | lua_pushvar(ls, &func->upvalues[i]); |
565 | else { | 554 | deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */ |
566 | for (i=0; i<func->nupvalues; i++) | 555 | code_oparg(ls, CLOSURE, f->nprotocnst++, -func->nupvalues); |
567 | lua_pushvar(ls, &func->upvalues[i]); | 556 | code_byte(ls, (Byte)func->nupvalues); |
568 | deltastack(ls, 1); /* CLOSURE puts one extra element (before poping) */ | ||
569 | code_oparg(ls, CLOSURE, c, -func->nupvalues); | ||
570 | code_byte(ls, (Byte)func->nupvalues); | ||
571 | } | ||
572 | } | 557 | } |
573 | 558 | ||
574 | 559 | ||
@@ -602,7 +587,9 @@ static void close_func (LexState *ls) { | |||
602 | code_opcode(ls, ENDCODE, 0); | 587 | code_opcode(ls, ENDCODE, 0); |
603 | f->code[0] = (Byte)fs->maxstacksize; | 588 | f->code[0] = (Byte)fs->maxstacksize; |
604 | luaM_reallocvector(ls->L, f->code, fs->pc, Byte); | 589 | luaM_reallocvector(ls->L, f->code, fs->pc, Byte); |
605 | luaM_reallocvector(ls->L, f->consts, f->nconsts, TObject); | 590 | luaM_reallocvector(ls->L, f->strcnst, f->nstrcnst, TaggedString *); |
591 | luaM_reallocvector(ls->L, f->numcnst, f->nnumcnst, real); | ||
592 | luaM_reallocvector(ls->L, f->protocnst, f->nprotocnst, TProtoFunc *); | ||
606 | if (fs->nvars != -1) { /* debug information? */ | 593 | if (fs->nvars != -1) { /* debug information? */ |
607 | luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */ | 594 | luaI_registerlocalvar(ls, NULL, -1); /* flag end of vector */ |
608 | luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar); | 595 | luaM_reallocvector(ls->L, f->locvars, fs->nvars, LocVar); |
@@ -683,7 +670,7 @@ static int checkname (LexState *ls) { | |||
683 | 670 | ||
684 | static TaggedString *str_checkname (LexState *ls) { | 671 | static TaggedString *str_checkname (LexState *ls) { |
685 | int i = checkname(ls); /* this call may realloc `f->consts' */ | 672 | int i = checkname(ls); /* this call may realloc `f->consts' */ |
686 | return tsvalue(&ls->fs->f->consts[i]); | 673 | return ls->fs->f->strcnst[i]; |
687 | } | 674 | } |
688 | 675 | ||
689 | 676 | ||
@@ -875,7 +862,7 @@ static void recfield (LexState *ls) { | |||
875 | /* recfield -> (NAME | '['exp1']') = exp1 */ | 862 | /* recfield -> (NAME | '['exp1']') = exp1 */ |
876 | switch (ls->token) { | 863 | switch (ls->token) { |
877 | case NAME: | 864 | case NAME: |
878 | code_constant(ls, checkname(ls)); | 865 | code_strcnst(ls, checkname(ls)); |
879 | break; | 866 | break; |
880 | 867 | ||
881 | case '[': | 868 | case '[': |
@@ -939,7 +926,7 @@ static void constructor_part (LexState *ls, constdesc *cd) { | |||
939 | if (ls->token == '=') { | 926 | if (ls->token == '=') { |
940 | switch (v.k) { | 927 | switch (v.k) { |
941 | case VGLOBAL: | 928 | case VGLOBAL: |
942 | code_constant(ls, v.info); | 929 | code_strcnst(ls, v.info); |
943 | break; | 930 | break; |
944 | case VLOCAL: | 931 | case VLOCAL: |
945 | code_string(ls, ls->fs->localvar[v.info]); | 932 | code_string(ls, ls->fs->localvar[v.info]); |
@@ -1297,7 +1284,7 @@ static int funcname (LexState *ls, vardesc *v) { | |||
1297 | needself = (ls->token == ':'); | 1284 | needself = (ls->token == ':'); |
1298 | next(ls); | 1285 | next(ls); |
1299 | lua_pushvar(ls, v); | 1286 | lua_pushvar(ls, v); |
1300 | code_constant(ls, checkname(ls)); | 1287 | code_strcnst(ls, checkname(ls)); |
1301 | v->k = VINDEXED; | 1288 | v->k = VINDEXED; |
1302 | } | 1289 | } |
1303 | return needself; | 1290 | return needself; |