1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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);
89 static void promote_arguments (void);
91 static GTY(()) tree operand_type[59];
93 static GTY(()) tree methods_ident;
94 static GTY(()) tree ncode_ident;
95 tree dtable_ident = NULL_TREE;
97 /* Set to nonzero value in order to emit class initialization code
98 before static field references. */
99 int always_initialize_class_p = 0;
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 physical memory page size used in this computer. See
134 build_field_ref(). */
135 static GTY(()) tree page_size;
137 /* The stack pointer of the Java virtual machine.
138 This does include the size of the quick_stack. */
142 const unsigned char *linenumber_table;
143 int linenumber_count;
145 /* Largest pc so far in this method that has been passed to lookup_label. */
146 int highest_label_pc_this_method = -1;
148 /* Base value for this method to add to pc to get generated label. */
149 int start_label_pc_this_method = 0;
152 init_expr_processing (void)
154 operand_type[21] = operand_type[54] = int_type_node;
155 operand_type[22] = operand_type[55] = long_type_node;
156 operand_type[23] = operand_type[56] = float_type_node;
157 operand_type[24] = operand_type[57] = double_type_node;
158 operand_type[25] = operand_type[58] = ptr_type_node;
162 java_truthvalue_conversion (tree expr)
164 /* It is simpler and generates better code to have only TRUTH_*_EXPR
165 or comparison expressions as truth values at this level.
167 This function should normally be identity for Java. */
169 switch (TREE_CODE (expr))
171 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
172 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
173 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
174 case ORDERED_EXPR: case UNORDERED_EXPR:
175 case TRUTH_ANDIF_EXPR:
176 case TRUTH_ORIF_EXPR:
185 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
188 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
190 /* are these legal? XXX JH */
194 /* These don't change whether an object is nonzero or zero. */
195 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
198 /* Distribute the conversion into the arms of a COND_EXPR. */
199 return fold_build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
200 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
201 java_truthvalue_conversion (TREE_OPERAND (expr, 2)));
204 /* If this is widening the argument, we can ignore it. */
205 if (TYPE_PRECISION (TREE_TYPE (expr))
206 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
207 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
208 /* fall through to default */
211 return fold_build2 (NE_EXPR, boolean_type_node,
212 expr, boolean_false_node);
216 /* Save any stack slots that happen to be in the quick_stack into their
217 home virtual register slots.
219 The copy order is from low stack index to high, to support the invariant
220 that the expression for a slot may contain decls for stack slots with
221 higher (or the same) index, but not lower. */
224 flush_quick_stack (void)
226 int stack_index = stack_pointer;
227 tree prev, cur, next;
229 /* First reverse the quick_stack, and count the number of slots it has. */
230 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
232 next = TREE_CHAIN (cur);
233 TREE_CHAIN (cur) = prev;
235 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
239 while (quick_stack != NULL_TREE)
242 tree node = quick_stack, type;
243 quick_stack = TREE_CHAIN (node);
244 TREE_CHAIN (node) = tree_list_free_list;
245 tree_list_free_list = node;
246 node = TREE_VALUE (node);
247 type = TREE_TYPE (node);
249 decl = find_stack_slot (stack_index, type);
251 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
252 stack_index += 1 + TYPE_IS_WIDE (type);
256 /* Push TYPE on the type stack.
257 Return true on success, 0 on overflow. */
260 push_type_0 (tree type)
263 type = promote_type (type);
264 n_words = 1 + TYPE_IS_WIDE (type);
265 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
267 /* Allocate decl for this variable now, so we get a temporary that
268 survives the whole method. */
269 find_stack_slot (stack_pointer, type);
270 stack_type_map[stack_pointer++] = type;
272 while (--n_words >= 0)
273 stack_type_map[stack_pointer++] = TYPE_SECOND;
278 push_type (tree type)
280 int r = push_type_0 (type);
285 push_value (tree value)
287 tree type = TREE_TYPE (value);
288 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
290 type = promote_type (type);
291 value = convert (type, value);
294 if (tree_list_free_list == NULL_TREE)
295 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
298 tree node = tree_list_free_list;
299 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
300 TREE_VALUE (node) = value;
301 TREE_CHAIN (node) = quick_stack;
306 /* Pop a type from the type stack.
307 TYPE is the expected type. Return the actual type, which must be
309 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
312 pop_type_0 (tree type, char **messagep)
317 if (TREE_CODE (type) == RECORD_TYPE)
318 type = promote_type (type);
319 n_words = 1 + TYPE_IS_WIDE (type);
320 if (stack_pointer < n_words)
322 *messagep = xstrdup ("stack underflow");
325 while (--n_words > 0)
327 if (stack_type_map[--stack_pointer] != void_type_node)
329 *messagep = xstrdup ("Invalid multi-word value on type stack");
333 t = stack_type_map[--stack_pointer];
334 if (type == NULL_TREE || t == type)
336 if (TREE_CODE (t) == TREE_LIST)
340 tree tt = TREE_PURPOSE (t);
341 if (! can_widen_reference_to (tt, type))
351 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
352 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
354 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
356 /* If the expected type we've been passed is object or ptr
357 (i.e. void*), the caller needs to know the real type. */
358 if (type == ptr_type_node || type == object_ptr_type_node)
361 /* Since the verifier has already run, we know that any
362 types we see will be compatible. In BC mode, this fact
363 may be checked at runtime, but if that is so then we can
364 assume its truth here as well. So, we always succeed
365 here, with the expected type. */
369 if (! flag_verify_invocations && flag_indirect_dispatch
370 && t == object_ptr_type_node)
372 if (type != ptr_type_node)
373 warning (0, "need to insert runtime check for %s",
374 xstrdup (lang_printable_name (type, 0)));
378 /* lang_printable_name uses a static buffer, so we must save the result
379 from calling it the first time. */
382 char *temp = xstrdup (lang_printable_name (type, 0));
383 /* If the stack contains a multi-word type, keep popping the stack until
384 the real type is found. */
385 while (t == void_type_node)
386 t = stack_type_map[--stack_pointer];
387 *messagep = concat ("expected type '", temp,
388 "' but stack contains '", lang_printable_name (t, 0),
395 /* Pop a type from the type stack.
396 TYPE is the expected type. Return the actual type, which must be
397 convertible to TYPE, otherwise call error. */
402 char *message = NULL;
403 type = pop_type_0 (type, &message);
406 error ("%s", message);
413 /* Return true if two type assertions are equal. */
416 type_assertion_eq (const void * k1_p, const void * k2_p)
418 type_assertion k1 = *(type_assertion *)k1_p;
419 type_assertion k2 = *(type_assertion *)k2_p;
420 return (k1.assertion_code == k2.assertion_code
422 && k1.op2 == k2.op2);
425 /* Hash a type assertion. */
428 type_assertion_hash (const void *p)
430 const type_assertion *k_p = p;
431 hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
432 k_p->assertion_code, 0);
433 hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
434 return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
437 /* Add an entry to the type assertion table for the given class.
438 CLASS is the class for which this assertion will be evaluated by the
439 runtime during loading/initialization.
440 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
441 OP1 and OP2 are the operands. The tree type of these arguments may be
442 specific to each assertion_code. */
445 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
447 htab_t assertions_htab;
451 assertions_htab = TYPE_ASSERTIONS (class);
452 if (assertions_htab == NULL)
454 assertions_htab = htab_create_ggc (7, type_assertion_hash,
455 type_assertion_eq, NULL);
456 TYPE_ASSERTIONS (current_class) = assertions_htab;
459 as.assertion_code = assertion_code;
463 as_pp = htab_find_slot (assertions_htab, &as, INSERT);
465 /* Don't add the same assertion twice. */
469 *as_pp = ggc_alloc (sizeof (type_assertion));
470 **(type_assertion **)as_pp = as;
474 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
475 Handles array types and interfaces. */
478 can_widen_reference_to (tree source_type, tree target_type)
480 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
483 /* Get rid of pointers */
484 if (TREE_CODE (source_type) == POINTER_TYPE)
485 source_type = TREE_TYPE (source_type);
486 if (TREE_CODE (target_type) == POINTER_TYPE)
487 target_type = TREE_TYPE (target_type);
489 if (source_type == target_type)
492 /* FIXME: This is very pessimistic, in that it checks everything,
493 even if we already know that the types are compatible. If we're
494 to support full Java class loader semantics, we need this.
495 However, we could do something more optimal. */
496 if (! flag_verify_invocations)
498 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
499 source_type, target_type);
502 warning (0, "assert: %s is assign compatible with %s",
503 xstrdup (lang_printable_name (target_type, 0)),
504 xstrdup (lang_printable_name (source_type, 0)));
505 /* Punt everything to runtime. */
509 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
515 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
517 HOST_WIDE_INT source_length, target_length;
518 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
520 /* An array implements Cloneable and Serializable. */
521 tree name = DECL_NAME (TYPE_NAME (target_type));
522 return (name == java_lang_cloneable_identifier_node
523 || name == java_io_serializable_identifier_node);
525 target_length = java_array_type_length (target_type);
526 if (target_length >= 0)
528 source_length = java_array_type_length (source_type);
529 if (source_length != target_length)
532 source_type = TYPE_ARRAY_ELEMENT (source_type);
533 target_type = TYPE_ARRAY_ELEMENT (target_type);
534 if (source_type == target_type)
536 if (TREE_CODE (source_type) != POINTER_TYPE
537 || TREE_CODE (target_type) != POINTER_TYPE)
539 return can_widen_reference_to (source_type, target_type);
543 int source_depth = class_depth (source_type);
544 int target_depth = class_depth (target_type);
546 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
549 warning (0, "assert: %s is assign compatible with %s",
550 xstrdup (lang_printable_name (target_type, 0)),
551 xstrdup (lang_printable_name (source_type, 0)));
555 /* class_depth can return a negative depth if an error occurred */
556 if (source_depth < 0 || target_depth < 0)
559 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
561 /* target_type is OK if source_type or source_type ancestors
562 implement target_type. We handle multiple sub-interfaces */
563 tree binfo, base_binfo;
566 for (binfo = TYPE_BINFO (source_type), i = 0;
567 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
568 if (can_widen_reference_to
569 (BINFO_TYPE (base_binfo), target_type))
576 for ( ; source_depth > target_depth; source_depth--)
579 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
581 return source_type == target_type;
587 pop_value (tree type)
589 type = pop_type (type);
592 tree node = quick_stack;
593 quick_stack = TREE_CHAIN (quick_stack);
594 TREE_CHAIN (node) = tree_list_free_list;
595 tree_list_free_list = node;
596 node = TREE_VALUE (node);
600 return find_stack_slot (stack_pointer, promote_type (type));
604 /* Pop and discard the top COUNT stack slots. */
607 java_stack_pop (int count)
613 gcc_assert (stack_pointer != 0);
615 type = stack_type_map[stack_pointer - 1];
616 if (type == TYPE_SECOND)
619 gcc_assert (stack_pointer != 1 && count > 0);
621 type = stack_type_map[stack_pointer - 2];
623 val = pop_value (type);
628 /* Implement the 'swap' operator (to swap two top stack slots). */
631 java_stack_swap (void)
637 if (stack_pointer < 2
638 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
639 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
640 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
641 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
642 /* Bad stack swap. */
644 /* Bad stack swap. */
646 flush_quick_stack ();
647 decl1 = find_stack_slot (stack_pointer - 1, type1);
648 decl2 = find_stack_slot (stack_pointer - 2, type2);
649 temp = build_decl (VAR_DECL, NULL_TREE, type1);
650 java_add_local_var (temp);
651 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
652 java_add_stmt (build2 (MODIFY_EXPR, type2,
653 find_stack_slot (stack_pointer - 1, type2),
655 java_add_stmt (build2 (MODIFY_EXPR, type1,
656 find_stack_slot (stack_pointer - 2, type1),
658 stack_type_map[stack_pointer - 1] = type2;
659 stack_type_map[stack_pointer - 2] = type1;
663 java_stack_dup (int size, int offset)
665 int low_index = stack_pointer - size - offset;
668 error ("stack underflow - dup* operation");
670 flush_quick_stack ();
672 stack_pointer += size;
673 dst_index = stack_pointer;
675 for (dst_index = stack_pointer; --dst_index >= low_index; )
678 int src_index = dst_index - size;
679 if (src_index < low_index)
680 src_index = dst_index + size + offset;
681 type = stack_type_map [src_index];
682 if (type == TYPE_SECOND)
684 /* Dup operation splits 64-bit number. */
685 gcc_assert (src_index > low_index);
687 stack_type_map[dst_index] = type;
688 src_index--; dst_index--;
689 type = stack_type_map[src_index];
690 gcc_assert (TYPE_IS_WIDE (type));
693 gcc_assert (! TYPE_IS_WIDE (type));
695 if (src_index != dst_index)
697 tree src_decl = find_stack_slot (src_index, type);
698 tree dst_decl = find_stack_slot (dst_index, type);
701 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
702 stack_type_map[dst_index] = type;
707 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
711 build_java_athrow (tree node)
715 call = build3 (CALL_EXPR,
717 build_address_of (throw_node),
718 build_tree_list (NULL_TREE, node),
720 TREE_SIDE_EFFECTS (call) = 1;
721 java_add_stmt (call);
722 java_stack_pop (stack_pointer);
725 /* Implementation for jsr/ret */
728 build_java_jsr (int target_pc, int return_pc)
730 tree where = lookup_label (target_pc);
731 tree ret = lookup_label (return_pc);
732 tree ret_label = fold_build1 (ADDR_EXPR, return_address_type_node, ret);
733 push_value (ret_label);
734 flush_quick_stack ();
735 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
737 /* Do not need to emit the label here. We noted the existence of the
738 label as a jump target in note_instructions; we'll emit the label
739 for real at the beginning of the expand_byte_code loop. */
743 build_java_ret (tree location)
745 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
748 /* Implementation of operations on array: new, load, store, length */
751 decode_newarray_type (int atype)
755 case 4: return boolean_type_node;
756 case 5: return char_type_node;
757 case 6: return float_type_node;
758 case 7: return double_type_node;
759 case 8: return byte_type_node;
760 case 9: return short_type_node;
761 case 10: return int_type_node;
762 case 11: return long_type_node;
763 default: return NULL_TREE;
767 /* Map primitive type to the code used by OPCODE_newarray. */
770 encode_newarray_type (tree type)
772 if (type == boolean_type_node)
774 else if (type == char_type_node)
776 else if (type == float_type_node)
778 else if (type == double_type_node)
780 else if (type == byte_type_node)
782 else if (type == short_type_node)
784 else if (type == int_type_node)
786 else if (type == long_type_node)
792 /* Build a call to _Jv_ThrowBadArrayIndex(), the
793 ArrayIndexOfBoundsException exception handler. */
796 build_java_throw_out_of_bounds_exception (tree index)
798 tree node = build3 (CALL_EXPR, int_type_node,
799 build_address_of (soft_badarrayindex_node),
800 build_tree_list (NULL_TREE, index), NULL_TREE);
801 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
805 /* Return the length of an array. Doesn't perform any checking on the nature
806 or value of the array NODE. May be used to implement some bytecodes. */
809 build_java_array_length_access (tree node)
811 tree type = TREE_TYPE (node);
812 tree array_type = TREE_TYPE (type);
813 HOST_WIDE_INT length;
815 if (!is_array_type_p (type))
817 /* With the new verifier, we will see an ordinary pointer type
818 here. In this case, we just use an arbitrary array type. */
819 array_type = build_java_array_type (object_ptr_type_node, -1);
820 type = promote_type (array_type);
823 length = java_array_type_length (type);
825 return build_int_cst (NULL_TREE, length);
827 node = build3 (COMPONENT_REF, int_type_node,
828 build_java_indirect_ref (array_type, node,
829 flag_check_references),
830 lookup_field (&array_type, get_identifier ("length")),
832 IS_ARRAY_LENGTH_ACCESS (node) = 1;
836 /* Optionally checks a reference against the NULL pointer. ARG1: the
837 expr, ARG2: we should check the reference. Don't generate extra
838 checks if we're not generating code. */
841 java_check_reference (tree expr, int check)
843 if (!flag_syntax_only && check)
845 expr = save_expr (expr);
846 expr = build3 (COND_EXPR, TREE_TYPE (expr),
847 build2 (EQ_EXPR, boolean_type_node,
848 expr, null_pointer_node),
849 build3 (CALL_EXPR, void_type_node,
850 build_address_of (soft_nullpointer_node),
851 NULL_TREE, NULL_TREE),
858 /* Reference an object: just like an INDIRECT_REF, but with checking. */
861 build_java_indirect_ref (tree type, tree expr, int check)
864 t = java_check_reference (expr, check);
865 t = convert (build_pointer_type (type), t);
866 return build1 (INDIRECT_REF, type, t);
869 /* Implement array indexing (either as l-value or r-value).
870 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
871 Optionally performs bounds checking and/or test to NULL.
872 At this point, ARRAY should have been verified as an array. */
875 build_java_arrayaccess (tree array, tree type, tree index)
877 tree node, throw = NULL_TREE;
880 tree array_type = TREE_TYPE (TREE_TYPE (array));
882 if (!is_array_type_p (TREE_TYPE (array)))
884 /* With the new verifier, we will see an ordinary pointer type
885 here. In this case, we just use the correct array type. */
886 array_type = build_java_array_type (type, -1);
889 if (flag_bounds_check)
892 * (unsigned jint) INDEX >= (unsigned jint) LEN
893 * && throw ArrayIndexOutOfBoundsException.
894 * Note this is equivalent to and more efficient than:
895 * INDEX < 0 || INDEX >= LEN && throw ... */
897 tree len = convert (unsigned_int_type_node,
898 build_java_array_length_access (array));
899 test = fold_build2 (GE_EXPR, boolean_type_node,
900 convert (unsigned_int_type_node, index),
902 if (! integer_zerop (test))
904 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
905 build_java_throw_out_of_bounds_exception (index));
906 /* allows expansion within COMPOUND */
907 TREE_SIDE_EFFECTS( throw ) = 1;
911 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
912 to have the bounds check evaluated first. */
913 if (throw != NULL_TREE)
914 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
916 data_field = lookup_field (&array_type, get_identifier ("data"));
918 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
919 build_java_indirect_ref (array_type, array,
920 flag_check_references),
921 data_field, NULL_TREE);
923 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
927 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
928 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
929 determine that no check is required. */
932 build_java_arraystore_check (tree array, tree object)
934 tree check, element_type, source;
935 tree array_type_p = TREE_TYPE (array);
936 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
938 if (! flag_verify_invocations)
940 /* With the new verifier, we don't track precise types. FIXME:
941 performance regression here. */
942 element_type = TYPE_NAME (object_type_node);
946 gcc_assert (is_array_type_p (array_type_p));
948 /* Get the TYPE_DECL for ARRAY's element type. */
950 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
953 gcc_assert (TREE_CODE (element_type) == TYPE_DECL
954 && TREE_CODE (object_type) == TYPE_DECL);
956 if (!flag_store_check)
957 return build1 (NOP_EXPR, array_type_p, array);
959 /* No check is needed if the element type is final. Also check that
960 element_type matches object_type, since in the bytecode
961 compilation case element_type may be the actual element type of
962 the array rather than its declared type. However, if we're doing
963 indirect dispatch, we can't do the `final' optimization. */
964 if (element_type == object_type
965 && ! flag_indirect_dispatch
966 && CLASS_FINAL (element_type))
967 return build1 (NOP_EXPR, array_type_p, array);
969 /* OBJECT might be wrapped by a SAVE_EXPR. */
970 if (TREE_CODE (object) == SAVE_EXPR)
971 source = TREE_OPERAND (object, 0);
975 /* Avoid the check if OBJECT was just loaded from the same array. */
976 if (TREE_CODE (source) == ARRAY_REF)
979 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
980 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
981 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
982 if (TREE_CODE (source) == SAVE_EXPR)
983 source = TREE_OPERAND (source, 0);
986 if (TREE_CODE (target) == SAVE_EXPR)
987 target = TREE_OPERAND (target, 0);
989 if (source == target)
990 return build1 (NOP_EXPR, array_type_p, array);
993 /* Build an invocation of _Jv_CheckArrayStore */
994 check = build3 (CALL_EXPR, void_type_node,
995 build_address_of (soft_checkarraystore_node),
996 tree_cons (NULL_TREE, array,
997 build_tree_list (NULL_TREE, object)),
999 TREE_SIDE_EFFECTS (check) = 1;
1004 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1005 ARRAY_NODE. This function is used to retrieve something less vague than
1006 a pointer type when indexing the first dimension of something like [[<t>.
1007 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1008 return unchanged. */
1011 build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED,
1014 /* We used to check to see if ARRAY_NODE really had array type.
1015 However, with the new verifier, this is not necessary, as we know
1016 that the object will be an array of the appropriate type. */
1018 return indexed_type;
1021 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1022 called with an integer code (the type of array to create), and the length
1023 of the array to create. */
1026 build_newarray (int atype_value, tree length)
1030 tree prim_type = decode_newarray_type (atype_value);
1032 = build_java_array_type (prim_type,
1033 host_integerp (length, 0) == INTEGER_CST
1034 ? tree_low_cst (length, 0) : -1);
1036 /* If compiling to native, pass a reference to the primitive type class
1037 and save the runtime some work. However, the bytecode generator
1038 expects to find the type_code int here. */
1039 if (flag_emit_class_files)
1040 type_arg = build_int_cst (NULL_TREE, atype_value);
1042 type_arg = build_class_ref (prim_type);
1044 return build3 (CALL_EXPR, promote_type (type),
1045 build_address_of (soft_newarray_node),
1046 tree_cons (NULL_TREE,
1048 build_tree_list (NULL_TREE, length)),
1052 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1053 of the dimension. */
1056 build_anewarray (tree class_type, tree length)
1059 = build_java_array_type (class_type,
1060 host_integerp (length, 0)
1061 ? tree_low_cst (length, 0) : -1);
1063 return build3 (CALL_EXPR, promote_type (type),
1064 build_address_of (soft_anewarray_node),
1065 tree_cons (NULL_TREE, length,
1066 tree_cons (NULL_TREE, build_class_ref (class_type),
1067 build_tree_list (NULL_TREE,
1068 null_pointer_node))),
1072 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1075 build_new_array (tree type, tree length)
1077 if (JPRIMITIVE_TYPE_P (type))
1078 return build_newarray (encode_newarray_type (type), length);
1080 return build_anewarray (TREE_TYPE (type), length);
1083 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1084 class pointer, a number of dimensions and the matching number of
1085 dimensions. The argument list is NULL terminated. */
1088 expand_java_multianewarray (tree class_type, int ndim)
1091 tree args = build_tree_list( NULL_TREE, null_pointer_node );
1093 for( i = 0; i < ndim; i++ )
1094 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1096 push_value (build3 (CALL_EXPR,
1097 promote_type (class_type),
1098 build_address_of (soft_multianewarray_node),
1099 tree_cons (NULL_TREE, build_class_ref (class_type),
1100 tree_cons (NULL_TREE,
1101 build_int_cst (NULL_TREE, ndim),
1106 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1107 ARRAY is an array type. May expand some bound checking and NULL
1108 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1109 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1110 INT. In those cases, we make the conversion.
1112 if ARRAy is a reference type, the assignment is checked at run-time
1113 to make sure that the RHS can be assigned to the array element
1114 type. It is not necessary to generate this code if ARRAY is final. */
1117 expand_java_arraystore (tree rhs_type_node)
1119 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
1120 && TYPE_PRECISION (rhs_type_node) <= 32) ?
1121 int_type_node : rhs_type_node);
1122 tree index = pop_value (int_type_node);
1123 tree array_type, array;
1125 /* If we're processing an `aaload' we might as well just pick
1127 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1129 array_type = build_java_array_type (object_ptr_type_node, -1);
1130 rhs_type_node = object_ptr_type_node;
1133 array_type = build_java_array_type (rhs_type_node, -1);
1135 array = pop_value (array_type);
1136 array = build1 (NOP_EXPR, promote_type (array_type), array);
1138 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1140 flush_quick_stack ();
1142 index = save_expr (index);
1143 array = save_expr (array);
1145 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1147 tree check = build_java_arraystore_check (array, rhs_node);
1148 java_add_stmt (check);
1151 array = build_java_arrayaccess (array, rhs_type_node, index);
1152 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1155 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1156 sure that LHS is an array type. May expand some bound checking and NULL
1158 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1159 BOOLEAN/SHORT, we push a promoted type back to the stack.
1163 expand_java_arrayload (tree lhs_type_node)
1166 tree index_node = pop_value (int_type_node);
1170 /* If we're processing an `aaload' we might as well just pick
1172 if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1174 array_type = build_java_array_type (object_ptr_type_node, -1);
1175 lhs_type_node = object_ptr_type_node;
1178 array_type = build_java_array_type (lhs_type_node, -1);
1179 array_node = pop_value (array_type);
1180 array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1182 index_node = save_expr (index_node);
1183 array_node = save_expr (array_node);
1185 lhs_type_node = build_java_check_indexed_type (array_node,
1187 load_node = build_java_arrayaccess (array_node,
1190 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1191 load_node = fold_build1 (NOP_EXPR, int_type_node, load_node);
1192 push_value (load_node);
1195 /* Expands .length. Makes sure that we deal with and array and may expand
1196 a NULL check on the array object. */
1199 expand_java_array_length (void)
1201 tree array = pop_value (ptr_type_node);
1202 tree length = build_java_array_length_access (array);
1204 push_value (length);
1207 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1208 either soft_monitorenter_node or soft_monitorexit_node. */
1211 build_java_monitor (tree call, tree object)
1213 return build3 (CALL_EXPR,
1215 build_address_of (call),
1216 build_tree_list (NULL_TREE, object),
1220 /* Emit code for one of the PUSHC instructions. */
1223 expand_java_pushc (int ival, tree type)
1226 if (type == ptr_type_node && ival == 0)
1227 value = null_pointer_node;
1228 else if (type == int_type_node || type == long_type_node)
1229 value = build_int_cst (type, ival);
1230 else if (type == float_type_node || type == double_type_node)
1233 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1234 value = build_real (type, x);
1243 expand_java_return (tree type)
1245 if (type == void_type_node)
1246 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1249 tree retval = pop_value (type);
1250 tree res = DECL_RESULT (current_function_decl);
1251 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1253 /* Handle the situation where the native integer type is smaller
1254 than the JVM integer. It can happen for many cross compilers.
1255 The whole if expression just goes away if INT_TYPE_SIZE < 32
1257 if (INT_TYPE_SIZE < 32
1258 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1259 < GET_MODE_SIZE (TYPE_MODE (type))))
1260 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1262 TREE_SIDE_EFFECTS (retval) = 1;
1263 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1268 expand_load_internal (int index, tree type, int pc)
1271 tree var = find_local_variable (index, type, pc);
1273 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1274 on the stack. If there is an assignment to this VAR_DECL between
1275 the stack push and the use, then the wrong code could be
1276 generated. To avoid this we create a new local and copy our
1277 value into it. Then we push this new local on the stack.
1278 Hopefully this all gets optimized out. */
1279 copy = build_decl (VAR_DECL, NULL_TREE, type);
1280 if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1281 && TREE_TYPE (copy) != TREE_TYPE (var))
1282 var = convert (type, var);
1283 java_add_local_var (copy);
1284 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1290 build_address_of (tree value)
1292 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1296 class_has_finalize_method (tree type)
1298 tree super = CLASSTYPE_SUPER (type);
1300 if (super == NULL_TREE)
1301 return false; /* Every class with a real finalizer inherits */
1302 /* from java.lang.Object. */
1304 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1308 java_create_object (tree type)
1310 tree alloc_node = (class_has_finalize_method (type)
1312 : alloc_no_finalizer_node);
1314 return build3 (CALL_EXPR, promote_type (type),
1315 build_address_of (alloc_node),
1316 build_tree_list (NULL_TREE, build_class_ref (type)),
1321 expand_java_NEW (tree type)
1325 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1326 : alloc_no_finalizer_node);
1327 if (! CLASS_LOADED_P (type))
1328 load_class (type, 1);
1329 safe_layout_class (type);
1330 push_value (build3 (CALL_EXPR, promote_type (type),
1331 build_address_of (alloc_node),
1332 build_tree_list (NULL_TREE, build_class_ref (type)),
1336 /* This returns an expression which will extract the class of an
1340 build_get_class (tree value)
1342 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1343 tree vtable_field = lookup_field (&object_type_node,
1344 get_identifier ("vtable"));
1345 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1346 build_java_indirect_ref (object_type_node, value,
1347 flag_check_references),
1348 vtable_field, NULL_TREE);
1349 return build3 (COMPONENT_REF, class_ptr_type,
1350 build1 (INDIRECT_REF, dtable_type, tmp),
1351 class_field, NULL_TREE);
1354 /* This builds the tree representation of the `instanceof' operator.
1355 It tries various tricks to optimize this in cases where types are
1359 build_instanceof (tree value, tree type)
1362 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1363 tree valtype = TREE_TYPE (TREE_TYPE (value));
1364 tree valclass = TYPE_NAME (valtype);
1367 /* When compiling from bytecode, we need to ensure that TYPE has
1369 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1371 load_class (type, 1);
1372 safe_layout_class (type);
1373 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1374 return error_mark_node;
1376 klass = TYPE_NAME (type);
1378 if (type == object_type_node || inherits_from_p (valtype, type))
1380 /* Anything except `null' is an instance of Object. Likewise,
1381 if the object is known to be an instance of the class, then
1382 we only need to check for `null'. */
1383 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1385 else if (flag_verify_invocations
1386 && ! TYPE_ARRAY_P (type)
1387 && ! TYPE_ARRAY_P (valtype)
1388 && DECL_P (klass) && DECL_P (valclass)
1389 && ! CLASS_INTERFACE (valclass)
1390 && ! CLASS_INTERFACE (klass)
1391 && ! inherits_from_p (type, valtype)
1392 && (CLASS_FINAL (klass)
1393 || ! inherits_from_p (valtype, type)))
1395 /* The classes are from different branches of the derivation
1396 tree, so we immediately know the answer. */
1397 expr = boolean_false_node;
1399 else if (DECL_P (klass) && CLASS_FINAL (klass))
1401 tree save = save_expr (value);
1402 expr = build3 (COND_EXPR, itype,
1403 build2 (NE_EXPR, boolean_type_node,
1404 save, null_pointer_node),
1405 build2 (EQ_EXPR, itype,
1406 build_get_class (save),
1407 build_class_ref (type)),
1408 boolean_false_node);
1412 expr = build3 (CALL_EXPR, itype,
1413 build_address_of (soft_instanceof_node),
1414 tree_cons (NULL_TREE, value,
1415 build_tree_list (NULL_TREE,
1416 build_class_ref (type))),
1419 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1424 expand_java_INSTANCEOF (tree type)
1426 tree value = pop_value (object_ptr_type_node);
1427 value = build_instanceof (value, type);
1432 expand_java_CHECKCAST (tree type)
1434 tree value = pop_value (ptr_type_node);
1435 value = build3 (CALL_EXPR, promote_type (type),
1436 build_address_of (soft_checkcast_node),
1437 tree_cons (NULL_TREE, build_class_ref (type),
1438 build_tree_list (NULL_TREE, value)),
1444 expand_iinc (unsigned int local_var_index, int ival, int pc)
1446 tree local_var, res;
1447 tree constant_value;
1449 flush_quick_stack ();
1450 local_var = find_local_variable (local_var_index, int_type_node, pc);
1451 constant_value = build_int_cst (NULL_TREE, ival);
1452 res = fold_build2 (PLUS_EXPR, int_type_node, local_var, constant_value);
1453 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1454 update_aliases (local_var, local_var_index, pc);
1459 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1462 tree arg1 = convert (type, op1);
1463 tree arg2 = convert (type, op2);
1465 if (type == int_type_node)
1469 case TRUNC_DIV_EXPR:
1470 call = soft_idiv_node;
1472 case TRUNC_MOD_EXPR:
1473 call = soft_irem_node;
1479 else if (type == long_type_node)
1483 case TRUNC_DIV_EXPR:
1484 call = soft_ldiv_node;
1486 case TRUNC_MOD_EXPR:
1487 call = soft_lrem_node;
1495 call = build3 (CALL_EXPR, type,
1496 build_address_of (call),
1497 tree_cons (NULL_TREE, arg1,
1498 build_tree_list (NULL_TREE, arg2)),
1505 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1512 tree u_type = java_unsigned_type (type);
1513 arg1 = convert (u_type, arg1);
1514 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1515 return convert (type, arg1);
1519 mask = build_int_cst (NULL_TREE,
1520 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1521 arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
1524 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1525 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1526 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1528 tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1529 boolean_type_node, arg1, arg2);
1530 tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
1531 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1532 ifexp2, integer_zero_node,
1533 op == COMPARE_L_EXPR
1534 ? integer_minus_one_node
1535 : integer_one_node);
1536 return fold_build3 (COND_EXPR, int_type_node, ifexp1,
1537 op == COMPARE_L_EXPR ? integer_one_node
1538 : integer_minus_one_node,
1542 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1544 tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
1545 tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
1546 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1547 ifexp2, integer_one_node,
1549 return fold_build3 (COND_EXPR, int_type_node,
1550 ifexp1, integer_minus_one_node, second_compare);
1552 case TRUNC_DIV_EXPR:
1553 case TRUNC_MOD_EXPR:
1554 if (TREE_CODE (type) == REAL_TYPE
1555 && op == TRUNC_MOD_EXPR)
1558 if (type != double_type_node)
1560 arg1 = convert (double_type_node, arg1);
1561 arg2 = convert (double_type_node, arg2);
1563 call = build3 (CALL_EXPR, double_type_node,
1564 build_address_of (soft_fmod_node),
1565 tree_cons (NULL_TREE, arg1,
1566 build_tree_list (NULL_TREE, arg2)),
1568 if (type != double_type_node)
1569 call = convert (type, call);
1573 if (TREE_CODE (type) == INTEGER_TYPE
1574 && flag_use_divide_subroutine
1575 && ! flag_syntax_only)
1576 return build_java_soft_divmod (op, type, arg1, arg2);
1581 return fold_build2 (op, type, arg1, arg2);
1585 expand_java_binop (tree type, enum tree_code op)
1595 rtype = int_type_node;
1596 rarg = pop_value (rtype);
1599 rarg = pop_value (rtype);
1601 larg = pop_value (ltype);
1602 push_value (build_java_binop (op, type, larg, rarg));
1605 /* Lookup the field named NAME in *TYPEP or its super classes.
1606 If not found, return NULL_TREE.
1607 (If the *TYPEP is not found, or if the field reference is
1608 ambiguous, return error_mark_node.)
1609 If found, return the FIELD_DECL, and set *TYPEP to the
1610 class containing the field. */
1613 lookup_field (tree *typep, tree name)
1615 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1617 load_class (*typep, 1);
1618 safe_layout_class (*typep);
1619 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1620 return error_mark_node;
1624 tree field, binfo, base_binfo;
1628 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1629 if (DECL_NAME (field) == name)
1632 /* Process implemented interfaces. */
1633 save_field = NULL_TREE;
1634 for (binfo = TYPE_BINFO (*typep), i = 0;
1635 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1637 tree t = BINFO_TYPE (base_binfo);
1638 if ((field = lookup_field (&t, name)))
1640 if (save_field == field)
1642 if (save_field == NULL_TREE)
1646 tree i1 = DECL_CONTEXT (save_field);
1647 tree i2 = DECL_CONTEXT (field);
1648 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1649 IDENTIFIER_POINTER (name),
1650 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1651 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1652 return error_mark_node;
1657 if (save_field != NULL_TREE)
1660 *typep = CLASSTYPE_SUPER (*typep);
1665 /* Look up the field named NAME in object SELF_VALUE,
1666 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1667 SELF_VALUE is NULL_TREE if looking for a static field. */
1670 build_field_ref (tree self_value, tree self_class, tree name)
1672 tree base_class = self_class;
1673 tree field_decl = lookup_field (&base_class, name);
1674 if (field_decl == NULL_TREE)
1676 error ("field %qs not found", IDENTIFIER_POINTER (name));
1677 return error_mark_node;
1679 if (self_value == NULL_TREE)
1681 return build_static_field_ref (field_decl);
1685 tree base_type = promote_type (base_class);
1687 /* CHECK is true if self_value is not the this pointer. */
1688 int check = (! (DECL_P (self_value)
1689 && DECL_NAME (self_value) == this_identifier_node));
1691 /* Determine whether a field offset from NULL will lie within
1692 Page 0: this is necessary on those GNU/Linux/BSD systems that
1693 trap SEGV to generate NullPointerExceptions.
1695 We assume that Page 0 will be mapped with NOPERM, and that
1696 memory may be allocated from any other page, so only field
1697 offsets < pagesize are guaratneed to trap. We also assume
1698 the smallest page size we'll encounter is 4k bytes. */
1699 if (check && ! flag_check_references && ! flag_indirect_dispatch)
1701 tree field_offset = byte_position (field_decl);
1703 page_size = size_int (4096);
1704 check = ! INT_CST_LT_UNSIGNED (field_offset, page_size);
1707 if (base_type != TREE_TYPE (self_value))
1708 self_value = fold_build1 (NOP_EXPR, base_type, self_value);
1709 if (! flag_syntax_only && flag_indirect_dispatch)
1712 = build_int_cst (NULL_TREE, get_symbol_table_index
1713 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1715 = build4 (ARRAY_REF, integer_type_node,
1716 TYPE_OTABLE_DECL (output_class), otable_index,
1717 NULL_TREE, NULL_TREE);
1720 if (DECL_CONTEXT (field_decl) != output_class)
1722 = build3 (COND_EXPR, TREE_TYPE (field_offset),
1723 build2 (EQ_EXPR, boolean_type_node,
1724 field_offset, integer_zero_node),
1725 build3 (CALL_EXPR, void_type_node,
1726 build_address_of (soft_nosuchfield_node),
1727 build_tree_list (NULL_TREE, otable_index),
1731 field_offset = fold (convert (sizetype, field_offset));
1732 self_value = java_check_reference (self_value, check);
1734 = fold_build2 (PLUS_EXPR,
1735 build_pointer_type (TREE_TYPE (field_decl)),
1736 self_value, field_offset);
1737 return fold_build1 (INDIRECT_REF, TREE_TYPE (field_decl), address);
1740 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1742 return fold_build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1743 self_value, field_decl, NULL_TREE);
1748 lookup_label (int pc)
1752 if (pc > highest_label_pc_this_method)
1753 highest_label_pc_this_method = pc;
1754 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", start_label_pc_this_method + pc);
1755 name = get_identifier (buf);
1756 if (IDENTIFIER_LOCAL_VALUE (name))
1757 return IDENTIFIER_LOCAL_VALUE (name);
1760 /* The type of the address of a label is return_address_type_node. */
1761 tree decl = create_label_decl (name);
1762 LABEL_PC (decl) = pc;
1763 return pushdecl (decl);
1767 /* Generate a unique name for the purpose of loops and switches
1768 labels, and try-catch-finally blocks label or temporary variables. */
1771 generate_name (void)
1773 static int l_number = 0;
1775 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1777 return get_identifier (buff);
1781 create_label_decl (tree name)
1784 decl = build_decl (LABEL_DECL, name,
1785 TREE_TYPE (return_address_type_node));
1786 DECL_CONTEXT (decl) = current_function_decl;
1787 DECL_IGNORED_P (decl) = 1;
1791 /* This maps a bytecode offset (PC) to various flags. */
1792 char *instruction_bits;
1795 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1797 lookup_label (target_pc);
1798 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1801 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1802 where CONDITION is one of one the compare operators. */
1805 expand_compare (enum tree_code condition, tree value1, tree value2,
1808 tree target = lookup_label (target_pc);
1809 tree cond = fold_build2 (condition, boolean_type_node, value1, value2);
1811 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1812 build1 (GOTO_EXPR, void_type_node, target),
1813 build_java_empty_stmt ()));
1816 /* Emit code for a TEST-type opcode. */
1819 expand_test (enum tree_code condition, tree type, int target_pc)
1821 tree value1, value2;
1822 flush_quick_stack ();
1823 value1 = pop_value (type);
1824 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1825 expand_compare (condition, value1, value2, target_pc);
1828 /* Emit code for a COND-type opcode. */
1831 expand_cond (enum tree_code condition, tree type, int target_pc)
1833 tree value1, value2;
1834 flush_quick_stack ();
1835 /* note: pop values in opposite order */
1836 value2 = pop_value (type);
1837 value1 = pop_value (type);
1838 /* Maybe should check value1 and value2 for type compatibility ??? */
1839 expand_compare (condition, value1, value2, target_pc);
1843 expand_java_goto (int target_pc)
1845 tree target_label = lookup_label (target_pc);
1846 flush_quick_stack ();
1847 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1851 expand_java_switch (tree selector, int default_pc)
1853 tree switch_expr, x;
1855 flush_quick_stack ();
1856 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1857 NULL_TREE, NULL_TREE);
1858 java_add_stmt (switch_expr);
1860 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1861 create_artificial_label ());
1862 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1864 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1865 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1871 expand_java_add_case (tree switch_expr, int match, int target_pc)
1875 value = build_int_cst (TREE_TYPE (switch_expr), match);
1877 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1878 create_artificial_label ());
1879 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1881 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1882 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1886 pop_arguments (tree arg_types)
1888 if (arg_types == end_params_node)
1890 if (TREE_CODE (arg_types) == TREE_LIST)
1892 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1893 tree type = TREE_VALUE (arg_types);
1894 tree arg = pop_value (type);
1896 /* We simply cast each argument to its proper type. This is
1897 needed since we lose type information coming out of the
1898 verifier. We also have to do this when we pop an integer
1899 type that must be promoted for the function call. */
1900 if (TREE_CODE (type) == POINTER_TYPE)
1901 arg = build1 (NOP_EXPR, type, arg);
1902 else if (targetm.calls.promote_prototypes (type)
1903 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1904 && INTEGRAL_TYPE_P (type))
1905 arg = convert (integer_type_node, arg);
1906 return tree_cons (NULL_TREE, arg, tail);
1911 /* Attach to PTR (a block) the declaration found in ENTRY. */
1914 attach_init_test_initialization_flags (void **entry, void *ptr)
1916 tree block = (tree)ptr;
1917 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1919 if (block != error_mark_node)
1921 if (TREE_CODE (block) == BIND_EXPR)
1923 tree body = BIND_EXPR_BODY (block);
1924 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1925 BIND_EXPR_VARS (block) = ite->value;
1926 body = build2 (COMPOUND_EXPR, void_type_node,
1927 build1 (DECL_EXPR, void_type_node, ite->value), body);
1928 BIND_EXPR_BODY (block) = body;
1932 tree body = BLOCK_SUBBLOCKS (block);
1933 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1934 BLOCK_EXPR_DECLS (block) = ite->value;
1935 body = build2 (COMPOUND_EXPR, void_type_node,
1936 build1 (DECL_EXPR, void_type_node, ite->value), body);
1937 BLOCK_SUBBLOCKS (block) = body;
1944 /* Build an expression to initialize the class CLAS.
1945 if EXPR is non-NULL, returns an expression to first call the initializer
1946 (if it is needed) and then calls EXPR. */
1949 build_class_init (tree clas, tree expr)
1953 /* An optimization: if CLAS is a superclass of the class we're
1954 compiling, we don't need to initialize it. However, if CLAS is
1955 an interface, it won't necessarily be initialized, even if we
1957 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1958 && inherits_from_p (current_class, clas))
1959 || current_class == clas)
1962 if (always_initialize_class_p)
1964 init = build3 (CALL_EXPR, void_type_node,
1965 build_address_of (soft_initclass_node),
1966 build_tree_list (NULL_TREE, build_class_ref (clas)),
1968 TREE_SIDE_EFFECTS (init) = 1;
1972 tree *init_test_decl;
1974 init_test_decl = java_treetreehash_new
1975 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1977 if (*init_test_decl == NULL)
1979 /* Build a declaration and mark it as a flag used to track
1980 static class initializations. */
1981 decl = build_decl (VAR_DECL, NULL_TREE,
1983 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1984 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
1985 DECL_CONTEXT (decl) = current_function_decl;
1986 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
1987 /* Tell the check-init code to ignore this decl when not
1988 optimizing class initialization. */
1989 if (!STATIC_CLASS_INIT_OPT_P ())
1990 DECL_BIT_INDEX (decl) = -1;
1991 DECL_INITIAL (decl) = boolean_false_node;
1992 /* Don't emit any symbolic debugging info for this decl. */
1993 DECL_IGNORED_P (decl) = 1;
1994 *init_test_decl = decl;
1997 init = build3 (CALL_EXPR, void_type_node,
1998 build_address_of (soft_initclass_node),
1999 build_tree_list (NULL_TREE, build_class_ref (clas)),
2001 TREE_SIDE_EFFECTS (init) = 1;
2002 init = build3 (COND_EXPR, void_type_node,
2003 build2 (EQ_EXPR, boolean_type_node,
2004 *init_test_decl, boolean_false_node),
2005 init, integer_zero_node);
2006 TREE_SIDE_EFFECTS (init) = 1;
2007 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
2008 build2 (MODIFY_EXPR, boolean_type_node,
2009 *init_test_decl, boolean_true_node));
2010 TREE_SIDE_EFFECTS (init) = 1;
2013 if (expr != NULL_TREE)
2015 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2016 TREE_SIDE_EFFECTS (expr) = 1;
2023 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2024 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2025 tree arg_list ATTRIBUTE_UNUSED)
2028 if (is_compiled_class (self_type))
2030 /* With indirect dispatch we have to use indirect calls for all
2031 publicly visible methods or gcc will use PLT indirections
2032 to reach them. We also have to use indirect dispatch for all
2033 external methods. */
2034 if (! flag_indirect_dispatch
2035 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2037 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2043 = build_int_cst (NULL_TREE, get_symbol_table_index
2044 (method, &TYPE_ATABLE_METHODS (output_class)));
2046 = build4 (ARRAY_REF,
2047 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2048 TYPE_ATABLE_DECL (output_class), table_index,
2049 NULL_TREE, NULL_TREE);
2051 func = convert (method_ptr_type_node, func);
2055 /* We don't know whether the method has been (statically) compiled.
2056 Compile this code to get a reference to the method's code:
2058 SELF_TYPE->methods[METHOD_INDEX].ncode
2062 int method_index = 0;
2065 /* The method might actually be declared in some superclass, so
2066 we have to use its class context, not the caller's notion of
2067 where the method is. */
2068 self_type = DECL_CONTEXT (method);
2069 ref = build_class_ref (self_type);
2070 ref = build1 (INDIRECT_REF, class_type_node, ref);
2071 if (ncode_ident == NULL_TREE)
2072 ncode_ident = get_identifier ("ncode");
2073 if (methods_ident == NULL_TREE)
2074 methods_ident = get_identifier ("methods");
2075 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2076 lookup_field (&class_type_node, methods_ident),
2078 for (meth = TYPE_METHODS (self_type);
2079 ; meth = TREE_CHAIN (meth))
2083 if (meth == NULL_TREE)
2084 fatal_error ("method '%s' not found in class",
2085 IDENTIFIER_POINTER (DECL_NAME (method)));
2088 method_index *= int_size_in_bytes (method_type_node);
2089 ref = fold_build2 (PLUS_EXPR, method_ptr_type_node,
2090 ref, build_int_cst (NULL_TREE, method_index));
2091 ref = build1 (INDIRECT_REF, method_type_node, ref);
2092 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2093 ref, lookup_field (&method_type_node, ncode_ident),
2100 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2102 tree dtable, objectref;
2104 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2106 /* If we're dealing with interfaces and if the objectref
2107 argument is an array then get the dispatch table of the class
2108 Object rather than the one from the objectref. */
2109 objectref = (is_invoke_interface
2110 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2111 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2113 if (dtable_ident == NULL_TREE)
2114 dtable_ident = get_identifier ("vtable");
2115 dtable = build_java_indirect_ref (object_type_node, objectref,
2116 flag_check_references);
2117 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2118 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2123 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2124 T. If this decl has not been seen before, it will be added to the
2125 [oa]table_methods. If it has, the existing table slot will be
2129 get_symbol_table_index (tree t, tree *symbol_table)
2134 if (*symbol_table == NULL_TREE)
2136 *symbol_table = build_tree_list (t, t);
2140 method_list = *symbol_table;
2144 tree value = TREE_VALUE (method_list);
2148 if (TREE_CHAIN (method_list) == NULL_TREE)
2151 method_list = TREE_CHAIN (method_list);
2154 TREE_CHAIN (method_list) = build_tree_list (t, t);
2159 build_invokevirtual (tree dtable, tree method)
2162 tree nativecode_ptr_ptr_type_node
2163 = build_pointer_type (nativecode_ptr_type_node);
2167 if (flag_indirect_dispatch)
2169 gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))));
2172 = build_int_cst (NULL_TREE, get_symbol_table_index
2173 (method, &TYPE_OTABLE_METHODS (output_class)));
2174 method_index = build4 (ARRAY_REF, integer_type_node,
2175 TYPE_OTABLE_DECL (output_class),
2176 otable_index, NULL_TREE, NULL_TREE);
2180 /* We fetch the DECL_VINDEX field directly here, rather than
2181 using get_method_index(). DECL_VINDEX is the true offset
2182 from the vtable base to a method, regrdless of any extra
2183 words inserted at the start of the vtable. */
2184 method_index = DECL_VINDEX (method);
2185 method_index = size_binop (MULT_EXPR, method_index,
2186 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2187 if (TARGET_VTABLE_USES_DESCRIPTORS)
2188 method_index = size_binop (MULT_EXPR, method_index,
2189 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2192 func = fold_build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2193 convert (nativecode_ptr_ptr_type_node, method_index));
2195 if (TARGET_VTABLE_USES_DESCRIPTORS)
2196 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2198 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2203 static GTY(()) tree class_ident;
2205 build_invokeinterface (tree dtable, tree method)
2211 /* We expand invokeinterface here. */
2213 if (class_ident == NULL_TREE)
2214 class_ident = get_identifier ("class");
2216 dtable = build_java_indirect_ref (dtable_type, dtable,
2217 flag_check_references);
2218 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2219 lookup_field (&dtable_type, class_ident), NULL_TREE);
2221 interface = DECL_CONTEXT (method);
2222 gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface)));
2223 layout_class_methods (interface);
2225 if (flag_indirect_dispatch)
2228 = 2 * (get_symbol_table_index
2229 (method, &TYPE_ITABLE_METHODS (output_class)));
2231 = build4 (ARRAY_REF,
2232 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2233 TYPE_ITABLE_DECL (output_class),
2234 build_int_cst (NULL_TREE, itable_index-1),
2235 NULL_TREE, NULL_TREE);
2237 = build4 (ARRAY_REF,
2238 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2239 TYPE_ITABLE_DECL (output_class),
2240 build_int_cst (NULL_TREE, itable_index),
2241 NULL_TREE, NULL_TREE);
2242 interface = convert (class_ptr_type, interface);
2243 idx = convert (integer_type_node, idx);
2247 idx = build_int_cst (NULL_TREE,
2248 get_interface_method_index (method, interface));
2249 interface = build_class_ref (interface);
2252 lookup_arg = tree_cons (NULL_TREE, dtable,
2253 tree_cons (NULL_TREE, interface,
2254 build_tree_list (NULL_TREE, idx)));
2256 return build3 (CALL_EXPR, ptr_type_node,
2257 build_address_of (soft_lookupinterfacemethod_node),
2258 lookup_arg, NULL_TREE);
2261 /* Expand one of the invoke_* opcodes.
2262 OPCODE is the specific opcode.
2263 METHOD_REF_INDEX is an index into the constant pool.
2264 NARGS is the number of arguments, or -1 if not specified. */
2267 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2269 tree method_signature
2270 = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
2271 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool,
2274 = get_class_constant (current_jcf,
2275 COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool,
2277 const char *const self_name
2278 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2279 tree call, func, method, arg_list, method_type;
2280 tree check = NULL_TREE;
2282 if (! CLASS_LOADED_P (self_type))
2284 load_class (self_type, 1);
2285 safe_layout_class (self_type);
2286 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2287 fatal_error ("failed to find class '%s'", self_name);
2289 layout_class_methods (self_type);
2291 if (ID_INIT_P (method_name))
2292 method = lookup_java_constructor (self_type, method_signature);
2294 method = lookup_java_method (self_type, method_name, method_signature);
2296 /* We've found a method in a class other than the one in which it
2297 was wanted. This can happen if, for instance, we're trying to
2298 compile invokespecial super.equals().
2299 FIXME: This is a kludge. Rather than nullifying the result, we
2300 should change lookup_java_method() so that it doesn't search the
2301 superclass chain when we're BC-compiling. */
2302 if (! flag_verify_invocations
2304 && ! TYPE_ARRAY_P (self_type)
2305 && self_type != DECL_CONTEXT (method))
2308 /* We've found a method in an interface, but this isn't an interface
2310 if (opcode != OPCODE_invokeinterface
2312 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2315 /* We've found a non-interface method but we are making an
2316 interface call. This can happen if the interface overrides a
2317 method in Object. */
2318 if (! flag_verify_invocations
2319 && opcode == OPCODE_invokeinterface
2321 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2324 if (method == NULL_TREE)
2326 if (flag_verify_invocations || ! flag_indirect_dispatch)
2328 error ("class '%s' has no method named '%s' matching signature '%s'",
2330 IDENTIFIER_POINTER (method_name),
2331 IDENTIFIER_POINTER (method_signature));
2335 int flags = ACC_PUBLIC;
2336 if (opcode == OPCODE_invokestatic)
2337 flags |= ACC_STATIC;
2338 if (opcode == OPCODE_invokeinterface)
2340 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2341 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2343 method = add_method (self_type, flags, method_name,
2345 DECL_ARTIFICIAL (method) = 1;
2346 METHOD_DUMMY (method) = 1;
2347 layout_class_method (self_type, NULL,
2352 /* Invoke static can't invoke static/abstract method */
2353 if (method != NULL_TREE)
2355 if (opcode == OPCODE_invokestatic)
2357 if (!METHOD_STATIC (method))
2359 error ("invokestatic on non static method");
2362 else if (METHOD_ABSTRACT (method))
2364 error ("invokestatic on abstract method");
2370 if (METHOD_STATIC (method))
2372 error ("invoke[non-static] on static method");
2378 if (method == NULL_TREE)
2380 /* If we got here, we emitted an error message above. So we
2381 just pop the arguments, push a properly-typed zero, and
2383 method_type = get_type_from_signature (method_signature);
2384 pop_arguments (TYPE_ARG_TYPES (method_type));
2385 if (opcode != OPCODE_invokestatic)
2386 pop_type (self_type);
2387 method_type = promote_type (TREE_TYPE (method_type));
2388 push_value (convert (method_type, integer_zero_node));
2392 method_type = TREE_TYPE (method);
2393 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2394 flush_quick_stack ();
2397 if (opcode == OPCODE_invokestatic)
2398 func = build_known_method_ref (method, method_type, self_type,
2399 method_signature, arg_list);
2400 else if (opcode == OPCODE_invokespecial
2401 || (opcode == OPCODE_invokevirtual
2402 && (METHOD_PRIVATE (method)
2403 || METHOD_FINAL (method)
2404 || CLASS_FINAL (TYPE_NAME (self_type)))))
2406 /* If the object for the method call is null, we throw an
2407 exception. We don't do this if the object is the current
2408 method's `this'. In other cases we just rely on an
2409 optimization pass to eliminate redundant checks. FIXME:
2410 Unfortunately there doesn't seem to be a way to determine
2411 what the current method is right now.
2412 We do omit the check if we're calling <init>. */
2413 /* We use a SAVE_EXPR here to make sure we only evaluate
2414 the new `self' expression once. */
2415 tree save_arg = save_expr (TREE_VALUE (arg_list));
2416 TREE_VALUE (arg_list) = save_arg;
2417 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2418 func = build_known_method_ref (method, method_type, self_type,
2419 method_signature, arg_list);
2423 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2425 if (opcode == OPCODE_invokevirtual)
2426 func = build_invokevirtual (dtable, method);
2428 func = build_invokeinterface (dtable, method);
2431 if (TREE_CODE (func) == ADDR_EXPR)
2432 TREE_TYPE (func) = build_pointer_type (method_type);
2434 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2436 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2437 func, arg_list, NULL_TREE);
2438 TREE_SIDE_EFFECTS (call) = 1;
2439 call = check_for_builtin (method, call);
2441 if (check != NULL_TREE)
2443 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2444 TREE_SIDE_EFFECTS (call) = 1;
2447 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2448 java_add_stmt (call);
2452 flush_quick_stack ();
2456 /* Create a stub which will be put into the vtable but which will call
2460 build_jni_stub (tree method)
2462 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2463 tree jni_func_type, tem;
2464 tree env_var, res_var = NULL_TREE, block;
2465 tree method_args, res_type;
2471 tree klass = DECL_CONTEXT (method);
2472 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2473 klass = build_class_ref (klass);
2475 gcc_assert (METHOD_NATIVE (method) && flag_jni);
2477 DECL_ARTIFICIAL (method) = 1;
2478 DECL_EXTERNAL (method) = 0;
2480 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2481 DECL_CONTEXT (env_var) = method;
2483 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2485 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2486 TREE_TYPE (TREE_TYPE (method)));
2487 DECL_CONTEXT (res_var) = method;
2488 TREE_CHAIN (env_var) = res_var;
2491 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2492 TREE_STATIC (meth_var) = 1;
2493 TREE_PUBLIC (meth_var) = 0;
2494 DECL_EXTERNAL (meth_var) = 0;
2495 DECL_CONTEXT (meth_var) = method;
2496 DECL_ARTIFICIAL (meth_var) = 1;
2497 DECL_INITIAL (meth_var) = null_pointer_node;
2498 TREE_USED (meth_var) = 1;
2499 chainon (env_var, meth_var);
2500 build_result_decl (method);
2502 /* One strange way that the front ends are different is that they
2503 store arguments differently. */
2505 method_args = DECL_ARGUMENTS (method);
2507 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2508 block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
2509 TREE_SIDE_EFFECTS (block) = 1;
2510 /* When compiling from source we don't set the type of the block,
2511 because that will prevent patch_return from ever being run. */
2513 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2515 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2516 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2517 build3 (CALL_EXPR, ptr_type_node,
2518 build_address_of (soft_getjnienvnewframe_node),
2519 build_tree_list (NULL_TREE, klass),
2521 CAN_COMPLETE_NORMALLY (body) = 1;
2523 /* All the arguments to this method become arguments to the
2524 underlying JNI function. If we had to wrap object arguments in a
2525 special way, we would do that here. */
2527 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2529 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2530 #ifdef PARM_BOUNDARY
2531 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2534 args_size += (arg_bits / BITS_PER_UNIT);
2536 args = tree_cons (NULL_TREE, tem, args);
2538 args = nreverse (args);
2539 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2541 /* For a static method the second argument is the class. For a
2542 non-static method the second argument is `this'; that is already
2543 available in the argument list. */
2544 if (METHOD_STATIC (method))
2546 args_size += int_size_in_bytes (TREE_TYPE (klass));
2547 args = tree_cons (NULL_TREE, klass, args);
2548 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2551 /* The JNIEnv structure is the first argument to the JNI function. */
2552 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2553 args = tree_cons (NULL_TREE, env_var, args);
2554 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2556 /* We call _Jv_LookupJNIMethod to find the actual underlying
2557 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2558 exception if this function is not found at runtime. */
2559 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2560 method_sig = build_java_signature (TREE_TYPE (method));
2561 lookup_arg = tree_cons (NULL_TREE,
2562 build_utf8_ref (unmangle_classname
2563 (IDENTIFIER_POINTER (method_sig),
2564 IDENTIFIER_LENGTH (method_sig))),
2566 tem = DECL_NAME (method);
2568 = tree_cons (NULL_TREE, klass,
2569 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2571 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2573 #ifdef MODIFY_JNI_METHOD_CALL
2574 tem = MODIFY_JNI_METHOD_CALL (tem);
2577 jni_func_type = build_pointer_type (tem);
2579 jnifunc = build3 (COND_EXPR, ptr_type_node,
2581 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2582 build3 (CALL_EXPR, ptr_type_node,
2584 (soft_lookupjnimethod_node),
2585 lookup_arg, NULL_TREE)));
2587 /* Now we make the actual JNI call via the resulting function
2589 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2590 build1 (NOP_EXPR, jni_func_type, jnifunc),
2593 /* If the JNI call returned a result, capture it here. If we had to
2594 unwrap JNI object results, we would do that here. */
2595 if (res_var != NULL_TREE)
2597 /* If the call returns an object, it may return a JNI weak
2598 reference, in which case we must unwrap it. */
2599 if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
2600 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2601 build_address_of (soft_unwrapjni_node),
2602 build_tree_list (NULL_TREE, call),
2604 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2608 TREE_SIDE_EFFECTS (call) = 1;
2609 CAN_COMPLETE_NORMALLY (call) = 1;
2611 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2612 TREE_SIDE_EFFECTS (body) = 1;
2614 /* Now free the environment we allocated. */
2615 call = build3 (CALL_EXPR, ptr_type_node,
2616 build_address_of (soft_jnipopsystemframe_node),
2617 build_tree_list (NULL_TREE, env_var),
2619 TREE_SIDE_EFFECTS (call) = 1;
2620 CAN_COMPLETE_NORMALLY (call) = 1;
2621 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2622 TREE_SIDE_EFFECTS (body) = 1;
2624 /* Finally, do the return. */
2625 res_type = void_type_node;
2626 if (res_var != NULL_TREE)
2629 gcc_assert (DECL_RESULT (method));
2630 /* Make sure we copy the result variable to the actual
2631 result. We use the type of the DECL_RESULT because it
2632 might be different from the return type of the function:
2633 it might be promoted. */
2634 drt = TREE_TYPE (DECL_RESULT (method));
2635 if (drt != TREE_TYPE (res_var))
2636 res_var = build1 (CONVERT_EXPR, drt, res_var);
2637 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2638 TREE_SIDE_EFFECTS (res_var) = 1;
2641 body = build2 (COMPOUND_EXPR, void_type_node, body,
2642 build1 (RETURN_EXPR, res_type, res_var));
2643 TREE_SIDE_EFFECTS (body) = 1;
2645 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2650 /* Expand an operation to extract from or store into a field.
2651 IS_STATIC is 1 iff the field is static.
2652 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2653 FIELD_REF_INDEX is an index into the constant pool. */
2656 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2659 = get_class_constant (current_jcf,
2660 COMPONENT_REF_CLASS_INDEX (¤t_jcf->cpool,
2662 const char *self_name
2663 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2664 tree field_name = COMPONENT_REF_NAME (¤t_jcf->cpool, field_ref_index);
2665 tree field_signature = COMPONENT_REF_SIGNATURE (¤t_jcf->cpool,
2667 tree field_type = get_type_from_signature (field_signature);
2668 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2671 tree original_self_type = self_type;
2674 if (! CLASS_LOADED_P (self_type))
2675 load_class (self_type, 1);
2676 field_decl = lookup_field (&self_type, field_name);
2677 if (field_decl == error_mark_node)
2681 else if (field_decl == NULL_TREE)
2683 if (! flag_verify_invocations)
2685 int flags = ACC_PUBLIC;
2687 flags |= ACC_STATIC;
2688 self_type = original_self_type;
2689 field_decl = add_field (original_self_type, field_name,
2691 DECL_ARTIFICIAL (field_decl) = 1;
2692 DECL_IGNORED_P (field_decl) = 1;
2696 error ("missing field '%s' in '%s'",
2697 IDENTIFIER_POINTER (field_name), self_name);
2701 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2703 error ("mismatching signature for field '%s' in '%s'",
2704 IDENTIFIER_POINTER (field_name), self_name);
2707 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2711 push_value (convert (field_type, integer_zero_node));
2712 flush_quick_stack ();
2716 field_ref = build_field_ref (field_ref, self_type, field_name);
2718 && ! flag_indirect_dispatch)
2719 field_ref = build_class_init (self_type, field_ref);
2722 flush_quick_stack ();
2723 if (FIELD_FINAL (field_decl))
2725 if (DECL_CONTEXT (field_decl) != current_class)
2726 error ("assignment to final field %q+D not in field's class",
2728 else if (FIELD_STATIC (field_decl))
2730 if (!DECL_CLINIT_P (current_function_decl))
2731 warning (0, "assignment to final static field %q+D not in "
2732 "class initializer",
2737 tree cfndecl_name = DECL_NAME (current_function_decl);
2738 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2739 && !ID_FINIT_P (cfndecl_name))
2740 warning (0, "assignment to final field %q+D not in constructor",
2744 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2745 field_ref, new_value));
2748 push_value (field_ref);
2752 load_type_state (tree label)
2755 tree vec = LABEL_TYPE_STATE (label);
2756 int cur_length = TREE_VEC_LENGTH (vec);
2757 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2758 for (i = 0; i < cur_length; i++)
2759 type_map [i] = TREE_VEC_ELT (vec, i);
2762 /* Go over METHOD's bytecode and note instruction starts in
2763 instruction_bits[]. */
2766 note_instructions (JCF *jcf, tree method)
2769 unsigned char* byte_ops;
2770 long length = DECL_CODE_LENGTH (method);
2775 #undef RET /* Defined by config/i386/i386.h */
2777 #define BCODE byte_ops
2778 #define BYTE_type_node byte_type_node
2779 #define SHORT_type_node short_type_node
2780 #define INT_type_node int_type_node
2781 #define LONG_type_node long_type_node
2782 #define CHAR_type_node char_type_node
2783 #define PTR_type_node ptr_type_node
2784 #define FLOAT_type_node float_type_node
2785 #define DOUBLE_type_node double_type_node
2786 #define VOID_type_node void_type_node
2787 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2788 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2789 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2790 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2792 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2794 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2795 byte_ops = jcf->read_ptr;
2796 instruction_bits = xrealloc (instruction_bits, length + 1);
2797 memset (instruction_bits, 0, length + 1);
2799 /* This pass figures out which PC can be the targets of jumps. */
2800 for (PC = 0; PC < length;)
2802 int oldpc = PC; /* PC at instruction start. */
2803 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2804 switch (byte_ops[PC++])
2806 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2808 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2811 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2813 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2814 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2815 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2816 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2817 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2818 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2819 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2820 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2822 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2823 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2824 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2825 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2826 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2827 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2828 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2829 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2831 /* two forms of wide instructions */
2832 #define PRE_SPECIAL_WIDE(IGNORE) \
2834 int modified_opcode = IMMEDIATE_u1; \
2835 if (modified_opcode == OPCODE_iinc) \
2837 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2838 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2842 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2846 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2848 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2850 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2851 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2852 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2853 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2854 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2855 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2856 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2857 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2858 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2859 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2861 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2862 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2863 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2864 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2865 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2866 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2867 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2869 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2871 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2873 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2874 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2876 #define PRE_LOOKUP_SWITCH \
2877 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2878 NOTE_LABEL (default_offset+oldpc); \
2880 while (--npairs >= 0) { \
2881 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2882 jint offset = IMMEDIATE_s4; \
2883 NOTE_LABEL (offset+oldpc); } \
2886 #define PRE_TABLE_SWITCH \
2887 { jint default_offset = IMMEDIATE_s4; \
2888 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2889 NOTE_LABEL (default_offset+oldpc); \
2891 while (low++ <= high) { \
2892 jint offset = IMMEDIATE_s4; \
2893 NOTE_LABEL (offset+oldpc); } \
2896 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2897 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2898 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2899 (void)(IMMEDIATE_u2); \
2900 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2902 #include "javaop.def"
2909 expand_byte_code (JCF *jcf, tree method)
2913 const unsigned char *linenumber_pointer;
2914 int dead_code_index = -1;
2915 unsigned char* byte_ops;
2916 long length = DECL_CODE_LENGTH (method);
2919 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2920 byte_ops = jcf->read_ptr;
2922 /* We make an initial pass of the line number table, to note
2923 which instructions have associated line number entries. */
2924 linenumber_pointer = linenumber_table;
2925 for (i = 0; i < linenumber_count; i++)
2927 int pc = GET_u2 (linenumber_pointer);
2928 linenumber_pointer += 4;
2930 warning (0, "invalid PC in line number table");
2933 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2934 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2935 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2939 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2942 promote_arguments ();
2944 /* Translate bytecodes. */
2945 linenumber_pointer = linenumber_table;
2946 for (PC = 0; PC < length;)
2948 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2950 tree label = lookup_label (PC);
2951 flush_quick_stack ();
2952 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2953 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2954 if (LABEL_VERIFIED (label) || PC == 0)
2955 load_type_state (label);
2958 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2960 if (dead_code_index == -1)
2962 /* This is the start of a region of unreachable bytecodes.
2963 They still need to be processed in order for EH ranges
2964 to get handled correctly. However, we can simply
2965 replace these bytecodes with nops. */
2966 dead_code_index = PC;
2969 /* Turn this bytecode into a nop. */
2974 if (dead_code_index != -1)
2976 /* We've just reached the end of a region of dead code. */
2978 warning (0, "unreachable bytecode from %d to before %d",
2979 dead_code_index, PC);
2980 dead_code_index = -1;
2984 /* Handle possible line number entry for this PC.
2986 This code handles out-of-order and multiple linenumbers per PC,
2987 but is optimized for the case of line numbers increasing
2988 monotonically with PC. */
2989 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2991 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2992 || GET_u2 (linenumber_pointer) != PC)
2993 linenumber_pointer = linenumber_table;
2994 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2996 int pc = GET_u2 (linenumber_pointer);
2997 linenumber_pointer += 4;
3000 int line = GET_u2 (linenumber_pointer - 2);
3001 #ifdef USE_MAPPED_LOCATION
3002 input_location = linemap_line_start (&line_table, line, 1);
3004 input_location.line = line;
3006 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
3011 maybe_pushlevels (PC);
3012 PC = process_jvm_instruction (PC, byte_ops, length);
3013 maybe_poplevels (PC);
3016 if (dead_code_index != -1)
3018 /* We've just reached the end of a region of dead code. */
3020 warning (0, "unreachable bytecode from %d to the end of the method",
3026 java_push_constant_from_pool (JCF *jcf, int index)
3029 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3032 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3033 index = alloc_name_constant (CONSTANT_String, name);
3034 c = build_ref_from_constant_pool (index);
3035 c = convert (promote_type (string_type_node), c);
3038 c = get_constant (jcf, index);
3043 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3044 long length ATTRIBUTE_UNUSED)
3046 const char *opname; /* Temporary ??? */
3047 int oldpc = PC; /* PC at instruction start. */
3049 /* If the instruction is at the beginning of an exception handler,
3050 replace the top of the stack with the thrown object reference. */
3051 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3053 /* Note that the verifier will not emit a type map at all for
3054 dead exception handlers. In this case we just ignore the
3056 if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
3058 tree type = pop_type (promote_type (throwable_type_node));
3059 push_value (build_exception_object_ref (type));
3063 switch (byte_ops[PC++])
3065 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3068 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3071 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3073 int saw_index = 0; \
3074 int index = OPERAND_VALUE; \
3076 (find_local_variable (index, return_address_type_node, oldpc)); \
3079 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3081 /* OPERAND_VALUE may have side-effects on PC */ \
3082 int opvalue = OPERAND_VALUE; \
3083 build_java_jsr (oldpc + opvalue, PC); \
3086 /* Push a constant onto the stack. */
3087 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3088 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3089 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3090 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3092 /* internal macro added for use by the WIDE case */
3093 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3094 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3096 /* Push local variable onto the opcode stack. */
3097 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3099 /* have to do this since OPERAND_VALUE may have side-effects */ \
3100 int opvalue = OPERAND_VALUE; \
3101 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3104 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3105 expand_java_return (OPERAND_TYPE##_type_node)
3107 #define REM_EXPR TRUNC_MOD_EXPR
3108 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3109 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3111 #define FIELD(IS_STATIC, IS_PUT) \
3112 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3114 #define TEST(OPERAND_TYPE, CONDITION) \
3115 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3117 #define COND(OPERAND_TYPE, CONDITION) \
3118 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3120 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3121 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3123 #define BRANCH_GOTO(OPERAND_VALUE) \
3124 expand_java_goto (oldpc + OPERAND_VALUE)
3126 #define BRANCH_CALL(OPERAND_VALUE) \
3127 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3130 #define BRANCH_RETURN(OPERAND_VALUE) \
3132 tree type = OPERAND_TYPE##_type_node; \
3133 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3134 expand_java_ret (value); \
3138 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3139 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3140 fprintf (stderr, "(not implemented)\n")
3141 #define NOT_IMPL1(OPERAND_VALUE) \
3142 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3143 fprintf (stderr, "(not implemented)\n")
3145 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3147 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3149 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3151 #define STACK_SWAP(COUNT) java_stack_swap()
3153 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3154 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3155 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3157 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3158 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3160 #define LOOKUP_SWITCH \
3161 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3162 tree selector = pop_value (INT_type_node); \
3163 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3164 while (--npairs >= 0) \
3166 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3167 expand_java_add_case (switch_expr, match, oldpc + offset); \
3171 #define TABLE_SWITCH \
3172 { jint default_offset = IMMEDIATE_s4; \
3173 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3174 tree selector = pop_value (INT_type_node); \
3175 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3176 for (; low <= high; low++) \
3178 jint offset = IMMEDIATE_s4; \
3179 expand_java_add_case (switch_expr, low, oldpc + offset); \
3183 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3184 { int opcode = byte_ops[PC-1]; \
3185 int method_ref_index = IMMEDIATE_u2; \
3187 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3189 expand_invoke (opcode, method_ref_index, nargs); \
3192 /* Handle new, checkcast, instanceof */
3193 #define OBJECT(TYPE, OP) \
3194 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3196 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3198 #define ARRAY_LOAD(OPERAND_TYPE) \
3200 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3203 #define ARRAY_STORE(OPERAND_TYPE) \
3205 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3208 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3209 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3210 #define ARRAY_NEW_PTR() \
3211 push_value (build_anewarray (get_class_constant (current_jcf, \
3213 pop_value (int_type_node)));
3214 #define ARRAY_NEW_NUM() \
3216 int atype = IMMEDIATE_u1; \
3217 push_value (build_newarray (atype, pop_value (int_type_node)));\
3219 #define ARRAY_NEW_MULTI() \
3221 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3222 int ndims = IMMEDIATE_u1; \
3223 expand_java_multianewarray( class, ndims ); \
3226 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3227 push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3228 pop_value (OPERAND_TYPE##_type_node)));
3230 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3232 push_value (build1 (NOP_EXPR, int_type_node, \
3233 (convert (TO_TYPE##_type_node, \
3234 pop_value (FROM_TYPE##_type_node))))); \
3237 #define CONVERT(FROM_TYPE, TO_TYPE) \
3239 push_value (convert (TO_TYPE##_type_node, \
3240 pop_value (FROM_TYPE##_type_node))); \
3243 /* internal macro added for use by the WIDE case
3244 Added TREE_TYPE (decl) assignment, apbianco */
3245 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3248 int index = OPVALUE; \
3249 tree type = OPTYPE; \
3250 value = pop_value (type); \
3251 type = TREE_TYPE (value); \
3252 decl = find_local_variable (index, type, oldpc); \
3253 set_local_type (index, type); \
3254 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3255 update_aliases (decl, index, PC); \
3258 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3260 /* have to do this since OPERAND_VALUE may have side-effects */ \
3261 int opvalue = OPERAND_VALUE; \
3262 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3265 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3266 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3268 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3269 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3271 #define MONITOR_OPERATION(call) \
3273 tree o = pop_value (ptr_type_node); \
3275 flush_quick_stack (); \
3276 c = build_java_monitor (call, o); \
3277 TREE_SIDE_EFFECTS (c) = 1; \
3278 java_add_stmt (c); \
3281 #define SPECIAL_IINC(IGNORED) \
3283 unsigned int local_var_index = IMMEDIATE_u1; \
3284 int ival = IMMEDIATE_s1; \
3285 expand_iinc(local_var_index, ival, oldpc); \
3288 #define SPECIAL_WIDE(IGNORED) \
3290 int modified_opcode = IMMEDIATE_u1; \
3291 unsigned int local_var_index = IMMEDIATE_u2; \
3292 switch (modified_opcode) \
3296 int ival = IMMEDIATE_s2; \
3297 expand_iinc (local_var_index, ival, oldpc); \
3300 case OPCODE_iload: \
3301 case OPCODE_lload: \
3302 case OPCODE_fload: \
3303 case OPCODE_dload: \
3304 case OPCODE_aload: \
3306 /* duplicate code from LOAD macro */ \
3307 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3310 case OPCODE_istore: \
3311 case OPCODE_lstore: \
3312 case OPCODE_fstore: \
3313 case OPCODE_dstore: \
3314 case OPCODE_astore: \
3316 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3320 error ("unrecogized wide sub-instruction"); \
3324 #define SPECIAL_THROW(IGNORED) \
3325 build_java_athrow (pop_value (throwable_type_node))
3327 #define SPECIAL_BREAK NOT_IMPL1
3328 #define IMPL NOT_IMPL
3330 #include "javaop.def"
3333 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3338 /* Return the opcode at PC in the code section pointed to by
3341 static unsigned char
3342 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3344 unsigned char opcode;
3345 long absolute_offset = (long)JCF_TELL (jcf);
3347 JCF_SEEK (jcf, code_offset);
3348 opcode = jcf->read_ptr [pc];
3349 JCF_SEEK (jcf, absolute_offset);
3353 /* Some bytecode compilers are emitting accurate LocalVariableTable
3354 attributes. Here's an example:
3359 Attribute "LocalVariableTable"
3360 slot #<n>: ... (PC: PC+1 length: L)
3362 This is accurate because the local in slot <n> really exists after
3363 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3365 This procedure recognizes this situation and extends the live range
3366 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3367 length of the store instruction.)
3369 This function is used by `give_name_to_locals' so that a local's
3370 DECL features a DECL_LOCAL_START_PC such that the first related
3371 store operation will use DECL as a destination, not an unrelated
3372 temporary created for the occasion.
3374 This function uses a global (instruction_bits) `note_instructions' should
3375 have allocated and filled properly. */
3378 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3379 int start_pc, int slot)
3381 int first, index, opcode;
3390 /* Find last previous instruction and remember it */
3391 for (pc = start_pc-1; pc; pc--)
3392 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3396 /* Retrieve the instruction, handle `wide'. */
3397 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3398 if (opcode == OPCODE_wide)
3401 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3406 case OPCODE_astore_0:
3407 case OPCODE_astore_1:
3408 case OPCODE_astore_2:
3409 case OPCODE_astore_3:
3410 first = OPCODE_astore_0;
3413 case OPCODE_istore_0:
3414 case OPCODE_istore_1:
3415 case OPCODE_istore_2:
3416 case OPCODE_istore_3:
3417 first = OPCODE_istore_0;
3420 case OPCODE_lstore_0:
3421 case OPCODE_lstore_1:
3422 case OPCODE_lstore_2:
3423 case OPCODE_lstore_3:
3424 first = OPCODE_lstore_0;
3427 case OPCODE_fstore_0:
3428 case OPCODE_fstore_1:
3429 case OPCODE_fstore_2:
3430 case OPCODE_fstore_3:
3431 first = OPCODE_fstore_0;
3434 case OPCODE_dstore_0:
3435 case OPCODE_dstore_1:
3436 case OPCODE_dstore_2:
3437 case OPCODE_dstore_3:
3438 first = OPCODE_dstore_0;
3446 index = peek_opcode_at_pc (jcf, code_offset, pc);
3449 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3450 index = (other << 8) + index;
3455 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3456 means we have a <t>store. */
3457 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3463 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3464 order, as specified by Java Language Specification.
3466 The problem is that while expand_expr will evaluate its sub-operands in
3467 left-to-right order, for variables it will just return an rtx (i.e.
3468 an lvalue) for the variable (rather than an rvalue). So it is possible
3469 that a later sub-operand will change the register, and when the
3470 actual operation is done, it will use the new value, when it should
3471 have used the original value.
3473 We fix this by using save_expr. This forces the sub-operand to be
3474 copied into a fresh virtual register,
3476 For method invocation, we modify the arguments so that a
3477 left-to-right order evaluation is performed. Saved expressions
3478 will, in CALL_EXPR order, be reused when the call will be expanded.
3480 We also promote outgoing args if needed. */
3483 force_evaluation_order (tree node)
3485 if (flag_syntax_only)
3487 if (TREE_CODE (node) == CALL_EXPR
3488 || TREE_CODE (node) == NEW_CLASS_EXPR
3489 || (TREE_CODE (node) == COMPOUND_EXPR
3490 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3491 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3497 /* Position arg properly, account for wrapped around ctors. */
3498 if (TREE_CODE (node) == COMPOUND_EXPR)
3499 arg = TREE_OPERAND (node, 0);
3501 arg = TREE_OPERAND (arg, 1);
3503 /* An empty argument list is ok, just ignore it. */
3507 /* Not having a list of arguments here is an error. */
3508 gcc_assert (TREE_CODE (arg) == TREE_LIST);
3510 /* This reverses the evaluation order. This is a desired effect. */
3511 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3513 /* Promote types smaller than integer. This is required by
3515 tree type = TREE_TYPE (TREE_VALUE (arg));
3517 if (targetm.calls.promote_prototypes (type)
3518 && INTEGRAL_TYPE_P (type)
3519 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
3520 TYPE_SIZE (integer_type_node)))
3521 TREE_VALUE (arg) = fold_convert (integer_type_node, TREE_VALUE (arg));
3523 saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3524 cmp = (cmp == NULL_TREE ? saved :
3525 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3526 TREE_VALUE (arg) = saved;
3529 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3530 TREE_SIDE_EFFECTS (cmp) = 1;
3534 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3535 if (TREE_TYPE (cmp) != void_type_node)
3536 cmp = save_expr (cmp);
3537 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3538 TREE_SIDE_EFFECTS (cmp) = 1;
3545 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3546 location where an expression or an identifier were encountered. It
3547 is necessary for languages where the frontend parser will handle
3548 recursively more than one file (Java is one of them). */
3551 build_expr_wfl (tree node,
3552 #ifdef USE_MAPPED_LOCATION
3553 source_location location
3555 const char *file, int line, int col
3561 #ifdef USE_MAPPED_LOCATION
3562 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3563 SET_EXPR_LOCATION (wfl, location);
3565 static const char *last_file = 0;
3566 static tree last_filenode = NULL_TREE;
3568 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3570 EXPR_WFL_SET_LINECOL (wfl, line, col);
3571 if (file != last_file)
3574 last_filenode = file ? get_identifier (file) : NULL_TREE;
3576 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3578 EXPR_WFL_NODE (wfl) = node;
3582 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3583 TREE_TYPE (wfl) = TREE_TYPE (node);
3589 #ifdef USE_MAPPED_LOCATION
3591 expr_add_location (tree node, source_location location, bool statement)
3595 /* FIXME. This optimization causes failures in code that expects an
3596 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3597 if (node && ! (statement && flag_emit_class_files))
3599 source_location node_loc = EXPR_LOCATION (node);
3600 if (node_loc == location || location == UNKNOWN_LOCATION)
3602 if (node_loc == UNKNOWN_LOCATION
3603 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3605 SET_EXPR_LOCATION (node, location);
3610 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3611 SET_EXPR_LOCATION (wfl, location);
3612 EXPR_WFL_NODE (wfl) = node;
3613 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3614 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3618 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3619 TREE_TYPE (wfl) = TREE_TYPE (node);
3626 /* Build a node to represent empty statements and blocks. */
3629 build_java_empty_stmt (void)
3631 tree t = build_empty_stmt ();
3632 CAN_COMPLETE_NORMALLY (t) = 1;
3636 /* Promote all args of integral type before generating any code. */
3639 promote_arguments (void)
3643 for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
3644 arg != NULL_TREE; arg = TREE_CHAIN (arg), i++)
3646 tree arg_type = TREE_TYPE (arg);
3647 if (INTEGRAL_TYPE_P (arg_type)
3648 && TYPE_PRECISION (arg_type) < 32)
3650 tree copy = find_local_variable (i, integer_type_node, -1);
3651 java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
3653 fold_convert (integer_type_node, arg)));
3655 if (TYPE_IS_WIDE (arg_type))
3660 #include "gt-java-expr.h"