OSDN Git Service

PR c++/27491
[pf3gnuchains/gcc-fork.git] / gcc / java / expr.c
index a8ae7c6..b2e03fc 100644 (file)
@@ -1,5 +1,5 @@
 /* Process expressions for the GNU compiler for the Java(TM) language.
-   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -16,8 +16,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  
+the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  
 
 Java and all Java-based marks are trademarks or registered trademarks
 of Sun Microsystems, Inc. in the United States and other countries.
@@ -86,6 +86,7 @@ static void java_stack_pop (int);
 static tree build_java_throw_out_of_bounds_exception (tree); 
 static tree build_java_check_indexed_type (tree, tree); 
 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
+static void promote_arguments (void);
 
 static GTY(()) tree operand_type[59];
 
@@ -129,6 +130,10 @@ static GTY(()) tree quick_stack;
 /* A free-list of unused permanent TREE_LIST nodes.  */
 static GTY((deletable)) tree tree_list_free_list;
 
+/* The physical memory page size used in this computer.  See
+   build_field_ref().  */
+static GTY(()) tree page_size;
+
 /* The stack pointer of the Java virtual machine.
    This does include the size of the quick_stack. */
 
@@ -137,6 +142,12 @@ int stack_pointer;
 const unsigned char *linenumber_table;
 int linenumber_count;
 
+/* Largest pc so far in this method that has been passed to lookup_label. */
+int highest_label_pc_this_method = -1;
+
+/* Base value for this method to add to pc to get generated label. */
+int start_label_pc_this_method = 0;
+
 void
 init_expr_processing (void)
 {
@@ -185,10 +196,9 @@ java_truthvalue_conversion (tree expr)
 
     case COND_EXPR:
       /* Distribute the conversion into the arms of a COND_EXPR.  */
-      return fold
-              (build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
-                       java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
-                       java_truthvalue_conversion (TREE_OPERAND (expr, 2))));
+      return fold_build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
+                         java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
+                         java_truthvalue_conversion (TREE_OPERAND (expr, 2)));
 
     case NOP_EXPR:
       /* If this is widening the argument, we can ignore it.  */
@@ -198,8 +208,8 @@ java_truthvalue_conversion (tree expr)
       /* fall through to default */
 
     default:
-      return fold (build2 (NE_EXPR, boolean_type_node,
-                          expr, boolean_false_node));
+      return fold_build2 (NE_EXPR, boolean_type_node,
+                         expr, boolean_false_node);
     }
 }
 
@@ -267,8 +277,8 @@ push_type_0 (tree type)
 void
 push_type (tree type)
 {
-  if (! push_type_0 (type))
-    abort ();
+  int r = push_type_0 (type);
+  gcc_assert (r);
 }
 
 static void
