1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
30 #include "coretypes.h"
37 #include "java-tree.h"
39 #include "java-opcodes.h"
41 #include "java-except.h"
46 #include "tree-gimple.h"
49 static void flush_quick_stack (void);
50 static void push_value (tree);
51 static tree pop_value (tree);
52 static void java_stack_swap (void);
53 static void java_stack_dup (int, int);
54 static void build_java_athrow (tree);
55 static void build_java_jsr (int, int);
56 static void build_java_ret (tree);
57 static void expand_java_multianewarray (tree, int);
58 static void expand_java_arraystore (tree);
59 static void expand_java_arrayload (tree);
60 static void expand_java_array_length (void);
61 static tree build_java_monitor (tree, tree);
62 static void expand_java_pushc (int, tree);
63 static void expand_java_return (tree);
64 static void expand_load_internal (int, tree, int);
65 static void expand_java_NEW (tree);
66 static void expand_java_INSTANCEOF (tree);
67 static void expand_java_CHECKCAST (tree);
68 static void expand_iinc (unsigned int, int, int);
69 static void expand_java_binop (tree, enum tree_code);
70 static void note_label (int, int);
71 static void expand_compare (enum tree_code, tree, tree, int);
72 static void expand_test (enum tree_code, tree, int);
73 static void expand_cond (enum tree_code, tree, int);
74 static void expand_java_goto (int);
75 static tree expand_java_switch (tree, int);
76 static void expand_java_add_case (tree, int, int);
78 static void expand_java_call (int, int);
79 static void expand_java_ret (tree);
81 static tree pop_arguments (tree);
82 static void expand_invoke (int, int, int);
83 static void expand_java_field_op (int, int, int);
84 static void java_push_constant_from_pool (struct JCF *, int);
85 static void java_stack_pop (int);
86 static tree build_java_throw_out_of_bounds_exception (tree);
87 static tree build_java_check_indexed_type (tree, tree);
88 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
90 static GTY(()) tree operand_type[59];
92 static GTY(()) tree methods_ident;
93 static GTY(()) tree ncode_ident;
94 tree dtable_ident = NULL_TREE;
96 /* Set to nonzero value in order to emit class initialization code
97 before static field references. */
98 /* FIXME: Make this work with gimplify. */
99 int always_initialize_class_p = 1;
101 /* We store the stack state in two places:
102 Within a basic block, we use the quick_stack, which is a
103 pushdown list (TREE_LISTs) of expression nodes.
104 This is the top part of the stack; below that we use find_stack_slot.
105 At the end of a basic block, the quick_stack must be flushed
106 to the stack slot array (as handled by find_stack_slot).
107 Using quick_stack generates better code (especially when
108 compiled without optimization), because we do not have to
109 explicitly store and load trees to temporary variables.
111 If a variable is on the quick stack, it means the value of variable
112 when the quick stack was last flushed. Conceptually, flush_quick_stack
113 saves all the quick_stack elements in parallel. However, that is
114 complicated, so it actually saves them (i.e. copies each stack value
115 to is home virtual register) from low indexes. This allows a quick_stack
116 element at index i (counting from the bottom of stack the) to references
117 slot virtuals for register that are >= i, but not those that are deeper.
118 This convention makes most operations easier. For example iadd works
119 even when the stack contains (reg[0], reg[1]): It results in the
120 stack containing (reg[0]+reg[1]), which is OK. However, some stack
121 operations are more complicated. For example dup given a stack
122 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
123 the convention, since stack value 1 would refer to a register with
124 lower index (reg[0]), which flush_quick_stack does not safely handle.
125 So dup cannot just add an extra element to the quick_stack, but iadd can.
128 static GTY(()) tree quick_stack;
130 /* A free-list of unused permanent TREE_LIST nodes. */
131 static GTY((deletable)) tree tree_list_free_list;
133 /* The stack pointer of the Java virtual machine.
134 This does include the size of the quick_stack. */
138 const unsigned char *linenumber_table;
139 int linenumber_count;
142 init_expr_processing (void)
144 operand_type[21] = operand_type[54] = int_type_node;
145 operand_type[22] = operand_type[55] = long_type_node;
146 operand_type[23] = operand_type[56] = float_type_node;
147 operand_type[24] = operand_type[57] = double_type_node;
148 operand_type[25] = operand_type[58] = ptr_type_node;
152 java_truthvalue_conversion (tree expr)
154 /* It is simpler and generates better code to have only TRUTH_*_EXPR
155 or comparison expressions as truth values at this level.
157 This function should normally be identity for Java. */
159 switch (TREE_CODE (expr))
161 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
162 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
163 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
164 case ORDERED_EXPR: case UNORDERED_EXPR:
165 case TRUTH_ANDIF_EXPR:
166 case TRUTH_ORIF_EXPR:
175 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
178 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
180 /* are these legal? XXX JH */
184 /* These don't change whether an object is nonzero or zero. */
185 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
188 /* Distribute the conversion into the arms of a COND_EXPR. */
190 (build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
191 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
192 java_truthvalue_conversion (TREE_OPERAND (expr, 2))));
195 /* If this is widening the argument, we can ignore it. */
196 if (TYPE_PRECISION (TREE_TYPE (expr))
197 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
198 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
199 /* fall through to default */
202 return fold (build2 (NE_EXPR, boolean_type_node,
203 expr, boolean_false_node));
207 /* Save any stack slots that happen to be in the quick_stack into their
208 home virtual register slots.
210 The copy order is from low stack index to high, to support the invariant
211 that the expression for a slot may contain decls for stack slots with
212 higher (or the same) index, but not lower. */
215 flush_quick_stack (void)
217 int stack_index = stack_pointer;
218 tree prev, cur, next;
220 /* First reverse the quick_stack, and count the number of slots it has. */
221 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
223 next = TREE_CHAIN (cur);
224 TREE_CHAIN (cur) = prev;
226 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
230 while (quick_stack != NULL_TREE)
233 tree node = quick_stack, type;
234 quick_stack = TREE_CHAIN (node);
235 TREE_CHAIN (node) = tree_list_free_list;
236 tree_list_free_list = node;
237 node = TREE_VALUE (node);
238 type = TREE_TYPE (node);
240 decl = find_stack_slot (stack_index, type);
242 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
243 stack_index += 1 + TYPE_IS_WIDE (type);
247 /* Push TYPE on the type stack.
248 Return true on success, 0 on overflow. */
251 push_type_0 (tree type)
254 type = promote_type (type);
255 n_words = 1 + TYPE_IS_WIDE (type);
256 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
258 /* Allocate decl for this variable now, so we get a temporary that
259 survives the whole method. */
260 find_stack_slot (stack_pointer, type);
261 stack_type_map[stack_pointer++] = type;
263 while (--n_words >= 0)
264 stack_type_map[stack_pointer++] = TYPE_SECOND;
269 push_type (tree type)
271 if (! push_type_0 (type))
276 push_value (tree value)
278 tree type = TREE_TYPE (value);
279 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
281 type = promote_type (type);
282 value = convert (type, value);
285 if (tree_list_free_list == NULL_TREE)
286 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
289 tree node = tree_list_free_list;
290 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
291 TREE_VALUE (node) = value;
292 TREE_CHAIN (node) = quick_stack;
297 /* Pop a type from the type stack.
298 TYPE is the expected type. Return the actual type, which must be
300 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
303 pop_type_0 (tree type, char **messagep)
308 if (TREE_CODE (type) == RECORD_TYPE)
309 type = promote_type (type);
310 n_words = 1 + TYPE_IS_WIDE (type);
311 if (stack_pointer < n_words)
313 *messagep = xstrdup ("stack underflow");
316 while (--n_words > 0)
318 if (stack_type_map[--stack_pointer] != void_type_node)
320 *messagep = xstrdup ("Invalid multi-word value on type stack");
324 t = stack_type_map[--stack_pointer];
325 if (type == NULL_TREE || t == type)
327 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
328 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
330 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
332 if (type == ptr_type_node || type == object_ptr_type_node)
334 else if (t == ptr_type_node) /* Special case for null reference. */
336 else if (can_widen_reference_to (t, type))
338 /* This is a kludge, but matches what Sun's verifier does.
339 It can be tricked, but is safe as long as type errors
340 (i.e. interface method calls) are caught at run-time. */
341 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
342 return object_ptr_type_node;
345 /* lang_printable_name uses a static buffer, so we must save the result
346 from calling it the first time. */
348 char *temp = xstrdup (lang_printable_name (type, 0));
349 *messagep = concat ("expected type '", temp,
350 "' but stack contains '", lang_printable_name (t, 0),
357 /* Pop a type from the type stack.
358 TYPE is the expected type. Return the actual type, which must be
359 convertible to TYPE, otherwise call error. */
364 char *message = NULL;
365 type = pop_type_0 (type, &message);
368 error ("%s", message);
374 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
375 Handles array types and interfaces. */
378 can_widen_reference_to (tree source_type, tree target_type)
380 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
383 /* Get rid of pointers */
384 if (TREE_CODE (source_type) == POINTER_TYPE)
385 source_type = TREE_TYPE (source_type);
386 if (TREE_CODE (target_type) == POINTER_TYPE)
387 target_type = TREE_TYPE (target_type);
389 if (source_type == target_type)
393 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
395 HOST_WIDE_INT source_length, target_length;
396 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
398 /* An array implements Cloneable and Serializable. */
399 tree name = DECL_NAME (TYPE_NAME (target_type));
400 return (name == java_lang_cloneable_identifier_node
401 || name == java_io_serializable_identifier_node);
403 target_length = java_array_type_length (target_type);
404 if (target_length >= 0)
406 source_length = java_array_type_length (source_type);
407 if (source_length != target_length)
410 source_type = TYPE_ARRAY_ELEMENT (source_type);
411 target_type = TYPE_ARRAY_ELEMENT (target_type);
412 if (source_type == target_type)
414 if (TREE_CODE (source_type) != POINTER_TYPE
415 || TREE_CODE (target_type) != POINTER_TYPE)
417 return can_widen_reference_to (source_type, target_type);
421 int source_depth = class_depth (source_type);
422 int target_depth = class_depth (target_type);
424 /* class_depth can return a negative depth if an error occurred */
425 if (source_depth < 0 || target_depth < 0)
428 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
430 /* target_type is OK if source_type or source_type ancestors
431 implement target_type. We handle multiple sub-interfaces */
432 tree binfo, base_binfo;
435 for (binfo = TYPE_BINFO (source_type), i = 0;
436 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
437 if (can_widen_reference_to
438 (BINFO_TYPE (base_binfo), target_type))
445 for ( ; source_depth > target_depth; source_depth--)
448 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
450 return source_type == target_type;
456 pop_value (tree type)
458 type = pop_type (type);
461 tree node = quick_stack;
462 quick_stack = TREE_CHAIN (quick_stack);
463 TREE_CHAIN (node) = tree_list_free_list;
464 tree_list_free_list = node;
465 node = TREE_VALUE (node);
469 return find_stack_slot (stack_pointer, promote_type (type));
473 /* Pop and discard the top COUNT stack slots. */
476 java_stack_pop (int count)
482 if (stack_pointer == 0)
485 type = stack_type_map[stack_pointer - 1];
486 if (type == TYPE_SECOND)
489 if (stack_pointer == 1 || count <= 0)
492 type = stack_type_map[stack_pointer - 2];
494 val = pop_value (type);
499 /* Implement the 'swap' operator (to swap two top stack slots). */
502 java_stack_swap (void)
508 if (stack_pointer < 2
509 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
510 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
511 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
512 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
513 /* Bad stack swap. */
516 flush_quick_stack ();
517 decl1 = find_stack_slot (stack_pointer - 1, type1);
518 decl2 = find_stack_slot (stack_pointer - 2, type2);
519 temp = build_decl (VAR_DECL, NULL_TREE, type1);
520 java_add_local_var (temp);
521 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
522 java_add_stmt (build2 (MODIFY_EXPR, type2,
523 find_stack_slot (stack_pointer - 1, type2),
525 java_add_stmt (build2 (MODIFY_EXPR, type1,
526 find_stack_slot (stack_pointer - 2, type1),
528 stack_type_map[stack_pointer - 1] = type2;
529 stack_type_map[stack_pointer - 2] = type1;
533 java_stack_dup (int size, int offset)
535 int low_index = stack_pointer - size - offset;
538 error ("stack underflow - dup* operation");
540 flush_quick_stack ();
542 stack_pointer += size;
543 dst_index = stack_pointer;
545 for (dst_index = stack_pointer; --dst_index >= low_index; )
548 int src_index = dst_index - size;
549 if (src_index < low_index)
550 src_index = dst_index + size + offset;
551 type = stack_type_map [src_index];
552 if (type == TYPE_SECOND)
554 if (src_index <= low_index)
555 /* Dup operation splits 64-bit number. */
558 stack_type_map[dst_index] = type;
559 src_index--; dst_index--;
560 type = stack_type_map[src_index];
561 if (! TYPE_IS_WIDE (type))
564 else if (TYPE_IS_WIDE (type))
567 if (src_index != dst_index)
569 tree src_decl = find_stack_slot (src_index, type);
570 tree dst_decl = find_stack_slot (dst_index, type);
573 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
574 stack_type_map[dst_index] = type;
579 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
583 build_java_athrow (tree node)
587 call = build3 (CALL_EXPR,
589 build_address_of (throw_node),
590 build_tree_list (NULL_TREE, node),
592 TREE_SIDE_EFFECTS (call) = 1;
593 java_add_stmt (call);
594 java_stack_pop (stack_pointer);
597 /* Implementation for jsr/ret */
600 build_java_jsr (int target_pc, int return_pc)
602 tree where = lookup_label (target_pc);
603 tree ret = lookup_label (return_pc);
604 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
605 push_value (ret_label);
606 flush_quick_stack ();
607 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
609 /* Do not need to emit the label here. We noted the existence of the
610 label as a jump target in note_instructions; we'll emit the label
611 for real at the beginning of the expand_byte_code loop. */
615 build_java_ret (tree location)
617 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
620 /* Implementation of operations on array: new, load, store, length */
623 decode_newarray_type (int atype)
627 case 4: return boolean_type_node;
628 case 5: return char_type_node;
629 case 6: return float_type_node;
630 case 7: return double_type_node;
631 case 8: return byte_type_node;
632 case 9: return short_type_node;
633 case 10: return int_type_node;
634 case 11: return long_type_node;
635 default: return NULL_TREE;
639 /* Map primitive type to the code used by OPCODE_newarray. */
642 encode_newarray_type (tree type)
644 if (type == boolean_type_node)
646 else if (type == char_type_node)
648 else if (type == float_type_node)
650 else if (type == double_type_node)
652 else if (type == byte_type_node)
654 else if (type == short_type_node)
656 else if (type == int_type_node)
658 else if (type == long_type_node)
664 /* Build a call to _Jv_ThrowBadArrayIndex(), the
665 ArrayIndexOfBoundsException exception handler. */
668 build_java_throw_out_of_bounds_exception (tree index)
670 tree node = build3 (CALL_EXPR, int_type_node,
671 build_address_of (soft_badarrayindex_node),
672 build_tree_list (NULL_TREE, index), NULL_TREE);
673 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
677 /* Return the length of an array. Doesn't perform any checking on the nature
678 or value of the array NODE. May be used to implement some bytecodes. */
681 build_java_array_length_access (tree node)
683 tree type = TREE_TYPE (node);
684 tree array_type = TREE_TYPE (type);
685 HOST_WIDE_INT length;
687 /* JVM spec: If the arrayref is null, the arraylength instruction
688 throws a NullPointerException. The only way we could get a node
689 of type ptr_type_node at this point is `aconst_null; arraylength'
690 or something equivalent. */
691 if (type == ptr_type_node)
692 return build3 (CALL_EXPR, int_type_node,
693 build_address_of (soft_nullpointer_node),
694 NULL_TREE, NULL_TREE);
696 if (!is_array_type_p (type))
699 length = java_array_type_length (type);
701 return build_int_cst (NULL_TREE, length);
703 node = build3 (COMPONENT_REF, int_type_node,
704 build_java_indirect_ref (array_type, node,
705 flag_check_references),
706 lookup_field (&array_type, get_identifier ("length")),
708 IS_ARRAY_LENGTH_ACCESS (node) = 1;
712 /* Optionally checks a reference against the NULL pointer. ARG1: the
713 expr, ARG2: we should check the reference. Don't generate extra
714 checks if we're not generating code. */
717 java_check_reference (tree expr, int check)
719 if (!flag_syntax_only && check)
721 expr = save_expr (expr);
722 expr = build3 (COND_EXPR, TREE_TYPE (expr),
723 build2 (EQ_EXPR, boolean_type_node,
724 expr, null_pointer_node),
725 build3 (CALL_EXPR, void_type_node,
726 build_address_of (soft_nullpointer_node),
727 NULL_TREE, NULL_TREE),
734 /* Reference an object: just like an INDIRECT_REF, but with checking. */
737 build_java_indirect_ref (tree type, tree expr, int check)
740 t = java_check_reference (expr, check);
741 t = convert (build_pointer_type (type), t);
742 return build1 (INDIRECT_REF, type, t);
745 /* Implement array indexing (either as l-value or r-value).
746 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
747 Optionally performs bounds checking and/or test to NULL.
748 At this point, ARRAY should have been verified as an array. */
751 build_java_arrayaccess (tree array, tree type, tree index)
753 tree node, throw = NULL_TREE;
756 tree array_type = TREE_TYPE (TREE_TYPE (array));
758 if (flag_bounds_check)
761 * (unsigned jint) INDEX >= (unsigned jint) LEN
762 * && throw ArrayIndexOutOfBoundsException.
763 * Note this is equivalent to and more efficient than:
764 * INDEX < 0 || INDEX >= LEN && throw ... */
766 tree len = convert (unsigned_int_type_node,
767 build_java_array_length_access (array));
768 test = fold (build2 (GE_EXPR, boolean_type_node,
769 convert (unsigned_int_type_node, index),
771 if (! integer_zerop (test))
773 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
774 build_java_throw_out_of_bounds_exception (index));
775 /* allows expansion within COMPOUND */
776 TREE_SIDE_EFFECTS( throw ) = 1;
780 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
781 to have the bounds check evaluated first. */
782 if (throw != NULL_TREE)
783 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
785 data_field = lookup_field (&array_type, get_identifier ("data"));
787 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
788 build_java_indirect_ref (array_type, array,
789 flag_check_references),
790 data_field, NULL_TREE);
792 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
796 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
797 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
798 determine that no check is required. */
801 build_java_arraystore_check (tree array, tree object)
803 tree check, element_type, source;
804 tree array_type_p = TREE_TYPE (array);
805 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
807 if (! is_array_type_p (array_type_p))
810 /* Get the TYPE_DECL for ARRAY's element type. */
811 element_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
813 if (TREE_CODE (element_type) != TYPE_DECL
814 || TREE_CODE (object_type) != TYPE_DECL)
817 if (!flag_store_check)
818 return build1 (NOP_EXPR, array_type_p, array);
820 /* No check is needed if the element type is final or is itself an array.
821 Also check that element_type matches object_type, since in the bytecode
822 compilation case element_type may be the actual element type of the array
823 rather than its declared type. */
824 if (element_type == object_type
825 && (TYPE_ARRAY_P (TREE_TYPE (element_type))
826 || CLASS_FINAL (element_type)))
827 return build1 (NOP_EXPR, array_type_p, array);
829 /* OBJECT might be wrapped by a SAVE_EXPR. */
830 if (TREE_CODE (object) == SAVE_EXPR)
831 source = TREE_OPERAND (object, 0);
835 /* Avoid the check if OBJECT was just loaded from the same array. */
836 if (TREE_CODE (source) == ARRAY_REF)
839 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
840 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
841 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
842 if (TREE_CODE (source) == SAVE_EXPR)
843 source = TREE_OPERAND (source, 0);
846 if (TREE_CODE (target) == SAVE_EXPR)
847 target = TREE_OPERAND (target, 0);
849 if (source == target)
850 return build1 (NOP_EXPR, array_type_p, array);
853 /* Build an invocation of _Jv_CheckArrayStore */
854 check = build3 (CALL_EXPR, void_type_node,
855 build_address_of (soft_checkarraystore_node),
856 tree_cons (NULL_TREE, array,
857 build_tree_list (NULL_TREE, object)),
859 TREE_SIDE_EFFECTS (check) = 1;
864 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
865 ARRAY_NODE. This function is used to retrieve something less vague than
866 a pointer type when indexing the first dimension of something like [[<t>.
867 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
869 As a side effect, it also makes sure that ARRAY_NODE is an array. */
872 build_java_check_indexed_type (tree array_node, tree indexed_type)
876 if (!is_array_type_p (TREE_TYPE (array_node)))
879 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
881 if (indexed_type == ptr_type_node )
882 return promote_type (elt_type);
884 /* BYTE/BOOLEAN store and load are used for both type */
885 if (indexed_type == byte_type_node && elt_type == boolean_type_node )
886 return boolean_type_node;
888 if (indexed_type != elt_type )
894 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
895 called with an integer code (the type of array to create), and the length
896 of the array to create. */
899 build_newarray (int atype_value, tree length)
903 tree prim_type = decode_newarray_type (atype_value);
905 = build_java_array_type (prim_type,
906 host_integerp (length, 0) == INTEGER_CST
907 ? tree_low_cst (length, 0) : -1);
909 /* If compiling to native, pass a reference to the primitive type class
910 and save the runtime some work. However, the bytecode generator
911 expects to find the type_code int here. */
912 if (flag_emit_class_files)
913 type_arg = build_int_cst (NULL_TREE, atype_value);
915 type_arg = build_class_ref (prim_type);
917 return build3 (CALL_EXPR, promote_type (type),
918 build_address_of (soft_newarray_node),
919 tree_cons (NULL_TREE,
921 build_tree_list (NULL_TREE, length)),
925 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
929 build_anewarray (tree class_type, tree length)
932 = build_java_array_type (class_type,
933 host_integerp (length, 0)
934 ? tree_low_cst (length, 0) : -1);
936 return build3 (CALL_EXPR, promote_type (type),
937 build_address_of (soft_anewarray_node),
938 tree_cons (NULL_TREE, length,
939 tree_cons (NULL_TREE, build_class_ref (class_type),
940 build_tree_list (NULL_TREE,
941 null_pointer_node))),
945 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
948 build_new_array (tree type, tree length)
950 if (JPRIMITIVE_TYPE_P (type))
951 return build_newarray (encode_newarray_type (type), length);
953 return build_anewarray (TREE_TYPE (type), length);
956 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
957 class pointer, a number of dimensions and the matching number of
958 dimensions. The argument list is NULL terminated. */
961 expand_java_multianewarray (tree class_type, int ndim)
964 tree args = build_tree_list( NULL_TREE, null_pointer_node );
966 for( i = 0; i < ndim; i++ )
967 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
969 push_value (build3 (CALL_EXPR,
970 promote_type (class_type),
971 build_address_of (soft_multianewarray_node),
972 tree_cons (NULL_TREE, build_class_ref (class_type),
973 tree_cons (NULL_TREE,
974 build_int_cst (NULL_TREE, ndim),
979 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
980 ARRAY is an array type. May expand some bound checking and NULL
981 pointer checking. RHS_TYPE_NODE we are going to store. In the case
982 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
983 INT. In those cases, we make the conversion.
985 if ARRAy is a reference type, the assignment is checked at run-time
986 to make sure that the RHS can be assigned to the array element
987 type. It is not necessary to generate this code if ARRAY is final. */
990 expand_java_arraystore (tree rhs_type_node)
992 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
993 && TYPE_PRECISION (rhs_type_node) <= 32) ?
994 int_type_node : rhs_type_node);
995 tree index = pop_value (int_type_node);
996 tree array = pop_value (ptr_type_node);
998 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1000 flush_quick_stack ();
1002 index = save_expr (index);
1003 array = save_expr (array);
1005 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1007 tree check = build_java_arraystore_check (array, rhs_node);
1008 java_add_stmt (check);
1011 array = build_java_arrayaccess (array, rhs_type_node, index);
1012 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1015 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1016 sure that LHS is an array type. May expand some bound checking and NULL
1018 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1019 BOOLEAN/SHORT, we push a promoted type back to the stack.
1023 expand_java_arrayload (tree lhs_type_node )
1026 tree index_node = pop_value (int_type_node);
1027 tree array_node = pop_value (ptr_type_node);
1029 index_node = save_expr (index_node);
1030 array_node = save_expr (array_node);
1032 if (TREE_TYPE (array_node) == ptr_type_node)
1033 /* The only way we could get a node of type ptr_type_node at this
1034 point is `aconst_null; arraylength' or something equivalent, so
1035 unconditionally throw NullPointerException. */
1036 load_node = build3 (CALL_EXPR, lhs_type_node,
1037 build_address_of (soft_nullpointer_node),
1038 NULL_TREE, NULL_TREE);
1041 lhs_type_node = build_java_check_indexed_type (array_node, lhs_type_node);
1042 load_node = build_java_arrayaccess (array_node,
1046 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1047 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
1048 push_value (load_node);
1051 /* Expands .length. Makes sure that we deal with and array and may expand
1052 a NULL check on the array object. */
1055 expand_java_array_length (void)
1057 tree array = pop_value (ptr_type_node);
1058 tree length = build_java_array_length_access (array);
1060 push_value (length);
1063 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1064 either soft_monitorenter_node or soft_monitorexit_node. */
1067 build_java_monitor (tree call, tree object)
1069 return build3 (CALL_EXPR,
1071 build_address_of (call),
1072 build_tree_list (NULL_TREE, object),
1076 /* Emit code for one of the PUSHC instructions. */
1079 expand_java_pushc (int ival, tree type)
1082 if (type == ptr_type_node && ival == 0)
1083 value = null_pointer_node;
1084 else if (type == int_type_node || type == long_type_node)
1085 value = build_int_cst (type, ival);
1086 else if (type == float_type_node || type == double_type_node)
1089 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1090 value = build_real (type, x);
1099 expand_java_return (tree type)
1101 if (type == void_type_node)
1102 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1105 tree retval = pop_value (type);
1106 tree res = DECL_RESULT (current_function_decl);
1107 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1109 /* Handle the situation where the native integer type is smaller
1110 than the JVM integer. It can happen for many cross compilers.
1111 The whole if expression just goes away if INT_TYPE_SIZE < 32
1113 if (INT_TYPE_SIZE < 32
1114 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1115 < GET_MODE_SIZE (TYPE_MODE (type))))
1116 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1118 TREE_SIDE_EFFECTS (retval) = 1;
1119 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1124 expand_load_internal (int index, tree type, int pc)
1127 tree var = find_local_variable (index, type, pc);
1129 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1130 on the stack. If there is an assignment to this VAR_DECL between
1131 the stack push and the use, then the wrong code could be
1132 generated. To avoid this we create a new local and copy our
1133 value into it. Then we push this new local on the stack.
1134 Hopefully this all gets optimized out. */
1135 copy = build_decl (VAR_DECL, NULL_TREE, type);
1136 java_add_local_var (copy);
1137 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1143 build_address_of (tree value)
1145 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1149 class_has_finalize_method (tree type)
1151 tree super = CLASSTYPE_SUPER (type);
1153 if (super == NULL_TREE)
1154 return false; /* Every class with a real finalizer inherits */
1155 /* from java.lang.Object. */
1157 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1161 expand_java_NEW (tree type)
1165 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1166 : alloc_no_finalizer_node);
1167 if (! CLASS_LOADED_P (type))
1168 load_class (type, 1);
1169 safe_layout_class (type);
1170 push_value (build3 (CALL_EXPR, promote_type (type),
1171 build_address_of (alloc_node),
1172 build_tree_list (NULL_TREE, build_class_ref (type)),
1176 /* This returns an expression which will extract the class of an
1180 build_get_class (tree value)
1182 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1183 tree vtable_field = lookup_field (&object_type_node,
1184 get_identifier ("vtable"));
1185 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1186 build_java_indirect_ref (object_type_node, value,
1187 flag_check_references),
1188 vtable_field, NULL_TREE);
1189 return build3 (COMPONENT_REF, class_ptr_type,
1190 build1 (INDIRECT_REF, dtable_type, tmp),
1191 class_field, NULL_TREE);
1194 /* This builds the tree representation of the `instanceof' operator.
1195 It tries various tricks to optimize this in cases where types are
1199 build_instanceof (tree value, tree type)
1202 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1203 tree valtype = TREE_TYPE (TREE_TYPE (value));
1204 tree valclass = TYPE_NAME (valtype);
1207 /* When compiling from bytecode, we need to ensure that TYPE has
1209 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1211 load_class (type, 1);
1212 safe_layout_class (type);
1213 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1214 return error_mark_node;
1216 klass = TYPE_NAME (type);
1218 if (type == object_type_node || inherits_from_p (valtype, type))
1220 /* Anything except `null' is an instance of Object. Likewise,
1221 if the object is known to be an instance of the class, then
1222 we only need to check for `null'. */
1223 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1225 else if (! TYPE_ARRAY_P (type)
1226 && ! TYPE_ARRAY_P (valtype)
1227 && DECL_P (klass) && DECL_P (valclass)
1228 && ! CLASS_INTERFACE (valclass)
1229 && ! CLASS_INTERFACE (klass)
1230 && ! inherits_from_p (type, valtype)
1231 && (CLASS_FINAL (klass)
1232 || ! inherits_from_p (valtype, type)))
1234 /* The classes are from different branches of the derivation
1235 tree, so we immediately know the answer. */
1236 expr = boolean_false_node;
1238 else if (DECL_P (klass) && CLASS_FINAL (klass))
1240 tree save = save_expr (value);
1241 expr = build3 (COND_EXPR, itype,
1242 build2 (NE_EXPR, boolean_type_node,
1243 save, null_pointer_node),
1244 build2 (EQ_EXPR, itype,
1245 build_get_class (save),
1246 build_class_ref (type)),
1247 boolean_false_node);
1251 expr = build3 (CALL_EXPR, itype,
1252 build_address_of (soft_instanceof_node),
1253 tree_cons (NULL_TREE, value,
1254 build_tree_list (NULL_TREE,
1255 build_class_ref (type))),
1258 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1263 expand_java_INSTANCEOF (tree type)
1265 tree value = pop_value (object_ptr_type_node);
1266 value = build_instanceof (value, type);
1271 expand_java_CHECKCAST (tree type)
1273 tree value = pop_value (ptr_type_node);
1274 value = build3 (CALL_EXPR, promote_type (type),
1275 build_address_of (soft_checkcast_node),
1276 tree_cons (NULL_TREE, build_class_ref (type),
1277 build_tree_list (NULL_TREE, value)),
1283 expand_iinc (unsigned int local_var_index, int ival, int pc)
1285 tree local_var, res;
1286 tree constant_value;
1288 flush_quick_stack ();
1289 local_var = find_local_variable (local_var_index, int_type_node, pc);
1290 constant_value = build_int_cst (NULL_TREE, ival);
1291 res = fold (build2 (PLUS_EXPR, int_type_node, local_var, constant_value));
1292 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1293 update_aliases (local_var, local_var_index, pc);
1298 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1301 tree arg1 = convert (type, op1);
1302 tree arg2 = convert (type, op2);
1304 if (type == int_type_node)
1308 case TRUNC_DIV_EXPR:
1309 call = soft_idiv_node;
1311 case TRUNC_MOD_EXPR:
1312 call = soft_irem_node;
1318 else if (type == long_type_node)
1322 case TRUNC_DIV_EXPR:
1323 call = soft_ldiv_node;
1325 case TRUNC_MOD_EXPR:
1326 call = soft_lrem_node;
1336 call = build3 (CALL_EXPR, type,
1337 build_address_of (call),
1338 tree_cons (NULL_TREE, arg1,
1339 build_tree_list (NULL_TREE, arg2)),
1346 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1353 tree u_type = java_unsigned_type (type);
1354 arg1 = convert (u_type, arg1);
1355 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1356 return convert (type, arg1);
1360 mask = build_int_cst (NULL_TREE,
1361 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1362 arg2 = fold (build2 (BIT_AND_EXPR, int_type_node, arg2, mask));
1365 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1366 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1367 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1369 tree ifexp1 = fold (build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1370 boolean_type_node, arg1, arg2));
1371 tree ifexp2 = fold (build2 (EQ_EXPR, boolean_type_node, arg1, arg2));
1372 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1373 ifexp2, integer_zero_node,
1374 op == COMPARE_L_EXPR
1375 ? integer_minus_one_node
1376 : integer_one_node));
1377 return fold (build3 (COND_EXPR, int_type_node, ifexp1,
1378 op == COMPARE_L_EXPR ? integer_one_node
1379 : integer_minus_one_node,
1383 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1385 tree ifexp1 = fold (build2 (LT_EXPR, boolean_type_node, arg1, arg2));
1386 tree ifexp2 = fold (build2 (GT_EXPR, boolean_type_node, arg1, arg2));
1387 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1388 ifexp2, integer_one_node,
1389 integer_zero_node));
1390 return fold (build3 (COND_EXPR, int_type_node,
1391 ifexp1, integer_minus_one_node, second_compare));
1393 case TRUNC_DIV_EXPR:
1394 case TRUNC_MOD_EXPR:
1395 if (TREE_CODE (type) == REAL_TYPE
1396 && op == TRUNC_MOD_EXPR)
1399 if (type != double_type_node)
1401 arg1 = convert (double_type_node, arg1);
1402 arg2 = convert (double_type_node, arg2);
1404 call = build3 (CALL_EXPR, double_type_node,
1405 build_address_of (soft_fmod_node),
1406 tree_cons (NULL_TREE, arg1,
1407 build_tree_list (NULL_TREE, arg2)),
1409 if (type != double_type_node)
1410 call = convert (type, call);
1414 if (TREE_CODE (type) == INTEGER_TYPE
1415 && flag_use_divide_subroutine
1416 && ! flag_syntax_only)
1417 return build_java_soft_divmod (op, type, arg1, arg2);
1422 return fold (build2 (op, type, arg1, arg2));
1426 expand_java_binop (tree type, enum tree_code op)
1436 rtype = int_type_node;
1437 rarg = pop_value (rtype);
1440 rarg = pop_value (rtype);
1442 larg = pop_value (ltype);
1443 push_value (build_java_binop (op, type, larg, rarg));
1446 /* Lookup the field named NAME in *TYPEP or its super classes.
1447 If not found, return NULL_TREE.
1448 (If the *TYPEP is not found, or if the field reference is
1449 ambiguous, return error_mark_node.)
1450 If found, return the FIELD_DECL, and set *TYPEP to the
1451 class containing the field. */
1454 lookup_field (tree *typep, tree name)
1456 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1458 load_class (*typep, 1);
1459 safe_layout_class (*typep);
1460 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1461 return error_mark_node;
1465 tree field, binfo, base_binfo;
1469 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1470 if (DECL_NAME (field) == name)
1473 /* Process implemented interfaces. */
1474 save_field = NULL_TREE;
1475 for (binfo = TYPE_BINFO (*typep), i = 0;
1476 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1478 tree t = BINFO_TYPE (base_binfo);
1479 if ((field = lookup_field (&t, name)))
1481 if (save_field == field)
1483 if (save_field == NULL_TREE)
1487 tree i1 = DECL_CONTEXT (save_field);
1488 tree i2 = DECL_CONTEXT (field);
1489 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1490 IDENTIFIER_POINTER (name),
1491 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1492 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1493 return error_mark_node;
1498 if (save_field != NULL_TREE)
1501 *typep = CLASSTYPE_SUPER (*typep);
1506 /* Look up the field named NAME in object SELF_VALUE,
1507 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1508 SELF_VALUE is NULL_TREE if looking for a static field. */
1511 build_field_ref (tree self_value, tree self_class, tree name)
1513 tree base_class = self_class;
1514 tree field_decl = lookup_field (&base_class, name);
1515 if (field_decl == NULL_TREE)
1517 error ("field %qs not found", IDENTIFIER_POINTER (name));
1518 return error_mark_node;
1520 if (self_value == NULL_TREE)
1522 return build_static_field_ref (field_decl);
1526 int check = (flag_check_references
1527 && ! (DECL_P (self_value)
1528 && DECL_NAME (self_value) == this_identifier_node));
1530 tree base_type = promote_type (base_class);
1531 if (base_type != TREE_TYPE (self_value))
1532 self_value = fold (build1 (NOP_EXPR, base_type, self_value));
1533 if (flag_indirect_dispatch
1534 && output_class != self_class)
1535 /* FIXME: output_class != self_class is not exactly the right
1536 test. What we really want to know is whether self_class is
1537 in the same translation unit as output_class. If it is,
1538 we can make a direct reference. */
1541 = build_int_cst (NULL_TREE, get_symbol_table_index
1542 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1544 = build4 (ARRAY_REF, integer_type_node,
1545 TYPE_OTABLE_DECL (output_class), otable_index,
1546 NULL_TREE, NULL_TREE);
1549 field_offset = fold (convert (sizetype, field_offset));
1551 = fold (build2 (PLUS_EXPR,
1552 build_pointer_type (TREE_TYPE (field_decl)),
1553 self_value, field_offset));
1554 return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
1557 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1559 return fold (build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1560 self_value, field_decl, NULL_TREE));
1565 lookup_label (int pc)
1569 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1570 name = get_identifier (buf);
1571 if (IDENTIFIER_LOCAL_VALUE (name))
1572 return IDENTIFIER_LOCAL_VALUE (name);
1575 /* The type of the address of a label is return_address_type_node. */
1576 tree decl = create_label_decl (name);
1577 LABEL_PC (decl) = pc;
1578 return pushdecl (decl);
1582 /* Generate a unique name for the purpose of loops and switches
1583 labels, and try-catch-finally blocks label or temporary variables. */
1586 generate_name (void)
1588 static int l_number = 0;
1590 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1592 return get_identifier (buff);
1596 create_label_decl (tree name)
1599 decl = build_decl (LABEL_DECL, name,
1600 TREE_TYPE (return_address_type_node));
1601 DECL_CONTEXT (decl) = current_function_decl;
1602 DECL_IGNORED_P (decl) = 1;
1606 /* This maps a bytecode offset (PC) to various flags. */
1607 char *instruction_bits;
1610 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1612 lookup_label (target_pc);
1613 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1616 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1617 where CONDITION is one of one the compare operators. */
1620 expand_compare (enum tree_code condition, tree value1, tree value2,
1623 tree target = lookup_label (target_pc);
1624 tree cond = fold (build2 (condition, boolean_type_node, value1, value2));
1626 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1627 build1 (GOTO_EXPR, void_type_node, target),
1628 build_java_empty_stmt ()));
1631 /* Emit code for a TEST-type opcode. */
1634 expand_test (enum tree_code condition, tree type, int target_pc)
1636 tree value1, value2;
1637 flush_quick_stack ();
1638 value1 = pop_value (type);
1639 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1640 expand_compare (condition, value1, value2, target_pc);
1643 /* Emit code for a COND-type opcode. */
1646 expand_cond (enum tree_code condition, tree type, int target_pc)
1648 tree value1, value2;
1649 flush_quick_stack ();
1650 /* note: pop values in opposite order */
1651 value2 = pop_value (type);
1652 value1 = pop_value (type);
1653 /* Maybe should check value1 and value2 for type compatibility ??? */
1654 expand_compare (condition, value1, value2, target_pc);
1658 expand_java_goto (int target_pc)
1660 tree target_label = lookup_label (target_pc);
1661 flush_quick_stack ();
1662 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1666 expand_java_switch (tree selector, int default_pc)
1668 tree switch_expr, x;
1670 flush_quick_stack ();
1671 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1672 NULL_TREE, NULL_TREE);
1673 java_add_stmt (switch_expr);
1675 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1676 create_artificial_label ());
1677 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1679 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1680 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1686 expand_java_add_case (tree switch_expr, int match, int target_pc)
1690 value = build_int_cst (TREE_TYPE (switch_expr), match);
1692 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1693 create_artificial_label ());
1694 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1696 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1697 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1702 expand_java_call (int target_pc, int return_address)
1704 tree target_label = lookup_label (target_pc);
1705 tree value = build_int_cst (NULL_TREE, return_address);
1707 flush_quick_stack ();
1708 expand_goto (target_label);
1712 expand_java_ret (tree return_address ATTRIBUTE_UNUSED)
1714 warning ("ret instruction not implemented");
1716 tree target_label = lookup_label (target_pc);
1717 flush_quick_stack ();
1718 expand_goto (target_label);
1724 pop_arguments (tree arg_types)
1726 if (arg_types == end_params_node)
1728 if (TREE_CODE (arg_types) == TREE_LIST)
1730 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1731 tree type = TREE_VALUE (arg_types);
1732 tree arg = pop_value (type);
1733 if (targetm.calls.promote_prototypes (type)
1734 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1735 && INTEGRAL_TYPE_P (type))
1736 arg = convert (integer_type_node, arg);
1737 return tree_cons (NULL_TREE, arg, tail);
1742 /* Build an expression to initialize the class CLAS.
1743 if EXPR is non-NULL, returns an expression to first call the initializer
1744 (if it is needed) and then calls EXPR. */
1747 build_class_init (tree clas, tree expr)
1751 /* An optimization: if CLAS is a superclass of the class we're
1752 compiling, we don't need to initialize it. However, if CLAS is
1753 an interface, it won't necessarily be initialized, even if we
1755 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1756 && inherits_from_p (current_class, clas))
1757 || current_class == clas)
1760 if (always_initialize_class_p)
1762 init = build3 (CALL_EXPR, void_type_node,
1763 build_address_of (soft_initclass_node),
1764 build_tree_list (NULL_TREE, build_class_ref (clas)),
1766 TREE_SIDE_EFFECTS (init) = 1;
1770 tree *init_test_decl;
1771 init_test_decl = java_treetreehash_new
1772 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1774 if (*init_test_decl == NULL)
1776 /* Build a declaration and mark it as a flag used to track
1777 static class initializations. */
1778 *init_test_decl = build_decl (VAR_DECL, NULL_TREE,
1780 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (*init_test_decl);
1781 LOCAL_CLASS_INITIALIZATION_FLAG (*init_test_decl) = 1;
1782 DECL_CONTEXT (*init_test_decl) = current_function_decl;
1783 DECL_FUNCTION_INIT_TEST_CLASS (*init_test_decl) = clas;
1784 /* Tell the check-init code to ignore this decl when not
1785 optimizing class initialization. */
1786 if (!STATIC_CLASS_INIT_OPT_P ())
1787 DECL_BIT_INDEX(*init_test_decl) = -1;
1788 DECL_INITIAL (*init_test_decl) = integer_zero_node;
1789 /* Don't emit any symbolic debugging info for this decl. */
1790 DECL_IGNORED_P (*init_test_decl) = 1;
1793 init = build3 (CALL_EXPR, void_type_node,
1794 build_address_of (soft_initclass_node),
1795 build_tree_list (NULL_TREE, build_class_ref (clas)),
1797 TREE_SIDE_EFFECTS (init) = 1;
1798 init = build3 (COND_EXPR, void_type_node,
1799 build2 (EQ_EXPR, boolean_type_node,
1800 *init_test_decl, boolean_false_node),
1801 init, integer_zero_node);
1802 TREE_SIDE_EFFECTS (init) = 1;
1803 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
1804 build2 (MODIFY_EXPR, boolean_type_node,
1805 *init_test_decl, boolean_true_node));
1806 TREE_SIDE_EFFECTS (init) = 1;
1809 if (expr != NULL_TREE)
1811 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1812 TREE_SIDE_EFFECTS (expr) = 1;
1819 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
1820 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
1821 tree arg_list ATTRIBUTE_UNUSED)
1824 if (is_compiled_class (self_type))
1826 if (!flag_indirect_dispatch
1827 || (!TREE_PUBLIC (method) && DECL_CONTEXT (method)))
1829 make_decl_rtl (method);
1830 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
1836 = build_int_cst (NULL_TREE, get_symbol_table_index
1837 (method, &TYPE_ATABLE_METHODS (output_class)));
1838 func = build4 (ARRAY_REF, method_ptr_type_node,
1839 TYPE_ATABLE_DECL (output_class), table_index,
1840 NULL_TREE, NULL_TREE);
1842 func = convert (method_ptr_type_node, func);
1846 /* We don't know whether the method has been (statically) compiled.
1847 Compile this code to get a reference to the method's code:
1849 SELF_TYPE->methods[METHOD_INDEX].ncode
1853 int method_index = 0;
1856 /* The method might actually be declared in some superclass, so
1857 we have to use its class context, not the caller's notion of
1858 where the method is. */
1859 self_type = DECL_CONTEXT (method);
1860 ref = build_class_ref (self_type);
1861 ref = build1 (INDIRECT_REF, class_type_node, ref);
1862 if (ncode_ident == NULL_TREE)
1863 ncode_ident = get_identifier ("ncode");
1864 if (methods_ident == NULL_TREE)
1865 methods_ident = get_identifier ("methods");
1866 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
1867 lookup_field (&class_type_node, methods_ident),
1869 for (meth = TYPE_METHODS (self_type);
1870 ; meth = TREE_CHAIN (meth))
1874 if (meth == NULL_TREE)
1875 fatal_error ("method '%s' not found in class",
1876 IDENTIFIER_POINTER (DECL_NAME (method)));
1879 method_index *= int_size_in_bytes (method_type_node);
1880 ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
1881 ref, build_int_cst (NULL_TREE, method_index)));
1882 ref = build1 (INDIRECT_REF, method_type_node, ref);
1883 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
1884 ref, lookup_field (&method_type_node, ncode_ident),
1891 invoke_build_dtable (int is_invoke_interface, tree arg_list)
1893 tree dtable, objectref;
1895 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
1897 /* If we're dealing with interfaces and if the objectref
1898 argument is an array then get the dispatch table of the class
1899 Object rather than the one from the objectref. */
1900 objectref = (is_invoke_interface
1901 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list))) ?
1902 object_type_node : TREE_VALUE (arg_list));
1904 if (dtable_ident == NULL_TREE)
1905 dtable_ident = get_identifier ("vtable");
1906 dtable = build_java_indirect_ref (object_type_node, objectref,
1907 flag_check_references);
1908 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
1909 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
1914 /* Determine the index in SYMBOL_TABLE for a reference to the decl
1915 T. If this decl has not been seen before, it will be added to the
1916 otable_methods. If it has, the existing table slot will be
1920 get_symbol_table_index (tree t, tree *symbol_table)
1925 if (*symbol_table == NULL_TREE)
1927 *symbol_table = build_tree_list (t, t);
1931 method_list = *symbol_table;
1935 tree value = TREE_VALUE (method_list);
1939 if (TREE_CHAIN (method_list) == NULL_TREE)
1942 method_list = TREE_CHAIN (method_list);
1945 TREE_CHAIN (method_list) = build_tree_list (t, t);
1950 build_invokevirtual (tree dtable, tree method)
1953 tree nativecode_ptr_ptr_type_node
1954 = build_pointer_type (nativecode_ptr_type_node);
1958 if (flag_indirect_dispatch)
1961 = build_int_cst (NULL_TREE, get_symbol_table_index
1962 (method, &TYPE_OTABLE_METHODS (output_class)));
1963 method_index = build4 (ARRAY_REF, integer_type_node,
1964 TYPE_OTABLE_DECL (output_class),
1965 otable_index, NULL_TREE, NULL_TREE);
1969 /* We fetch the DECL_VINDEX field directly here, rather than
1970 using get_method_index(). DECL_VINDEX is the true offset
1971 from the vtable base to a method, regrdless of any extra
1972 words inserted at the start of the vtable. */
1973 method_index = DECL_VINDEX (method);
1974 method_index = size_binop (MULT_EXPR, method_index,
1975 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
1976 if (TARGET_VTABLE_USES_DESCRIPTORS)
1977 method_index = size_binop (MULT_EXPR, method_index,
1978 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
1981 func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
1982 convert (nativecode_ptr_ptr_type_node, method_index)));
1984 if (TARGET_VTABLE_USES_DESCRIPTORS)
1985 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
1987 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
1992 static GTY(()) tree class_ident;
1994 build_invokeinterface (tree dtable, tree method)
2001 /* We expand invokeinterface here. _Jv_LookupInterfaceMethod() will
2002 ensure that the selected method exists, is public and not
2003 abstract nor static. */
2005 if (class_ident == NULL_TREE)
2006 class_ident = get_identifier ("class");
2008 dtable = build_java_indirect_ref (dtable_type, dtable,
2009 flag_check_references);
2010 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2011 lookup_field (&dtable_type, class_ident), NULL_TREE);
2013 interface = DECL_CONTEXT (method);
2014 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2016 layout_class_methods (interface);
2018 if (flag_indirect_dispatch)
2021 = build_int_cst (NULL_TREE, get_symbol_table_index
2022 (method, &TYPE_OTABLE_METHODS (output_class)));
2023 idx = build4 (ARRAY_REF, integer_type_node,
2024 TYPE_OTABLE_DECL (output_class), otable_index,
2025 NULL_TREE, NULL_TREE);
2028 idx = build_int_cst (NULL_TREE,
2029 get_interface_method_index (method, interface));
2031 lookup_arg = tree_cons (NULL_TREE, dtable,
2032 tree_cons (NULL_TREE, build_class_ref (interface),
2033 build_tree_list (NULL_TREE, idx)));
2035 return build3 (CALL_EXPR, ptr_type_node,
2036 build_address_of (soft_lookupinterfacemethod_node),
2037 lookup_arg, NULL_TREE);
2040 /* Expand one of the invoke_* opcodes.
2041 OCPODE is the specific opcode.
2042 METHOD_REF_INDEX is an index into the constant pool.
2043 NARGS is the number of arguments, or -1 if not specified. */
2046 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2048 tree method_signature
2049 = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
2050 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool, method_ref_index);
2052 = get_class_constant (current_jcf,
2053 COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool,
2055 const char *const self_name
2056 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2057 tree call, func, method, arg_list, method_type;
2058 tree check = NULL_TREE;
2060 if (! CLASS_LOADED_P (self_type))
2062 load_class (self_type, 1);
2063 safe_layout_class (self_type);
2064 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2065 fatal_error ("failed to find class '%s'", self_name);
2067 layout_class_methods (self_type);
2069 if (ID_INIT_P (method_name))
2070 method = lookup_java_constructor (self_type, method_signature);
2072 method = lookup_java_method (self_type, method_name, method_signature);
2073 if (method == NULL_TREE)
2075 error ("class '%s' has no method named '%s' matching signature '%s'",
2077 IDENTIFIER_POINTER (method_name),
2078 IDENTIFIER_POINTER (method_signature));
2080 /* Invoke static can't invoke static/abstract method */
2081 else if (opcode == OPCODE_invokestatic)
2083 if (!METHOD_STATIC (method))
2085 error ("invokestatic on non static method");
2088 else if (METHOD_ABSTRACT (method))
2090 error ("invokestatic on abstract method");
2096 if (METHOD_STATIC (method))
2098 error ("invoke[non-static] on static method");
2103 if (method == NULL_TREE)
2105 method_type = get_type_from_signature (method_signature);
2106 pop_arguments (TYPE_ARG_TYPES (method_type));
2107 if (opcode != OPCODE_invokestatic)
2108 pop_type (self_type);
2109 method_type = promote_type (TREE_TYPE (method_type));
2110 push_value (convert (method_type, integer_zero_node));
2114 method_type = TREE_TYPE (method);
2115 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2116 flush_quick_stack ();
2119 if (opcode == OPCODE_invokestatic)
2120 func = build_known_method_ref (method, method_type, self_type,
2121 method_signature, arg_list);
2122 else if (opcode == OPCODE_invokespecial
2123 || (opcode == OPCODE_invokevirtual
2124 && (METHOD_PRIVATE (method)
2125 || METHOD_FINAL (method)
2126 || CLASS_FINAL (TYPE_NAME (self_type)))))
2128 /* If the object for the method call is null, we throw an
2129 exception. We don't do this if the object is the current
2130 method's `this'. In other cases we just rely on an
2131 optimization pass to eliminate redundant checks. FIXME:
2132 Unfortunately there doesn't seem to be a way to determine
2133 what the current method is right now.
2134 We do omit the check if we're calling <init>. */
2135 /* We use a SAVE_EXPR here to make sure we only evaluate
2136 the new `self' expression once. */
2137 tree save_arg = save_expr (TREE_VALUE (arg_list));
2138 TREE_VALUE (arg_list) = save_arg;
2139 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2140 func = build_known_method_ref (method, method_type, self_type,
2141 method_signature, arg_list);
2145 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2147 if (opcode == OPCODE_invokevirtual)
2148 func = build_invokevirtual (dtable, method);
2150 func = build_invokeinterface (dtable, method);
2153 if (TREE_CODE (func) == ADDR_EXPR)
2154 TREE_TYPE (func) = build_pointer_type (method_type);
2156 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2158 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2159 func, arg_list, NULL_TREE);
2160 TREE_SIDE_EFFECTS (call) = 1;
2161 call = check_for_builtin (method, call);
2163 if (check != NULL_TREE)
2165 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2166 TREE_SIDE_EFFECTS (call) = 1;
2169 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2170 java_add_stmt (call);
2174 flush_quick_stack ();
2178 /* Create a stub which will be put into the vtable but which will call
2182 build_jni_stub (tree method)
2184 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2185 tree jni_func_type, tem;
2186 tree env_var, res_var = NULL_TREE, block;
2187 tree method_args, res_type;
2193 tree klass = DECL_CONTEXT (method);
2194 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2195 klass = build_class_ref (klass);
2197 if (! METHOD_NATIVE (method) || ! flag_jni)
2200 DECL_ARTIFICIAL (method) = 1;
2201 DECL_EXTERNAL (method) = 0;
2203 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2204 DECL_CONTEXT (env_var) = method;
2206 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2208 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2209 TREE_TYPE (TREE_TYPE (method)));
2210 DECL_CONTEXT (res_var) = method;
2211 TREE_CHAIN (env_var) = res_var;
2214 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2215 TREE_STATIC (meth_var) = 1;
2216 TREE_PUBLIC (meth_var) = 0;
2217 DECL_EXTERNAL (meth_var) = 0;
2218 DECL_CONTEXT (meth_var) = method;
2219 DECL_ARTIFICIAL (meth_var) = 1;
2220 DECL_INITIAL (meth_var) = null_pointer_node;
2221 TREE_USED (meth_var) = 1;
2222 chainon (env_var, meth_var);
2223 build_result_decl (method);
2225 /* One strange way that the front ends are different is that they
2226 store arguments differently. */
2228 method_args = DECL_ARGUMENTS (method);
2230 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2231 block = build_block (env_var, NULL_TREE, NULL_TREE,
2232 method_args, NULL_TREE);
2233 TREE_SIDE_EFFECTS (block) = 1;
2234 /* When compiling from source we don't set the type of the block,
2235 because that will prevent patch_return from ever being run. */
2237 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2239 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2240 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2241 build3 (CALL_EXPR, ptr_type_node,
2242 build_address_of (soft_getjnienvnewframe_node),
2243 build_tree_list (NULL_TREE, klass),
2245 CAN_COMPLETE_NORMALLY (body) = 1;
2247 /* All the arguments to this method become arguments to the
2248 underlying JNI function. If we had to wrap object arguments in a
2249 special way, we would do that here. */
2251 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2253 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (tem)));
2254 #ifdef PARM_BOUNDARY
2255 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2258 args_size += (arg_bits / BITS_PER_UNIT);
2260 args = tree_cons (NULL_TREE, tem, args);
2262 args = nreverse (args);
2263 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2265 /* For a static method the second argument is the class. For a
2266 non-static method the second argument is `this'; that is already
2267 available in the argument list. */
2268 if (METHOD_STATIC (method))
2270 args_size += int_size_in_bytes (TREE_TYPE (klass));
2271 args = tree_cons (NULL_TREE, klass, args);
2272 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2275 /* The JNIEnv structure is the first argument to the JNI function. */
2276 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2277 args = tree_cons (NULL_TREE, env_var, args);
2278 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2280 /* We call _Jv_LookupJNIMethod to find the actual underlying
2281 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2282 exception if this function is not found at runtime. */
2283 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2284 method_sig = build_java_signature (TREE_TYPE (method));
2285 lookup_arg = tree_cons (NULL_TREE,
2286 build_utf8_ref (unmangle_classname
2287 (IDENTIFIER_POINTER (method_sig),
2288 IDENTIFIER_LENGTH (method_sig))),
2290 tem = DECL_NAME (method);
2292 = tree_cons (NULL_TREE, klass,
2293 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2295 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2297 #ifdef MODIFY_JNI_METHOD_CALL
2298 tem = MODIFY_JNI_METHOD_CALL (tem);
2301 jni_func_type = build_pointer_type (tem);
2303 jnifunc = build3 (COND_EXPR, ptr_type_node,
2305 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2306 build3 (CALL_EXPR, ptr_type_node,
2308 (soft_lookupjnimethod_node),
2309 lookup_arg, NULL_TREE)));
2311 /* Now we make the actual JNI call via the resulting function
2313 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2314 build1 (NOP_EXPR, jni_func_type, jnifunc),
2317 /* If the JNI call returned a result, capture it here. If we had to
2318 unwrap JNI object results, we would do that here. */
2319 if (res_var != NULL_TREE)
2320 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2323 TREE_SIDE_EFFECTS (call) = 1;
2324 CAN_COMPLETE_NORMALLY (call) = 1;
2326 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2327 TREE_SIDE_EFFECTS (body) = 1;
2329 /* Now free the environment we allocated. */
2330 call = build3 (CALL_EXPR, ptr_type_node,
2331 build_address_of (soft_jnipopsystemframe_node),
2332 build_tree_list (NULL_TREE, env_var),
2334 TREE_SIDE_EFFECTS (call) = 1;
2335 CAN_COMPLETE_NORMALLY (call) = 1;
2336 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2337 TREE_SIDE_EFFECTS (body) = 1;
2339 /* Finally, do the return. */
2340 res_type = void_type_node;
2341 if (res_var != NULL_TREE)
2344 if (! DECL_RESULT (method))
2346 /* Make sure we copy the result variable to the actual
2347 result. We use the type of the DECL_RESULT because it
2348 might be different from the return type of the function:
2349 it might be promoted. */
2350 drt = TREE_TYPE (DECL_RESULT (method));
2351 if (drt != TREE_TYPE (res_var))
2352 res_var = build1 (CONVERT_EXPR, drt, res_var);
2353 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2354 TREE_SIDE_EFFECTS (res_var) = 1;
2357 body = build2 (COMPOUND_EXPR, void_type_node, body,
2358 build1 (RETURN_EXPR, res_type, res_var));
2359 TREE_SIDE_EFFECTS (body) = 1;
2361 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2366 /* Expand an operation to extract from or store into a field.
2367 IS_STATIC is 1 iff the field is static.
2368 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2369 FIELD_REF_INDEX is an index into the constant pool. */
2372 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2375 = get_class_constant (current_jcf,
2376 COMPONENT_REF_CLASS_INDEX (¤t_jcf->cpool,
2378 const char *self_name
2379 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2380 tree field_name = COMPONENT_REF_NAME (¤t_jcf->cpool, field_ref_index);
2381 tree field_signature = COMPONENT_REF_SIGNATURE (¤t_jcf->cpool,
2383 tree field_type = get_type_from_signature (field_signature);
2384 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2389 if (! CLASS_LOADED_P (self_type))
2390 load_class (self_type, 1);
2391 field_decl = lookup_field (&self_type, field_name);
2392 if (field_decl == error_mark_node)
2396 else if (field_decl == NULL_TREE)
2398 error ("missing field '%s' in '%s'",
2399 IDENTIFIER_POINTER (field_name), self_name);
2402 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2404 error ("mismatching signature for field '%s' in '%s'",
2405 IDENTIFIER_POINTER (field_name), self_name);
2408 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2412 push_value (convert (field_type, integer_zero_node));
2413 flush_quick_stack ();
2417 field_ref = build_field_ref (field_ref, self_type, field_name);
2419 field_ref = build_class_init (self_type, field_ref);
2422 flush_quick_stack ();
2423 if (FIELD_FINAL (field_decl))
2425 if (DECL_CONTEXT (field_decl) != current_class)
2426 error ("%Jassignment to final field '%D' not in field's class",
2427 field_decl, field_decl);
2428 else if (FIELD_STATIC (field_decl))
2430 if (!DECL_CLINIT_P (current_function_decl))
2431 warning ("%Jassignment to final static field %qD not in "
2432 "class initializer",
2433 field_decl, field_decl);
2437 tree cfndecl_name = DECL_NAME (current_function_decl);
2438 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2439 && !ID_FINIT_P (cfndecl_name))
2440 warning ("%Jassignment to final field '%D' not in constructor",
2441 field_decl, field_decl);
2444 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2445 field_ref, new_value));
2448 push_value (field_ref);
2452 load_type_state (tree label)
2455 tree vec = LABEL_TYPE_STATE (label);
2456 int cur_length = TREE_VEC_LENGTH (vec);
2457 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2458 for (i = 0; i < cur_length; i++)
2459 type_map [i] = TREE_VEC_ELT (vec, i);
2462 /* Go over METHOD's bytecode and note instruction starts in
2463 instruction_bits[]. */
2466 note_instructions (JCF *jcf, tree method)
2469 unsigned char* byte_ops;
2470 long length = DECL_CODE_LENGTH (method);
2475 #undef RET /* Defined by config/i386/i386.h */
2477 #define BCODE byte_ops
2478 #define BYTE_type_node byte_type_node
2479 #define SHORT_type_node short_type_node
2480 #define INT_type_node int_type_node
2481 #define LONG_type_node long_type_node
2482 #define CHAR_type_node char_type_node
2483 #define PTR_type_node ptr_type_node
2484 #define FLOAT_type_node float_type_node
2485 #define DOUBLE_type_node double_type_node
2486 #define VOID_type_node void_type_node
2487 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2488 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2489 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2490 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2492 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2494 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2495 byte_ops = jcf->read_ptr;
2496 instruction_bits = xrealloc (instruction_bits, length + 1);
2497 memset (instruction_bits, 0, length + 1);
2499 /* This pass figures out which PC can be the targets of jumps. */
2500 for (PC = 0; PC < length;)
2502 int oldpc = PC; /* PC at instruction start. */
2503 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2504 switch (byte_ops[PC++])
2506 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2508 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2511 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2513 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2514 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2515 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2516 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2517 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2518 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2519 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2520 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2522 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2523 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2524 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2525 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2526 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2527 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2528 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2529 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2531 /* two forms of wide instructions */
2532 #define PRE_SPECIAL_WIDE(IGNORE) \
2534 int modified_opcode = IMMEDIATE_u1; \
2535 if (modified_opcode == OPCODE_iinc) \
2537 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2538 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2542 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2546 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2548 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2550 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2551 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2552 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2553 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2554 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2555 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2556 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2557 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2558 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2559 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2561 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2562 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2563 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2564 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2565 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2566 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2567 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2569 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2571 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2573 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2574 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2576 #define PRE_LOOKUP_SWITCH \
2577 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2578 NOTE_LABEL (default_offset+oldpc); \
2580 while (--npairs >= 0) { \
2581 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2582 jint offset = IMMEDIATE_s4; \
2583 NOTE_LABEL (offset+oldpc); } \
2586 #define PRE_TABLE_SWITCH \
2587 { jint default_offset = IMMEDIATE_s4; \
2588 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2589 NOTE_LABEL (default_offset+oldpc); \
2591 while (low++ <= high) { \
2592 jint offset = IMMEDIATE_s4; \
2593 NOTE_LABEL (offset+oldpc); } \
2596 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2597 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2598 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2599 (void)(IMMEDIATE_u2); \
2600 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2602 #include "javaop.def"
2609 expand_byte_code (JCF *jcf, tree method)
2613 const unsigned char *linenumber_pointer;
2614 int dead_code_index = -1;
2615 unsigned char* byte_ops;
2616 long length = DECL_CODE_LENGTH (method);
2619 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2620 byte_ops = jcf->read_ptr;
2622 /* We make an initial pass of the line number table, to note
2623 which instructions have associated line number entries. */
2624 linenumber_pointer = linenumber_table;
2625 for (i = 0; i < linenumber_count; i++)
2627 int pc = GET_u2 (linenumber_pointer);
2628 linenumber_pointer += 4;
2630 warning ("invalid PC in line number table");
2633 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2634 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2635 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2639 if (! verify_jvm_instructions (jcf, byte_ops, length))
2642 /* Translate bytecodes. */
2643 linenumber_pointer = linenumber_table;
2644 for (PC = 0; PC < length;)
2646 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2648 tree label = lookup_label (PC);
2649 flush_quick_stack ();
2650 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2651 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2652 if (LABEL_VERIFIED (label) || PC == 0)
2653 load_type_state (label);
2656 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2658 if (dead_code_index == -1)
2660 /* This is the start of a region of unreachable bytecodes.
2661 They still need to be processed in order for EH ranges
2662 to get handled correctly. However, we can simply
2663 replace these bytecodes with nops. */
2664 dead_code_index = PC;
2667 /* Turn this bytecode into a nop. */
2672 if (dead_code_index != -1)
2674 /* We've just reached the end of a region of dead code. */
2676 warning ("unreachable bytecode from %d to before %d",
2677 dead_code_index, PC);
2678 dead_code_index = -1;
2682 /* Handle possible line number entry for this PC.
2684 This code handles out-of-order and multiple linenumbers per PC,
2685 but is optimized for the case of line numbers increasing
2686 monotonically with PC. */
2687 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2689 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2690 || GET_u2 (linenumber_pointer) != PC)
2691 linenumber_pointer = linenumber_table;
2692 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2694 int pc = GET_u2 (linenumber_pointer);
2695 linenumber_pointer += 4;
2698 int line = GET_u2 (linenumber_pointer - 2);
2699 #ifdef USE_MAPPED_LOCATION
2700 input_location = linemap_line_start (&line_table, line, 1);
2702 input_location.line = line;
2704 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2709 maybe_pushlevels (PC);
2710 PC = process_jvm_instruction (PC, byte_ops, length);
2711 maybe_poplevels (PC);
2714 if (dead_code_index != -1)
2716 /* We've just reached the end of a region of dead code. */
2718 warning ("unreachable bytecode from %d to the end of the method",
2724 java_push_constant_from_pool (JCF *jcf, int index)
2727 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
2730 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
2731 index = alloc_name_constant (CONSTANT_String, name);
2732 c = build_ref_from_constant_pool (index);
2733 c = convert (promote_type (string_type_node), c);
2736 c = get_constant (jcf, index);
2741 process_jvm_instruction (int PC, const unsigned char* byte_ops,
2742 long length ATTRIBUTE_UNUSED)
2744 const char *opname; /* Temporary ??? */
2745 int oldpc = PC; /* PC at instruction start. */
2747 /* If the instruction is at the beginning of a exception handler,
2748 replace the top of the stack with the thrown object reference */
2749 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
2751 tree type = pop_type (ptr_type_node);
2752 push_value (build_exception_object_ref (type));
2755 switch (byte_ops[PC++])
2757 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2760 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2763 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
2765 int saw_index = 0; \
2766 int index = OPERAND_VALUE; \
2768 (find_local_variable (index, return_address_type_node, oldpc)); \
2771 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
2773 /* OPERAND_VALUE may have side-effects on PC */ \
2774 int opvalue = OPERAND_VALUE; \
2775 build_java_jsr (oldpc + opvalue, PC); \
2778 /* Push a constant onto the stack. */
2779 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
2780 { int saw_index = 0; int ival = (OPERAND_VALUE); \
2781 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
2782 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
2784 /* internal macro added for use by the WIDE case */
2785 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
2786 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
2788 /* Push local variable onto the opcode stack. */
2789 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
2791 /* have to do this since OPERAND_VALUE may have side-effects */ \
2792 int opvalue = OPERAND_VALUE; \
2793 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2796 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
2797 expand_java_return (OPERAND_TYPE##_type_node)
2799 #define REM_EXPR TRUNC_MOD_EXPR
2800 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
2801 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
2803 #define FIELD(IS_STATIC, IS_PUT) \
2804 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
2806 #define TEST(OPERAND_TYPE, CONDITION) \
2807 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2809 #define COND(OPERAND_TYPE, CONDITION) \
2810 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2812 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2813 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
2815 #define BRANCH_GOTO(OPERAND_VALUE) \
2816 expand_java_goto (oldpc + OPERAND_VALUE)
2818 #define BRANCH_CALL(OPERAND_VALUE) \
2819 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
2822 #define BRANCH_RETURN(OPERAND_VALUE) \
2824 tree type = OPERAND_TYPE##_type_node; \
2825 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
2826 expand_java_ret (value); \
2830 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
2831 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2832 fprintf (stderr, "(not implemented)\n")
2833 #define NOT_IMPL1(OPERAND_VALUE) \
2834 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2835 fprintf (stderr, "(not implemented)\n")
2837 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
2839 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
2841 #define STACK_POP(COUNT) java_stack_pop (COUNT)
2843 #define STACK_SWAP(COUNT) java_stack_swap()
2845 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
2846 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
2847 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
2849 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2850 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
2852 #define LOOKUP_SWITCH \
2853 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2854 tree selector = pop_value (INT_type_node); \
2855 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
2856 while (--npairs >= 0) \
2858 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
2859 expand_java_add_case (switch_expr, match, oldpc + offset); \
2863 #define TABLE_SWITCH \
2864 { jint default_offset = IMMEDIATE_s4; \
2865 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2866 tree selector = pop_value (INT_type_node); \
2867 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
2868 for (; low <= high; low++) \
2870 jint offset = IMMEDIATE_s4; \
2871 expand_java_add_case (switch_expr, low, oldpc + offset); \
2875 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2876 { int opcode = byte_ops[PC-1]; \
2877 int method_ref_index = IMMEDIATE_u2; \
2879 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
2881 expand_invoke (opcode, method_ref_index, nargs); \
2884 /* Handle new, checkcast, instanceof */
2885 #define OBJECT(TYPE, OP) \
2886 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
2888 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
2890 #define ARRAY_LOAD(OPERAND_TYPE) \
2892 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
2895 #define ARRAY_STORE(OPERAND_TYPE) \
2897 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
2900 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
2901 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
2902 #define ARRAY_NEW_PTR() \
2903 push_value (build_anewarray (get_class_constant (current_jcf, \
2905 pop_value (int_type_node)));
2906 #define ARRAY_NEW_NUM() \
2908 int atype = IMMEDIATE_u1; \
2909 push_value (build_newarray (atype, pop_value (int_type_node)));\
2911 #define ARRAY_NEW_MULTI() \
2913 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
2914 int ndims = IMMEDIATE_u1; \
2915 expand_java_multianewarray( class, ndims ); \
2918 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
2919 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
2920 pop_value (OPERAND_TYPE##_type_node))));
2922 #define CONVERT2(FROM_TYPE, TO_TYPE) \
2924 push_value (build1 (NOP_EXPR, int_type_node, \
2925 (convert (TO_TYPE##_type_node, \
2926 pop_value (FROM_TYPE##_type_node))))); \
2929 #define CONVERT(FROM_TYPE, TO_TYPE) \
2931 push_value (convert (TO_TYPE##_type_node, \
2932 pop_value (FROM_TYPE##_type_node))); \
2935 /* internal macro added for use by the WIDE case
2936 Added TREE_TYPE (decl) assignment, apbianco */
2937 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
2940 int index = OPVALUE; \
2941 tree type = OPTYPE; \
2942 value = pop_value (type); \
2943 type = TREE_TYPE (value); \
2944 decl = find_local_variable (index, type, oldpc); \
2945 set_local_type (index, type); \
2946 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
2947 update_aliases (decl, index, PC); \
2950 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
2952 /* have to do this since OPERAND_VALUE may have side-effects */ \
2953 int opvalue = OPERAND_VALUE; \
2954 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2957 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2958 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2960 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
2961 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
2963 #define MONITOR_OPERATION(call) \
2965 tree o = pop_value (ptr_type_node); \
2967 flush_quick_stack (); \
2968 c = build_java_monitor (call, o); \
2969 TREE_SIDE_EFFECTS (c) = 1; \
2970 java_add_stmt (c); \
2973 #define SPECIAL_IINC(IGNORED) \
2975 unsigned int local_var_index = IMMEDIATE_u1; \
2976 int ival = IMMEDIATE_s1; \
2977 expand_iinc(local_var_index, ival, oldpc); \
2980 #define SPECIAL_WIDE(IGNORED) \
2982 int modified_opcode = IMMEDIATE_u1; \
2983 unsigned int local_var_index = IMMEDIATE_u2; \
2984 switch (modified_opcode) \
2988 int ival = IMMEDIATE_s2; \
2989 expand_iinc (local_var_index, ival, oldpc); \
2992 case OPCODE_iload: \
2993 case OPCODE_lload: \
2994 case OPCODE_fload: \
2995 case OPCODE_dload: \
2996 case OPCODE_aload: \
2998 /* duplicate code from LOAD macro */ \
2999 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3002 case OPCODE_istore: \
3003 case OPCODE_lstore: \
3004 case OPCODE_fstore: \
3005 case OPCODE_dstore: \
3006 case OPCODE_astore: \
3008 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3012 error ("unrecogized wide sub-instruction"); \
3016 #define SPECIAL_THROW(IGNORED) \
3017 build_java_athrow (pop_value (throwable_type_node))
3019 #define SPECIAL_BREAK NOT_IMPL1
3020 #define IMPL NOT_IMPL
3022 #include "javaop.def"
3025 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3030 /* Return the opcode at PC in the code section pointed to by
3033 static unsigned char
3034 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3036 unsigned char opcode;
3037 long absolute_offset = (long)JCF_TELL (jcf);
3039 JCF_SEEK (jcf, code_offset);
3040 opcode = jcf->read_ptr [pc];
3041 JCF_SEEK (jcf, absolute_offset);
3045 /* Some bytecode compilers are emitting accurate LocalVariableTable
3046 attributes. Here's an example:
3051 Attribute "LocalVariableTable"
3052 slot #<n>: ... (PC: PC+1 length: L)
3054 This is accurate because the local in slot <n> really exists after
3055 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3057 This procedure recognizes this situation and extends the live range
3058 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3059 length of the store instruction.)
3061 This function is used by `give_name_to_locals' so that a local's
3062 DECL features a DECL_LOCAL_START_PC such that the first related
3063 store operation will use DECL as a destination, not a unrelated
3064 temporary created for the occasion.
3066 This function uses a global (instruction_bits) `note_instructions' should
3067 have allocated and filled properly. */
3070 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3071 int start_pc, int slot)
3073 int first, index, opcode;
3082 /* Find last previous instruction and remember it */
3083 for (pc = start_pc-1; pc; pc--)
3084 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3088 /* Retrieve the instruction, handle `wide'. */
3089 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3090 if (opcode == OPCODE_wide)
3093 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3098 case OPCODE_astore_0:
3099 case OPCODE_astore_1:
3100 case OPCODE_astore_2:
3101 case OPCODE_astore_3:
3102 first = OPCODE_astore_0;
3105 case OPCODE_istore_0:
3106 case OPCODE_istore_1:
3107 case OPCODE_istore_2:
3108 case OPCODE_istore_3:
3109 first = OPCODE_istore_0;
3112 case OPCODE_lstore_0:
3113 case OPCODE_lstore_1:
3114 case OPCODE_lstore_2:
3115 case OPCODE_lstore_3:
3116 first = OPCODE_lstore_0;
3119 case OPCODE_fstore_0:
3120 case OPCODE_fstore_1:
3121 case OPCODE_fstore_2:
3122 case OPCODE_fstore_3:
3123 first = OPCODE_fstore_0;
3126 case OPCODE_dstore_0:
3127 case OPCODE_dstore_1:
3128 case OPCODE_dstore_2:
3129 case OPCODE_dstore_3:
3130 first = OPCODE_dstore_0;
3138 index = peek_opcode_at_pc (jcf, code_offset, pc);
3141 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3142 index = (other << 8) + index;
3147 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3148 means we have a <t>store. */
3149 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3155 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3156 order, as specified by Java Language Specification.
3158 The problem is that while expand_expr will evaluate its sub-operands in
3159 left-to-right order, for variables it will just return an rtx (i.e.
3160 an lvalue) for the variable (rather than an rvalue). So it is possible
3161 that a later sub-operand will change the register, and when the
3162 actual operation is done, it will use the new value, when it should
3163 have used the original value.
3165 We fix this by using save_expr. This forces the sub-operand to be
3166 copied into a fresh virtual register,
3168 For method invocation, we modify the arguments so that a
3169 left-to-right order evaluation is performed. Saved expressions
3170 will, in CALL_EXPR order, be reused when the call will be expanded.
3174 force_evaluation_order (tree node)
3176 if (flag_syntax_only)
3178 if (TREE_CODE (node) == CALL_EXPR
3179 || TREE_CODE (node) == NEW_CLASS_EXPR
3180 || (TREE_CODE (node) == COMPOUND_EXPR
3181 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3182 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3188 /* Position arg properly, account for wrapped around ctors. */
3189 if (TREE_CODE (node) == COMPOUND_EXPR)
3190 arg = TREE_OPERAND (node, 0);
3192 arg = TREE_OPERAND (arg, 1);
3194 /* An empty argument list is ok, just ignore it. */
3198 /* Not having a list of arguments here is an error. */
3199 if (TREE_CODE (arg) != TREE_LIST)
3202 /* This reverses the evaluation order. This is a desired effect. */
3203 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3205 tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3206 cmp = (cmp == NULL_TREE ? saved :
3207 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3208 TREE_VALUE (arg) = saved;
3211 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3212 TREE_SIDE_EFFECTS (cmp) = 1;
3216 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3217 if (TREE_TYPE (cmp) != void_type_node)
3218 cmp = save_expr (cmp);
3219 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3220 TREE_SIDE_EFFECTS (cmp) = 1;
3227 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3228 location where an expression or an identifier were encountered. It
3229 is necessary for languages where the frontend parser will handle
3230 recursively more than one file (Java is one of them). */
3233 build_expr_wfl (tree node,
3234 #ifdef USE_MAPPED_LOCATION
3235 source_location location
3237 const char *file, int line, int col
3242 #ifdef USE_MAPPED_LOCATION
3243 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3244 SET_EXPR_LOCATION (wfl, location);
3246 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3248 static const char *last_file = 0;
3249 static tree last_filenode = NULL_TREE;
3251 EXPR_WFL_SET_LINECOL (wfl, line, col);
3252 if (file != last_file)
3255 last_filenode = file ? get_identifier (file) : NULL_TREE;
3257 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3259 EXPR_WFL_NODE (wfl) = node;
3262 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3263 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3264 TREE_TYPE (wfl) = TREE_TYPE (node);
3270 #ifdef USE_MAPPED_LOCATION
3272 expr_add_location (tree node, source_location location, bool statement)
3276 /* FIXME. This optimization causes failures in code that expects an
3277 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3278 if (node && ! (statement && flag_emit_class_files))
3280 source_location node_loc = EXPR_LOCATION (node);
3281 if (node_loc == location || location == UNKNOWN_LOCATION)
3283 if (node_loc == UNKNOWN_LOCATION
3284 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3286 SET_EXPR_LOCATION (node, location);
3291 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3292 SET_EXPR_LOCATION (wfl, location);
3293 EXPR_WFL_NODE (wfl) = node;
3294 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3295 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3298 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3299 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3300 TREE_TYPE (wfl) = TREE_TYPE (node);
3307 /* Build a node to represent empty statements and blocks. */
3310 build_java_empty_stmt (void)
3312 tree t = build_empty_stmt ();
3313 CAN_COMPLETE_NORMALLY (t) = 1;
3317 #include "gt-java-expr.h"