[Python-checkins] python/dist/src/Python newcompile.c,1.1.2.51,1.1.2.52

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Thu, 24 Apr 2003 10:39:28 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv5779

Modified Files:
      Tag: ast-branch
	newcompile.c 
Log Message:
Add some preliminary code to generate a line number table (lnotab).

Untested because the compiler doesn't generate SET_LINENO
instructions, but this branch was made before they were removed from
the interpreter.


Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.51
retrieving revision 1.1.2.52
diff -C2 -d -r1.1.2.51 -r1.1.2.52
*** newcompile.c	3 Apr 2003 13:52:24 -0000	1.1.2.51
--- newcompile.c	24 Apr 2003 17:39:24 -0000	1.1.2.52
***************
*** 70,77 ****
  struct assembler {
  	PyObject *a_bytecode;  /* string containing bytecode */
- 	PyObject *a_lnotab;    /* string containing lnotab */
  	int a_offset;          /* offset into bytecode */
  	int a_nblocks;         /* number of reachable blocks */
  	int *a_postorder;      /* list of block indices in dfs postorder */
  };
  
--- 70,80 ----
  struct assembler {
  	PyObject *a_bytecode;  /* string containing bytecode */
  	int a_offset;          /* offset into bytecode */
  	int a_nblocks;         /* number of reachable blocks */
  	int *a_postorder;      /* list of block indices in dfs postorder */
+ 	PyObject *a_lnotab;    /* string containing lnotab */
+ 	int a_lnotab_off;      /* offset into lnotab */
+ 	int a_lineno;          /* last lineno of emitted instruction */
+ 	int a_lineno_off;      /* bytecode offset of last lineno */
  };
  