@@ -343,36 +353,24 @@ pop_type_0 (tree type, char **messagep)
     return t;
   if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
     {
-      if (flag_new_verifier)
-       {
-         /* Since the verifier has already run, we know that any
-            types we see will be compatible.  In BC mode, this fact
-            may be checked at runtime, but if that is so then we can
-            assume its truth here as well.  So, we always succeed
-            here, with the expected type.  */
-         return type;
-       }
-      else
-       {
-         if (type == ptr_type_node || type == object_ptr_type_node)
-           return t;
-         else if (t == ptr_type_node)  /* Special case for null reference. */
-           return type;
-         /* This is a kludge, but matches what Sun's verifier does.
-            It can be tricked, but is safe as long as type errors
-            (i.e. interface method calls) are caught at run-time. */
-         else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
-           return object_ptr_type_node;
-         else if (can_widen_reference_to (t, type))
-           return t;
-       }
+      /* If the expected type we've been passed is object or ptr
+        (i.e. void*), the caller needs to know the real type.  */
+      if (type == ptr_type_node || type == object_ptr_type_node)
+        return t;
+
+      /* Since the verifier has already run, we know that any
+        types we see will be compatible.  In BC mode, this fact
+        may be checked at runtime, but if that is so then we can
+        assume its truth here as well.  So, we always succeed
+        here, with the expected type.  */
+      return type;
     }
 
   if (! flag_verify_invocations && flag_indirect_dispatch
       && t == object_ptr_type_node)
     {
       if (type != ptr_type_node)
-       warning ("need to insert runtime check for %s", 
+       warning (0, "need to insert runtime check for %s", 
                 xstrdup (lang_printable_name (type, 0)));
       return type;
     }
@@ -382,6 +380,10 @@ pop_type_0 (tree type, char **messagep)
  fail:
   {
     char *temp = xstrdup (lang_printable_name (type, 0));
+    /* If the stack contains a multi-word type, keep popping the stack until 
+       the real type is found.  */
+    while (t == void_type_node)
+      t = stack_type_map[--stack_pointer];
     *messagep = concat ("expected type '", temp,
                        "' but stack contains '", lang_printable_name (t, 0),
                        "'", NULL);
@@ -497,7 +499,7 @@ can_widen_reference_to (tree source_type, tree target_type)
                          source_type, target_type);
 
       if (!quiet_flag)
-       warning ("assert: %s is assign compatible with %s", 
+       warning (0, "assert: %s is assign compatible with %s", 
                xstrdup (lang_printable_name (target_type, 0)),
                xstrdup (lang_printable_name (source_type, 0)));
       /* Punt everything to runtime.  */
@@ -544,7 +546,7 @@ can_widen_reference_to (tree source_type, tree target_type)
          if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
            {
              if (! quiet_flag)
-               warning ("assert: %s is assign compatible with %s", 
+               warning (0, "assert: %s is assign compatible with %s", 
                         xstrdup (lang_printable_name (target_type, 0)),
                         xstrdup (lang_printable_name (source_type, 0)));
              return 1;
@@ -608,15 +610,13 @@ java_stack_pop (int count)
     {
       tree type, val;
 
-      if (stack_pointer == 0)
-       abort ();
+      gcc_assert (stack_pointer != 0);
 
       type = stack_type_map[stack_pointer - 1];
       if (type == TYPE_SECOND)
        {
          count--;
-         if (stack_pointer == 1 || count <= 0)
-           abort ();
+         gcc_assert (stack_pointer != 1 && count > 0);
 
          type = stack_type_map[stack_pointer - 2];
        }
@@ -641,6 +641,7 @@ java_stack_swap (void)
       || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
     /* Bad stack swap.  */
     abort ();
+  /* Bad stack swap.  */
 
   flush_quick_stack ();
   decl1 = find_stack_slot (stack_pointer - 1, type1);
@@ -680,18 +681,16 @@ java_stack_dup (int size, int offset)
       type = stack_type_map [src_index];
       if (type == TYPE_SECOND)
        {
-         if (src_index <= low_index)
-           /* Dup operation splits 64-bit number.  */
-           abort ();
+         /* Dup operation splits 64-bit number.  */
+         gcc_assert (src_index > low_index);
 
          stack_type_map[dst_index] = type;
          src_index--;  dst_index--;
          type = stack_type_map[src_index];
-         if (! TYPE_IS_WIDE (type))
-           abort ();
+         gcc_assert (TYPE_IS_WIDE (type));
        }
-      else if (TYPE_IS_WIDE (type))
-       abort ();
+      else
+       gcc_assert (! TYPE_IS_WIDE (type));
 
       if (src_index != dst_index)
        {
@@ -730,7 +729,7 @@ build_java_jsr (int target_pc, int return_pc)
 {
   tree where =  lookup_label (target_pc);
   tree ret = lookup_label (return_pc);
-  tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
+  tree ret_label = fold_build1 (ADDR_EXPR, return_address_type_node, ret);
   push_value (ret_label);
   flush_quick_stack ();
   java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
@@ -787,7 +786,7 @@ encode_newarray_type (tree type)
   else if (type == long_type_node)
     return 11;
   else
-    abort ();
+    gcc_unreachable ();
 }
 
 /* Build a call to _Jv_ThrowBadArrayIndex(), the
@@ -813,15 +812,6 @@ build_java_array_length_access (tree node)
   tree array_type = TREE_TYPE (type);
   HOST_WIDE_INT length;
 
-  /* JVM spec: If the arrayref is null, the arraylength instruction
-     throws a NullPointerException.  The only way we could get a node
-     of type ptr_type_node at this point is `aconst_null; arraylength'
-     or something equivalent.  */
-  if (!flag_new_verifier && type == ptr_type_node)
-    return build3 (CALL_EXPR, int_type_node, 
-                  build_address_of (soft_nullpointer_node),
-                  NULL_TREE, NULL_TREE);
-
   if (!is_array_type_p (type))
     {
       /* With the new verifier, we will see an ordinary pointer type
@@ -906,9 +896,9 @@ build_java_arrayaccess (tree array, tree type, tree index)
       tree test;
       tree len = convert (unsigned_int_type_node,
                          build_java_array_length_access (array));
-      test = fold (build2 (GE_EXPR, boolean_type_node, 
-                          convert (unsigned_int_type_node, index),
-                          len));
+      test = fold_build2 (GE_EXPR, boolean_type_node, 
+                         convert (unsigned_int_type_node, index),
+                         len);
       if (! integer_zerop (test))
        {
          throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
@@ -953,17 +943,15 @@ build_java_arraystore_check (tree array, tree object)
     }
   else
     {
-      if (! is_array_type_p (array_type_p))
-       abort ();
+      gcc_assert (is_array_type_p (array_type_p));
 
       /* Get the TYPE_DECL for ARRAY's element type. */
       element_type
        = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
     }
 
-  if (TREE_CODE (element_type) != TYPE_DECL   
-      || TREE_CODE (object_type) != TYPE_DECL)
-    abort ();
+  gcc_assert (TREE_CODE (element_type) == TYPE_DECL
+             && TREE_CODE (object_type) == TYPE_DECL);
 
   if (!flag_store_check)
     return build1 (NOP_EXPR, array_type_p, array);
@@ -1020,33 +1008,14 @@ build_java_arraystore_check (tree array, tree object)
    return unchanged.  */
 
 static tree
-build_java_check_indexed_type (tree array_node, tree indexed_type)
+build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED,
+                              tree indexed_type)
 {
-  tree elt_type;
-
   /* We used to check to see if ARRAY_NODE really had array type.
      However, with the new verifier, this is not necessary, as we know
      that the object will be an array of the appropriate type.  */
 
-  if (flag_new_verifier)
-    return indexed_type;
-
-  if (!is_array_type_p (TREE_TYPE (array_node)))
-    abort ();
-
-  elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
-
-  if (indexed_type == ptr_type_node)
-    return promote_type (elt_type);
-
-  /* BYTE/BOOLEAN store and load are used for both type */
-  if (indexed_type == byte_type_node && elt_type == boolean_type_node)
-    return boolean_type_node;
-
-  if (indexed_type != elt_type )
-    abort ();
-  else
-    return indexed_type;
+  return indexed_type;
 }
 
 /* newarray triggers a call to _Jv_NewPrimArray. This function should be 
@@ -1153,23 +1122,18 @@ expand_java_arraystore (tree rhs_type_node)
   tree index = pop_value (int_type_node);
   tree array_type, array;
 
-  if (flag_new_verifier)
+  /* If we're processing an `aaload' we might as well just pick
+     `Object'.  */
+  if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
     {
-      /* If we're processing an `aaload' we might as well just pick
-        `Object'.  */
-      if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
-       {
-         array_type = build_java_array_type (object_ptr_type_node, -1);
-         rhs_type_node = object_ptr_type_node;
-       }
-      else
-       array_type = build_java_array_type (rhs_type_node, -1);
+      array_type = build_java_array_type (object_ptr_type_node, -1);
+      rhs_type_node = object_ptr_type_node;
     }
   else
-    array_type = ptr_type_node;
+    array_type = build_java_array_type (rhs_type_node, -1);
+
   array = pop_value (array_type);
-  if (flag_new_verifier)
-    array = build1 (NOP_EXPR, promote_type (array_type), array);
+  array = build1 (NOP_EXPR, promote_type (array_type), array);
 
   rhs_type_node    = build_java_check_indexed_type (array, rhs_type_node);
 
@@ -1203,44 +1167,28 @@ expand_java_arrayload (tree lhs_type_node)
   tree array_type;
   tree array_node;
 
-  if (flag_new_verifier)
+  /* If we're processing an `aaload' we might as well just pick
+     `Object'.  */
+  if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
     {
-      /* If we're processing an `aaload' we might as well just pick
-        `Object'.  */
-      if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
-       {
-         array_type = build_java_array_type (object_ptr_type_node, -1);
-         lhs_type_node = object_ptr_type_node;
-       }
-      else
-       array_type = build_java_array_type (lhs_type_node, -1);
+      array_type = build_java_array_type (object_ptr_type_node, -1);
+      lhs_type_node = object_ptr_type_node;
     }
   else
-    array_type = ptr_type_node;
+    array_type = build_java_array_type (lhs_type_node, -1);
   array_node = pop_value (array_type);
-  if (flag_new_verifier)
-    array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
+  array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
 
   index_node = save_expr (index_node);
   array_node = save_expr (array_node);
 
-  if (TREE_TYPE (array_node) == ptr_type_node)
-    /* The only way we could get a node of type ptr_type_node at this
-       point is `aconst_null; arraylength' or something equivalent, so
-       unconditionally throw NullPointerException.  */
-    load_node = build3 (CALL_EXPR, lhs_type_node, 
-                       build_address_of (soft_nullpointer_node),
-                       NULL_TREE, NULL_TREE);
-  else
-    {
-      lhs_type_node = build_java_check_indexed_type (array_node,
-                                                    lhs_type_node);
-      load_node = build_java_arrayaccess (array_node,
-                                         lhs_type_node,
-                                         index_node);
-    }
+  lhs_type_node = build_java_check_indexed_type (array_node,
+                                                lhs_type_node);
+  load_node = build_java_arrayaccess (array_node,
+                                     lhs_type_node,
+                                     index_node);
   if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
-    load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
+    load_node = fold_build1 (NOP_EXPR, int_type_node, load_node);
   push_value (load_node);
 }
 
@@ -1286,7 +1234,7 @@ expand_java_pushc (int ival, tree type)
       value = build_real (type, x);
     }
   else
-    abort ();
+    gcc_unreachable ();
 
   push_value (value);
 }
@@ -1329,7 +1277,7 @@ expand_load_internal (int index, tree type, int pc)
      value into it.  Then we push this new local on the stack.
      Hopefully this all gets optimized out.  */
   copy = build_decl (VAR_DECL, NULL_TREE, type);
-  if (INTEGRAL_TYPE_P (type)
+  if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
       && TREE_TYPE (copy) != TREE_TYPE (var))
     var = convert (type, var);
   java_add_local_var (copy);
@@ -1363,10 +1311,10 @@ java_create_object (tree type)
                     ? alloc_object_node
                     : alloc_no_finalizer_node);
   
-  return build (CALL_EXPR, promote_type (type),
-               build_address_of (alloc_node),
-               build_tree_list (NULL_TREE, build_class_ref (type)),
-               NULL_TREE);
+  return build3 (CALL_EXPR, promote_type (type),
+                build_address_of (alloc_node),
+                build_tree_list (NULL_TREE, build_class_ref (type)),
+                NULL_TREE);
 }
 
 static void
