[Python-checkins] python/dist/src/Python newcompile.c, 1.1.2.93, 1.1.2.94

jhylton at users.sourceforge.net jhylton at users.sourceforge.net
Thu Apr 22 12:11:18 EDT 2004


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24660

Modified Files:
      Tag: ast-branch
	newcompile.c 
Log Message:
Fix code generation for extended slices.

The AST should probably be changed for slices, because an
ExtendedSlice is sufficiently unlike any of the other cases.  In
particular, an ExtSlice can contain any of the other slice types, and
it is the only slice type that can do this.

Also, fix stack depth calculation for STORE+n opcodes.


Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.93
retrieving revision 1.1.2.94
diff -C2 -d -r1.1.2.93 -r1.1.2.94
*** newcompile.c	21 Apr 2004 14:41:24 -0000	1.1.2.93
--- newcompile.c	22 Apr 2004 16:11:15 -0000	1.1.2.94
***************
*** 700,710 ****
  
  		case SLICE+0:
! 			return 0;
  		case SLICE+1:
! 			return -1;
  		case SLICE+2:
! 			return -1;
  		case SLICE+3:
! 			return -2;
  
  		case STORE_SLICE+0:
--- 700,710 ----
  
  		case SLICE+0:
! 			return 1;
  		case SLICE+1:
! 			return 0;
  		case SLICE+2:
! 			return 0;
  		case SLICE+3:
! 			return -1;
  
  		case STORE_SLICE+0:
***************
*** 2573,2576 ****
--- 2573,2577 ----
      int op;
  
