This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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] Implement more java.lang.Math builtins.


The following patch tweaks the gcj front-end to use GCC's built-ins
for the mathematical functions atan, atan2, exp, log, pow and tan
in java.lang.Math builtins.

The simplify the process greatly I took the liberty of changing
the builtin_record structure used the java front-end to record
the available built-in functions.  Rather than require each
individual built-in to provide a "creator" function to build
the required method call, its now possible to alternatively just
specify the built-in index used by GCC's middle-end.  This
mechanism is much more scaleable, and although this patch adds
six additional built-ins, it actually reduces the total size of
java/builtins.c.

This patch also calls "fold" on the tree's constructed for built-in
method calls.  Because "fold" isn't recursive and the top-level tree
nodes aren't problematic for java/fold, this should not only be safe
but will also apply constant folding optimizations at compile-time.

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


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

	* builtins.c (cos_builtin, sin_builtin, sqrt_builtin): Delete.
	(builtin_record): Add an additional builtin_code field to
	record which GCC built-in the corresponds to Java function.
	(java_builtins):  Add new entries for atan, atan2, exp, log,
	pow and tan.
	(max_builtin, min_builtin, abs_builtin): Perform constant
	folding on the resulting tree.
	(java_build_function_call_expr): Likewise, perform constant
	folding on the resulting tree.
	(initialize_builtins): The NULL creators are now allowed in
	the java_builtins table, which is now terminated by an entry
	with builtin_code == END_BUILTINS.
	(check_for_builtin): Likewise.  If the matching creator is
	NULL, construct the call using java_build_function_call_expr
	directly with the decl for the corresponding builtin_code.


Index: builtins.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/builtins.c,v
retrieving revision 1.17
diff -c -3 -p -r1.17 builtins.c
*** builtins.c	2 Mar 2003 01:34:34 -0000	1.17
--- builtins.c	25 May 2003 18:32:58 -0000
*************** enum builtin_type
*** 66,74 ****
  static tree max_builtin (tree, tree);
  static tree min_builtin (tree, tree);
  static tree abs_builtin (tree, tree);
- static tree cos_builtin (tree, tree);
- static tree sin_builtin (tree, tree);
- static tree sqrt_builtin (tree, tree);

  static tree java_build_function_call_expr (tree, tree);
  static void define_builtin (enum built_in_function, const char *,
--- 66,71 ----
*************** struct builtin_record GTY(())
*** 97,113 ****
    union string_or_tree GTY ((desc ("1"))) class_name;
    union string_or_tree GTY ((desc ("1"))) method_name;
    builtin_creator_function * GTY((skip (""))) creator;
  };

  static GTY(()) struct builtin_record java_builtins[] =
  {
!   { { "java.lang.Math" }, { "min" }, min_builtin },
!   { { "java.lang.Math" }, { "max" }, max_builtin },
!   { { "java.lang.Math" }, { "abs" }, abs_builtin },
!   { { "java.lang.Math" }, { "cos" }, cos_builtin },
!   { { "java.lang.Math" }, { "sin" }, sin_builtin },
!   { { "java.lang.Math" }, { "sqrt" }, sqrt_builtin },
!   { { NULL }, { NULL }, NULL }
  };

  /* This is only used transiently, so we don't mark it as roots for the
--- 94,117 ----
    union string_or_tree GTY ((desc ("1"))) class_name;
    union string_or_tree GTY ((desc ("1"))) method_name;
    builtin_creator_function * GTY((skip (""))) creator;
+   enum built_in_function builtin_code;
  };

  static GTY(()) struct builtin_record java_builtins[] =
  {
!   { { "java.lang.Math" }, { "min" }, min_builtin, 0 },
!   { { "java.lang.Math" }, { "max" }, max_builtin, 0 },
!   { { "java.lang.Math" }, { "abs" }, abs_builtin, 0 },
!   { { "java.lang.Math" }, { "atan" }, NULL, BUILT_IN_ATAN },
!   { { "java.lang.Math" }, { "atan2" }, NULL, BUILT_IN_ATAN2 },
!   { { "java.lang.Math" }, { "cos" }, NULL, BUILT_IN_COS },
!   { { "java.lang.Math" }, { "exp" }, NULL, BUILT_IN_EXP },
!   { { "java.lang.Math" }, { "log" }, NULL, BUILT_IN_LOG },
!   { { "java.lang.Math" }, { "pow" }, NULL, BUILT_IN_POW },
!   { { "java.lang.Math" }, { "sin" }, NULL, BUILT_IN_SIN },
!   { { "java.lang.Math" }, { "sqrt" }, NULL, BUILT_IN_SQRT },
!   { { "java.lang.Math" }, { "tan" }, NULL, BUILT_IN_TAN },
!   { { NULL }, { NULL }, NULL, END_BUILTINS }
  };

  /* This is only used transiently, so we don't mark it as roots for the
*************** static tree builtin_types[(int) BT_LAST]
*** 120,143 ****
  static tree
  max_builtin (tree method_return_type, tree method_arguments)
  {
!   return build (MAX_EXPR, method_return_type,
! 		TREE_VALUE (method_arguments),
! 		TREE_VALUE (TREE_CHAIN (method_arguments)));
  }

  static tree
  min_builtin (tree method_return_type, tree method_arguments)
  {
!   return build (MIN_EXPR, method_return_type,
! 		TREE_VALUE (method_arguments),
! 		TREE_VALUE (TREE_CHAIN (method_arguments)));
  }

  static tree
  abs_builtin (tree method_return_type, tree method_arguments)
  {
!   return build1 (ABS_EXPR, method_return_type,
! 		 TREE_VALUE (method_arguments));
  }

  /* Mostly copied from ../builtins.c.  */