@@ -1501,7 +1449,7 @@ expand_iinc (unsigned int local_var_index, int ival, int pc)
   flush_quick_stack ();
   local_var = find_local_variable (local_var_index, int_type_node, pc);
   constant_value = build_int_cst (NULL_TREE, ival);
-  res = fold (build2 (PLUS_EXPR, int_type_node, local_var, constant_value));
+  res = fold_build2 (PLUS_EXPR, int_type_node, local_var, constant_value);
   java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
   update_aliases (local_var, local_var_index, pc);
 }
@@ -1543,9 +1491,7 @@ build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
        }
     }
 
-  if (! call)
-    abort ();
-                 
+  gcc_assert (call);
   call = build3 (CALL_EXPR, type,
                 build_address_of (call),
                 tree_cons (NULL_TREE, arg1,
@@ -1572,36 +1518,36 @@ build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
     case RSHIFT_EXPR:
       mask = build_int_cst (NULL_TREE,
                            TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
-      arg2 = fold (build2 (BIT_AND_EXPR, int_type_node, arg2, mask));
+      arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
       break;
 
     case COMPARE_L_EXPR:  /* arg1 > arg2 ?  1 : arg1 == arg2 ? 0 : -1 */
     case COMPARE_G_EXPR:  /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 :  1 */
       arg1 = save_expr (arg1);  arg2 = save_expr (arg2);
       {
-       tree ifexp1 = fold (build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
-                                   boolean_type_node, arg1, arg2));
-       tree ifexp2 = fold (build2 (EQ_EXPR, boolean_type_node, arg1, arg2));
-       tree second_compare = fold (build3 (COND_EXPR, int_type_node,
-                                           ifexp2, integer_zero_node,
-                                           op == COMPARE_L_EXPR
-                                           ? integer_minus_one_node
-                                           : integer_one_node));
-       return fold (build3 (COND_EXPR, int_type_node, ifexp1,
-                            op == COMPARE_L_EXPR ? integer_one_node
-                            : integer_minus_one_node,
-                            second_compare));
+       tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
+                                  boolean_type_node, arg1, arg2);
+       tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
+       tree second_compare = fold_build3 (COND_EXPR, int_type_node,
+                                          ifexp2, integer_zero_node,
+                                          op == COMPARE_L_EXPR
+                                          ? integer_minus_one_node
+                                          : integer_one_node);
+       return fold_build3 (COND_EXPR, int_type_node, ifexp1,
+                           op == COMPARE_L_EXPR ? integer_one_node
+                           : integer_minus_one_node,
+                           second_compare);
       }
     case COMPARE_EXPR:
       arg1 = save_expr (arg1);  arg2 = save_expr (arg2);
       {
-       tree ifexp1 = fold (build2 (LT_EXPR, boolean_type_node, arg1, arg2));
-       tree ifexp2 = fold (build2 (GT_EXPR, boolean_type_node, arg1, arg2));
-       tree second_compare = fold (build3 (COND_EXPR, int_type_node,
-                                           ifexp2, integer_one_node,
-                                           integer_zero_node));
-       return fold (build3 (COND_EXPR, int_type_node,
-                            ifexp1, integer_minus_one_node, second_compare));
+       tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
+       tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
+       tree second_compare = fold_build3 (COND_EXPR, int_type_node,
+                                          ifexp2, integer_one_node,
+                                          integer_zero_node);
+       return fold_build3 (COND_EXPR, int_type_node,
+                           ifexp1, integer_minus_one_node, second_compare);
       }      
     case TRUNC_DIV_EXPR:
     case TRUNC_MOD_EXPR:
@@ -1632,7 +1578,7 @@ build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
       break;
     default:  ;
     }
-  return fold (build2 (op, type, arg1, arg2));
+  return fold_build2 (op, type, arg1, arg2);
 }
 
 static void
@@ -1736,20 +1682,32 @@ build_field_ref (tree self_value, tree self_class, tree name)
     }
   else
     {
-      int check = (flag_check_references
-                  && ! (DECL_P (self_value)
-                        && DECL_NAME (self_value) == this_identifier_node));
-
       tree base_type = promote_type (base_class);
+
+      /* CHECK is true if self_value is not the this pointer.  */
+      int check = (! (DECL_P (self_value)
+                     && DECL_NAME (self_value) == this_identifier_node));
+
+      /* Determine whether a field offset from NULL will lie within
+        Page 0: this is necessary on those GNU/Linux/BSD systems that
+        trap SEGV to generate NullPointerExceptions.  
+
+        We assume that Page 0 will be mapped with NOPERM, and that
+        memory may be allocated from any other page, so only field
+        offsets < pagesize are guaranteed to trap.  We also assume
+        the smallest page size we'll encounter is 4k bytes.  */
+      if (! flag_syntax_only && check && ! flag_check_references 
+         && ! flag_indirect_dispatch)
+       {
+         tree field_offset = byte_position (field_decl);
+         if (! page_size)
+           page_size = size_int (4096);              
+         check = ! INT_CST_LT_UNSIGNED (field_offset, page_size);
+       }
+
       if (base_type != TREE_TYPE (self_value))
-       self_value = fold (build1 (NOP_EXPR, base_type, self_value));
-      if (! flag_syntax_only
-         && (flag_indirect_dispatch
-             /* DECL_FIELD_OFFSET == 0 if we have no reference for
-                the field, perhaps because we couldn't find the class
-                in which the field is defined.  
-                FIXME: We should investigate this.  */
-             || DECL_FIELD_OFFSET (field_decl) == 0))
+       self_value = fold_build1 (NOP_EXPR, base_type, self_value);
+      if (! flag_syntax_only && flag_indirect_dispatch)
        {
          tree otable_index
            = build_int_cst (NULL_TREE, get_symbol_table_index 
@@ -1760,18 +1718,30 @@ build_field_ref (tree self_value, tree self_class, tree name)
                      NULL_TREE, NULL_TREE);
          tree address;
 
+         if (DECL_CONTEXT (field_decl) != output_class)
+           field_offset
+             = build3 (COND_EXPR, TREE_TYPE (field_offset),
+                       build2 (EQ_EXPR, boolean_type_node,
+                               field_offset, integer_zero_node),
+                       build3 (CALL_EXPR, void_type_node, 
+                               build_address_of (soft_nosuchfield_node),
+                               build_tree_list (NULL_TREE, otable_index), 
+                               NULL_TREE),
+                       field_offset);
+         
          field_offset = fold (convert (sizetype, field_offset));
+         self_value = java_check_reference (self_value, check);
          address 
-           = fold (build2 (PLUS_EXPR, 
-                           build_pointer_type (TREE_TYPE (field_decl)),
-                           self_value, field_offset));
-         return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
+           = fold_build2 (PLUS_EXPR, 
+                          build_pointer_type (TREE_TYPE (field_decl)),
+                          self_value, field_offset);
+         return fold_build1 (INDIRECT_REF, TREE_TYPE (field_decl), address);
        }
 
       self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
                                            self_value, check);
