1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
34 #include "java-tree.h"
36 #include "java-opcodes.h"
38 #include "java-except.h"
44 static void flush_quick_stack PARAMS ((void));
45 static void push_value PARAMS ((tree));
46 static tree pop_value PARAMS ((tree));
47 static void java_stack_swap PARAMS ((void));
48 static void java_stack_dup PARAMS ((int, int));
49 static void build_java_athrow PARAMS ((tree));
50 static void build_java_jsr PARAMS ((tree, tree));
51 static void build_java_ret PARAMS ((tree));
52 static void expand_java_multianewarray PARAMS ((tree, int));
53 static void expand_java_arraystore PARAMS ((tree));
54 static void expand_java_arrayload PARAMS ((tree));
55 static void expand_java_array_length PARAMS ((void));
56 static tree build_java_monitor PARAMS ((tree, tree));
57 static void expand_java_pushc PARAMS ((int, tree));
58 static void expand_java_return PARAMS ((tree));
59 static void expand_java_NEW PARAMS ((tree));
60 static void expand_java_INSTANCEOF PARAMS ((tree));
61 static void expand_java_CHECKCAST PARAMS ((tree));
62 static void expand_iinc PARAMS ((unsigned int, int, int));
63 static void expand_java_binop PARAMS ((tree, enum tree_code));
64 static void note_label PARAMS ((int, int));
65 static void expand_compare PARAMS ((enum tree_code, tree, tree, int));
66 static void expand_test PARAMS ((enum tree_code, tree, int));
67 static void expand_cond PARAMS ((enum tree_code, tree, int));
68 static void expand_java_goto PARAMS ((int));
70 static void expand_java_call PARAMS ((int, int));
71 static void expand_java_ret PARAMS ((tree));
73 static tree pop_arguments PARAMS ((tree));
74 static void expand_invoke PARAMS ((int, int, int));
75 static void expand_java_field_op PARAMS ((int, int, int));
76 static void java_push_constant_from_pool PARAMS ((struct JCF *, int));
77 static void java_stack_pop PARAMS ((int));
78 static tree build_java_throw_out_of_bounds_exception PARAMS ((tree));
79 static tree build_java_check_indexed_type PARAMS ((tree, tree));
80 static tree java_array_data_offset PARAMS ((tree));
81 static tree case_identity PARAMS ((tree, tree));
82 static unsigned char peek_opcode_at_pc PARAMS ((struct JCF *, int, int));
84 static tree operand_type[59];
85 extern struct obstack permanent_obstack;
87 static tree methods_ident = NULL_TREE;
88 static tree ncode_ident = NULL_TREE;
89 tree dtable_ident = NULL_TREE;
91 /* Set to non-zero value in order to emit class initilization code
92 before static field references. */
93 int always_initialize_class_p;
95 /* We store the stack state in two places:
96 Within a basic block, we use the quick_stack, which is a
97 pushdown list (TREE_LISTs) of expression nodes.
98 This is the top part of the stack; below that we use find_stack_slot.
99 At the end of a basic block, the quick_stack must be flushed
100 to the stack slot array (as handled by find_stack_slot).
101 Using quick_stack generates better code (especially when
102 compiled without optimization), because we do not have to
103 explicitly store and load trees to temporary variables.
105 If a variable is on the quick stack, it means the value of variable
106 when the quick stack was last flushed. Conceptually, flush_quick_stack
107 saves all the the quick_stack elements in parellel. However, that is
108 complicated, so it actually saves them (i.e. copies each stack value
109 to is home virtual register) from low indexes. This allows a quick_stack
110 element at index i (counting from the bottom of stack the) to references
111 slot virtuals for register that are >= i, but not those that are deeper.
112 This convention makes most operations easier. For example iadd works
113 even when the stack contains (reg[0], reg[1]): It results in the
114 stack containing (reg[0]+reg[1]), which is OK. However, some stack
115 operations are more complicated. For example dup given a stack
116 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
117 the convention, since stack value 1 would refer to a register with
118 lower index (reg[0]), which flush_quick_stack does not safely handle.
119 So dup cannot just add an extra element to the quick_stack, but iadd can.
122 static tree quick_stack = NULL_TREE;
124 /* A free-list of unused permamnet TREE_LIST nodes. */
125 static tree tree_list_free_list = NULL_TREE;
127 /* The stack pointer of the Java virtual machine.
128 This does include the size of the quick_stack. */
132 const unsigned char *linenumber_table;
133 int linenumber_count;
136 init_expr_processing()
138 operand_type[21] = operand_type[54] = int_type_node;
139 operand_type[22] = operand_type[55] = long_type_node;
140 operand_type[23] = operand_type[56] = float_type_node;
141 operand_type[24] = operand_type[57] = double_type_node;
142 operand_type[25] = operand_type[58] = ptr_type_node;
143 ggc_add_tree_root (operand_type, 59);
144 ggc_add_tree_root (&methods_ident, 1);
145 ggc_add_tree_root (&ncode_ident, 1);
146 ggc_add_tree_root (&quick_stack, 1);
147 ggc_add_tree_root (&tree_list_free_list, 1);
151 truthvalue_conversion (expr)
154 /* It is simpler and generates better code to have only TRUTH_*_EXPR
155 or comparison expressions as truth values at this level.
157 This function should normally be identity for Java. */
159 switch (TREE_CODE (expr))
162 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
163 case TRUTH_ANDIF_EXPR:
164 case TRUTH_ORIF_EXPR:
171 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
174 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
176 /* are these legal? XXX JH */
181 /* These don't change whether an object is non-zero or zero. */
182 return truthvalue_conversion (TREE_OPERAND (expr, 0));
185 /* Distribute the conversion into the arms of a COND_EXPR. */
186 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
187 truthvalue_conversion (TREE_OPERAND (expr, 1)),
188 truthvalue_conversion (TREE_OPERAND (expr, 2))));
191 /* If this is widening the argument, we can ignore it. */
192 if (TYPE_PRECISION (TREE_TYPE (expr))
193 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
194 return truthvalue_conversion (TREE_OPERAND (expr, 0));
195 /* fall through to default */
198 return fold (build (NE_EXPR, boolean_type_node, expr, boolean_false_node));
202 #ifdef JAVA_USE_HANDLES
203 /* Given a pointer to a handle, get a pointer to an object. */
209 tree field, handle_type;
210 expr = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
211 handle_type = TREE_TYPE (expr);
212 field = TYPE_FIELDS (handle_type);
213 expr = build (COMPONENT_REF, TREE_TYPE (field), expr, field);
218 /* Save any stack slots that happen to be in the quick_stack into their
219 home virtual register slots.
221 The copy order is from low stack index to high, to support the invariant
222 that the expression for a slot may contain decls for stack slots with
223 higher (or the same) index, but not lower. */
228 int stack_index = stack_pointer;
229 register tree prev, cur, next;
231 /* First reverse the quick_stack, and count the number of slots it has. */
232 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
234 next = TREE_CHAIN (cur);
235 TREE_CHAIN (cur) = prev;
237 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
241 while (quick_stack != NULL_TREE)
244 tree node = quick_stack, type;
245 quick_stack = TREE_CHAIN (node);
246 TREE_CHAIN (node) = tree_list_free_list;
247 tree_list_free_list = node;
248 node = TREE_VALUE (node);
249 type = TREE_TYPE (node);
251 decl = find_stack_slot (stack_index, type);
253 expand_assignment (decl, node, 0, 0);
254 stack_index += 1 + TYPE_IS_WIDE (type);
258 /* Push TYPE on the type stack.
259 Return true on success, 0 on overflow. */
266 type = promote_type (type);
267 n_words = 1 + TYPE_IS_WIDE (type);
268 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
270 stack_type_map[stack_pointer++] = type;
272 while (--n_words >= 0)
273 stack_type_map[stack_pointer++] = TYPE_SECOND;
281 if (! push_type_0 (type))
282 fatal ("stack overflow");
289 tree type = TREE_TYPE (value);
290 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
292 type = promote_type (type);
293 value = convert (type, value);
296 if (tree_list_free_list == NULL_TREE)
297 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
300 tree node = tree_list_free_list;
301 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
302 TREE_VALUE (node) = value;
303 TREE_CHAIN (node) = quick_stack;
308 /* Pop a type from the type stack.
309 TYPE is the expected type. Return the actual type, which must be
311 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
314 pop_type_0 (type, messagep)
321 if (TREE_CODE (type) == RECORD_TYPE)
322 type = promote_type (type);
323 n_words = 1 + TYPE_IS_WIDE (type);
324 if (stack_pointer < n_words)
326 *messagep = xstrdup ("stack underflow");
329 while (--n_words > 0)
331 if (stack_type_map[--stack_pointer] != void_type_node)
333 *messagep = xstrdup ("Invalid multi-word value on type stack");
337 t = stack_type_map[--stack_pointer];
338 if (type == NULL_TREE || t == type)
340 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
341 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
343 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
345 if (type == ptr_type_node || type == object_ptr_type_node)
347 else if (t == ptr_type_node) /* Special case for null reference. */
349 else if (can_widen_reference_to (t, type))
351 /* This is a kludge, but matches what Sun's verifier does.
352 It can be tricked, but is safe as long as type errors
353 (i.e. interface method calls) are caught at run-time. */
354 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
355 return object_ptr_type_node;
358 const char *str1 = "expected type '";
359 const char *str3 = "' but stack contains '";
360 const char *str5 = "'";
361 int len1 = strlen (str1);
362 int len2 = strlen (lang_printable_name (type, 0));
363 int len3 = strlen (str3);
364 int len4 = strlen (lang_printable_name (t, 0));
365 int len5 = strlen (str5);
366 char *msg = xmalloc (len1 + len2 + len3 + len4 + len5 + 1);
368 strcpy (msg, str1); msg += len1;
369 strcpy (msg, lang_printable_name (type, 0)); msg += len2;
370 strcpy (msg, str3); msg += len3;
371 strcpy (msg, lang_printable_name (t, 0)); msg += len4;
377 /* Pop a type from the type stack.
378 TYPE is the expected type. Return the actual type, which must be
379 convertible to TYPE, otherwise call error. */
385 char *message = NULL;
386 type = pop_type_0 (type, &message);
395 /* Return 1f if SOURCE_TYPE can be safely widened to TARGET_TYPE.
396 Handles array types and interfaces. */
399 can_widen_reference_to (source_type, target_type)
400 tree source_type, target_type;
402 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
405 /* Get rid of pointers */
406 if (TREE_CODE (source_type) == POINTER_TYPE)
407 source_type = TREE_TYPE (source_type);
408 if (TREE_CODE (target_type) == POINTER_TYPE)
409 target_type = TREE_TYPE (target_type);
411 if (source_type == target_type)
415 source_type = HANDLE_TO_CLASS_TYPE (source_type);
416 target_type = HANDLE_TO_CLASS_TYPE (target_type);
417 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
419 HOST_WIDE_INT source_length, target_length;
420 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
422 target_length = java_array_type_length (target_type);
423 if (target_length >= 0)
425 source_length = java_array_type_length (source_type);
426 if (source_length != target_length)
429 source_type = TYPE_ARRAY_ELEMENT (source_type);
430 target_type = TYPE_ARRAY_ELEMENT (target_type);
431 if (source_type == target_type)
433 if (TREE_CODE (source_type) != POINTER_TYPE
434 || TREE_CODE (target_type) != POINTER_TYPE)
436 return can_widen_reference_to (source_type, target_type);
440 int source_depth = class_depth (source_type);
441 int target_depth = class_depth (target_type);
443 /* class_depth can return a negative depth if an error occurred */
444 if (source_depth < 0 || target_depth < 0)
447 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
449 /* target_type is OK if source_type or source_type ancestors
450 implement target_type. We handle multiple sub-interfaces */
452 tree basetype_vec = TYPE_BINFO_BASETYPES (source_type);
453 int n = TREE_VEC_LENGTH (basetype_vec), i;
454 for (i=0 ; i < n; i++)
455 if (can_widen_reference_to
456 (TREE_TYPE (TREE_VEC_ELT (basetype_vec, i)),
463 for ( ; source_depth > target_depth; source_depth--)
465 source_type = TYPE_BINFO_BASETYPE (source_type, 0);
467 return source_type == target_type;
476 type = pop_type (type);
479 tree node = quick_stack;
480 quick_stack = TREE_CHAIN (quick_stack);
481 TREE_CHAIN (node) = tree_list_free_list;
482 tree_list_free_list = node;
483 node = TREE_VALUE (node);
487 return find_stack_slot (stack_pointer, promote_type (type));
491 /* Pop and discrad the top COUNT stack slots. */
494 java_stack_pop (count)
500 if (stack_pointer == 0)
501 fatal ("stack underflow");
502 type = stack_type_map[stack_pointer - 1];
503 if (type == TYPE_SECOND)
506 if (stack_pointer == 1 || count <= 0)
507 fatal ("stack underflow");
508 type = stack_type_map[stack_pointer - 2];
510 val = pop_value (type);
515 /* Implement the 'swap' operator (to swap two top stack slots). */
524 if (stack_pointer < 2
525 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
526 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
527 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
528 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
529 fatal ("bad stack swap");
531 flush_quick_stack ();
532 decl1 = find_stack_slot (stack_pointer - 1, type1);
533 decl2 = find_stack_slot (stack_pointer - 2, type2);
534 temp = copy_to_reg (DECL_RTL (decl1));
535 emit_move_insn (DECL_RTL (decl1), DECL_RTL (decl2));
536 emit_move_insn (DECL_RTL (decl2), temp);
537 stack_type_map[stack_pointer - 1] = type2;
538 stack_type_map[stack_pointer - 2] = type1;
542 java_stack_dup (size, offset)
545 int low_index = stack_pointer - size - offset;
548 error ("stack underflow - dup* operation");
550 flush_quick_stack ();
552 stack_pointer += size;
553 dst_index = stack_pointer;
555 for (dst_index = stack_pointer; --dst_index >= low_index; )
558 int src_index = dst_index - size;
559 if (src_index < low_index)
560 src_index = dst_index + size + offset;
561 type = stack_type_map [src_index];
562 if (type == TYPE_SECOND)
564 if (src_index <= low_index)
565 fatal ("dup operation splits 64-bit number");
566 stack_type_map[dst_index] = type;
567 src_index--; dst_index--;
568 type = stack_type_map[src_index];
569 if (! TYPE_IS_WIDE (type))
570 fatal ("internal error - dup operation");
572 else if (TYPE_IS_WIDE (type))
573 fatal ("internal error - dup operation");
574 if (src_index != dst_index)
576 tree src_decl = find_stack_slot (src_index, type);
577 tree dst_decl = find_stack_slot (dst_index, type);
578 emit_move_insn (DECL_RTL (dst_decl), DECL_RTL (src_decl));
579 stack_type_map[dst_index] = type;
584 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
588 build_java_athrow (node)
593 call = build (CALL_EXPR,
595 build_address_of (throw_node[exceptions_via_longjmp ? 1 : 0]),
596 build_tree_list (NULL_TREE, node),
598 TREE_SIDE_EFFECTS (call) = 1;
599 expand_expr_stmt (call);
600 java_stack_pop (stack_pointer);
603 /* Implementation for jsr/ret */
606 build_java_jsr (where, ret)
610 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
611 push_value (ret_label);
612 flush_quick_stack ();
613 emit_jump (label_rtx (where));
618 build_java_ret (location)
621 expand_computed_goto (location);
624 /* Implementation of operations on array: new, load, store, length */
626 /* Array core info access macros */
628 #define JAVA_ARRAY_LENGTH_OFFSET(A) \
629 byte_position (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (TREE_TYPE (A)))))
632 decode_newarray_type (atype)
637 case 4: return boolean_type_node;
638 case 5: return char_type_node;
639 case 6: return float_type_node;
640 case 7: return double_type_node;
641 case 8: return byte_type_node;
642 case 9: return short_type_node;
643 case 10: return int_type_node;
644 case 11: return long_type_node;
645 default: return NULL_TREE;
649 /* Map primitive type to the code used by OPCODE_newarray. */
652 encode_newarray_type (type)
655 if (type == boolean_type_node)
657 else if (type == char_type_node)
659 else if (type == float_type_node)
661 else if (type == double_type_node)
663 else if (type == byte_type_node)
665 else if (type == short_type_node)
667 else if (type == int_type_node)
669 else if (type == long_type_node)
672 fatal ("Can't compute type code - patch_newarray");
675 /* Build a call to _Jv_ThrowBadArrayIndex(), the
676 ArrayIndexOfBoundsException exception handler. */
679 build_java_throw_out_of_bounds_exception (index)
682 tree node = build (CALL_EXPR, int_type_node,
683 build_address_of (soft_badarrayindex_node),
684 build_tree_list (NULL_TREE, index), NULL_TREE);
685 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
689 /* Return the length of an array. Doesn't perform any checking on the nature
690 or value of the array NODE. May be used to implement some bytecodes. */
693 build_java_array_length_access (node)
696 tree type = TREE_TYPE (node);
697 HOST_WIDE_INT length;
698 if (!is_array_type_p (type))
699 fatal ("array length on a non-array reference");
700 length = java_array_type_length (type);
702 return build_int_2 (length, 0);
703 return fold (build1 (INDIRECT_REF,
705 fold (build (PLUS_EXPR, ptr_type_node,
707 JAVA_ARRAY_LENGTH_OFFSET(node)))));
710 /* Optionally checks an array against the NULL pointer, eventually throwing a
711 NullPointerException. It could replace signal handling, but tied to NULL.
712 ARG1: the pointer to check, ARG2: the expression to use if
713 the pointer is non-null and ARG3 the type that should be returned. */
716 build_java_arraynull_check (node, expr, type)
717 tree node ATTRIBUTE_UNUSED;
719 tree type ATTRIBUTE_UNUSED;
722 static int java_array_access_throws_null_exception = 0;
724 if (java_array_access_throws_null_exception)
725 return (build (COND_EXPR,
727 build (EQ_EXPR, int_type_node, node, null_pointer_node),
728 build_java_athrow (node), expr ));
735 java_array_data_offset (array)
738 tree array_type = TREE_TYPE (TREE_TYPE (array));
739 tree data_fld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
741 if (data_fld == NULL_TREE)
742 return size_in_bytes (array_type);
744 return byte_position (data_fld);
747 /* Implement array indexing (either as l-value or r-value).
748 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
749 Optionally performs bounds checking and/or test to NULL.
750 At this point, ARRAY should have been verified as an array. */
753 build_java_arrayaccess (array, type, index)
754 tree array, type, index;
756 tree arith, node, throw = NULL_TREE;
758 arith = fold (build (PLUS_EXPR, int_type_node,
759 java_array_data_offset (array),
760 fold (build (MULT_EXPR, int_type_node,
761 index, size_in_bytes(type)))));
763 if (flag_bounds_check)
766 * (unsigned jint) INDEX >= (unsigned jint) LEN
767 * && throw ArrayIndexOutOfBoundsException.
768 * Note this is equivalent to and more efficient than:
769 * INDEX < 0 || INDEX >= LEN && throw ... */
771 tree len = build_java_array_length_access (array);
772 TREE_TYPE (len) = unsigned_int_type_node;
773 test = fold (build (GE_EXPR, boolean_type_node,
774 convert (unsigned_int_type_node, index),
776 if (! integer_zerop (test))
778 throw = build (TRUTH_ANDIF_EXPR, int_type_node, test,
779 build_java_throw_out_of_bounds_exception (index));
780 /* allows expansion within COMPOUND */
781 TREE_SIDE_EFFECTS( throw ) = 1;
785 node = build1 (INDIRECT_REF, type,
786 fold (build (PLUS_EXPR, ptr_type_node,
788 (throw ? build (COMPOUND_EXPR, int_type_node,
792 return (fold (build_java_arraynull_check (array, node, type)));
795 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
796 ARRAY_NODE. This function is used to retrieve something less vague than
797 a pointer type when indexing the first dimension of something like [[<t>.
798 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
800 As a side effect, it also makes sure that ARRAY_NODE is an array. */
803 build_java_check_indexed_type (array_node, indexed_type)
809 if (!is_array_type_p (TREE_TYPE (array_node)))
810 fatal ("array indexing on a non-array reference");
812 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
814 if (indexed_type == ptr_type_node )
815 return promote_type (elt_type);
817 /* BYTE/BOOLEAN store and load are used for both type */
818 if (indexed_type == byte_type_node && elt_type == boolean_type_node )
819 return boolean_type_node;
821 if (indexed_type != elt_type )
822 fatal ("type array element mismatch");
827 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
828 called with an integer code (the type of array to create), and the length
829 of the array to create. */
832 build_newarray (atype_value, length)
838 tree prim_type = decode_newarray_type (atype_value);
840 = build_java_array_type (prim_type,
841 host_integerp (length, 0) == INTEGER_CST
842 ? tree_low_cst (length, 0) : -1);
844 /* If compiling to native, pass a reference to the primitive type class
845 and save the runtime some work. However, the bytecode generator
846 expects to find the type_code int here. */
847 if (flag_emit_class_files)
848 type_arg = build_int_2 (atype_value, 0);
850 type_arg = build_class_ref (prim_type);
852 return build (CALL_EXPR, promote_type (type),
853 build_address_of (soft_newarray_node),
854 tree_cons (NULL_TREE,
856 build_tree_list (NULL_TREE, length)),
860 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
864 build_anewarray (class_type, length)
869 = build_java_array_type (class_type,
870 host_integerp (length, 0)
871 ? tree_low_cst (length, 0) : -1);
873 return build (CALL_EXPR, promote_type (type),
874 build_address_of (soft_anewarray_node),
875 tree_cons (NULL_TREE, length,
876 tree_cons (NULL_TREE, build_class_ref (class_type),
877 build_tree_list (NULL_TREE,
878 null_pointer_node))),
882 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
885 build_new_array (type, length)
889 if (JPRIMITIVE_TYPE_P (type))
890 return build_newarray (encode_newarray_type (type), length);
892 return build_anewarray (TREE_TYPE (type), length);
895 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
896 class pointer, a number of dimensions and the matching number of
897 dimensions. The argument list is NULL terminated. */
900 expand_java_multianewarray (class_type, ndim)
905 tree args = build_tree_list( NULL_TREE, null_pointer_node );
907 for( i = 0; i < ndim; i++ )
908 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
910 push_value (build (CALL_EXPR,
911 promote_type (class_type),
912 build_address_of (soft_multianewarray_node),
913 tree_cons (NULL_TREE, build_class_ref (class_type),
914 tree_cons (NULL_TREE,
915 build_int_2 (ndim, 0), args )),
919 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
920 ARRAY is an array type. May expand some bound checking and NULL
921 pointer checking. RHS_TYPE_NODE we are going to store. In the case
922 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
923 INT. In those cases, we make the convertion.
925 if ARRAy is a reference type, the assignment is checked at run-time
926 to make sure that the RHS can be assigned to the array element
927 type. It is not necessary to generate this code if ARRAY is final. */
930 expand_java_arraystore (rhs_type_node)
933 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
934 && TYPE_PRECISION (rhs_type_node) <= 32) ?
935 int_type_node : rhs_type_node);
936 tree index = pop_value (int_type_node);
937 tree array = pop_value (ptr_type_node);
939 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
941 flush_quick_stack ();
943 index = save_expr (index);
944 array = save_expr (array);
946 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
948 tree check = build (CALL_EXPR, void_type_node,
949 build_address_of (soft_checkarraystore_node),
950 tree_cons (NULL_TREE, array,
951 build_tree_list (NULL_TREE, rhs_node)),
953 TREE_SIDE_EFFECTS (check) = 1;
954 expand_expr_stmt (check);
957 expand_assignment (build_java_arrayaccess (array,
963 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
964 sure that LHS is an array type. May expand some bound checking and NULL
966 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
967 BOOLEAN/SHORT, we push a promoted type back to the stack.
971 expand_java_arrayload (lhs_type_node )
975 tree index_node = pop_value (int_type_node);
976 tree array_node = pop_value (ptr_type_node);
978 index_node = save_expr (index_node);
979 array_node = save_expr (array_node);
980 lhs_type_node = build_java_check_indexed_type (array_node, lhs_type_node);
982 load_node = build_java_arrayaccess (array_node,
986 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
987 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
988 push_value (load_node);
991 /* Expands .length. Makes sure that we deal with and array and may expand
992 a NULL check on the array object. */
995 expand_java_array_length ()
997 tree array = pop_value (ptr_type_node);
998 tree length = build_java_array_length_access (array);
1000 push_value (build_java_arraynull_check (array, length, int_type_node));
1003 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1004 either soft_monitorenter_node or soft_monitorexit_node. */
1007 build_java_monitor (call, object)
1011 return (build (CALL_EXPR,
1013 build_address_of (call),
1014 build_tree_list (NULL_TREE, object),
1018 /* Emit code for one of the PUSHC instructions. */
1021 expand_java_pushc (ival, type)
1026 if (type == ptr_type_node && ival == 0)
1027 value = null_pointer_node;
1028 else if (type == int_type_node || type == long_type_node)
1030 value = build_int_2 (ival, ival < 0 ? -1 : 0);
1031 TREE_TYPE (value) = type;
1033 else if (type == float_type_node || type == double_type_node)
1036 #ifdef REAL_ARITHMETIC
1037 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1041 value = build_real (type, x);
1044 fatal ("internal error in expand_java_pushc");
1049 expand_java_return (type)
1052 if (type == void_type_node)
1053 expand_null_return ();
1056 tree retval = pop_value (type);
1057 tree res = DECL_RESULT (current_function_decl);
1058 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1060 /* Handle the situation where the native integer type is smaller
1061 than the JVM integer. It can happen for many cross compilers.
1062 The whole if expression just goes away if INT_TYPE_SIZE < 32
1064 if (INT_TYPE_SIZE < 32
1065 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1066 < GET_MODE_SIZE (TYPE_MODE (type))))
1067 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1069 TREE_SIDE_EFFECTS (retval) = 1;
1070 expand_return (retval);
1075 build_address_of (value)
1078 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1082 expand_java_NEW (type)
1085 if (! CLASS_LOADED_P (type))
1086 load_class (type, 1);
1087 safe_layout_class (type);
1088 push_value (build (CALL_EXPR, promote_type (type),
1089 build_address_of (alloc_object_node),
1090 tree_cons (NULL_TREE, build_class_ref (type),
1091 build_tree_list (NULL_TREE,
1092 size_in_bytes (type))),
1096 /* This returns an expression which will extract the class of an
1100 build_get_class (value)
1103 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1104 tree vtable_field = lookup_field (&object_type_node,
1105 get_identifier ("vtable"));
1106 return build (COMPONENT_REF, class_ptr_type,
1107 build1 (INDIRECT_REF, dtable_type,
1108 build (COMPONENT_REF, dtable_ptr_type,
1109 build1 (INDIRECT_REF, object_type_node, value),
1114 /* This builds the tree representation of the `instanceof' operator.
1115 It tries various tricks to optimize this in cases where types are
1119 build_instanceof (value, type)
1123 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1124 tree valtype = TREE_TYPE (TREE_TYPE (value));
1125 tree valclass = TYPE_NAME (valtype);
1128 /* When compiling from bytecode, we need to ensure that TYPE has
1130 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1132 load_class (type, 1);
1133 safe_layout_class (type);
1134 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1135 return error_mark_node;
1137 klass = TYPE_NAME (type);
1139 if (type == object_type_node || inherits_from_p (valtype, type))
1141 /* Anything except `null' is an instance of Object. Likewise,
1142 if the object is known to be an instance of the class, then
1143 we only need to check for `null'. */
1144 expr = build (COND_EXPR, itype,
1146 boolean_true_node, boolean_false_node);
1148 else if (DECL_P (klass) && DECL_P (valclass)
1149 && ! CLASS_INTERFACE (valclass)
1150 && ! CLASS_INTERFACE (klass)
1151 && ! inherits_from_p (type, valtype)
1152 && (CLASS_FINAL (klass)
1153 || ! inherits_from_p (valtype, type)))
1155 /* The classes are from different branches of the derivation
1156 tree, so we immediately know the answer. */
1157 expr = boolean_false_node;
1159 else if (DECL_P (klass) && CLASS_FINAL (klass))
1161 tree save = save_expr (value);
1162 expr = build (COND_EXPR, itype,
1164 build (EQ_EXPR, itype,
1165 build_get_class (save),
1166 build_class_ref (type)),
1167 boolean_false_node);
1171 expr = build (CALL_EXPR, itype,
1172 build_address_of (soft_instanceof_node),
1173 tree_cons (NULL_TREE, value,
1174 build_tree_list (NULL_TREE,
1175 build_class_ref (type))),
1178 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1183 expand_java_INSTANCEOF (type)
1186 tree value = pop_value (object_ptr_type_node);
1187 value = build_instanceof (value, type);
1192 expand_java_CHECKCAST (type)
1195 tree value = pop_value (ptr_type_node);
1196 value = build (CALL_EXPR, promote_type (type),
1197 build_address_of (soft_checkcast_node),
1198 tree_cons (NULL_TREE, build_class_ref (type),
1199 build_tree_list (NULL_TREE, value)),
1205 expand_iinc (local_var_index, ival, pc)
1206 unsigned int local_var_index;
1210 tree local_var, res;
1211 tree constant_value;
1213 flush_quick_stack ();
1214 local_var = find_local_variable (local_var_index, int_type_node, pc);
1215 constant_value = build_int_2 (ival, ival < 0 ? -1 : 0);
1216 res = fold (build (PLUS_EXPR, int_type_node, local_var, constant_value));
1217 expand_assignment (local_var, res, 0, 0);
1222 build_java_soft_divmod (op, type, op1, op2)
1224 tree type, op1, op2;
1227 tree arg1 = convert (type, op1);
1228 tree arg2 = convert (type, op2);
1230 if (type == int_type_node)
1234 case TRUNC_DIV_EXPR:
1235 call = soft_idiv_node;
1237 case TRUNC_MOD_EXPR:
1238 call = soft_irem_node;
1244 else if (type == long_type_node)
1248 case TRUNC_DIV_EXPR:
1249 call = soft_ldiv_node;
1251 case TRUNC_MOD_EXPR:
1252 call = soft_lrem_node;
1260 fatal ("Internal compiler error in build_java_soft_divmod");
1262 call = build (CALL_EXPR, type,
1263 build_address_of (call),
1264 tree_cons (NULL_TREE, arg1,
1265 build_tree_list (NULL_TREE, arg2)),
1272 build_java_binop (op, type, arg1, arg2)
1274 tree type, arg1, arg2;
1281 tree u_type = unsigned_type (type);
1282 arg1 = convert (u_type, arg1);
1283 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1284 return convert (type, arg1);
1288 mask = build_int_2 (TYPE_PRECISION (TREE_TYPE (arg1)) - 1, 0);
1289 arg2 = fold (build (BIT_AND_EXPR, int_type_node, arg2, mask));
1292 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1293 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1294 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1296 tree ifexp1 = fold ( build (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1297 boolean_type_node, arg1, arg2));
1298 tree ifexp2 = fold ( build (EQ_EXPR, boolean_type_node, arg1, arg2));
1299 tree second_compare = fold (build (COND_EXPR, int_type_node,
1300 ifexp2, integer_zero_node,
1301 op == COMPARE_L_EXPR
1302 ? integer_minus_one_node
1303 : integer_one_node));
1304 return fold (build (COND_EXPR, int_type_node, ifexp1,
1305 op == COMPARE_L_EXPR ? integer_one_node
1306 : integer_minus_one_node,
1310 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1312 tree ifexp1 = fold ( build (LT_EXPR, boolean_type_node, arg1, arg2));
1313 tree ifexp2 = fold ( build (GT_EXPR, boolean_type_node, arg1, arg2));
1314 tree second_compare = fold ( build (COND_EXPR, int_type_node,
1315 ifexp2, integer_one_node,
1316 integer_zero_node));
1317 return fold (build (COND_EXPR, int_type_node,
1318 ifexp1, integer_minus_one_node, second_compare));
1320 case TRUNC_DIV_EXPR:
1321 case TRUNC_MOD_EXPR:
1322 if (TREE_CODE (type) == REAL_TYPE
1323 && op == TRUNC_MOD_EXPR)
1326 if (type != double_type_node)
1328 arg1 = convert (double_type_node, arg1);
1329 arg2 = convert (double_type_node, arg2);
1331 call = build (CALL_EXPR, double_type_node,
1332 build_address_of (soft_fmod_node),
1333 tree_cons (NULL_TREE, arg1,
1334 build_tree_list (NULL_TREE, arg2)),
1336 if (type != double_type_node)
1337 call = convert (type, call);
1341 if (TREE_CODE (type) == INTEGER_TYPE
1342 && flag_use_divide_subroutine
1343 && ! flag_syntax_only)
1344 return build_java_soft_divmod (op, type, arg1, arg2);
1349 return fold (build (op, type, arg1, arg2));
1353 expand_java_binop (type, op)
1354 tree type; enum tree_code op;
1364 rtype = int_type_node;
1365 rarg = pop_value (rtype);
1368 rarg = pop_value (rtype);
1370 larg = pop_value (ltype);
1371 push_value (build_java_binop (op, type, larg, rarg));
1374 /* Lookup the field named NAME in *TYPEP or its super classes.
1375 If not found, return NULL_TREE.
1376 (If the *TYPEP is not found, or if the field reference is
1377 ambiguous, return error_mark_node.)
1378 If found, return the FIELD_DECL, and set *TYPEP to the
1379 class containing the field. */
1382 lookup_field (typep, name)
1386 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1388 load_class (*typep, 1);
1389 safe_layout_class (*typep);
1390 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1391 return error_mark_node;
1395 tree field, basetype_vec;
1399 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1400 if (DECL_NAME (field) == name)
1403 /* If *typep is an innerclass, lookup the field in its enclosing
1405 if (INNER_CLASS_TYPE_P (*typep))
1407 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (*typep)));
1409 if ((field = lookup_field (&outer_type, name)))
1413 /* Process implemented interfaces. */
1414 basetype_vec = TYPE_BINFO_BASETYPES (*typep);
1415 n = TREE_VEC_LENGTH (basetype_vec);
1416 save_field = NULL_TREE;
1417 for (i = 0; i < n; i++)
1419 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
1420 if ((field = lookup_field (&t, name)))
1422 if (save_field == field)
1424 if (save_field == NULL_TREE)
1428 tree i1 = DECL_CONTEXT (save_field);
1429 tree i2 = DECL_CONTEXT (field);
1430 error ("reference `%s' is ambiguous: appears in interface `%s' and interface `%s'",
1431 IDENTIFIER_POINTER (name),
1432 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1433 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1434 return error_mark_node;
1439 if (save_field != NULL_TREE)
1442 *typep = CLASSTYPE_SUPER (*typep);
1447 /* Look up the field named NAME in object SELF_VALUE,
1448 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1449 SELF_VALUE is NULL_TREE if looking for a static field. */
1452 build_field_ref (self_value, self_class, name)
1453 tree self_value, self_class, name;
1455 tree base_class = self_class;
1456 tree field_decl = lookup_field (&base_class, name);
1457 if (field_decl == NULL_TREE)
1459 error ("field `%s' not found", IDENTIFIER_POINTER (name));
1460 return error_mark_node;
1462 if (self_value == NULL_TREE)
1464 return build_static_field_ref (field_decl);
1468 tree base_handle_type = promote_type (base_class);
1469 if (base_handle_type != TREE_TYPE (self_value))
1470 self_value = fold (build1 (NOP_EXPR, base_handle_type, self_value));
1471 #ifdef JAVA_USE_HANDLES
1472 self_value = unhand_expr (self_value);
1474 self_value = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (self_value)),
1476 return fold (build (COMPONENT_REF, TREE_TYPE (field_decl),
1477 self_value, field_decl));
1487 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1488 name = get_identifier (buf);
1489 if (IDENTIFIER_LOCAL_VALUE (name))
1490 return IDENTIFIER_LOCAL_VALUE (name);
1493 /* The type of the address of a label is return_address_type_node. */
1494 tree decl = create_label_decl (name);
1495 LABEL_PC (decl) = pc;
1497 return pushdecl (decl);
1501 /* Generate a unique name for the purpose of loops and switches
1502 labels, and try-catch-finally blocks label or temporary variables. */
1507 static int l_number = 0;
1509 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1511 return get_identifier (buff);
1515 create_label_decl (name)
1519 decl = build_decl (LABEL_DECL, name,
1520 TREE_TYPE (return_address_type_node));
1521 DECL_CONTEXT (decl) = current_function_decl;
1522 DECL_IGNORED_P (decl) = 1;
1526 /* This maps a bytecode offset (PC) to various flags. */
1527 char *instruction_bits;
1530 note_label (current_pc, target_pc)
1531 int current_pc ATTRIBUTE_UNUSED, target_pc;
1533 lookup_label (target_pc);
1534 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1537 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1538 where CONDITION is one of one the compare operators. */
1541 expand_compare (condition, value1, value2, target_pc)
1542 enum tree_code condition;
1543 tree value1, value2;
1546 tree target = lookup_label (target_pc);
1547 tree cond = fold (build (condition, boolean_type_node, value1, value2));
1548 expand_start_cond (truthvalue_conversion (cond), 0);
1549 expand_goto (target);
1553 /* Emit code for a TEST-type opcode. */
1556 expand_test (condition, type, target_pc)
1557 enum tree_code condition;
1561 tree value1, value2;
1562 flush_quick_stack ();
1563 value1 = pop_value (type);
1564 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1565 expand_compare (condition, value1, value2, target_pc);
1568 /* Emit code for a COND-type opcode. */
1571 expand_cond (condition, type, target_pc)
1572 enum tree_code condition;
1576 tree value1, value2;
1577 flush_quick_stack ();
1578 /* note: pop values in opposite order */
1579 value2 = pop_value (type);
1580 value1 = pop_value (type);
1581 /* Maybe should check value1 and value2 for type compatibility ??? */
1582 expand_compare (condition, value1, value2, target_pc);
1586 expand_java_goto (target_pc)
1589 tree target_label = lookup_label (target_pc);
1590 flush_quick_stack ();
1591 expand_goto (target_label);
1596 expand_java_call (target_pc, return_address)
1597 int target_pc, return_address;
1599 tree target_label = lookup_label (target_pc);
1600 tree value = build_int_2 (return_address, return_address < 0 ? -1 : 0);
1602 flush_quick_stack ();
1603 expand_goto (target_label);
1607 expand_java_ret (return_address)
1608 tree return_address ATTRIBUTE_UNUSED;
1610 warning ("ret instruction not implemented");
1612 tree target_label = lookup_label (target_pc);
1613 flush_quick_stack ();
1614 expand_goto (target_label);
1620 pop_arguments (arg_types)
1623 if (arg_types == end_params_node)
1625 if (TREE_CODE (arg_types) == TREE_LIST)
1627 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1628 tree type = TREE_VALUE (arg_types);
1629 tree arg = pop_value (type);
1630 if (PROMOTE_PROTOTYPES
1631 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1632 && INTEGRAL_TYPE_P (type))
1633 arg = convert (integer_type_node, arg);
1634 return tree_cons (NULL_TREE, arg, tail);
1639 /* Build an expression to initialize the class CLAS.
1640 if EXPR is non-NULL, returns an expression to first call the initializer
1641 (if it is needed) and then calls EXPR. */
1644 build_class_init (clas, expr)
1648 struct init_test_hash_entry *ite;
1649 if (inherits_from_p (current_class, clas))
1652 if (always_initialize_class_p)
1654 init = build (CALL_EXPR, void_type_node,
1655 build_address_of (soft_initclass_node),
1656 build_tree_list (NULL_TREE, build_class_ref (clas)),
1658 TREE_SIDE_EFFECTS (init) = 1;
1662 ite = (struct init_test_hash_entry *)
1663 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
1664 (const hash_table_key) clas,
1667 if (ite->init_test_decl == 0)
1668 ite->init_test_decl = build_decl (VAR_DECL, NULL_TREE,
1670 /* Tell the check-init code to ignore this decl. */
1671 DECL_BIT_INDEX(ite->init_test_decl) = -1;
1673 init = build (CALL_EXPR, void_type_node,
1674 build_address_of (soft_initclass_node),
1675 build_tree_list (NULL_TREE, build_class_ref (clas)),
1677 TREE_SIDE_EFFECTS (init) = 1;
1678 call = build (COMPOUND_EXPR, TREE_TYPE (expr), init,
1679 build (MODIFY_EXPR, boolean_type_node,
1680 ite->init_test_decl, boolean_true_node));
1681 TREE_SIDE_EFFECTS (call) = 1;
1682 init = build (COND_EXPR, void_type_node,
1683 build (EQ_EXPR, boolean_type_node,
1684 ite->init_test_decl, boolean_false_node),
1685 call, integer_zero_node);
1686 TREE_SIDE_EFFECTS (init) = 1;
1689 if (expr != NULL_TREE)
1691 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1692 TREE_SIDE_EFFECTS (expr) = 1;
1699 build_known_method_ref (method, method_type, self_type, method_signature, arg_list)
1700 tree method, method_type ATTRIBUTE_UNUSED, self_type,
1701 method_signature ATTRIBUTE_UNUSED, arg_list ATTRIBUTE_UNUSED;
1704 if (is_compiled_class (self_type))
1706 make_decl_rtl (method, NULL);
1707 func = build1 (ADDR_EXPR, method_ptr_type_node, method);
1711 /* We don't know whether the method has been (statically) compiled.
1712 Compile this code to get a reference to the method's code:
1714 SELF_TYPE->methods[METHOD_INDEX].ncode
1716 This is guaranteed to work (assuming SELF_TYPE has
1717 been initialized), since if the method is not compiled yet,
1718 its ncode points to a trampoline that forces compilation. */
1720 int method_index = 0;
1722 tree ref = build_class_ref (self_type);
1723 ref = build1 (INDIRECT_REF, class_type_node, ref);
1724 if (ncode_ident == NULL_TREE)
1725 ncode_ident = get_identifier ("ncode");
1726 if (methods_ident == NULL_TREE)
1727 methods_ident = get_identifier ("methods");
1728 ref = build (COMPONENT_REF, method_ptr_type_node, ref,
1729 lookup_field (&class_type_node, methods_ident));
1730 for (meth = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (self_type));
1731 ; meth = TREE_CHAIN (meth))
1735 if (meth == NULL_TREE)
1736 fatal ("method '%s' not found in class",
1737 IDENTIFIER_POINTER (DECL_NAME (method)));
1740 method_index *= int_size_in_bytes (method_type_node);
1741 ref = fold (build (PLUS_EXPR, method_ptr_type_node,
1742 ref, build_int_2 (method_index, 0)));
1743 ref = build1 (INDIRECT_REF, method_type_node, ref);
1744 func = build (COMPONENT_REF, nativecode_ptr_type_node,
1746 lookup_field (&method_type_node, ncode_ident));
1752 invoke_build_dtable (is_invoke_interface, arg_list)
1753 int is_invoke_interface;
1756 tree dtable, objectref;
1758 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
1760 /* If we're dealing with interfaces and if the objectref
1761 argument is an array then get the dispatch table of the class
1762 Object rather than the one from the objectref. */
1763 objectref = (is_invoke_interface
1764 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list))) ?
1765 object_type_node : TREE_VALUE (arg_list));
1767 if (dtable_ident == NULL_TREE)
1768 dtable_ident = get_identifier ("vtable");
1769 dtable = build1 (INDIRECT_REF, object_type_node, objectref );
1770 dtable = build (COMPONENT_REF, dtable_ptr_type, dtable,
1771 lookup_field (&object_type_node, dtable_ident));
1777 build_invokevirtual (dtable, method)
1778 tree dtable, method;
1781 tree nativecode_ptr_ptr_type_node
1782 = build_pointer_type (nativecode_ptr_type_node);
1783 tree method_index = convert (sizetype, DECL_VINDEX (method));
1785 /* Add one to skip "class" field of dtable, and one to skip unused
1786 vtable entry (for C++ compatibility). */
1787 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1788 method_index = size_binop (MULT_EXPR, method_index,
1789 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
1790 func = fold (build (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
1791 convert (nativecode_ptr_ptr_type_node, method_index)));
1792 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
1798 build_invokeinterface (dtable, method)
1799 tree dtable, method;
1801 static tree class_ident = NULL_TREE;
1808 /* We expand invokeinterface here. _Jv_LookupInterfaceMethod() will
1809 ensure that the selected method exists, is public and not
1810 abstract nor static. */
1812 if (class_ident == NULL_TREE)
1814 class_ident = get_identifier ("class");
1815 ggc_add_tree_root (&class_ident, 1);
1818 dtable = build1 (INDIRECT_REF, dtable_type, dtable);
1819 dtable = build (COMPONENT_REF, class_ptr_type, dtable,
1820 lookup_field (&dtable_type, class_ident));
1822 interface = DECL_CONTEXT (method);
1823 layout_class_methods (interface);
1826 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
1830 idx = build_int_2 (i, 0);
1833 if (meth == NULL_TREE)
1834 fatal ("internal error in build_invokeinterface");
1837 lookup_arg = tree_cons (NULL_TREE, dtable,
1838 tree_cons (NULL_TREE, build_class_ref (interface),
1839 build_tree_list (NULL_TREE, idx)));
1841 return build (CALL_EXPR, ptr_type_node,
1842 build_address_of (soft_lookupinterfacemethod_node),
1843 lookup_arg, NULL_TREE);
1846 /* Expand one of the invoke_* opcodes.
1847 OCPODE is the specific opcode.
1848 METHOD_REF_INDEX is an index into the constant pool.
1849 NARGS is the number of arguments, or -1 if not specified. */
1852 expand_invoke (opcode, method_ref_index, nargs)
1854 int method_ref_index;
1855 int nargs ATTRIBUTE_UNUSED;
1857 tree method_signature = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
1858 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool, method_ref_index);
1859 tree self_type = get_class_constant
1860 (current_jcf, COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool, method_ref_index));
1861 const char *self_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
1862 tree call, func, method, arg_list, method_type;
1863 tree cond = NULL_TREE;
1865 if (! CLASS_LOADED_P (self_type))
1867 load_class (self_type, 1);
1868 safe_layout_class (self_type);
1869 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
1870 fatal ("failed to find class '%s'", self_name);
1872 layout_class_methods (self_type);
1874 if (ID_INIT_P (method_name))
1875 method = lookup_java_constructor (CLASS_TO_HANDLE_TYPE (self_type),
1878 method = lookup_java_method (CLASS_TO_HANDLE_TYPE (self_type),
1879 method_name, method_signature);
1880 if (method == NULL_TREE)
1882 error ("Class '%s' has no method named '%s' matching signature '%s'",
1884 IDENTIFIER_POINTER (method_name),
1885 IDENTIFIER_POINTER (method_signature));
1887 /* Invoke static can't invoke static/abstract method */
1888 else if (opcode == OPCODE_invokestatic)
1890 if (!METHOD_STATIC (method))
1892 error ("invokestatic on non static method");
1895 else if (METHOD_ABSTRACT (method))
1897 error ("invokestatic on abstract method");
1903 if (METHOD_STATIC (method))
1905 error ("invoke[non-static] on static method");
1910 if (method == NULL_TREE)
1912 method_type = get_type_from_signature (method_signature);
1913 pop_arguments (TYPE_ARG_TYPES (method_type));
1914 if (opcode != OPCODE_invokestatic)
1915 pop_type (self_type);
1916 method_type = promote_type (TREE_TYPE (method_type));
1917 push_value (convert (method_type, integer_zero_node));
1921 method_type = TREE_TYPE (method);
1922 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
1923 flush_quick_stack ();
1926 if (opcode == OPCODE_invokestatic)
1927 func = build_known_method_ref (method, method_type, self_type,
1928 method_signature, arg_list);
1929 else if (opcode == OPCODE_invokespecial
1930 || (opcode == OPCODE_invokevirtual
1931 && (METHOD_PRIVATE (method)
1932 || METHOD_FINAL (method)
1933 || CLASS_FINAL (TYPE_NAME (self_type)))))
1935 /* If the object for the method call is null, we throw an
1936 exception. We don't do this if the object is the current
1937 method's `this'. In other cases we just rely on an
1938 optimization pass to eliminate redundant checks. FIXME:
1939 Unfortunately there doesn't seem to be a way to determine
1940 what the current method is right now. */
1941 /* We use a SAVE_EXPR here to make sure we only evaluate
1942 the new `self' expression once. */
1943 tree save_arg = save_expr (TREE_VALUE (arg_list));
1944 TREE_VALUE (arg_list) = save_arg;
1945 cond = build (EQ_EXPR, boolean_type_node, save_arg, null_pointer_node);
1946 func = build_known_method_ref (method, method_type, self_type,
1947 method_signature, arg_list);
1951 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
1953 if (opcode == OPCODE_invokevirtual)
1954 func = build_invokevirtual (dtable, method);
1956 func = build_invokeinterface (dtable, method);
1958 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
1959 call = build (CALL_EXPR, TREE_TYPE (method_type), func, arg_list, NULL_TREE);
1960 TREE_SIDE_EFFECTS (call) = 1;
1962 if (cond != NULL_TREE)
1964 /* We have to make the `then' branch a compound expression to
1965 make the types turn out right. This seems bizarre. */
1966 call = build (COND_EXPR, TREE_TYPE (call), cond,
1967 build (COMPOUND_EXPR, TREE_TYPE (call),
1968 build (CALL_EXPR, void_type_node,
1969 build_address_of (soft_nullpointer_node),
1970 NULL_TREE, NULL_TREE),
1971 (FLOAT_TYPE_P (TREE_TYPE (call))
1972 ? build_real (TREE_TYPE (call), dconst0)
1973 : build1 (CONVERT_EXPR, TREE_TYPE (call),
1974 integer_zero_node))),
1976 TREE_SIDE_EFFECTS (call) = 1;
1979 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
1980 expand_expr_stmt (call);
1984 flush_quick_stack ();
1988 /* Create a stub which will be put into the vtable but which will call
1992 build_jni_stub (method)
1995 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
1996 tree jni_func_type, tem;
1997 tree env_var, res_var = NULL_TREE, block;
1998 tree method_args, res_type;
2001 tree klass = DECL_CONTEXT (method);
2002 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2003 klass = build_class_ref (klass);
2005 if (! METHOD_NATIVE (method) || ! flag_jni)
2008 DECL_ARTIFICIAL (method) = 1;
2009 DECL_EXTERNAL (method) = 0;
2011 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2012 DECL_CONTEXT (env_var) = method;
2014 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2016 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2017 TREE_TYPE (TREE_TYPE (method)));
2018 DECL_CONTEXT (res_var) = method;
2019 TREE_CHAIN (env_var) = res_var;
2022 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2023 TREE_STATIC (meth_var) = 1;
2024 TREE_PUBLIC (meth_var) = 0;
2025 DECL_EXTERNAL (meth_var) = 0;
2026 DECL_CONTEXT (meth_var) = method;
2027 make_decl_rtl (meth_var, NULL);
2028 meth_var = pushdecl_top_level (meth_var);
2030 /* One strange way that the front ends are different is that they
2031 store arguments differently. */
2033 method_args = DECL_ARGUMENTS (method);
2035 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2036 block = build_block (env_var, NULL_TREE, NULL_TREE,
2037 method_args, NULL_TREE);
2038 TREE_SIDE_EFFECTS (block) = 1;
2039 /* When compiling from source we don't set the type of the block,
2040 because that will prevent patch_return from ever being run. */
2042 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2044 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2045 body = build (MODIFY_EXPR, ptr_type_node, env_var,
2046 build (CALL_EXPR, ptr_type_node,
2047 build_address_of (soft_getjnienvnewframe_node),
2048 build_tree_list (NULL_TREE, klass),
2050 CAN_COMPLETE_NORMALLY (body) = 1;
2052 /* All the arguments to this method become arguments to the
2053 underlying JNI function. If we had to wrap object arguments in a
2054 special way, we would do that here. */
2056 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2057 args = tree_cons (NULL_TREE, tem, args);
2058 args = nreverse (args);
2059 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2061 /* For a static method the second argument is the class. For a
2062 non-static method the second argument is `this'; that is already
2063 available in the argument list. */
2064 if (METHOD_STATIC (method))
2066 args = tree_cons (NULL_TREE, klass, args);
2067 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2070 /* The JNIEnv structure is the first argument to the JNI function. */
2071 args = tree_cons (NULL_TREE, env_var, args);
2072 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2074 /* We call _Jv_LookupJNIMethod to find the actual underlying
2075 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2076 exception if this function is not found at runtime. */
2077 method_sig = build_java_signature (TREE_TYPE (method));
2079 build_tree_list (NULL_TREE,
2080 build_utf8_ref (unmangle_classname
2081 (IDENTIFIER_POINTER (method_sig),
2082 IDENTIFIER_LENGTH (method_sig))));
2083 tem = DECL_NAME (method);
2085 = tree_cons (NULL_TREE, klass,
2086 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2089 = build_pointer_type (build_function_type (TREE_TYPE (TREE_TYPE (method)),
2092 jnifunc = build (COND_EXPR, ptr_type_node,
2094 build (MODIFY_EXPR, ptr_type_node,
2096 build (CALL_EXPR, ptr_type_node,
2097 build_address_of (soft_lookupjnimethod_node),
2098 lookup_arg, NULL_TREE)));
2100 /* Now we make the actual JNI call via the resulting function
2102 call = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2103 build1 (NOP_EXPR, jni_func_type, jnifunc),
2106 /* If the JNI call returned a result, capture it here. If we had to
2107 unwrap JNI object results, we would do that here. */
2108 if (res_var != NULL_TREE)
2109 call = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2112 TREE_SIDE_EFFECTS (call) = 1;
2113 CAN_COMPLETE_NORMALLY (call) = 1;
2115 body = build (COMPOUND_EXPR, void_type_node, body, call);
2116 TREE_SIDE_EFFECTS (body) = 1;
2118 /* Now free the environment we allocated. */
2119 call = build (CALL_EXPR, ptr_type_node,
2120 build_address_of (soft_jnipopsystemframe_node),
2121 build_tree_list (NULL_TREE, env_var),
2123 TREE_SIDE_EFFECTS (call) = 1;
2124 CAN_COMPLETE_NORMALLY (call) = 1;
2125 body = build (COMPOUND_EXPR, void_type_node, body, call);
2126 TREE_SIDE_EFFECTS (body) = 1;
2128 /* Finally, do the return. When compiling from source we rely on
2129 patch_return to patch the return value -- because DECL_RESULT is
2130 not set at the time this function is called. */
2133 res_type = void_type_node;
2134 if (res_var != NULL_TREE)
2137 if (! DECL_RESULT (method))
2139 /* Make sure we copy the result variable to the actual
2140 result. We use the type of the DECL_RESULT because it
2141 might be different from the return type of the function:
2142 it might be promoted. */
2143 drt = TREE_TYPE (DECL_RESULT (method));
2144 if (drt != TREE_TYPE (res_var))
2145 res_var = build1 (CONVERT_EXPR, drt, res_var);
2146 res_var = build (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2147 TREE_SIDE_EFFECTS (res_var) = 1;
2152 /* This is necessary to get patch_return to run. */
2153 res_type = NULL_TREE;
2155 body = build (COMPOUND_EXPR, void_type_node, body,
2156 build1 (RETURN_EXPR, res_type, res_var));
2157 TREE_SIDE_EFFECTS (body) = 1;
2159 BLOCK_EXPR_BODY (block) = body;
2163 /* Expand an operation to extract from or store into a field.
2164 IS_STATIC is 1 iff the field is static.
2165 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2166 FIELD_REF_INDEX is an index into the constant pool. */
2169 expand_java_field_op (is_static, is_putting, field_ref_index)
2172 int field_ref_index;
2175 get_class_constant (current_jcf,
2176 COMPONENT_REF_CLASS_INDEX (¤t_jcf->cpool,
2178 const char *self_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2179 tree field_name = COMPONENT_REF_NAME (¤t_jcf->cpool, field_ref_index);
2180 tree field_signature = COMPONENT_REF_SIGNATURE (¤t_jcf->cpool,
2182 tree field_type = get_type_from_signature (field_signature);
2183 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2186 tree field_decl = lookup_field (&self_type, field_name);
2187 if (field_decl == error_mark_node)
2191 else if (field_decl == NULL_TREE)
2193 error ("Missing field '%s' in '%s'",
2194 IDENTIFIER_POINTER (field_name), self_name);
2197 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2199 error ("Mismatching signature for field '%s' in '%s'",
2200 IDENTIFIER_POINTER (field_name), self_name);
2203 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2207 push_value (convert (field_type, integer_zero_node));
2208 flush_quick_stack ();
2212 /* Inline references to java.lang.PRIMTYPE.TYPE.
2213 In addition to being a useful (minor) optimization,
2214 this is also needed to avoid circularities in the implementation
2215 of these fields in libjava. */
2216 if (field_name == TYPE_identifier_node && ! is_putting
2217 && ! flag_emit_class_files && field_type == class_ptr_type
2218 && strncmp (self_name, "java.lang.", 10) == 0)
2220 tree typ = build_primtype_type_ref (self_name);
2228 field_ref = build_field_ref (field_ref, self_type, field_name);
2230 field_ref = build_class_init (self_type, field_ref);
2233 flush_quick_stack ();
2234 if (FIELD_FINAL (field_decl))
2236 if (DECL_CONTEXT (field_decl) != current_class)
2237 error_with_decl (field_decl,
2238 "assignment to final field `%s' not in field's class");
2239 else if (FIELD_STATIC (field_decl))
2241 if (!DECL_CLINIT_P (current_function_decl))
2242 warning_with_decl (field_decl,
2243 "assignment to final static field `%s' not in class initializer");
2247 tree cfndecl_name = DECL_NAME (current_function_decl);
2248 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2249 && !ID_FINIT_P (cfndecl_name))
2250 warning_with_decl (field_decl, "assignment to final field `%s' not in constructor");
2253 expand_assignment (field_ref, new_value, 0, 0);
2256 push_value (field_ref);
2260 build_primtype_type_ref (self_name)
2261 const char *self_name;
2263 const char *class_name = self_name+10;
2265 if (strncmp(class_name, "Byte", 4) == 0)
2266 typ = byte_type_node;
2267 else if (strncmp(class_name, "Short", 5) == 0)
2268 typ = short_type_node;
2269 else if (strncmp(class_name, "Integer", 7) == 0)
2270 typ = int_type_node;
2271 else if (strncmp(class_name, "Long", 4) == 0)
2272 typ = long_type_node;
2273 else if (strncmp(class_name, "Float", 5) == 0)
2274 typ = float_type_node;
2275 else if (strncmp(class_name, "Double", 6) == 0)
2276 typ = double_type_node;
2277 else if (strncmp(class_name, "Boolean", 7) == 0)
2278 typ = boolean_type_node;
2279 else if (strncmp(class_name, "Char", 4) == 0)
2280 typ = char_type_node;
2281 else if (strncmp(class_name, "Void", 4) == 0)
2282 typ = void_type_node;
2285 if (typ != NULL_TREE)
2286 return build_class_ref (typ);
2292 load_type_state (label)
2296 tree vec = LABEL_TYPE_STATE (label);
2297 int cur_length = TREE_VEC_LENGTH (vec);
2298 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2299 for (i = 0; i < cur_length; i++)
2300 type_map [i] = TREE_VEC_ELT (vec, i);
2303 /* Do the expansion of a Java switch. With Gcc, switches are front-end
2304 dependant things, but they rely on gcc routines. This function is
2305 placed here because it uses things defined locally in parse.y. */
2308 case_identity (t, v)
2309 tree t __attribute__ ((__unused__));
2315 /* Return the name of the vtable for an array of a given primitive
2318 get_primitive_array_vtable (tree elt)
2321 if (elt == boolean_type_node)
2322 r = boolean_array_vtable;
2323 else if (elt == byte_type_node)
2324 r = byte_array_vtable;
2325 else if (elt == char_type_node)
2326 r = char_array_vtable;
2327 else if (elt == short_type_node)
2328 r = short_array_vtable;
2329 else if (elt == int_type_node)
2330 r = int_array_vtable;
2331 else if (elt == long_type_node)
2332 r = long_array_vtable;
2333 else if (elt == float_type_node)
2334 r = float_array_vtable;
2335 else if (elt == double_type_node)
2336 r = double_array_vtable;
2339 return build_address_of (r);
2343 java_lang_expand_expr (exp, target, tmode, modifier)
2345 rtx target ATTRIBUTE_UNUSED;
2346 enum machine_mode tmode ATTRIBUTE_UNUSED;
2347 enum expand_modifier modifier ATTRIBUTE_UNUSED;
2351 switch (TREE_CODE (exp))
2353 case NEW_ARRAY_INIT:
2356 tree array_type = TREE_TYPE (TREE_TYPE (exp));
2357 tree element_type = TYPE_ARRAY_ELEMENT (array_type);
2358 tree data_fld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
2359 HOST_WIDE_INT ilength = java_array_type_length (array_type);
2360 tree length = build_int_2 (ilength, 0);
2361 tree init = TREE_OPERAND (exp, 0);
2364 /* See if we can generate the array statically. */
2365 if (TREE_CONSTANT (init) && TREE_STATIC (exp)
2366 && JPRIMITIVE_TYPE_P (element_type))
2368 tree temp, value, init_decl;
2370 START_RECORD_CONSTRUCTOR (temp, object_type_node);
2371 PUSH_FIELD_VALUE (temp, "vtable",
2372 get_primitive_array_vtable (element_type));
2373 if (! flag_hash_synchronization)
2374 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
2375 FINISH_RECORD_CONSTRUCTOR (temp);
2376 START_RECORD_CONSTRUCTOR (value, array_type);
2377 PUSH_SUPER_VALUE (value, temp);
2378 /* FIXME: build a new `length' here to get it on the right
2380 PUSH_FIELD_VALUE (value, "length", build_int_2 (ilength, 0));
2381 PUSH_FIELD_VALUE (value, "data", init);
2382 FINISH_RECORD_CONSTRUCTOR (value);
2384 init_decl = build_decl (VAR_DECL, generate_name (), array_type);
2385 pushdecl_top_level (init_decl);
2386 TREE_STATIC (init_decl) = 1;
2387 DECL_INITIAL (init_decl) = value;
2388 DECL_IGNORED_P (init_decl) = 1;
2389 TREE_READONLY (init_decl) = 1;
2390 make_decl_rtl (init_decl, NULL);
2391 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl)) = 1;
2392 init = build1 (ADDR_EXPR, TREE_TYPE (exp), init_decl);
2393 r = expand_expr (init, target, tmode, modifier);
2397 array_decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (exp));
2398 expand_decl (array_decl);
2399 tmp = expand_assignment (array_decl,
2400 build_new_array (element_type, length),
2402 if (TREE_CONSTANT (init)
2403 && ilength >= 10 && JPRIMITIVE_TYPE_P (element_type))
2406 init_decl = build_decl (VAR_DECL, generate_name (),
2408 pushdecl_top_level (init_decl);
2409 TREE_STATIC (init_decl) = 1;
2410 DECL_INITIAL (init_decl) = init;
2411 DECL_IGNORED_P (init_decl) = 1;
2412 TREE_READONLY (init_decl) = 1;
2413 make_decl_rtl (init_decl, NULL);
2414 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl)) = 1;
2417 expand_assignment (build (COMPONENT_REF, TREE_TYPE (data_fld),
2418 build1 (INDIRECT_REF, array_type,
2419 array_decl), data_fld), init, 0, 0);
2423 if (BLOCK_EXPR_BODY (exp))
2426 tree body = BLOCK_EXPR_BODY (exp);
2427 pushlevel (2); /* 2 and above */
2428 expand_start_bindings (0);
2429 local = BLOCK_EXPR_DECLS (exp);
2432 tree next = TREE_CHAIN (local);
2433 layout_decl (local, 0);
2434 expand_decl (pushdecl (local));
2437 /* Avoid deep recursion for long block. */
2438 while (TREE_CODE (body) == COMPOUND_EXPR)
2440 expand_expr (TREE_OPERAND (body, 0), const0_rtx, VOIDmode, 0);
2442 body = TREE_OPERAND (body, 1);
2444 expand_expr (body, const0_rtx, VOIDmode, 0);
2447 expand_end_bindings (getdecls (), 1, 0);
2455 if (pushcase (TREE_OPERAND (exp, 0), case_identity,
2456 build_decl (LABEL_DECL, NULL_TREE, NULL_TREE),
2459 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (exp);
2461 (wfl_operator, "Duplicate case label: `%s'",
2462 print_int_node (TREE_OPERAND (exp, 0)));
2468 pushcase (NULL_TREE, 0,
2469 build_decl (LABEL_DECL, NULL_TREE, NULL_TREE), NULL);
2473 expand_start_case (0, TREE_OPERAND (exp, 0), int_type_node, "switch");
2474 expand_expr_stmt (TREE_OPERAND (exp, 1));
2475 expand_end_case (TREE_OPERAND (exp, 0));
2479 /* We expand a try[-catch] block */
2481 /* Expand the try block */
2482 expand_eh_region_start ();
2483 expand_expr_stmt (TREE_OPERAND (exp, 0));
2484 expand_start_all_catch ();
2486 /* Expand all catch clauses (EH handlers) */
2487 for (current = TREE_OPERAND (exp, 1); current;
2488 current = TREE_CHAIN (current))
2491 tree catch = TREE_OPERAND (current, 0);
2492 tree decl = BLOCK_EXPR_DECLS (catch);
2493 type = (decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE);
2494 start_catch_handler (prepare_eh_table_type (type));
2495 expand_expr_stmt (TREE_OPERAND (current, 0));
2497 expand_resume_after_catch ();
2498 end_catch_handler ();
2500 expand_end_all_catch ();
2504 fatal ("Can't expand '%s' tree - java_lang_expand_expr",
2505 tree_code_name [TREE_CODE (exp)]);
2509 /* Go over METHOD's bytecode and note instruction starts in
2510 instruction_bits[]. */
2513 note_instructions (jcf, method)
2518 unsigned char* byte_ops;
2519 long length = DECL_CODE_LENGTH (method);
2524 #undef RET /* Defined by config/i386/i386.h */
2525 #undef AND /* Causes problems with opcodes for iand and land. */
2527 #define BCODE byte_ops
2528 #define BYTE_type_node byte_type_node
2529 #define SHORT_type_node short_type_node
2530 #define INT_type_node int_type_node
2531 #define LONG_type_node long_type_node
2532 #define CHAR_type_node char_type_node
2533 #define PTR_type_node ptr_type_node
2534 #define FLOAT_type_node float_type_node
2535 #define DOUBLE_type_node double_type_node
2536 #define VOID_type_node void_type_node
2537 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2538 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2539 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2540 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2542 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2544 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2545 byte_ops = jcf->read_ptr;
2546 instruction_bits = xrealloc (instruction_bits, length + 1);
2547 memset (instruction_bits, 0, length + 1);
2549 /* This pass figures out which PC can be the targets of jumps. */
2550 for (PC = 0; PC < length;)
2552 int oldpc = PC; /* PC at instruction start. */
2553 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2554 switch (byte_ops[PC++])
2556 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2558 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2561 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2563 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2564 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2565 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2566 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2567 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2568 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2569 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2570 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2572 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2573 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2574 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2575 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2576 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2577 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2578 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2579 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2581 /* two forms of wide instructions */
2582 #define PRE_SPECIAL_WIDE(IGNORE) \
2584 int modified_opcode = IMMEDIATE_u1; \
2585 if (modified_opcode == OPCODE_iinc) \
2587 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2588 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2592 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2596 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2598 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2600 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2601 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2602 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2603 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2604 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2605 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2606 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2607 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2608 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2609 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2611 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2612 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2613 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2614 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2615 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2616 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2617 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2618 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2620 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2622 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2623 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2625 #define PRE_LOOKUP_SWITCH \
2626 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2627 NOTE_LABEL (default_offset+oldpc); \
2629 while (--npairs >= 0) { \
2630 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2631 jint offset = IMMEDIATE_s4; \
2632 NOTE_LABEL (offset+oldpc); } \
2635 #define PRE_TABLE_SWITCH \
2636 { jint default_offset = IMMEDIATE_s4; \
2637 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2638 NOTE_LABEL (default_offset+oldpc); \
2640 while (low++ <= high) { \
2641 jint offset = IMMEDIATE_s4; \
2642 NOTE_LABEL (offset+oldpc); } \
2645 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2646 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2647 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2648 (void)(IMMEDIATE_u2); \
2649 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2651 #include "javaop.def"
2658 expand_byte_code (jcf, method)
2664 const unsigned char *linenumber_pointer;
2665 int dead_code_index = -1;
2666 unsigned char* byte_ops;
2667 long length = DECL_CODE_LENGTH (method);
2670 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2671 byte_ops = jcf->read_ptr;
2673 /* We make an initial pass of the line number table, to note
2674 which instructions have associated line number entries. */
2675 linenumber_pointer = linenumber_table;
2676 for (i = 0; i < linenumber_count; i++)
2678 int pc = GET_u2 (linenumber_pointer);
2679 linenumber_pointer += 4;
2681 warning ("invalid PC in line number table");
2684 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2685 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2686 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2690 if (! verify_jvm_instructions (jcf, byte_ops, length))
2693 /* Translate bytecodes to rtl instructions. */
2694 linenumber_pointer = linenumber_table;
2695 for (PC = 0; PC < length;)
2697 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2699 tree label = lookup_label (PC);
2700 flush_quick_stack ();
2701 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2702 expand_label (label);
2703 if (LABEL_VERIFIED (label) || PC == 0)
2704 load_type_state (label);
2707 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2709 if (dead_code_index == -1)
2711 /* This is the start of a region of unreachable bytecodes.
2712 They still need to be processed in order for EH ranges
2713 to get handled correctly. However, we can simply
2714 replace these bytecodes with nops. */
2715 dead_code_index = PC;
2718 /* Turn this bytecode into a nop. */
2723 if (dead_code_index != -1)
2725 /* We've just reached the end of a region of dead code. */
2726 warning ("Unreachable bytecode from %d to before %d.",
2727 dead_code_index, PC);
2728 dead_code_index = -1;
2732 /* Handle possible line number entry for this PC.
2734 This code handles out-of-order and multiple linenumbers per PC,
2735 but is optimized for the case of line numbers increasing
2736 monotonically with PC. */
2737 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2739 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2740 || GET_u2 (linenumber_pointer) != PC)
2741 linenumber_pointer = linenumber_table;
2742 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2744 int pc = GET_u2 (linenumber_pointer);
2745 linenumber_pointer += 4;
2748 lineno = GET_u2 (linenumber_pointer - 2);
2749 emit_line_note (input_filename, lineno);
2750 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2755 maybe_pushlevels (PC);
2756 PC = process_jvm_instruction (PC, byte_ops, length);
2757 maybe_poplevels (PC);
2760 if (dead_code_index != -1)
2762 /* We've just reached the end of a region of dead code. */
2763 warning ("Unreachable bytecode from %d to the end of the method.",
2769 java_push_constant_from_pool (jcf, index)
2774 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
2777 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
2778 index = alloc_name_constant (CONSTANT_String, name);
2779 c = build_ref_from_constant_pool (index);
2780 TREE_TYPE (c) = promote_type (string_type_node);
2783 c = get_constant (jcf, index);
2788 process_jvm_instruction (PC, byte_ops, length)
2790 const unsigned char* byte_ops;
2791 long length ATTRIBUTE_UNUSED;
2793 const char *opname; /* Temporary ??? */
2794 int oldpc = PC; /* PC at instruction start. */
2796 /* If the instruction is at the beginning of a exception handler,
2797 replace the top of the stack with the thrown object reference */
2798 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
2800 tree type = pop_type (ptr_type_node);
2801 push_value (build1 (NOP_EXPR, type, soft_exceptioninfo_call_node));
2804 switch (byte_ops[PC++])
2806 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2809 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2812 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
2814 int saw_index = 0; \
2815 int index = OPERAND_VALUE; \
2816 build_java_ret (find_local_variable (index, ptr_type_node, oldpc)); \
2819 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
2821 tree where = lookup_label (oldpc+OPERAND_VALUE); \
2822 tree ret = lookup_label (PC); \
2823 build_java_jsr (where, ret); \
2824 load_type_state (ret); \
2827 /* Push a constant onto the stack. */
2828 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
2829 { int saw_index = 0; int ival = (OPERAND_VALUE); \
2830 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
2831 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
2833 /* internal macro added for use by the WIDE case */
2834 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
2835 push_value (find_local_variable (OPVALUE, type_map[OPVALUE], oldpc));
2837 /* Push local variable onto the opcode stack. */
2838 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
2840 /* have to do this since OPERAND_VALUE may have side-effects */ \
2841 int opvalue = OPERAND_VALUE; \
2842 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2845 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
2846 expand_java_return (OPERAND_TYPE##_type_node)
2848 #define REM_EXPR TRUNC_MOD_EXPR
2849 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
2850 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
2852 #define FIELD(IS_STATIC, IS_PUT) \
2853 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
2855 #define TEST(OPERAND_TYPE, CONDITION) \
2856 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2858 #define COND(OPERAND_TYPE, CONDITION) \
2859 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2861 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2862 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
2864 #define BRANCH_GOTO(OPERAND_VALUE) \
2865 expand_java_goto (oldpc + OPERAND_VALUE)
2867 #define BRANCH_CALL(OPERAND_VALUE) \
2868 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
2871 #define BRANCH_RETURN(OPERAND_VALUE) \
2873 tree type = OPERAND_TYPE##_type_node; \
2874 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
2875 expand_java_ret (value); \
2879 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
2880 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2881 fprintf (stderr, "(not implemented)\n")
2882 #define NOT_IMPL1(OPERAND_VALUE) \
2883 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2884 fprintf (stderr, "(not implemented)\n")
2886 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
2888 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
2890 #define STACK_POP(COUNT) java_stack_pop (COUNT)
2892 #define STACK_SWAP(COUNT) java_stack_swap()
2894 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
2895 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
2896 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
2898 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2899 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
2901 #define LOOKUP_SWITCH \
2902 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2903 tree selector = pop_value (INT_type_node); \
2904 tree duplicate, label; \
2905 tree type = TREE_TYPE (selector); \
2906 flush_quick_stack (); \
2907 expand_start_case (0, selector, type, "switch statement");\
2908 while (--npairs >= 0) \
2910 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
2911 tree value = build_int_2 (match, match < 0 ? -1 : 0); \
2912 TREE_TYPE (value) = type; \
2913 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2914 pushcase (value, convert, label, &duplicate); \
2915 expand_java_goto (oldpc + offset); \
2917 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2918 pushcase (NULL_TREE, 0, label, &duplicate); \
2919 expand_java_goto (oldpc + default_offset); \
2920 expand_end_case (selector); \
2923 #define TABLE_SWITCH \
2924 { jint default_offset = IMMEDIATE_s4; \
2925 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2926 tree selector = pop_value (INT_type_node); \
2927 tree duplicate, label; \
2928 tree type = TREE_TYPE (selector); \
2929 flush_quick_stack (); \
2930 expand_start_case (0, selector, type, "switch statement");\
2931 for (; low <= high; low++) \
2933 jint offset = IMMEDIATE_s4; \
2934 tree value = build_int_2 (low, low < 0 ? -1 : 0); \
2935 TREE_TYPE (value) = type; \
2936 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2937 pushcase (value, convert, label, &duplicate); \
2938 expand_java_goto (oldpc + offset); \
2940 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2941 pushcase (NULL_TREE, 0, label, &duplicate); \
2942 expand_java_goto (oldpc + default_offset); \
2943 expand_end_case (selector); \
2946 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2947 { int opcode = byte_ops[PC-1]; \
2948 int method_ref_index = IMMEDIATE_u2; \
2950 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
2952 expand_invoke (opcode, method_ref_index, nargs); \
2955 /* Handle new, checkcast, instanceof */
2956 #define OBJECT(TYPE, OP) \
2957 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
2959 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
2961 #define ARRAY_LOAD(OPERAND_TYPE) \
2963 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
2966 #define ARRAY_STORE(OPERAND_TYPE) \
2968 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
2971 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
2972 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
2973 #define ARRAY_NEW_PTR() \
2974 push_value (build_anewarray (get_class_constant (current_jcf, \
2976 pop_value (int_type_node)));
2977 #define ARRAY_NEW_NUM() \
2979 int atype = IMMEDIATE_u1; \
2980 push_value (build_newarray (atype, pop_value (int_type_node)));\
2982 #define ARRAY_NEW_MULTI() \
2984 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
2985 int ndims = IMMEDIATE_u1; \
2986 expand_java_multianewarray( class, ndims ); \
2989 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
2990 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
2991 pop_value (OPERAND_TYPE##_type_node))));
2993 #define CONVERT2(FROM_TYPE, TO_TYPE) \
2995 push_value (build1 (NOP_EXPR, int_type_node, \
2996 (convert (TO_TYPE##_type_node, \
2997 pop_value (FROM_TYPE##_type_node))))); \
3000 #define CONVERT(FROM_TYPE, TO_TYPE) \
3002 push_value (convert (TO_TYPE##_type_node, \
3003 pop_value (FROM_TYPE##_type_node))); \
3006 /* internal macro added for use by the WIDE case
3007 Added TREE_TYPE (decl) assignment, apbianco */
3008 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3011 int var = OPVALUE; \
3012 tree type = OPTYPE; \
3013 value = pop_value (type); \
3014 type = TREE_TYPE (value); \
3015 decl = find_local_variable (var, type, oldpc); \
3016 set_local_type (var, type ); \
3017 expand_assignment (decl, value, 0, 0); \
3020 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3022 /* have to do this since OPERAND_VALUE may have side-effects */ \
3023 int opvalue = OPERAND_VALUE; \
3024 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3027 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3028 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3030 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3031 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3033 #define MONITOR_OPERATION(call) \
3035 tree o = pop_value (ptr_type_node); \
3037 flush_quick_stack (); \
3038 c = build_java_monitor (call, o); \
3039 TREE_SIDE_EFFECTS (c) = 1; \
3040 expand_expr_stmt (c); \
3043 #define SPECIAL_IINC(IGNORED) \
3045 unsigned int local_var_index = IMMEDIATE_u1; \
3046 int ival = IMMEDIATE_s1; \
3047 expand_iinc(local_var_index, ival, oldpc); \
3050 #define SPECIAL_WIDE(IGNORED) \
3052 int modified_opcode = IMMEDIATE_u1; \
3053 unsigned int local_var_index = IMMEDIATE_u2; \
3054 switch (modified_opcode) \
3058 int ival = IMMEDIATE_s2; \
3059 expand_iinc (local_var_index, ival, oldpc); \
3062 case OPCODE_iload: \
3063 case OPCODE_lload: \
3064 case OPCODE_fload: \
3065 case OPCODE_dload: \
3066 case OPCODE_aload: \
3068 /* duplicate code from LOAD macro */ \
3069 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3072 case OPCODE_istore: \
3073 case OPCODE_lstore: \
3074 case OPCODE_fstore: \
3075 case OPCODE_dstore: \
3076 case OPCODE_astore: \
3078 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3082 error ("unrecogized wide sub-instruction"); \
3086 #define SPECIAL_THROW(IGNORED) \
3087 build_java_athrow (pop_value (throwable_type_node))
3089 #define SPECIAL_BREAK NOT_IMPL1
3090 #define IMPL NOT_IMPL
3092 #include "javaop.def"
3095 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3100 /* Return the opcode at PC in the code section pointed to by
3103 static unsigned char
3104 peek_opcode_at_pc (jcf, code_offset, pc)
3106 int code_offset, pc;
3108 unsigned char opcode;
3109 long absolute_offset = (long)JCF_TELL (jcf);
3111 JCF_SEEK (jcf, code_offset);
3112 opcode = jcf->read_ptr [pc];
3113 JCF_SEEK (jcf, absolute_offset);
3117 /* Some bytecode compilers are emitting accurate LocalVariableTable
3118 attributes. Here's an example:
3123 Attribute "LocalVariableTable"
3124 slot #<n>: ... (PC: PC+1 length: L)
3126 This is accurate because the local in slot <n> really exists after
3127 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3129 This procedure recognizes this situation and extends the live range
3130 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3131 length of the store instruction.)
3133 This function is used by `give_name_to_locals' so that a local's
3134 DECL features a DECL_LOCAL_START_PC such that the first related
3135 store operation will use DECL as a destination, not a unrelated
3136 temporary created for the occasion.
3138 This function uses a global (instruction_bits) `note_instructions' should
3139 have allocated and filled properly. */
3142 maybe_adjust_start_pc (jcf, code_offset, start_pc, slot)
3144 int code_offset, start_pc, slot;
3146 int first, index, opcode;
3155 /* Find last previous instruction and remember it */
3156 for (pc = start_pc-1; pc; pc--)
3157 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3161 /* Retrieve the instruction, handle `wide'. */
3162 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3163 if (opcode == OPCODE_wide)
3166 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3171 case OPCODE_astore_0:
3172 case OPCODE_astore_1:
3173 case OPCODE_astore_2:
3174 case OPCODE_astore_3:
3175 first = OPCODE_astore_0;
3178 case OPCODE_istore_0:
3179 case OPCODE_istore_1:
3180 case OPCODE_istore_2:
3181 case OPCODE_istore_3:
3182 first = OPCODE_istore_0;
3185 case OPCODE_lstore_0:
3186 case OPCODE_lstore_1:
3187 case OPCODE_lstore_2:
3188 case OPCODE_lstore_3:
3189 first = OPCODE_lstore_0;
3192 case OPCODE_fstore_0:
3193 case OPCODE_fstore_1:
3194 case OPCODE_fstore_2:
3195 case OPCODE_fstore_3:
3196 first = OPCODE_fstore_0;
3199 case OPCODE_dstore_0:
3200 case OPCODE_dstore_1:
3201 case OPCODE_dstore_2:
3202 case OPCODE_dstore_3:
3203 first = OPCODE_dstore_0;
3211 index = peek_opcode_at_pc (jcf, code_offset, pc);
3214 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3215 index = (other << 8) + index;
3220 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3221 means we have a <t>store. */
3222 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3228 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3229 order, as specified by Java Language Specification.
3231 The problem is that while expand_expr will evaluate its sub-operands in
3232 left-to-right order, for variables it will just return an rtx (i.e.
3233 an lvalue) for the variable (rather than an rvalue). So it is possible
3234 that a later sub-operand will change the register, and when the
3235 actual operation is done, it will use the new value, when it should
3236 have used the original value.
3238 We fix this by using save_expr. This forces the sub-operand to be
3239 copied into a fresh virtual register,
3241 For method invocation, we modify the arguments so that a
3242 left-to-right order evaluation is performed. Saved expressions
3243 will, in CALL_EXPR order, be reused when the call will be expanded.
3247 force_evaluation_order (node)
3250 if (flag_syntax_only)
3252 if (TREE_CODE_CLASS (TREE_CODE (node)) == '2')
3254 if (TREE_SIDE_EFFECTS (TREE_OPERAND (node, 1)))
3255 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
3257 else if (TREE_CODE (node) == CALL_EXPR || TREE_CODE (node) == NEW_CLASS_EXPR)
3261 if (!TREE_OPERAND (node, 1))
3264 /* This reverses the evaluation order. This is a desired effect. */
3265 for (cmp = NULL_TREE, arg = TREE_OPERAND (node, 1);
3266 arg; arg = TREE_CHAIN (arg))
3268 tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3269 cmp = (cmp == NULL_TREE ? saved :
3270 build (COMPOUND_EXPR, void_type_node, cmp, saved));
3271 TREE_VALUE (arg) = saved;
3274 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3275 TREE_SIDE_EFFECTS (cmp) = 1;
3279 cmp = save_expr (build (COMPOUND_EXPR, TREE_TYPE (node), cmp, node));
3280 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3281 TREE_SIDE_EFFECTS (cmp) = 1;