+     /* XXX this code is duplicated */
      switch (ctx) {
      case AugLoad: /* fall through to Load */
***************
*** 2579,2583 ****
      case Store:   op = STORE_SUBSCR; break;
      case Del:     op = DELETE_SUBSCR; break;
!     default:
          fprintf(stderr, "invalid %s kind %d in compiler_visit_slice\n", 
                  kind, ctx);
--- 2580,2584 ----
      case Store:   op = STORE_SUBSCR; break;
      case Del:     op = DELETE_SUBSCR; break;
!     case Param:
          fprintf(stderr, "invalid %s kind %d in compiler_visit_slice\n", 
                  kind, ctx);
***************
*** 2595,2650 ****
  
  static int
! compiler_slice(struct compiler *c, slice_ty s, int op, expr_context_ty ctx)
  {
! 	int slice_offset = 0, stack_count = 0;
  	assert(s->kind == Slice_kind);
  
!         /* XXX: is this right?  is ExtSlice ever used? */
! 	if (s->v.Slice.step) {
!             if (s->v.Slice.lower) {
                  VISIT(c, expr, s->v.Slice.lower);
!             }
!             else {
                  ADDOP_O(c, LOAD_CONST, Py_None, consts);
!             }
                  
!             if (s->v.Slice.upper) {
                  VISIT(c, expr, s->v.Slice.upper);
!             }
!             else {
                  ADDOP_O(c, LOAD_CONST, Py_None, consts);
!             }
  
!             VISIT(c, expr, s->v.Slice.step);
!             ADDOP_I(c, BUILD_SLICE, 3);   /* XXX: always 3? remove arg? */
!             return compiler_handle_subscr(c, "extended slice", ctx);
!         }
  
  	if (s->v.Slice.lower) {
  		stack_count++;
! 		slice_offset |= 1;
! 		if (ctx != AugStore)
  			VISIT(c, expr, s->v.Slice.lower);
  	}
  	if (s->v.Slice.upper) {
  		stack_count++;
! 		slice_offset |= 2;
! 		if (ctx != AugStore)
  			VISIT(c, expr, s->v.Slice.upper);
  	}
  
! 	if (ctx == AugLoad) {
! 		switch (stack_count) {
! 		case 0: ADDOP(c, DUP_TOP); break;
! 		case 1: ADDOP_I(c, DUP_TOPX, 2); break;
! 		case 2: ADDOP_I(c, DUP_TOPX, 3); break;
! 		}
! 	}
! 	else if (ctx == AugStore) {
! 		switch (stack_count) {
! 		case 0: ADDOP(c, ROT_TWO); break;
! 		case 1: ADDOP(c, ROT_THREE); break;
! 		case 2: ADDOP(c, ROT_FOUR); break;
! 		}
  	}
  
--- 2596,2670 ----
  
  static int
! compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
  {
! 	int n = 2;
  	assert(s->kind == Slice_kind);
  
! 	/* only handles the cases where BUILD_SLICE is emitted */
! 	if (s->v.Slice.lower) {
                  VISIT(c, expr, s->v.Slice.lower);
! 	}
! 	else {
                  ADDOP_O(c, LOAD_CONST, Py_None, consts);
! 	}
                  
! 	if (s->v.Slice.upper) {
                  VISIT(c, expr, s->v.Slice.upper);
! 	}
! 	else {
                  ADDOP_O(c, LOAD_CONST, Py_None, consts);
! 	}
  
! 	if (s->v.Slice.step) {
! 		n++;
! 		VISIT(c, expr, s->v.Slice.step);
! 	}
! 	ADDOP_I(c, BUILD_SLICE, n);
! 	return 1;
! }
! 
! static int
! compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
! {
! 	int op, slice_offset = 0, stack_count = 0;
  
+ 	assert(s->v.Slice.step == NULL);
  	if (s->v.Slice.lower) {
+ 		slice_offset++;
  		stack_count++;
! 		if (ctx != AugStore) 
  			VISIT(c, expr, s->v.Slice.lower);
  	}
  	if (s->v.Slice.upper) {
+ 		slice_offset += 2;
  		stack_count++;
! 		if (ctx != AugStore) 
  			VISIT(c, expr, s->v.Slice.upper);
  	}
  
!  	if (ctx == AugLoad) {
!  		switch (stack_count) {
!  		case 0: ADDOP(c, DUP_TOP); break;
!  		case 1: ADDOP_I(c, DUP_TOPX, 2); break;
!  		case 2: ADDOP_I(c, DUP_TOPX, 3); break;
!  		}
!   	}
!  	else if (ctx == AugStore) {
!  		switch (stack_count) {
!  		case 0: ADDOP(c, ROT_TWO); break;
!  		case 1: ADDOP(c, ROT_THREE); break;
!  		case 2: ADDOP(c, ROT_FOUR); break;
!   		}
!   	}
! 
! 	switch (ctx) {
! 	case AugLoad: /* fall through to Load */
! 	case Load: op = SLICE; break;
! 	case AugStore:/* fall through to Store */
! 	case Store: op = STORE_SLICE; break;
! 	case Del: op = DELETE_SLICE; break;
! 	case Param:  /* XXX impossible? */
! 		fprintf(stderr, "param invalid\n");
! 		assert(0);
  	}
  
***************
*** 2654,2660 ****
  
  static int
  compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
  {
- 	int op;
  	switch (s->kind) {
  	case Ellipsis_kind:
--- 2674,2701 ----
  
  static int
+ compiler_visit_nested_slice(struct compiler *c, slice_ty s, 
+ 			    expr_context_ty ctx)
+ {
+ 	switch (s->kind) {
+ 	case Ellipsis_kind:
+ 		ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
+ 		break;
+ 	case Slice_kind:
+ 		return compiler_slice(c, s, ctx);
+ 		break;
+ 	case Index_kind:
+ 		VISIT(c, expr, s->v.Index.value);
+ 		break;
+ 	case ExtSlice_kind:
+ 		assert(0);
+ 		break;
+ 	}
+ 	return 1;
+ }
+ 
+ 
+ static int
  compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
  {
  	switch (s->kind) {
  	case Ellipsis_kind:
***************
*** 2662,2680 ****
  		break;
  	case Slice_kind:
! 		switch (ctx) {
! 		case AugLoad: /* fall through to Load */
! 		case Load:    op = SLICE; break;
! 		case AugStore:/* fall through to Store */
! 		case Store:   op = STORE_SLICE; break;
! 		case Del:     op = DELETE_SLICE; break;
! 		default:
! 			fprintf(stderr, "invalid slice kind %d "
! 				"in compiler_visit_slice\n", ctx);
  			return 0;
  		}
!                 return compiler_slice(c, s, op, ctx);
! 	case ExtSlice_kind:
!                 /* XXX: do we need to do anything?  should this be removed? */
  		break;
  	case Index_kind:
                  if (ctx != AugStore)
--- 2703,2729 ----
  		break;
  	case Slice_kind:
! 		if (!s->v.Slice.step) 
! 			return compiler_simple_slice(c, s, ctx);
!                 if (!compiler_slice(c, s, ctx))
  			return 0;
+ 		if (ctx == AugLoad) {
+ 			ADDOP_I(c, DUP_TOPX, 2);
  		}
! 		else if (ctx == AugStore) {
! 			ADDOP(c, ROT_THREE);
! 		}
! 		return compiler_handle_subscr(c, "slice", ctx);
  		break;
+ 	case ExtSlice_kind: {
+ 		int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
+ 		for (i = 0; i < n; i++) {
+ 			slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
+ 			if (!compiler_visit_nested_slice(c, sub, ctx))
+ 				return 0;
+ 		}
+ 		ADDOP_I(c, BUILD_TUPLE, n);
+                 return compiler_handle_subscr(c, "extended slice", ctx);
+ 		break;
+ 	}
  	case Index_kind:
                  if (ctx != AugStore)
***************
*** 2729,2736 ****
  		instr = &b->b_instr[i];
  		depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
- 		assert(depth >= 0); /* invalid code or bug in stackdepth() */
  		if (depth > maxdepth)
  			maxdepth = depth;
! 		fprintf(stderr, "  %s %d\n", opnames[instr->i_opcode], depth);
  		if (instr->i_jrel || instr->i_jabs) {
  			maxdepth = stackdepth_walk(c, instr->i_target,
--- 2778,2787 ----
  		instr = &b->b_instr[i];
  		depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
  		if (depth > maxdepth)
  			maxdepth = depth;
! 		fprintf(stderr, "  %14s %3d %3d (%d)\n", 
! 			opnames[instr->i_opcode], depth, maxdepth,
! 			instr->i_lineno);
! 		assert(depth >= 0); /* invalid code or bug in stackdepth() */
  		if (instr->i_jrel || instr->i_jabs) {
  			maxdepth = stackdepth_walk(c, instr->i_target,




More information about the Python-checkins mailing list