-      return fold (build3 (COMPONENT_REF, TREE_TYPE (field_decl),
-                          self_value, field_decl, NULL_TREE));
+      return fold_build3 (COMPONENT_REF, TREE_TYPE (field_decl),
+                         self_value, field_decl, NULL_TREE);
     }
 }
 
@@ -1780,7 +1750,9 @@ lookup_label (int pc)
 {
   tree name;
   char buf[32];
-  ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
+  if (pc > highest_label_pc_this_method)
+    highest_label_pc_this_method = pc;
+  ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", start_label_pc_this_method + pc);
   name = get_identifier (buf);
   if (IDENTIFIER_LOCAL_VALUE (name))
     return IDENTIFIER_LOCAL_VALUE (name);
@@ -1835,7 +1807,7 @@ expand_compare (enum tree_code condition, tree value1, tree value2,
                int target_pc)
 {
   tree target = lookup_label (target_pc);
-  tree cond = fold (build2 (condition, boolean_type_node, value1, value2));
+  tree cond = fold_build2 (condition, boolean_type_node, value1, value2);
   java_add_stmt 
     (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
             build1 (GOTO_EXPR, void_type_node, target), 
@@ -1922,12 +1894,11 @@ pop_arguments (tree arg_types)
       tree type = TREE_VALUE (arg_types);
       tree arg = pop_value (type);
 
-      /* With the new verifier we simply cast each argument to its
-        proper type.  This is needed since we lose type information
-        coming out of the verifier.  We also have to do this with the
-        old verifier when we pop an integer type that must be
-        promoted for the function call.  */
-      if (flag_new_verifier && TREE_CODE (type) == POINTER_TYPE)
+      /* We simply cast each argument to its proper type.  This is
+        needed since we lose type information coming out of the
+        verifier.  We also have to do this when we pop an integer
+        type that must be promoted for the function call.  */
+      if (TREE_CODE (type) == POINTER_TYPE)
        arg = build1 (NOP_EXPR, type, arg);
       else if (targetm.calls.promote_prototypes (type)
               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
@@ -1935,7 +1906,7 @@ pop_arguments (tree arg_types)
        arg = convert (integer_type_node, arg);
       return tree_cons (NULL_TREE, arg, tail);
     }
-  abort ();
+  gcc_unreachable ();
 }
 
 /* Attach to PTR (a block) the declaration found in ENTRY. */
@@ -2049,6 +2020,86 @@ build_class_init (tree clas, tree expr)
   return init;
 }
 
+\f
+
+/* Rewrite expensive calls that require stack unwinding at runtime to
+   cheaper alternatives.  The logic here performs thse
+   transformations:
+
+   java.lang.Class.forName("foo") -> java.lang.Class.forName("foo", class$)
+   java.lang.Class.getClassLoader() -> java.lang.Class.getClassLoader(class$)
+
+*/
+
+typedef struct
+{
+  const char *classname;
+  const char *method;
+  const char *signature;
+  const char *new_signature;
+  int flags;
+  tree (*rewrite_arglist) (tree arglist);
+} rewrite_rule;
+
+/* Add this.class to the end of an arglist.  */
+
+static tree 
+rewrite_arglist_getclass (tree arglist)
+{
+  return chainon (arglist, 
+                 tree_cons (NULL_TREE, build_class_ref (output_class), NULL_TREE));
+}
+
+static rewrite_rule rules[] =
+  {{"java.lang.Class", "getClassLoader", "()Ljava/lang/ClassLoader;", 
+    "(Ljava/lang/Class;)Ljava/lang/ClassLoader;", 
+    ACC_FINAL|ACC_PRIVATE, rewrite_arglist_getclass},
+   {"java.lang.Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
+    "(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class;",
+    ACC_FINAL|ACC_PRIVATE|ACC_STATIC, rewrite_arglist_getclass},
+   {NULL, NULL, NULL, NULL, 0, NULL}};
+
+/* Scan the rules list for replacements for *METHOD_P and replace the
+   args accordingly.  */
+
+void
+maybe_rewrite_invocation (tree *method_p, tree *arg_list_p, 
+                         tree *method_signature_p)
+{
+  tree context = DECL_NAME (TYPE_NAME (DECL_CONTEXT (*method_p)));
+  rewrite_rule *p;
+  for (p = rules; p->classname; p++)
+    {
+      if (get_identifier (p->classname) == context)
+       {
+         tree method = DECL_NAME (*method_p);
+         if (get_identifier (p->method) == method
+             && get_identifier (p->signature) == *method_signature_p)
+           {
+             tree maybe_method
+               = lookup_java_method (DECL_CONTEXT (*method_p),
+                                     method,
+                                     get_identifier (p->new_signature));
+             if (! maybe_method && ! flag_verify_invocations)
+               {
+                 maybe_method
+                   = add_method (DECL_CONTEXT (*method_p), p->flags, 
+                                 method, get_identifier (p->new_signature));
+                 DECL_EXTERNAL (maybe_method) = 1;
+               }
+             *method_p = maybe_method;
+             gcc_assert (*method_p);
+             *arg_list_p = p->rewrite_arglist (*arg_list_p);
+             *method_signature_p = get_identifier (p->new_signature);
+
+             break;
+           }
+       }
+    }
+}
+
+\f
+
 tree
 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
                        tree self_type, tree method_signature ATTRIBUTE_UNUSED,
@@ -2064,7 +2115,6 @@ build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
       if (! flag_indirect_dispatch 
          || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
        {
-         make_decl_rtl (method);
          func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
                         method);
        }
@@ -2117,8 +2167,8 @@ build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
          method_index++;
        }
       method_index *= int_size_in_bytes (method_type_node);
