aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lopcodes.h10
-rw-r--r--lua.stx64
-rw-r--r--lvm.c14
3 files changed, 56 insertions, 32 deletions
diff --git a/lopcodes.h b/lopcodes.h
index 29392229..ccab6341 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.2 1997/09/19 18:40:32 roberto Exp roberto $
3** Opcodes for Lua virtual machine 3** Opcodes for Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -35,7 +35,8 @@ PUSHLOCAL7,/* - LOC[7] */
35PUSHLOCAL8,/* - LOC[8] */ 35PUSHLOCAL8,/* - LOC[8] */
36PUSHLOCAL9,/* - LOC[9] */ 36PUSHLOCAL9,/* - LOC[9] */
37PUSHLOCAL,/* b - LOC[b] */ 37PUSHLOCAL,/* b - LOC[b] */
38PUSHGLOBAL,/* w - VAR[w] */ 38PUSHGLOBALB,/* b - VAR[CNST[b]] */
39PUSHGLOBAL,/* w - VAR[CNST[w]] */
39GETTABLE,/* i t t[i] */ 40GETTABLE,/* i t t[i] */
40PUSHSELF,/* w t t t[CNST[w]] */ 41PUSHSELF,/* w t t t[CNST[w]] */
41CREATEARRAY,/* w - newarray(size = w) */ 42CREATEARRAY,/* w - newarray(size = w) */
@@ -51,8 +52,9 @@ SETLOCAL7,/* x - LOC[7]=x */
51SETLOCAL8,/* x - LOC[8]=x */ 52SETLOCAL8,/* x - LOC[8]=x */
52SETLOCAL9,/* x - LOC[9]=x */ 53SETLOCAL9,/* x - LOC[9]=x */
53SETLOCAL,/* b x - LOC[b]=x */ 54SETLOCAL,/* b x - LOC[b]=x */
54SETGLOBAL,/* w x - VAR[w]=x */ 55SETGLOBALB,/* b x - VAR[CNST[b]]=x */
55SETTABLE0,/* v i t - t[i]=v */ 56SETGLOBAL,/* w x - VAR[CNST[w]]=x */
57SETTABLE0,/* v i t - t[i]=v */
56SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */ 58SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
57SETLIST0,/* b v_b...v_1 t - t[i]=v_i */ 59SETLIST0,/* b v_b...v_1 t - t[i]=v_i */
58SETLIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */ 60SETLIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
diff --git a/lua.stx b/lua.stx
index 4829e9ff..51159737 100644
--- a/lua.stx
+++ b/lua.stx
@@ -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 */
54static struct State { 53typedef 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
68static 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
163static int next_constant (void) 164static 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
175static int string_constant (TaggedString *s) 175static 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
190static void code_string (TaggedString *s) 190static 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
300static int aux_localname (TaggedString *n, struct State *st) 300static 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
309static vardesc singlevar (TaggedString *n, struct State *st) 309static 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)
427static void lua_pushvar (vardesc number) 427static 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*/
582TProtoFunc *luaY_parser (ZIO *z, char *chunkname) 596TProtoFunc *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 ;
diff --git a/lvm.c b/lvm.c
index b4818ea9..50213247 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: lvm.c,v 1.2 1997/09/19 18:40:32 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -326,8 +326,12 @@ StkId luaV_execute (Closure *cl, StkId base)
326 *luaD_stack.top++ = *((luaD_stack.stack+base) + (*pc++)); 326 *luaD_stack.top++ = *((luaD_stack.stack+base) + (*pc++));
327 break; 327 break;
328 328
329 case PUSHGLOBALB:
330 luaV_getglobal(luaG_findsymbol(tsvalue(&func->consts[*pc++])));
331 break;
332
329 case PUSHGLOBAL: 333 case PUSHGLOBAL:
330 luaV_getglobal(get_word(pc)); 334 luaV_getglobal(luaG_findsymbol(tsvalue(&func->consts[get_word(pc)])));
331 break; 335 break;
332 336
333 case GETTABLE: 337 case GETTABLE:
@@ -369,8 +373,12 @@ StkId luaV_execute (Closure *cl, StkId base)
369 case SETLOCAL: 373 case SETLOCAL:
370 *((luaD_stack.stack+base) + (*pc++)) = *(--luaD_stack.top); break; 374 *((luaD_stack.stack+base) + (*pc++)) = *(--luaD_stack.top); break;
371 375
376 case SETGLOBALB:
377 luaV_setglobal(luaG_findsymbol(tsvalue(&func->consts[*pc++])));
378 break;
379
372 case SETGLOBAL: 380 case SETGLOBAL:
373 luaV_setglobal(get_word(pc)); 381 luaV_setglobal(luaG_findsymbol(tsvalue(&func->consts[get_word(pc)])));
374 break; 382 break;
375 383
376 case SETTABLE0: 384 case SETTABLE0: