Răsfoiți Sursa

new style for debug information about functions: no more SETFUNCTION
opcodes. When a function is called, its entry in the stack is marked with
LUA_T_(C)MARK, so function 'luaD_stackedfunction' can find it if
needed.
Functions now have their file names in the headers, so there is no need
of 'addfile' and the like.

Roberto Ierusalimschy 30 ani în urmă
părinte
comite
2cffb08a5c
8 a modificat fișierele cu 121 adăugiri și 214 ștergeri
  1. 4 0
      func.h
  2. 9 100
      inout.c
  3. 3 6
      inout.h
  4. 13 22
      lua.stx
  5. 34 25
      opcode.c
  6. 2 4
      opcode.h
  7. 51 51
      table.c
  8. 5 6
      table.h

+ 4 - 0
func.h

@@ -12,6 +12,10 @@ typedef struct TFunc
   char		marked;
   int		size;
   Byte		*code;
+  int		lineDefined;
+  char		*name1;  /* function or method name (or null if main) */
+  char		*name2;  /* object name (or null if not method) */
+  char		*fileName;
 } TFunc;
 
 Long luaI_funccollector (void);

+ 9 - 100
inout.c

@@ -5,7 +5,7 @@
 ** Also provides some predefined lua functions.
 */
 
-char *rcs_inout="$Id: inout.c,v 2.21 1995/10/04 14:20:26 roberto Exp roberto $";
+char *rcs_inout="$Id: inout.c,v 2.22 1995/10/09 13:06:20 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,20 +31,9 @@ char *rcs_inout="$Id: inout.c,v 2.21 1995/10/04 14:20:26 roberto Exp roberto $";
 Word lua_linenumber;
 Bool lua_debug;
 Word lua_debugline = 0;
+char *lua_parsedfile;
 
 
-/* Internal variables */
-
-typedef struct FuncStackNode {
-  struct FuncStackNode *next;
-  char *file;
-  Word function;
-  Word line;
-} FuncStackNode;
- 
-static FuncStackNode *funcStack = NULL;
-static Word nfuncstack=0;
-
 static FILE *fp;
 static char *st;
 
@@ -70,16 +59,17 @@ static int stringinput (void)
 */
 char *lua_openfile (char *fn)
 {
- lua_linenumber = 1;
  lua_setinput (fileinput);
  fp = fopen (fn, "r");
  if (fp == NULL)
  {
    static char buff[255];
-   sprintf(buff, "unable to open file %.230s", fn);
+   sprintf(buff, "unable to open file `%.200s'", fn);
    return buff;
  }
- return lua_addfile (fn);
+ lua_linenumber = 1;
+ lua_parsedfile = lua_constcreate(fn)->ts.str;
+ return NULL;
 }
 
 /*
@@ -89,7 +79,6 @@ void lua_closefile (void)
 {
  if (fp != NULL)
  {
-  lua_delfile();
   fclose (fp);
   fp = NULL;
  }
@@ -98,16 +87,12 @@ void lua_closefile (void)
 /*
 ** Function to open a string to be input unit
 */
-char *lua_openstring (char *s)
+void lua_openstring (char *s)
 {
- lua_linenumber = 1;
  lua_setinput (stringinput);
  st = s;
- {
-  char sn[64];
-  sprintf (sn, "String: %10.10s...", s);
-  return lua_addfile (sn);
- }
+ lua_linenumber = 1;
+ lua_parsedfile = lua_constcreate("(string)")->ts.str;
 }
 
 /*
@@ -115,75 +100,6 @@ char *lua_openstring (char *s)
 */
 void lua_closestring (void)
 {
- lua_delfile();
-}
-
-
-/*
-** Called to execute  SETFUNCTION opcode, this function pushs a function into
-** function stack.
-*/
-void lua_pushfunction (char *file, Word function)
-{
- FuncStackNode *newNode;
- if (nfuncstack++ >= MAXFUNCSTACK)
- {
-  lua_error("function stack overflow");
- }
- newNode = new(FuncStackNode);
- newNode->function = function;
- newNode->file = file;
- newNode->line= lua_debugline;
- newNode->next = funcStack;
- funcStack = newNode;
-}
-
-/*
-** Called to execute RESET opcode, this function pops a function from 
-** function stack.
-*/
-void lua_popfunction (void)
-{
- FuncStackNode *temp = funcStack;
- if (temp == NULL) return;
- --nfuncstack;
- lua_debugline = temp->line;
- funcStack = temp->next;
- luaI_free(temp);
-}
-
-/*
-** Report bug building a message and pushing it on the stack.
-*/
-void luaI_reportbug (char *s, int err)
-{
-  char msg[MAXMESSAGE];
-  strcpy (msg, s);
- if (lua_debugline != 0)
- {
-  if (funcStack)
-  {
-   FuncStackNode *func = funcStack;
-   int line = lua_debugline;
-   sprintf (strchr(msg,0), "\n\tactive stack:\n");
-   do
-   {
-     sprintf (strchr(msg,0),
-       "\t-> function \"%s\" at file \"%s\":%u\n", 
-              lua_constant[func->function]->str, func->file, line);
-     line = func->line;
-     func = func->next;
-     if (err) lua_popfunction();
-   } while (func);
-  }
-  else
-  {
-   sprintf (strchr(msg,0),
-         "\n\tin statement begining at line %u of file \"%s\"", 
-         lua_debugline, lua_filename());
-  }
- }
- lua_pushstring(msg);
 }
 
  
@@ -297,10 +213,3 @@ void luaI_error (void)
   lua_error(s);
 }
 
-void luaI_getstack (void)
-{
-  char *s = lua_getstring(lua_getparam(1));
-  if (s == NULL) s = "";
-  luaI_reportbug(s, 0);
-}
-

+ 3 - 6
inout.h

@@ -1,5 +1,5 @@
 /*
-** $Id: inout.h,v 1.8 1995/05/02 18:43:03 roberto Exp roberto $
+** $Id: inout.h,v 1.9 1995/05/16 17:23:58 roberto Exp roberto $
 */
 
 
@@ -12,21 +12,18 @@
 extern Word lua_linenumber;
 extern Bool lua_debug;
 extern Word lua_debugline;
+extern char *lua_parsedfile;
 
 char *lua_openfile     (char *fn);
 void lua_closefile    (void);
-char *lua_openstring   (char *s);
+void lua_openstring   (char *s);
 void lua_closestring  (void);
-void lua_pushfunction (char *file, Word function);
-void lua_popfunction  (void);
-void luaI_reportbug (char *s, int err);
 
 void    lua_internaldofile (void);
 void    lua_internaldostring (void);
 void    lua_print      (void);
 void    luaI_type       (void);
 void    lua_obj2number (void);
-void	luaI_getstack  (void);
 void	luaI_error     (void);
 
 #endif

+ 13 - 22
lua.stx

@@ -1,6 +1,6 @@
 %{
 
-char *rcs_luastx = "$Id: lua.stx,v 3.19 1995/06/08 19:47:28 roberto Exp $";
+char *rcs_luastx = "$Id: lua.stx,v 3.20 1995/10/04 14:20:26 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -244,17 +244,10 @@ static void init_function (TreeNode *func)
  }
  pc=0; basepc=funcCode; maxcurr=maxcode; 
  nlocalvar=0;
-  if (lua_debug)
-  {
-    code_byte(SETFUNCTION); 
-    code_code((TFunc *)luaI_strdup(lua_file[lua_nfile-1]));
-    code_word(luaI_findconstant(func));
-  }
 }
 
 static void codereturn (void)
 {
-  if (lua_debug) code_byte(RESET); 
   if (nlocalvar == 0)
     code_byte(RETCODE0);
   else
@@ -345,8 +338,8 @@ static void codeIf (Long thenAdd, Long elseAdd)
 static void yyerror (char *s)
 {
  static char msg[256];
- sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"",
-          s, lua_lasttext (), lua_linenumber, lua_filename());
+ sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
+          s, lua_lasttext (), lua_linenumber, lua_parsedfile);
  lua_error (msg);
 }
 
@@ -435,6 +428,7 @@ functionlist : /* empty */
 function     : FUNCTION NAME 
                {
 		init_function($2);
+		$<vInt>$ = lua_linenumber;
 	       }
                body 	 
 	       { 
@@ -442,6 +436,10 @@ function     : FUNCTION NAME
 		luaI_insertfunction($4);  /* may take part in GC */
 	        s_tag(func) = LUA_T_FUNCTION;
                 lua_table[func].object.value.tf = $4;
+		$4->lineDefined = $<vInt>3;
+		$4->name1 = $2->ts.str;
+		$4->name2 = NULL;
+		$4->fileName = lua_parsedfile;
 	       }
 	       ;
 
