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"
45 static void flush_quick_stack PARAMS ((void));
46 static void push_value PARAMS ((tree));
47 static tree pop_value PARAMS ((tree));
48 static void java_stack_swap PARAMS ((void));
49 static void java_stack_dup PARAMS ((int, int));
50 static void build_java_athrow PARAMS ((tree));
51 static void build_java_jsr PARAMS ((tree, tree));
52 static void build_java_ret PARAMS ((tree));
53 static void expand_java_multianewarray PARAMS ((tree, int));
54 static void expand_java_arraystore PARAMS ((tree));
55 static void expand_java_arrayload PARAMS ((tree));
56 static void expand_java_array_length PARAMS ((void));
57 static tree build_java_monitor PARAMS ((tree, tree));
58 static void expand_java_pushc PARAMS ((int, tree));
59 static void expand_java_return PARAMS ((tree));
60 static void expand_java_NEW PARAMS ((tree));
61 static void expand_java_INSTANCEOF PARAMS ((tree));
62 static void expand_java_CHECKCAST PARAMS ((tree));
63 static void expand_iinc PARAMS ((unsigned int, int, int));
64 static void expand_java_binop PARAMS ((tree, enum tree_code));
65 static void note_label PARAMS ((int, int));
66 static void expand_compare PARAMS ((enum tree_code, tree, tree, int));
67 static void expand_test PARAMS ((enum tree_code, tree, int));
68 static void expand_cond PARAMS ((enum tree_code, tree, int));
69 static void expand_java_goto PARAMS ((int));
71 static void expand_java_call PARAMS ((int, int));
72 static void expand_java_ret PARAMS ((tree));
74 static tree pop_arguments PARAMS ((tree));
75 static void expand_invoke PARAMS ((int, int, int));
76 static void expand_java_field_op PARAMS ((int, int, int));
77 static void java_push_constant_from_pool PARAMS ((struct JCF *, int));
78 static void java_stack_pop PARAMS ((int));
79 static tree build_java_throw_out_of_bounds_exception PARAMS ((tree));
80 static tree build_java_check_indexed_type PARAMS ((tree, tree));
81 static tree java_array_data_offset PARAMS ((tree));
82 static tree case_identity PARAMS ((tree, tree));
83 static unsigned char peek_opcode_at_pc PARAMS ((struct JCF *, int, int));
85 static tree operand_type[59];
86 extern struct obstack permanent_obstack;
88 static tree methods_ident = NULL_TREE;
89 static tree ncode_ident = NULL_TREE;
90 tree dtable_ident = NULL_TREE;
92 /* Set to non-zero value in order to emit class initilization code
93 before static field references. */
94 int always_initialize_class_p;
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 static tree quick_stack = NULL_TREE;
125 /* A free-list of unused permamnet TREE_LIST nodes. */
126 static 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 init_expr_processing()
139 operand_type[21] = operand_type[54] = int_type_node;
140 operand_type[22] = operand_type[55] = long_type_node;
141 operand_type[23] = operand_type[56] = float_type_node;
142 operand_type[24] = operand_type[57] = double_type_node;
143 operand_type[25] = operand_type[58] = ptr_type_node;
144 ggc_add_tree_root (operand_type, 59);
145 ggc_add_tree_root (&methods_ident, 1);
146 ggc_add_tree_root (&ncode_ident, 1);
147 ggc_add_tree_root (&quick_stack, 1);
148 ggc_add_tree_root (&tree_list_free_list, 1);
152 truthvalue_conversion (expr)
155 /* It is simpler and generates better code to have only TRUTH_*_EXPR
156 or comparison expressions as truth values at this level.
158 This function should normally be identity for Java. */
160 switch (TREE_CODE (expr))
163 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
164 case TRUTH_ANDIF_EXPR:
165 case TRUTH_ORIF_EXPR:
172 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
175 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
177 /* are these legal? XXX JH */
182 /* These don't change whether an object is non-zero or zero. */
183 return truthvalue_conversion (TREE_OPERAND (expr, 0));
186 /* Distribute the conversion into the arms of a COND_EXPR. */
187 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
188 truthvalue_conversion (TREE_OPERAND (expr, 1)),
189 truthvalue_conversion (TREE_OPERAND (expr, 2))));
192 /* If this is widening the argument, we can ignore it. */
193 if (TYPE_PRECISION (TREE_TYPE (expr))
194 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
195 return truthvalue_conversion (TREE_OPERAND (expr, 0));
196 /* fall through to default */
199 return fold (build (NE_EXPR, boolean_type_node, expr, boolean_false_node));
203 #ifdef JAVA_USE_HANDLES
204 /* Given a pointer to a handle, get a pointer to an object. */
210 tree field, handle_type;
211 expr = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
212 handle_type = TREE_TYPE (expr);
213 field = TYPE_FIELDS (handle_type);
214 expr = build (COMPONENT_REF, TREE_TYPE (field), expr, field);
219 /* Save any stack slots that happen to be in the quick_stack into their
220 home virtual register slots.
222 The copy order is from low stack index to high, to support the invariant
223 that the expression for a slot may contain decls for stack slots with
224 higher (or the same) index, but not lower. */
229 int stack_index = stack_pointer;
230 register tree prev, cur, next;
232 /* First reverse the quick_stack, and count the number of slots it has. */
233 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
235 next = TREE_CHAIN (cur);
236 TREE_CHAIN (cur) = prev;
238 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
242 while (quick_stack != NULL_TREE)
245 tree node = quick_stack, type;
246 quick_stack = TREE_CHAIN (node);
247 TREE_CHAIN (node) = tree_list_free_list;
248 tree_list_free_list = node;
249 node = TREE_VALUE (node);
250 type = TREE_TYPE (node);
252 decl = find_stack_slot (stack_index, type);
254 expand_assignment (decl, node, 0, 0);
255 stack_index += 1 + TYPE_IS_WIDE (type);
259 /* Push TYPE on the type stack.
260 Return true on success, 0 on overflow. */
267 type = promote_type (type);
268 n_words = 1 + TYPE_IS_WIDE (type);
269 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
271 stack_type_map[stack_pointer++] = type;
273 while (--n_words >= 0)
274 stack_type_map[stack_pointer++] = TYPE_SECOND;
282 if (! push_type_0 (type))
283 fatal ("stack overflow");
290 tree type = TREE_TYPE (value);
291 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
293 type = promote_type (type);
294 value = convert (type, value);
297 if (tree_list_free_list == NULL_TREE)
298 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
301 tree node = tree_list_free_list;
302 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
303 TREE_VALUE (node) = value;
304 TREE_CHAIN (node) = quick_stack;
309 /* Pop a type from the type stack.
310 TYPE is the expected type. Return the actual type, which must be
312 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
315 pop_type_0 (type, messagep)
322 if (TREE_CODE (type) == RECORD_TYPE)
323 type = promote_type (type);
324 n_words = 1 + TYPE_IS_WIDE (type);
325 if (stack_pointer < n_words)
327 *messagep = xstrdup ("stack underflow");
330 while (--n_words > 0)
332 if (stack_type_map[--stack_pointer] != void_type_node)
334 *messagep = xstrdup ("Invalid multi-word value on type stack");
338 t = stack_type_map[--stack_pointer];
339 if (type == NULL_TREE || t == type)
341 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
342 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
344 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
346 if (type == ptr_type_node || type == object_ptr_type_node)
348 else if (t == ptr_type_node) /* Special case for null reference. */
350 else if (can_widen_reference_to (t, type))
352 /* This is a kludge, but matches what Sun's verifier does.
353 It can be tricked, but is safe as long as type errors
354 (i.e. interface method calls) are caught at run-time. */
355 /* FIXME: this is worse than a kludge, probably. */
356 return object_ptr_type_node;
359 const char *str1 = "expected type '";
360 const char *str3 = "' but stack contains '";
361 const char *str5 = "'";
362 int len1 = strlen (str1);
363 int len2 = strlen (lang_printable_name (type, 0));
364 int len3 = strlen (str3);
365 int len4 = strlen (lang_printable_name (t, 0));
366 int len5 = strlen (str5);
367 char *msg = xmalloc (len1 + len2 + len3 + len4 + len5 + 1);
369 strcpy (msg, str1); msg += len1;
370 strcpy (msg, lang_printable_name (type, 0)); msg += len2;
371 strcpy (msg, str3); msg += len3;
372 strcpy (msg, lang_printable_name (t, 0)); msg += len4;
378 /* Pop a type from the type stack.
379 TYPE is the expected type. Return the actual type, which must be
380 convertible to TYPE, otherwise call error. */
386 char *message = NULL;
387 type = pop_type_0 (type, &message);
396 /* Return 1f if SOURCE_TYPE can be safely widened to TARGET_TYPE.
397 Handles array types and interfaces. */
400 can_widen_reference_to (source_type, target_type)
401 tree source_type, target_type;
403 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
406 /* Get rid of pointers */
407 if (TREE_CODE (source_type) == POINTER_TYPE)
408 source_type = TREE_TYPE (source_type);
409 if (TREE_CODE (target_type) == POINTER_TYPE)
410 target_type = TREE_TYPE (target_type);
412 if (source_type == target_type)
416 source_type = HANDLE_TO_CLASS_TYPE (source_type);
417 target_type = HANDLE_TO_CLASS_TYPE (target_type);
418 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
420 HOST_WIDE_INT source_length, target_length;
421 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
423 target_length = java_array_type_length (target_type);
424 if (target_length >= 0)
426 source_length = java_array_type_length (source_type);
427 if (source_length != target_length)
430 source_type = TYPE_ARRAY_ELEMENT (source_type);
431 target_type = TYPE_ARRAY_ELEMENT (target_type);
432 if (source_type == target_type)
434 if (TREE_CODE (source_type) != POINTER_TYPE
435 || TREE_CODE (target_type) != POINTER_TYPE)
437 return can_widen_reference_to (source_type, target_type);
441 int source_depth = class_depth (source_type);
442 int target_depth = class_depth (target_type);
444 /* class_depth can return a negative depth if an error occurred */
445 if (source_depth < 0 || target_depth < 0)
448 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
450 /* target_type is OK if source_type or source_type ancestors
451 implement target_type. We handle multiple sub-interfaces */
453 tree basetype_vec = TYPE_BINFO_BASETYPES (source_type);
454 int n = TREE_VEC_LENGTH (basetype_vec), i;
455 for (i=0 ; i < n; i++)
456 if (can_widen_reference_to
457 (TREE_TYPE (TREE_VEC_ELT (basetype_vec, i)),
464 for ( ; source_depth > target_depth; source_depth--)
466 source_type = TYPE_BINFO_BASETYPE (source_type, 0);
468 return source_type == target_type;
477 type = pop_type (type);
480 tree node = quick_stack;
481 quick_stack = TREE_CHAIN (quick_stack);
482 TREE_CHAIN (node) = tree_list_free_list;
483 tree_list_free_list = node;
484 node = TREE_VALUE (node);
488 return find_stack_slot (stack_pointer, promote_type (type));
492 /* Pop and discrad the top COUNT stack slots. */
495 java_stack_pop (count)
501 if (stack_pointer == 0)
502 fatal ("stack underflow");
503 type = stack_type_map[stack_pointer - 1];
504 if (type == TYPE_SECOND)
507 if (stack_pointer == 1 || count <= 0)
508 fatal ("stack underflow");
509 type = stack_type_map[stack_pointer - 2];
511 val = pop_value (type);
516 /* Implement the 'swap' operator (to swap two top stack slots). */
525 if (stack_pointer < 2
526 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
527 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
528 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
529 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
530 fatal ("bad stack swap");
532 flush_quick_stack ();
533 decl1 = find_stack_slot (stack_pointer - 1, type1);
534 decl2 = find_stack_slot (stack_pointer - 2, type2);
535 temp = copy_to_reg (DECL_RTL (decl1));
536 emit_move_insn (DECL_RTL (decl1), DECL_RTL (decl2));
537 emit_move_insn (DECL_RTL (decl2), temp);
538 stack_type_map[stack_pointer - 1] = type2;
539 stack_type_map[stack_pointer - 2] = type1;
543 java_stack_dup (size, offset)
546 int low_index = stack_pointer - size - offset;
549 error ("stack underflow - dup* operation");
551 flush_quick_stack ();
553 stack_pointer += size;
554 dst_index = stack_pointer;
556 for (dst_index = stack_pointer; --dst_index >= low_index; )
559 int src_index = dst_index - size;
560 if (src_index < low_index)
561 src_index = dst_index + size + offset;
562 type = stack_type_map [src_index];
563 if (type == TYPE_SECOND)
565 if (src_index <= low_index)
566 fatal ("dup operation splits 64-bit number");
567 stack_type_map[dst_index] = type;
568 src_index--; dst_index--;
569 type = stack_type_map[src_index];
570 if (! TYPE_IS_WIDE (type))
571 fatal ("internal error - dup operation");
573 else if (TYPE_IS_WIDE (type))
574 fatal ("internal error - dup operation");
575 if (src_index != dst_index)
577 tree src_decl = find_stack_slot (src_index, type);
578 tree dst_decl = find_stack_slot (dst_index, type);
579 emit_move_insn (DECL_RTL (dst_decl), DECL_RTL (src_decl));
580 stack_type_map[dst_index] = type;
585 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
589 build_java_athrow (node)
594 call = build (CALL_EXPR,
596 build_address_of (throw_node[exceptions_via_longjmp ? 1 : 0]),
597 build_tree_list (NULL_TREE, node),
599 TREE_SIDE_EFFECTS (call) = 1;
600 expand_expr_stmt (call);
601 java_stack_pop (stack_pointer);
604 /* Implementation for jsr/ret */
607 build_java_jsr (where, ret)
611 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
612 push_value (ret_label);
613 flush_quick_stack ();
614 emit_jump (label_rtx (where));
619 build_java_ret (location)
622 expand_computed_goto (location);
625 /* Implementation of operations on array: new, load, store, length */
627 /* Array core info access macros */
629 #define JAVA_ARRAY_LENGTH_OFFSET(A) \
630 byte_position (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (TREE_TYPE (A)))))
633 decode_newarray_type (atype)
638 case 4: return boolean_type_node;
639 case 5: return char_type_node;
640 case 6: return float_type_node;
641 case 7: return double_type_node;
642 case 8: return byte_type_node;
643 case 9: return short_type_node;
644 case 10: return int_type_node;
645 case 11: return long_type_node;
646 default: return NULL_TREE;
650 /* Map primitive type to the code used by OPCODE_newarray. */
653 encode_newarray_type (type)
656 if (type == boolean_type_node)
658 else if (type == char_type_node)
660 else if (type == float_type_node)
662 else if (type == double_type_node)
664 else if (type == byte_type_node)
666 else if (type == short_type_node)
668 else if (type == int_type_node)
670 else if (type == long_type_node)
673 fatal ("Can't compute type code - patch_newarray");
676 /* Build a call to _Jv_ThrowBadArrayIndex(), the
677 ArrayIndexOfBoundsException exception handler. */
680 build_java_throw_out_of_bounds_exception (index)
683 tree node = build (CALL_EXPR, int_type_node,
684 build_address_of (soft_badarrayindex_node),
685 build_tree_list (NULL_TREE, index), NULL_TREE);
686 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
690 /* Return the length of an array. Doesn't perform any checking on the nature
691 or value of the array NODE. May be used to implement some bytecodes. */
694 build_java_array_length_access (node)
697 tree type = TREE_TYPE (node);
698 HOST_WIDE_INT length;
699 if (!is_array_type_p (type))
700 fatal ("array length on a non-array reference");
701 length = java_array_type_length (type);
703 return build_int_2 (length, 0);
704 return fold (build1 (INDIRECT_REF,
706 fold (build (PLUS_EXPR, ptr_type_node,
708 JAVA_ARRAY_LENGTH_OFFSET(node)))));
711 /* Optionally checks an array against the NULL pointer, eventually throwing a
712 NullPointerException. It could replace signal handling, but tied to NULL.
713 ARG1: the pointer to check, ARG2: the expression to use if
714 the pointer is non-null and ARG3 the type that should be returned. */
717 build_java_arraynull_check (node, expr, type)
718 tree node ATTRIBUTE_UNUSED;
720 tree type ATTRIBUTE_UNUSED;
723 static int java_array_access_throws_null_exception = 0;
725 if (java_array_access_throws_null_exception)
726 return (build (COND_EXPR,
728 build (EQ_EXPR, int_type_node, node, null_pointer_node),
729 build_java_athrow (node), expr ));
736 java_array_data_offset (array)
739 tree array_type = TREE_TYPE (TREE_TYPE (array));
740 tree data_fld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
742 if (data_fld == NULL_TREE)
743 return size_in_bytes (array_type);
745 return byte_position (data_fld);
748 /* Implement array indexing (either as l-value or r-value).
749 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
750 Optionally performs bounds checking and/or test to NULL.
751 At this point, ARRAY should have been verified as an array. */
754 build_java_arrayaccess (array, type, index)
755 tree array, type, index;
757 tree arith, node, throw = NULL_TREE;
759 arith = fold (build (PLUS_EXPR, int_type_node,
760 java_array_data_offset (array),
761 fold (build (MULT_EXPR, int_type_node,
762 index, size_in_bytes(type)))));
764 if (flag_bounds_check)
767 * (unsigned jint) INDEX >= (unsigned jint) LEN
768 * && throw ArrayIndexOutOfBoundsException.
769 * Note this is equivalent to and more efficient than:
770 * INDEX < 0 || INDEX >= LEN && throw ... */
772 tree len = build_java_array_length_access (array);
773 TREE_TYPE (len) = unsigned_int_type_node;
774 test = fold (build (GE_EXPR, boolean_type_node,
775 convert (unsigned_int_type_node, index),
777 if (! integer_zerop (test))
779 throw = build (TRUTH_ANDIF_EXPR, int_type_node, test,
780 build_java_throw_out_of_bounds_exception (index));
781 /* allows expansion within COMPOUND */
782 TREE_SIDE_EFFECTS( throw ) = 1;
786 node = build1 (INDIRECT_REF, type,
787 fold (build (PLUS_EXPR, ptr_type_node,
789 (throw ? build (COMPOUND_EXPR, int_type_node,
793 return (fold (build_java_arraynull_check (array, node, type)));
796 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
797 ARRAY_NODE. This function is used to retrieve something less vague than
798 a pointer type when indexing the first dimension of something like [[<t>.
799 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
801 As a side effect, it also makes sure that ARRAY_NODE is an array. */
804 build_java_check_indexed_type (array_node, indexed_type)
810 if (!is_array_type_p (TREE_TYPE (array_node)))
811 fatal ("array indexing on a non-array reference");
813 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
815 if (indexed_type == ptr_type_node )
816 return promote_type (elt_type);
818 /* BYTE/BOOLEAN store and load are used for both type */
819 if (indexed_type == byte_type_node && elt_type == boolean_type_node )
820 return boolean_type_node;
822 if (indexed_type != elt_type )
823 fatal ("type array element mismatch");
828 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
829 called with an integer code (the type of array to create), and the length
830 of the array to create. */
833 build_newarray (atype_value, length)
839 tree prim_type = decode_newarray_type (atype_value);
841 = build_java_array_type (prim_type,
842 host_integerp (length, 0) == INTEGER_CST
843 ? tree_low_cst (length, 0) : -1);
845 /* If compiling to native, pass a reference to the primitive type class
846 and save the runtime some work. However, the bytecode generator
847 expects to find the type_code int here. */
848 if (flag_emit_class_files)
849 type_arg = build_int_2 (atype_value, 0);
851 type_arg = build_class_ref (prim_type);
853 return build (CALL_EXPR, promote_type (type),
854 build_address_of (soft_newarray_node),
855 tree_cons (NULL_TREE,
857 build_tree_list (NULL_TREE, length)),
861 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
865 build_anewarray (class_type, length)
870 = build_java_array_type (class_type,
871 host_integerp (length, 0)
872 ? tree_low_cst (length, 0) : -1);
874 return build (CALL_EXPR, promote_type (type),
875 build_address_of (soft_anewarray_node),
876 tree_cons (NULL_TREE, length,
877 tree_cons (NULL_TREE, build_class_ref (class_type),
878 build_tree_list (NULL_TREE,
879 null_pointer_node))),
883 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
886 build_new_array (type, length)
890 if (JPRIMITIVE_TYPE_P (type))
891 return build_newarray (encode_newarray_type (type), length);
893 return build_anewarray (TREE_TYPE (type), length);
896 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
897 class pointer, a number of dimensions and the matching number of
898 dimensions. The argument list is NULL terminated. */
901 expand_java_multianewarray (class_type, ndim)
906 tree args = build_tree_list( NULL_TREE, null_pointer_node );
908 for( i = 0; i < ndim; i++ )
909 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
911 push_value (build (CALL_EXPR,
912 promote_type (class_type),
913 build_address_of (soft_multianewarray_node),
914 tree_cons (NULL_TREE, build_class_ref (class_type),
915 tree_cons (NULL_TREE,
916 build_int_2 (ndim, 0), args )),
920 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
921 ARRAY is an array type. May expand some bound checking and NULL
922 pointer checking. RHS_TYPE_NODE we are going to store. In the case
923 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
924 INT. In those cases, we make the convertion.
926 if ARRAy is a reference type, the assignment is checked at run-time
927 to make sure that the RHS can be assigned to the array element
928 type. It is not necessary to generate this code if ARRAY is final. */
931 expand_java_arraystore (rhs_type_node)
934 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
935 && TYPE_PRECISION (rhs_type_node) <= 32) ?
936 int_type_node : rhs_type_node);
937 tree index = pop_value (int_type_node);
938 tree array = pop_value (ptr_type_node);
940 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
942 flush_quick_stack ();
944 index = save_expr (index);
945 array = save_expr (array);
947 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
949 tree check = build (CALL_EXPR, void_type_node,
950 build_address_of (soft_checkarraystore_node),
951 tree_cons (NULL_TREE, array,
952 build_tree_list (NULL_TREE, rhs_node)),
954 TREE_SIDE_EFFECTS (check) = 1;
955 expand_expr_stmt (check);
958 expand_assignment (build_java_arrayaccess (array,
964 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
965 sure that LHS is an array type. May expand some bound checking and NULL
967 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
968 BOOLEAN/SHORT, we push a promoted type back to the stack.
972 expand_java_arrayload (lhs_type_node )
976 tree index_node = pop_value (int_type_node);
977 tree array_node = pop_value (ptr_type_node);
979 index_node = save_expr (index_node);
980 array_node = save_expr (array_node);
981 lhs_type_node = build_java_check_indexed_type (array_node, lhs_type_node);
983 load_node = build_java_arrayaccess (array_node,
987 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
988 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
989 push_value (load_node);
992 /* Expands .length. Makes sure that we deal with and array and may expand
993 a NULL check on the array object. */
996 expand_java_array_length ()
998 tree array = pop_value (ptr_type_node);
999 tree length = build_java_array_length_access (array);
1001 push_value (build_java_arraynull_check (array, length, int_type_node));
1004 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1005 either soft_monitorenter_node or soft_monitorexit_node. */
1008 build_java_monitor (call, object)
1012 return (build (CALL_EXPR,
1014 build_address_of (call),
1015 build_tree_list (NULL_TREE, object),
1019 /* Emit code for one of the PUSHC instructions. */
1022 expand_java_pushc (ival, type)
1027 if (type == ptr_type_node && ival == 0)
1028 value = null_pointer_node;
1029 else if (type == int_type_node || type == long_type_node)
1031 value = build_int_2 (ival, ival < 0 ? -1 : 0);
1032 TREE_TYPE (value) = type;
1034 else if (type == float_type_node || type == double_type_node)
1037 #ifdef REAL_ARITHMETIC
1038 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1042 value = build_real (type, x);
1045 fatal ("internal error in expand_java_pushc");
1050 expand_java_return (type)
1053 if (type == void_type_node)
1054 expand_null_return ();
1057 tree retval = pop_value (type);
1058 tree res = DECL_RESULT (current_function_decl);
1059 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1061 /* Handle the situation where the native integer type is smaller
1062 than the JVM integer. It can happen for many cross compilers.
1063 The whole if expression just goes away if INT_TYPE_SIZE < 32
1065 if (INT_TYPE_SIZE < 32
1066 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1067 < GET_MODE_SIZE (TYPE_MODE (type))))
1068 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1070 TREE_SIDE_EFFECTS (retval) = 1;
1071 expand_return (retval);
1076 build_address_of (value)
1079 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1083 expand_java_NEW (type)
1086 if (! CLASS_LOADED_P (type))
1087 load_class (type, 1);
1088 safe_layout_class (type);
1089 push_value (build (CALL_EXPR, promote_type (type),
1090 build_address_of (alloc_object_node),
1091 tree_cons (NULL_TREE, build_class_ref (type),
1092 build_tree_list (NULL_TREE,
1093 size_in_bytes (type))),
1097 /* This returns an expression which will extract the class of an
1101 build_get_class (value)
1104 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1105 tree vtable_field = lookup_field (&object_type_node,
1106 get_identifier ("vtable"));
1107 return build (COMPONENT_REF, class_ptr_type,
1108 build1 (INDIRECT_REF, dtable_type,
1109 build (COMPONENT_REF, dtable_ptr_type,
1110 build1 (INDIRECT_REF, object_type_node, value),
1115 /* This builds the tree representation of the `instanceof' operator.
1116 It tries various tricks to optimize this in cases where types are
1120 build_instanceof (value, type)
1124 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1125 tree valtype = TREE_TYPE (TREE_TYPE (value));
1126 tree valclass = TYPE_NAME (valtype);
1129 /* When compiling from bytecode, we need to ensure that TYPE has
1131 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1133 load_class (type, 1);
1134 safe_layout_class (type);
1135 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1136 return error_mark_node;
1138 klass = TYPE_NAME (type);
1140 if (type == object_type_node || inherits_from_p (valtype, type))
1142 /* Anything except `null' is an instance of Object. Likewise,
1143 if the object is known to be an instance of the class, then
1144 we only need to check for `null'. */
1145 expr = build (COND_EXPR, itype,
1147 boolean_true_node, boolean_false_node);
1149 else if (DECL_P (klass) && DECL_P (valclass)
1150 && ! CLASS_INTERFACE (valclass)
1151 && ! CLASS_INTERFACE (klass)
1152 && ! inherits_from_p (type, valtype)
1153 && (CLASS_FINAL (klass)
1154 || ! inherits_from_p (valtype, type)))
1156 /* The classes are from different branches of the derivation
1157 tree, so we immediately know the answer. */
1158 expr = boolean_false_node;
1160 else if (DECL_P (klass) && CLASS_FINAL (klass))
1162 tree save = save_expr (value);
1163 expr = build (COND_EXPR, itype,
1165 build (EQ_EXPR, itype,
1166 build_get_class (save),
1167 build_class_ref (type)),
1168 boolean_false_node);
1172 expr = build (CALL_EXPR, itype,
1173 build_address_of (soft_instanceof_node),
1174 tree_cons (NULL_TREE, value,
1175 build_tree_list (NULL_TREE,
1176 build_class_ref (type))),
1179 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1184 expand_java_INSTANCEOF (type)
1187 tree value = pop_value (object_ptr_type_node);
1188 value = build_instanceof (value, type);
1193 expand_java_CHECKCAST (type)
1196 tree value = pop_value (ptr_type_node);
1197 value = build (CALL_EXPR, promote_type (type),
1198 build_address_of (soft_checkcast_node),
1199 tree_cons (NULL_TREE, build_class_ref (type),
1200 build_tree_list (NULL_TREE, value)),
1206 expand_iinc (local_var_index, ival, pc)
1207 unsigned int local_var_index;
1211 tree local_var, res;
1212 tree constant_value;
1214 flush_quick_stack ();
1215 local_var = find_local_variable (local_var_index, int_type_node, pc);
1216 constant_value = build_int_2 (ival, ival < 0 ? -1 : 0);
1217 res = fold (build (PLUS_EXPR, int_type_node, local_var, constant_value));
1218 expand_assignment (local_var, res, 0, 0);
1223 build_java_soft_divmod (op, type, op1, op2)
1225 tree type, op1, op2;
1228 tree arg1 = convert (type, op1);
1229 tree arg2 = convert (type, op2);
1231 if (type == int_type_node)
1235 case TRUNC_DIV_EXPR:
1236 call = soft_idiv_node;
1238 case TRUNC_MOD_EXPR:
1239 call = soft_irem_node;
1245 else if (type == long_type_node)
1249 case TRUNC_DIV_EXPR:
1250 call = soft_ldiv_node;
1252 case TRUNC_MOD_EXPR:
1253 call = soft_lrem_node;
1261 fatal ("Internal compiler error in build_java_soft_divmod");
1263 call = build (CALL_EXPR, type,
1264 build_address_of (call),
1265 tree_cons (NULL_TREE, arg1,
1266 build_tree_list (NULL_TREE, arg2)),
1273 build_java_binop (op, type, arg1, arg2)
1275 tree type, arg1, arg2;
1282 tree u_type = unsigned_type (type);
1283 arg1 = convert (u_type, arg1);
1284 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1285 return convert (type, arg1);
1289 mask = build_int_2 (TYPE_PRECISION (TREE_TYPE (arg1)) - 1, 0);
1290 arg2 = fold (build (BIT_AND_EXPR, int_type_node, arg2, mask));
1293 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1294 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1295 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1297 tree ifexp1 = fold ( build (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1298 boolean_type_node, arg1, arg2));
1299 tree ifexp2 = fold ( build (EQ_EXPR, boolean_type_node, arg1, arg2));
1300 tree second_compare = fold (build (COND_EXPR, int_type_node,
1301 ifexp2, integer_zero_node,
1302 op == COMPARE_L_EXPR
1303 ? integer_negative_one_node
1304 : integer_one_node));
1305 return fold (build (COND_EXPR, int_type_node, ifexp1,
1306 op == COMPARE_L_EXPR ? integer_one_node
1307 : integer_negative_one_node,
1311 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1313 tree ifexp1 = fold ( build (LT_EXPR, boolean_type_node, arg1, arg2));
1314 tree ifexp2 = fold ( build (GT_EXPR, boolean_type_node, arg1, arg2));
1315 tree second_compare = fold ( build (COND_EXPR, int_type_node,
1316 ifexp2, integer_one_node,
1317 integer_zero_node));
1318 return fold (build (COND_EXPR, int_type_node,
1319 ifexp1, integer_negative_one_node, second_compare));
1321 case TRUNC_DIV_EXPR:
1322 case TRUNC_MOD_EXPR:
1323 if (TREE_CODE (type) == REAL_TYPE
1324 && op == TRUNC_MOD_EXPR)
1327 if (type != double_type_node)
1329 arg1 = convert (double_type_node, arg1);
1330 arg2 = convert (double_type_node, arg2);
1332 call = build (CALL_EXPR, double_type_node,
1333 build_address_of (soft_fmod_node),
1334 tree_cons (NULL_TREE, arg1,
1335 build_tree_list (NULL_TREE, arg2)),
1337 if (type != double_type_node)
1338 call = convert (type, call);
1342 if (TREE_CODE (type) == INTEGER_TYPE
1343 && flag_use_divide_subroutine
1344 && ! flag_syntax_only)
1345 return build_java_soft_divmod (op, type, arg1, arg2);
1350 return fold (build (op, type, arg1, arg2));
1354 expand_java_binop (type, op)
1355 tree type; enum tree_code op;
1365 rtype = int_type_node;
1366 rarg = pop_value (rtype);
1369 rarg = pop_value (rtype);
1371 larg = pop_value (ltype);
1372 push_value (build_java_binop (op, type, larg, rarg));
1375 /* Lookup the field named NAME in *TYPEP or its super classes.
1376 If not found, return NULL_TREE.
1377 (If the *TYPEP is not found, or if the field reference is
1378 ambiguous, return error_mark_node.)
1379 If found, return the FIELD_DECL, and set *TYPEP to the
1380 class containing the field. */
1383 lookup_field (typep, name)
1387 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1389 load_class (*typep, 1);
1390 safe_layout_class (*typep);
1391 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1392 return error_mark_node;
1396 tree field, basetype_vec;
1400 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1401 if (DECL_NAME (field) == name)
1404 /* If *typep is an innerclass, lookup the field in its enclosing
1406 if (INNER_CLASS_TYPE_P (*typep))
1408 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (*typep)));
1410 if ((field = lookup_field (&outer_type, name)))
1414 /* Process implemented interfaces. */
1415 basetype_vec = TYPE_BINFO_BASETYPES (*typep);
1416 n = TREE_VEC_LENGTH (basetype_vec);
1417 save_field = NULL_TREE;
1418 for (i = 0; i < n; i++)
1420 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
1421 if ((field = lookup_field (&t, name)))
1423 if (save_field == field)
1425 if (save_field == NULL_TREE)
1429 tree i1 = DECL_CONTEXT (save_field);
1430 tree i2 = DECL_CONTEXT (field);
1431 error ("reference `%s' is ambiguous: appears in interface `%s' and interface `%s'",
1432 IDENTIFIER_POINTER (name),
1433 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1434 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1435 return error_mark_node;
1440 if (save_field != NULL_TREE)
1443 *typep = CLASSTYPE_SUPER (*typep);
1448 /* Look up the field named NAME in object SELF_VALUE,
1449 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1450 SELF_VALUE is NULL_TREE if looking for a static field. */
1453 build_field_ref (self_value, self_class, name)
1454 tree self_value, self_class, name;
1456 tree base_class = self_class;
1457 tree field_decl = lookup_field (&base_class, name);
1458 if (field_decl == NULL_TREE)
1460 error ("field `%s' not found", IDENTIFIER_POINTER (name));
1461 return error_mark_node;
1463 if (self_value == NULL_TREE)
1465 return build_static_field_ref (field_decl);
1469 tree base_handle_type = promote_type (base_class);
1470 if (base_handle_type != TREE_TYPE (self_value))
1471 self_value = fold (build1 (NOP_EXPR, base_handle_type, self_value));
1472 #ifdef JAVA_USE_HANDLES
1473 self_value = unhand_expr (self_value);
1475 self_value = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (self_value)),
1477 return fold (build (COMPONENT_REF, TREE_TYPE (field_decl),
1478 self_value, field_decl));
1488 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1489 name = get_identifier (buf);
1490 if (IDENTIFIER_LOCAL_VALUE (name))
1491 return IDENTIFIER_LOCAL_VALUE (name);
1494 /* The type of the address of a label is return_address_type_node. */
1495 tree decl = create_label_decl (name);
1496 LABEL_PC (decl) = pc;
1498 return pushdecl (decl);
1502 /* Generate a unique name for the purpose of loops and switches
1503 labels, and try-catch-finally blocks label or temporary variables. */
1508 static int l_number = 0;
1510 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1512 return get_identifier (buff);
1516 create_label_decl (name)
1520 decl = build_decl (LABEL_DECL, name,
1521 TREE_TYPE (return_address_type_node));
1522 DECL_CONTEXT (decl) = current_function_decl;
1523 DECL_IGNORED_P (decl) = 1;
1527 /* This maps a bytecode offset (PC) to various flags. */
1528 char *instruction_bits;
1531 note_label (current_pc, target_pc)
1532 int current_pc ATTRIBUTE_UNUSED, target_pc;
1534 lookup_label (target_pc);
1535 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1538 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1539 where CONDITION is one of one the compare operators. */
1542 expand_compare (condition, value1, value2, target_pc)
1543 enum tree_code condition;
1544 tree value1, value2;
1547 tree target = lookup_label (target_pc);
1548 tree cond = fold (build (condition, boolean_type_node, value1, value2));
1549 expand_start_cond (truthvalue_conversion (cond), 0);
1550 expand_goto (target);
1554 /* Emit code for a TEST-type opcode. */
1557 expand_test (condition, type, target_pc)
1558 enum tree_code condition;
1562 tree value1, value2;
1563 flush_quick_stack ();
1564 value1 = pop_value (type);
1565 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1566 expand_compare (condition, value1, value2, target_pc);
1569 /* Emit code for a COND-type opcode. */
1572 expand_cond (condition, type, target_pc)
1573 enum tree_code condition;
1577 tree value1, value2;
1578 flush_quick_stack ();
1579 /* note: pop values in opposite order */
1580 value2 = pop_value (type);
1581 value1 = pop_value (type);
1582 /* Maybe should check value1 and value2 for type compatibility ??? */
1583 expand_compare (condition, value1, value2, target_pc);
1587 expand_java_goto (target_pc)
1590 tree target_label = lookup_label (target_pc);
1591 flush_quick_stack ();
1592 expand_goto (target_label);
1597 expand_java_call (target_pc, return_address)
1598 int target_pc, return_address;
1600 tree target_label = lookup_label (target_pc);
1601 tree value = build_int_2 (return_address, return_address < 0 ? -1 : 0);
1603 flush_quick_stack ();
1604 expand_goto (target_label);
1608 expand_java_ret (return_address)
1609 tree return_address ATTRIBUTE_UNUSED;
1611 warning ("ret instruction not implemented");
1613 tree target_label = lookup_label (target_pc);
1614 flush_quick_stack ();
1615 expand_goto (target_label);
1621 pop_arguments (arg_types)
1624 if (arg_types == end_params_node)
1626 if (TREE_CODE (arg_types) == TREE_LIST)
1628 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1629 tree type = TREE_VALUE (arg_types);
1630 tree arg = pop_value (type);
1631 if (PROMOTE_PROTOTYPES
1632 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1633 && INTEGRAL_TYPE_P (type))
1634 arg = convert (integer_type_node, arg);
1635 return tree_cons (NULL_TREE, arg, tail);
1640 /* Build an expression to initialize the class CLAS.
1641 if EXPR is non-NULL, returns an expression to first call the initializer
1642 (if it is needed) and then calls EXPR. */
1645 build_class_init (clas, expr)
1649 struct init_test_hash_entry *ite;
1650 if (inherits_from_p (current_class, clas))
1653 if (always_initialize_class_p)
1655 init = build (CALL_EXPR, void_type_node,
1656 build_address_of (soft_initclass_node),
1657 build_tree_list (NULL_TREE, build_class_ref (clas)),
1659 TREE_SIDE_EFFECTS (init) = 1;
1663 ite = (struct init_test_hash_entry *)
1664 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
1665 (const hash_table_key) clas,
1668 if (ite->init_test_decl == 0)
1669 ite->init_test_decl = build_decl (VAR_DECL, NULL_TREE,
1671 /* Tell the check-init code to ignore this decl. */
1672 DECL_BIT_INDEX(ite->init_test_decl) = -1;
1674 init = build (CALL_EXPR, void_type_node,
1675 build_address_of (soft_initclass_node),
1676 build_tree_list (NULL_TREE, build_class_ref (clas)),
1678 TREE_SIDE_EFFECTS (init) = 1;
1679 call = build (COMPOUND_EXPR, TREE_TYPE (expr), init,
1680 build (MODIFY_EXPR, boolean_type_node,
1681 ite->init_test_decl, boolean_true_node));
1682 TREE_SIDE_EFFECTS (call) = 1;
1683 init = build (COND_EXPR, void_type_node,
1684 build (EQ_EXPR, boolean_type_node,
1685 ite->init_test_decl, boolean_false_node),
1686 call, integer_zero_node);
1687 TREE_SIDE_EFFECTS (init) = 1;
1690 if (expr != NULL_TREE)
1692 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1693 TREE_SIDE_EFFECTS (expr) = 1;
1700 build_known_method_ref (method, method_type, self_type, method_signature, arg_list)
1701 tree method, method_type ATTRIBUTE_UNUSED, self_type,
1702 method_signature ATTRIBUTE_UNUSED, arg_list ATTRIBUTE_UNUSED;
1705 if (is_compiled_class (self_type))
1707 make_decl_rtl (method, NULL);
1708 func = build1 (ADDR_EXPR, method_ptr_type_node, method);
1712 /* We don't know whether the method has been (statically) compiled.
1713 Compile this code to get a reference to the method's code:
1715 SELF_TYPE->methods[METHOD_INDEX].ncode
1717 This is guaranteed to work (assuming SELF_TYPE has
1718 been initialized), since if the method is not compiled yet,
1719 its ncode points to a trampoline that forces compilation. */
1721 int method_index = 0;
1723 tree ref = build_class_ref (self_type);
1724 ref = build1 (INDIRECT_REF, class_type_node, ref);
1725 if (ncode_ident == NULL_TREE)
1726 ncode_ident = get_identifier ("ncode");
1727 if (methods_ident == NULL_TREE)
1728 methods_ident = get_identifier ("methods");
1729 ref = build (COMPONENT_REF, method_ptr_type_node, ref,
1730 lookup_field (&class_type_node, methods_ident));
1731 for (meth = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (self_type));
1732 ; meth = TREE_CHAIN (meth))
1736 if (meth == NULL_TREE)
1737 fatal ("method '%s' not found in class",
1738 IDENTIFIER_POINTER (DECL_NAME (method)));
1741 method_index *= int_size_in_bytes (method_type_node);
1742 ref = fold (build (PLUS_EXPR, method_ptr_type_node,
1743 ref, build_int_2 (method_index, 0)));
1744 ref = build1 (INDIRECT_REF, method_type_node, ref);
1745 func = build (COMPONENT_REF, nativecode_ptr_type_node,
1747 lookup_field (&method_type_node, ncode_ident));
1753 invoke_build_dtable (is_invoke_interface, arg_list)
1754 int is_invoke_interface;
1757 tree dtable, objectref;
1759 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
1761 /* If we're dealing with interfaces and if the objectref
1762 argument is an array then get the dispatch table of the class
1763 Object rather than the one from the objectref. */
1764 objectref = (is_invoke_interface
1765 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list))) ?
1766 object_type_node : TREE_VALUE (arg_list));
1768 if (dtable_ident == NULL_TREE)
1769 dtable_ident = get_identifier ("vtable");
1770 dtable = build1 (INDIRECT_REF, object_type_node, objectref );
1771 dtable = build (COMPONENT_REF, dtable_ptr_type, dtable,
1772 lookup_field (&object_type_node, dtable_ident));
1778 build_invokevirtual (dtable, method)
1779 tree dtable, method;
1782 tree nativecode_ptr_ptr_type_node
1783 = build_pointer_type (nativecode_ptr_type_node);
1784 tree method_index = convert (sizetype, DECL_VINDEX (method));
1786 /* Add one to skip "class" field of dtable, and one to skip unused
1787 vtable entry (for C++ compatibility). */
1788 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1789 method_index = size_binop (MULT_EXPR, method_index,
1790 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
1791 func = fold (build (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
1792 convert (nativecode_ptr_ptr_type_node, method_index)));
1793 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
1799 build_invokeinterface (dtable, method)
1800 tree dtable, method;
1802 static tree class_ident = NULL_TREE;
1809 /* We expand invokeinterface here. _Jv_LookupInterfaceMethod() will
1810 ensure that the selected method exists, is public and not
1811 abstract nor static. */
1813 if (class_ident == NULL_TREE)
1815 class_ident = get_identifier ("class");
1816 ggc_add_tree_root (&class_ident, 1);
1819 dtable = build1 (INDIRECT_REF, dtable_type, dtable);
1820 dtable = build (COMPONENT_REF, class_ptr_type, dtable,
1821 lookup_field (&dtable_type, class_ident));
1823 interface = DECL_CONTEXT (method);
1824 layout_class_methods (interface);
1827 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
1831 idx = build_int_2 (i, 0);
1834 if (meth == NULL_TREE)
1835 fatal ("internal error in build_invokeinterface");
1838 lookup_arg = tree_cons (NULL_TREE, dtable,
1839 tree_cons (NULL_TREE, build_class_ref (interface),
1840 build_tree_list (NULL_TREE, idx)));
1842 return build (CALL_EXPR, ptr_type_node,
1843 build_address_of (soft_lookupinterfacemethod_node),
1844 lookup_arg, NULL_TREE);
1847 /* Expand one of the invoke_* opcodes.
1848 OCPODE is the specific opcode.
1849 METHOD_REF_INDEX is an index into the constant pool.
1850 NARGS is the number of arguments, or -1 if not specified. */
1853 expand_invoke (opcode, method_ref_index, nargs)
1855 int method_ref_index;
1856 int nargs ATTRIBUTE_UNUSED;
1858 tree method_signature = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
1859 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool, method_ref_index);
1860 tree self_type = get_class_constant
1861 (current_jcf, COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool, method_ref_index));
1862 const char *self_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
1863 tree call, func, method, arg_list, method_type;
1864 tree cond = NULL_TREE;
1866 if (! CLASS_LOADED_P (self_type))
1868 load_class (self_type, 1);
1869 safe_layout_class (self_type);
1870 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
1871 fatal ("failed to find class '%s'", self_name);
1873 layout_class_methods (self_type);
1875 if (ID_INIT_P (method_name))
1876 method = lookup_java_constructor (CLASS_TO_HANDLE_TYPE (self_type),
1879 method = lookup_java_method (CLASS_TO_HANDLE_TYPE (self_type),
1880 method_name, method_signature);
1881 if (method == NULL_TREE)
1883 error ("Class '%s' has no method named '%s' matching signature '%s'",
1885 IDENTIFIER_POINTER (method_name),
1886 IDENTIFIER_POINTER (method_signature));
1888 /* Invoke static can't invoke static/abstract method */
1889 else if (opcode == OPCODE_invokestatic)
1891 if (!METHOD_STATIC (method))
1893 error ("invokestatic on non static method");
1896 else if (METHOD_ABSTRACT (method))
1898 error ("invokestatic on abstract method");
1904 if (METHOD_STATIC (method))
1906 error ("invoke[non-static] on static method");
1911 if (method == NULL_TREE)
1913 method_type = get_type_from_signature (method_signature);
1914 pop_arguments (TYPE_ARG_TYPES (method_type));
1915 if (opcode != OPCODE_invokestatic)
1916 pop_type (self_type);
1917 method_type = promote_type (TREE_TYPE (method_type));
1918 push_value (convert (method_type, integer_zero_node));
1922 method_type = TREE_TYPE (method);
1923 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
1924 flush_quick_stack ();
1927 if (opcode == OPCODE_invokestatic)
1928 func = build_known_method_ref (method, method_type, self_type,
1929 method_signature, arg_list);
1930 else if (opcode == OPCODE_invokespecial
1931 || (opcode == OPCODE_invokevirtual
1932 && (METHOD_PRIVATE (method)
1933 || METHOD_FINAL (method)
1934 || CLASS_FINAL (TYPE_NAME (self_type)))))
1936 /* If the object for the method call is null, we throw an
1937 exception. We don't do this if the object is the current
1938 method's `this'. In other cases we just rely on an
1939 optimization pass to eliminate redundant checks. FIXME:
1940 Unfortunately there doesn't seem to be a way to determine
1941 what the current method is right now. */
1942 /* We use a SAVE_EXPR here to make sure we only evaluate
1943 the new `self' expression once. */
1944 tree save_arg = save_expr (TREE_VALUE (arg_list));
1945 TREE_VALUE (arg_list) = save_arg;
1946 cond = build (EQ_EXPR, boolean_type_node, save_arg, null_pointer_node);
1947 func = build_known_method_ref (method, method_type, self_type,
1948 method_signature, arg_list);
1952 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
1954 if (opcode == OPCODE_invokevirtual)
1955 func = build_invokevirtual (dtable, method);
1957 func = build_invokeinterface (dtable, method);
1959 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
1960 call = build (CALL_EXPR, TREE_TYPE (method_type), func, arg_list, NULL_TREE);
1961 TREE_SIDE_EFFECTS (call) = 1;
1963 if (cond != NULL_TREE)
1965 /* We have to make the `then' branch a compound expression to
1966 make the types turn out right. This seems bizarre. */
1967 call = build (COND_EXPR, TREE_TYPE (call), cond,
1968 build (COMPOUND_EXPR, TREE_TYPE (call),
1969 build (CALL_EXPR, void_type_node,
1970 build_address_of (soft_nullpointer_node),
1971 NULL_TREE, NULL_TREE),
1972 (FLOAT_TYPE_P (TREE_TYPE (call))
1973 ? build_real (TREE_TYPE (call), dconst0)
1974 : build1 (CONVERT_EXPR, TREE_TYPE (call),
1975 integer_zero_node))),
1977 TREE_SIDE_EFFECTS (call) = 1;
1980 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
1981 expand_expr_stmt (call);
1985 flush_quick_stack ();
1989 /* Create a stub which will be put into the vtable but which will call
1993 build_jni_stub (method)
1996 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
1997 tree jni_func_type, tem;
1998 tree env_var, res_var = NULL_TREE, block;
1999 tree method_args, res_type;
2002 tree klass = DECL_CONTEXT (method);
2003 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2004 klass = build_class_ref (klass);
2006 if (! METHOD_NATIVE (method) || ! flag_jni)
2009 DECL_ARTIFICIAL (method) = 1;
2010 DECL_EXTERNAL (method) = 0;
2012 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2013 DECL_CONTEXT (env_var) = method;
2015 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2017 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2018 TREE_TYPE (TREE_TYPE (method)));
2019 DECL_CONTEXT (res_var) = method;
2020 TREE_CHAIN (env_var) = res_var;
2023 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2024 TREE_STATIC (meth_var) = 1;
2025 TREE_PUBLIC (meth_var) = 0;
2026 DECL_EXTERNAL (meth_var) = 0;
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 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl)) = 1;
2391 make_decl_rtl (init_decl, NULL);
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 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl)) = 1;
2414 make_decl_rtl (init_decl, NULL);
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;