This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[JAVA PATCH] SAVE_EXPR evaluate-once semantics in jcf-write.c


The following patch implements the required evaluate-once semantics
for SAVE_EXPR when generating java bytecodes.  Historically, gcj has
just re-evaluated the saved expression which is less than ideal.

The biggest constraint when implementing SAVE_EXPR is that the only
"state" available is the RTL expression stored in SAVE_EXPR_RTL.
Obviously, jcf-write could care less about RTL, but storing anything
other than valid RTL would likely confuse GCC's garbage collection,
PCH, code-inlining etc...  Fortunately, although we'd like to store
an anonymous DECL, all we really need to encode is the temporary local
variable used to hold the cached expression.  In the JVM, this is
represented by the DECL's DECL_LOCAL_INDEX, which is conveniently
just a integer which can easily be encoded in an RTL CONST_INT.

In theory, we could create a new subroutine, emit_load_or_store_index
to share nine of the lines added below with emit_load_or_store.  Its
a close call, but I decided the benefits of presenting this patch as
a single hunk (for ease of review) outweighted the slight code
duplication.  I'm happy to factor the code if required, perhaps even
as a follow-up patch.  Is anyone concerned with me changing the
emit_load_or_store API to take an integer index and tree type instead
of a DECL?


The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all languages except treelang, and regression tested
with a top-level "make -k check" with no new failures.

Ok for mainline?


2003-09-28  Roger Sayle  <roger@eyesopen.com>

	* jcf-write.c (generate_bytecode_insns): Implement evaluate-once
	semantics for SAVE_EXPR, by caching the result in a temporary.


Index: jcf-write.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/jcf-write.c,v
retrieving revision 1.135
diff -c -3 -p -r1.135 jcf-write.c
*** jcf-write.c	23 Sep 2003 15:49:53 -0000	1.135
--- jcf-write.c	28 Sep 2003 19:31:48 -0000
*************** generate_bytecode_insns (tree exp, int t
*** 2151,2157 ****
        }
        break;
      case SAVE_EXPR:
!       generate_bytecode_insns (TREE_OPERAND (exp, 0), STACK_TARGET, state);
        break;
      case CONVERT_EXPR:
      case NOP_EXPR:
--- 2151,2187 ----
        }
        break;
      case SAVE_EXPR:
!       /* Because the state associated with a SAVE_EXPR tree node must
! 	 be a RTL expression, we use it to store the DECL_LOCAL_INDEX
! 	 of a temporary variable in a CONST_INT.  */
!       if (! SAVE_EXPR_RTL (exp))
! 	{
! 	  tree type = TREE_TYPE (exp);
! 	  tree decl = build_decl (VAR_DECL, NULL_TREE, type);
! 	  generate_bytecode_insns (TREE_OPERAND (exp, 0),
! 				   STACK_TARGET, state);
! 	  localvar_alloc (decl, state);
! 	  SAVE_EXPR_RTL (exp) = GEN_INT (DECL_LOCAL_INDEX (decl));
! 	  emit_dup (TYPE_IS_WIDE (type) ? 2 : 1, 0, state);
! 	  emit_store (decl, state);
! 	}
!       else
! 	{
! 	  /* The following code avoids creating a temporary DECL just
! 	     to pass to emit_load.  This code could be factored with
! 	     the similar implementation in emit_load_or_store.  */
! 	  tree type = TREE_TYPE (exp);
! 	  int kind = adjust_typed_op (type, 4);
! 	  int index = (int) INTVAL (SAVE_EXPR_RTL (exp));
! 	  if (index <= 3)
! 	    {
! 	      RESERVE (1);  /* [ilfda]load_[0123]  */
! 	      OP1 (OPCODE_iload + 5 + 4*kind + index);
! 	    }
! 	  else  /* [ilfda]load  */
! 	    maybe_wide (OPCODE_iload + kind, index, state);
! 	  NOTE_PUSH (TYPE_IS_WIDE (type) ? 2 : 1);
! 	}
        break;
      case CONVERT_EXPR:
      case NOP_EXPR:


Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]