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 java_add_local_var (copy);
1333 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1339 build_address_of (tree value)
1341 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1345 class_has_finalize_method (tree type)
1347 tree super = CLASSTYPE_SUPER (type);
1349 if (super == NULL_TREE)
1350 return false; /* Every class with a real finalizer inherits */
1351 /* from java.lang.Object. */
1353 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1357 java_create_object (tree type)
1359 tree alloc_node = (class_has_finalize_method (type)
1361 : alloc_no_finalizer_node);
1363 return build (CALL_EXPR, promote_type (type),
1364 build_address_of (alloc_node),
1365 build_tree_list (NULL_TREE, build_class_ref (type)),
1370 expand_java_NEW (tree type)
1374 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1375 : alloc_no_finalizer_node);
1376 if (! CLASS_LOADED_P (type))
1377 load_class (type, 1);
1378 safe_layout_class (type);
1379 push_value (build3 (CALL_EXPR, promote_type (type),
1380 build_address_of (alloc_node),
1381 build_tree_list (NULL_TREE, build_class_ref (type)),
1385 /* This returns an expression which will extract the class of an
1389 build_get_class (tree value)
1391 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1392 tree vtable_field = lookup_field (&object_type_node,
1393 get_identifier ("vtable"));
1394 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1395 build_java_indirect_ref (object_type_node, value,
1396 flag_check_references),
1397 vtable_field, NULL_TREE);
1398 return build3 (COMPONENT_REF, class_ptr_type,
1399 build1 (INDIRECT_REF, dtable_type, tmp),
1400 class_field, NULL_TREE);
1403 /* This builds the tree representation of the `instanceof' operator.
1404 It tries various tricks to optimize this in cases where types are
1408 build_instanceof (tree value, tree type)
1411 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1412 tree valtype = TREE_TYPE (TREE_TYPE (value));
1413 tree valclass = TYPE_NAME (valtype);
1416 /* When compiling from bytecode, we need to ensure that TYPE has
1418 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1420 load_class (type, 1);
1421 safe_layout_class (type);
1422 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1423 return error_mark_node;
1425 klass = TYPE_NAME (type);
1427 if (type == object_type_node || inherits_from_p (valtype, type))
1429 /* Anything except `null' is an instance of Object. Likewise,
1430 if the object is known to be an instance of the class, then
1431 we only need to check for `null'. */
1432 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1434 else if (flag_verify_invocations
1435 && ! TYPE_ARRAY_P (type)
1436 && ! TYPE_ARRAY_P (valtype)
1437 && DECL_P (klass) && DECL_P (valclass)
1438 && ! CLASS_INTERFACE (valclass)
1439 && ! CLASS_INTERFACE (klass)
1440 && ! inherits_from_p (type, valtype)
1441 && (CLASS_FINAL (klass)
1442 || ! inherits_from_p (valtype, type)))
1444 /* The classes are from different branches of the derivation
1445 tree, so we immediately know the answer. */
1446 expr = boolean_false_node;
1448 else if (DECL_P (klass) && CLASS_FINAL (klass))
1450 tree save = save_expr (value);
1451 expr = build3 (COND_EXPR, itype,
1452 build2 (NE_EXPR, boolean_type_node,
1453 save, null_pointer_node),
1454 build2 (EQ_EXPR, itype,
1455 build_get_class (save),
1456 build_class_ref (type)),
1457 boolean_false_node);
1461 expr = build3 (CALL_EXPR, itype,
1462 build_address_of (soft_instanceof_node),
1463 tree_cons (NULL_TREE, value,
1464 build_tree_list (NULL_TREE,
1465 build_class_ref (type))),
1468 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1473 expand_java_INSTANCEOF (tree type)
1475 tree value = pop_value (object_ptr_type_node);
1476 value = build_instanceof (value, type);
1481 expand_java_CHECKCAST (tree type)
1483 tree value = pop_value (ptr_type_node);
1484 value = build3 (CALL_EXPR, promote_type (type),
1485 build_address_of (soft_checkcast_node),
1486 tree_cons (NULL_TREE, build_class_ref (type),
1487 build_tree_list (NULL_TREE, value)),
1493 expand_iinc (unsigned int local_var_index, int ival, int pc)
1495 tree local_var, res;
1496 tree constant_value;
1498 flush_quick_stack ();
1499 local_var = find_local_variable (local_var_index, int_type_node, pc);
1500 constant_value = build_int_cst (NULL_TREE, ival);
1501 res = fold (build2 (PLUS_EXPR, int_type_node, local_var, constant_value));
1502 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1503 update_aliases (local_var, local_var_index, pc);
1508 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1511 tree arg1 = convert (type, op1);
1512 tree arg2 = convert (type, op2);
1514 if (type == int_type_node)
1518 case TRUNC_DIV_EXPR:
1519 call = soft_idiv_node;
1521 case TRUNC_MOD_EXPR:
1522 call = soft_irem_node;
1528 else if (type == long_type_node)
1532 case TRUNC_DIV_EXPR:
1533 call = soft_ldiv_node;
1535 case TRUNC_MOD_EXPR:
1536 call = soft_lrem_node;
1546 call = build3 (CALL_EXPR, type,
1547 build_address_of (call),
1548 tree_cons (NULL_TREE, arg1,
1549 build_tree_list (NULL_TREE, arg2)),
1556 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1563 tree u_type = java_unsigned_type (type);
1564 arg1 = convert (u_type, arg1);
1565 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1566 return convert (type, arg1);
1570 mask = build_int_cst (NULL_TREE,
1571 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1572 arg2 = fold (build2 (BIT_AND_EXPR, int_type_node, arg2, mask));
1575 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1576 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1577 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1579 tree ifexp1 = fold (build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1580 boolean_type_node, arg1, arg2));
1581 tree ifexp2 = fold (build2 (EQ_EXPR, boolean_type_node, arg1, arg2));
1582 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1583 ifexp2, integer_zero_node,
1584 op == COMPARE_L_EXPR
1585 ? integer_minus_one_node
1586 : integer_one_node));
1587 return fold (build3 (COND_EXPR, int_type_node, ifexp1,
1588 op == COMPARE_L_EXPR ? integer_one_node
1589 : integer_minus_one_node,
1593 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1595 tree ifexp1 = fold (build2 (LT_EXPR, boolean_type_node, arg1, arg2));
1596 tree ifexp2 = fold (build2 (GT_EXPR, boolean_type_node, arg1, arg2));
1597 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1598 ifexp2, integer_one_node,
1599 integer_zero_node));
1600 return fold (build3 (COND_EXPR, int_type_node,
1601 ifexp1, integer_minus_one_node, second_compare));
1603 case TRUNC_DIV_EXPR:
1604 case TRUNC_MOD_EXPR:
1605 if (TREE_CODE (type) == REAL_TYPE
1606 && op == TRUNC_MOD_EXPR)
1609 if (type != double_type_node)
1611 arg1 = convert (double_type_node, arg1);
1612 arg2 = convert (double_type_node, arg2);
1614 call = build3 (CALL_EXPR, double_type_node,
1615 build_address_of (soft_fmod_node),
1616 tree_cons (NULL_TREE, arg1,
1617 build_tree_list (NULL_TREE, arg2)),
1619 if (type != double_type_node)
1620 call = convert (type, call);
1624 if (TREE_CODE (type) == INTEGER_TYPE
1625 && flag_use_divide_subroutine
1626 && ! flag_syntax_only)
1627 return build_java_soft_divmod (op, type, arg1, arg2);
1632 return fold (build2 (op, type, arg1, arg2));
1636 expand_java_binop (tree type, enum tree_code op)
1646 rtype = int_type_node;
1647 rarg = pop_value (rtype);
1650 rarg = pop_value (rtype);
1652 larg = pop_value (ltype);
1653 push_value (build_java_binop (op, type, larg, rarg));
1656 /* Lookup the field named NAME in *TYPEP or its super classes.
1657 If not found, return NULL_TREE.
1658 (If the *TYPEP is not found, or if the field reference is
1659 ambiguous, return error_mark_node.)
1660 If found, return the FIELD_DECL, and set *TYPEP to the
1661 class containing the field. */
1664 lookup_field (tree *typep, tree name)
1666 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1668 load_class (*typep, 1);
1669 safe_layout_class (*typep);
1670 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1671 return error_mark_node;
1675 tree field, binfo, base_binfo;
1679 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1680 if (DECL_NAME (field) == name)
1683 /* Process implemented interfaces. */
1684 save_field = NULL_TREE;
1685 for (binfo = TYPE_BINFO (*typep), i = 0;
1686 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1688 tree t = BINFO_TYPE (base_binfo);
1689 if ((field = lookup_field (&t, name)))
1691 if (save_field == field)
1693 if (save_field == NULL_TREE)
1697 tree i1 = DECL_CONTEXT (save_field);
1698 tree i2 = DECL_CONTEXT (field);
1699 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1700 IDENTIFIER_POINTER (name),
1701 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1702 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1703 return error_mark_node;
1708 if (save_field != NULL_TREE)
1711 *typep = CLASSTYPE_SUPER (*typep);
1716 /* Look up the field named NAME in object SELF_VALUE,
1717 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1718 SELF_VALUE is NULL_TREE if looking for a static field. */
1721 build_field_ref (tree self_value, tree self_class, tree name)
1723 tree base_class = self_class;
1724 tree field_decl = lookup_field (&base_class, name);
1725 if (field_decl == NULL_TREE)
1727 error ("field %qs not found", IDENTIFIER_POINTER (name));
1728 return error_mark_node;
1730 if (self_value == NULL_TREE)
1732 return build_static_field_ref (field_decl);
1736 int check = (flag_check_references
1737 && ! (DECL_P (self_value)
1738 && DECL_NAME (self_value) == this_identifier_node));
1740 tree base_type = promote_type (base_class);
1741 if (base_type != TREE_TYPE (self_value))
1742 self_value = fold (build1 (NOP_EXPR, base_type, self_value));
1743 if (! flag_syntax_only
1744 && (flag_indirect_dispatch
1745 /* DECL_FIELD_OFFSET == 0 if we have no reference for
1746 the field, perhaps because we couldn't find the class
1747 in which the field is defined.
1748 FIXME: We should investigate this. */
1749 || DECL_FIELD_OFFSET (field_decl) == 0))
1752 = build_int_cst (NULL_TREE, get_symbol_table_index
1753 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1755 = build4 (ARRAY_REF, integer_type_node,
1756 TYPE_OTABLE_DECL (output_class), otable_index,
1757 NULL_TREE, NULL_TREE);
1760 field_offset = fold (convert (sizetype, field_offset));
1762 = fold (build2 (PLUS_EXPR,
1763 build_pointer_type (TREE_TYPE (field_decl)),
1764 self_value, field_offset));
1765 return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
1768 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1770 return fold (build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1771 self_value, field_decl, NULL_TREE));
1776 lookup_label (int pc)
1780 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1781 name = get_identifier (buf);
1782 if (IDENTIFIER_LOCAL_VALUE (name))
1783 return IDENTIFIER_LOCAL_VALUE (name);
1786 /* The type of the address of a label is return_address_type_node. */
1787 tree decl = create_label_decl (name);
1788 LABEL_PC (decl) = pc;
1789 return pushdecl (decl);
1793 /* Generate a unique name for the purpose of loops and switches
1794 labels, and try-catch-finally blocks label or temporary variables. */
1797 generate_name (void)
1799 static int l_number = 0;
1801 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1803 return get_identifier (buff);
1807 create_label_decl (tree name)
1810 decl = build_decl (LABEL_DECL, name,
1811 TREE_TYPE (return_address_type_node));
1812 DECL_CONTEXT (decl) = current_function_decl;
1813 DECL_IGNORED_P (decl) = 1;
1817 /* This maps a bytecode offset (PC) to various flags. */
1818 char *instruction_bits;
1821 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1823 lookup_label (target_pc);
1824 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1827 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1828 where CONDITION is one of one the compare operators. */
1831 expand_compare (enum tree_code condition, tree value1, tree value2,
1834 tree target = lookup_label (target_pc);
1835 tree cond = fold (build2 (condition, boolean_type_node, value1, value2));
1837 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1838 build1 (GOTO_EXPR, void_type_node, target),
1839 build_java_empty_stmt ()));
1842 /* Emit code for a TEST-type opcode. */
1845 expand_test (enum tree_code condition, tree type, int target_pc)
1847 tree value1, value2;
1848 flush_quick_stack ();
1849 value1 = pop_value (type);
1850 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1851 expand_compare (condition, value1, value2, target_pc);
1854 /* Emit code for a COND-type opcode. */
1857 expand_cond (enum tree_code condition, tree type, int target_pc)
1859 tree value1, value2;
1860 flush_quick_stack ();
1861 /* note: pop values in opposite order */
1862 value2 = pop_value (type);
1863 value1 = pop_value (type);
1864 /* Maybe should check value1 and value2 for type compatibility ??? */
1865 expand_compare (condition, value1, value2, target_pc);
1869 expand_java_goto (int target_pc)
1871 tree target_label = lookup_label (target_pc);
1872 flush_quick_stack ();
1873 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1877 expand_java_switch (tree selector, int default_pc)
1879 tree switch_expr, x;
1881 flush_quick_stack ();
1882 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1883 NULL_TREE, NULL_TREE);
1884 java_add_stmt (switch_expr);
1886 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1887 create_artificial_label ());
1888 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1890 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1891 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1897 expand_java_add_case (tree switch_expr, int match, int target_pc)
1901 value = build_int_cst (TREE_TYPE (switch_expr), match);
1903 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1904 create_artificial_label ());
1905 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1907 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1908 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1912 pop_arguments (tree arg_types)
1914 if (arg_types == end_params_node)
1916 if (TREE_CODE (arg_types) == TREE_LIST)
1918 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1919 tree type = TREE_VALUE (arg_types);
1920 tree arg = pop_value (type);
1922 /* With the new verifier we simply cast each argument to its
1923 proper type. This is needed since we lose type information
1924 coming out of the verifier. We also have to do this with the
1925 old verifier when we pop an integer type that must be
1926 promoted for the function call. */
1927 if (flag_new_verifier && TREE_CODE (type) == POINTER_TYPE)
1928 arg = build1 (NOP_EXPR, type, arg);
1929 else if (targetm.calls.promote_prototypes (type)
1930 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1931 && INTEGRAL_TYPE_P (type))
1932 arg = convert (integer_type_node, arg);
1933 return tree_cons (NULL_TREE, arg, tail);
1938 /* Attach to PTR (a block) the declaration found in ENTRY. */
1941 attach_init_test_initialization_flags (void **entry, void *ptr)
1943 tree block = (tree)ptr;
1944 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1946 if (block != error_mark_node)
1948 if (TREE_CODE (block) == BIND_EXPR)
1950 tree body = BIND_EXPR_BODY (block);
1951 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1952 BIND_EXPR_VARS (block) = ite->value;
1953 body = build2 (COMPOUND_EXPR, void_type_node,
1954 build1 (DECL_EXPR, void_type_node, ite->value), body);
1955 BIND_EXPR_BODY (block) = body;
1959 tree body = BLOCK_SUBBLOCKS (block);
1960 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1961 BLOCK_EXPR_DECLS (block) = ite->value;
1962 body = build2 (COMPOUND_EXPR, void_type_node,
1963 build1 (DECL_EXPR, void_type_node, ite->value), body);
1964 BLOCK_SUBBLOCKS (block) = body;
1971 /* Build an expression to initialize the class CLAS.
1972 if EXPR is non-NULL, returns an expression to first call the initializer
1973 (if it is needed) and then calls EXPR. */
1976 build_class_init (tree clas, tree expr)
1980 /* An optimization: if CLAS is a superclass of the class we're
1981 compiling, we don't need to initialize it. However, if CLAS is
1982 an interface, it won't necessarily be initialized, even if we
1984 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1985 && inherits_from_p (current_class, clas))
1986 || current_class == clas)
1989 if (always_initialize_class_p)
1991 init = build3 (CALL_EXPR, void_type_node,
1992 build_address_of (soft_initclass_node),
1993 build_tree_list (NULL_TREE, build_class_ref (clas)),
1995 TREE_SIDE_EFFECTS (init) = 1;
1999 tree *init_test_decl;
2001 init_test_decl = java_treetreehash_new
2002 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
2004 if (*init_test_decl == NULL)
2006 /* Build a declaration and mark it as a flag used to track
2007 static class initializations. */
2008 decl = build_decl (VAR_DECL, NULL_TREE,
2010 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2011 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
2012 DECL_CONTEXT (decl) = current_function_decl;
2013 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
2014 /* Tell the check-init code to ignore this decl when not
2015 optimizing class initialization. */
2016 if (!STATIC_CLASS_INIT_OPT_P ())
2017 DECL_BIT_INDEX (decl) = -1;
2018 DECL_INITIAL (decl) = boolean_false_node;
2019 /* Don't emit any symbolic debugging info for this decl. */
2020 DECL_IGNORED_P (decl) = 1;
2021 *init_test_decl = decl;
2024 init = build3 (CALL_EXPR, void_type_node,
2025 build_address_of (soft_initclass_node),
2026 build_tree_list (NULL_TREE, build_class_ref (clas)),
2028 TREE_SIDE_EFFECTS (init) = 1;
2029 init = build3 (COND_EXPR, void_type_node,
2030 build2 (EQ_EXPR, boolean_type_node,
2031 *init_test_decl, boolean_false_node),
2032 init, integer_zero_node);
2033 TREE_SIDE_EFFECTS (init) = 1;
2034 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
2035 build2 (MODIFY_EXPR, boolean_type_node,
2036 *init_test_decl, boolean_true_node));
2037 TREE_SIDE_EFFECTS (init) = 1;
2040 if (expr != NULL_TREE)
2042 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2043 TREE_SIDE_EFFECTS (expr) = 1;
2050 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2051 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2052 tree arg_list ATTRIBUTE_UNUSED)
2055 if (is_compiled_class (self_type))
2057 /* With indirect dispatch we have to use indirect calls for all
2058 publically visible methods or gcc will use PLT indirections
2059 to reach them. We also have to use indirect dispatch for all
2060 external methods. */
2061 if (! flag_indirect_dispatch
2062 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2064 make_decl_rtl (method);
2065 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2071 = build_int_cst (NULL_TREE, get_symbol_table_index
2072 (method, &TYPE_ATABLE_METHODS (output_class)));
2074 = build4 (ARRAY_REF,
2075 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2076 TYPE_ATABLE_DECL (output_class), table_index,
2077 NULL_TREE, NULL_TREE);
2079 func = convert (method_ptr_type_node, func);
2083 /* We don't know whether the method has been (statically) compiled.
2084 Compile this code to get a reference to the method's code:
2086 SELF_TYPE->methods[METHOD_INDEX].ncode
2090 int method_index = 0;
2093 /* The method might actually be declared in some superclass, so
2094 we have to use its class context, not the caller's notion of
2095 where the method is. */
2096 self_type = DECL_CONTEXT (method);
2097 ref = build_class_ref (self_type);
2098 ref = build1 (INDIRECT_REF, class_type_node, ref);
2099 if (ncode_ident == NULL_TREE)
2100 ncode_ident = get_identifier ("ncode");
2101 if (methods_ident == NULL_TREE)
2102 methods_ident = get_identifier ("methods");
2103 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2104 lookup_field (&class_type_node, methods_ident),
2106 for (meth = TYPE_METHODS (self_type);
2107 ; meth = TREE_CHAIN (meth))
2111 if (meth == NULL_TREE)
2112 fatal_error ("method '%s' not found in class",
2113 IDENTIFIER_POINTER (DECL_NAME (method)));
2116 method_index *= int_size_in_bytes (method_type_node);
2117 ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
2118 ref, build_int_cst (NULL_TREE, method_index)));
2119 ref = build1 (INDIRECT_REF, method_type_node, ref);
2120 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2121 ref, lookup_field (&method_type_node, ncode_ident),
2128 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2130 tree dtable, objectref;
2132 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2134 /* If we're dealing with interfaces and if the objectref
2135 argument is an array then get the dispatch table of the class
2136 Object rather than the one from the objectref. */
2137 objectref = (is_invoke_interface
2138 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2139 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2141 if (dtable_ident == NULL_TREE)
2142 dtable_ident = get_identifier ("vtable");
2143 dtable = build_java_indirect_ref (object_type_node, objectref,
2144 flag_check_references);
2145 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2146 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2151 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2152 T. If this decl has not been seen before, it will be added to the
2153 [oa]table_methods. If it has, the existing table slot will be
2157 get_symbol_table_index (tree t, tree *symbol_table)
2162 if (*symbol_table == NULL_TREE)
2164 *symbol_table = build_tree_list (t, t);
2168 method_list = *symbol_table;
2172 tree value = TREE_VALUE (method_list);
2176 if (TREE_CHAIN (method_list) == NULL_TREE)
2179 method_list = TREE_CHAIN (method_list);
2182 TREE_CHAIN (method_list) = build_tree_list (t, t);
2187 build_invokevirtual (tree dtable, tree method)
2190 tree nativecode_ptr_ptr_type_node
2191 = build_pointer_type (nativecode_ptr_type_node);
2195 if (flag_indirect_dispatch)
2197 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2201 = build_int_cst (NULL_TREE, get_symbol_table_index
2202 (method, &TYPE_OTABLE_METHODS (output_class)));
2203 method_index = build4 (ARRAY_REF, integer_type_node,
2204 TYPE_OTABLE_DECL (output_class),
2205 otable_index, NULL_TREE, NULL_TREE);
2209 /* We fetch the DECL_VINDEX field directly here, rather than
2210 using get_method_index(). DECL_VINDEX is the true offset
2211 from the vtable base to a method, regrdless of any extra
2212 words inserted at the start of the vtable. */
2213 method_index = DECL_VINDEX (method);
2214 method_index = size_binop (MULT_EXPR, method_index,
2215 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2216 if (TARGET_VTABLE_USES_DESCRIPTORS)
2217 method_index = size_binop (MULT_EXPR, method_index,
2218 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2221 func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2222 convert (nativecode_ptr_ptr_type_node, method_index)));
2224 if (TARGET_VTABLE_USES_DESCRIPTORS)
2225 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2227 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2232 static GTY(()) tree class_ident;
2234 build_invokeinterface (tree dtable, tree method)
2240 /* We expand invokeinterface here. */
2242 if (class_ident == NULL_TREE)
2243 class_ident = get_identifier ("class");
2245 dtable = build_java_indirect_ref (dtable_type, dtable,
2246 flag_check_references);
2247 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2248 lookup_field (&dtable_type, class_ident), NULL_TREE);
2250 interface = DECL_CONTEXT (method);
2251 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2253 layout_class_methods (interface);
2255 if (flag_indirect_dispatch)
2258 = 2 * (get_symbol_table_index
2259 (method, &TYPE_ITABLE_METHODS (output_class)));
2261 = build4 (ARRAY_REF,
2262 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2263 TYPE_ITABLE_DECL (output_class),
2264 build_int_cst (NULL_TREE, itable_index-1),
2265 NULL_TREE, NULL_TREE);
2267 = build4 (ARRAY_REF,
2268 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2269 TYPE_ITABLE_DECL (output_class),
2270 build_int_cst (NULL_TREE, itable_index),
2271 NULL_TREE, NULL_TREE);
2272 interface = convert (class_ptr_type, interface);
2273 idx = convert (integer_type_node, idx);
2277 idx = build_int_cst (NULL_TREE,
2278 get_interface_method_index (method, interface));
2279 interface = build_class_ref (interface);
2282 lookup_arg = tree_cons (NULL_TREE, dtable,
2283 tree_cons (NULL_TREE, interface,
2284 build_tree_list (NULL_TREE, idx)));
2286 return build3 (CALL_EXPR, ptr_type_node,
2287 build_address_of (soft_lookupinterfacemethod_node),
2288 lookup_arg, NULL_TREE);
2291 /* Expand one of the invoke_* opcodes.
2292 OPCODE is the specific opcode.
2293 METHOD_REF_INDEX is an index into the constant pool.
2294 NARGS is the number of arguments, or -1 if not specified. */
2297 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2299 tree method_signature
2300 = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
2301 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool,
2304 = get_class_constant (current_jcf,
2305 COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool,
2307 const char *const self_name
2308 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2309 tree call, func, method, arg_list, method_type;
2310 tree check = NULL_TREE;
2312 if (! CLASS_LOADED_P (self_type))
2314 load_class (self_type, 1);
2315 safe_layout_class (self_type);
2316 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2317 fatal_error ("failed to find class '%s'", self_name);
2319 layout_class_methods (self_type);
2321 if (ID_INIT_P (method_name))
2322 method = lookup_java_constructor (self_type, method_signature);
2324 method = lookup_java_method (self_type, method_name, method_signature);
2326 /* We've found a method in an interface, but this isn't an interface
2328 if (opcode != OPCODE_invokeinterface
2330 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2333 /* We've found a non-interface method but we are making an
2334 interface call. This can happen if the interface overrides a
2335 method in Object. */
2336 if (! flag_verify_invocations
2337 && opcode == OPCODE_invokeinterface
2339 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2342 if (method == NULL_TREE)
2344 if (flag_verify_invocations || ! flag_indirect_dispatch)
2346 error ("class '%s' has no method named '%s' matching signature '%s'",
2348 IDENTIFIER_POINTER (method_name),
2349 IDENTIFIER_POINTER (method_signature));
2353 int flags = ACC_PUBLIC;
2354 if (opcode == OPCODE_invokestatic)
2355 flags |= ACC_STATIC;
2356 if (opcode == OPCODE_invokeinterface)
2358 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2359 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2361 method = add_method (self_type, flags, method_name,
2363 DECL_ARTIFICIAL (method) = 1;
2364 METHOD_DUMMY (method) = 1;
2365 layout_class_method (self_type, NULL,
2370 /* Invoke static can't invoke static/abstract method */
2371 if (method != NULL_TREE)
2373 if (opcode == OPCODE_invokestatic)
2375 if (!METHOD_STATIC (method))
2377 error ("invokestatic on non static method");
2380 else if (METHOD_ABSTRACT (method))
2382 error ("invokestatic on abstract method");
2388 if (METHOD_STATIC (method))
2390 error ("invoke[non-static] on static method");
2396 if (method == NULL_TREE)
2398 /* If we got here, we emitted an error message above. So we
2399 just pop the arguments, push a properly-typed zero, and
2401 method_type = get_type_from_signature (method_signature);
2402 pop_arguments (TYPE_ARG_TYPES (method_type));
2403 if (opcode != OPCODE_invokestatic)
2404 pop_type (self_type);
2405 method_type = promote_type (TREE_TYPE (method_type));
2406 push_value (convert (method_type, integer_zero_node));
2410 method_type = TREE_TYPE (method);
2411 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2412 flush_quick_stack ();
2415 if (opcode == OPCODE_invokestatic)
2416 func = build_known_method_ref (method, method_type, self_type,
2417 method_signature, arg_list);
2418 else if (opcode == OPCODE_invokespecial
2419 || (opcode == OPCODE_invokevirtual
2420 && (METHOD_PRIVATE (method)
2421 || METHOD_FINAL (method)
2422 || CLASS_FINAL (TYPE_NAME (self_type)))))
2424 /* If the object for the method call is null, we throw an
2425 exception. We don't do this if the object is the current
2426 method's `this'. In other cases we just rely on an
2427 optimization pass to eliminate redundant checks. FIXME:
2428 Unfortunately there doesn't seem to be a way to determine
2429 what the current method is right now.
2430 We do omit the check if we're calling <init>. */
2431 /* We use a SAVE_EXPR here to make sure we only evaluate
2432 the new `self' expression once. */
2433 tree save_arg = save_expr (TREE_VALUE (arg_list));
2434 TREE_VALUE (arg_list) = save_arg;
2435 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2436 func = build_known_method_ref (method, method_type, self_type,
2437 method_signature, arg_list);
2441 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2443 if (opcode == OPCODE_invokevirtual)
2444 func = build_invokevirtual (dtable, method);
2446 func = build_invokeinterface (dtable, method);
2449 if (TREE_CODE (func) == ADDR_EXPR)
2450 TREE_TYPE (func) = build_pointer_type (method_type);
2452 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2454 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2455 func, arg_list, NULL_TREE);
2456 TREE_SIDE_EFFECTS (call) = 1;
2457 call = check_for_builtin (method, call);
2459 if (check != NULL_TREE)
2461 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2462 TREE_SIDE_EFFECTS (call) = 1;
2465 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2466 java_add_stmt (call);
2470 flush_quick_stack ();
2474 /* Create a stub which will be put into the vtable but which will call
2478 build_jni_stub (tree method)
2480 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2481 tree jni_func_type, tem;
2482 tree env_var, res_var = NULL_TREE, block;
2483 tree method_args, res_type;
2489 tree klass = DECL_CONTEXT (method);
2490 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2491 klass = build_class_ref (klass);
2493 if (! METHOD_NATIVE (method) || ! flag_jni)
2496 DECL_ARTIFICIAL (method) = 1;
2497 DECL_EXTERNAL (method) = 0;
2499 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2500 DECL_CONTEXT (env_var) = method;
2502 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2504 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2505 TREE_TYPE (TREE_TYPE (method)));
2506 DECL_CONTEXT (res_var) = method;
2507 TREE_CHAIN (env_var) = res_var;
2510 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2511 TREE_STATIC (meth_var) = 1;
2512 TREE_PUBLIC (meth_var) = 0;
2513 DECL_EXTERNAL (meth_var) = 0;
2514 DECL_CONTEXT (meth_var) = method;
2515 DECL_ARTIFICIAL (meth_var) = 1;
2516 DECL_INITIAL (meth_var) = null_pointer_node;
2517 TREE_USED (meth_var) = 1;
2518 chainon (env_var, meth_var);
2519 build_result_decl (method);
2521 /* One strange way that the front ends are different is that they
2522 store arguments differently. */
2524 method_args = DECL_ARGUMENTS (method);
2526 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2527 block = build_block (env_var, NULL_TREE, NULL_TREE,
2528 method_args, NULL_TREE);
2529 TREE_SIDE_EFFECTS (block) = 1;
2530 /* When compiling from source we don't set the type of the block,
2531 because that will prevent patch_return from ever being run. */
2533 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2535 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2536 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2537 build3 (CALL_EXPR, ptr_type_node,
2538 build_address_of (soft_getjnienvnewframe_node),
2539 build_tree_list (NULL_TREE, klass),
2541 CAN_COMPLETE_NORMALLY (body) = 1;
2543 /* All the arguments to this method become arguments to the
2544 underlying JNI function. If we had to wrap object arguments in a
2545 special way, we would do that here. */
2547 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2549 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (tem)));
2550 #ifdef PARM_BOUNDARY
2551 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2554 args_size += (arg_bits / BITS_PER_UNIT);
2556 args = tree_cons (NULL_TREE, tem, args);
2558 args = nreverse (args);
2559 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2561 /* For a static method the second argument is the class. For a
2562 non-static method the second argument is `this'; that is already
2563 available in the argument list. */
2564 if (METHOD_STATIC (method))
2566 args_size += int_size_in_bytes (TREE_TYPE (klass));
2567 args = tree_cons (NULL_TREE, klass, args);
2568 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2571 /* The JNIEnv structure is the first argument to the JNI function. */
2572 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2573 args = tree_cons (NULL_TREE, env_var, args);
2574 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2576 /* We call _Jv_LookupJNIMethod to find the actual underlying
2577 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2578 exception if this function is not found at runtime. */
2579 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2580 method_sig = build_java_signature (TREE_TYPE (method));
2581 lookup_arg = tree_cons (NULL_TREE,
2582 build_utf8_ref (unmangle_classname
2583 (IDENTIFIER_POINTER (method_sig),
2584 IDENTIFIER_LENGTH (method_sig))),
2586 tem = DECL_NAME (method);
2588 = tree_cons (NULL_TREE, klass,
2589 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2591 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2593 #ifdef MODIFY_JNI_METHOD_CALL
2594 tem = MODIFY_JNI_METHOD_CALL (tem);
2597 jni_func_type = build_pointer_type (tem);
2599 jnifunc = build3 (COND_EXPR, ptr_type_node,
2601 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2602 build3 (CALL_EXPR, ptr_type_node,
2604 (soft_lookupjnimethod_node),
2605 lookup_arg, NULL_TREE)));
2607 /* Now we make the actual JNI call via the resulting function
2609 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2610 build1 (NOP_EXPR, jni_func_type, jnifunc),
2613 /* If the JNI call returned a result, capture it here. If we had to
2614 unwrap JNI object results, we would do that here. */
2615 if (res_var != NULL_TREE)
2616 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2619 TREE_SIDE_EFFECTS (call) = 1;
2620 CAN_COMPLETE_NORMALLY (call) = 1;
2622 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2623 TREE_SIDE_EFFECTS (body) = 1;
2625 /* Now free the environment we allocated. */
2626 call = build3 (CALL_EXPR, ptr_type_node,
2627 build_address_of (soft_jnipopsystemframe_node),
2628 build_tree_list (NULL_TREE, env_var),
2630 TREE_SIDE_EFFECTS (call) = 1;
2631 CAN_COMPLETE_NORMALLY (call) = 1;
2632 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2633 TREE_SIDE_EFFECTS (body) = 1;
2635 /* Finally, do the return. */
2636 res_type = void_type_node;
2637 if (res_var != NULL_TREE)
2640 if (! DECL_RESULT (method))
2642 /* Make sure we copy the result variable to the actual
2643 result. We use the type of the DECL_RESULT because it
2644 might be different from the return type of the function:
2645 it might be promoted. */
2646 drt = TREE_TYPE (DECL_RESULT (method));
2647 if (drt != TREE_TYPE (res_var))
2648 res_var = build1 (CONVERT_EXPR, drt, res_var);
2649 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2650 TREE_SIDE_EFFECTS (res_var) = 1;
2653 body = build2 (COMPOUND_EXPR, void_type_node, body,
2654 build1 (RETURN_EXPR, res_type, res_var));
2655 TREE_SIDE_EFFECTS (body) = 1;
2657 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2662 /* Expand an operation to extract from or store into a field.
2663 IS_STATIC is 1 iff the field is static.
2664 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2665 FIELD_REF_INDEX is an index into the constant pool. */
2668 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2671 = get_class_constant (current_jcf,
2672 COMPONENT_REF_CLASS_INDEX (¤t_jcf->cpool,
2674 const char *self_name
2675 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2676 tree field_name = COMPONENT_REF_NAME (¤t_jcf->cpool, field_ref_index);
2677 tree field_signature = COMPONENT_REF_SIGNATURE (¤t_jcf->cpool,
2679 tree field_type = get_type_from_signature (field_signature);
2680 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2683 tree original_self_type = self_type;
2686 if (! CLASS_LOADED_P (self_type))
2687 load_class (self_type, 1);
2688 field_decl = lookup_field (&self_type, field_name);
2689 if (field_decl == error_mark_node)
2693 else if (field_decl == NULL_TREE)
2695 if (! flag_verify_invocations)
2697 int flags = ACC_PUBLIC;
2699 flags |= ACC_STATIC;
2700 self_type = original_self_type;
2701 field_decl = add_field (original_self_type, field_name,
2703 DECL_ARTIFICIAL (field_decl) = 1;
2704 DECL_IGNORED_P (field_decl) = 1;
2708 error ("missing field '%s' in '%s'",
2709 IDENTIFIER_POINTER (field_name), self_name);
2713 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2715 error ("mismatching signature for field '%s' in '%s'",
2716 IDENTIFIER_POINTER (field_name), self_name);
2719 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2723 push_value (convert (field_type, integer_zero_node));
2724 flush_quick_stack ();
2728 field_ref = build_field_ref (field_ref, self_type, field_name);
2730 field_ref = build_class_init (self_type, field_ref);
2733 flush_quick_stack ();
2734 if (FIELD_FINAL (field_decl))
2736 if (DECL_CONTEXT (field_decl) != current_class)
2737 error ("%Jassignment to final field '%D' not in field's class",
2738 field_decl, field_decl);
2739 else if (FIELD_STATIC (field_decl))
2741 if (!DECL_CLINIT_P (current_function_decl))
2742 warning ("%Jassignment to final static field %qD not in "
2743 "class initializer",
2744 field_decl, field_decl);
2748 tree cfndecl_name = DECL_NAME (current_function_decl);
2749 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2750 && !ID_FINIT_P (cfndecl_name))
2751 warning ("%Jassignment to final field '%D' not in constructor",
2752 field_decl, field_decl);
2755 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2756 field_ref, new_value));
2759 push_value (field_ref);
2763 load_type_state (tree label)
2766 tree vec = LABEL_TYPE_STATE (label);
2767 int cur_length = TREE_VEC_LENGTH (vec);
2768 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2769 for (i = 0; i < cur_length; i++)
2770 type_map [i] = TREE_VEC_ELT (vec, i);
2773 /* Go over METHOD's bytecode and note instruction starts in
2774 instruction_bits[]. */
2777 note_instructions (JCF *jcf, tree method)
2780 unsigned char* byte_ops;
2781 long length = DECL_CODE_LENGTH (method);
2786 #undef RET /* Defined by config/i386/i386.h */
2788 #define BCODE byte_ops
2789 #define BYTE_type_node byte_type_node
2790 #define SHORT_type_node short_type_node
2791 #define INT_type_node int_type_node
2792 #define LONG_type_node long_type_node
2793 #define CHAR_type_node char_type_node
2794 #define PTR_type_node ptr_type_node
2795 #define FLOAT_type_node float_type_node
2796 #define DOUBLE_type_node double_type_node
2797 #define VOID_type_node void_type_node
2798 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2799 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2800 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2801 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2803 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2805 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2806 byte_ops = jcf->read_ptr;
2807 instruction_bits = xrealloc (instruction_bits, length + 1);
2808 memset (instruction_bits, 0, length + 1);
2810 /* This pass figures out which PC can be the targets of jumps. */
2811 for (PC = 0; PC < length;)
2813 int oldpc = PC; /* PC at instruction start. */
2814 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2815 switch (byte_ops[PC++])
2817 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2819 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2822 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2824 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2825 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2826 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2827 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2828 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2829 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2830 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2831 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2833 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2834 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2835 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2836 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2837 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2838 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2839 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2840 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2842 /* two forms of wide instructions */
2843 #define PRE_SPECIAL_WIDE(IGNORE) \
2845 int modified_opcode = IMMEDIATE_u1; \
2846 if (modified_opcode == OPCODE_iinc) \
2848 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2849 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2853 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2857 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2859 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2861 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2862 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2863 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2864 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2865 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2866 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2867 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2868 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2869 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2870 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2872 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2873 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2874 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2875 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2876 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2877 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2878 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2880 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2882 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2884 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2885 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2887 #define PRE_LOOKUP_SWITCH \
2888 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2889 NOTE_LABEL (default_offset+oldpc); \
2891 while (--npairs >= 0) { \
2892 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2893 jint offset = IMMEDIATE_s4; \
2894 NOTE_LABEL (offset+oldpc); } \
2897 #define PRE_TABLE_SWITCH \
2898 { jint default_offset = IMMEDIATE_s4; \
2899 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2900 NOTE_LABEL (default_offset+oldpc); \
2902 while (low++ <= high) { \
2903 jint offset = IMMEDIATE_s4; \
2904 NOTE_LABEL (offset+oldpc); } \
2907 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2908 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2909 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2910 (void)(IMMEDIATE_u2); \
2911 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2913 #include "javaop.def"
2920 expand_byte_code (JCF *jcf, tree method)
2924 const unsigned char *linenumber_pointer;
2925 int dead_code_index = -1;
2926 unsigned char* byte_ops;
2927 long length = DECL_CODE_LENGTH (method);
2930 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2931 byte_ops = jcf->read_ptr;
2933 /* We make an initial pass of the line number table, to note
2934 which instructions have associated line number entries. */
2935 linenumber_pointer = linenumber_table;
2936 for (i = 0; i < linenumber_count; i++)
2938 int pc = GET_u2 (linenumber_pointer);
2939 linenumber_pointer += 4;
2941 warning ("invalid PC in line number table");
2944 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2945 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2946 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2950 if (flag_new_verifier)
2952 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2957 if (! verify_jvm_instructions (jcf, byte_ops, length))
2961 /* Translate bytecodes. */
2962 linenumber_pointer = linenumber_table;
2963 for (PC = 0; PC < length;)
2965 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2967 tree label = lookup_label (PC);
2968 flush_quick_stack ();
2969 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2970 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2971 if (LABEL_VERIFIED (label) || PC == 0)
2972 load_type_state (label);
2975 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2977 if (dead_code_index == -1)
2979 /* This is the start of a region of unreachable bytecodes.
2980 They still need to be processed in order for EH ranges
2981 to get handled correctly. However, we can simply
2982 replace these bytecodes with nops. */
2983 dead_code_index = PC;
2986 /* Turn this bytecode into a nop. */
2991 if (dead_code_index != -1)
2993 /* We've just reached the end of a region of dead code. */
2995 warning ("unreachable bytecode from %d to before %d",
2996 dead_code_index, PC);
2997 dead_code_index = -1;
3001 /* Handle possible line number entry for this PC.
3003 This code handles out-of-order and multiple linenumbers per PC,
3004 but is optimized for the case of line numbers increasing
3005 monotonically with PC. */
3006 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
3008 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
3009 || GET_u2 (linenumber_pointer) != PC)
3010 linenumber_pointer = linenumber_table;
3011 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
3013 int pc = GET_u2 (linenumber_pointer);
3014 linenumber_pointer += 4;
3017 int line = GET_u2 (linenumber_pointer - 2);
3018 #ifdef USE_MAPPED_LOCATION
3019 input_location = linemap_line_start (&line_table, line, 1);
3021 input_location.line = line;
3023 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
3028 maybe_pushlevels (PC);
3029 PC = process_jvm_instruction (PC, byte_ops, length);
3030 maybe_poplevels (PC);
3033 if (dead_code_index != -1)
3035 /* We've just reached the end of a region of dead code. */
3037 warning ("unreachable bytecode from %d to the end of the method",
3043 java_push_constant_from_pool (JCF *jcf, int index)
3046 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3049 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3050 index = alloc_name_constant (CONSTANT_String, name);
3051 c = build_ref_from_constant_pool (index);
3052 c = convert (promote_type (string_type_node), c);
3055 c = get_constant (jcf, index);
3060 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3061 long length ATTRIBUTE_UNUSED)
3063 const char *opname; /* Temporary ??? */
3064 int oldpc = PC; /* PC at instruction start. */
3066 /* If the instruction is at the beginning of a exception handler,
3067 replace the top of the stack with the thrown object reference */
3068 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3070 /* Note that the new verifier will not emit a type map at all
3071 for dead exception handlers. In this case we just ignore
3073 if (! flag_new_verifier || (instruction_bits[PC] & BCODE_VERIFIED) != 0)
3075 tree type = pop_type (promote_type (throwable_type_node));
3076 push_value (build_exception_object_ref (type));
3080 switch (byte_ops[PC++])
3082 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3085 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3088 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3090 int saw_index = 0; \
3091 int index = OPERAND_VALUE; \
3093 (find_local_variable (index, return_address_type_node, oldpc)); \
3096 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3098 /* OPERAND_VALUE may have side-effects on PC */ \
3099 int opvalue = OPERAND_VALUE; \
3100 build_java_jsr (oldpc + opvalue, PC); \
3103 /* Push a constant onto the stack. */
3104 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3105 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3106 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3107 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3109 /* internal macro added for use by the WIDE case */
3110 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3111 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3113 /* Push local variable onto the opcode stack. */
3114 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3116 /* have to do this since OPERAND_VALUE may have side-effects */ \
3117 int opvalue = OPERAND_VALUE; \
3118 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3121 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3122 expand_java_return (OPERAND_TYPE##_type_node)
3124 #define REM_EXPR TRUNC_MOD_EXPR
3125 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3126 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3128 #define FIELD(IS_STATIC, IS_PUT) \
3129 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3131 #define TEST(OPERAND_TYPE, CONDITION) \
3132 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3134 #define COND(OPERAND_TYPE, CONDITION) \
3135 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3137 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3138 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3140 #define BRANCH_GOTO(OPERAND_VALUE) \
3141 expand_java_goto (oldpc + OPERAND_VALUE)
3143 #define BRANCH_CALL(OPERAND_VALUE) \
3144 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3147 #define BRANCH_RETURN(OPERAND_VALUE) \
3149 tree type = OPERAND_TYPE##_type_node; \
3150 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3151 expand_java_ret (value); \
3155 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3156 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3157 fprintf (stderr, "(not implemented)\n")
3158 #define NOT_IMPL1(OPERAND_VALUE) \
3159 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3160 fprintf (stderr, "(not implemented)\n")
3162 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3164 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3166 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3168 #define STACK_SWAP(COUNT) java_stack_swap()
3170 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3171 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3172 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3174 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3175 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3177 #define LOOKUP_SWITCH \
3178 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3179 tree selector = pop_value (INT_type_node); \
3180 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3181 while (--npairs >= 0) \
3183 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3184 expand_java_add_case (switch_expr, match, oldpc + offset); \
3188 #define TABLE_SWITCH \
3189 { jint default_offset = IMMEDIATE_s4; \
3190 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3191 tree selector = pop_value (INT_type_node); \
3192 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3193 for (; low <= high; low++) \
3195 jint offset = IMMEDIATE_s4; \
3196 expand_java_add_case (switch_expr, low, oldpc + offset); \
3200 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3201 { int opcode = byte_ops[PC-1]; \
3202 int method_ref_index = IMMEDIATE_u2; \
3204 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3206 expand_invoke (opcode, method_ref_index, nargs); \
3209 /* Handle new, checkcast, instanceof */
3210 #define OBJECT(TYPE, OP) \
3211 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3213 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3215 #define ARRAY_LOAD(OPERAND_TYPE) \
3217 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3220 #define ARRAY_STORE(OPERAND_TYPE) \
3222 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3225 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3226 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3227 #define ARRAY_NEW_PTR() \
3228 push_value (build_anewarray (get_class_constant (current_jcf, \
3230 pop_value (int_type_node)));
3231 #define ARRAY_NEW_NUM() \
3233 int atype = IMMEDIATE_u1; \
3234 push_value (build_newarray (atype, pop_value (int_type_node)));\
3236 #define ARRAY_NEW_MULTI() \
3238 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3239 int ndims = IMMEDIATE_u1; \
3240 expand_java_multianewarray( class, ndims ); \
3243 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3244 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3245 pop_value (OPERAND_TYPE##_type_node))));
3247 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3249 push_value (build1 (NOP_EXPR, int_type_node, \
3250 (convert (TO_TYPE##_type_node, \
3251 pop_value (FROM_TYPE##_type_node))))); \
3254 #define CONVERT(FROM_TYPE, TO_TYPE) \
3256 push_value (convert (TO_TYPE##_type_node, \
3257 pop_value (FROM_TYPE##_type_node))); \
3260 /* internal macro added for use by the WIDE case
3261 Added TREE_TYPE (decl) assignment, apbianco */
3262 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3265 int index = OPVALUE; \
3266 tree type = OPTYPE; \
3267 value = pop_value (type); \
3268 type = TREE_TYPE (value); \
3269 decl = find_local_variable (index, type, oldpc); \
3270 set_local_type (index, type); \
3271 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3272 update_aliases (decl, index, PC); \
3275 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3277 /* have to do this since OPERAND_VALUE may have side-effects */ \
3278 int opvalue = OPERAND_VALUE; \
3279 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3282 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3283 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3285 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3286 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3288 #define MONITOR_OPERATION(call) \
3290 tree o = pop_value (ptr_type_node); \
3292 flush_quick_stack (); \
3293 c = build_java_monitor (call, o); \
3294 TREE_SIDE_EFFECTS (c) = 1; \
3295 java_add_stmt (c); \
3298 #define SPECIAL_IINC(IGNORED) \
3300 unsigned int local_var_index = IMMEDIATE_u1; \
3301 int ival = IMMEDIATE_s1; \
3302 expand_iinc(local_var_index, ival, oldpc); \
3305 #define SPECIAL_WIDE(IGNORED) \
3307 int modified_opcode = IMMEDIATE_u1; \
3308 unsigned int local_var_index = IMMEDIATE_u2; \
3309 switch (modified_opcode) \
3313 int ival = IMMEDIATE_s2; \
3314 expand_iinc (local_var_index, ival, oldpc); \
3317 case OPCODE_iload: \
3318 case OPCODE_lload: \
3319 case OPCODE_fload: \
3320 case OPCODE_dload: \
3321 case OPCODE_aload: \
3323 /* duplicate code from LOAD macro */ \
3324 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3327 case OPCODE_istore: \
3328 case OPCODE_lstore: \
3329 case OPCODE_fstore: \
3330 case OPCODE_dstore: \
3331 case OPCODE_astore: \
3333 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3337 error ("unrecogized wide sub-instruction"); \
3341 #define SPECIAL_THROW(IGNORED) \
3342 build_java_athrow (pop_value (throwable_type_node))
3344 #define SPECIAL_BREAK NOT_IMPL1
3345 #define IMPL NOT_IMPL
3347 #include "javaop.def"
3350 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3355 /* Return the opcode at PC in the code section pointed to by
3358 static unsigned char
3359 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3361 unsigned char opcode;
3362 long absolute_offset = (long)JCF_TELL (jcf);
3364 JCF_SEEK (jcf, code_offset);
3365 opcode = jcf->read_ptr [pc];
3366 JCF_SEEK (jcf, absolute_offset);
3370 /* Some bytecode compilers are emitting accurate LocalVariableTable
3371 attributes. Here's an example:
3376 Attribute "LocalVariableTable"
3377 slot #<n>: ... (PC: PC+1 length: L)
3379 This is accurate because the local in slot <n> really exists after
3380 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3382 This procedure recognizes this situation and extends the live range
3383 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3384 length of the store instruction.)
3386 This function is used by `give_name_to_locals' so that a local's
3387 DECL features a DECL_LOCAL_START_PC such that the first related
3388 store operation will use DECL as a destination, not a unrelated
3389 temporary created for the occasion.
3391 This function uses a global (instruction_bits) `note_instructions' should
3392 have allocated and filled properly. */
3395 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3396 int start_pc, int slot)
3398 int first, index, opcode;
3407 /* Find last previous instruction and remember it */
3408 for (pc = start_pc-1; pc; pc--)
3409 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3413 /* Retrieve the instruction, handle `wide'. */
3414 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3415 if (opcode == OPCODE_wide)
3418 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3423 case OPCODE_astore_0:
3424 case OPCODE_astore_1:
3425 case OPCODE_astore_2:
3426 case OPCODE_astore_3:
3427 first = OPCODE_astore_0;
3430 case OPCODE_istore_0:
3431 case OPCODE_istore_1:
3432 case OPCODE_istore_2:
3433 case OPCODE_istore_3:
3434 first = OPCODE_istore_0;
3437 case OPCODE_lstore_0:
3438 case OPCODE_lstore_1:
3439 case OPCODE_lstore_2:
3440 case OPCODE_lstore_3:
3441 first = OPCODE_lstore_0;
3444 case OPCODE_fstore_0:
3445 case OPCODE_fstore_1:
3446 case OPCODE_fstore_2:
3447 case OPCODE_fstore_3:
3448 first = OPCODE_fstore_0;
3451 case OPCODE_dstore_0:
3452 case OPCODE_dstore_1:
3453 case OPCODE_dstore_2:
3454 case OPCODE_dstore_3:
3455 first = OPCODE_dstore_0;
3463 index = peek_opcode_at_pc (jcf, code_offset, pc);
3466 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3467 index = (other << 8) + index;
3472 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3473 means we have a <t>store. */
3474 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3480 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3481 order, as specified by Java Language Specification.
3483 The problem is that while expand_expr will evaluate its sub-operands in
3484 left-to-right order, for variables it will just return an rtx (i.e.
3485 an lvalue) for the variable (rather than an rvalue). So it is possible
3486 that a later sub-operand will change the register, and when the
3487 actual operation is done, it will use the new value, when it should
3488 have used the original value.
3490 We fix this by using save_expr. This forces the sub-operand to be
3491 copied into a fresh virtual register,
3493 For method invocation, we modify the arguments so that a
3494 left-to-right order evaluation is performed. Saved expressions
3495 will, in CALL_EXPR order, be reused when the call will be expanded.
3499 force_evaluation_order (tree node)
3501 if (flag_syntax_only)
3503 if (TREE_CODE (node) == CALL_EXPR
3504 || TREE_CODE (node) == NEW_CLASS_EXPR
3505 || (TREE_CODE (node) == COMPOUND_EXPR
3506 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3507 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3513 /* Position arg properly, account for wrapped around ctors. */
3514 if (TREE_CODE (node) == COMPOUND_EXPR)
3515 arg = TREE_OPERAND (node, 0);
3517 arg = TREE_OPERAND (arg, 1);
3519 /* An empty argument list is ok, just ignore it. */
3523 /* Not having a list of arguments here is an error. */
3524 if (TREE_CODE (arg) != TREE_LIST)
3527 /* This reverses the evaluation order. This is a desired effect. */
3528 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3530 tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3531 cmp = (cmp == NULL_TREE ? saved :
3532 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3533 TREE_VALUE (arg) = saved;
3536 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3537 TREE_SIDE_EFFECTS (cmp) = 1;
3541 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3542 if (TREE_TYPE (cmp) != void_type_node)
3543 cmp = save_expr (cmp);
3544 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3545 TREE_SIDE_EFFECTS (cmp) = 1;
3552 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3553 location where an expression or an identifier were encountered. It
3554 is necessary for languages where the frontend parser will handle
3555 recursively more than one file (Java is one of them). */
3558 build_expr_wfl (tree node,
3559 #ifdef USE_MAPPED_LOCATION
3560 source_location location
3562 const char *file, int line, int col
3567 static const char *last_file = 0;
3568 static tree last_filenode = NULL_TREE;
3570 #ifdef USE_MAPPED_LOCATION
3571 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3572 SET_EXPR_LOCATION (wfl, location);
3574 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3576 EXPR_WFL_SET_LINECOL (wfl, line, col);
3577 if (file != last_file)
3580 last_filenode = file ? get_identifier (file) : NULL_TREE;
3582 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3584 EXPR_WFL_NODE (wfl) = node;
3587 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3588 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3589 TREE_TYPE (wfl) = TREE_TYPE (node);
3595 #ifdef USE_MAPPED_LOCATION
3597 expr_add_location (tree node, source_location location, bool statement)
3601 /* FIXME. This optimization causes failures in code that expects an
3602 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3603 if (node && ! (statement && flag_emit_class_files))
3605 source_location node_loc = EXPR_LOCATION (node);
3606 if (node_loc == location || location == UNKNOWN_LOCATION)
3608 if (node_loc == UNKNOWN_LOCATION
3609 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3611 SET_EXPR_LOCATION (node, location);
3616 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3617 SET_EXPR_LOCATION (wfl, location);
3618 EXPR_WFL_NODE (wfl) = node;
3619 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3620 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3623 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3624 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3625 TREE_TYPE (wfl) = TREE_TYPE (node);
3632 /* Build a node to represent empty statements and blocks. */
3635 build_java_empty_stmt (void)
3637 tree t = build_empty_stmt ();
3638 CAN_COMPLETE_NORMALLY (t) = 1;
3642 #include "gt-java-expr.h"