--- 124,147 ----
  static tree
  max_builtin (tree method_return_type, tree method_arguments)
  {
!   return fold (build (MAX_EXPR, method_return_type,
! 		      TREE_VALUE (method_arguments),
! 		      TREE_VALUE (TREE_CHAIN (method_arguments))));
  }

  static tree
  min_builtin (tree method_return_type, tree method_arguments)
  {
!   return fold (build (MIN_EXPR, method_return_type,
! 		      TREE_VALUE (method_arguments),
! 		      TREE_VALUE (TREE_CHAIN (method_arguments))));
  }

  static tree
  abs_builtin (tree method_return_type, tree method_arguments)
  {
!   return fold (build1 (ABS_EXPR, method_return_type,
! 		       TREE_VALUE (method_arguments)));
  }

  /* Mostly copied from ../builtins.c.  */
*************** java_build_function_call_expr (tree fn,
*** 150,186 ****
    call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
  		     call_expr, arglist);
    TREE_SIDE_EFFECTS (call_expr) = 1;
!   return call_expr;
! }
!
! static tree
! cos_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
! {
!   /* FIXME: this assumes that jdouble and double are the same.  */
!   tree fn = built_in_decls[BUILT_IN_COS];
!   if (fn == NULL_TREE)
!     return NULL_TREE;
!   return java_build_function_call_expr (fn, method_arguments);
! }
!
! static tree
! sin_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
! {
!   /* FIXME: this assumes that jdouble and double are the same.  */
!   tree fn = built_in_decls[BUILT_IN_SIN];
!   if (fn == NULL_TREE)
!     return NULL_TREE;
!   return java_build_function_call_expr (fn, method_arguments);
! }
!
! static tree
! sqrt_builtin (tree method_return_type ATTRIBUTE_UNUSED, tree method_arguments)
! {
!   /* FIXME: this assumes that jdouble and double are the same.  */
!   tree fn = built_in_decls[BUILT_IN_SQRT];
!   if (fn == NULL_TREE)
!     return NULL_TREE;
!   return java_build_function_call_expr (fn, method_arguments);
  }


--- 154,160 ----
    call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
  		     call_expr, arglist);
    TREE_SIDE_EFFECTS (call_expr) = 1;
!   return fold (call_expr);
  }


*************** initialize_builtins (void)
*** 263,269 ****
  {
    int i;

!   for (i = 0; java_builtins[i].creator != NULL; ++i)
      {
        tree klass_id = get_identifier (java_builtins[i].class_name.s);
        tree m = get_identifier (java_builtins[i].method_name.s);
--- 237,243 ----
  {
    int i;

!   for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
      {
        tree klass_id = get_identifier (java_builtins[i].class_name.s);
        tree m = get_identifier (java_builtins[i].method_name.s);
*************** check_for_builtin (tree method, tree cal
*** 331,343 ****
        tree method_name = DECL_NAME (method);
        tree method_return_type = TREE_TYPE (TREE_TYPE (method));

!       for (i = 0; java_builtins[i].creator != NULL; ++i)
  	{
  	  if (method_class == java_builtins[i].class_name.t
  	      && method_name == java_builtins[i].method_name.t)
  	    {
! 	      return (*java_builtins[i].creator) (method_return_type,
! 						  method_arguments);
  	    }
  	}
      }
--- 305,324 ----
        tree method_name = DECL_NAME (method);
        tree method_return_type = TREE_TYPE (TREE_TYPE (method));

!       for (i = 0; java_builtins[i].builtin_code != END_BUILTINS; ++i)
  	{
  	  if (method_class == java_builtins[i].class_name.t
  	      && method_name == java_builtins[i].method_name.t)
  	    {
! 	      tree fn;
!
! 	      if (java_builtins[i].creator != NULL)
! 		return (*java_builtins[i].creator) (method_return_type,
! 						    method_arguments);
! 	      fn = built_in_decls[java_builtins[i].builtin_code];
! 	      if (fn == NULL_TREE)
! 		return NULL_TREE;
! 	      return java_build_function_call_expr (fn, method_arguments);
  	    }
  	}
      }


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]