1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
30 #include "coretypes.h"
37 #include "java-tree.h"
39 #include "java-opcodes.h"
41 #include "java-except.h"
46 #include "tree-gimple.h"
49 static void flush_quick_stack (void);
50 static void push_value (tree);
51 static tree pop_value (tree);
52 static void java_stack_swap (void);
53 static void java_stack_dup (int, int);
54 static void build_java_athrow (tree);
55 static void build_java_jsr (int, int);
56 static void build_java_ret (tree);
57 static void expand_java_multianewarray (tree, int);
58 static void expand_java_arraystore (tree);
59 static void expand_java_arrayload (tree);
60 static void expand_java_array_length (void);
61 static tree build_java_monitor (tree, tree);
62 static void expand_java_pushc (int, tree);
63 static void expand_java_return (tree);
64 static void expand_load_internal (int, tree, int);
65 static void expand_java_NEW (tree);
66 static void expand_java_INSTANCEOF (tree);
67 static void expand_java_CHECKCAST (tree);
68 static void expand_iinc (unsigned int, int, int);
69 static void expand_java_binop (tree, enum tree_code);
70 static void note_label (int, int);
71 static void expand_compare (enum tree_code, tree, tree, int);
72 static void expand_test (enum tree_code, tree, int);
73 static void expand_cond (enum tree_code, tree, int);
74 static void expand_java_goto (int);
75 static tree expand_java_switch (tree, int);
76 static void expand_java_add_case (tree, int, int);
78 static void expand_java_call (int, int);
79 static void expand_java_ret (tree);
81 static tree pop_arguments (tree);
82 static void expand_invoke (int, int, int);
83 static void expand_java_field_op (int, int, int);
84 static void java_push_constant_from_pool (struct JCF *, int);
85 static void java_stack_pop (int);
86 static tree build_java_throw_out_of_bounds_exception (tree);
87 static tree build_java_check_indexed_type (tree, tree);
88 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
90 static GTY(()) tree operand_type[59];
92 static GTY(()) tree methods_ident;
93 static GTY(()) tree ncode_ident;
94 tree dtable_ident = NULL_TREE;
96 /* Set to nonzero value in order to emit class initialization code
97 before static field references. */
98 int always_initialize_class_p = 0;
100 /* We store the stack state in two places:
101 Within a basic block, we use the quick_stack, which is a
102 pushdown list (TREE_LISTs) of expression nodes.
103 This is the top part of the stack; below that we use find_stack_slot.
104 At the end of a basic block, the quick_stack must be flushed
105 to the stack slot array (as handled by find_stack_slot).
106 Using quick_stack generates better code (especially when
107 compiled without optimization), because we do not have to
108 explicitly store and load trees to temporary variables.
110 If a variable is on the quick stack, it means the value of variable
111 when the quick stack was last flushed. Conceptually, flush_quick_stack
112 saves all the quick_stack elements in parallel. However, that is
113 complicated, so it actually saves them (i.e. copies each stack value
114 to is home virtual register) from low indexes. This allows a quick_stack
115 element at index i (counting from the bottom of stack the) to references
116 slot virtuals for register that are >= i, but not those that are deeper.
117 This convention makes most operations easier. For example iadd works
118 even when the stack contains (reg[0], reg[1]): It results in the
119 stack containing (reg[0]+reg[1]), which is OK. However, some stack
120 operations are more complicated. For example dup given a stack
121 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
122 the convention, since stack value 1 would refer to a register with
123 lower index (reg[0]), which flush_quick_stack does not safely handle.
124 So dup cannot just add an extra element to the quick_stack, but iadd can.
127 static GTY(()) tree quick_stack;
129 /* A free-list of unused permanent TREE_LIST nodes. */
130 static GTY((deletable)) tree tree_list_free_list;
132 /* The stack pointer of the Java virtual machine.
133 This does include the size of the quick_stack. */
137 const unsigned char *linenumber_table;
138 int linenumber_count;
141 init_expr_processing (void)
143 operand_type[21] = operand_type[54] = int_type_node;
144 operand_type[22] = operand_type[55] = long_type_node;
145 operand_type[23] = operand_type[56] = float_type_node;
146 operand_type[24] = operand_type[57] = double_type_node;
147 operand_type[25] = operand_type[58] = ptr_type_node;
151 java_truthvalue_conversion (tree expr)
153 /* It is simpler and generates better code to have only TRUTH_*_EXPR
154 or comparison expressions as truth values at this level.
156 This function should normally be identity for Java. */
158 switch (TREE_CODE (expr))
160 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
161 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
162 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
163 case ORDERED_EXPR: case UNORDERED_EXPR:
164 case TRUTH_ANDIF_EXPR:
165 case TRUTH_ORIF_EXPR:
174 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
177 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
179 /* are these legal? XXX JH */
183 /* These don't change whether an object is nonzero or zero. */
184 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
187 /* Distribute the conversion into the arms of a COND_EXPR. */
189 (build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
190 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
191 java_truthvalue_conversion (TREE_OPERAND (expr, 2))));
194 /* If this is widening the argument, we can ignore it. */
195 if (TYPE_PRECISION (TREE_TYPE (expr))
196 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
197 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
198 /* fall through to default */
201 return fold (build2 (NE_EXPR, boolean_type_node,
202 expr, boolean_false_node));
206 /* Save any stack slots that happen to be in the quick_stack into their
207 home virtual register slots.
209 The copy order is from low stack index to high, to support the invariant
210 that the expression for a slot may contain decls for stack slots with
211 higher (or the same) index, but not lower. */
214 flush_quick_stack (void)
216 int stack_index = stack_pointer;
217 tree prev, cur, next;
219 /* First reverse the quick_stack, and count the number of slots it has. */
220 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
222 next = TREE_CHAIN (cur);
223 TREE_CHAIN (cur) = prev;
225 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
229 while (quick_stack != NULL_TREE)
232 tree node = quick_stack, type;
233 quick_stack = TREE_CHAIN (node);
234 TREE_CHAIN (node) = tree_list_free_list;
235 tree_list_free_list = node;
236 node = TREE_VALUE (node);
237 type = TREE_TYPE (node);
239 decl = find_stack_slot (stack_index, type);
241 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
242 stack_index += 1 + TYPE_IS_WIDE (type);
246 /* Push TYPE on the type stack.
247 Return true on success, 0 on overflow. */
250 push_type_0 (tree type)
253 type = promote_type (type);
254 n_words = 1 + TYPE_IS_WIDE (type);
255 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
257 /* Allocate decl for this variable now, so we get a temporary that
258 survives the whole method. */
259 find_stack_slot (stack_pointer, type);
260 stack_type_map[stack_pointer++] = type;
262 while (--n_words >= 0)
263 stack_type_map[stack_pointer++] = TYPE_SECOND;
268 push_type (tree type)
270 if (! push_type_0 (type))
275 push_value (tree value)
277 tree type = TREE_TYPE (value);
278 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
280 type = promote_type (type);
281 value = convert (type, value);
284 if (tree_list_free_list == NULL_TREE)
285 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
288 tree node = tree_list_free_list;
289 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
290 TREE_VALUE (node) = value;
291 TREE_CHAIN (node) = quick_stack;
296 /* Pop a type from the type stack.
297 TYPE is the expected type. Return the actual type, which must be
299 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
302 pop_type_0 (tree type, char **messagep)
307 if (TREE_CODE (type) == RECORD_TYPE)
308 type = promote_type (type);
309 n_words = 1 + TYPE_IS_WIDE (type);
310 if (stack_pointer < n_words)
312 *messagep = xstrdup ("stack underflow");
315 while (--n_words > 0)
317 if (stack_type_map[--stack_pointer] != void_type_node)
319 *messagep = xstrdup ("Invalid multi-word value on type stack");
323 t = stack_type_map[--stack_pointer];
324 if (type == NULL_TREE || t == type)
326 if (TREE_CODE (t) == TREE_LIST)
330 tree tt = TREE_PURPOSE (t);
331 if (! can_widen_reference_to (tt, type))
341 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
342 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
344 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
346 if (flag_new_verifier)
348 /* Since the verifier has already run, we know that any
349 types we see will be compatible. In BC mode, this fact
350 may be checked at runtime, but if that is so then we can
351 assume its truth here as well. So, we always succeed
352 here, with the expected type. */
357 if (type == ptr_type_node || type == object_ptr_type_node)
359 else if (t == ptr_type_node) /* Special case for null reference. */
361 /* This is a kludge, but matches what Sun's verifier does.
362 It can be tricked, but is safe as long as type errors
363 (i.e. interface method calls) are caught at run-time. */
364 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
365 return object_ptr_type_node;
366 else if (can_widen_reference_to (t, type))
371 if (! flag_verify_invocations && flag_indirect_dispatch
372 && t == object_ptr_type_node)
374 if (type != ptr_type_node)
375 warning ("need to insert runtime check for %s",
376 xstrdup (lang_printable_name (type, 0)));
380 /* lang_printable_name uses a static buffer, so we must save the result
381 from calling it the first time. */
384 char *temp = xstrdup (lang_printable_name (type, 0));
385 *messagep = concat ("expected type '", temp,
386 "' but stack contains '", lang_printable_name (t, 0),
393 /* Pop a type from the type stack.
394 TYPE is the expected type. Return the actual type, which must be
395 convertible to TYPE, otherwise call error. */
400 char *message = NULL;
401 type = pop_type_0 (type, &message);
404 error ("%s", message);
411 /* Return true if two type assertions are equal. */
414 type_assertion_eq (const void * k1_p, const void * k2_p)
416 type_assertion k1 = *(type_assertion *)k1_p;
417 type_assertion k2 = *(type_assertion *)k2_p;
418 return (k1.assertion_code == k2.assertion_code
420 && k1.op2 == k2.op2);
423 /* Hash a type assertion. */
426 type_assertion_hash (const void *p)
428 const type_assertion *k_p = p;
429 hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
430 k_p->assertion_code, 0);
431 hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
432 return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
435 /* Add an entry to the type assertion table for the given class.
436 CLASS is the class for which this assertion will be evaluated by the
437 runtime during loading/initialization.
438 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
439 OP1 and OP2 are the operands. The tree type of these arguments may be
440 specific to each assertion_code. */
443 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
445 htab_t assertions_htab;
449 assertions_htab = TYPE_ASSERTIONS (class);
450 if (assertions_htab == NULL)
452 assertions_htab = htab_create_ggc (7, type_assertion_hash,
453 type_assertion_eq, NULL);
454 TYPE_ASSERTIONS (current_class) = assertions_htab;
457 as.assertion_code = assertion_code;
461 as_pp = htab_find_slot (assertions_htab, &as, true);
463 /* Don't add the same assertion twice. */
467 *as_pp = ggc_alloc (sizeof (type_assertion));
468 **(type_assertion **)as_pp = as;
472 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
473 Handles array types and interfaces. */
476 can_widen_reference_to (tree source_type, tree target_type)
478 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
481 /* Get rid of pointers */
482 if (TREE_CODE (source_type) == POINTER_TYPE)
483 source_type = TREE_TYPE (source_type);
484 if (TREE_CODE (target_type) == POINTER_TYPE)
485 target_type = TREE_TYPE (target_type);
487 if (source_type == target_type)
490 /* FIXME: This is very pessimistic, in that it checks everything,
491 even if we already know that the types are compatible. If we're
492 to support full Java class loader semantics, we need this.
493 However, we could do something more optimal. */
494 if (! flag_verify_invocations)
496 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
497 source_type, target_type);
500 warning ("assert: %s is assign compatible with %s",
501 xstrdup (lang_printable_name (target_type, 0)),
502 xstrdup (lang_printable_name (source_type, 0)));
503 /* Punt everything to runtime. */
507 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
513 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
515 HOST_WIDE_INT source_length, target_length;
516 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
518 /* An array implements Cloneable and Serializable. */
519 tree name = DECL_NAME (TYPE_NAME (target_type));
520 return (name == java_lang_cloneable_identifier_node
521 || name == java_io_serializable_identifier_node);
523 target_length = java_array_type_length (target_type);
524 if (target_length >= 0)
526 source_length = java_array_type_length (source_type);
527 if (source_length != target_length)
530 source_type = TYPE_ARRAY_ELEMENT (source_type);
531 target_type = TYPE_ARRAY_ELEMENT (target_type);
532 if (source_type == target_type)
534 if (TREE_CODE (source_type) != POINTER_TYPE
535 || TREE_CODE (target_type) != POINTER_TYPE)
537 return can_widen_reference_to (source_type, target_type);
541 int source_depth = class_depth (source_type);
542 int target_depth = class_depth (target_type);
544 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
547 warning ("assert: %s is assign compatible with %s",
548 xstrdup (lang_printable_name (target_type, 0)),
549 xstrdup (lang_printable_name (source_type, 0)));
553 /* class_depth can return a negative depth if an error occurred */
554 if (source_depth < 0 || target_depth < 0)
557 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
559 /* target_type is OK if source_type or source_type ancestors
560 implement target_type. We handle multiple sub-interfaces */
561 tree binfo, base_binfo;
564 for (binfo = TYPE_BINFO (source_type), i = 0;
565 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
566 if (can_widen_reference_to
567 (BINFO_TYPE (base_binfo), target_type))
574 for ( ; source_depth > target_depth; source_depth--)
577 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
579 return source_type == target_type;
585 pop_value (tree type)
587 type = pop_type (type);
590 tree node = quick_stack;
591 quick_stack = TREE_CHAIN (quick_stack);
592 TREE_CHAIN (node) = tree_list_free_list;
593 tree_list_free_list = node;
594 node = TREE_VALUE (node);
598 return find_stack_slot (stack_pointer, promote_type (type));
602 /* Pop and discard the top COUNT stack slots. */
605 java_stack_pop (int count)
611 if (stack_pointer == 0)
614 type = stack_type_map[stack_pointer - 1];
615 if (type == TYPE_SECOND)
618 if (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. */
645 flush_quick_stack ();
646 decl1 = find_stack_slot (stack_pointer - 1, type1);
647 decl2 = find_stack_slot (stack_pointer - 2, type2);
648 temp = build_decl (VAR_DECL, NULL_TREE, type1);
649 java_add_local_var (temp);
650 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
651 java_add_stmt (build2 (MODIFY_EXPR, type2,
652 find_stack_slot (stack_pointer - 1, type2),
654 java_add_stmt (build2 (MODIFY_EXPR, type1,
655 find_stack_slot (stack_pointer - 2, type1),
657 stack_type_map[stack_pointer - 1] = type2;
658 stack_type_map[stack_pointer - 2] = type1;
662 java_stack_dup (int size, int offset)
664 int low_index = stack_pointer - size - offset;
667 error ("stack underflow - dup* operation");
669 flush_quick_stack ();
671 stack_pointer += size;
672 dst_index = stack_pointer;
674 for (dst_index = stack_pointer; --dst_index >= low_index; )
677 int src_index = dst_index - size;
678 if (src_index < low_index)
679 src_index = dst_index + size + offset;
680 type = stack_type_map [src_index];
681 if (type == TYPE_SECOND)
683 if (src_index <= low_index)
684 /* Dup operation splits 64-bit number. */
687 stack_type_map[dst_index] = type;
688 src_index--; dst_index--;
689 type = stack_type_map[src_index];
690 if (! TYPE_IS_WIDE (type))
693 else if (TYPE_IS_WIDE (type))
696 if (src_index != dst_index)
698 tree src_decl = find_stack_slot (src_index, type);
699 tree dst_decl = find_stack_slot (dst_index, type);
702 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
703 stack_type_map[dst_index] = type;
708 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
712 build_java_athrow (tree node)
716 call = build3 (CALL_EXPR,
718 build_address_of (throw_node),
719 build_tree_list (NULL_TREE, node),
721 TREE_SIDE_EFFECTS (call) = 1;
722 java_add_stmt (call);
723 java_stack_pop (stack_pointer);
726 /* Implementation for jsr/ret */
729 build_java_jsr (int target_pc, int return_pc)
731 tree where = lookup_label (target_pc);
732 tree ret = lookup_label (return_pc);
733 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
734 push_value (ret_label);
735 flush_quick_stack ();
736 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
738 /* Do not need to emit the label here. We noted the existence of the
739 label as a jump target in note_instructions; we'll emit the label
740 for real at the beginning of the expand_byte_code loop. */
744 build_java_ret (tree location)
746 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
749 /* Implementation of operations on array: new, load, store, length */
752 decode_newarray_type (int atype)
756 case 4: return boolean_type_node;
757 case 5: return char_type_node;
758 case 6: return float_type_node;
759 case 7: return double_type_node;
760 case 8: return byte_type_node;
761 case 9: return short_type_node;
762 case 10: return int_type_node;
763 case 11: return long_type_node;
764 default: return NULL_TREE;
768 /* Map primitive type to the code used by OPCODE_newarray. */
771 encode_newarray_type (tree type)
773 if (type == boolean_type_node)
775 else if (type == char_type_node)
777 else if (type == float_type_node)
779 else if (type == double_type_node)
781 else if (type == byte_type_node)
783 else if (type == short_type_node)
785 else if (type == int_type_node)
787 else if (type == long_type_node)
793 /* Build a call to _Jv_ThrowBadArrayIndex(), the
794 ArrayIndexOfBoundsException exception handler. */
797 build_java_throw_out_of_bounds_exception (tree index)
799 tree node = build3 (CALL_EXPR, int_type_node,
800 build_address_of (soft_badarrayindex_node),
801 build_tree_list (NULL_TREE, index), NULL_TREE);
802 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
806 /* Return the length of an array. Doesn't perform any checking on the nature
807 or value of the array NODE. May be used to implement some bytecodes. */
810 build_java_array_length_access (tree node)
812 tree type = TREE_TYPE (node);
813 tree array_type = TREE_TYPE (type);
814 HOST_WIDE_INT length;
816 /* JVM spec: If the arrayref is null, the arraylength instruction
817 throws a NullPointerException. The only way we could get a node
818 of type ptr_type_node at this point is `aconst_null; arraylength'
819 or something equivalent. */
820 if (!flag_new_verifier && type == ptr_type_node)
821 return build3 (CALL_EXPR, int_type_node,
822 build_address_of (soft_nullpointer_node),
823 NULL_TREE, NULL_TREE);
825 if (!is_array_type_p (type))
827 /* With the new verifier, we will see an ordinary pointer type
828 here. In this case, we just use an arbitrary array type. */
829 array_type = build_java_array_type (object_ptr_type_node, -1);
830 type = promote_type (array_type);
833 length = java_array_type_length (type);
835 return build_int_cst (NULL_TREE, length);
837 node = build3 (COMPONENT_REF, int_type_node,
838 build_java_indirect_ref (array_type, node,
839 flag_check_references),
840 lookup_field (&array_type, get_identifier ("length")),
842 IS_ARRAY_LENGTH_ACCESS (node) = 1;
846 /* Optionally checks a reference against the NULL pointer. ARG1: the
847 expr, ARG2: we should check the reference. Don't generate extra
848 checks if we're not generating code. */
851 java_check_reference (tree expr, int check)
853 if (!flag_syntax_only && check)
855 expr = save_expr (expr);
856 expr = build3 (COND_EXPR, TREE_TYPE (expr),
857 build2 (EQ_EXPR, boolean_type_node,
858 expr, null_pointer_node),
859 build3 (CALL_EXPR, void_type_node,
860 build_address_of (soft_nullpointer_node),
861 NULL_TREE, NULL_TREE),
868 /* Reference an object: just like an INDIRECT_REF, but with checking. */
871 build_java_indirect_ref (tree type, tree expr, int check)
874 t = java_check_reference (expr, check);
875 t = convert (build_pointer_type (type), t);
876 return build1 (INDIRECT_REF, type, t);
879 /* Implement array indexing (either as l-value or r-value).
880 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
881 Optionally performs bounds checking and/or test to NULL.
882 At this point, ARRAY should have been verified as an array. */
885 build_java_arrayaccess (tree array, tree type, tree index)
887 tree node, throw = NULL_TREE;
890 tree array_type = TREE_TYPE (TREE_TYPE (array));
892 if (!is_array_type_p (TREE_TYPE (array)))
894 /* With the new verifier, we will see an ordinary pointer type
895 here. In this case, we just use the correct array type. */
896 array_type = build_java_array_type (type, -1);
899 if (flag_bounds_check)
902 * (unsigned jint) INDEX >= (unsigned jint) LEN
903 * && throw ArrayIndexOutOfBoundsException.
904 * Note this is equivalent to and more efficient than:
905 * INDEX < 0 || INDEX >= LEN && throw ... */
907 tree len = convert (unsigned_int_type_node,
908 build_java_array_length_access (array));
909 test = fold (build2 (GE_EXPR, boolean_type_node,
910 convert (unsigned_int_type_node, index),
912 if (! integer_zerop (test))
914 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
915 build_java_throw_out_of_bounds_exception (index));
916 /* allows expansion within COMPOUND */
917 TREE_SIDE_EFFECTS( throw ) = 1;
921 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
922 to have the bounds check evaluated first. */
923 if (throw != NULL_TREE)
924 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
926 data_field = lookup_field (&array_type, get_identifier ("data"));
928 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
929 build_java_indirect_ref (array_type, array,
930 flag_check_references),
931 data_field, NULL_TREE);
933 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
937 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
938 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
939 determine that no check is required. */
942 build_java_arraystore_check (tree array, tree object)
944 tree check, element_type, source;
945 tree array_type_p = TREE_TYPE (array);
946 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
948 if (! flag_verify_invocations)
950 /* With the new verifier, we don't track precise types. FIXME:
951 performance regression here. */
952 element_type = TYPE_NAME (object_type_node);
956 if (! is_array_type_p (array_type_p))
959 /* Get the TYPE_DECL for ARRAY's element type. */
961 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
964 if (TREE_CODE (element_type) != TYPE_DECL
965 || TREE_CODE (object_type) != TYPE_DECL)
968 if (!flag_store_check)
969 return build1 (NOP_EXPR, array_type_p, array);
971 /* No check is needed if the element type is final. Also check that
972 element_type matches object_type, since in the bytecode
973 compilation case element_type may be the actual element type of
974 the array rather than its declared type. However, if we're doing
975 indirect dispatch, we can't do the `final' optimization. */
976 if (element_type == object_type
977 && ! flag_indirect_dispatch
978 && CLASS_FINAL (element_type))
979 return build1 (NOP_EXPR, array_type_p, array);
981 /* OBJECT might be wrapped by a SAVE_EXPR. */
982 if (TREE_CODE (object) == SAVE_EXPR)
983 source = TREE_OPERAND (object, 0);
987 /* Avoid the check if OBJECT was just loaded from the same array. */
988 if (TREE_CODE (source) == ARRAY_REF)
991 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
992 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
993 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
994 if (TREE_CODE (source) == SAVE_EXPR)
995 source = TREE_OPERAND (source, 0);
998 if (TREE_CODE (target) == SAVE_EXPR)
999 target = TREE_OPERAND (target, 0);
1001 if (source == target)
1002 return build1 (NOP_EXPR, array_type_p, array);
1005 /* Build an invocation of _Jv_CheckArrayStore */
1006 check = build3 (CALL_EXPR, void_type_node,
1007 build_address_of (soft_checkarraystore_node),
1008 tree_cons (NULL_TREE, array,
1009 build_tree_list (NULL_TREE, object)),
1011 TREE_SIDE_EFFECTS (check) = 1;
1016 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1017 ARRAY_NODE. This function is used to retrieve something less vague than
1018 a pointer type when indexing the first dimension of something like [[<t>.
1019 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1020 return unchanged. */
1023 build_java_check_indexed_type (tree array_node, tree indexed_type)
1027 /* We used to check to see if ARRAY_NODE really had array type.
1028 However, with the new verifier, this is not necessary, as we know
1029 that the object will be an array of the appropriate type. */
1031 if (flag_new_verifier)
1032 return indexed_type;
1034 if (!is_array_type_p (TREE_TYPE (array_node)))
1037 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
1039 if (indexed_type == ptr_type_node)
1040 return promote_type (elt_type);
1042 /* BYTE/BOOLEAN store and load are used for both type */
1043 if (indexed_type == byte_type_node && elt_type == boolean_type_node)
1044 return boolean_type_node;
1046 if (indexed_type != elt_type )
1049 return indexed_type;
1052 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1053 called with an integer code (the type of array to create), and the length
1054 of the array to create. */
1057 build_newarray (int atype_value, tree length)
1061 tree prim_type = decode_newarray_type (atype_value);
1063 = build_java_array_type (prim_type,
1064 host_integerp (length, 0) == INTEGER_CST
1065 ? tree_low_cst (length, 0) : -1);
1067 /* If compiling to native, pass a reference to the primitive type class
1068 and save the runtime some work. However, the bytecode generator
1069 expects to find the type_code int here. */
1070 if (flag_emit_class_files)
1071 type_arg = build_int_cst (NULL_TREE, atype_value);
1073 type_arg = build_class_ref (prim_type);
1075 return build3 (CALL_EXPR, promote_type (type),
1076 build_address_of (soft_newarray_node),
1077 tree_cons (NULL_TREE,
1079 build_tree_list (NULL_TREE, length)),
1083 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1084 of the dimension. */
1087 build_anewarray (tree class_type, tree length)
1090 = build_java_array_type (class_type,
1091 host_integerp (length, 0)
1092 ? tree_low_cst (length, 0) : -1);
1094 return build3 (CALL_EXPR, promote_type (type),
1095 build_address_of (soft_anewarray_node),
1096 tree_cons (NULL_TREE, length,
1097 tree_cons (NULL_TREE, build_class_ref (class_type),
1098 build_tree_list (NULL_TREE,
1099 null_pointer_node))),
1103 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1106 build_new_array (tree type, tree length)
1108 if (JPRIMITIVE_TYPE_P (type))
1109 return build_newarray (encode_newarray_type (type), length);
1111 return build_anewarray (TREE_TYPE (type), length);
1114 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1115 class pointer, a number of dimensions and the matching number of
1116 dimensions. The argument list is NULL terminated. */
1119 expand_java_multianewarray (tree class_type, int ndim)
1122 tree args = build_tree_list( NULL_TREE, null_pointer_node );
1124 for( i = 0; i < ndim; i++ )
1125 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1127 push_value (build3 (CALL_EXPR,
1128 promote_type (class_type),
1129 build_address_of (soft_multianewarray_node),
1130 tree_cons (NULL_TREE, build_class_ref (class_type),
1131 tree_cons (NULL_TREE,
1132 build_int_cst (NULL_TREE, ndim),
1137 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1138 ARRAY is an array type. May expand some bound checking and NULL
1139 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1140 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1141 INT. In those cases, we make the conversion.
1143 if ARRAy is a reference type, the assignment is checked at run-time
1144 to make sure that the RHS can be assigned to the array element
1145 type. It is not necessary to generate this code if ARRAY is final. */
1148 expand_java_arraystore (tree rhs_type_node)
1150 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
1151 && TYPE_PRECISION (rhs_type_node) <= 32) ?
1152 int_type_node : rhs_type_node);
1153 tree index = pop_value (int_type_node);
1154 tree array_type, array;
1156 if (flag_new_verifier)
1158 /* If we're processing an `aaload' we might as well just pick
1160 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1162 array_type = build_java_array_type (object_ptr_type_node, -1);
1163 rhs_type_node = object_ptr_type_node;
1166 array_type = build_java_array_type (rhs_type_node, -1);
1169 array_type = ptr_type_node;
1170 array = pop_value (array_type);
1171 if (flag_new_verifier)
1172 array = build1 (NOP_EXPR, promote_type (array_type), array);
1174 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1176 flush_quick_stack ();
1178 index = save_expr (index);
1179 array = save_expr (array);
1181 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1183 tree check = build_java_arraystore_check (array, rhs_node);
1184 java_add_stmt (check);
1187 array = build_java_arrayaccess (array, rhs_type_node, index);
1188 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1191 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1192 sure that LHS is an array type. May expand some bound checking and NULL
1194 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1195 BOOLEAN/SHORT, we push a promoted type back to the stack.
1199 expand_java_arrayload (tree lhs_type_node)
1202 tree index_node = pop_value (int_type_node);
1206 if (flag_new_verifier)
1208 /* If we're processing an `aaload' we might as well just pick
1210 if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1212 array_type = build_java_array_type (object_ptr_type_node, -1);
1213 lhs_type_node = object_ptr_type_node;
1216 array_type = build_java_array_type (lhs_type_node, -1);
1219 array_type = ptr_type_node;
1220 array_node = pop_value (array_type);
1221 if (flag_new_verifier)
1222 array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1224 index_node = save_expr (index_node);
1225 array_node = save_expr (array_node);
1227 if (TREE_TYPE (array_node) == ptr_type_node)
1228 /* The only way we could get a node of type ptr_type_node at this
1229 point is `aconst_null; arraylength' or something equivalent, so
1230 unconditionally throw NullPointerException. */
1231 load_node = build3 (CALL_EXPR, lhs_type_node,
1232 build_address_of (soft_nullpointer_node),
1233 NULL_TREE, NULL_TREE);
1236 lhs_type_node = build_java_check_indexed_type (array_node,
1238 load_node = build_java_arrayaccess (array_node,
1242 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1243 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
1244 push_value (load_node);
1247 /* Expands .length. Makes sure that we deal with and array and may expand
1248 a NULL check on the array object. */
1251 expand_java_array_length (void)
1253 tree array = pop_value (ptr_type_node);
1254 tree length = build_java_array_length_access (array);
1256 push_value (length);
1259 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1260 either soft_monitorenter_node or soft_monitorexit_node. */
1263 build_java_monitor (tree call, tree object)
1265 return build3 (CALL_EXPR,
1267 build_address_of (call),
1268 build_tree_list (NULL_TREE, object),
1272 /* Emit code for one of the PUSHC instructions. */
1275 expand_java_pushc (int ival, tree type)
1278 if (type == ptr_type_node && ival == 0)
1279 value = null_pointer_node;
1280 else if (type == int_type_node || type == long_type_node)
1281 value = build_int_cst (type, ival);
1282 else if (type == float_type_node || type == double_type_node)
1285 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1286 value = build_real (type, x);
1295 expand_java_return (tree type)
1297 if (type == void_type_node)
1298 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1301 tree retval = pop_value (type);
1302 tree res = DECL_RESULT (current_function_decl);
1303 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1305 /* Handle the situation where the native integer type is smaller
1306 than the JVM integer. It can happen for many cross compilers.
1307 The whole if expression just goes away if INT_TYPE_SIZE < 32
1309 if (INT_TYPE_SIZE < 32
1310 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1311 < GET_MODE_SIZE (TYPE_MODE (type))))
1312 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1314 TREE_SIDE_EFFECTS (retval) = 1;
1315 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1320 expand_load_internal (int index, tree type, int pc)
1323 tree var = find_local_variable (index, type, pc);
1325 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1326 on the stack. If there is an assignment to this VAR_DECL between
1327 the stack push and the use, then the wrong code could be
1328 generated. To avoid this we create a new local and copy our
1329 value into it. Then we push this new local on the stack.
1330 Hopefully this all gets optimized out. */
1331 copy = build_decl (VAR_DECL, NULL_TREE, type);
1332 if (INTEGRAL_TYPE_P (type)
1333 && TREE_TYPE (copy) != TREE_TYPE (var))
1334 var = convert (type, var);
1335 java_add_local_var (copy);
1336 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1342 build_address_of (tree value)
1344 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1348 class_has_finalize_method (tree type)
1350 tree super = CLASSTYPE_SUPER (type);
1352 if (super == NULL_TREE)
1353 return false; /* Every class with a real finalizer inherits */
1354 /* from java.lang.Object. */
1356 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1360 java_create_object (tree type)
1362 tree alloc_node = (class_has_finalize_method (type)
1364 : alloc_no_finalizer_node);
1366 return build (CALL_EXPR, promote_type (type),
1367 build_address_of (alloc_node),
1368 build_tree_list (NULL_TREE, build_class_ref (type)),
1373 expand_java_NEW (tree type)
1377 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1378 : alloc_no_finalizer_node);
1379 if (! CLASS_LOADED_P (type))
1380 load_class (type, 1);
1381 safe_layout_class (type);
1382 push_value (build3 (CALL_EXPR, promote_type (type),
1383 build_address_of (alloc_node),
1384 build_tree_list (NULL_TREE, build_class_ref (type)),
1388 /* This returns an expression which will extract the class of an
1392 build_get_class (tree value)
1394 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1395 tree vtable_field = lookup_field (&object_type_node,
1396 get_identifier ("vtable"));
1397 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1398 build_java_indirect_ref (object_type_node, value,
1399 flag_check_references),
1400 vtable_field, NULL_TREE);
1401 return build3 (COMPONENT_REF, class_ptr_type,
1402 build1 (INDIRECT_REF, dtable_type, tmp),
1403 class_field, NULL_TREE);
1406 /* This builds the tree representation of the `instanceof' operator.
1407 It tries various tricks to optimize this in cases where types are
1411 build_instanceof (tree value, tree type)
1414 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1415 tree valtype = TREE_TYPE (TREE_TYPE (value));
1416 tree valclass = TYPE_NAME (valtype);
1419 /* When compiling from bytecode, we need to ensure that TYPE has
1421 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1423 load_class (type, 1);
1424 safe_layout_class (type);
1425 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1426 return error_mark_node;
1428 klass = TYPE_NAME (type);
1430 if (type == object_type_node || inherits_from_p (valtype, type))
1432 /* Anything except `null' is an instance of Object. Likewise,
1433 if the object is known to be an instance of the class, then
1434 we only need to check for `null'. */
1435 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1437 else if (flag_verify_invocations
1438 && ! TYPE_ARRAY_P (type)
1439 && ! TYPE_ARRAY_P (valtype)
1440 && DECL_P (klass) && DECL_P (valclass)
1441 && ! CLASS_INTERFACE (valclass)
1442 && ! CLASS_INTERFACE (klass)
1443 && ! inherits_from_p (type, valtype)
1444 && (CLASS_FINAL (klass)
1445 || ! inherits_from_p (valtype, type)))
1447 /* The classes are from different branches of the derivation
1448 tree, so we immediately know the answer. */
1449 expr = boolean_false_node;
1451 else if (DECL_P (klass) && CLASS_FINAL (klass))
1453 tree save = save_expr (value);
1454 expr = build3 (COND_EXPR, itype,
1455 build2 (NE_EXPR, boolean_type_node,
1456 save, null_pointer_node),
1457 build2 (EQ_EXPR, itype,
1458 build_get_class (save),
1459 build_class_ref (type)),
1460 boolean_false_node);
1464 expr = build3 (CALL_EXPR, itype,
1465 build_address_of (soft_instanceof_node),
1466 tree_cons (NULL_TREE, value,
1467 build_tree_list (NULL_TREE,
1468 build_class_ref (type))),
1471 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1476 expand_java_INSTANCEOF (tree type)
1478 tree value = pop_value (object_ptr_type_node);
1479 value = build_instanceof (value, type);
1484 expand_java_CHECKCAST (tree type)
1486 tree value = pop_value (ptr_type_node);
1487 value = build3 (CALL_EXPR, promote_type (type),
1488 build_address_of (soft_checkcast_node),
1489 tree_cons (NULL_TREE, build_class_ref (type),
1490 build_tree_list (NULL_TREE, value)),
1496 expand_iinc (unsigned int local_var_index, int ival, int pc)
1498 tree local_var, res;
1499 tree constant_value;
1501 flush_quick_stack ();
1502 local_var = find_local_variable (local_var_index, int_type_node, pc);
1503 constant_value = build_int_cst (NULL_TREE, ival);
1504 res = fold (build2 (PLUS_EXPR, int_type_node, local_var, constant_value));
1505 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1506 update_aliases (local_var, local_var_index, pc);
1511 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1514 tree arg1 = convert (type, op1);
1515 tree arg2 = convert (type, op2);
1517 if (type == int_type_node)
1521 case TRUNC_DIV_EXPR:
1522 call = soft_idiv_node;
1524 case TRUNC_MOD_EXPR:
1525 call = soft_irem_node;
1531 else if (type == long_type_node)
1535 case TRUNC_DIV_EXPR:
1536 call = soft_ldiv_node;
1538 case TRUNC_MOD_EXPR:
1539 call = soft_lrem_node;
1549 call = build3 (CALL_EXPR, type,
1550 build_address_of (call),
1551 tree_cons (NULL_TREE, arg1,
1552 build_tree_list (NULL_TREE, arg2)),
1559 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1566 tree u_type = java_unsigned_type (type);
1567 arg1 = convert (u_type, arg1);
1568 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1569 return convert (type, arg1);
1573 mask = build_int_cst (NULL_TREE,
1574 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1575 arg2 = fold (build2 (BIT_AND_EXPR, int_type_node, arg2, mask));
1578 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1579 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1580 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1582 tree ifexp1 = fold (build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1583 boolean_type_node, arg1, arg2));
1584 tree ifexp2 = fold (build2 (EQ_EXPR, boolean_type_node, arg1, arg2));
1585 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1586 ifexp2, integer_zero_node,
1587 op == COMPARE_L_EXPR
1588 ? integer_minus_one_node
1589 : integer_one_node));
1590 return fold (build3 (COND_EXPR, int_type_node, ifexp1,
1591 op == COMPARE_L_EXPR ? integer_one_node
1592 : integer_minus_one_node,
1596 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1598 tree ifexp1 = fold (build2 (LT_EXPR, boolean_type_node, arg1, arg2));
1599 tree ifexp2 = fold (build2 (GT_EXPR, boolean_type_node, arg1, arg2));
1600 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1601 ifexp2, integer_one_node,
1602 integer_zero_node));
1603 return fold (build3 (COND_EXPR, int_type_node,
1604 ifexp1, integer_minus_one_node, second_compare));
1606 case TRUNC_DIV_EXPR:
1607 case TRUNC_MOD_EXPR:
1608 if (TREE_CODE (type) == REAL_TYPE
1609 && op == TRUNC_MOD_EXPR)
1612 if (type != double_type_node)
1614 arg1 = convert (double_type_node, arg1);
1615 arg2 = convert (double_type_node, arg2);
1617 call = build3 (CALL_EXPR, double_type_node,
1618 build_address_of (soft_fmod_node),
1619 tree_cons (NULL_TREE, arg1,
1620 build_tree_list (NULL_TREE, arg2)),
1622 if (type != double_type_node)
1623 call = convert (type, call);
1627 if (TREE_CODE (type) == INTEGER_TYPE
1628 && flag_use_divide_subroutine
1629 && ! flag_syntax_only)
1630 return build_java_soft_divmod (op, type, arg1, arg2);
1635 return fold (build2 (op, type, arg1, arg2));
1639 expand_java_binop (tree type, enum tree_code op)
1649 rtype = int_type_node;
1650 rarg = pop_value (rtype);
1653 rarg = pop_value (rtype);
1655 larg = pop_value (ltype);
1656 push_value (build_java_binop (op, type, larg, rarg));
1659 /* Lookup the field named NAME in *TYPEP or its super classes.
1660 If not found, return NULL_TREE.
1661 (If the *TYPEP is not found, or if the field reference is
1662 ambiguous, return error_mark_node.)
1663 If found, return the FIELD_DECL, and set *TYPEP to the
1664 class containing the field. */
1667 lookup_field (tree *typep, tree name)
1669 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1671 load_class (*typep, 1);
1672 safe_layout_class (*typep);
1673 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1674 return error_mark_node;
1678 tree field, binfo, base_binfo;
1682 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1683 if (DECL_NAME (field) == name)
1686 /* Process implemented interfaces. */
1687 save_field = NULL_TREE;
1688 for (binfo = TYPE_BINFO (*typep), i = 0;
1689 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1691 tree t = BINFO_TYPE (base_binfo);
1692 if ((field = lookup_field (&t, name)))
1694 if (save_field == field)
1696 if (save_field == NULL_TREE)
1700 tree i1 = DECL_CONTEXT (save_field);
1701 tree i2 = DECL_CONTEXT (field);
1702 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1703 IDENTIFIER_POINTER (name),
1704 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1705 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1706 return error_mark_node;
1711 if (save_field != NULL_TREE)
1714 *typep = CLASSTYPE_SUPER (*typep);
1719 /* Look up the field named NAME in object SELF_VALUE,
1720 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1721 SELF_VALUE is NULL_TREE if looking for a static field. */
1724 build_field_ref (tree self_value, tree self_class, tree name)
1726 tree base_class = self_class;
1727 tree field_decl = lookup_field (&base_class, name);
1728 if (field_decl == NULL_TREE)
1730 error ("field %qs not found", IDENTIFIER_POINTER (name));
1731 return error_mark_node;
1733 if (self_value == NULL_TREE)
1735 return build_static_field_ref (field_decl);
1739 int check = (flag_check_references
1740 && ! (DECL_P (self_value)
1741 && DECL_NAME (self_value) == this_identifier_node));
1743 tree base_type = promote_type (base_class);
1744 if (base_type != TREE_TYPE (self_value))
1745 self_value = fold (build1 (NOP_EXPR, base_type, self_value));
1746 if (! flag_syntax_only
1747 && (flag_indirect_dispatch
1748 /* DECL_FIELD_OFFSET == 0 if we have no reference for
1749 the field, perhaps because we couldn't find the class
1750 in which the field is defined.
1751 FIXME: We should investigate this. */
1752 || DECL_FIELD_OFFSET (field_decl) == 0))
1755 = build_int_cst (NULL_TREE, get_symbol_table_index
1756 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1758 = build4 (ARRAY_REF, integer_type_node,
1759 TYPE_OTABLE_DECL (output_class), otable_index,
1760 NULL_TREE, NULL_TREE);
1763 field_offset = fold (convert (sizetype, field_offset));
1765 = fold (build2 (PLUS_EXPR,
1766 build_pointer_type (TREE_TYPE (field_decl)),
1767 self_value, field_offset));
1768 return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
1771 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1773 return fold (build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1774 self_value, field_decl, NULL_TREE));
1779 lookup_label (int pc)
1783 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1784 name = get_identifier (buf);
1785 if (IDENTIFIER_LOCAL_VALUE (name))
1786 return IDENTIFIER_LOCAL_VALUE (name);
1789 /* The type of the address of a label is return_address_type_node. */
1790 tree decl = create_label_decl (name);
1791 LABEL_PC (decl) = pc;
1792 return pushdecl (decl);
1796 /* Generate a unique name for the purpose of loops and switches
1797 labels, and try-catch-finally blocks label or temporary variables. */
1800 generate_name (void)
1802 static int l_number = 0;
1804 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1806 return get_identifier (buff);
1810 create_label_decl (tree name)
1813 decl = build_decl (LABEL_DECL, name,
1814 TREE_TYPE (return_address_type_node));
1815 DECL_CONTEXT (decl) = current_function_decl;
1816 DECL_IGNORED_P (decl) = 1;
1820 /* This maps a bytecode offset (PC) to various flags. */
1821 char *instruction_bits;
1824 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1826 lookup_label (target_pc);
1827 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1830 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1831 where CONDITION is one of one the compare operators. */
1834 expand_compare (enum tree_code condition, tree value1, tree value2,
1837 tree target = lookup_label (target_pc);
1838 tree cond = fold (build2 (condition, boolean_type_node, value1, value2));
1840 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1841 build1 (GOTO_EXPR, void_type_node, target),
1842 build_java_empty_stmt ()));
1845 /* Emit code for a TEST-type opcode. */
1848 expand_test (enum tree_code condition, tree type, int target_pc)
1850 tree value1, value2;
1851 flush_quick_stack ();
1852 value1 = pop_value (type);
1853 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1854 expand_compare (condition, value1, value2, target_pc);
1857 /* Emit code for a COND-type opcode. */
1860 expand_cond (enum tree_code condition, tree type, int target_pc)
1862 tree value1, value2;
1863 flush_quick_stack ();
1864 /* note: pop values in opposite order */
1865 value2 = pop_value (type);
1866 value1 = pop_value (type);
1867 /* Maybe should check value1 and value2 for type compatibility ??? */
1868 expand_compare (condition, value1, value2, target_pc);
1872 expand_java_goto (int target_pc)
1874 tree target_label = lookup_label (target_pc);
1875 flush_quick_stack ();
1876 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1880 expand_java_switch (tree selector, int default_pc)
1882 tree switch_expr, x;
1884 flush_quick_stack ();
1885 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1886 NULL_TREE, NULL_TREE);
1887 java_add_stmt (switch_expr);
1889 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1890 create_artificial_label ());
1891 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1893 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1894 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1900 expand_java_add_case (tree switch_expr, int match, int target_pc)
1904 value = build_int_cst (TREE_TYPE (switch_expr), match);
1906 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1907 create_artificial_label ());
1908 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1910 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1911 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1915 pop_arguments (tree arg_types)
1917 if (arg_types == end_params_node)
1919 if (TREE_CODE (arg_types) == TREE_LIST)
1921 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1922 tree type = TREE_VALUE (arg_types);
1923 tree arg = pop_value (type);
1925 /* With the new verifier we simply cast each argument to its
1926 proper type. This is needed since we lose type information
1927 coming out of the verifier. We also have to do this with the
1928 old verifier when we pop an integer type that must be
1929 promoted for the function call. */
1930 if (flag_new_verifier && TREE_CODE (type) == POINTER_TYPE)
1931 arg = build1 (NOP_EXPR, type, arg);
1932 else if (targetm.calls.promote_prototypes (type)
1933 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1934 && INTEGRAL_TYPE_P (type))
1935 arg = convert (integer_type_node, arg);
1936 return tree_cons (NULL_TREE, arg, tail);
1941 /* Attach to PTR (a block) the declaration found in ENTRY. */
1944 attach_init_test_initialization_flags (void **entry, void *ptr)
1946 tree block = (tree)ptr;
1947 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1949 if (block != error_mark_node)
1951 if (TREE_CODE (block) == BIND_EXPR)
1953 tree body = BIND_EXPR_BODY (block);
1954 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1955 BIND_EXPR_VARS (block) = ite->value;
1956 body = build2 (COMPOUND_EXPR, void_type_node,
1957 build1 (DECL_EXPR, void_type_node, ite->value), body);
1958 BIND_EXPR_BODY (block) = body;
1962 tree body = BLOCK_SUBBLOCKS (block);
1963 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1964 BLOCK_EXPR_DECLS (block) = ite->value;
1965 body = build2 (COMPOUND_EXPR, void_type_node,
1966 build1 (DECL_EXPR, void_type_node, ite->value), body);
1967 BLOCK_SUBBLOCKS (block) = body;
1974 /* Build an expression to initialize the class CLAS.
1975 if EXPR is non-NULL, returns an expression to first call the initializer
1976 (if it is needed) and then calls EXPR. */
1979 build_class_init (tree clas, tree expr)
1983 /* An optimization: if CLAS is a superclass of the class we're
1984 compiling, we don't need to initialize it. However, if CLAS is
1985 an interface, it won't necessarily be initialized, even if we
1987 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1988 && inherits_from_p (current_class, clas))
1989 || current_class == clas)
1992 if (always_initialize_class_p)
1994 init = build3 (CALL_EXPR, void_type_node,
1995 build_address_of (soft_initclass_node),
1996 build_tree_list (NULL_TREE, build_class_ref (clas)),
1998 TREE_SIDE_EFFECTS (init) = 1;
2002 tree *init_test_decl;
2004 init_test_decl = java_treetreehash_new
2005 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
2007 if (*init_test_decl == NULL)
2009 /* Build a declaration and mark it as a flag used to track
2010 static class initializations. */
2011 decl = build_decl (VAR_DECL, NULL_TREE,
2013 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2014 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
2015 DECL_CONTEXT (decl) = current_function_decl;
2016 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
2017 /* Tell the check-init code to ignore this decl when not
2018 optimizing class initialization. */
2019 if (!STATIC_CLASS_INIT_OPT_P ())
2020 DECL_BIT_INDEX (decl) = -1;
2021 DECL_INITIAL (decl) = boolean_false_node;
2022 /* Don't emit any symbolic debugging info for this decl. */
2023 DECL_IGNORED_P (decl) = 1;
2024 *init_test_decl = decl;
2027 init = build3 (CALL_EXPR, void_type_node,
2028 build_address_of (soft_initclass_node),
2029 build_tree_list (NULL_TREE, build_class_ref (clas)),
2031 TREE_SIDE_EFFECTS (init) = 1;
2032 init = build3 (COND_EXPR, void_type_node,
2033 build2 (EQ_EXPR, boolean_type_node,
2034 *init_test_decl, boolean_false_node),
2035 init, integer_zero_node);
2036 TREE_SIDE_EFFECTS (init) = 1;
2037 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
2038 build2 (MODIFY_EXPR, boolean_type_node,
2039 *init_test_decl, boolean_true_node));
2040 TREE_SIDE_EFFECTS (init) = 1;
2043 if (expr != NULL_TREE)
2045 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2046 TREE_SIDE_EFFECTS (expr) = 1;
2053 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2054 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2055 tree arg_list ATTRIBUTE_UNUSED)
2058 if (is_compiled_class (self_type))
2060 /* With indirect dispatch we have to use indirect calls for all
2061 publicly visible methods or gcc will use PLT indirections
2062 to reach them. We also have to use indirect dispatch for all
2063 external methods. */
2064 if (! flag_indirect_dispatch
2065 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2067 make_decl_rtl (method);
2068 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2074 = build_int_cst (NULL_TREE, get_symbol_table_index
2075 (method, &TYPE_ATABLE_METHODS (output_class)));
2077 = build4 (ARRAY_REF,
2078 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2079 TYPE_ATABLE_DECL (output_class), table_index,
2080 NULL_TREE, NULL_TREE);
2082 func = convert (method_ptr_type_node, func);
2086 /* We don't know whether the method has been (statically) compiled.
2087 Compile this code to get a reference to the method's code:
2089 SELF_TYPE->methods[METHOD_INDEX].ncode
2093 int method_index = 0;
2096 /* The method might actually be declared in some superclass, so
2097 we have to use its class context, not the caller's notion of
2098 where the method is. */
2099 self_type = DECL_CONTEXT (method);
2100 ref = build_class_ref (self_type);
2101 ref = build1 (INDIRECT_REF, class_type_node, ref);
2102 if (ncode_ident == NULL_TREE)
2103 ncode_ident = get_identifier ("ncode");
2104 if (methods_ident == NULL_TREE)
2105 methods_ident = get_identifier ("methods");
2106 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2107 lookup_field (&class_type_node, methods_ident),
2109 for (meth = TYPE_METHODS (self_type);
2110 ; meth = TREE_CHAIN (meth))
2114 if (meth == NULL_TREE)
2115 fatal_error ("method '%s' not found in class",
2116 IDENTIFIER_POINTER (DECL_NAME (method)));
2119 method_index *= int_size_in_bytes (method_type_node);
2120 ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
2121 ref, build_int_cst (NULL_TREE, method_index)));
2122 ref = build1 (INDIRECT_REF, method_type_node, ref);
2123 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2124 ref, lookup_field (&method_type_node, ncode_ident),
2131 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2133 tree dtable, objectref;
2135 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2137 /* If we're dealing with interfaces and if the objectref
2138 argument is an array then get the dispatch table of the class
2139 Object rather than the one from the objectref. */
2140 objectref = (is_invoke_interface
2141 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2142 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2144 if (dtable_ident == NULL_TREE)
2145 dtable_ident = get_identifier ("vtable");
2146 dtable = build_java_indirect_ref (object_type_node, objectref,
2147 flag_check_references);
2148 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2149 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2154 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2155 T. If this decl has not been seen before, it will be added to the
2156 [oa]table_methods. If it has, the existing table slot will be
2160 get_symbol_table_index (tree t, tree *symbol_table)
2165 if (*symbol_table == NULL_TREE)
2167 *symbol_table = build_tree_list (t, t);
2171 method_list = *symbol_table;
2175 tree value = TREE_VALUE (method_list);
2179 if (TREE_CHAIN (method_list) == NULL_TREE)
2182 method_list = TREE_CHAIN (method_list);
2185 TREE_CHAIN (method_list) = build_tree_list (t, t);
2190 build_invokevirtual (tree dtable, tree method)
2193 tree nativecode_ptr_ptr_type_node
2194 = build_pointer_type (nativecode_ptr_type_node);
2198 if (flag_indirect_dispatch)
2200 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2204 = build_int_cst (NULL_TREE, get_symbol_table_index
2205 (method, &TYPE_OTABLE_METHODS (output_class)));
2206 method_index = build4 (ARRAY_REF, integer_type_node,
2207 TYPE_OTABLE_DECL (output_class),
2208 otable_index, NULL_TREE, NULL_TREE);
2212 /* We fetch the DECL_VINDEX field directly here, rather than
2213 using get_method_index(). DECL_VINDEX is the true offset
2214 from the vtable base to a method, regrdless of any extra
2215 words inserted at the start of the vtable. */
2216 method_index = DECL_VINDEX (method);
2217 method_index = size_binop (MULT_EXPR, method_index,
2218 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2219 if (TARGET_VTABLE_USES_DESCRIPTORS)
2220 method_index = size_binop (MULT_EXPR, method_index,
2221 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2224 func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2225 convert (nativecode_ptr_ptr_type_node, method_index)));
2227 if (TARGET_VTABLE_USES_DESCRIPTORS)
2228 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2230 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2235 static GTY(()) tree class_ident;
2237 build_invokeinterface (tree dtable, tree method)
2243 /* We expand invokeinterface here. */
2245 if (class_ident == NULL_TREE)
2246 class_ident = get_identifier ("class");
2248 dtable = build_java_indirect_ref (dtable_type, dtable,
2249 flag_check_references);
2250 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2251 lookup_field (&dtable_type, class_ident), NULL_TREE);
2253 interface = DECL_CONTEXT (method);
2254 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2256 layout_class_methods (interface);
2258 if (flag_indirect_dispatch)
2261 = 2 * (get_symbol_table_index
2262 (method, &TYPE_ITABLE_METHODS (output_class)));
2264 = build4 (ARRAY_REF,
2265 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2266 TYPE_ITABLE_DECL (output_class),
2267 build_int_cst (NULL_TREE, itable_index-1),
2268 NULL_TREE, NULL_TREE);
2270 = build4 (ARRAY_REF,
2271 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2272 TYPE_ITABLE_DECL (output_class),
2273 build_int_cst (NULL_TREE, itable_index),
2274 NULL_TREE, NULL_TREE);
2275 interface = convert (class_ptr_type, interface);
2276 idx = convert (integer_type_node, idx);
2280 idx = build_int_cst (NULL_TREE,
2281 get_interface_method_index (method, interface));
2282 interface = build_class_ref (interface);
2285 lookup_arg = tree_cons (NULL_TREE, dtable,
2286 tree_cons (NULL_TREE, interface,
2287 build_tree_list (NULL_TREE, idx)));
2289 return build3 (CALL_EXPR, ptr_type_node,
2290 build_address_of (soft_lookupinterfacemethod_node),
2291 lookup_arg, NULL_TREE);
2294 /* Expand one of the invoke_* opcodes.
2295 OPCODE is the specific opcode.
2296 METHOD_REF_INDEX is an index into the constant pool.
2297 NARGS is the number of arguments, or -1 if not specified. */
2300 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2302 tree method_signature
2303 = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
2304 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool,
2307 = get_class_constant (current_jcf,
2308 COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool,
2310 const char *const self_name
2311 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2312 tree call, func, method, arg_list, method_type;
2313 tree check = NULL_TREE;
2315 if (! CLASS_LOADED_P (self_type))
2317 load_class (self_type, 1);
2318 safe_layout_class (self_type);
2319 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2320 fatal_error ("failed to find class '%s'", self_name);
2322 layout_class_methods (self_type);
2324 if (ID_INIT_P (method_name))
2325 method = lookup_java_constructor (self_type, method_signature);
2327 method = lookup_java_method (self_type, method_name, method_signature);
2329 /* We've found a method in an interface, but this isn't an interface
2331 if (opcode != OPCODE_invokeinterface
2333 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2336 /* We've found a non-interface method but we are making an
2337 interface call. This can happen if the interface overrides a
2338 method in Object. */
2339 if (! flag_verify_invocations
2340 && opcode == OPCODE_invokeinterface
2342 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2345 if (method == NULL_TREE)
2347 if (flag_verify_invocations || ! flag_indirect_dispatch)
2349 error ("class '%s' has no method named '%s' matching signature '%s'",
2351 IDENTIFIER_POINTER (method_name),
2352 IDENTIFIER_POINTER (method_signature));
2356 int flags = ACC_PUBLIC;
2357 if (opcode == OPCODE_invokestatic)
2358 flags |= ACC_STATIC;
2359 if (opcode == OPCODE_invokeinterface)
2361 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2362 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2364 method = add_method (self_type, flags, method_name,
2366 DECL_ARTIFICIAL (method) = 1;
2367 METHOD_DUMMY (method) = 1;
2368 layout_class_method (self_type, NULL,
2373 /* Invoke static can't invoke static/abstract method */
2374 if (method != NULL_TREE)
2376 if (opcode == OPCODE_invokestatic)
2378 if (!METHOD_STATIC (method))
2380 error ("invokestatic on non static method");
2383 else if (METHOD_ABSTRACT (method))
2385 error ("invokestatic on abstract method");
2391 if (METHOD_STATIC (method))
2393 error ("invoke[non-static] on static method");
2399 if (method == NULL_TREE)
2401 /* If we got here, we emitted an error message above. So we
2402 just pop the arguments, push a properly-typed zero, and
2404 method_type = get_type_from_signature (method_signature);
2405 pop_arguments (TYPE_ARG_TYPES (method_type));
2406 if (opcode != OPCODE_invokestatic)
2407 pop_type (self_type);
2408 method_type = promote_type (TREE_TYPE (method_type));
2409 push_value (convert (method_type, integer_zero_node));
2413 method_type = TREE_TYPE (method);
2414 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2415 flush_quick_stack ();
2418 if (opcode == OPCODE_invokestatic)
2419 func = build_known_method_ref (method, method_type, self_type,
2420 method_signature, arg_list);
2421 else if (opcode == OPCODE_invokespecial
2422 || (opcode == OPCODE_invokevirtual
2423 && (METHOD_PRIVATE (method)
2424 || METHOD_FINAL (method)
2425 || CLASS_FINAL (TYPE_NAME (self_type)))))
2427 /* If the object for the method call is null, we throw an
2428 exception. We don't do this if the object is the current
2429 method's `this'. In other cases we just rely on an
2430 optimization pass to eliminate redundant checks. FIXME:
2431 Unfortunately there doesn't seem to be a way to determine
2432 what the current method is right now.
2433 We do omit the check if we're calling <init>. */
2434 /* We use a SAVE_EXPR here to make sure we only evaluate
2435 the new `self' expression once. */
2436 tree save_arg = save_expr (TREE_VALUE (arg_list));
2437 TREE_VALUE (arg_list) = save_arg;
2438 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2439 func = build_known_method_ref (method, method_type, self_type,
2440 method_signature, arg_list);
2444 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2446 if (opcode == OPCODE_invokevirtual)
2447 func = build_invokevirtual (dtable, method);
2449 func = build_invokeinterface (dtable, method);
2452 if (TREE_CODE (func) == ADDR_EXPR)
2453 TREE_TYPE (func) = build_pointer_type (method_type);
2455 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2457 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2458 func, arg_list, NULL_TREE);
2459 TREE_SIDE_EFFECTS (call) = 1;
2460 call = check_for_builtin (method, call);
2462 if (check != NULL_TREE)
2464 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2465 TREE_SIDE_EFFECTS (call) = 1;
2468 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2469 java_add_stmt (call);
2473 flush_quick_stack ();
2477 /* Create a stub which will be put into the vtable but which will call
2481 build_jni_stub (tree method)
2483 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2484 tree jni_func_type, tem;
2485 tree env_var, res_var = NULL_TREE, block;
2486 tree method_args, res_type;
2492 tree klass = DECL_CONTEXT (method);
2493 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2494 klass = build_class_ref (klass);
2496 if (! METHOD_NATIVE (method) || ! flag_jni)
2499 DECL_ARTIFICIAL (method) = 1;
2500 DECL_EXTERNAL (method) = 0;
2502 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2503 DECL_CONTEXT (env_var) = method;
2505 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2507 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2508 TREE_TYPE (TREE_TYPE (method)));
2509 DECL_CONTEXT (res_var) = method;
2510 TREE_CHAIN (env_var) = res_var;
2513 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2514 TREE_STATIC (meth_var) = 1;
2515 TREE_PUBLIC (meth_var) = 0;
2516 DECL_EXTERNAL (meth_var) = 0;
2517 DECL_CONTEXT (meth_var) = method;
2518 DECL_ARTIFICIAL (meth_var) = 1;
2519 DECL_INITIAL (meth_var) = null_pointer_node;
2520 TREE_USED (meth_var) = 1;
2521 chainon (env_var, meth_var);
2522 build_result_decl (method);
2524 /* One strange way that the front ends are different is that they
2525 store arguments differently. */
2527 method_args = DECL_ARGUMENTS (method);
2529 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2530 block = build_block (env_var, NULL_TREE, NULL_TREE,
2531 method_args, NULL_TREE);
2532 TREE_SIDE_EFFECTS (block) = 1;
2533 /* When compiling from source we don't set the type of the block,
2534 because that will prevent patch_return from ever being run. */
2536 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2538 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2539 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2540 build3 (CALL_EXPR, ptr_type_node,
2541 build_address_of (soft_getjnienvnewframe_node),
2542 build_tree_list (NULL_TREE, klass),
2544 CAN_COMPLETE_NORMALLY (body) = 1;
2546 /* All the arguments to this method become arguments to the
2547 underlying JNI function. If we had to wrap object arguments in a
2548 special way, we would do that here. */
2550 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2552 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (tem)));
2553 #ifdef PARM_BOUNDARY
2554 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2557 args_size += (arg_bits / BITS_PER_UNIT);
2559 args = tree_cons (NULL_TREE, tem, args);
2561 args = nreverse (args);
2562 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2564 /* For a static method the second argument is the class. For a
2565 non-static method the second argument is `this'; that is already
2566 available in the argument list. */
2567 if (METHOD_STATIC (method))
2569 args_size += int_size_in_bytes (TREE_TYPE (klass));
2570 args = tree_cons (NULL_TREE, klass, args);
2571 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2574 /* The JNIEnv structure is the first argument to the JNI function. */
2575 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2576 args = tree_cons (NULL_TREE, env_var, args);
2577 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2579 /* We call _Jv_LookupJNIMethod to find the actual underlying
2580 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2581 exception if this function is not found at runtime. */
2582 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2583 method_sig = build_java_signature (TREE_TYPE (method));
2584 lookup_arg = tree_cons (NULL_TREE,
2585 build_utf8_ref (unmangle_classname
2586 (IDENTIFIER_POINTER (method_sig),
2587 IDENTIFIER_LENGTH (method_sig))),
2589 tem = DECL_NAME (method);
2591 = tree_cons (NULL_TREE, klass,
2592 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2594 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2596 #ifdef MODIFY_JNI_METHOD_CALL
2597 tem = MODIFY_JNI_METHOD_CALL (tem);
2600 jni_func_type = build_pointer_type (tem);
2602 jnifunc = build3 (COND_EXPR, ptr_type_node,
2604 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2605 build3 (CALL_EXPR, ptr_type_node,
2607 (soft_lookupjnimethod_node),
2608 lookup_arg, NULL_TREE)));
2610 /* Now we make the actual JNI call via the resulting function
2612 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2613 build1 (NOP_EXPR, jni_func_type, jnifunc),
2616 /* If the JNI call returned a result, capture it here. If we had to
2617 unwrap JNI object results, we would do that here. */
2618 if (res_var != NULL_TREE)
2619 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2622 TREE_SIDE_EFFECTS (call) = 1;
2623 CAN_COMPLETE_NORMALLY (call) = 1;
2625 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2626 TREE_SIDE_EFFECTS (body) = 1;
2628 /* Now free the environment we allocated. */
2629 call = build3 (CALL_EXPR, ptr_type_node,
2630 build_address_of (soft_jnipopsystemframe_node),
2631 build_tree_list (NULL_TREE, env_var),
2633 TREE_SIDE_EFFECTS (call) = 1;
2634 CAN_COMPLETE_NORMALLY (call) = 1;
2635 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2636 TREE_SIDE_EFFECTS (body) = 1;
2638 /* Finally, do the return. */
2639 res_type = void_type_node;
2640 if (res_var != NULL_TREE)
2643 if (! DECL_RESULT (method))
2645 /* Make sure we copy the result variable to the actual
2646 result. We use the type of the DECL_RESULT because it
2647 might be different from the return type of the function:
2648 it might be promoted. */
2649 drt = TREE_TYPE (DECL_RESULT (method));
2650 if (drt != TREE_TYPE (res_var))
2651 res_var = build1 (CONVERT_EXPR, drt, res_var);
2652 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2653 TREE_SIDE_EFFECTS (res_var) = 1;
2656 body = build2 (COMPOUND_EXPR, void_type_node, body,
2657 build1 (RETURN_EXPR, res_type, res_var));
2658 TREE_SIDE_EFFECTS (body) = 1;
2660 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2665 /* Expand an operation to extract from or store into a field.
2666 IS_STATIC is 1 iff the field is static.
2667 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2668 FIELD_REF_INDEX is an index into the constant pool. */
2671 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2674 = get_class_constant (current_jcf,
2675 COMPONENT_REF_CLASS_INDEX (¤t_jcf->cpool,
2677 const char *self_name
2678 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2679 tree field_name = COMPONENT_REF_NAME (¤t_jcf->cpool, field_ref_index);
2680 tree field_signature = COMPONENT_REF_SIGNATURE (¤t_jcf->cpool,
2682 tree field_type = get_type_from_signature (field_signature);
2683 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2686 tree original_self_type = self_type;
2689 if (! CLASS_LOADED_P (self_type))
2690 load_class (self_type, 1);
2691 field_decl = lookup_field (&self_type, field_name);
2692 if (field_decl == error_mark_node)
2696 else if (field_decl == NULL_TREE)
2698 if (! flag_verify_invocations)
2700 int flags = ACC_PUBLIC;
2702 flags |= ACC_STATIC;
2703 self_type = original_self_type;
2704 field_decl = add_field (original_self_type, field_name,
2706 DECL_ARTIFICIAL (field_decl) = 1;
2707 DECL_IGNORED_P (field_decl) = 1;
2711 error ("missing field '%s' in '%s'",
2712 IDENTIFIER_POINTER (field_name), self_name);
2716 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2718 error ("mismatching signature for field '%s' in '%s'",
2719 IDENTIFIER_POINTER (field_name), self_name);
2722 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2726 push_value (convert (field_type, integer_zero_node));
2727 flush_quick_stack ();
2731 field_ref = build_field_ref (field_ref, self_type, field_name);
2733 field_ref = build_class_init (self_type, field_ref);
2736 flush_quick_stack ();
2737 if (FIELD_FINAL (field_decl))
2739 if (DECL_CONTEXT (field_decl) != current_class)
2740 error ("%Jassignment to final field '%D' not in field's class",
2741 field_decl, field_decl);
2742 else if (FIELD_STATIC (field_decl))
2744 if (!DECL_CLINIT_P (current_function_decl))
2745 warning ("%Jassignment to final static field %qD not in "
2746 "class initializer",
2747 field_decl, field_decl);
2751 tree cfndecl_name = DECL_NAME (current_function_decl);
2752 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2753 && !ID_FINIT_P (cfndecl_name))
2754 warning ("%Jassignment to final field '%D' not in constructor",
2755 field_decl, field_decl);
2758 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2759 field_ref, new_value));
2762 push_value (field_ref);
2766 load_type_state (tree label)
2769 tree vec = LABEL_TYPE_STATE (label);
2770 int cur_length = TREE_VEC_LENGTH (vec);
2771 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2772 for (i = 0; i < cur_length; i++)
2773 type_map [i] = TREE_VEC_ELT (vec, i);
2776 /* Go over METHOD's bytecode and note instruction starts in
2777 instruction_bits[]. */
2780 note_instructions (JCF *jcf, tree method)
2783 unsigned char* byte_ops;
2784 long length = DECL_CODE_LENGTH (method);
2789 #undef RET /* Defined by config/i386/i386.h */
2791 #define BCODE byte_ops
2792 #define BYTE_type_node byte_type_node
2793 #define SHORT_type_node short_type_node
2794 #define INT_type_node int_type_node
2795 #define LONG_type_node long_type_node
2796 #define CHAR_type_node char_type_node
2797 #define PTR_type_node ptr_type_node
2798 #define FLOAT_type_node float_type_node
2799 #define DOUBLE_type_node double_type_node
2800 #define VOID_type_node void_type_node
2801 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2802 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2803 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2804 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2806 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2808 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2809 byte_ops = jcf->read_ptr;
2810 instruction_bits = xrealloc (instruction_bits, length + 1);
2811 memset (instruction_bits, 0, length + 1);
2813 /* This pass figures out which PC can be the targets of jumps. */
2814 for (PC = 0; PC < length;)
2816 int oldpc = PC; /* PC at instruction start. */
2817 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2818 switch (byte_ops[PC++])
2820 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2822 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2825 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2827 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2828 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2829 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2830 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2831 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2832 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2833 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2834 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2836 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2837 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2838 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2839 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2840 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2841 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2842 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2843 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2845 /* two forms of wide instructions */
2846 #define PRE_SPECIAL_WIDE(IGNORE) \
2848 int modified_opcode = IMMEDIATE_u1; \
2849 if (modified_opcode == OPCODE_iinc) \
2851 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2852 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2856 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2860 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2862 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2864 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2865 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2866 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2867 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2868 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2869 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2870 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2871 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2872 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2873 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2875 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2876 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2877 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2878 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2879 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2880 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2881 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2883 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2885 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2887 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2888 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2890 #define PRE_LOOKUP_SWITCH \
2891 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2892 NOTE_LABEL (default_offset+oldpc); \
2894 while (--npairs >= 0) { \
2895 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2896 jint offset = IMMEDIATE_s4; \
2897 NOTE_LABEL (offset+oldpc); } \
2900 #define PRE_TABLE_SWITCH \
2901 { jint default_offset = IMMEDIATE_s4; \
2902 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2903 NOTE_LABEL (default_offset+oldpc); \
2905 while (low++ <= high) { \
2906 jint offset = IMMEDIATE_s4; \
2907 NOTE_LABEL (offset+oldpc); } \
2910 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2911 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2912 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2913 (void)(IMMEDIATE_u2); \
2914 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2916 #include "javaop.def"
2923 expand_byte_code (JCF *jcf, tree method)
2927 const unsigned char *linenumber_pointer;
2928 int dead_code_index = -1;
2929 unsigned char* byte_ops;
2930 long length = DECL_CODE_LENGTH (method);
2933 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2934 byte_ops = jcf->read_ptr;
2936 /* We make an initial pass of the line number table, to note
2937 which instructions have associated line number entries. */
2938 linenumber_pointer = linenumber_table;
2939 for (i = 0; i < linenumber_count; i++)
2941 int pc = GET_u2 (linenumber_pointer);
2942 linenumber_pointer += 4;
2944 warning ("invalid PC in line number table");
2947 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2948 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2949 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2953 if (flag_new_verifier)
2955 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2960 if (! verify_jvm_instructions (jcf, byte_ops, length))
2964 /* Translate bytecodes. */
2965 linenumber_pointer = linenumber_table;
2966 for (PC = 0; PC < length;)
2968 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2970 tree label = lookup_label (PC);
2971 flush_quick_stack ();
2972 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2973 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2974 if (LABEL_VERIFIED (label) || PC == 0)
2975 load_type_state (label);
2978 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2980 if (dead_code_index == -1)
2982 /* This is the start of a region of unreachable bytecodes.
2983 They still need to be processed in order for EH ranges
2984 to get handled correctly. However, we can simply
2985 replace these bytecodes with nops. */
2986 dead_code_index = PC;
2989 /* Turn this bytecode into a nop. */
2994 if (dead_code_index != -1)
2996 /* We've just reached the end of a region of dead code. */
2998 warning ("unreachable bytecode from %d to before %d",
2999 dead_code_index, PC);
3000 dead_code_index = -1;
3004 /* Handle possible line number entry for this PC.
3006 This code handles out-of-order and multiple linenumbers per PC,
3007 but is optimized for the case of line numbers increasing
3008 monotonically with PC. */
3009 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
3011 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
3012 || GET_u2 (linenumber_pointer) != PC)
3013 linenumber_pointer = linenumber_table;
3014 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
3016 int pc = GET_u2 (linenumber_pointer);
3017 linenumber_pointer += 4;
3020 int line = GET_u2 (linenumber_pointer - 2);
3021 #ifdef USE_MAPPED_LOCATION
3022 input_location = linemap_line_start (&line_table, line, 1);
3024 input_location.line = line;
3026 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
3031 maybe_pushlevels (PC);
3032 PC = process_jvm_instruction (PC, byte_ops, length);
3033 maybe_poplevels (PC);
3036 if (dead_code_index != -1)
3038 /* We've just reached the end of a region of dead code. */
3040 warning ("unreachable bytecode from %d to the end of the method",
3046 java_push_constant_from_pool (JCF *jcf, int index)
3049 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3052 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3053 index = alloc_name_constant (CONSTANT_String, name);
3054 c = build_ref_from_constant_pool (index);
3055 c = convert (promote_type (string_type_node), c);
3058 c = get_constant (jcf, index);
3063 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3064 long length ATTRIBUTE_UNUSED)
3066 const char *opname; /* Temporary ??? */
3067 int oldpc = PC; /* PC at instruction start. */
3069 /* If the instruction is at the beginning of a exception handler,
3070 replace the top of the stack with the thrown object reference */
3071 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3073 /* Note that the new verifier will not emit a type map at all
3074 for dead exception handlers. In this case we just ignore
3076 if (! flag_new_verifier || (instruction_bits[PC] & BCODE_VERIFIED) != 0)
3078 tree type = pop_type (promote_type (throwable_type_node));
3079 push_value (build_exception_object_ref (type));
3083 switch (byte_ops[PC++])
3085 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3088 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3091 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3093 int saw_index = 0; \
3094 int index = OPERAND_VALUE; \
3096 (find_local_variable (index, return_address_type_node, oldpc)); \
3099 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3101 /* OPERAND_VALUE may have side-effects on PC */ \
3102 int opvalue = OPERAND_VALUE; \
3103 build_java_jsr (oldpc + opvalue, PC); \
3106 /* Push a constant onto the stack. */
3107 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3108 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3109 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3110 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3112 /* internal macro added for use by the WIDE case */
3113 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3114 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3116 /* Push local variable onto the opcode stack. */
3117 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3119 /* have to do this since OPERAND_VALUE may have side-effects */ \
3120 int opvalue = OPERAND_VALUE; \
3121 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3124 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3125 expand_java_return (OPERAND_TYPE##_type_node)
3127 #define REM_EXPR TRUNC_MOD_EXPR
3128 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3129 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3131 #define FIELD(IS_STATIC, IS_PUT) \
3132 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3134 #define TEST(OPERAND_TYPE, CONDITION) \
3135 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3137 #define COND(OPERAND_TYPE, CONDITION) \
3138 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3140 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3141 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3143 #define BRANCH_GOTO(OPERAND_VALUE) \
3144 expand_java_goto (oldpc + OPERAND_VALUE)
3146 #define BRANCH_CALL(OPERAND_VALUE) \
3147 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3150 #define BRANCH_RETURN(OPERAND_VALUE) \
3152 tree type = OPERAND_TYPE##_type_node; \
3153 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3154 expand_java_ret (value); \
3158 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3159 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3160 fprintf (stderr, "(not implemented)\n")
3161 #define NOT_IMPL1(OPERAND_VALUE) \
3162 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3163 fprintf (stderr, "(not implemented)\n")
3165 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3167 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3169 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3171 #define STACK_SWAP(COUNT) java_stack_swap()
3173 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3174 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3175 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3177 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3178 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3180 #define LOOKUP_SWITCH \
3181 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3182 tree selector = pop_value (INT_type_node); \
3183 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3184 while (--npairs >= 0) \
3186 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3187 expand_java_add_case (switch_expr, match, oldpc + offset); \
3191 #define TABLE_SWITCH \
3192 { jint default_offset = IMMEDIATE_s4; \
3193 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3194 tree selector = pop_value (INT_type_node); \
3195 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3196 for (; low <= high; low++) \
3198 jint offset = IMMEDIATE_s4; \
3199 expand_java_add_case (switch_expr, low, oldpc + offset); \
3203 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3204 { int opcode = byte_ops[PC-1]; \
3205 int method_ref_index = IMMEDIATE_u2; \
3207 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3209 expand_invoke (opcode, method_ref_index, nargs); \
3212 /* Handle new, checkcast, instanceof */
3213 #define OBJECT(TYPE, OP) \
3214 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3216 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3218 #define ARRAY_LOAD(OPERAND_TYPE) \
3220 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3223 #define ARRAY_STORE(OPERAND_TYPE) \
3225 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3228 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3229 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3230 #define ARRAY_NEW_PTR() \
3231 push_value (build_anewarray (get_class_constant (current_jcf, \
3233 pop_value (int_type_node)));
3234 #define ARRAY_NEW_NUM() \
3236 int atype = IMMEDIATE_u1; \
3237 push_value (build_newarray (atype, pop_value (int_type_node)));\
3239 #define ARRAY_NEW_MULTI() \
3241 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3242 int ndims = IMMEDIATE_u1; \
3243 expand_java_multianewarray( class, ndims ); \
3246 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3247 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3248 pop_value (OPERAND_TYPE##_type_node))));
3250 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3252 push_value (build1 (NOP_EXPR, int_type_node, \
3253 (convert (TO_TYPE##_type_node, \
3254 pop_value (FROM_TYPE##_type_node))))); \
3257 #define CONVERT(FROM_TYPE, TO_TYPE) \
3259 push_value (convert (TO_TYPE##_type_node, \
3260 pop_value (FROM_TYPE##_type_node))); \
3263 /* internal macro added for use by the WIDE case
3264 Added TREE_TYPE (decl) assignment, apbianco */
3265 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3268 int index = OPVALUE; \
3269 tree type = OPTYPE; \
3270 value = pop_value (type); \
3271 type = TREE_TYPE (value); \
3272 decl = find_local_variable (index, type, oldpc); \
3273 set_local_type (index, type); \
3274 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3275 update_aliases (decl, index, PC); \
3278 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3280 /* have to do this since OPERAND_VALUE may have side-effects */ \
3281 int opvalue = OPERAND_VALUE; \
3282 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3285 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3286 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3288 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3289 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3291 #define MONITOR_OPERATION(call) \
3293 tree o = pop_value (ptr_type_node); \
3295 flush_quick_stack (); \
3296 c = build_java_monitor (call, o); \
3297 TREE_SIDE_EFFECTS (c) = 1; \
3298 java_add_stmt (c); \
3301 #define SPECIAL_IINC(IGNORED) \
3303 unsigned int local_var_index = IMMEDIATE_u1; \
3304 int ival = IMMEDIATE_s1; \
3305 expand_iinc(local_var_index, ival, oldpc); \
3308 #define SPECIAL_WIDE(IGNORED) \
3310 int modified_opcode = IMMEDIATE_u1; \
3311 unsigned int local_var_index = IMMEDIATE_u2; \
3312 switch (modified_opcode) \
3316 int ival = IMMEDIATE_s2; \
3317 expand_iinc (local_var_index, ival, oldpc); \
3320 case OPCODE_iload: \
3321 case OPCODE_lload: \
3322 case OPCODE_fload: \
3323 case OPCODE_dload: \
3324 case OPCODE_aload: \
3326 /* duplicate code from LOAD macro */ \
3327 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3330 case OPCODE_istore: \
3331 case OPCODE_lstore: \
3332 case OPCODE_fstore: \
3333 case OPCODE_dstore: \
3334 case OPCODE_astore: \
3336 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3340 error ("unrecogized wide sub-instruction"); \
3344 #define SPECIAL_THROW(IGNORED) \
3345 build_java_athrow (pop_value (throwable_type_node))
3347 #define SPECIAL_BREAK NOT_IMPL1
3348 #define IMPL NOT_IMPL
3350 #include "javaop.def"
3353 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3358 /* Return the opcode at PC in the code section pointed to by
3361 static unsigned char
3362 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3364 unsigned char opcode;
3365 long absolute_offset = (long)JCF_TELL (jcf);
3367 JCF_SEEK (jcf, code_offset);
3368 opcode = jcf->read_ptr [pc];
3369 JCF_SEEK (jcf, absolute_offset);
3373 /* Some bytecode compilers are emitting accurate LocalVariableTable
3374 attributes. Here's an example:
3379 Attribute "LocalVariableTable"
3380 slot #<n>: ... (PC: PC+1 length: L)
3382 This is accurate because the local in slot <n> really exists after
3383 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3385 This procedure recognizes this situation and extends the live range
3386 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3387 length of the store instruction.)
3389 This function is used by `give_name_to_locals' so that a local's
3390 DECL features a DECL_LOCAL_START_PC such that the first related
3391 store operation will use DECL as a destination, not a unrelated
3392 temporary created for the occasion.
3394 This function uses a global (instruction_bits) `note_instructions' should
3395 have allocated and filled properly. */
3398 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3399 int start_pc, int slot)
3401 int first, index, opcode;
3410 /* Find last previous instruction and remember it */
3411 for (pc = start_pc-1; pc; pc--)
3412 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3416 /* Retrieve the instruction, handle `wide'. */
3417 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3418 if (opcode == OPCODE_wide)
3421 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3426 case OPCODE_astore_0:
3427 case OPCODE_astore_1:
3428 case OPCODE_astore_2:
3429 case OPCODE_astore_3:
3430 first = OPCODE_astore_0;
3433 case OPCODE_istore_0:
3434 case OPCODE_istore_1:
3435 case OPCODE_istore_2:
3436 case OPCODE_istore_3:
3437 first = OPCODE_istore_0;
3440 case OPCODE_lstore_0:
3441 case OPCODE_lstore_1:
3442 case OPCODE_lstore_2:
3443 case OPCODE_lstore_3:
3444 first = OPCODE_lstore_0;
3447 case OPCODE_fstore_0:
3448 case OPCODE_fstore_1:
3449 case OPCODE_fstore_2:
3450 case OPCODE_fstore_3:
3451 first = OPCODE_fstore_0;
3454 case OPCODE_dstore_0:
3455 case OPCODE_dstore_1:
3456 case OPCODE_dstore_2:
3457 case OPCODE_dstore_3:
3458 first = OPCODE_dstore_0;
3466 index = peek_opcode_at_pc (jcf, code_offset, pc);
3469 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3470 index = (other << 8) + index;
3475 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3476 means we have a <t>store. */
3477 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3483 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3484 order, as specified by Java Language Specification.
3486 The problem is that while expand_expr will evaluate its sub-operands in
3487 left-to-right order, for variables it will just return an rtx (i.e.
3488 an lvalue) for the variable (rather than an rvalue). So it is possible
3489 that a later sub-operand will change the register, and when the
3490 actual operation is done, it will use the new value, when it should
3491 have used the original value.
3493 We fix this by using save_expr. This forces the sub-operand to be
3494 copied into a fresh virtual register,
3496 For method invocation, we modify the arguments so that a
3497 left-to-right order evaluation is performed. Saved expressions
3498 will, in CALL_EXPR order, be reused when the call will be expanded.
3502 force_evaluation_order (tree node)
3504 if (flag_syntax_only)
3506 if (TREE_CODE (node) == CALL_EXPR
3507 || TREE_CODE (node) == NEW_CLASS_EXPR
3508 || (TREE_CODE (node) == COMPOUND_EXPR
3509 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3510 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3516 /* Position arg properly, account for wrapped around ctors. */
3517 if (TREE_CODE (node) == COMPOUND_EXPR)
3518 arg = TREE_OPERAND (node, 0);
3520 arg = TREE_OPERAND (arg, 1);
3522 /* An empty argument list is ok, just ignore it. */