diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-19 18:17:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-19 18:17:52 -0300 |
| commit | 2079cfe8faa34ebe435d1ef0526b04d3e57b5349 (patch) | |
| tree | 617a80c97583dc9eff9a11a2db88049c49b65e1a /lua.stx | |
| parent | dfe03c7abea6a00925a56239dfaac5be2770396e (diff) | |
| download | lua-2079cfe8faa34ebe435d1ef0526b04d3e57b5349.tar.gz lua-2079cfe8faa34ebe435d1ef0526b04d3e57b5349.tar.bz2 lua-2079cfe8faa34ebe435d1ef0526b04d3e57b5349.zip | |
new way to code globals, using const table instead of putting global
index inside the opcode.
Diffstat (limited to 'lua.stx')
| -rw-r--r-- | lua.stx | 64 |
1 files changed, 39 insertions, 25 deletions
| @@ -1,6 +1,6 @@ | |||
| 1 | %{ | 1 | %{ |
| 2 | /* | 2 | /* |
| 3 | ** $Id: lua.stx,v 1.1 1997/09/16 19:33:21 roberto Exp roberto $ | 3 | ** $Id: lua.stx,v 1.2 1997/09/19 18:40:32 roberto Exp roberto $ |
| 4 | ** Syntax analizer and code generator | 4 | ** Syntax analizer and code generator |
| 5 | ** See Copyright Notice in lua.h | 5 | ** See Copyright Notice in lua.h |
| 6 | */ | 6 | */ |
| @@ -11,7 +11,6 @@ | |||
| 11 | #include "lauxlib.h" | 11 | #include "lauxlib.h" |
| 12 | #include "ldo.h" | 12 | #include "ldo.h" |
| 13 | #include "lfunc.h" | 13 | #include "lfunc.h" |
| 14 | #include "lglobal.h" | ||
| 15 | #include "llex.h" | 14 | #include "llex.h" |
| 16 | #include "lmem.h" | 15 | #include "lmem.h" |
| 17 | #include "lopcodes.h" | 16 | #include "lopcodes.h" |
| @@ -51,7 +50,7 @@ typedef long vardesc; | |||
| 51 | 50 | ||
| 52 | 51 | ||
| 53 | /* state needed to generate code for a given function */ | 52 | /* state needed to generate code for a given function */ |
| 54 | static struct State { | 53 | typedef struct State { |
| 55 | TProtoFunc *f; /* current function header */ | 54 | TProtoFunc *f; /* current function header */ |
| 56 | int pc; /* next position to code */ | 55 | int pc; /* next position to code */ |
| 57 | TaggedString *localvar[MAXLOCALS]; /* store local variable names */ | 56 | TaggedString *localvar[MAXLOCALS]; /* store local variable names */ |
| @@ -64,7 +63,9 @@ static struct State { | |||
| 64 | int maxconsts; /* size of f->consts */ | 63 | int maxconsts; /* size of f->consts */ |
| 65 | vardesc varbuffer[MAXVAR]; /* variables in an assignment list */ | 64 | vardesc varbuffer[MAXVAR]; /* variables in an assignment list */ |
| 66 | vardesc upvalues[MAXUPVALUES]; /* upvalues */ | 65 | vardesc upvalues[MAXUPVALUES]; /* upvalues */ |
| 67 | } *mainState, *currState; | 66 | } State; |
| 67 | |||
| 68 | static State *mainState, *currState; | ||
| 68 | 69 | ||
| 69 | 70 | ||
| 70 | 71 | ||
| @@ -160,25 +161,24 @@ static void code_constant (int c) | |||
| 160 | } | 161 | } |
| 161 | 162 | ||
| 162 | 163 | ||
| 163 | static int next_constant (void) | 164 | static int next_constant (State *cs) |
| 164 | { | 165 | { |
| 165 | TProtoFunc *f = currState->f; | 166 | TProtoFunc *f = cs->f; |
| 166 | if (f->nconsts >= currState->maxconsts) { | 167 | if (f->nconsts >= cs->maxconsts) { |
| 167 | currState->maxconsts = | 168 | cs->maxconsts = luaM_growvector(&f->consts, cs->maxconsts, TObject, |
| 168 | luaM_growvector(&f->consts, currState->maxconsts, TObject, | 169 | constantEM, MAX_WORD); |
| 169 | constantEM, MAX_WORD); | ||
| 170 | } | 170 | } |
| 171 | return f->nconsts++; | 171 | return f->nconsts++; |
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | 174 | ||
| 175 | static int string_constant (TaggedString *s) | 175 | static int string_constant (TaggedString *s, State *cs) |
| 176 | { | 176 | { |
| 177 | TProtoFunc *f = currState->f; | 177 | TProtoFunc *f = cs->f; |
| 178 | int c = s->u.s.constindex; | 178 | int c = s->u.s.constindex; |
| 179 | if (!(0 <= c && c < f->nconsts && | 179 | if (!(0 <= c && c < f->nconsts && |
| 180 | ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { | 180 | ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { |
| 181 | c = next_constant(); | 181 | c = next_constant(cs); |
| 182 | ttype(&f->consts[c]) = LUA_T_STRING; | 182 | ttype(&f->consts[c]) = LUA_T_STRING; |
| 183 | tsvalue(&f->consts[c]) = s; | 183 | tsvalue(&f->consts[c]) = s; |
| 184 | s->u.s.constindex = c; /* hint for next time */ | 184 | s->u.s.constindex = c; /* hint for next time */ |
| @@ -189,7 +189,7 @@ static int string_constant (TaggedString *s) | |||
| 189 | 189 | ||
| 190 | static void code_string (TaggedString *s) | 190 | static void code_string (TaggedString *s) |
| 191 | { | 191 | { |
| 192 | code_constant(string_constant(s)); | 192 | code_constant(string_constant(s, currState)); |
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | 195 | ||
| @@ -205,7 +205,7 @@ static int real_constant (real r) | |||
| 205 | return c; | 205 | return c; |
| 206 | } | 206 | } |
| 207 | /* not found; create a luaM_new entry */ | 207 | /* not found; create a luaM_new entry */ |
| 208 | c = next_constant(); | 208 | c = next_constant(currState); |
| 209 | cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */ | 209 | cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */ |
| 210 | ttype(&cnt[c]) = LUA_T_NUMBER; | 210 | ttype(&cnt[c]) = LUA_T_NUMBER; |
| 211 | nvalue(&cnt[c]) = r; | 211 | nvalue(&cnt[c]) = r; |
| @@ -297,7 +297,7 @@ static void add_varbuffer (vardesc var, int n) | |||
| 297 | } | 297 | } |
| 298 | 298 | ||
| 299 | 299 | ||
| 300 | static int aux_localname (TaggedString *n, struct State *st) | 300 | static int aux_localname (TaggedString *n, State *st) |
| 301 | { | 301 | { |
| 302 | int i; | 302 | int i; |
| 303 | for (i=st->nlocalvar-1; i >= 0; i--) | 303 | for (i=st->nlocalvar-1; i >= 0; i--) |
| @@ -306,7 +306,7 @@ static int aux_localname (TaggedString *n, struct State *st) | |||
| 306 | } | 306 | } |
| 307 | 307 | ||
| 308 | 308 | ||
| 309 | static vardesc singlevar (TaggedString *n, struct State *st) | 309 | static vardesc singlevar (TaggedString *n, State *st) |
| 310 | { | 310 | { |
| 311 | int i = aux_localname(n, st); | 311 | int i = aux_localname(n, st); |
| 312 | if (i == -1) { /* check shadowing */ | 312 | if (i == -1) { /* check shadowing */ |
| @@ -314,7 +314,7 @@ static vardesc singlevar (TaggedString *n, struct State *st) | |||
| 314 | for (l=1; l<=(st-mainState); l++) | 314 | for (l=1; l<=(st-mainState); l++) |
| 315 | if (aux_localname(n, st-l) >= 0) | 315 | if (aux_localname(n, st-l) >= 0) |
| 316 | luaY_syntaxerror("cannot access a variable in outer scope", n->str); | 316 | luaY_syntaxerror("cannot access a variable in outer scope", n->str); |
| 317 | return luaG_findsymbol(n)+1; /* positive value */ | 317 | return string_constant(n, st)+1; /* positive value */ |
| 318 | } | 318 | } |
| 319 | else return -(i+1); /* negative value */ | 319 | else return -(i+1); /* negative value */ |
| 320 | } | 320 | } |
| @@ -427,8 +427,15 @@ static void code_args (int dots) | |||
| 427 | static void lua_pushvar (vardesc number) | 427 | static void lua_pushvar (vardesc number) |
| 428 | { | 428 | { |
| 429 | if (number > 0) { /* global var */ | 429 | if (number > 0) { /* global var */ |
| 430 | code_push(PUSHGLOBAL); | 430 | number--; |
| 431 | code_word(number-1); | 431 | if (number <= 255) { |
| 432 | code_push(PUSHGLOBALB); | ||
| 433 | code_byte(number); | ||
| 434 | } | ||
| 435 | else { | ||
| 436 | code_push(PUSHGLOBAL); | ||
| 437 | code_word(number); | ||
| 438 | } | ||
| 432 | } | 439 | } |
| 433 | else if (number < 0) { /* local var */ | 440 | else if (number < 0) { /* local var */ |
| 434 | number = (-number) - 1; | 441 | number = (-number) - 1; |
| @@ -450,8 +457,15 @@ static void storevar (vardesc number) | |||
| 450 | if (number == 0) /* indexed var */ | 457 | if (number == 0) /* indexed var */ |
| 451 | code_opcode(SETTABLE0, -3); | 458 | code_opcode(SETTABLE0, -3); |
| 452 | else if (number > 0) { /* global var */ | 459 | else if (number > 0) { /* global var */ |
| 453 | code_pop(SETGLOBAL); | 460 | number--; |
| 454 | code_word(number-1); | 461 | if (number <= 255) { |
| 462 | code_pop(SETGLOBALB); | ||
| 463 | code_byte(number); | ||
| 464 | } | ||
| 465 | else { | ||
| 466 | code_pop(SETGLOBAL); | ||
| 467 | code_word(number); | ||
| 468 | } | ||
| 455 | } | 469 | } |
| 456 | else { /* number < 0 - local var */ | 470 | else { /* number < 0 - local var */ |
| 457 | number = (-number) - 1; | 471 | number = (-number) - 1; |
| @@ -516,7 +530,7 @@ static void func_onstack (TProtoFunc *f) | |||
| 516 | { | 530 | { |
| 517 | int i; | 531 | int i; |
| 518 | int nupvalues = (currState+1)->f->nupvalues; | 532 | int nupvalues = (currState+1)->f->nupvalues; |
| 519 | int c = next_constant(); | 533 | int c = next_constant(currState); |
| 520 | ttype(&currState->f->consts[c]) = LUA_T_PROTO; | 534 | ttype(&currState->f->consts[c]) = LUA_T_PROTO; |
| 521 | currState->f->consts[c].value.tf = (currState+1)->f; | 535 | currState->f->consts[c].value.tf = (currState+1)->f; |
| 522 | for (i=0; i<nupvalues; i++) | 536 | for (i=0; i<nupvalues; i++) |
| @@ -581,7 +595,7 @@ static TProtoFunc *close_func (void) | |||
| 581 | */ | 595 | */ |
| 582 | TProtoFunc *luaY_parser (ZIO *z, char *chunkname) | 596 | TProtoFunc *luaY_parser (ZIO *z, char *chunkname) |
| 583 | { | 597 | { |
| 584 | struct State state[MAXSTATES]; | 598 | State state[MAXSTATES]; |
| 585 | currState = mainState = &state[0]; | 599 | currState = mainState = &state[0]; |
| 586 | luaX_setinput(z); | 600 | luaX_setinput(z); |
| 587 | init_state(luaS_new(chunkname)); | 601 | init_state(luaS_new(chunkname)); |
| @@ -788,7 +802,7 @@ funcvalue : varexp { $$ = 0; } | |||
| 788 | | varexp ':' NAME | 802 | | varexp ':' NAME |
| 789 | { | 803 | { |
| 790 | code_push(PUSHSELF); | 804 | code_push(PUSHSELF); |
| 791 | code_word(string_constant($3)); | 805 | code_word(string_constant($3, currState)); |
| 792 | $$ = 1; | 806 | $$ = 1; |
| 793 | } | 807 | } |
| 794 | ; | 808 | ; |
