[Python-checkins] python/dist/src/Python newcompile.c,1.1.2.2,1.1.2.3

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Fri, 23 Aug 2002 11:35:44 -0700


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

Modified Files:
      Tag: ast-branch
	newcompile.c 
Log Message:
Define Py_OptimizeFlag.

Add _Py_Mangle() which used to be in compile.c.

Add top-level compile code to handle different Module types.

Add compiler_name() to generate the appropriate opcode, which is
    determined from the expression context and the symbol table.

Add compiler_boolop() to generate boolean expressions.

Add placeholder compiler_visit_slice() and compiler_visit_arguments().





Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -C2 -d -r1.1.2.2 -r1.1.2.3
*** newcompile.c	2 Aug 2002 17:52:59 -0000	1.1.2.2
--- newcompile.c	23 Aug 2002 18:35:41 -0000	1.1.2.3
***************
*** 7,10 ****
--- 7,12 ----
  #include "opcode.h"
  
+ int Py_OptimizeFlag = 0;
+ 
  /* fblockinfo tracks the current frame block.
  
***************
*** 30,34 ****
  
  	/* info that changes for each code block */
! 	PySymtableEntryObject *c_symbols;
  	int c_nblocks;
  	int c_curblock;
--- 32,36 ----
  
  	/* info that changes for each code block */
! 	PySTEntryObject *c_ste;
  	int c_nblocks;
  	int c_curblock;
***************
*** 65,68 ****
--- 67,98 ----
  static void compiler_pop_fblock(struct compiler *, enum fblocktype, int);
  
+ int
+ _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
+ {
+ 	/* Name mangling: __private becomes _classname__private.
+ 	   This is independent from how the name is used. */
+ 	size_t nlen, plen;
+ 	if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
+ 		return 0;
+ 	nlen = strlen(name);
+ 	if (nlen+2 >= maxlen)
+ 		return 0; /* Don't mangle __extremely_long_names */
+ 	if (name[nlen-1] == '_' && name[nlen-2] == '_')
+ 		return 0; /* Don't mangle __whatever__ */
+ 	/* Strip leading underscores from class name */
+ 	while (*p == '_')
+ 		p++;
+ 	if (*p == '\0')
+ 		return 0; /* Don't mangle if class is just underscores */
+ 	plen = strlen(p);
+ 	if (plen + nlen >= maxlen)
+ 		plen = maxlen-nlen-2; /* Truncate class name if too long */
+ 	/* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
+ 	buffer[0] = '_';
+ 	strncpy(buffer+1, p, plen);
+ 	strcpy(buffer+1+plen, name);
+ 	return 1;
+ }
+ 
  PyCodeObject *
  PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags)
***************
*** 123,131 ****
  		return 0;
  	}
! 	assert(PySymtableEntry_Check(v));
! 	c->c_symbols = (PySymtableEntryObject *)v;
  
  	c->c_nblocks = 0;
! 	c->c_blocks = (struct basicblock **)malloc(sizeof(struct basicblock *) 
  						   * DEFAULT_BLOCKS);
  	if (!c->c_blocks)
--- 153,161 ----
  		return 0;
  	}
! 	assert(PySTEntry_Check(v));
! 	c->c_ste = (PySTEntryObject *)v;
  
  	c->c_nblocks = 0;
! 	c->c_blocks = (struct basicblock **)malloc(sizeof(struct basicblock *)
  						   * DEFAULT_BLOCKS);
  	if (!c->c_blocks)
***************
*** 145,150 ****
  {
  	/* get the code object for the current block.
! 	   XXX may want to return a thunk insttead
! 	       to allow later passes
  	*/
  	return NULL;
--- 175,179 ----
  {
  	/* get the code object for the current block.
! 	   XXX may want to return a thunk instead to allow later passes
  	*/
  	return NULL;
***************
*** 316,325 ****
--- 345,359 ----
  	switch (mod->kind) {
  	case Module_kind:
+ 		VISIT_SEQ(c, stmt, mod->v.Module.body);
  		break;
  	case Interactive_kind:
+ 		VISIT(c, stmt, mod->v.Interactive.body);
  		break;
  	case Expression_kind:
+ 		VISIT(c, expr, mod->v.Expression.body);
  		break;
  	case Suite_kind:
+ 		assert(0);
+ 		VISIT_SEQ(c, stmt, mod->v.Suite.body);
  		break;
  	}
***************
*** 568,572 ****
  		break;
          case While_kind:
! 		return compiler_if(c, s);
  		break;
          case If_kind:
--- 602,606 ----
  		break;
          case While_kind:
! 		return compiler_while(c, s);
  		break;
          case If_kind:
***************
*** 688,691 ****
--- 722,820 ----
  	return 0;
  }
+ 
+ static int
+ compiler_name(struct compiler *c, expr_ty e)
+ {
+ 	int op, scope;
+ 	enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
+ 
+ 	/* XXX AugStore isn't used anywhere! */
+ 	op = 0;
+ 	optype = OP_NAME;
+ 	scope = PyST_GetScope(c->c_ste, e->v.Name.id);
+ 	switch (scope) {
+ 	case FREE:
+ 	case CELL:
+ 		optype = OP_DEREF;
+ 		break;
+ 	case LOCAL:
+ 		if (c->c_ste->ste_type == FunctionBlock)
+ 			optype = OP_FAST;
+ 		break;
+ 	case GLOBAL_IMPLICIT:
+ 		if (c->c_ste->ste_optimized)
+ 			optype = OP_GLOBAL;
+ 		break;
+ 	case GLOBAL_EXPLICIT:
+ 		optype = OP_GLOBAL;
+ 		break;
+ 	}
+ 
+ 	switch (optype) {
+ 	case OP_DEREF:
+ 		switch (e->v.Name.ctx) {
+ 		case Load: op = LOAD_DEREF; break;
+ 		case Store: op = STORE_DEREF; break;
+ 		case AugStore:
+ 			break;
+ 		case Del:
+ 			assert(0); /* impossible */
+ 		}
+ 	case OP_FAST:
+ 		switch (e->v.Name.ctx) {
+ 		case Load: op = LOAD_FAST; break;
+ 		case Store: op = STORE_FAST; break;
+ 		case Del: op = DELETE_FAST; break;
+ 		case AugStore:
+ 			break;
+ 		}
+ 	case OP_GLOBAL:
+ 		switch (e->v.Name.ctx) {
+ 		case Load: op = LOAD_GLOBAL; break;
+ 		case Store: op = STORE_GLOBAL; break;
+ 		case Del: op = DELETE_GLOBAL; break;
+ 		case AugStore:
+ 			break;
+ 		}
+ 	case OP_NAME:
+ 		switch (e->v.Name.ctx) {
+ 		case Load: op = LOAD_NAME; break;
+ 		case Store: op = STORE_NAME; break;
+ 		case Del: op = DELETE_NAME; break;
+ 		case AugStore:
+ 			break;
+ 		}
+ 	}
+ 	
+ 	assert(op);
+ 	ADDOP_O(c, op, e->v.Name.id);
+ 	return 1;
+ }
+ 
+ static int
+ compiler_boolop(struct compiler *c, expr_ty e)
+ {
+ 	int end, jumpi, i, n;
+ 	asdl_seq *s;
+ 	
+ 	if (e->v.BoolOp.op == And)
+ 		jumpi = JUMP_IF_FALSE;
+ 	else
+ 		jumpi = JUMP_IF_TRUE;
+ 	end = compiler_new_block(c);
+ 	if (!end)
+ 		return 0;
+ 	s = e->v.BoolOp.values;
+ 	n = asdl_seq_LEN(s) - 1;
+ 	for (i = 0; i < n; ++i) {
+ 		VISIT(c, expr, asdl_seq_get(s, i));
+ 		ADDOP_I(c, jumpi, end);
+ 		NEW_BLOCK(c);
+ 		ADDOP(c, POP_TOP)
+ 	}
+ 	VISIT(c, expr, asdl_seq_get(s, n));
+ 	compiler_use_block(c, end);
+ 	return 1;
+ }
  	
  static int 
***************
*** 695,699 ****
  
  	switch (e->kind) {
!         case BoolOp_kind:
  		break;
          case BinOp_kind:
--- 824,829 ----
  
  	switch (e->kind) {
!         case BoolOp_kind: 
! 		return compiler_boolop(c, e);
  		break;
          case BinOp_kind:
***************
*** 758,762 ****
  		VISIT(c, slice, e->v.Subscript.slice);
  		break;
!         case Name_kind:
  		break;
  	/* child nodes of List and Tuple will have expr_context set */
--- 888,893 ----
  		VISIT(c, slice, e->v.Subscript.slice);
  		break;
!         case Name_kind: 
! 		return compiler_name(c, e);
  		break;
  	/* child nodes of List and Tuple will have expr_context set */
***************
*** 819,821 ****
--- 950,964 ----
  	Py_XDECREF(v);
  	return 0;
+ }
+ 
+ static int
+ compiler_visit_slice(struct compiler *c, slice_ty s)
+ {
+ 	return 1;
+ }
+ 
+ static int
+ compiler_visit_arguments(struct compiler *c, arguments_ty a)
+ {
+ 	return 1;
  }