From 33b8a010324863ddb495768ebe9e92403c5116c8 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Thu, 25 Nov 1999 16:59:43 -0200
Subject: [PATCH] new way to code CALLs + passing multiple arguments between
 function calls

---
 lopcodes.h | 26 ++++++++++++--------------
 lparser.c  | 17 +++++++----------
 lvm.c      | 11 +++--------
 3 files changed, 22 insertions(+), 32 deletions(-)

diff --git a/lopcodes.h b/lopcodes.h
index f9633cac..f6e81bff 100644
--- a/lopcodes.h
+++ b/lopcodes.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lopcodes.h,v 1.32 1999/03/10 14:09:45 roberto Exp roberto $
+** $Id: lopcodes.h,v 1.33 1999/06/17 17:04:03 roberto Exp roberto $
 ** Opcodes for Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -20,12 +20,12 @@ typedef enum {
 ENDCODE,/*	-	-		(return)			*/
 RETCODE,/*	b	-		(return)			*/
 
-CALL,/*		b c	v_c...v_1 f	r_b...r_1	f(v1,...,v_c)	*/
+CALL,/*		b c	v_n-v_1 f(at c)	r_b-r_1		f(v1,...,v_n)	*/
 
-TAILCALL,/*	b c	v_c...v_1 f	(return)	f(v1,...,v_c)	*/
+TAILCALL,/*	b c	v_c-v_1 f	(return)	f(v1,...,v_c)	*/
 
-PUSHNIL,/*	b	-		nil_0...nil_b			*/
-POP,/*		b	a_b...a_1	-				*/
+PUSHNIL,/*	b	-		nil_0-nil_b			*/
+POP,/*		b	a_b-a_1		-				*/
 
 PUSHNUMBERW,/*	w	-		(float)w			*/
 PUSHNUMBER,/*	b	-		(float)b			*/
@@ -61,12 +61,12 @@ SETGLOBAL,/*	b	x		-		VAR[CNST[b]]=x	*/
 
 SETTABLEPOP,/*	-	v i t		-		t[i]=v		*/
 
-SETTABLE,/*	b	v a_b...a_1 i t	 a_b...a_1 i t	  t[i]=v	*/
+SETTABLE,/*	b	v a_b-a_1 i t	 a_b-a_1 i t	  t[i]=v	*/
 
-SETLISTW,/*	w c	v_c...v_1 t	t		t[i+w*FPF]=v_i	*/
-SETLIST,/*	b c	v_c...v_1 t	t		t[i+b*FPF]=v_i	*/
+SETLISTW,/*	w c	v_c-v_1 t	t		t[i+w*FPF]=v_i	*/
+SETLIST,/*	b c	v_c-v_1 t	t		t[i+b*FPF]=v_i	*/
 
-SETMAP,/*	b	v_b k_b ...v_0 k_0 t	t	t[k_i]=v_i	*/
+SETMAP,/*	b	v_b k_b - v_0 k_0 t	t	t[k_i]=v_i	*/
 
 NEQOP,/*	-	y x		(x~=y)? 1 : nil			*/
 EQOP,/*		-	y x		(x==y)? 1 : nil			*/
@@ -96,16 +96,14 @@ IFTUPJMP,/*	b	x		-		(x!=nil)? PC-=b	*/
 IFFUPJMPW,/*	w	x		-		(x==nil)? PC-=w	*/
 IFFUPJMP,/*	b	x		-		(x==nil)? PC-=b	*/
 
-CLOSUREW,/*	w c	v_c...v_1	closure(CNST[w], v_c...v_1)	*/
-CLOSURE,/*	b c	v_c...v_1	closure(CNST[b], v_c...v_1)	*/
+CLOSUREW,/*	w c	v_c-v_1		closure(CNST[w], v_c-v_1)	*/
+CLOSURE,/*	b c	v_c-v_1		closure(CNST[b], v_c-v_1)	*/
 
 SETLINEW,/*	w	-		-		LINE=w		*/
 SETLINE,/*	b	-		-		LINE=b		*/
 
 LONGARGW,/*	w	(add w*(1<<16) to arg of next instruction)	*/
-LONGARG,/*	b	(add b*(1<<16) to arg of next instruction)	*/
-
-CHECKSTACK /*	b  (assert #temporaries == b; only for internal debuging!) */
+LONGARG /*	b	(add b*(1<<16) to arg of next instruction)	*/
 
 } OpCode;
 