-      ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
-                         ref, build_int_cst (NULL_TREE, method_index)));
+      ref = fold_build2 (PLUS_EXPR, method_ptr_type_node,
+                        ref, build_int_cst (NULL_TREE, method_index));
       ref = build1 (INDIRECT_REF, method_type_node, ref);
       func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
                     ref, lookup_field (&method_type_node, ncode_ident),
@@ -2197,8 +2247,7 @@ build_invokevirtual (tree dtable, tree method)
 
   if (flag_indirect_dispatch)
     {
-      if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
-       abort ();
+      gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))));
 
       otable_index 
        = build_int_cst (NULL_TREE, get_symbol_table_index 
@@ -2221,8 +2270,8 @@ build_invokevirtual (tree dtable, tree method)
                                   size_int (TARGET_VTABLE_USES_DESCRIPTORS));
     }
 
-  func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
-                      convert (nativecode_ptr_ptr_type_node, method_index)));
+  func = fold_build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
+                     convert (nativecode_ptr_ptr_type_node, method_index));
 
   if (TARGET_VTABLE_USES_DESCRIPTORS)
     func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
@@ -2251,8 +2300,7 @@ build_invokeinterface (tree dtable, tree method)
                   lookup_field (&dtable_type, class_ident), NULL_TREE);
 
   interface = DECL_CONTEXT (method);
-  if (! CLASS_INTERFACE (TYPE_NAME (interface)))
-    abort ();
+  gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface)));
   layout_class_methods (interface);
   
   if (flag_indirect_dispatch)
@@ -2326,6 +2374,18 @@ expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
   else
     method = lookup_java_method (self_type, method_name, method_signature);
 