@@ -449,6 +447,7 @@ method         : FUNCTION NAME ':' NAME
 	       {
 		init_function($4);
 		add_localvar(luaI_findsymbolbyname("self"));
+		$<vInt>$ = lua_linenumber;
 	       }
 	       body
 	       {
@@ -462,6 +461,10 @@ method         : FUNCTION NAME ':' NAME
                 code_code($6);
                 code_byte(STOREINDEXED0);
 		maincode=pc; *initcode=basepc; maxmain=maxcurr;
+		$6->lineDefined = $<vInt>5;
+		$6->name1 = $4->ts.str;
+		$6->name2 = $2->ts.str;
+		$6->fileName = lua_parsedfile;
 	       }
 	       ;
 
@@ -966,17 +969,6 @@ static void PrintCode (Byte *code, Byte *end)
     			printf ("%d    RETCODE   %d\n", p-code, *(++p));
     			p++;
    			break;
-   case SETFUNCTION:
-                        {
-			 CodeCode c1;
-                         CodeWord c2;
-                         int n = p-code;
-                         p++;
-                         get_code(c1,p);
-                         get_word(c2,p);
-                         printf ("%d    SETFUNCTION  %s  %d\n", n, (char *)c1.tf, c2.w);
-                        }
-                        break;
    case SETLINE:
                         {
                          CodeWord c;
@@ -987,7 +979,6 @@ static void PrintCode (Byte *code, Byte *end)
                         }
                         break;
 
-   case RESET:		printf ("%d    RESET\n", (p++)-code); break;
    default:		printf ("%d    Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
   }
  }

+ 34 - 25
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 3.42 1995/10/09 18:45:59 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 3.43 1995/10/13 15:16:25 roberto Exp roberto $";
 
 #include <setjmp.h>
 #include <stdlib.h>
@@ -248,9 +248,15 @@ static void do_call (StkId base, int nResults)
   StkId firstResult;
   Object *func = stack+base-1;
   if (tag(func) == LUA_T_CFUNCTION)
+  {
+    tag(func) = LUA_T_CMARK;
     firstResult = callC(fvalue(func), base);
+  }
   else if (tag(func) == LUA_T_FUNCTION)
+  {
+    tag(func) = LUA_T_MARK;
     firstResult = lua_execute(func->value.tf->code, base);
+  }
   else
   { /* func is not a function */
     call_funcFB(base, nResults);
@@ -313,21 +319,21 @@ static void storesubscript (void)
 /*
 ** Traverse all objects on stack
 */
-void lua_travstack (void (*fn)(Object *))
+void lua_travstack (int (*fn)(Object *))
 {
  Object *o;
  for (o = top-1; o >= stack; o--)
-  fn (o);
+   fn (o);
 }
 
 
 /*
-** Error messages
+** Error messages and debug functions
 */
 
 static void lua_message (char *s)
 {
-  luaI_reportbug(s, 1);
+  lua_pushstring(s);
   callFB(FB_ERROR);
 }
 
@@ -347,6 +353,25 @@ void lua_error (char *s)
 }
 
 
+lua_Object luaD_stackedfunction (int level)
+{
+  Object *p = top;
+  while (--p >= stack)
+    if (p->tag == LUA_T_MARK || p->tag == LUA_T_CMARK)
+      if (level-- == 0)
+        return Ref(p);
+  return LUA_NOOBJECT;
+}
+
+
+void luaD_funcInfo (lua_Object func, char **filename, char **funcname,
+                    char **objname, int *linedefined)
+{
+  return luaI_funcInfo(Address(func), filename, funcname, objname, linedefined);
+}
+
+
+
 /*
 ** Execute a protected call. Assumes that function is at CBase and
 ** parameters are on top of it. Leave nResults on the stack. 
@@ -386,6 +411,9 @@ static int do_protectedmain (void)
   adjustC(1);  /* one slot for the pseudo-function */
   stack[CBase].tag = LUA_T_FUNCTION;
   stack[CBase].value.tf = &tf;
