1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 97-99, 2000 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));
83 static tree operand_type[59];
84 extern struct obstack permanent_obstack;
87 init_expr_processing()
89 operand_type[21] = operand_type[54] = int_type_node;
90 operand_type[22] = operand_type[55] = long_type_node;
91 operand_type[23] = operand_type[56] = float_type_node;
92 operand_type[24] = operand_type[57] = double_type_node;
93 operand_type[25] = operand_type[58] = ptr_type_node;
96 /* We store the stack state in two places:
97 Within a basic block, we use the quick_stack, which is a
98 pushdown list (TREE_LISTs) of expression nodes.
99 This is the top part of the stack; below that we use find_stack_slot.
100 At the end of a basic block, the quick_stack must be flushed
101 to the stack slot array (as handled by find_stack_slot).
102 Using quick_stack generates better code (especially when
103 compiled without optimization), because we do not have to
104 explicitly store and load trees to temporary variables.
106 If a variable is on the quick stack, it means the value of variable
107 when the quick stack was last flushed. Conceptually, flush_quick_stack
108 saves all the the quick_stack elements in parellel. However, that is
109 complicated, so it actually saves them (i.e. copies each stack value
110 to is home virtual register) from low indexes. This allows a quick_stack
111 element at index i (counting from the bottom of stack the) to references
112 slot virtuals for register that are >= i, but not those that are deeper.
113 This convention makes most operations easier. For example iadd works
114 even when the stack contains (reg[0], reg[1]): It results in the
115 stack containing (reg[0]+reg[1]), which is OK. However, some stack
116 operations are more complicated. For example dup given a stack
117 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
118 the convention, since stack value 1 would refer to a register with
119 lower index (reg[0]), which flush_quick_stack does not safely handle.
120 So dup cannot just add an extra element to the quick_stack, but iadd can.
123 tree quick_stack = NULL_TREE;
125 /* A free-list of unused permamnet TREE_LIST nodes. */
126 tree tree_list_free_list = NULL_TREE;
128 /* The stack pointer of the Java virtual machine.
129 This does include the size of the quick_stack. */
133 const unsigned char *linenumber_table;
134 int linenumber_count;
137 truthvalue_conversion (expr)
140 /* It is simpler and generates better code to have only TRUTH_*_EXPR
141 or comparison expressions as truth values at this level.
143 This function should normally be identity for Java. */
145 switch (TREE_CODE (expr))
148 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
149 case TRUTH_ANDIF_EXPR:
150 case TRUTH_ORIF_EXPR:
157 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
160 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
162 /* are these legal? XXX JH */
167 /* These don't change whether an object is non-zero or zero. */
168 return truthvalue_conversion (TREE_OPERAND (expr, 0));
171 /* Distribute the conversion into the arms of a COND_EXPR. */
172 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
173 truthvalue_conversion (TREE_OPERAND (expr, 1)),
174 truthvalue_conversion (TREE_OPERAND (expr, 2))));
177 /* If this is widening the argument, we can ignore it. */
178 if (TYPE_PRECISION (TREE_TYPE (expr))
179 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
180 return truthvalue_conversion (TREE_OPERAND (expr, 0));
181 /* fall through to default */
184 return fold (build (NE_EXPR, boolean_type_node, expr, boolean_false_node));
188 #ifdef JAVA_USE_HANDLES
189 /* Given a pointer to a handle, get a pointer to an object. */
195 tree field, handle_type;
196 expr = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
197 handle_type = TREE_TYPE (expr);
198 field = TYPE_FIELDS (handle_type);
199 expr = build (COMPONENT_REF, TREE_TYPE (field), expr, field);
204 /* Save any stack slots that happen to be in the quick_stack into their
205 home virtual register slots.
207 The copy order is from low stack index to high, to support the invariant
208 that the expression for a slot may contain decls for stack slots with
209 higher (or the same) index, but not lower. */
214 int stack_index = stack_pointer;
215 register tree prev, cur, next;
217 /* First reverse the quick_stack, and count the number of slots it has. */
218 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
220 next = TREE_CHAIN (cur);
221 TREE_CHAIN (cur) = prev;
223 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
227 while (quick_stack != NULL_TREE)
230 tree node = quick_stack, type;
231 quick_stack = TREE_CHAIN (node);
232 TREE_CHAIN (node) = tree_list_free_list;
233 tree_list_free_list = node;
234 node = TREE_VALUE (node);
235 type = TREE_TYPE (node);
237 decl = find_stack_slot (stack_index, type);
239 expand_assignment (decl, node, 0, 0);
240 stack_index += 1 + TYPE_IS_WIDE (type);
249 type = promote_type (type);
250 n_words = 1 + TYPE_IS_WIDE (type);
251 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
252 fatal ("stack overflow");
253 stack_type_map[stack_pointer++] = type;
255 while (--n_words >= 0)
256 stack_type_map[stack_pointer++] = TYPE_SECOND;
263 tree type = TREE_TYPE (value);
264 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
266 type = promote_type (type);
267 value = convert (type, value);
270 if (tree_list_free_list == NULL_TREE)
271 quick_stack = perm_tree_cons (NULL_TREE, value, quick_stack);
274 tree node = tree_list_free_list;
275 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
276 TREE_VALUE (node) = value;
277 TREE_CHAIN (node) = quick_stack;
282 /* Pop a type from the type stack.
283 TYPE is the expected type. Return the actual type, which must be
284 convertible to TYPE, otherwise NULL_TREE is returned. */
292 if (TREE_CODE (type) == RECORD_TYPE)
293 type = promote_type (type);
294 n_words = 1 + TYPE_IS_WIDE (type);
295 if (stack_pointer < n_words)
296 fatal ("stack underflow");
297 while (--n_words > 0)
299 if (stack_type_map[--stack_pointer] != void_type_node)
300 fatal ("Invalid multi-word value on type stack");
302 t = stack_type_map[--stack_pointer];
303 if (type == NULL_TREE || t == type)
305 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
306 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
308 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
310 if (type == ptr_type_node || type == object_ptr_type_node)
312 else if (t == ptr_type_node) /* Special case for null reference. */
314 else if (can_widen_reference_to (t, type))
316 /* This is a kludge, but matches what Sun's verifier does.
317 It can be tricked, but is safe as long as type errors
318 (i.e. interface method calls) are caught at run-time. */
319 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type)))
320 && t == object_ptr_type_node)
326 /* Pop a type from the type stack.
327 TYPE is the expected type. Return the actual type, which must be
328 convertible to TYPE, otherwise call error. */
334 tree t = pop_type_0 (type);
337 error ("unexpected type on stack");
341 /* Return 1f if SOURCE_TYPE can be safely widened to TARGET_TYPE.
342 Handles array types and interfaces. */
345 can_widen_reference_to (source_type, target_type)
346 tree source_type, target_type;
348 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
351 /* Get rid of pointers */
352 if (TREE_CODE (source_type) == POINTER_TYPE)
353 source_type = TREE_TYPE (source_type);
354 if (TREE_CODE (target_type) == POINTER_TYPE)
355 target_type = TREE_TYPE (target_type);
357 if (source_type == target_type)
361 source_type = HANDLE_TO_CLASS_TYPE (source_type);
362 target_type = HANDLE_TO_CLASS_TYPE (target_type);
363 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
365 HOST_WIDE_INT source_length, target_length;
366 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
368 target_length = java_array_type_length (target_type);
369 if (target_length >= 0)
371 source_length = java_array_type_length (source_type);
372 if (source_length != target_length)
375 source_type = TYPE_ARRAY_ELEMENT (source_type);
376 target_type = TYPE_ARRAY_ELEMENT (target_type);
377 if (source_type == target_type)
379 if (TREE_CODE (source_type) != POINTER_TYPE
380 || TREE_CODE (target_type) != POINTER_TYPE)
382 return can_widen_reference_to (source_type, target_type);
386 int source_depth = class_depth (source_type);
387 int target_depth = class_depth (target_type);
389 /* class_depth can return a negative depth if an error occurred */
390 if (source_depth < 0 || target_depth < 0)
393 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
395 /* target_type is OK if source_type or source_type ancestors
396 implement target_type. We handle multiple sub-interfaces */
398 tree basetype_vec = TYPE_BINFO_BASETYPES (source_type);
399 int n = TREE_VEC_LENGTH (basetype_vec), i;
400 for (i=0 ; i < n; i++)
401 if (can_widen_reference_to
402 (TREE_TYPE (TREE_VEC_ELT (basetype_vec, i)),
409 for ( ; source_depth > target_depth; source_depth--)
411 source_type = TYPE_BINFO_BASETYPE (source_type, 0);
413 return source_type == target_type;
422 type = pop_type (type);
425 tree node = quick_stack;
426 quick_stack = TREE_CHAIN (quick_stack);
427 TREE_CHAIN (node) = tree_list_free_list;
428 tree_list_free_list = node;
429 node = TREE_VALUE (node);
433 return find_stack_slot (stack_pointer, promote_type (type));
437 /* Pop and discrad the top COUNT stack slots. */
440 java_stack_pop (count)
446 if (stack_pointer == 0)
447 fatal ("stack underflow");
448 type = stack_type_map[stack_pointer - 1];
449 if (type == TYPE_SECOND)
452 if (stack_pointer == 1 || count <= 0)
453 fatal ("stack underflow");
454 type = stack_type_map[stack_pointer - 2];
456 val = pop_value (type);
461 /* Implement the 'swap' operator (to swap two top stack slots). */
470 if (stack_pointer < 2
471 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
472 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
473 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
474 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
475 fatal ("bad stack swap");
477 flush_quick_stack ();
478 decl1 = find_stack_slot (stack_pointer - 1, type1);
479 decl2 = find_stack_slot (stack_pointer - 2, type2);
480 temp = copy_to_reg (DECL_RTL (decl1));
481 emit_move_insn (DECL_RTL (decl1), DECL_RTL (decl2));
482 emit_move_insn (DECL_RTL (decl2), temp);
483 stack_type_map[stack_pointer - 1] = type2;
484 stack_type_map[stack_pointer - 2] = type1;
488 java_stack_dup (size, offset)
491 int low_index = stack_pointer - size - offset;
494 error ("stack underflow - dup* operation");
496 flush_quick_stack ();
498 stack_pointer += size;
499 dst_index = stack_pointer;
501 for (dst_index = stack_pointer; --dst_index >= low_index; )
504 int src_index = dst_index - size;
505 if (src_index < low_index)
506 src_index = dst_index + size + offset;
507 type = stack_type_map [src_index];
508 if (type == TYPE_SECOND)
510 if (src_index <= low_index)
511 fatal ("dup operation splits 64-bit number");
512 stack_type_map[dst_index] = type;
513 src_index--; dst_index--;
514 type = stack_type_map[src_index];
515 if (! TYPE_IS_WIDE (type))
516 fatal ("internal error - dup operation");
518 else if (TYPE_IS_WIDE (type))
519 fatal ("internal error - dup operation");
520 if (src_index != dst_index)
522 tree src_decl = find_stack_slot (src_index, type);
523 tree dst_decl = find_stack_slot (dst_index, type);
524 emit_move_insn (DECL_RTL (dst_decl), DECL_RTL (src_decl));
525 stack_type_map[dst_index] = type;
530 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
534 build_java_athrow (node)
539 call = build (CALL_EXPR,
541 build_address_of (throw_node[exceptions_via_longjmp ? 1 : 0]),
542 build_tree_list (NULL_TREE, node),
544 TREE_SIDE_EFFECTS (call) = 1;
545 expand_expr_stmt (call);
546 java_stack_pop (stack_pointer);
549 /* Implementation for jsr/ret */
552 build_java_jsr (where, ret)
556 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
557 push_value (ret_label);
558 flush_quick_stack ();
564 build_java_ret (location)
567 expand_computed_goto (location);
570 /* Implementation of operations on array: new, load, store, length */
572 /* Array core info access macros */
574 #define JAVA_ARRAY_LENGTH_OFFSET(A) \
575 size_binop (CEIL_DIV_EXPR, \
577 (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (TREE_TYPE (A)))))), \
578 size_int (BITS_PER_UNIT))
581 decode_newarray_type (atype)
586 case 4: return boolean_type_node;
587 case 5: return char_type_node;
588 case 6: return float_type_node;
589 case 7: return double_type_node;
590 case 8: return byte_type_node;
591 case 9: return short_type_node;
592 case 10: return int_type_node;
593 case 11: return long_type_node;
594 default: return NULL_TREE;
598 /* Map primitive type to the code used by OPCODE_newarray. */
601 encode_newarray_type (type)
604 if (type == boolean_type_node)
606 else if (type == char_type_node)
608 else if (type == float_type_node)
610 else if (type == double_type_node)
612 else if (type == byte_type_node)
614 else if (type == short_type_node)
616 else if (type == int_type_node)
618 else if (type == long_type_node)
621 fatal ("Can't compute type code - patch_newarray");
624 /* Build a call to _Jv_ThrowBadArrayIndex(), the
625 ArrayIndexOfBoundsException exception handler. */
628 build_java_throw_out_of_bounds_exception (index)
631 tree node = build (CALL_EXPR, int_type_node,
632 build_address_of (soft_badarrayindex_node),
633 build_tree_list (NULL_TREE, index), NULL_TREE);
634 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
638 /* Return the length of an array. Doesn't perform any checking on the nature
639 or value of the array NODE. May be used to implement some bytecodes. */
642 build_java_array_length_access (node)
645 tree type = TREE_TYPE (node);
646 HOST_WIDE_INT length;
647 if (!is_array_type_p (type))
648 fatal ("array length on a non-array reference");
649 length = java_array_type_length (type);
651 return build_int_2 (length, 0);
652 return fold (build1 (INDIRECT_REF,
654 fold (build (PLUS_EXPR, ptr_type_node,
656 JAVA_ARRAY_LENGTH_OFFSET(node)))));
659 /* Optionally checks an array against the NULL pointer, eventually throwing a
660 NullPointerException. It could replace signal handling, but tied to NULL.
661 ARG1: the pointer to check, ARG2: the expression to use if
662 the pointer is non-null and ARG3 the type that should be returned. */
665 build_java_arraynull_check (node, expr, type)
666 tree node ATTRIBUTE_UNUSED;
668 tree type ATTRIBUTE_UNUSED;
671 static int java_array_access_throws_null_exception = 0;
673 if (java_array_access_throws_null_exception)
674 return (build (COND_EXPR,
676 build (EQ_EXPR, int_type_node, node, null_pointer_node),
677 build_java_athrow (node), expr ));
684 java_array_data_offset (array)
687 tree array_type = TREE_TYPE (TREE_TYPE (array));
688 tree data_fld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
689 if (data_fld == NULL_TREE)
690 return size_in_bytes (array_type);
692 return build_int_2 (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (data_fld))
696 /* Implement array indexing (either as l-value or r-value).
697 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
698 Optionally performs bounds checking and/or test to NULL.
699 At this point, ARRAY should have been verified as an array. */
702 build_java_arrayaccess (array, type, index)
703 tree array, type, index;
705 tree arith, node, throw = NULL_TREE;
707 arith = fold (build (PLUS_EXPR, int_type_node,
708 java_array_data_offset (array),
709 fold (build (MULT_EXPR, int_type_node,
710 index, size_in_bytes(type)))));
712 if (flag_bounds_check)
715 * (unsigned jint) INDEX >= (unsigned jint) LEN
716 * && throw ArrayIndexOutOfBoundsException.
717 * Note this is equivalent to and more efficient than:
718 * INDEX < 0 || INDEX >= LEN && throw ... */
720 tree len = build_java_array_length_access (array);
721 TREE_TYPE (len) = unsigned_int_type_node;
722 test = fold (build (GE_EXPR, boolean_type_node,
723 convert (unsigned_int_type_node, index),
725 if (! integer_zerop (test))
727 throw = build (TRUTH_ANDIF_EXPR, int_type_node, test,
728 build_java_throw_out_of_bounds_exception (index));
729 /* allows expansion within COMPOUND */
730 TREE_SIDE_EFFECTS( throw ) = 1;
734 node = build1 (INDIRECT_REF, type,
735 fold (build (PLUS_EXPR, ptr_type_node,
737 (throw ? build (COMPOUND_EXPR, int_type_node,
741 return (fold (build_java_arraynull_check (array, node, type)));
744 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
745 ARRAY_NODE. This function is used to retrieve something less vague than
746 a pointer type when indexing the first dimension of something like [[<t>.
747 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
749 As a side effect, it also makes sure that ARRAY_NODE is an array. */
752 build_java_check_indexed_type (array_node, indexed_type)
758 if (!is_array_type_p (TREE_TYPE (array_node)))
759 fatal ("array indexing on a non-array reference");
761 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
763 if (indexed_type == ptr_type_node )
764 return promote_type (elt_type);
766 /* BYTE/BOOLEAN store and load are used for both type */
767 if (indexed_type == byte_type_node && elt_type == boolean_type_node )
768 return boolean_type_node;
770 if (indexed_type != elt_type )
771 fatal ("type array element mismatch");
776 /* newarray triggers a call to _Jv_NewArray. This function should be called
777 with an integer code (the type of array to create) and get from the stack
778 the size of the dimmension. */
781 build_newarray (atype_value, length)
785 tree type = build_java_array_type (decode_newarray_type (atype_value),
786 TREE_CODE (length) == INTEGER_CST
787 ? TREE_INT_CST_LOW (length)
789 return build (CALL_EXPR, promote_type (type),
790 build_address_of (soft_newarray_node),
791 tree_cons (NULL_TREE,
792 build_int_2 (atype_value, 0),
793 build_tree_list (NULL_TREE, length)),
797 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
801 build_anewarray (class_type, length)
805 tree type = build_java_array_type (class_type,
806 TREE_CODE (length) == INTEGER_CST
807 ? TREE_INT_CST_LOW (length)
809 return build (CALL_EXPR, promote_type (type),
810 build_address_of (soft_anewarray_node),
811 tree_cons (NULL_TREE, length,
812 tree_cons (NULL_TREE, build_class_ref (class_type),
813 build_tree_list (NULL_TREE,
814 null_pointer_node))),
818 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
821 build_new_array (type, length)
825 if (JPRIMITIVE_TYPE_P (type))
826 return build_newarray (encode_newarray_type (type), length);
828 return build_anewarray (TREE_TYPE (type), length);
831 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
832 class pointer, a number of dimensions and the matching number of
833 dimensions. The argument list is NULL terminated. */
836 expand_java_multianewarray (class_type, ndim)
841 tree args = build_tree_list( NULL_TREE, null_pointer_node );
843 for( i = 0; i < ndim; i++ )
844 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
846 push_value (build (CALL_EXPR,
847 promote_type (class_type),
848 build_address_of (soft_multianewarray_node),
849 tree_cons (NULL_TREE, build_class_ref (class_type),
850 tree_cons (NULL_TREE,
851 build_int_2 (ndim, 0), args )),
855 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
856 ARRAY is an array type. May expand some bound checking and NULL
857 pointer checking. RHS_TYPE_NODE we are going to store. In the case
858 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
859 INT. In those cases, we make the convertion.
861 if ARRAy is a reference type, the assignment is checked at run-time
862 to make sure that the RHS can be assigned to the array element
863 type. It is not necessary to generate this code if ARRAY is final. */
866 expand_java_arraystore (rhs_type_node)
869 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
870 && TYPE_PRECISION (rhs_type_node) <= 32) ?
871 int_type_node : rhs_type_node);
872 tree index = pop_value (int_type_node);
873 tree array = pop_value (ptr_type_node);
875 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
877 flush_quick_stack ();
879 index = save_expr (index);
880 array = save_expr (array);
882 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
884 tree check = build (CALL_EXPR, void_type_node,
885 build_address_of (soft_checkarraystore_node),
886 tree_cons (NULL_TREE, array,
887 build_tree_list (NULL_TREE, rhs_node)),
889 TREE_SIDE_EFFECTS (check) = 1;
890 expand_expr_stmt (check);
893 expand_assignment (build_java_arrayaccess (array,
899 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
900 sure that LHS is an array type. May expand some bound checking and NULL
902 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
903 BOOLEAN/SHORT, we push a promoted type back to the stack.
907 expand_java_arrayload (lhs_type_node )
911 tree index_node = pop_value (int_type_node);
912 tree array_node = pop_value (ptr_type_node);
914 index_node = save_expr (index_node);
915 array_node = save_expr (array_node);
916 lhs_type_node = build_java_check_indexed_type (array_node, lhs_type_node);
918 load_node = build_java_arrayaccess (array_node,
922 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
923 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
924 push_value (load_node);
927 /* Expands .length. Makes sure that we deal with and array and may expand
928 a NULL check on the array object. */
931 expand_java_array_length ()
933 tree array = pop_value (ptr_type_node);
934 tree length = build_java_array_length_access (array);
936 push_value (build_java_arraynull_check (array, length, int_type_node));
939 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
940 either soft_monitorenter_node or soft_monitorexit_node. */
943 build_java_monitor (call, object)
947 return (build (CALL_EXPR,
949 build_address_of (call),
950 build_tree_list (NULL_TREE, object),
954 /* Emit code for one of the PUSHC instructions. */
957 expand_java_pushc (ival, type)
962 if (type == ptr_type_node && ival == 0)
963 value = null_pointer_node;
964 else if (type == int_type_node || type == long_type_node)
966 value = build_int_2 (ival, ival < 0 ? -1 : 0);
967 TREE_TYPE (value) = type;
969 else if (type == float_type_node || type == double_type_node)
972 #ifdef REAL_ARITHMETIC
973 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
977 value = build_real (type, x);
980 fatal ("internal error in expand_java_pushc");
985 expand_java_return (type)
988 if (type == void_type_node)
989 expand_null_return ();
992 tree retval = pop_value (type);
993 tree res = DECL_RESULT (current_function_decl);
994 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, retval);
995 TREE_SIDE_EFFECTS (retval) = 1;
996 expand_return (retval);
1001 build_address_of (value)
1004 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1008 expand_java_NEW (type)
1011 if (! CLASS_LOADED_P (type))
1012 load_class (type, 1);
1013 safe_layout_class (type);
1014 push_value (build (CALL_EXPR, promote_type (type),
1015 build_address_of (alloc_object_node),
1016 tree_cons (NULL_TREE, build_class_ref (type),
1017 build_tree_list (NULL_TREE,
1018 size_in_bytes (type))),
1023 expand_java_INSTANCEOF (type)
1026 tree value = pop_value (object_ptr_type_node);
1027 value = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (soft_instanceof_node)),
1028 build_address_of (soft_instanceof_node),
1029 tree_cons (NULL_TREE, value,
1030 build_tree_list (NULL_TREE,
1031 build_class_ref (type))),
1037 expand_java_CHECKCAST (type)
1040 tree value = pop_value (ptr_type_node);
1041 value = build (CALL_EXPR, promote_type (type),
1042 build_address_of (soft_checkcast_node),
1043 tree_cons (NULL_TREE, build_class_ref (type),
1044 build_tree_list (NULL_TREE, value)),
1050 expand_iinc (local_var_index, ival, pc)
1051 unsigned int local_var_index;
1055 tree local_var, res;
1056 tree constant_value;
1058 flush_quick_stack ();
1059 local_var = find_local_variable (local_var_index, int_type_node, pc);
1060 constant_value = build_int_2 (ival, ival < 0 ? -1 : 0);
1061 res = fold (build (PLUS_EXPR, int_type_node, local_var, constant_value));
1062 expand_assignment (local_var, res, 0, 0);
1067 build_java_soft_divmod (op, type, op1, op2)
1069 tree type, op1, op2;
1072 tree arg1 = convert (type, op1);
1073 tree arg2 = convert (type, op2);
1075 if (type == int_type_node)
1079 case TRUNC_DIV_EXPR:
1080 call = soft_idiv_node;
1082 case TRUNC_MOD_EXPR:
1083 call = soft_irem_node;
1089 else if (type == long_type_node)
1093 case TRUNC_DIV_EXPR:
1094 call = soft_ldiv_node;
1096 case TRUNC_MOD_EXPR:
1097 call = soft_lrem_node;
1105 fatal ("Internal compiler error in build_java_soft_divmod");
1107 call = build (CALL_EXPR, type,
1108 build_address_of (call),
1109 tree_cons (NULL_TREE, arg1,
1110 build_tree_list (NULL_TREE, arg2)),
1117 build_java_binop (op, type, arg1, arg2)
1119 tree type, arg1, arg2;
1126 tree u_type = unsigned_type (type);
1127 arg1 = convert (u_type, arg1);
1128 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1129 return convert (type, arg1);
1133 mask = build_int_2 (TYPE_PRECISION (TREE_TYPE (arg1)) - 1, 0);
1134 arg2 = fold (build (BIT_AND_EXPR, int_type_node, arg2, mask));
1137 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1138 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1139 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1141 tree ifexp1 = fold ( build (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1142 boolean_type_node, arg1, arg2));
1143 tree ifexp2 = fold ( build (EQ_EXPR, boolean_type_node, arg1, arg2));
1144 tree second_compare = fold (build (COND_EXPR, int_type_node,
1145 ifexp2, integer_zero_node,
1146 op == COMPARE_L_EXPR
1147 ? integer_negative_one_node
1148 : integer_one_node));
1149 return fold (build (COND_EXPR, int_type_node, ifexp1,
1150 op == COMPARE_L_EXPR ? integer_one_node
1151 : integer_negative_one_node,
1155 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1157 tree ifexp1 = fold ( build (LT_EXPR, boolean_type_node, arg1, arg2));
1158 tree ifexp2 = fold ( build (GT_EXPR, boolean_type_node, arg1, arg2));
1159 tree second_compare = fold ( build (COND_EXPR, int_type_node,
1160 ifexp2, integer_one_node,
1161 integer_zero_node));
1162 return fold (build (COND_EXPR, int_type_node,
1163 ifexp1, integer_negative_one_node, second_compare));
1165 case TRUNC_DIV_EXPR:
1166 case TRUNC_MOD_EXPR:
1167 if (TREE_CODE (type) == REAL_TYPE
1168 && op == TRUNC_MOD_EXPR)
1171 if (type != double_type_node)
1173 arg1 = convert (double_type_node, arg1);
1174 arg2 = convert (double_type_node, arg2);
1176 call = build (CALL_EXPR, double_type_node,
1177 build_address_of (soft_fmod_node),
1178 tree_cons (NULL_TREE, arg1,
1179 build_tree_list (NULL_TREE, arg2)),
1181 if (type != double_type_node)
1182 call = convert (type, call);
1186 if (TREE_CODE (type) == INTEGER_TYPE
1187 && flag_use_divide_subroutine
1188 && ! flag_syntax_only)
1189 return build_java_soft_divmod (op, type, arg1, arg2);
1194 return fold (build (op, type, arg1, arg2));
1198 expand_java_binop (type, op)
1199 tree type; enum tree_code op;
1209 rtype = int_type_node;
1210 rarg = pop_value (rtype);
1213 rarg = pop_value (rtype);
1215 larg = pop_value (ltype);
1216 push_value (build_java_binop (op, type, larg, rarg));
1219 /* Lookup the field named NAME in *TYPEP or its super classes.
1220 If not found, return NULL_TREE.
1221 (If the *TYPEP is not found, return error_mark_node.)
1222 If found, return the FIELD_DECL, and set *TYPEP to the
1223 class containing the field. */
1226 lookup_field (typep, name)
1230 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1232 load_class (*typep, 1);
1233 safe_layout_class (*typep);
1234 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1235 return error_mark_node;
1239 tree field, basetype_vec;
1242 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1243 if (DECL_NAME (field) == name)
1246 /* Process implemented interfaces. */
1247 basetype_vec = TYPE_BINFO_BASETYPES (*typep);
1248 n = TREE_VEC_LENGTH (basetype_vec);
1249 for (i = 0; i < n; i++)
1251 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
1252 if ((field = lookup_field (&t, name)))
1255 *typep = CLASSTYPE_SUPER (*typep);
1260 /* Look up the field named NAME in object SELF_VALUE,
1261 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1262 SELF_VALUE is NULL_TREE if looking for a static field. */
1265 build_field_ref (self_value, self_class, name)
1266 tree self_value, self_class, name;
1268 tree base_class = self_class;
1269 tree field_decl = lookup_field (&base_class, name);
1270 if (field_decl == NULL_TREE)
1272 error ("field `%s' not found", IDENTIFIER_POINTER (name));
1273 return error_mark_node;
1275 if (self_value == NULL_TREE)
1277 return build_static_field_ref (field_decl);
1281 tree base_handle_type = promote_type (base_class);
1282 if (base_handle_type != TREE_TYPE (self_value))
1283 self_value = fold (build1 (NOP_EXPR, base_handle_type, self_value));
1284 #ifdef JAVA_USE_HANDLES
1285 self_value = unhand_expr (self_value);
1287 self_value = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (self_value)),
1289 return fold (build (COMPONENT_REF, TREE_TYPE (field_decl),
1290 self_value, field_decl));
1300 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1301 name = get_identifier (buf);
1302 if (IDENTIFIER_LOCAL_VALUE (name))
1303 return IDENTIFIER_LOCAL_VALUE (name);
1306 /* The type of the address of a label is return_address_type_node. */
1307 tree decl = create_label_decl (name);
1308 LABEL_PC (decl) = pc;
1310 return pushdecl (decl);
1314 /* Generate a unique name for the purpose of loops and switches
1315 labels, and try-catch-finally blocks label or temporary variables. */
1320 static int l_number = 0;
1322 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1324 return get_identifier (buff);
1328 create_label_decl (name)
1332 push_obstacks (&permanent_obstack, &permanent_obstack);
1333 decl = build_decl (LABEL_DECL, name,
1334 TREE_TYPE (return_address_type_node));
1336 DECL_CONTEXT (decl) = current_function_decl;
1337 DECL_IGNORED_P (decl) = 1;
1341 /* This maps a bytecode offset (PC) to various flags. */
1342 char *instruction_bits;
1345 note_label (current_pc, target_pc)
1346 int current_pc ATTRIBUTE_UNUSED, target_pc;
1348 lookup_label (target_pc);
1349 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1352 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1353 where CONDITION is one of one the compare operators. */
1356 expand_compare (condition, value1, value2, target_pc)
1357 enum tree_code condition;
1358 tree value1, value2;
1361 tree target = lookup_label (target_pc);
1362 tree cond = fold (build (condition, boolean_type_node, value1, value2));
1363 expand_start_cond (truthvalue_conversion (cond), 0);
1364 expand_goto (target);
1368 /* Emit code for a TEST-type opcode. */
1371 expand_test (condition, type, target_pc)
1372 enum tree_code condition;
1376 tree value1, value2;
1377 flush_quick_stack ();
1378 value1 = pop_value (type);
1379 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1380 expand_compare (condition, value1, value2, target_pc);
1383 /* Emit code for a COND-type opcode. */
1386 expand_cond (condition, type, target_pc)
1387 enum tree_code condition;
1391 tree value1, value2;
1392 flush_quick_stack ();
1393 /* note: pop values in opposite order */
1394 value2 = pop_value (type);
1395 value1 = pop_value (type);
1396 /* Maybe should check value1 and value2 for type compatibility ??? */
1397 expand_compare (condition, value1, value2, target_pc);
1401 expand_java_goto (target_pc)
1404 tree target_label = lookup_label (target_pc);
1405 flush_quick_stack ();
1406 expand_goto (target_label);
1411 expand_java_call (target_pc, return_address)
1412 int target_pc, return_address;
1414 tree target_label = lookup_label (target_pc);
1415 tree value = build_int_2 (return_address, return_address < 0 ? -1 : 0);
1417 flush_quick_stack ();
1418 expand_goto (target_label);
1422 expand_java_ret (return_address)
1423 tree return_address ATTRIBUTE_UNUSED;
1425 warning ("ret instruction not implemented");
1427 tree target_label = lookup_label (target_pc);
1428 flush_quick_stack ();
1429 expand_goto (target_label);
1434 /* Recursive helper function to pop argument types during verifiation. */
1437 pop_argument_types (arg_types)
1440 if (arg_types == end_params_node)
1442 if (TREE_CODE (arg_types) == TREE_LIST)
1444 pop_argument_types (TREE_CHAIN (arg_types));
1445 pop_type (TREE_VALUE (arg_types));
1452 pop_arguments (arg_types)
1455 if (arg_types == end_params_node)
1457 if (TREE_CODE (arg_types) == TREE_LIST)
1459 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1460 tree type = TREE_VALUE (arg_types);
1461 tree arg = pop_value (type);
1462 if (PROMOTE_PROTOTYPES
1463 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1464 && INTEGRAL_TYPE_P (type))
1465 arg = convert (integer_type_node, arg);
1466 return tree_cons (NULL_TREE, arg, tail);
1471 /* Build an expression to initialize the class CLAS.
1472 if EXPR is non-NULL, returns an expression to first call the initializer
1473 (if it is needed) and then calls EXPR. */
1476 build_class_init (clas, expr)
1480 if (inherits_from_p (current_class, clas))
1482 init = build (CALL_EXPR, void_type_node,
1483 build_address_of (soft_initclass_node),
1484 build_tree_list (NULL_TREE, build_class_ref (clas)),
1486 TREE_SIDE_EFFECTS (init) = 1;
1487 if (expr != NULL_TREE)
1489 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1490 TREE_SIDE_EFFECTS (expr) = 1;
1496 static tree methods_ident = NULL_TREE;
1497 static tree ncode_ident = NULL_TREE;
1498 tree dtable_ident = NULL_TREE;
1501 build_known_method_ref (method, method_type, self_type, method_signature, arg_list)
1502 tree method, method_type ATTRIBUTE_UNUSED, self_type,
1503 method_signature ATTRIBUTE_UNUSED, arg_list ATTRIBUTE_UNUSED;
1506 if (is_compiled_class (self_type))
1508 make_decl_rtl (method, NULL, 1);
1509 func = build1 (ADDR_EXPR, method_ptr_type_node, method);
1513 /* We don't know whether the method has been (statically) compiled.
1514 Compile this code to get a reference to the method's code:
1516 SELF_TYPE->methods[METHOD_INDEX].ncode
1518 This is guaranteed to work (assuming SELF_TYPE has
1519 been initialized), since if the method is not compiled yet,
1520 its ncode points to a trampoline that forces compilation. */
1522 int method_index = 0;
1524 tree ref = build_class_ref (self_type);
1525 ref = build1 (INDIRECT_REF, class_type_node, ref);
1526 if (ncode_ident == NULL_TREE)
1527 ncode_ident = get_identifier ("ncode");
1528 if (methods_ident == NULL_TREE)
1529 methods_ident = get_identifier ("methods");
1530 ref = build (COMPONENT_REF, method_ptr_type_node, ref,
1531 lookup_field (&class_type_node, methods_ident));
1532 for (meth = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (self_type));
1533 ; meth = TREE_CHAIN (meth))
1537 if (meth == NULL_TREE)
1538 fatal ("method '%s' not found in class",
1539 IDENTIFIER_POINTER (DECL_NAME (method)));
1542 method_index *= int_size_in_bytes (method_type_node);
1543 ref = fold (build (PLUS_EXPR, method_ptr_type_node,
1544 ref, build_int_2 (method_index, 0)));
1545 ref = build1 (INDIRECT_REF, method_type_node, ref);
1546 func = build (COMPONENT_REF, nativecode_ptr_type_node,
1548 lookup_field (&method_type_node, ncode_ident));
1554 invoke_build_dtable (is_invoke_interface, arg_list)
1555 int is_invoke_interface;
1558 tree dtable, objectref;
1560 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
1562 /* If we're dealing with interfaces and if the objectref
1563 argument is an array then get the dispatch table of the class
1564 Object rather than the one from the objectref. */
1565 objectref = (is_invoke_interface
1566 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list))) ?
1567 object_type_node : TREE_VALUE (arg_list));
1569 if (dtable_ident == NULL_TREE)
1570 dtable_ident = get_identifier ("vtable");
1571 dtable = build1 (INDIRECT_REF, object_type_node, objectref );
1572 dtable = build (COMPONENT_REF, dtable_ptr_type, dtable,
1573 lookup_field (&object_type_node, dtable_ident));
1579 build_invokevirtual (dtable, method)
1580 tree dtable, method;
1583 tree nativecode_ptr_ptr_type_node
1584 = build_pointer_type (nativecode_ptr_type_node);
1585 int method_index = TREE_INT_CST_LOW (DECL_VINDEX (method));
1586 /* Add one to skip "class" field of dtable, and one to skip unused
1587 vtable entry (for C++ compatibility). */
1590 *= int_size_in_bytes (nativecode_ptr_ptr_type_node);
1591 func = fold (build (PLUS_EXPR, nativecode_ptr_ptr_type_node,
1592 dtable, build_int_2 (method_index, 0)));
1593 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
1599 build_invokeinterface (dtable, method_name, method_signature)
1600 tree dtable, method_name, method_signature;
1602 static tree class_ident = NULL_TREE;
1605 /* We expand invokeinterface here. _Jv_LookupInterfaceMethod() will
1606 ensure that the selected method exists, is public and not
1607 abstract nor static. */
1609 if (class_ident == NULL_TREE)
1610 class_ident = get_identifier ("class");
1612 dtable = build1 (INDIRECT_REF, dtable_type, dtable);
1613 dtable = build (COMPONENT_REF, class_ptr_type, dtable,
1614 lookup_field (&dtable_type, class_ident));
1615 lookup_arg = build_tree_list (NULL_TREE,
1618 (IDENTIFIER_POINTER(method_signature),
1619 IDENTIFIER_LENGTH(method_signature)))));
1620 lookup_arg = tree_cons (NULL_TREE, dtable,
1621 tree_cons (NULL_TREE, build_utf8_ref (method_name),
1623 return build (CALL_EXPR, ptr_type_node,
1624 build_address_of (soft_lookupinterfacemethod_node),
1625 lookup_arg, NULL_TREE);
1628 /* Expand one of the invoke_* opcodes.
1629 OCPODE is the specific opcode.
1630 METHOD_REF_INDEX is an index into the constant pool.
1631 NARGS is the number of arguments, or -1 if not specified. */
1634 expand_invoke (opcode, method_ref_index, nargs)
1636 int method_ref_index;
1637 int nargs ATTRIBUTE_UNUSED;
1639 tree method_signature = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
1640 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool, method_ref_index);
1641 tree self_type = get_class_constant
1642 (current_jcf, COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool, method_ref_index));
1643 const char *self_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
1644 tree call, func, method, arg_list, method_type;
1646 if (! CLASS_LOADED_P (self_type))
1648 load_class (self_type, 1);
1649 safe_layout_class (self_type);
1650 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
1651 fatal ("failed to find class '%s'", self_name);
1653 layout_class_methods (self_type);
1655 if (method_name == init_identifier_node)
1656 method = lookup_java_constructor (CLASS_TO_HANDLE_TYPE (self_type),
1659 method = lookup_java_method (CLASS_TO_HANDLE_TYPE (self_type),
1660 method_name, method_signature);
1661 if (method == NULL_TREE)
1663 error ("Class '%s' has no method named '%s' matching signature '%s'",
1665 IDENTIFIER_POINTER (method_name),
1666 IDENTIFIER_POINTER (method_signature));
1668 /* Invoke static can't invoke static/abstract method */
1669 else if (opcode == OPCODE_invokestatic)
1671 if (!METHOD_STATIC (method))
1673 error ("invokestatic on non static method");
1676 else if (METHOD_ABSTRACT (method))
1678 error ("invokestatic on abstract method");
1684 if (METHOD_STATIC (method))
1686 error ("invoke[non-static] on static method");
1691 if (method == NULL_TREE)
1693 method_type = get_type_from_signature (method_signature);
1694 pop_arguments (TYPE_ARG_TYPES (method_type));
1695 if (opcode != OPCODE_invokestatic)
1696 pop_type (self_type);
1697 method_type = promote_type (TREE_TYPE (method_type));
1698 push_value (convert (method_type, integer_zero_node));
1702 method_type = TREE_TYPE (method);
1703 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
1704 flush_quick_stack ();
1707 if (opcode == OPCODE_invokestatic || opcode == OPCODE_invokespecial
1708 || (opcode == OPCODE_invokevirtual
1709 && (METHOD_PRIVATE (method)
1710 || METHOD_FINAL (method)
1711 || CLASS_FINAL (TYPE_NAME (self_type)))))
1712 func = build_known_method_ref (method, method_type, self_type,
1713 method_signature, arg_list);
1716 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
1718 if (opcode == OPCODE_invokevirtual)
1719 func = build_invokevirtual (dtable, method);
1721 func = build_invokeinterface (dtable, method_name, method_signature);
1723 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
1724 call = build (CALL_EXPR, TREE_TYPE (method_type), func, arg_list, NULL_TREE);
1725 TREE_SIDE_EFFECTS (call) = 1;
1727 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
1728 expand_expr_stmt (call);
1732 flush_quick_stack ();
1737 /* Expand an operation to extract from or store into a field.
1738 IS_STATIC is 1 iff the field is static.
1739 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
1740 FIELD_REF_INDEX is an index into the constant pool. */
1743 expand_java_field_op (is_static, is_putting, field_ref_index)
1746 int field_ref_index;
1749 get_class_constant (current_jcf,
1750 COMPONENT_REF_CLASS_INDEX (¤t_jcf->cpool,
1752 const char *self_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
1753 tree field_name = COMPONENT_REF_NAME (¤t_jcf->cpool, field_ref_index);
1754 tree field_signature = COMPONENT_REF_SIGNATURE (¤t_jcf->cpool,
1756 tree field_type = get_type_from_signature (field_signature);
1757 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
1760 tree field_decl = lookup_field (&self_type, field_name);
1761 if (field_decl == error_mark_node)
1765 else if (field_decl == NULL_TREE)
1767 error ("Missing field '%s' in '%s'",
1768 IDENTIFIER_POINTER (field_name), self_name);
1771 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
1773 error ("Mismatching signature for field '%s' in '%s'",
1774 IDENTIFIER_POINTER (field_name), self_name);
1777 field_ref = is_static ? NULL_TREE : pop_value (self_type);
1781 push_value (convert (field_type, integer_zero_node));
1782 flush_quick_stack ();
1786 /* Inline references to java.lang.PRIMTYPE.TYPE.
1787 In addition to being a useful (minor) optimization,
1788 this is also needed to avoid circularities in the implementation
1789 of these fields in libjava. */
1790 if (field_name == TYPE_identifier_node && ! is_putting
1791 && ! flag_emit_class_files && field_type == class_ptr_type
1792 && strncmp (self_name, "java.lang.", 10) == 0)
1794 tree typ = build_primtype_type_ref (self_name);
1802 field_ref = build_field_ref (field_ref, self_type, field_name);
1804 field_ref = build_class_init (self_type, field_ref);
1807 flush_quick_stack ();
1808 if (FIELD_FINAL (field_decl))
1810 if (DECL_CONTEXT (field_decl) != current_class)
1811 error_with_decl (field_decl,
1812 "assignment to final field `%s' not in field's class");
1813 else if (FIELD_STATIC (field_decl))
1815 if (!IS_CLINIT (current_function_decl))
1816 error_with_decl (field_decl,
1817 "assignment to final static field `%s' not in class initializer");
1821 tree cfndecl_name = DECL_NAME (current_function_decl);
1822 if (! DECL_CONSTRUCTOR_P (current_function_decl)
1823 && (cfndecl_name != finit_identifier_node))
1824 error_with_decl (field_decl, "assignment to final field `%s' not in constructor");
1827 expand_assignment (field_ref, new_value, 0, 0);
1830 push_value (field_ref);
1834 build_primtype_type_ref (self_name)
1835 const char *self_name;
1837 const char *class_name = self_name+10;
1839 if (strncmp(class_name, "Byte", 4) == 0)
1840 typ = byte_type_node;
1841 else if (strncmp(class_name, "Short", 5) == 0)
1842 typ = short_type_node;
1843 else if (strncmp(class_name, "Integer", 7) == 0)
1844 typ = int_type_node;
1845 else if (strncmp(class_name, "Long", 4) == 0)
1846 typ = long_type_node;
1847 else if (strncmp(class_name, "Float", 5) == 0)
1848 typ = float_type_node;
1849 else if (strncmp(class_name, "Double", 6) == 0)
1850 typ = double_type_node;
1851 else if (strncmp(class_name, "Boolean", 7) == 0)
1852 typ = boolean_type_node;
1853 else if (strncmp(class_name, "Char", 4) == 0)
1854 typ = char_type_node;
1855 else if (strncmp(class_name, "Void", 4) == 0)
1856 typ = void_type_node;
1859 if (typ != NULL_TREE)
1860 return build_class_ref (typ);
1866 load_type_state (label)
1870 tree vec = LABEL_TYPE_STATE (label);
1871 int cur_length = TREE_VEC_LENGTH (vec);
1872 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
1873 for (i = 0; i < cur_length; i++)
1874 type_map [i] = TREE_VEC_ELT (vec, i);
1877 /* Do the expansion of a Java switch. With Gcc, switches are front-end
1878 dependant things, but they rely on gcc routines. This function is
1879 placed here because it uses things defined locally in parse.y. */
1882 case_identity (t, v)
1883 tree t __attribute__ ((__unused__));
1890 java_lang_expand_expr (exp, target, tmode, modifier)
1892 rtx target ATTRIBUTE_UNUSED;
1893 enum machine_mode tmode ATTRIBUTE_UNUSED;
1894 enum expand_modifier modifier ATTRIBUTE_UNUSED;
1898 switch (TREE_CODE (exp))
1900 case NEW_ARRAY_INIT:
1903 tree array_type = TREE_TYPE (TREE_TYPE (exp));
1904 tree element_type = TYPE_ARRAY_ELEMENT (array_type);
1905 tree data_fld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
1906 HOST_WIDE_INT ilength = java_array_type_length (array_type);
1907 tree length = build_int_2 (ilength, 0);
1908 tree init = TREE_OPERAND (exp, 0);
1911 /* Enable this once we can set the vtable field statically. FIXME */
1912 if (TREE_CONSTANT (init) && TREE_STATIC (exp)
1913 && JPRIMITIVE_TYPE_P (element_type))
1915 tree temp, value, init_decl;
1916 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1917 PUSH_FIELD_VALUE (temp, "vtable",
1918 null_pointer_node /* FIXME */
1920 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1921 FINISH_RECORD_CONSTRUCTOR (temp);
1922 START_RECORD_CONSTRUCTOR (value, array_type);
1923 PUSH_SUPER_VALUE (value, temp);
1924 PUSH_FIELD_VALUE (value, "length", length);
1925 PUSH_FIELD_VALUE (value, "data", init);
1926 FINISH_RECORD_CONSTRUCTOR (value);
1928 init_decl = build_decl (VAR_DECL, generate_name (), array_type);
1929 pushdecl_top_level (init_decl);
1930 TREE_STATIC (init_decl) = 1;
1931 DECL_INITIAL (init_decl) = value;
1932 DECL_IGNORED_P (init_decl) = 1;
1933 TREE_READONLY (init_decl) = 1;
1934 make_decl_rtl (init_decl, NULL, 1);
1935 init = build1 (ADDR_EXPR, TREE_TYPE (exp), init_decl);
1936 return expand_expr (init, target, tmode, modifier);
1939 array_decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (exp));
1940 expand_decl (array_decl);
1941 tmp = expand_assignment (array_decl,
1942 build_new_array (element_type, length),
1944 if (TREE_CONSTANT (init)
1945 && ilength >= 10 && JPRIMITIVE_TYPE_P (element_type))
1948 push_obstacks (&permanent_obstack, &permanent_obstack);
1949 init_decl = build_decl (VAR_DECL, generate_name (),
1951 pushdecl_top_level (init_decl);
1952 TREE_STATIC (init_decl) = 1;
1953 DECL_INITIAL (init_decl) = init;
1954 DECL_IGNORED_P (init_decl) = 1;
1955 TREE_READONLY (init_decl) = 1;
1956 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl)) = 1;
1957 make_decl_rtl (init_decl, NULL, 1);
1961 expand_assignment (build (COMPONENT_REF, TREE_TYPE (data_fld),
1962 build1 (INDIRECT_REF, array_type, array_decl),
1968 if (BLOCK_EXPR_BODY (exp))
1971 tree body = BLOCK_EXPR_BODY (exp);
1972 pushlevel (2); /* 2 and above */
1973 expand_start_bindings (0);
1974 local = BLOCK_EXPR_DECLS (exp);
1977 tree next = TREE_CHAIN (local);
1978 layout_decl (local, 0);
1979 expand_decl (pushdecl (local));
1982 /* Avoid deep recursion for long block. */
1983 while (TREE_CODE (body) == COMPOUND_EXPR)
1985 expand_expr (TREE_OPERAND (body, 0), const0_rtx, VOIDmode, 0);
1987 body = TREE_OPERAND (body, 1);
1989 expand_expr (body, const0_rtx, VOIDmode, 0);
1992 expand_end_bindings (getdecls (), 1, 0);
2000 if (pushcase (TREE_OPERAND (exp, 0), case_identity,
2001 build_decl (LABEL_DECL, NULL_TREE, NULL_TREE),
2004 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (exp);
2006 (wfl_operator, "Duplicate case label: `%s'",
2007 print_int_node (TREE_OPERAND (exp, 0)));
2013 pushcase (NULL_TREE, 0,
2014 build_decl (LABEL_DECL, NULL_TREE, NULL_TREE), NULL);
2018 expand_start_case (0, TREE_OPERAND (exp, 0), int_type_node, "switch");
2019 expand_expr_stmt (TREE_OPERAND (exp, 1));
2020 expand_end_case (TREE_OPERAND (exp, 0));
2024 /* We expand a try[-catch] block */
2026 /* Expand the try block */
2027 push_obstacks (&permanent_obstack, &permanent_obstack);
2028 expand_eh_region_start ();
2030 expand_expr_stmt (TREE_OPERAND (exp, 0));
2031 push_obstacks (&permanent_obstack, &permanent_obstack);
2032 expand_start_all_catch ();
2035 /* Expand all catch clauses (EH handlers) */
2036 for (current = TREE_OPERAND (exp, 1); current;
2037 current = TREE_CHAIN (current))
2040 tree catch = TREE_OPERAND (current, 0);
2041 tree decl = BLOCK_EXPR_DECLS (catch);
2042 type = (decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE);
2043 start_catch_handler (prepare_eh_table_type (type));
2044 expand_expr_stmt (TREE_OPERAND (current, 0));
2046 expand_resume_after_catch ();
2047 end_catch_handler ();
2049 expand_end_all_catch ();
2053 fatal ("Can't expand '%s' tree - java_lang_expand_expr",
2054 tree_code_name [TREE_CODE (exp)]);
2059 expand_byte_code (jcf, method)
2066 const unsigned char *linenumber_pointer;
2067 int dead_code_index = -1;
2069 #undef RET /* Defined by config/i386/i386.h */
2070 #undef AND /* Causes problems with opcodes for iand and land. */
2072 #define BCODE byte_ops
2073 #define BYTE_type_node byte_type_node
2074 #define SHORT_type_node short_type_node
2075 #define INT_type_node int_type_node
2076 #define LONG_type_node long_type_node
2077 #define CHAR_type_node char_type_node
2078 #define PTR_type_node ptr_type_node
2079 #define FLOAT_type_node float_type_node
2080 #define DOUBLE_type_node double_type_node
2081 #define VOID_type_node void_type_node
2083 unsigned char* byte_ops;
2084 long length = DECL_CODE_LENGTH (method);
2087 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2088 byte_ops = jcf->read_ptr;
2090 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2091 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2092 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2093 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2095 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2097 instruction_bits = oballoc (length + 1);
2098 bzero (instruction_bits, length + 1);
2100 /* We make an initial pass of the line number table, to note
2101 which instructions have associated line number entries. */
2102 linenumber_pointer = linenumber_table;
2103 for (i = 0; i < linenumber_count; i++)
2105 int pc = GET_u2 (linenumber_pointer);
2106 linenumber_pointer += 4;
2108 warning ("invalid PC in line number table");
2111 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2112 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2113 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2117 /* Do a preliminary pass.
2118 * This figures out which PC can be the targets of jumps. */
2119 for (PC = 0; PC < length;)
2121 int oldpc = PC; /* PC at instruction start. */
2122 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2123 switch (byte_ops[PC++])
2125 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2127 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2130 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2132 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2133 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2134 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2135 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2136 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2137 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2138 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2139 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2141 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2142 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2143 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2144 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2145 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2146 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2147 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2148 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2150 /* two forms of wide instructions */
2151 #define PRE_SPECIAL_WIDE(IGNORE) \
2153 int modified_opcode = IMMEDIATE_u1; \
2154 if (modified_opcode == OPCODE_iinc) \
2156 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2157 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2161 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2165 /* nothing */ /* XXX JH */
2167 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2169 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2171 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2172 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2173 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2174 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2175 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2176 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2177 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2178 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2179 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2180 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2182 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2183 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2184 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2185 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2186 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2187 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2188 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2189 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2191 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2193 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2194 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2196 #define PRE_LOOKUP_SWITCH \
2197 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2198 NOTE_LABEL (default_offset+oldpc); \
2200 while (--npairs >= 0) { \
2201 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2202 jint offset = IMMEDIATE_s4; \
2203 NOTE_LABEL (offset+oldpc); } \
2206 #define PRE_TABLE_SWITCH \
2207 { jint default_offset = IMMEDIATE_s4; \
2208 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2209 NOTE_LABEL (default_offset+oldpc); \
2211 while (low++ <= high) { \
2212 jint offset = IMMEDIATE_s4; \
2213 NOTE_LABEL (offset+oldpc); } \
2216 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2217 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2218 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2219 (void)(IMMEDIATE_u2); \
2220 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2222 #include "javaop.def"
2227 if (! verify_jvm_instructions (jcf, byte_ops, length))
2230 /* Translate bytecodes to rtl instructions. */
2231 linenumber_pointer = linenumber_table;
2232 for (PC = 0; PC < length;)
2234 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2236 tree label = lookup_label (PC);
2237 flush_quick_stack ();
2238 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2239 expand_label (label);
2240 if (LABEL_VERIFIED (label) || PC == 0)
2241 load_type_state (label);
2244 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2246 if (dead_code_index == -1)
2248 /* This is the start of a region of unreachable bytecodes.
2249 They still need to be processed in order for EH ranges
2250 to get handled correctly. However, we can simply
2251 replace these bytecodes with nops. */
2252 dead_code_index = PC;
2255 /* Turn this bytecode into a nop. */
2260 if (dead_code_index != -1)
2262 /* We've just reached the end of a region of dead code. */
2263 warning ("Unreachable bytecode from %d to before %d.",
2264 dead_code_index, PC);
2265 dead_code_index = -1;
2269 /* Handle possible line number entry for this PC.
2271 This code handles out-of-order and multiple linenumbers per PC,
2272 but is optimized for the case of line numbers increasing
2273 monotonically with PC. */
2274 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2276 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2277 || GET_u2 (linenumber_pointer) != PC)
2278 linenumber_pointer = linenumber_table;
2279 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2281 int pc = GET_u2 (linenumber_pointer);
2282 linenumber_pointer += 4;
2285 lineno = GET_u2 (linenumber_pointer - 2);
2286 emit_line_note (input_filename, lineno);
2287 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2292 maybe_start_try (PC);
2293 maybe_pushlevels (PC);
2295 PC = process_jvm_instruction (PC, byte_ops, length);
2297 maybe_poplevels (PC);
2301 if (dead_code_index != -1)
2303 /* We've just reached the end of a region of dead code. */
2304 warning ("Unreachable bytecode from %d to the end of the method.",
2310 java_push_constant_from_pool (jcf, index)
2315 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
2318 push_obstacks (&permanent_obstack, &permanent_obstack);
2319 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
2320 index = alloc_name_constant (CONSTANT_String, name);
2321 c = build_ref_from_constant_pool (index);
2322 TREE_TYPE (c) = promote_type (string_type_node);
2326 c = get_constant (jcf, index);
2331 process_jvm_instruction (PC, byte_ops, length)
2333 const unsigned char* byte_ops;
2334 long length ATTRIBUTE_UNUSED;
2336 const char *opname; /* Temporary ??? */
2337 int oldpc = PC; /* PC at instruction start. */
2339 /* If the instruction is at the beginning of a exception handler,
2340 replace the top of the stack with the thrown object reference */
2341 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
2343 tree type = pop_type (ptr_type_node);
2344 push_value (build1 (NOP_EXPR, type, soft_exceptioninfo_call_node));
2347 switch (byte_ops[PC++])
2349 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2352 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2355 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
2357 int saw_index = 0; \
2358 int index = OPERAND_VALUE; \
2359 build_java_ret (find_local_variable (index, ptr_type_node, oldpc)); \
2362 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
2364 tree where = lookup_label (oldpc+OPERAND_VALUE); \
2365 tree ret = lookup_label (PC); \
2366 build_java_jsr (where, ret); \
2367 load_type_state (ret); \
2370 /* Push a constant onto the stack. */
2371 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
2372 { int saw_index = 0; int ival = (OPERAND_VALUE); \
2373 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
2374 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
2376 /* internal macro added for use by the WIDE case */
2377 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
2378 push_value (find_local_variable (OPVALUE, type_map[OPVALUE], oldpc));
2380 /* Push local variable onto the opcode stack. */
2381 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
2383 /* have to do this since OPERAND_VALUE may have side-effects */ \
2384 int opvalue = OPERAND_VALUE; \
2385 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2388 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
2389 expand_java_return (OPERAND_TYPE##_type_node)
2391 #define REM_EXPR TRUNC_MOD_EXPR
2392 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
2393 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
2395 #define FIELD(IS_STATIC, IS_PUT) \
2396 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
2398 #define TEST(OPERAND_TYPE, CONDITION) \
2399 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2401 #define COND(OPERAND_TYPE, CONDITION) \
2402 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2404 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2405 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
2407 #define BRANCH_GOTO(OPERAND_VALUE) \
2408 expand_java_goto (oldpc + OPERAND_VALUE)
2410 #define BRANCH_CALL(OPERAND_VALUE) \
2411 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
2414 #define BRANCH_RETURN(OPERAND_VALUE) \
2416 tree type = OPERAND_TYPE##_type_node; \
2417 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
2418 expand_java_ret (value); \
2422 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
2423 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2424 fprintf (stderr, "(not implemented)\n")
2425 #define NOT_IMPL1(OPERAND_VALUE) \
2426 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2427 fprintf (stderr, "(not implemented)\n")
2429 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
2431 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
2433 #define STACK_POP(COUNT) java_stack_pop (COUNT)
2435 #define STACK_SWAP(COUNT) java_stack_swap()
2437 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
2438 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
2439 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
2441 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2442 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
2444 #define LOOKUP_SWITCH \
2445 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2446 tree selector = pop_value (INT_type_node); \
2447 tree duplicate, label; \
2448 tree type = TREE_TYPE (selector); \
2449 flush_quick_stack (); \
2450 expand_start_case (0, selector, type, "switch statement");\
2451 push_momentary (); \
2452 while (--npairs >= 0) \
2454 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
2455 tree value = build_int_2 (match, match < 0 ? -1 : 0); \
2456 TREE_TYPE (value) = type; \
2457 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2458 pushcase (value, convert, label, &duplicate); \
2459 expand_java_goto (oldpc + offset); \
2461 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2462 pushcase (NULL_TREE, 0, label, &duplicate); \
2463 expand_java_goto (oldpc + default_offset); \
2465 expand_end_case (selector); \
2468 #define TABLE_SWITCH \
2469 { jint default_offset = IMMEDIATE_s4; \
2470 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2471 tree selector = pop_value (INT_type_node); \
2472 tree duplicate, label; \
2473 tree type = TREE_TYPE (selector); \
2474 flush_quick_stack (); \
2475 expand_start_case (0, selector, type, "switch statement");\
2476 push_momentary (); \
2477 for (; low <= high; low++) \
2479 jint offset = IMMEDIATE_s4; \
2480 tree value = build_int_2 (low, low < 0 ? -1 : 0); \
2481 TREE_TYPE (value) = type; \
2482 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2483 pushcase (value, convert, label, &duplicate); \
2484 expand_java_goto (oldpc + offset); \
2486 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2487 pushcase (NULL_TREE, 0, label, &duplicate); \
2488 expand_java_goto (oldpc + default_offset); \
2490 expand_end_case (selector); \
2493 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2494 { int opcode = byte_ops[PC-1]; \
2495 int method_ref_index = IMMEDIATE_u2; \
2497 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
2499 expand_invoke (opcode, method_ref_index, nargs); \
2502 /* Handle new, checkcast, instanceof */
2503 #define OBJECT(TYPE, OP) \
2504 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
2506 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
2508 #define ARRAY_LOAD(OPERAND_TYPE) \
2510 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
2513 #define ARRAY_STORE(OPERAND_TYPE) \
2515 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
2518 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
2519 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
2520 #define ARRAY_NEW_PTR() \
2521 push_value (build_anewarray (get_class_constant (current_jcf, \
2523 pop_value (int_type_node)));
2524 #define ARRAY_NEW_NUM() \
2526 int atype = IMMEDIATE_u1; \
2527 push_value (build_newarray (atype, pop_value (int_type_node)));\
2529 #define ARRAY_NEW_MULTI() \
2531 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
2532 int ndims = IMMEDIATE_u1; \
2533 expand_java_multianewarray( class, ndims ); \
2536 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
2537 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
2538 pop_value (OPERAND_TYPE##_type_node))));
2540 #define CONVERT2(FROM_TYPE, TO_TYPE) \
2542 push_value (build1 (NOP_EXPR, int_type_node, \
2543 (convert (TO_TYPE##_type_node, \
2544 pop_value (FROM_TYPE##_type_node))))); \
2547 #define CONVERT(FROM_TYPE, TO_TYPE) \
2549 push_value (convert (TO_TYPE##_type_node, \
2550 pop_value (FROM_TYPE##_type_node))); \
2553 /* internal macro added for use by the WIDE case
2554 Added TREE_TYPE (decl) assignment, apbianco */
2555 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
2558 int var = OPVALUE; \
2559 tree type = OPTYPE; \
2560 value = pop_value (type); \
2561 type = TREE_TYPE (value); \
2562 decl = find_local_variable (var, type, oldpc); \
2563 set_local_type (var, type ); \
2564 expand_assignment (decl, value, 0, 0); \
2567 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
2569 /* have to do this since OPERAND_VALUE may have side-effects */ \
2570 int opvalue = OPERAND_VALUE; \
2571 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2574 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2575 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2577 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
2578 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
2580 #define MONITOR_OPERATION(call) \
2582 tree o = pop_value (ptr_type_node); \
2584 flush_quick_stack (); \
2585 c = build_java_monitor (call, o); \
2586 TREE_SIDE_EFFECTS (c) = 1; \
2587 expand_expr_stmt (c); \
2590 #define SPECIAL_IINC(IGNORED) \
2592 unsigned int local_var_index = IMMEDIATE_u1; \
2593 int ival = IMMEDIATE_s1; \
2594 expand_iinc(local_var_index, ival, oldpc); \
2597 #define SPECIAL_WIDE(IGNORED) \
2599 int modified_opcode = IMMEDIATE_u1; \
2600 unsigned int local_var_index = IMMEDIATE_u2; \
2601 switch (modified_opcode) \
2605 int ival = IMMEDIATE_s2; \
2606 expand_iinc (local_var_index, ival, oldpc); \
2609 case OPCODE_iload: \
2610 case OPCODE_lload: \
2611 case OPCODE_fload: \
2612 case OPCODE_dload: \
2613 case OPCODE_aload: \
2615 /* duplicate code from LOAD macro */ \
2616 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
2619 case OPCODE_istore: \
2620 case OPCODE_lstore: \
2621 case OPCODE_fstore: \
2622 case OPCODE_dstore: \
2623 case OPCODE_astore: \
2625 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
2629 error ("unrecogized wide sub-instruction"); \
2633 #define SPECIAL_THROW(IGNORED) \
2634 build_java_athrow (pop_value (throwable_type_node))
2636 #define SPECIAL_BREAK NOT_IMPL1
2637 #define IMPL NOT_IMPL
2639 #include "javaop.def"
2642 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
2647 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
2648 order, as specified by Java Language Specification.
2650 The problem is that while expand_expr will evaluate its sub-operands in
2651 left-to-right order, for variables it will just return an rtx (i.e.
2652 an lvalue) for the variable (rather than an rvalue). So it is possible
2653 that a later sub-operand will change the register, and when the
2654 actual operation is done, it will use the new value, when it should
2655 have used the original value.
2657 We fix this by using save_expr. This forces the sub-operand to be
2658 copied into a fresh virtual register,
2660 For method invocation, we modify the arguments so that a
2661 left-to-right order evaluation is performed. Saved expressions
2662 will, in CALL_EXPR order, be reused when the call will be expanded.
2666 force_evaluation_order (node)
2669 if (flag_syntax_only)
2671 if (TREE_CODE_CLASS (TREE_CODE (node)) == '2')
2673 if (TREE_SIDE_EFFECTS (TREE_OPERAND (node, 1)))
2674 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
2676 else if (TREE_CODE (node) == CALL_EXPR || TREE_CODE (node) == NEW_CLASS_EXPR)
2680 if (!TREE_OPERAND (node, 1))
2683 /* This reverses the evaluation order. This is a desired effect. */
2684 for (cmp = NULL_TREE, arg = TREE_OPERAND (node, 1);
2685 arg; arg = TREE_CHAIN (arg))
2687 tree saved = save_expr (TREE_VALUE (arg));
2688 cmp = (cmp == NULL_TREE ? saved :
2689 build (COMPOUND_EXPR, void_type_node, cmp, saved));
2690 TREE_VALUE (arg) = saved;
2693 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
2694 TREE_SIDE_EFFECTS (cmp) = 1;
2698 cmp = save_expr (build (COMPOUND_EXPR, TREE_TYPE (node), cmp, node));
2699 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
2700 TREE_SIDE_EFFECTS (cmp) = 1;