+  /* We've found a method in a class other than the one in which it
+     was wanted.  This can happen if, for instance, we're trying to
+     compile invokespecial super.equals().  
+     FIXME: This is a kludge.  Rather than nullifying the result, we
+     should change lookup_java_method() so that it doesn't search the
+     superclass chain when we're BC-compiling.  */
+  if (! flag_verify_invocations
+      && method
+      && ! TYPE_ARRAY_P (self_type)
+      && self_type != DECL_CONTEXT (method))
+    method = NULL_TREE;
+
   /* We've found a method in an interface, but this isn't an interface
      call.  */
   if (opcode != OPCODE_invokeinterface
@@ -2414,6 +2474,8 @@ expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
   arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
   flush_quick_stack ();
 
+  maybe_rewrite_invocation (&method, &arg_list, &method_signature);
+
   func = NULL_TREE;
   if (opcode == OPCODE_invokestatic)
     func = build_known_method_ref (method, method_type, self_type,
@@ -2493,8 +2555,7 @@ build_jni_stub (tree method)
   int from_class = ! CLASS_FROM_SOURCE_P (klass);
   klass = build_class_ref (klass);
 
-  if (! METHOD_NATIVE (method) || ! flag_jni)
-    abort ();
+  gcc_assert (METHOD_NATIVE (method) && flag_jni);
 
   DECL_ARTIFICIAL (method) = 1;
   DECL_EXTERNAL (method) = 0;
@@ -2527,8 +2588,7 @@ build_jni_stub (tree method)
     method_args = DECL_ARGUMENTS (method);
   else
     method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
-  block = build_block (env_var, NULL_TREE, NULL_TREE,
-                      method_args, NULL_TREE);
+  block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
   TREE_SIDE_EFFECTS (block) = 1;
   /* When compiling from source we don't set the type of the block,
      because that will prevent patch_return from ever being run.  */
@@ -2616,8 +2676,17 @@ build_jni_stub (tree method)
   /* If the JNI call returned a result, capture it here.  If we had to
      unwrap JNI object results, we would do that here.  */
   if (res_var != NULL_TREE)
-    call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
-                  res_var, call);
+    {
+      /* If the call returns an object, it may return a JNI weak
+        reference, in which case we must unwrap it.  */
+      if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
+       call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
+                      build_address_of (soft_unwrapjni_node),
+                      build_tree_list (NULL_TREE, call),
+                      NULL_TREE);
+      call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
+                    res_var, call);
+    }
 
   TREE_SIDE_EFFECTS (call) = 1;
   CAN_COMPLETE_NORMALLY (call) = 1;