+  tf.lineDefined = 0;
+  tf.name1 = tf.name2 = NULL;
+  tf.fileName = lua_parsedfile;
   tf.code = NULL;
   if (setjmp(myErrorJmp) == 0)
   {
@@ -454,12 +482,7 @@ int lua_dofile (char *filename)
 int lua_dostring (char *string)
 {
   int status;
-  char *message = lua_openstring(string);
-  if (message)
-  {
-    lua_message(message);
-    return 1;
-  }
+  lua_openstring(string);
   status = do_protectedmain();
   lua_closestring();
   return status;
@@ -1138,16 +1161,6 @@ static StkId lua_execute (Byte *pc, StkId base)
    case RETCODE:
      return base+*pc;
 
-   case SETFUNCTION:
-   {
-    CodeCode file;
-    CodeWord func;
-    get_code(file,pc);
-    get_word(func,pc);
-    lua_pushfunction ((char *)file.tf, func.w);
-   }
-   break;
-
    case SETLINE:
    {
     CodeWord code;
@@ -1156,10 +1169,6 @@ static StkId lua_execute (Byte *pc, StkId base)
    }
    break;
 
-   case RESET:
-    lua_popfunction ();
-   break;
-
    default:
     lua_error ("internal error - opcode doesn't match");
   }

+ 2 - 4
opcode.h

@@ -1,6 +1,6 @@
 /*
 ** TeCGraf - PUC-Rio
-** $Id: opcode.h,v 3.11 1995/04/11 17:56:30 celes Exp $
+** $Id: opcode.h,v 3.12 1995/10/04 17:13:02 roberto Exp roberto $
 */
 
 #ifndef opcode_h
@@ -68,9 +68,7 @@ typedef enum
  CALLFUNC,
  RETCODE0,
  RETCODE,
- SETFUNCTION,
  SETLINE,
- RESET
 } OpCode;
 
 #define MULT_RET	255
@@ -149,7 +147,7 @@ void    lua_setinput   (Input fn);	/* from "lex.c" module */
 char   *lua_lasttext   (void);		/* from "lex.c" module */
 int     yylex (void);		        /* from "lex.c" module */
 void    lua_parse      (TFunc *tf);	/* from "lua.stx" module */
-void    lua_travstack (void (*fn)(Object *));
+void    lua_travstack (int (*fn)(Object *));
 Object *luaI_Address (lua_Object o);
 void	luaI_pushobject (Object *o);
 void    luaI_gcFB       (Object *o);

+ 51 - 51
table.c

@@ -3,7 +3,7 @@
 ** Module to control static tables
 */
 
-char *rcs_table="$Id: table.c,v 2.33 1995/10/04 14:20:26 roberto Exp roberto $";
+char *rcs_table="$Id: table.c,v 2.34 1995/10/13 15:16:25 roberto Exp roberto $";
 
 #include <string.h>
 
@@ -28,11 +28,6 @@ static Word lua_nconstant = 0;
 static Long lua_maxconstant = 0;
 
 
-
-#define MAXFILE 	20
-char  		       *lua_file[MAXFILE];
-int      		lua_nfile;
-
 #define GARBAGE_BLOCK 1024
 #define MIN_GARBAGE_BLOCK (GARBAGE_BLOCK/2)
 
@@ -68,8 +63,6 @@ static void lua_initsymbol (void)
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
  n = luaI_findsymbolbyname("setfallback");
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
- n = luaI_findsymbolbyname("getstack");
- s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_getstack;
  n = luaI_findsymbolbyname("error");
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
 }
@@ -154,25 +147,29 @@ Word  luaI_findconstantbyname (char *name)
 /*
 ** Traverse symbol table objects
 */
-void lua_travsymbol (void (*fn)(Object *))
+static char *lua_travsymbol (int (*fn)(Object *))
 {
  Word i;
  for (i=0; i<lua_ntable; i++)
-  fn(&s_object(i));
+  if (fn(&s_object(i)))
+    return luaI_nodebysymbol(i)->ts.str;
+ return NULL;
 }
 
 
 /*
 ** Mark an object if it is a string or a unmarked array.
 */
-void lua_markobject (Object *o)
+int lua_markobject (Object *o)
 {
  if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
    tsvalue(o)->marked = 1;
  else if (tag(o) == LUA_T_ARRAY)
    lua_hashmark (avalue(o));
- else if (o->tag == LUA_T_FUNCTION && !o->value.tf->marked)
+ else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
+           && !o->value.tf->marked)
    o->value.tf->marked = 1;
+ return 0;
 }
 
 
@@ -199,71 +196,40 @@ void lua_pack (void)
 } 
 
 