diff --git a/lparser.c b/lparser.c
index c96ea0d0..4305cc1d 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lparser.c,v 1.42 1999/11/04 17:23:12 roberto Exp roberto $
+** $Id: lparser.c,v 1.43 1999/11/22 13:12:07 roberto Exp roberto $
 ** LL(1) Parser and code generator for Lua
 ** See Copyright Notice in lua.h
 */
@@ -426,12 +426,9 @@ static void close_exp (LexState *ls, int pc, int nresults) {
   if (pc > 0) {  /* expression is an open function call? */
     Byte *code = ls->fs->f->code;
     code[pc-1] = (Byte)nresults;  /* set nresults */
-    /* push results, pop params (at code[pc]) and function */
-    deltastack(ls, nresults-(code[pc]+1));
+    if (nresults != MULT_RET)
+      deltastack(ls, nresults);  /* push results */
   }
-#ifdef DEBUG
-  code_oparg(ls, CHECKSTACK, ls->fs->stacksize, 0);
-#endif
 }
 
 
@@ -1152,7 +1149,7 @@ static void var_or_func_tail (LexState *ls, vardesc *v) {
 
 static int funcparams (LexState *ls, int slf) {
   FuncState *fs = ls->fs;
-  int nparams = 1;  /* in cases STRING and constructor */
+  int slevel = fs->stacksize - slf - 1;  /* where is func in the stack */
   switch (ls->token) {
     case '(': {  /* funcparams -> '(' explist ')' */
       int line = ls->linenumber;
@@ -1160,8 +1157,7 @@ static int funcparams (LexState *ls, int slf) {
       next(ls);
       explist(ls, &e);
       check_match(ls, ')', '(', line);
-      close_exp(ls, e.pc, 1);
-      nparams = e.n;
+      close_exp(ls, e.pc, MULT_RET);  /* close 1 for old semantics */
       break;
     }
 
@@ -1180,7 +1176,8 @@ static int funcparams (LexState *ls, int slf) {
   }
   code_byte(ls, CALL);
   code_byte(ls, 0);  /* save space for nresult */
-  code_byte(ls, (Byte)(nparams+slf));
+  code_byte(ls, (Byte)slevel);
+  fs->stacksize = slevel;  /* call will remove func and params */
   return fs->pc-1;
 }
 
diff --git a/lvm.c b/lvm.c
index 05840302..2a906dc4 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.65 1999/11/04 17:22:26 roberto Exp roberto $
+** $Id: lvm.c,v 1.66 1999/11/22 13:12:07 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -325,11 +325,11 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId
         goto ret;
 
       case CALL: aux = *pc++;
-        luaD_calln(L, *pc++, aux);
+        luaD_call(L, (S->stack+base) + *pc++, aux);
         break;
 
       case TAILCALL: aux = *pc++;
-        luaD_calln(L, *pc++, MULT_RET);
+        luaD_call(L, (S->stack+base) + *pc++, MULT_RET);
         base += aux;
         goto ret;
 
@@ -604,11 +604,6 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, StkId
         aux = highbyte(L, highbyte(L, aux));
         goto switchentry;  /* do not reset "aux" */
 
-      case CHECKSTACK: aux = *pc++;
-        LUA_ASSERT(L, (S->top-S->stack)-base == aux && S->last >= S->top,
-                   "wrong stack size");
-        break;
-
     }
   } ret:
   if (L->callhook)