summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lopcodes.h84
-rw-r--r--lparser.c86
-rw-r--r--lvm.c235
3 files changed, 96 insertions, 309 deletions
diff --git a/lopcodes.h b/lopcodes.h
index f4a8a9cc..810d1ebe 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lopcodes.h,v 1.17 1998/03/25 18:52:29 roberto Exp roberto $ 2** $Id: lopcodes.h,v 1.18 1998/06/25 14:37:00 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*/
@@ -8,10 +8,11 @@
8#define lopcodes_h 8#define lopcodes_h
9 9
10 10
11#define NUMOFFSET 100
12
11/* 13/*
12** NOTICE: variants of the same opcode must be consecutive: First, those 14** NOTICE: variants of the same opcode must be consecutive: First, those
13** with byte parameter, then with built-in parameters, and last with 15** with byte parameter, then with word parameter.
14** word parameter.
15*/ 16*/
16 17
17 18
@@ -21,98 +22,34 @@ typedef enum {
21ENDCODE,/* - - - */ 22ENDCODE,/* - - - */
22 23
23PUSHNIL,/* b - nil_0...nil_b */ 24PUSHNIL,/* b - nil_0...nil_b */
24PUSHNIL0,/* - - nil */
25 25
26PUSHNUMBER,/* b - (float)b */ 26PUSHNUMBER,/* b - (float)(b-NUMOFFSET) */
27PUSHNUMBER0,/* - - 0.0 */ 27PUSHNUMBERW,/* w - (float)(w-NUMOFFSET) */
28PUSHNUMBER1,/* - - 1.0 */
29PUSHNUMBER2,/* - - 2.0 */
30PUSHNUMBERW,/* w - (float)w */
31 28
32PUSHCONSTANT,/* b - CNST[b] */ 29PUSHCONSTANT,/* b - CNST[b] */
33PUSHCONSTANT0,/*- - CNST[0] */
34PUSHCONSTANT1,/*- - CNST[1] */
35PUSHCONSTANT2,/*- - CNST[2] */
36PUSHCONSTANT3,/*- - CNST[3] */
37PUSHCONSTANT4,/*- - CNST[4] */
38PUSHCONSTANT5,/*- - CNST[5] */
39PUSHCONSTANT6,/*- - CNST[6] */
40PUSHCONSTANT7,/*- - CNST[7] */
41PUSHCONSTANTW,/*w - CNST[w] */ 30PUSHCONSTANTW,/*w - CNST[w] */
42 31
43PUSHUPVALUE,/* b - Closure[b] */ 32PUSHUPVALUE,/* b - Closure[b] */
44PUSHUPVALUE0,/* - - Closure[0] */
45PUSHUPVALUE1,/* - - Closure[1] */
46 33
47PUSHLOCAL,/* b - LOC[b] */ 34PUSHLOCAL,/* b - LOC[b] */
48PUSHLOCAL0,/* - - LOC[0] */
49PUSHLOCAL1,/* - - LOC[1] */
50PUSHLOCAL2,/* - - LOC[2] */
51PUSHLOCAL3,/* - - LOC[3] */
52PUSHLOCAL4,/* - - LOC[4] */
53PUSHLOCAL5,/* - - LOC[5] */
54PUSHLOCAL6,/* - - LOC[6] */
55PUSHLOCAL7,/* - - LOC[7] */
56 35
57GETGLOBAL,/* b - VAR[CNST[b]] */ 36GETGLOBAL,/* b - VAR[CNST[b]] */
58GETGLOBAL0,/* - - VAR[CNST[0]] */
59GETGLOBAL1,/* - - VAR[CNST[1]] */
60GETGLOBAL2,/* - - VAR[CNST[2]] */
61GETGLOBAL3,/* - - VAR[CNST[3]] */
62GETGLOBAL4,/* - - VAR[CNST[4]] */
63GETGLOBAL5,/* - - VAR[CNST[5]] */
64GETGLOBAL6,/* - - VAR[CNST[6]] */
65GETGLOBAL7,/* - - VAR[CNST[7]] */
66GETGLOBALW,/* w - VAR[CNST[w]] */ 37GETGLOBALW,/* w - VAR[CNST[w]] */
67 38
68GETTABLE,/* - i t t[i] */ 39GETTABLE,/* - i t t[i] */
69 40
70GETDOTTED,/* b t t[CNST[b]] */ 41GETDOTTED,/* b t t[CNST[b]] */
71GETDOTTED0,/* - t t[CNST[0]] */
72GETDOTTED1,/* - t t[CNST[1]] */
73GETDOTTED2,/* - t t[CNST[2]] */
74GETDOTTED3,/* - t t[CNST[3]] */
75GETDOTTED4,/* - t t[CNST[4]] */
76GETDOTTED5,/* - t t[CNST[5]] */
77GETDOTTED6,/* - t t[CNST[6]] */
78GETDOTTED7,/* - t t[CNST[7]] */
79GETDOTTEDW,/* w t t[CNST[w]] */ 42GETDOTTEDW,/* w t t[CNST[w]] */
80 43
81PUSHSELF,/* b t t t[CNST[b]] */ 44PUSHSELF,/* b t t t[CNST[b]] */
82PUSHSELF0,/* - t t t[CNST[0]] */
83PUSHSELF1,/* - t t t[CNST[1]] */
84PUSHSELF2,/* - t t t[CNST[2]] */
85PUSHSELF3,/* - t t t[CNST[3]] */
86PUSHSELF4,/* - t t t[CNST[4]] */
87PUSHSELF5,/* - t t t[CNST[5]] */
88PUSHSELF6,/* - t t t[CNST[6]] */
89PUSHSELF7,/* - t t t[CNST[7]] */
90PUSHSELFW,/* w t t t[CNST[w]] */ 45PUSHSELFW,/* w t t t[CNST[w]] */
91 46
92CREATEARRAY,/* b - newarray(size = b) */ 47CREATEARRAY,/* b - newarray(size = b) */
93CREATEARRAY0,/* - - newarray(size = 0) */
94CREATEARRAY1,/* - - newarray(size = 1) */
95CREATEARRAYW,/* w - newarray(size = w) */ 48CREATEARRAYW,/* w - newarray(size = w) */
96 49
97SETLOCAL,/* b x - LOC[b]=x */ 50SETLOCAL,/* b x - LOC[b]=x */
98SETLOCAL0,/* - x - LOC[0]=x */
99SETLOCAL1,/* - x - LOC[1]=x */
100SETLOCAL2,/* - x - LOC[2]=x */
101SETLOCAL3,/* - x - LOC[3]=x */
102SETLOCAL4,/* - x - LOC[4]=x */
103SETLOCAL5,/* - x - LOC[5]=x */
104SETLOCAL6,/* - x - LOC[6]=x */
105SETLOCAL7,/* - x - LOC[7]=x */
106 51
107SETGLOBAL,/* b x - VAR[CNST[b]]=x */ 52SETGLOBAL,/* b x - VAR[CNST[b]]=x */
108SETGLOBAL0,/* - x - VAR[CNST[0]]=x */
109SETGLOBAL1,/* - x - VAR[CNST[1]]=x */
110SETGLOBAL2,/* - x - VAR[CNST[2]]=x */
111SETGLOBAL3,/* - x - VAR[CNST[3]]=x */
112SETGLOBAL4,/* - x - VAR[CNST[4]]=x */
113SETGLOBAL5,/* - x - VAR[CNST[5]]=x */
114SETGLOBAL6,/* - x - VAR[CNST[6]]=x */
115SETGLOBAL7,/* - x - VAR[CNST[7]]=x */
116SETGLOBALW,/* w x - VAR[CNST[w]]=x */ 53SETGLOBALW,/* w x - VAR[CNST[w]]=x */
117 54
118SETTABLE0,/* - v i t - t[i]=v */ 55SETTABLE0,/* - v i t - t[i]=v */
@@ -120,11 +57,9 @@ SETTABLE0,/* - v i t - t[i]=v */
120SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */ 57SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
121 58
122SETLIST,/* b c v_c...v_1 t - t[i+b*FPF]=v_i */ 59SETLIST,/* b c v_c...v_1 t - t[i+b*FPF]=v_i */
123SETLIST0,/* b v_b...v_1 t - t[i]=v_i */
124SETLISTW,/* w c v_c...v_1 t - t[i+w*FPF]=v_i */ 60SETLISTW,/* w c v_c...v_1 t - t[i+w*FPF]=v_i */
125 61
126SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */ 62SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */
127SETMAP0,/* - v_0 k_0 t t t[k_0]=v_0 */
128 63
129EQOP,/* - y x (x==y)? 1 : nil */ 64EQOP,/* - y x (x==y)? 1 : nil */
130NEQOP,/* - y x (x~=y)? 1 : nil */ 65NEQOP,/* - y x (x~=y)? 1 : nil */
@@ -155,20 +90,15 @@ IFFUPJMP,/* b x - (x==nil)? PC-=b */
155IFFUPJMPW,/* w x - (x==nil)? PC-=w */ 90IFFUPJMPW,/* w x - (x==nil)? PC-=w */
156 91
157CLOSURE,/* b c v_c...v_1 closure(CNST[b], v_c...v_1) */ 92CLOSURE,/* b c v_c...v_1 closure(CNST[b], v_c...v_1) */
158CLOSUREW,/* w c v_b...v_1 closure(CNST[w], v_c...v_1) */
159 93
160CALLFUNC,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */ 94CALLFUNC,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */
161CALLFUNC0,/* b v_b...v_1 f - f(v1,...,v_b) */
162CALLFUNC1,/* b v_b...v_1 f r_1 f(v1,...,v_b) */
163 95
164RETCODE,/* b - - */ 96RETCODE,/* b - - */
165 97
166SETLINE,/* b - - LINE=b */ 98SETLINE,/* b - - LINE=b */
167SETLINEW,/* w - - LINE=w */ 99SETLINEW,/* w - - LINE=w */
168 100
169POP,/* b - - TOP-=(b+1) */ 101POP /* b - - TOP-=(b+1) */
170POP0,/* - - - TOP-=1 */
171POP1/* - - - TOP-=2 */
172 102
173} OpCode; 103} OpCode;
174 104
diff --git a/lparser.c b/lparser.c
index 263277f4..8d632038 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.11 1999/02/01 18:52:05 roberto Exp roberto $ 2** $Id: lparser.c,v 1.12 1999/02/02 13:47:31 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*/
@@ -153,24 +153,19 @@ static void deltastack (LexState *ls, int delta) {
153} 153}
154 154
155 155
156static int code_oparg_at (LexState *ls, int pc, OpCode op, int builtin, 156static int code_oparg_at (LexState *ls, int pc, OpCode op, int arg, int delta) {
157 int arg, int delta) {
158 Byte *code = ls->fs->f->code; 157 Byte *code = ls->fs->f->code;
159 deltastack(ls, delta); 158 deltastack(ls, delta);
160 if (arg < builtin) { 159 if (arg <= 255) {
161 code[pc] = (Byte)(op+1+arg);
162 return 1;
163 }
164 else if (arg <= 255) {
165 code[pc] = (Byte)op; 160 code[pc] = (Byte)op;
166 code[pc+1] = (Byte)arg; 161 code[pc+1] = (Byte)arg;
167 return 2; 162 return 2; /* code size (opcode + 1 byte) */
168 } 163 }
169 else if (arg <= MAX_WORD) { 164 else if (arg <= MAX_WORD) {
170 code[pc] = (Byte)(op+1+builtin); 165 code[pc] = (Byte)(op+1);
171 code[pc+1] = (Byte)(arg>>8); 166 code[pc+1] = (Byte)(arg>>8);
172 code[pc+2] = (Byte)(arg&0xFF); 167 code[pc+2] = (Byte)(arg&0xFF);
173 return 3; 168 return 3; /* code size (opcode + 1 word) */
174 } 169 }
175 else luaX_error(ls, "code too long " MES_LIM("64K") 170 else luaX_error(ls, "code too long " MES_LIM("64K")
176 " (try turning off debug mode)"); 171 " (try turning off debug mode)");
@@ -178,25 +173,21 @@ static int code_oparg_at (LexState *ls, int pc, OpCode op, int builtin,
178} 173}
179 174
180 175
181static int fix_opcode (LexState *ls, int pc, OpCode op, int builtin, int arg) { 176static int fix_opcode (LexState *ls, int pc, OpCode op, int arg) {
182 FuncState *fs = ls->fs; 177 FuncState *fs = ls->fs;
183 TProtoFunc *f = fs->f; 178 TProtoFunc *f = fs->f;
184 if (arg < builtin) { /* close space */ 179 if (arg > 255) { /* open space */
185 luaO_memdown(f->code+pc+1, f->code+pc+2, fs->pc-(pc+2));
186 fs->pc--;
187 }
188 else if (arg > 255) { /* open space */
189 check_pc(fs, 1); 180 check_pc(fs, 1);
190 luaO_memup(f->code+pc+1, f->code+pc, fs->pc-pc); 181 luaO_memup(f->code+pc+1, f->code+pc, fs->pc-pc);
191 fs->pc++; 182 fs->pc++;
192 } 183 }
193 return code_oparg_at(ls, pc, op, builtin, arg, 0) - 2; 184 return code_oparg_at(ls, pc, op, arg, 0) - 2;
194} 185}
195 186
196static void code_oparg (LexState *ls, OpCode op, int builtin, int arg, 187
197 int delta) { 188static void code_oparg (LexState *ls, OpCode op, int arg, int delta) {
198 check_pc(ls->fs, 3); /* maximum code size */ 189 check_pc(ls->fs, 3); /* maximum code size */
199 ls->fs->pc += code_oparg_at(ls, ls->fs->pc, op, builtin, arg, delta); 190 ls->fs->pc += code_oparg_at(ls, ls->fs->pc, op, arg, delta);
200} 191}
201 192
202 193
@@ -207,7 +198,7 @@ static void code_opcode (LexState *ls, OpCode op, int delta) {
207 198
208 199
209static void code_constant (LexState *ls, int c) { 200static void code_constant (LexState *ls, int c) {
210 code_oparg(ls, PUSHCONSTANT, 8, c, 1); 201 code_oparg(ls, PUSHCONSTANT, c, 1);
211} 202}
212 203
213 204
@@ -261,8 +252,9 @@ static int real_constant (FuncState *fs, real r) {
261 252
262static void code_number (LexState *ls, real f) { 253static void code_number (LexState *ls, real f) {
263 int i; 254 int i;
264 if (0 <= f && f <= (real)MAX_WORD && (real)(i=(int)f) == f) 255 if (0 <= f+NUMOFFSET && f+NUMOFFSET <= (real)MAX_WORD &&
265 code_oparg(ls, PUSHNUMBER, 3, i, 1); /* f has a short integer value */ 256 (real)(i=(int)f) == f) /* f+NUMOFFSET has a short integer value? */
257 code_oparg(ls, PUSHNUMBER, i+NUMOFFSET, 1);
266 else 258 else
267 code_constant(ls, real_constant(ls->fs, f)); 259 code_constant(ls, real_constant(ls->fs, f));
268} 260}
@@ -270,13 +262,13 @@ static void code_number (LexState *ls, real f) {
270 262
271static void flush_record (LexState *ls, int n) { 263static void flush_record (LexState *ls, int n) {
272 if (n > 0) 264 if (n > 0)
273 code_oparg(ls, SETMAP, 1, n-1, -2*n); 265 code_oparg(ls, SETMAP, n-1, -2*n);
274} 266}
275 267
276 268
277static void flush_list (LexState *ls, int m, int n) { 269static void flush_list (LexState *ls, int m, int n) {
278 if (n == 0) return; 270 if (n == 0) return;
279 code_oparg(ls, SETLIST, 1, m, -n); 271 code_oparg(ls, SETLIST, m, -n);
280 code_byte(ls->fs, (Byte)n); 272 code_byte(ls->fs, (Byte)n);
281} 273}
282 274
@@ -285,14 +277,12 @@ static void luaI_registerlocalvar (FuncState *fs, TaggedString *varname,
285 int line) { 277 int line) {
286 if (fs->maxvars != -1) { /* debug information? */ 278 if (fs->maxvars != -1) { /* debug information? */
287 TProtoFunc *f = fs->f; 279 TProtoFunc *f = fs->f;
288 if (fs->nvars+2 > fs->maxvars) 280 if (fs->nvars+1 > fs->maxvars)
289 fs->maxvars = luaM_growvector(&f->locvars, fs->maxvars+2, 281 fs->maxvars = luaM_growvector(&f->locvars, fs->maxvars+1,
290 LocVar, "", MAX_WORD); 282 LocVar, "", MAX_WORD);
291 f->locvars[fs->nvars].varname = varname; 283 f->locvars[fs->nvars].varname = varname;
292 f->locvars[fs->nvars].line = line; 284 f->locvars[fs->nvars].line = line;
293 fs->nvars++; 285 fs->nvars++;
294 f->locvars[fs->nvars].line = -1; /* flag end of vector */
295
296 } 286 }
297} 287}
298 288
@@ -369,14 +359,14 @@ static void pushupvalue (LexState *ls, TaggedString *n) {
369 if (aux_localname(ls->fs, n) >= 0) 359 if (aux_localname(ls->fs, n) >= 0)
370 luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str); 360 luaX_syntaxerror(ls, "cannot access an upvalue in current scope", n->str);
371 i = indexupvalue(ls, n); 361 i = indexupvalue(ls, n);
372 code_oparg(ls, PUSHUPVALUE, 2, i, 1); 362 code_oparg(ls, PUSHUPVALUE, i, 1);
373} 363}
374 364
375 365
376 366
377static void check_debugline (LexState *ls) { 367static void check_debugline (LexState *ls) {
378 if (lua_debug && ls->linenumber != ls->fs->lastsetline) { 368 if (lua_debug && ls->linenumber != ls->fs->lastsetline) {
379 code_oparg(ls, SETLINE, 0, ls->linenumber, 0); 369 code_oparg(ls, SETLINE, ls->linenumber, 0);
380 ls->fs->lastsetline = ls->linenumber; 370 ls->fs->lastsetline = ls->linenumber;
381 } 371 }
382} 372}
@@ -384,9 +374,9 @@ static void check_debugline (LexState *ls) {
384 374
385static void adjuststack (LexState *ls, int n) { 375static void adjuststack (LexState *ls, int n) {
386 if (n > 0) 376 if (n > 0)
387 code_oparg(ls, POP, 2, n-1, -n); 377 code_oparg(ls, POP, n-1, -n);
388 else if (n < 0) 378 else if (n < 0)
389 code_oparg(ls, PUSHNIL, 1, (-n)-1, -n); 379 code_oparg(ls, PUSHNIL, (-n)-1, -n);
390} 380}
391 381
392 382
@@ -394,7 +384,7 @@ static void close_exp (LexState *ls, int pc, int nresults) {
394 if (pc > 0) { /* expression is an open function call */ 384 if (pc > 0) { /* expression is an open function call */
395 Byte *code = ls->fs->f->code; 385 Byte *code = ls->fs->f->code;
396 Byte nparams = code[pc]; /* save nparams */ 386 Byte nparams = code[pc]; /* save nparams */
397 pc += fix_opcode(ls, pc-2, CALLFUNC, 2, nresults); 387 pc += fix_opcode(ls, pc-2, CALLFUNC, nresults);
398 code[pc] = nparams; /* restore nparams */ 388 code[pc] = nparams; /* restore nparams */
399 if (nresults != MULT_RET) 389 if (nresults != MULT_RET)
400 deltastack(ls, nresults); /* "push" results */ 390 deltastack(ls, nresults); /* "push" results */
@@ -442,13 +432,13 @@ static void code_args (LexState *ls, int nparams, int dots) {
442static void lua_pushvar (LexState *ls, vardesc *var) { 432static void lua_pushvar (LexState *ls, vardesc *var) {
443 switch (var->k) { 433 switch (var->k) {
444 case VLOCAL: 434 case VLOCAL:
445 code_oparg(ls, PUSHLOCAL, 8, var->info, 1); 435 code_oparg(ls, PUSHLOCAL, var->info, 1);
446 break; 436 break;
447 case VGLOBAL: 437 case VGLOBAL:
448 code_oparg(ls, GETGLOBAL, 8, var->info, 1); 438 code_oparg(ls, GETGLOBAL, var->info, 1);
449 break; 439 break;
450 case VDOT: 440 case VDOT:
451 code_oparg(ls, GETDOTTED, 8, var->info, 0); 441 code_oparg(ls, GETDOTTED, var->info, 0);
452 break; 442 break;
453 case VINDEXED: 443 case VINDEXED:
454 code_opcode(ls, GETTABLE, -1); 444 code_opcode(ls, GETTABLE, -1);
@@ -465,10 +455,10 @@ static void lua_pushvar (LexState *ls, vardesc *var) {
465static void storevar (LexState *ls, vardesc *var) { 455static void storevar (LexState *ls, vardesc *var) {
466 switch (var->k) { 456 switch (var->k) {
467 case VLOCAL: 457 case VLOCAL:
468 code_oparg(ls, SETLOCAL, 8, var->info, -1); 458 code_oparg(ls, SETLOCAL, var->info, -1);
469 break; 459 break;
470 case VGLOBAL: 460 case VGLOBAL:
471 code_oparg(ls, SETGLOBAL, 8, var->info, -1); 461 code_oparg(ls, SETGLOBAL, var->info, -1);
472 break; 462 break;
473 case VINDEXED: 463 case VINDEXED:
474 code_opcode(ls, SETTABLE0, -3); 464 code_opcode(ls, SETTABLE0, -3);
@@ -481,14 +471,14 @@ static void storevar (LexState *ls, vardesc *var) {
481 471
482static int fix_jump (LexState *ls, int pc, OpCode op, int n) { 472static int fix_jump (LexState *ls, int pc, OpCode op, int n) {
483 /* jump is relative to position following jump instruction */ 473 /* jump is relative to position following jump instruction */
484 return fix_opcode(ls, pc, op, 0, n-(pc+JMPSIZE)); 474 return fix_opcode(ls, pc, op, n-(pc+JMPSIZE));
485} 475}
486 476
487 477
488static void fix_upjmp (LexState *ls, OpCode op, int pos) { 478static void fix_upjmp (LexState *ls, OpCode op, int pos) {
489 int delta = ls->fs->pc+JMPSIZE - pos; /* jump is relative */ 479 int delta = ls->fs->pc+JMPSIZE - pos; /* jump is relative */
490 if (delta > 255) delta++; 480 if (delta > 255) delta++;
491 code_oparg(ls, op, 0, delta, 0); 481 code_oparg(ls, op, delta, 0);
492} 482}
493 483
494 484
@@ -516,7 +506,7 @@ static void func_onstack (LexState *ls, FuncState *func) {
516 else { 506 else {
517 for (i=0; i<func->nupvalues; i++) 507 for (i=0; i<func->nupvalues; i++)
518 lua_pushvar(ls, &func->upvalues[i]); 508 lua_pushvar(ls, &func->upvalues[i]);
519 code_oparg(ls, CLOSURE, 0, c, -func->nupvalues+1); 509 code_oparg(ls, CLOSURE, c, -func->nupvalues+1);
520 code_byte(fs, (Byte)func->nupvalues); 510 code_byte(fs, (Byte)func->nupvalues);
521 } 511 }
522} 512}
@@ -862,7 +852,7 @@ static void ifpart (LexState *ls, int isexp, int line) {
862 } 852 }
863 else { /* is exp */ 853 else { /* is exp */
864 if (elsepart) exp1(ls); 854 if (elsepart) exp1(ls);
865 else code_oparg(ls, PUSHNIL, 1, 0, 1); /* empty else exp */ 855 else adjuststack(ls, -1); /* empty else exp -> push nil */
866 } 856 }
867 check_match(ls, END, IF, line); 857 check_match(ls, END, IF, line);
868 } 858 }
@@ -878,7 +868,7 @@ static void ret (LexState *ls) {
878 next(ls); 868 next(ls);
879 explist(ls, &e); 869 explist(ls, &e);
880 close_exp(ls, e.pc, MULT_RET); 870 close_exp(ls, e.pc, MULT_RET);
881 code_oparg(ls, RETCODE, 0, ls->fs->nlocalvar, 0); 871 code_oparg(ls, RETCODE, ls->fs->nlocalvar, 0);
882 ls->fs->stacksize = ls->fs->nlocalvar; /* removes all temp values */ 872 ls->fs->stacksize = ls->fs->nlocalvar; /* removes all temp values */
883 optional(ls, ';'); 873 optional(ls, ';');
884 } 874 }
@@ -1078,7 +1068,7 @@ static void var_or_func_tail (LexState *ls, vardesc *v) {
1078 case ':': /* var_or_func_tail -> ':' NAME funcparams */ 1068 case ':': /* var_or_func_tail -> ':' NAME funcparams */
1079 next(ls); 1069 next(ls);
1080 lua_pushvar(ls, v); /* 'v' must be on stack */ 1070 lua_pushvar(ls, v); /* 'v' must be on stack */
1081 code_oparg(ls, PUSHSELF, 8, checkname(ls), 1); 1071 code_oparg(ls, PUSHSELF, checkname(ls), 1);
1082 v->k = VEXP; 1072 v->k = VEXP;
1083 v->info = funcparams(ls, 1); 1073 v->info = funcparams(ls, 1);
1084 break; 1074 break;
@@ -1242,7 +1232,7 @@ static int assignment (LexState *ls, vardesc *v, int nvars) {
1242 storevar(ls, v); 1232 storevar(ls, v);
1243 } 1233 }
1244 else { /* indexed var with values in between*/ 1234 else { /* indexed var with values in between*/
1245 code_oparg(ls, SETTABLE, 0, left+(nvars-1), -1); 1235 code_oparg(ls, SETTABLE, left+(nvars-1), -1);
1246 left += 2; /* table/index are not popped, because they aren't on top */ 1236 left += 2; /* table/index are not popped, because they aren't on top */
1247 } 1237 }
1248 return left; 1238 return left;
@@ -1267,7 +1257,7 @@ static void constructor (LexState *ls) {
1267 nelems += other_cd.n; 1257 nelems += other_cd.n;
1268 } 1258 }
1269 check_match(ls, '}', '{', line); 1259 check_match(ls, '}', '{', line);
1270 fix_opcode(ls, pc, CREATEARRAY, 2, nelems); 1260 fix_opcode(ls, pc, CREATEARRAY, nelems);
1271} 1261}
1272 1262
1273static void part (LexState *ls, constdesc *cd) { 1263static void part (LexState *ls, constdesc *cd) {
diff --git a/lvm.c b/lvm.c
index 55481f4e..082c7e8f 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 1.40 1999/01/20 20:22:06 roberto Exp roberto $ 2** $Id: lvm.c,v 1.41 1999/01/25 17:39:28 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*/
@@ -31,9 +31,7 @@
31#endif 31#endif
32 32
33 33
34#define skip_word(pc) (pc+=2) 34#define highbyte(x) ((x)<<8)
35#define get_word(pc) ((*(pc)<<8)+(*((pc)+1)))
36#define next_word(pc) (pc+=2, get_word(pc-2))
37 35
38 36
39/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */ 37/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
@@ -315,10 +313,9 @@ static void adjust_varargs (StkId first_extra_arg)
315** [stack+base,top). Returns n such that the the results are between 313** [stack+base,top). Returns n such that the the results are between
316** [stack+n,top). 314** [stack+n,top).
317*/ 315*/
318StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) 316StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) {
319{
320 struct Stack *S = &L->stack; /* to optimize */ 317 struct Stack *S = &L->stack; /* to optimize */
321 Byte *pc = tf->code; 318 register Byte *pc = tf->code;
322 TObject *consts = tf->consts; 319 TObject *consts = tf->consts;
323 if (lua_callhook) 320 if (lua_callhook)
324 luaD_callHook(base, tf, 0); 321 luaD_callHook(base, tf, 0);
@@ -330,54 +327,28 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
330 adjust_varargs(base+(*pc++)-ZEROVARARG); 327 adjust_varargs(base+(*pc++)-ZEROVARARG);
331 } 328 }
332 for (;;) { 329 for (;;) {
333 int aux; 330 register int aux = 0;
334 switch ((OpCode)(aux = *pc++)) { 331 switch ((OpCode)*pc++) {
335
336 case PUSHNIL0:
337 ttype(S->top++) = LUA_T_NIL;
338 break;
339 332
340 case PUSHNIL: 333 case PUSHNIL: aux = *pc++;
341 aux = *pc++;
342 do { 334 do {
343 ttype(S->top++) = LUA_T_NIL; 335 ttype(S->top++) = LUA_T_NIL;
344 } while (aux--); 336 } while (aux--);
345 break; 337 break;
346 338
347 case PUSHNUMBER: 339 case PUSHNUMBERW: aux = highbyte(*pc++);
348 aux = *pc++; goto pushnumber; 340 case PUSHNUMBER: aux += *pc++;
349
350 case PUSHNUMBERW:
351 aux = next_word(pc); goto pushnumber;
352
353 case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2:
354 aux -= PUSHNUMBER0;
355 pushnumber:
356 ttype(S->top) = LUA_T_NUMBER; 341 ttype(S->top) = LUA_T_NUMBER;
357 nvalue(S->top) = aux; 342 nvalue(S->top) = aux-NUMOFFSET;
358 S->top++; 343 S->top++;
359 break; 344 break;
360 345
361 case PUSHLOCAL: 346 case PUSHLOCAL: aux = *pc++;
362 aux = *pc++; goto pushlocal;
363
364 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
365 case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
366 aux -= PUSHLOCAL0;
367 pushlocal:
368 *S->top++ = *((S->stack+base) + aux); 347 *S->top++ = *((S->stack+base) + aux);
369 break; 348 break;
370 349
371 case GETGLOBALW: 350 case GETGLOBALW: aux = highbyte(*pc++);
372 aux = next_word(pc); goto getglobal; 351 case GETGLOBAL: aux += *pc++;
373
374 case GETGLOBAL:
375 aux = *pc++; goto getglobal;
376
377 case GETGLOBAL0: case GETGLOBAL1: case GETGLOBAL2: case GETGLOBAL3:
378 case GETGLOBAL4: case GETGLOBAL5: case GETGLOBAL6: case GETGLOBAL7:
379 aux -= GETGLOBAL0;
380 getglobal:
381 luaV_getglobal(tsvalue(&consts[aux])); 352 luaV_getglobal(tsvalue(&consts[aux]));
382 break; 353 break;
383 354
@@ -385,30 +356,14 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
385 luaV_gettable(); 356 luaV_gettable();
386 break; 357 break;
387 358
388 case GETDOTTEDW: 359 case GETDOTTEDW: aux = highbyte(*pc++);
389 aux = next_word(pc); goto getdotted; 360 case GETDOTTED: aux += *pc++;
390
391 case GETDOTTED:
392 aux = *pc++; goto getdotted;
393
394 case GETDOTTED0: case GETDOTTED1: case GETDOTTED2: case GETDOTTED3:
395 case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7:
396 aux -= GETDOTTED0;
397 getdotted:
398 *S->top++ = consts[aux]; 361 *S->top++ = consts[aux];
399 luaV_gettable(); 362 luaV_gettable();
400 break; 363 break;
401 364
402 case PUSHSELFW: 365 case PUSHSELFW: aux = highbyte(*pc++);
403 aux = next_word(pc); goto pushself; 366 case PUSHSELF: aux += *pc++; {
404
405 case PUSHSELF:
406 aux = *pc++; goto pushself;
407
408 case PUSHSELF0: case PUSHSELF1: case PUSHSELF2: case PUSHSELF3:
409 case PUSHSELF4: case PUSHSELF5: case PUSHSELF6: case PUSHSELF7:
410 aux -= PUSHSELF0;
411 pushself: {
412 TObject receiver = *(S->top-1); 367 TObject receiver = *(S->top-1);
413 *S->top++ = consts[aux]; 368 *S->top++ = consts[aux];
414 luaV_gettable(); 369 luaV_gettable();
@@ -416,49 +371,21 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
416 break; 371 break;
417 } 372 }
418 373
419 case PUSHCONSTANTW: 374 case PUSHCONSTANTW: aux = highbyte(*pc++);
420 aux = next_word(pc); goto pushconstant; 375 case PUSHCONSTANT: aux += *pc++;
421
422 case PUSHCONSTANT:
423 aux = *pc++; goto pushconstant;
424
425 case PUSHCONSTANT0: case PUSHCONSTANT1: case PUSHCONSTANT2:
426 case PUSHCONSTANT3: case PUSHCONSTANT4: case PUSHCONSTANT5:
427 case PUSHCONSTANT6: case PUSHCONSTANT7:
428 aux -= PUSHCONSTANT0;
429 pushconstant:
430 *S->top++ = consts[aux]; 376 *S->top++ = consts[aux];
431 break; 377 break;
432 378
433 case PUSHUPVALUE: 379 case PUSHUPVALUE: aux = *pc++;
434 aux = *pc++; goto pushupvalue;
435
436 case PUSHUPVALUE0: case PUSHUPVALUE1:
437 aux -= PUSHUPVALUE0;
438 pushupvalue:
439 *S->top++ = cl->consts[aux+1]; 380 *S->top++ = cl->consts[aux+1];
440 break; 381 break;
441 382
442 case SETLOCAL: 383 case SETLOCAL: aux = *pc++;
443 aux = *pc++; goto setlocal;
444
445 case SETLOCAL0: case SETLOCAL1: case SETLOCAL2: case SETLOCAL3:
446 case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7:
447 aux -= SETLOCAL0;
448 setlocal:
449 *((S->stack+base) + aux) = *(--S->top); 384 *((S->stack+base) + aux) = *(--S->top);
450 break; 385 break;
451 386
452 case SETGLOBALW: 387 case SETGLOBALW: aux = highbyte(*pc++);
453 aux = next_word(pc); goto setglobal; 388 case SETGLOBAL: aux += *pc++;
454
455 case SETGLOBAL:
456 aux = *pc++; goto setglobal;
457
458 case SETGLOBAL0: case SETGLOBAL1: case SETGLOBAL2: case SETGLOBAL3:
459 case SETGLOBAL4: case SETGLOBAL5: case SETGLOBAL6: case SETGLOBAL7:
460 aux -= SETGLOBAL0;
461 setglobal:
462 luaV_setglobal(tsvalue(&consts[aux])); 389 luaV_setglobal(tsvalue(&consts[aux]));
463 break; 390 break;
464 391
@@ -470,28 +397,17 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
470 luaV_settable(S->top-3-(*pc++), 1); 397 luaV_settable(S->top-3-(*pc++), 1);
471 break; 398 break;
472 399
473 case SETLISTW: 400 case SETLISTW: aux = highbyte(*pc++);
474 aux = next_word(pc); aux *= LFIELDS_PER_FLUSH; goto setlist; 401 case SETLIST: aux += *pc++; {
475
476 case SETLIST:
477 aux = *(pc++) * LFIELDS_PER_FLUSH; goto setlist;
478
479 case SETLIST0:
480 aux = 0;
481 setlist: {
482 int n = *(pc++); 402 int n = *(pc++);
483 TObject *arr = S->top-n-1; 403 TObject *arr = S->top-n-1;
404 aux *= LFIELDS_PER_FLUSH;
484 for (; n; n--) 405 for (; n; n--)
485 luaH_setint(avalue(arr), n+aux, --S->top); 406 luaH_setint(avalue(arr), n+aux, --S->top);
486 break; 407 break;
487 } 408 }
488 409
489 case SETMAP0: 410 case SETMAP: aux = *pc++; {
490 aux = 0; goto setmap;
491
492 case SETMAP:
493 aux = *pc++;
494 setmap: {
495 TObject *arr = S->top-(2*aux)-3; 411 TObject *arr = S->top-(2*aux)-3;
496 do { 412 do {
497 luaH_set(avalue(arr), S->top-2, S->top-1); 413 luaH_set(avalue(arr), S->top-2, S->top-1);
@@ -500,34 +416,23 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
500 break; 416 break;
501 } 417 }
502 418
503 case POP: 419 case POP: aux = *pc++;
504 aux = *pc++; goto pop;
505
506 case POP0: case POP1:
507 aux -= POP0;
508 pop:
509 S->top -= (aux+1); 420 S->top -= (aux+1);
510 break; 421 break;
511 422
512 case CREATEARRAYW: 423 case CREATEARRAYW: aux = highbyte(*pc++);
513 aux = next_word(pc); goto createarray; 424 case CREATEARRAY: aux += *pc++;
514
515 case CREATEARRAY0: case CREATEARRAY1:
516 aux -= CREATEARRAY0; goto createarray;
517
518 case CREATEARRAY:
519 aux = *pc++;
520 createarray:
521 luaC_checkGC(); 425 luaC_checkGC();
522 avalue(S->top) = luaH_new(aux); 426 avalue(S->top) = luaH_new(aux);
523 ttype(S->top) = LUA_T_ARRAY; 427 ttype(S->top) = LUA_T_ARRAY;
524 S->top++; 428 S->top++;
525 break; 429 break;
526 430
527 case EQOP: case NEQOP: { 431 case NEQOP: aux = 1;
432 case EQOP: {
528 int res = luaO_equalObj(S->top-2, S->top-1); 433 int res = luaO_equalObj(S->top-2, S->top-1);
434 if (aux) res = !res;
529 S->top--; 435 S->top--;
530 if (aux == NEQOP) res = !res;
531 ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL; 436 ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
532 nvalue(S->top-1) = 1; 437 nvalue(S->top-1) = 1;
533 break; 438 break;
@@ -630,98 +535,60 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
630 nvalue(S->top-1) = 1; 535 nvalue(S->top-1) = 1;
631 break; 536 break;
632 537
633 case ONTJMPW: 538 case ONTJMPW: aux = highbyte(*pc++);
634 aux = next_word(pc); goto ontjmp; 539 case ONTJMP: aux += *pc++;
635
636 case ONTJMP:
637 aux = *pc++;
638 ontjmp:
639 if (ttype(S->top-1) != LUA_T_NIL) pc += aux; 540 if (ttype(S->top-1) != LUA_T_NIL) pc += aux;
640 else S->top--; 541 else S->top--;
641 break; 542 break;
642 543
643 case ONFJMPW: 544 case ONFJMPW: aux = highbyte(*pc++);
644 aux = next_word(pc); goto onfjmp; 545 case ONFJMP: aux += *pc++;
645
646 case ONFJMP:
647 aux = *pc++;
648 onfjmp:
649 if (ttype(S->top-1) == LUA_T_NIL) pc += aux; 546 if (ttype(S->top-1) == LUA_T_NIL) pc += aux;
650 else S->top--; 547 else S->top--;
651 break; 548 break;
652 549
653 case JMPW: 550 case JMPW: aux = highbyte(*pc++);
654 aux = next_word(pc); goto jmp; 551 case JMP: aux += *pc++;
655
656 case JMP:
657 aux = *pc++;
658 jmp:
659 pc += aux; 552 pc += aux;
660 break; 553 break;
661 554
662 case IFFJMPW: 555 case IFFJMPW: aux = highbyte(*pc++);
663 aux = next_word(pc); goto iffjmp; 556 case IFFJMP: aux += *pc++;
664
665 case IFFJMP:
666 aux = *pc++;
667 iffjmp:
668 if (ttype(--S->top) == LUA_T_NIL) pc += aux; 557 if (ttype(--S->top) == LUA_T_NIL) pc += aux;
669 break; 558 break;
670 559
671 case IFTUPJMPW: 560 case IFTUPJMPW: aux = highbyte(*pc++);
672 aux = next_word(pc); goto iftupjmp; 561 case IFTUPJMP: aux += *pc++;
673
674 case IFTUPJMP:
675 aux = *pc++;
676 iftupjmp:
677 if (ttype(--S->top) != LUA_T_NIL) pc -= aux; 562 if (ttype(--S->top) != LUA_T_NIL) pc -= aux;
678 break; 563 break;
679 564
680 case IFFUPJMPW: 565 case IFFUPJMPW: aux = highbyte(*pc++);
681 aux = next_word(pc); goto iffupjmp; 566 case IFFUPJMP: aux += *pc++;
682
683 case IFFUPJMP:
684 aux = *pc++;
685 iffupjmp:
686 if (ttype(--S->top) == LUA_T_NIL) pc -= aux; 567 if (ttype(--S->top) == LUA_T_NIL) pc -= aux;
687 break; 568 break;
688 569
689 case CLOSUREW: 570 case CLOSURE: aux = *pc++;
690 aux = next_word(pc); goto closure;
691
692 case CLOSURE:
693 aux = *pc++;
694 closure:
695 *S->top++ = consts[aux]; 571 *S->top++ = consts[aux];
696 luaV_closure(*pc++); 572 luaV_closure(*pc++);
697 luaC_checkGC(); 573 luaC_checkGC();
698 break; 574 break;
699 575
700 case CALLFUNC: 576 case CALLFUNC: aux = *pc++; {
701 aux = *pc++; goto callfunc;
702
703 case CALLFUNC0: case CALLFUNC1:
704 aux -= CALLFUNC0;
705 callfunc: {
706 StkId newBase = (S->top-S->stack)-(*pc++); 577 StkId newBase = (S->top-S->stack)-(*pc++);
707 luaD_call(newBase, aux); 578 luaD_call(newBase, aux);
708 break; 579 break;
709 } 580 }
710 581
711 case ENDCODE: 582 case ENDCODE: aux = 1;
712 S->top = S->stack + base; 583 S->top = S->stack + base;
713 /* goes through */ 584 /* goes through */
714 case RETCODE: 585 case RETCODE:
715 if (lua_callhook) 586 if (lua_callhook)
716 luaD_callHook(base, NULL, 1); 587 luaD_callHook(base, NULL, 1);
717 return (base + ((aux==RETCODE) ? *pc : 0)); 588 return base + (aux ? 0 : *pc);
718
719 case SETLINEW:
720 aux = next_word(pc); goto setline;
721 589
722 case SETLINE: 590 case SETLINEW: aux = highbyte(*pc++);
723 aux = *pc++; 591 case SETLINE: aux += *pc++;
724 setline:
725 if ((S->stack+base-1)->ttype != LUA_T_LINE) { 592 if ((S->stack+base-1)->ttype != LUA_T_LINE) {
726 /* open space for LINE value */ 593 /* open space for LINE value */
727 luaD_openstack((S->top-S->stack)-base); 594 luaD_openstack((S->top-S->stack)-base);