-/*
-** Add a file name at file table, checking overflow. This function also set
-** the external variable "lua_filename" with the function filename set.
-** Return 0 on success or error message on error.
-*/
-char *lua_addfile (char *fn)
-{
- if (lua_nfile >= MAXFILE)
-   return "too many files";
- if ((lua_file[lua_nfile++] = luaI_strdup (fn)) == NULL)
-   return "not enough memory";
- return NULL;
-}
-
-/*
-** Delete a file from file stack
-*/
-int lua_delfile (void)
-{
- luaI_free(lua_file[--lua_nfile]); 
- return 1;
-}
-
-/*
-** Return the last file name set.
-*/
-char *lua_filename (void)
-{
- return lua_file[lua_nfile-1];
-}
-
 /*
 ** Internal function: return next global variable
 */
 static void lua_nextvar (void)
 {
- char *varname;
- TreeNode *next;
+ Word next;
  lua_Object o = lua_getparam(1);
  if (o == LUA_NOOBJECT)
    lua_error("too few arguments to function `nextvar'");
  if (lua_getparam(2) != LUA_NOOBJECT)
    lua_error("too many arguments to function `nextvar'");
  if (lua_isnil(o))
-   varname = NULL;
+   next = 0;
  else if (!lua_isstring(o))
  {
    lua_error("incorrect argument to function `nextvar'"); 
    return;  /* to avoid warnings */
  }
  else
-   varname = lua_getstring(o);
- next = lua_varnext(varname);
- if (next == NULL)
+   next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
+ while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
+ if (next >= lua_ntable)
  {
   lua_pushnil();
   lua_pushnil();
  }
  else
  {
+  TreeNode *t = luaI_nodebysymbol(next);
   Object name;
   tag(&name) = LUA_T_STRING;
-  tsvalue(&name) = &(next->ts);
+  tsvalue(&name) = &(t->ts);
   luaI_pushobject(&name);
-  luaI_pushobject(&s_object(next->varindex));
+  luaI_pushobject(&s_object(next));
  }
 }
 
@@ -286,3 +252,37 @@ static void getglobal (void)
     lua_error("incorrect argument to function `getglobal'");
   lua_pushobject(lua_getglobal(lua_getstring(name)));
 }
+
+
+static lua_CFunction cfunc = NULL;
+static int checkfunc (Object *o)
+{
+  return ((o->tag == LUA_T_CMARK || o->tag == LUA_T_CFUNCTION) &&
+           o->value.f == cfunc);
+}
+
+
+void luaI_funcInfo (struct Object *func, char **filename, char **funcname,
+                    char **objname, int *linedefined)
+{
+  if (func->tag == LUA_T_MARK || func->tag == LUA_T_FUNCTION)
+  {
+    TFunc *f = func->value.tf;
+    *filename = f->fileName;
+    *funcname = f->name1;
+    *objname = f->name2;
+    *linedefined = f->lineDefined;
+  }
+  else if (func->tag == LUA_T_CMARK || func->tag == LUA_T_CFUNCTION)
+  {
+    /* temporario: */
+    cfunc = func->value.f;
+    *filename = "(?)";
+    *objname = 0;
+    *linedefined = 0;
+    *funcname = lua_travsymbol(checkfunc);
+    if (*funcname == NULL)
+      *funcname = luaI_travfallbacks(checkfunc);
+  }
+}
+

+ 5 - 6
table.h

@@ -1,7 +1,7 @@
 /*
 ** Module to control static tables
 ** TeCGraf - PUC-Rio
-** $Id: table.h,v 2.10 1994/12/20 21:20:36 roberto Exp roberto $
+** $Id: table.h,v 2.11 1995/10/13 15:16:25 roberto Exp roberto $
 */
 
 #ifndef table_h
@@ -22,11 +22,10 @@ Word  luaI_findsymbolbyname (char *name);
 Word  luaI_findsymbol      (TreeNode *t);
 Word  luaI_findconstant    (TreeNode *t);
 Word  luaI_findconstantbyname (char *name);
-void  lua_travsymbol      (void (*fn)(Object *));
-void  lua_markobject      (Object *o);
+int   lua_markobject      (Object *o);
 void  lua_pack            (void);
-char *lua_addfile         (char *fn);
-int   lua_delfile 	  (void);
-char *lua_filename        (void);
+
+void luaI_funcInfo (Object *func, char **filename, char **funcname,
+                    char **objname, int *linedefined);
 
 #endif