@@ -2640,8 +2709,7 @@ build_jni_stub (tree method)
   if (res_var != NULL_TREE)
     {
       tree drt;
-      if (! DECL_RESULT (method))
-       abort ();
+      gcc_assert (DECL_RESULT (method));
       /* Make sure we copy the result variable to the actual
         result.  We use the type of the DECL_RESULT because it
         might be different from the return type of the function:
@@ -2729,7 +2797,8 @@ expand_java_field_op (int is_static, int is_putting, int field_ref_index)
     }
 
   field_ref = build_field_ref (field_ref, self_type, field_name);
-  if (is_static)
+  if (is_static
+      && ! flag_indirect_dispatch)
     field_ref = build_class_init (self_type, field_ref);
   if (is_putting)
     {
@@ -2737,22 +2806,22 @@ expand_java_field_op (int is_static, int is_putting, int field_ref_index)
       if (FIELD_FINAL (field_decl))
        {
          if (DECL_CONTEXT (field_decl) != current_class)
-            error ("%Jassignment to final field '%D' not in field's class",
-                   field_decl, field_decl);
+            error ("assignment to final field %q+D not in field's class",
+                   field_decl);
          else if (FIELD_STATIC (field_decl))
            {
              if (!DECL_CLINIT_P (current_function_decl))
-               warning ("%Jassignment to final static field %qD not in "
+               warning (0, "assignment to final static field %q+D not in "
                          "class initializer",
-                         field_decl, field_decl);
+                         field_decl);
            }
          else
            {
              tree cfndecl_name = DECL_NAME (current_function_decl);
              if (! DECL_CONSTRUCTOR_P (current_function_decl)
                  && !ID_FINIT_P (cfndecl_name))
-                warning ("%Jassignment to final field '%D' not in constructor",
-                        field_decl, field_decl);
+                warning (0, "assignment to final field %q+D not in constructor",
+                        field_decl);
            }
        }
       java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
@@ -2941,7 +3010,7 @@ expand_byte_code (JCF *jcf, tree method)
       int pc = GET_u2 (linenumber_pointer);
       linenumber_pointer += 4;
       if (pc >= length)
-       warning ("invalid PC in line number table");
+       warning (0, "invalid PC in line number table");
       else
        {
          if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
@@ -2950,16 +3019,10 @@ expand_byte_code (JCF *jcf, tree method)
        }
     }  
 
-  if (flag_new_verifier)
-    {
-      if (! verify_jvm_instructions_new (jcf, byte_ops, length))
-        return;
-    }
-  else
-    {
-      if (! verify_jvm_instructions (jcf, byte_ops, length))
-       return;
-    }
+  if (! verify_jvm_instructions_new (jcf, byte_ops, length))
+    return;
+
+  promote_arguments ();
 
   /* Translate bytecodes.  */
   linenumber_pointer = linenumber_table;
@@ -2995,7 +3058,7 @@ expand_byte_code (JCF *jcf, tree method)
            {
               /* We've just reached the end of a region of dead code.  */
              if (extra_warnings)
-               warning ("unreachable bytecode from %d to before %d",
+               warning (0, "unreachable bytecode from %d to before %d",
                         dead_code_index, PC);
               dead_code_index = -1;
             }
@@ -3037,7 +3100,7 @@ expand_byte_code (JCF *jcf, tree method)
     {
       /* We've just reached the end of a region of dead code.  */
       if (extra_warnings)
-       warning ("unreachable bytecode from %d to the end of the method", 
+       warning (0, "unreachable bytecode from %d to the end of the method", 
                 dead_code_index);
     }
 }
@@ -3066,14 +3129,14 @@ process_jvm_instruction (int PC, const unsigned char* byte_ops,
   const char *opname; /* Temporary ??? */
   int oldpc = PC; /* PC at instruction start. */
 
-  /* If the instruction is at the beginning of a exception handler,
-     replace the top of the stack with the thrown object reference */
+  /* If the instruction is at the beginning of an exception handler,
+     replace the top of the stack with the thrown object reference */
   if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
     {
-      /* Note that the new verifier will not emit a type map at all
-        for dead exception handlers.  In this case we just ignore
-        the situation.  */
-      if (! flag_new_verifier || (instruction_bits[PC] & BCODE_VERIFIED) != 0)
+      /* Note that the verifier will not emit a type map at all for
+        dead exception handlers.  In this case we just ignore the
+        situation.  */
+      if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
        {
          tree type = pop_type (promote_type (throwable_type_node));
          push_value (build_exception_object_ref (type));
@@ -3244,8 +3307,8 @@ process_jvm_instruction (int PC, const unsigned char* byte_ops,
   }
 
 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
-  push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
-                           pop_value (OPERAND_TYPE##_type_node))));
+  push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
+                          pop_value (OPERAND_TYPE##_type_node)));
 
 #define CONVERT2(FROM_TYPE, TO_TYPE)                                    \
   {                                                                     \
@@ -3388,7 +3451,7 @@ peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
 
    This function is used by `give_name_to_locals' so that a local's
    DECL features a DECL_LOCAL_START_PC such that the first related
-   store operation will use DECL as a destination, not a unrelated
+   store operation will use DECL as a destination, not an unrelated
    temporary created for the occasion.
 
    This function uses a global (instruction_bits) `note_instructions' should
@@ -3496,7 +3559,8 @@ maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
    For method invocation, we modify the arguments so that a
    left-to-right order evaluation is performed. Saved expressions
    will, in CALL_EXPR order, be reused when the call will be expanded.
-*/
+
+   We also promote outgoing args if needed.  */
 
 tree
 force_evaluation_order (tree node)
@@ -3524,13 +3588,22 @@ force_evaluation_order (tree node)
        return node;
 
       /* Not having a list of arguments here is an error. */ 
-      if (TREE_CODE (arg) != TREE_LIST)
-        abort ();
+      gcc_assert (TREE_CODE (arg) == TREE_LIST);
 
       /* This reverses the evaluation order. This is a desired effect. */
       for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
        {
-         tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
+         /* Promote types smaller than integer.  This is required by
+            some ABIs.  */
+         tree type = TREE_TYPE (TREE_VALUE (arg));
+         tree saved;
+         if (targetm.calls.promote_prototypes (type)
+             && INTEGRAL_TYPE_P (type)
+             && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
+                                     TYPE_SIZE (integer_type_node)))
+           TREE_VALUE (arg) = fold_convert (integer_type_node, TREE_VALUE (arg));
+
+         saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
          cmp = (cmp == NULL_TREE ? saved :
                 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
          TREE_VALUE (arg) = saved;
@@ -3588,7 +3661,7 @@ build_expr_wfl (tree node,
   EXPR_WFL_NODE (wfl) = node;
   if (node)
     {
-      if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
+      if (!TYPE_P (node))
        TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
       TREE_TYPE (wfl) = TREE_TYPE (node);
     }
@@ -3624,7 +3697,7 @@ expr_add_location (tree node, source_location location, bool statement)
     EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
   if (node)
     {
-      if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
+      if (!TYPE_P (node))
        TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
       TREE_TYPE (wfl) = TREE_TYPE (node);
     }
@@ -3643,4 +3716,28 @@ build_java_empty_stmt (void)
   return t;
 }
 
+/* Promote all args of integral type before generating any code.  */
+
+static void
+promote_arguments (void)
+{
+  int i;
+  tree arg;
+  for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
+       arg != NULL_TREE;  arg = TREE_CHAIN (arg), i++)
+    {
+      tree arg_type = TREE_TYPE (arg);
+      if (INTEGRAL_TYPE_P (arg_type)
+         && TYPE_PRECISION (arg_type) < 32)
+       {
+         tree copy = find_local_variable (i, integer_type_node, -1);
+         java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
+                                copy,
+                                fold_convert (integer_type_node, arg)));
+       }
+      if (TYPE_IS_WIDE (arg_type))
+       i++;
+    }
+}
+
 #include "gt-java-expr.h"