***************
*** 492,495 ****
--- 495,500 ----
  {
  	struct basicblock *b;
+ 	fprintf(stderr, "set_lineno() set=%d lineno=%d\n",
+ 		c->u->u_lineno_set, c->u->u_lineno);
  	if (c->u->u_lineno_set)
  		return;
***************
*** 2031,2037 ****
--- 2036,2046 ----
  {
  	memset(a, 0, sizeof(struct assembler));
+ 	a->a_lineno = 1;
  	a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
  	if (!a->a_bytecode)
  		return 0;
+ 	a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
+ 	if (!a->a_lnotab)
+ 		return 0;
  	a->a_postorder = (int *)PyObject_Malloc(sizeof(int) * nblocks);
  	if (!a->a_postorder)
***************
*** 2044,2056 ****
  {
  	Py_XDECREF(a->a_bytecode);
  	if (a->a_postorder)
  		PyObject_Free(a->a_postorder);
  }
  
! /* All about c_lnotab.
  
- c_lnotab is an array of unsigned bytes disguised as a Python string.  In -O
- mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
- to source code line #s (when needed for tracebacks) via c_lnotab instead.
  The array is conceptually a list of
      (bytecode offset increment, line number increment)
--- 2053,2111 ----
  {
  	Py_XDECREF(a->a_bytecode);
+ 	Py_XDECREF(a->a_lnotab);
  	if (a->a_postorder)
  		PyObject_Free(a->a_postorder);
  }
  
! /* Return the size of a basic block in bytes. */
! 
! static int
! instrsize(struct instr *instr)
! {
! 	int size = 1;
! 	if (instr->i_hasarg) {
! 		size += 2;
! 		if (instr->i_oparg >> 16)
! 			size += 2;
! 	}
! 	return size;
! }
! 
! static int
! blocksize(struct basicblock *b)
! {
! 	int i;
! 	int size = 0;
! 
! 	for (i = 0; i < b->b_iused; i++)
! 		size += instrsize(&b->b_instr[i]);
! 	return size;
! }
! 
! /* Produce output that looks rather like dis module output. */
! 
! static void
! assemble_display(struct assembler *a, struct instr *i)
! {
!     /* Dispatch the simple case first. */
!     if (!i->i_hasarg) {
! 	    fprintf(stderr, "%5d %-20.20s %d\n",
! 		    a->a_offset, opnames[i->i_opcode], i->i_lineno);
! 	    return;
!     }
!     
!     fprintf(stderr, "%5d %-20.20s %d %d",
! 	    a->a_offset, opnames[i->i_opcode], i->i_oparg, i->i_lineno);
!     if (i->i_jrel) 
! 	    fprintf(stderr, " (to %d)", a->a_offset + i->i_oparg + 3);
!     fprintf(stderr, "\n");
! }
! 
! /* All about a_lnotab.
! 
! c_lnotab is an array of unsigned bytes disguised as a Python string.
! It is used to map bytecode offsets to source code line #s (when needed
! for tracebacks).
  
  The array is conceptually a list of
      (bytecode offset increment, line number increment)
***************
*** 2093,2144 ****
  
  static int
! assemble_lnotab(struct assembler *a)
  {
! 	return 1;
! }
  
! /* Return the size of a basic block in bytes. */
  
! static int
! instrsize(struct instr *instr)
! {
! 	int size = 1;
! 	if (instr->i_hasarg) {
! 		size += 2;
! 		if (instr->i_oparg >> 16)
! 			size += 2;
! 	}
! 	return size;
! }
  
! static int
! blocksize(struct basicblock *b)
! {
! 	int i;
! 	int size = 0;
  
! 	for (i = 0; i < b->b_iused; i++)
! 		size += instrsize(&b->b_instr[i]);
! 	return size;
  }
  
! /* Produce output that looks rather like dis module output. */
! 
! static void
! assemble_display(struct assembler *a, struct instr *i)
! {
!     /* Dispatch the simple case first. */
!     if (!i->i_hasarg) {
! 	    fprintf(stderr, "%5d %-20.20s\n",
! 		    a->a_offset, opnames[i->i_opcode]);
! 	    return;
!     }
!     
!     fprintf(stderr, "%5d %-20.20s %d",
! 	    a->a_offset, opnames[i->i_opcode], i->i_oparg);
!     if (i->i_jrel) 
! 	    fprintf(stderr, " (to %d)", a->a_offset + i->i_oparg + 3);
!     fprintf(stderr, "\n");
! }
  
  static int
--- 2148,2183 ----
  
  static int
! assemble_lnotab(struct assembler *a, struct instr *i)
  {
! 	int d_bytecode, d_lineno;
! 	int len;
! 	char *lnotab;
  
! 	d_bytecode = a->a_offset - a->a_lineno_off;
! 	d_lineno = i->i_lineno - a->a_lineno;
  
! 	if (d_lineno == 0)
! 		return 1;
  
! 	/* XXX for now */
! 	assert(d_bytecode < 256);
! 	assert(d_lineno < 256);
  
! 	len = PyString_GET_SIZE(a->a_lnotab);
! 	if (a->a_lnotab_off + 2 >= len) {
! 		if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
! 			return 0;
! 	}
! 	lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
! 	*lnotab++ = d_bytecode;
! 	*lnotab++ = d_lineno;
! 	a->a_lnotab_off += 2;
! 	return 1;
  }
  
! /* assemble_emit()
!    Extend the bytecode with a new instruction.
!    Update lnotab if necessary.
! */
  
  static int
***************
*** 2157,2160 ****
--- 2196,2201 ----
  		arg = i->i_oparg;
  	}
+ 	if (i->i_lineno && !assemble_lnotab(a, i))
+ 			return 0;
  	if (a->a_offset + size >= len) {
  		if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
***************
*** 2291,2295 ****
  			filename, c->u->u_name,
  			0,
! 			filename); /* XXX lnotab */
   error:
  	Py_XDECREF(consts);
--- 2332,2336 ----
  			filename, c->u->u_name,
  			0,
! 			a->a_lnotab);
   error:
  	Py_XDECREF(consts);
***************
*** 2324,2329 ****
  	if (!assemble_jump_offsets(&a, c))
  		goto error;
- 	if (!assemble_lnotab(&a))
- 		goto error;
  
  	/* Emit code in reverse postorder from dfs. */
--- 2365,2368 ----
***************
*** 2341,2344 ****
--- 2380,2385 ----
  	fprintf(stderr, "\n");
  
+ 	if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
+ 		goto error;
  	if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
  		goto error;