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 tree case_identity (tree, tree);
89 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
90 static int emit_init_test_initialization (void **entry, void * ptr);
92 static GTY(()) tree operand_type[59];
94 static GTY(()) tree methods_ident;
95 static GTY(()) tree ncode_ident;
96 tree dtable_ident = NULL_TREE;
98 /* Set to nonzero value in order to emit class initialization code
99 before static field references. */
100 /* FIXME: Make this work with gimplify. */
101 int always_initialize_class_p = 1;
103 /* We store the stack state in two places:
104 Within a basic block, we use the quick_stack, which is a
105 pushdown list (TREE_LISTs) of expression nodes.
106 This is the top part of the stack; below that we use find_stack_slot.
107 At the end of a basic block, the quick_stack must be flushed
108 to the stack slot array (as handled by find_stack_slot).
109 Using quick_stack generates better code (especially when
110 compiled without optimization), because we do not have to
111 explicitly store and load trees to temporary variables.
113 If a variable is on the quick stack, it means the value of variable
114 when the quick stack was last flushed. Conceptually, flush_quick_stack
115 saves all the the quick_stack elements in parallel. However, that is
116 complicated, so it actually saves them (i.e. copies each stack value
117 to is home virtual register) from low indexes. This allows a quick_stack
118 element at index i (counting from the bottom of stack the) to references
119 slot virtuals for register that are >= i, but not those that are deeper.
120 This convention makes most operations easier. For example iadd works
121 even when the stack contains (reg[0], reg[1]): It results in the
122 stack containing (reg[0]+reg[1]), which is OK. However, some stack
123 operations are more complicated. For example dup given a stack
124 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
125 the convention, since stack value 1 would refer to a register with
126 lower index (reg[0]), which flush_quick_stack does not safely handle.
127 So dup cannot just add an extra element to the quick_stack, but iadd can.
130 static GTY(()) tree quick_stack;
132 /* A free-list of unused permanent TREE_LIST nodes. */
133 static GTY((deletable)) tree tree_list_free_list;
135 /* The stack pointer of the Java virtual machine.
136 This does include the size of the quick_stack. */
140 const unsigned char *linenumber_table;
141 int linenumber_count;
144 init_expr_processing (void)
146 operand_type[21] = operand_type[54] = int_type_node;
147 operand_type[22] = operand_type[55] = long_type_node;
148 operand_type[23] = operand_type[56] = float_type_node;
149 operand_type[24] = operand_type[57] = double_type_node;
150 operand_type[25] = operand_type[58] = ptr_type_node;
154 java_truthvalue_conversion (tree expr)
156 /* It is simpler and generates better code to have only TRUTH_*_EXPR
157 or comparison expressions as truth values at this level.
159 This function should normally be identity for Java. */
161 switch (TREE_CODE (expr))
164 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
165 case TRUTH_ANDIF_EXPR:
166 case TRUTH_ORIF_EXPR:
173 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
176 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
178 /* are these legal? XXX JH */
182 /* These don't change whether an object is nonzero or zero. */
183 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
186 /* Distribute the conversion into the arms of a COND_EXPR. */
187 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
188 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
189 java_truthvalue_conversion (TREE_OPERAND (expr, 2))));
192 /* If this is widening the argument, we can ignore it. */
193 if (TYPE_PRECISION (TREE_TYPE (expr))
194 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
195 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
196 /* fall through to default */
199 return fold (build (NE_EXPR, boolean_type_node, expr, boolean_false_node));
203 /* Save any stack slots that happen to be in the quick_stack into their
204 home virtual register slots.
206 The copy order is from low stack index to high, to support the invariant
207 that the expression for a slot may contain decls for stack slots with
208 higher (or the same) index, but not lower. */
211 flush_quick_stack (void)
213 int stack_index = stack_pointer;
214 tree prev, cur, next;
216 /* First reverse the quick_stack, and count the number of slots it has. */
217 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
219 next = TREE_CHAIN (cur);
220 TREE_CHAIN (cur) = prev;
222 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
226 while (quick_stack != NULL_TREE)
229 tree node = quick_stack, type;
230 quick_stack = TREE_CHAIN (node);
231 TREE_CHAIN (node) = tree_list_free_list;
232 tree_list_free_list = node;
233 node = TREE_VALUE (node);
234 type = TREE_TYPE (node);
236 decl = find_stack_slot (stack_index, type);
238 java_add_stmt (build (MODIFY_EXPR, TREE_TYPE (node), decl, node));
239 stack_index += 1 + TYPE_IS_WIDE (type);
243 /* Push TYPE on the type stack.
244 Return true on success, 0 on overflow. */
247 push_type_0 (tree type)
250 type = promote_type (type);
251 n_words = 1 + TYPE_IS_WIDE (type);
252 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
254 stack_type_map[stack_pointer++] = type;
256 while (--n_words >= 0)
257 stack_type_map[stack_pointer++] = TYPE_SECOND;
262 push_type (tree type)
264 if (! push_type_0 (type))
269 push_value (tree value)
271 tree type = TREE_TYPE (value);
272 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
274 type = promote_type (type);
275 value = convert (type, value);
278 if (tree_list_free_list == NULL_TREE)
279 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
282 tree node = tree_list_free_list;
283 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
284 TREE_VALUE (node) = value;
285 TREE_CHAIN (node) = quick_stack;
290 /* Pop a type from the type stack.
291 TYPE is the expected type. Return the actual type, which must be
293 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
296 pop_type_0 (tree type, char **messagep)
301 if (TREE_CODE (type) == RECORD_TYPE)
302 type = promote_type (type);
303 n_words = 1 + TYPE_IS_WIDE (type);
304 if (stack_pointer < n_words)
306 *messagep = xstrdup ("stack underflow");
309 while (--n_words > 0)
311 if (stack_type_map[--stack_pointer] != void_type_node)
313 *messagep = xstrdup ("Invalid multi-word value on type stack");
317 t = stack_type_map[--stack_pointer];
318 if (type == NULL_TREE || t == type)
320 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
321 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
323 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
325 if (type == ptr_type_node || type == object_ptr_type_node)
327 else if (t == ptr_type_node) /* Special case for null reference. */
329 else if (can_widen_reference_to (t, type))
331 /* This is a kludge, but matches what Sun's verifier does.
332 It can be tricked, but is safe as long as type errors
333 (i.e. interface method calls) are caught at run-time. */
334 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
335 return object_ptr_type_node;
338 /* lang_printable_name uses a static buffer, so we must save the result
339 from calling it the first time. */
341 char *temp = xstrdup (lang_printable_name (type, 0));
342 *messagep = concat ("expected type '", temp,
343 "' but stack contains '", lang_printable_name (t, 0),
350 /* Pop a type from the type stack.
351 TYPE is the expected type. Return the actual type, which must be
352 convertible to TYPE, otherwise call error. */
357 char *message = NULL;
358 type = pop_type_0 (type, &message);
361 error ("%s", message);
367 /* Return 1f if SOURCE_TYPE can be safely widened to TARGET_TYPE.
368 Handles array types and interfaces. */
371 can_widen_reference_to (tree source_type, tree target_type)
373 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
376 /* Get rid of pointers */
377 if (TREE_CODE (source_type) == POINTER_TYPE)
378 source_type = TREE_TYPE (source_type);
379 if (TREE_CODE (target_type) == POINTER_TYPE)
380 target_type = TREE_TYPE (target_type);
382 if (source_type == target_type)
386 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
388 HOST_WIDE_INT source_length, target_length;
389 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
391 /* An array implements Cloneable and Serializable. */
392 tree name = DECL_NAME (TYPE_NAME (target_type));
393 return (name == java_lang_cloneable_identifier_node
394 || name == java_io_serializable_identifier_node);
396 target_length = java_array_type_length (target_type);
397 if (target_length >= 0)
399 source_length = java_array_type_length (source_type);
400 if (source_length != target_length)
403 source_type = TYPE_ARRAY_ELEMENT (source_type);
404 target_type = TYPE_ARRAY_ELEMENT (target_type);
405 if (source_type == target_type)
407 if (TREE_CODE (source_type) != POINTER_TYPE
408 || TREE_CODE (target_type) != POINTER_TYPE)
410 return can_widen_reference_to (source_type, target_type);
414 int source_depth = class_depth (source_type);
415 int target_depth = class_depth (target_type);
417 /* class_depth can return a negative depth if an error occurred */
418 if (source_depth < 0 || target_depth < 0)
421 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
423 /* target_type is OK if source_type or source_type ancestors
424 implement target_type. We handle multiple sub-interfaces */
426 tree basetype_vec = TYPE_BINFO_BASETYPES (source_type);
427 int n = TREE_VEC_LENGTH (basetype_vec), i;
428 for (i=0 ; i < n; i++)
429 if (can_widen_reference_to
430 (TREE_TYPE (TREE_VEC_ELT (basetype_vec, i)),
437 for ( ; source_depth > target_depth; source_depth--)
439 source_type = TYPE_BINFO_BASETYPE (source_type, 0);
441 return source_type == target_type;
447 pop_value (tree type)
449 type = pop_type (type);
452 tree node = quick_stack;
453 quick_stack = TREE_CHAIN (quick_stack);
454 TREE_CHAIN (node) = tree_list_free_list;
455 tree_list_free_list = node;
456 node = TREE_VALUE (node);
460 return find_stack_slot (stack_pointer, promote_type (type));
464 /* Pop and discard the top COUNT stack slots. */
467 java_stack_pop (int count)
473 if (stack_pointer == 0)
476 type = stack_type_map[stack_pointer - 1];
477 if (type == TYPE_SECOND)
480 if (stack_pointer == 1 || count <= 0)
483 type = stack_type_map[stack_pointer - 2];
485 val = pop_value (type);
490 /* Implement the 'swap' operator (to swap two top stack slots). */
493 java_stack_swap (void)
499 if (stack_pointer < 2
500 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
501 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
502 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
503 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
504 /* Bad stack swap. */
507 flush_quick_stack ();
508 decl1 = find_stack_slot (stack_pointer - 1, type1);
509 decl2 = find_stack_slot (stack_pointer - 2, type2);
510 temp = build_decl (VAR_DECL, NULL_TREE, type1);
511 java_add_local_var (temp);
512 java_add_stmt (build (MODIFY_EXPR, type1, temp, decl1));
513 java_add_stmt (build (MODIFY_EXPR, type2,
514 find_stack_slot (stack_pointer - 1, type2),
516 java_add_stmt (build (MODIFY_EXPR, type1,
517 find_stack_slot (stack_pointer - 2, type1),
519 stack_type_map[stack_pointer - 1] = type2;
520 stack_type_map[stack_pointer - 2] = type1;
524 java_stack_dup (int size, int offset)
526 int low_index = stack_pointer - size - offset;
529 error ("stack underflow - dup* operation");
531 flush_quick_stack ();
533 stack_pointer += size;
534 dst_index = stack_pointer;
536 for (dst_index = stack_pointer; --dst_index >= low_index; )
539 int src_index = dst_index - size;
540 if (src_index < low_index)
541 src_index = dst_index + size + offset;
542 type = stack_type_map [src_index];
543 if (type == TYPE_SECOND)
545 if (src_index <= low_index)
546 /* Dup operation splits 64-bit number. */
549 stack_type_map[dst_index] = type;
550 src_index--; dst_index--;
551 type = stack_type_map[src_index];
552 if (! TYPE_IS_WIDE (type))
555 else if (TYPE_IS_WIDE (type))
558 if (src_index != dst_index)
560 tree src_decl = find_stack_slot (src_index, type);
561 tree dst_decl = find_stack_slot (dst_index, type);
564 (build (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
565 stack_type_map[dst_index] = type;
570 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
574 build_java_athrow (tree node)
578 call = build (CALL_EXPR,
580 build_address_of (throw_node),
581 build_tree_list (NULL_TREE, node),
583 TREE_SIDE_EFFECTS (call) = 1;
584 java_add_stmt (call);
585 java_stack_pop (stack_pointer);
588 /* Implementation for jsr/ret */
591 build_java_jsr (int target_pc, int return_pc)
593 tree where = lookup_label (target_pc);
594 tree ret = lookup_label (return_pc);
595 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
596 push_value (ret_label);
597 flush_quick_stack ();
598 java_add_stmt (build (GOTO_EXPR, void_type_node, where));
600 /* Do not need to emit the label here. We noted the existance of the
601 label as a jump target in note_instructions; we'll emit the label
602 for real at the beginning of the expand_byte_code loop. */
606 build_java_ret (tree location)
608 java_add_stmt (build (GOTO_EXPR, void_type_node, location));
611 /* Implementation of operations on array: new, load, store, length */
614 decode_newarray_type (int atype)
618 case 4: return boolean_type_node;
619 case 5: return char_type_node;
620 case 6: return float_type_node;
621 case 7: return double_type_node;
622 case 8: return byte_type_node;
623 case 9: return short_type_node;
624 case 10: return int_type_node;
625 case 11: return long_type_node;
626 default: return NULL_TREE;
630 /* Map primitive type to the code used by OPCODE_newarray. */
633 encode_newarray_type (tree type)
635 if (type == boolean_type_node)
637 else if (type == char_type_node)
639 else if (type == float_type_node)
641 else if (type == double_type_node)
643 else if (type == byte_type_node)
645 else if (type == short_type_node)
647 else if (type == int_type_node)
649 else if (type == long_type_node)
655 /* Build a call to _Jv_ThrowBadArrayIndex(), the
656 ArrayIndexOfBoundsException exception handler. */
659 build_java_throw_out_of_bounds_exception (tree index)
661 tree node = build (CALL_EXPR, int_type_node,
662 build_address_of (soft_badarrayindex_node),
663 build_tree_list (NULL_TREE, index), NULL_TREE);
664 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
668 /* Return the length of an array. Doesn't perform any checking on the nature
669 or value of the array NODE. May be used to implement some bytecodes. */
672 build_java_array_length_access (tree node)
674 tree type = TREE_TYPE (node);
675 tree array_type = TREE_TYPE (type);
676 HOST_WIDE_INT length;
678 /* JVM spec: If the arrayref is null, the arraylength instruction
679 throws a NullPointerException. The only way we could get a node
680 of type ptr_type_node at this point is `aconst_null; arraylength'
681 or something equivalent. */
682 if (type == ptr_type_node)
683 return build (CALL_EXPR, int_type_node,
684 build_address_of (soft_nullpointer_node),
685 NULL_TREE, NULL_TREE);
687 if (!is_array_type_p (type))
690 length = java_array_type_length (type);
692 return build_int_2 (length, 0);
694 node = build (COMPONENT_REF, int_type_node,
695 build_java_indirect_ref (array_type, node,
696 flag_check_references),
697 lookup_field (&array_type, get_identifier ("length")));
698 IS_ARRAY_LENGTH_ACCESS (node) = 1;
702 /* Optionally checks a reference against the NULL pointer. ARG1: the
703 expr, ARG2: we should check the reference. Don't generate extra
704 checks if we're not generating code. */
707 java_check_reference (tree expr, int check)
709 if (!flag_syntax_only && check)
711 expr = save_expr (expr);
712 expr = build (COND_EXPR, TREE_TYPE (expr),
713 build (EQ_EXPR, boolean_type_node, expr, null_pointer_node),
714 build (CALL_EXPR, void_type_node,
715 build_address_of (soft_nullpointer_node),
716 NULL_TREE, NULL_TREE),
723 /* Reference an object: just like an INDIRECT_REF, but with checking. */
726 build_java_indirect_ref (tree type, tree expr, int check)
729 t = java_check_reference (expr, check);
730 t = convert (build_pointer_type (type), t);
731 return build1 (INDIRECT_REF, type, t);
734 /* Implement array indexing (either as l-value or r-value).
735 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
736 Optionally performs bounds checking and/or test to NULL.
737 At this point, ARRAY should have been verified as an array. */
740 build_java_arrayaccess (tree array, tree type, tree index)
742 tree node, throw = NULL_TREE;
745 tree array_type = TREE_TYPE (TREE_TYPE (array));
747 if (flag_bounds_check)
750 * (unsigned jint) INDEX >= (unsigned jint) LEN
751 * && throw ArrayIndexOutOfBoundsException.
752 * Note this is equivalent to and more efficient than:
753 * INDEX < 0 || INDEX >= LEN && throw ... */
755 tree len = build_java_array_length_access (array);
756 TREE_TYPE (len) = unsigned_int_type_node;
757 test = fold (build (GE_EXPR, boolean_type_node,
758 convert (unsigned_int_type_node, index),
760 if (! integer_zerop (test))
762 throw = build (TRUTH_ANDIF_EXPR, int_type_node, test,
763 build_java_throw_out_of_bounds_exception (index));
764 /* allows expansion within COMPOUND */
765 TREE_SIDE_EFFECTS( throw ) = 1;
769 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
770 to have the bounds check evaluated first. */
771 if (throw != NULL_TREE)
772 index = build (COMPOUND_EXPR, int_type_node, throw, index);
774 data_field = lookup_field (&array_type, get_identifier ("data"));
776 ref = build (COMPONENT_REF, TREE_TYPE (data_field),
777 build_java_indirect_ref (array_type, array,
778 flag_check_references),
781 node = build (ARRAY_REF, type, ref, index);
785 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
786 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
787 determine that no check is required. */
790 build_java_arraystore_check (tree array, tree object)
792 tree check, element_type, source;
793 tree array_type_p = TREE_TYPE (array);
794 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
796 if (! is_array_type_p (array_type_p))
799 /* Get the TYPE_DECL for ARRAY's element type. */
800 element_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
802 if (TREE_CODE (element_type) != TYPE_DECL
803 || TREE_CODE (object_type) != TYPE_DECL)
806 if (!flag_store_check)
807 return build1 (NOP_EXPR, array_type_p, array);
809 /* No check is needed if the element type is final or is itself an array.
810 Also check that element_type matches object_type, since in the bytecode
811 compilation case element_type may be the actual element type of the array
812 rather than its declared type. */
813 if (element_type == object_type
814 && (TYPE_ARRAY_P (TREE_TYPE (element_type))
815 || CLASS_FINAL (element_type)))
816 return build1 (NOP_EXPR, array_type_p, array);
818 /* OBJECT might be wrapped by a SAVE_EXPR. */
819 if (TREE_CODE (object) == SAVE_EXPR)
820 source = TREE_OPERAND (object, 0);
824 /* Avoid the check if OBJECT was just loaded from the same array. */
825 if (TREE_CODE (source) == ARRAY_REF)
828 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
829 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
830 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
831 if (TREE_CODE (source) == SAVE_EXPR)
832 source = TREE_OPERAND (source, 0);
835 if (TREE_CODE (target) == SAVE_EXPR)
836 target = TREE_OPERAND (target, 0);
838 if (source == target)
839 return build1 (NOP_EXPR, array_type_p, array);
842 /* Build an invocation of _Jv_CheckArrayStore */
843 check = build (CALL_EXPR, void_type_node,
844 build_address_of (soft_checkarraystore_node),
845 tree_cons (NULL_TREE, array,
846 build_tree_list (NULL_TREE, object)),
848 TREE_SIDE_EFFECTS (check) = 1;
853 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
854 ARRAY_NODE. This function is used to retrieve something less vague than
855 a pointer type when indexing the first dimension of something like [[<t>.
856 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
858 As a side effect, it also makes sure that ARRAY_NODE is an array. */
861 build_java_check_indexed_type (tree array_node, tree indexed_type)
865 if (!is_array_type_p (TREE_TYPE (array_node)))
868 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
870 if (indexed_type == ptr_type_node )
871 return promote_type (elt_type);
873 /* BYTE/BOOLEAN store and load are used for both type */
874 if (indexed_type == byte_type_node && elt_type == boolean_type_node )
875 return boolean_type_node;
877 if (indexed_type != elt_type )
883 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
884 called with an integer code (the type of array to create), and the length
885 of the array to create. */
888 build_newarray (int atype_value, tree length)
892 tree prim_type = decode_newarray_type (atype_value);
894 = build_java_array_type (prim_type,
895 host_integerp (length, 0) == INTEGER_CST
896 ? tree_low_cst (length, 0) : -1);
898 /* If compiling to native, pass a reference to the primitive type class
899 and save the runtime some work. However, the bytecode generator
900 expects to find the type_code int here. */
901 if (flag_emit_class_files)
902 type_arg = build_int_2 (atype_value, 0);
904 type_arg = build_class_ref (prim_type);
906 return build (CALL_EXPR, promote_type (type),
907 build_address_of (soft_newarray_node),
908 tree_cons (NULL_TREE,
910 build_tree_list (NULL_TREE, length)),
914 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
918 build_anewarray (tree class_type, tree length)
921 = build_java_array_type (class_type,
922 host_integerp (length, 0)
923 ? tree_low_cst (length, 0) : -1);
925 return build (CALL_EXPR, promote_type (type),
926 build_address_of (soft_anewarray_node),
927 tree_cons (NULL_TREE, length,
928 tree_cons (NULL_TREE, build_class_ref (class_type),
929 build_tree_list (NULL_TREE,
930 null_pointer_node))),
934 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
937 build_new_array (tree type, tree length)
939 if (JPRIMITIVE_TYPE_P (type))
940 return build_newarray (encode_newarray_type (type), length);
942 return build_anewarray (TREE_TYPE (type), length);
945 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
946 class pointer, a number of dimensions and the matching number of
947 dimensions. The argument list is NULL terminated. */
950 expand_java_multianewarray (tree class_type, int ndim)
953 tree args = build_tree_list( NULL_TREE, null_pointer_node );
955 for( i = 0; i < ndim; i++ )
956 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
958 push_value (build (CALL_EXPR,
959 promote_type (class_type),
960 build_address_of (soft_multianewarray_node),
961 tree_cons (NULL_TREE, build_class_ref (class_type),
962 tree_cons (NULL_TREE,
963 build_int_2 (ndim, 0), args )),
967 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
968 ARRAY is an array type. May expand some bound checking and NULL
969 pointer checking. RHS_TYPE_NODE we are going to store. In the case
970 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
971 INT. In those cases, we make the conversion.
973 if ARRAy is a reference type, the assignment is checked at run-time
974 to make sure that the RHS can be assigned to the array element
975 type. It is not necessary to generate this code if ARRAY is final. */
978 expand_java_arraystore (tree rhs_type_node)
980 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
981 && TYPE_PRECISION (rhs_type_node) <= 32) ?
982 int_type_node : rhs_type_node);
983 tree index = pop_value (int_type_node);
984 tree array = pop_value (ptr_type_node);
986 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
988 flush_quick_stack ();
990 index = save_expr (index);
991 array = save_expr (array);
993 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
995 tree check = build_java_arraystore_check (array, rhs_node);
996 java_add_stmt (check);
999 array = build_java_arrayaccess (array, rhs_type_node, index);
1000 java_add_stmt (build (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1003 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1004 sure that LHS is an array type. May expand some bound checking and NULL
1006 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1007 BOOLEAN/SHORT, we push a promoted type back to the stack.
1011 expand_java_arrayload (tree lhs_type_node )
1014 tree index_node = pop_value (int_type_node);
1015 tree array_node = pop_value (ptr_type_node);
1017 index_node = save_expr (index_node);
1018 array_node = save_expr (array_node);
1020 if (TREE_TYPE (array_node) == ptr_type_node)
1021 /* The only way we could get a node of type ptr_type_node at this
1022 point is `aconst_null; arraylength' or something equivalent, so
1023 unconditionally throw NullPointerException. */
1024 load_node = build (CALL_EXPR, lhs_type_node,
1025 build_address_of (soft_nullpointer_node),
1026 NULL_TREE, NULL_TREE);
1029 lhs_type_node = build_java_check_indexed_type (array_node, lhs_type_node);
1030 load_node = build_java_arrayaccess (array_node,
1034 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1035 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
1036 push_value (load_node);
1039 /* Expands .length. Makes sure that we deal with and array and may expand
1040 a NULL check on the array object. */
1043 expand_java_array_length (void)
1045 tree array = pop_value (ptr_type_node);
1046 tree length = build_java_array_length_access (array);
1048 push_value (length);
1051 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1052 either soft_monitorenter_node or soft_monitorexit_node. */
1055 build_java_monitor (tree call, tree object)
1057 return (build (CALL_EXPR,
1059 build_address_of (call),
1060 build_tree_list (NULL_TREE, object),
1064 /* Emit code for one of the PUSHC instructions. */
1067 expand_java_pushc (int ival, tree type)
1070 if (type == ptr_type_node && ival == 0)
1071 value = null_pointer_node;
1072 else if (type == int_type_node || type == long_type_node)
1074 value = build_int_2 (ival, ival < 0 ? -1 : 0);
1075 TREE_TYPE (value) = type;
1077 else if (type == float_type_node || type == double_type_node)
1080 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1081 value = build_real (type, x);
1090 expand_java_return (tree type)
1092 if (type == void_type_node)
1093 java_add_stmt (build (RETURN_EXPR, void_type_node, NULL));
1096 tree retval = pop_value (type);
1097 tree res = DECL_RESULT (current_function_decl);
1098 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1100 /* Handle the situation where the native integer type is smaller
1101 than the JVM integer. It can happen for many cross compilers.
1102 The whole if expression just goes away if INT_TYPE_SIZE < 32
1104 if (INT_TYPE_SIZE < 32
1105 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1106 < GET_MODE_SIZE (TYPE_MODE (type))))
1107 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1109 TREE_SIDE_EFFECTS (retval) = 1;
1110 java_add_stmt (build (RETURN_EXPR, TREE_TYPE (retval), retval));
1115 expand_load_internal (int index, tree type, int pc)
1118 tree var = find_local_variable (index, type, pc);
1120 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1121 on the stack. If there is an assignment to this VAR_DECL between
1122 the stack push and the use, then the wrong code could be
1123 generated. To avoid this we create a new local and copy our
1124 value into it. Then we push this new local on the stack.
1125 Hopefully this all gets optimized out. */
1126 copy = build_decl (VAR_DECL, NULL_TREE, type);
1127 java_add_local_var (copy);
1128 java_add_stmt (build (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1134 build_address_of (tree value)
1136 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1139 bool class_has_finalize_method (tree type)
1141 tree super = CLASSTYPE_SUPER (type);
1143 if (super == NULL_TREE)
1144 return false; /* Every class with a real finalizer inherits */
1145 /* from java.lang.Object. */
1147 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1151 expand_java_NEW (tree type)
1155 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1156 : alloc_no_finalizer_node);
1157 if (! CLASS_LOADED_P (type))
1158 load_class (type, 1);
1159 safe_layout_class (type);
1160 push_value (build (CALL_EXPR, promote_type (type),
1161 build_address_of (alloc_node),
1162 build_tree_list (NULL_TREE, build_class_ref (type)),
1166 /* This returns an expression which will extract the class of an
1170 build_get_class (tree value)
1172 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1173 tree vtable_field = lookup_field (&object_type_node,
1174 get_identifier ("vtable"));
1175 return build (COMPONENT_REF, class_ptr_type,
1176 build1 (INDIRECT_REF, dtable_type,
1177 build (COMPONENT_REF, dtable_ptr_type,
1178 build_java_indirect_ref (object_type_node, value,
1179 flag_check_references),
1184 /* This builds the tree representation of the `instanceof' operator.
1185 It tries various tricks to optimize this in cases where types are
1189 build_instanceof (tree value, tree type)
1192 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1193 tree valtype = TREE_TYPE (TREE_TYPE (value));
1194 tree valclass = TYPE_NAME (valtype);
1197 /* When compiling from bytecode, we need to ensure that TYPE has
1199 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1201 load_class (type, 1);
1202 safe_layout_class (type);
1203 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1204 return error_mark_node;
1206 klass = TYPE_NAME (type);
1208 if (type == object_type_node || inherits_from_p (valtype, type))
1210 /* Anything except `null' is an instance of Object. Likewise,
1211 if the object is known to be an instance of the class, then
1212 we only need to check for `null'. */
1213 expr = build (NE_EXPR, itype, value, null_pointer_node);
1215 else if (! TYPE_ARRAY_P (type)
1216 && ! TYPE_ARRAY_P (valtype)
1217 && DECL_P (klass) && DECL_P (valclass)
1218 && ! CLASS_INTERFACE (valclass)
1219 && ! CLASS_INTERFACE (klass)
1220 && ! inherits_from_p (type, valtype)
1221 && (CLASS_FINAL (klass)
1222 || ! inherits_from_p (valtype, type)))
1224 /* The classes are from different branches of the derivation
1225 tree, so we immediately know the answer. */
1226 expr = boolean_false_node;
1228 else if (DECL_P (klass) && CLASS_FINAL (klass))
1230 tree save = save_expr (value);
1231 expr = build (COND_EXPR, itype,
1232 build (NE_EXPR, boolean_type_node,
1233 save, null_pointer_node),
1234 build (EQ_EXPR, itype,
1235 build_get_class (save),
1236 build_class_ref (type)),
1237 boolean_false_node);
1241 expr = build (CALL_EXPR, itype,
1242 build_address_of (soft_instanceof_node),
1243 tree_cons (NULL_TREE, value,
1244 build_tree_list (NULL_TREE,
1245 build_class_ref (type))),
1248 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1253 expand_java_INSTANCEOF (tree type)
1255 tree value = pop_value (object_ptr_type_node);
1256 value = build_instanceof (value, type);
1261 expand_java_CHECKCAST (tree type)
1263 tree value = pop_value (ptr_type_node);
1264 value = build (CALL_EXPR, promote_type (type),
1265 build_address_of (soft_checkcast_node),
1266 tree_cons (NULL_TREE, build_class_ref (type),
1267 build_tree_list (NULL_TREE, value)),
1273 expand_iinc (unsigned int local_var_index, int ival, int pc)
1275 tree local_var, res;
1276 tree constant_value;
1278 flush_quick_stack ();
1279 local_var = find_local_variable (local_var_index, int_type_node, pc);
1280 constant_value = build_int_2 (ival, ival < 0 ? -1 : 0);
1281 res = fold (build (PLUS_EXPR, int_type_node, local_var, constant_value));
1282 java_add_stmt (build (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1283 update_aliases (local_var, local_var_index);
1288 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1291 tree arg1 = convert (type, op1);
1292 tree arg2 = convert (type, op2);
1294 if (type == int_type_node)
1298 case TRUNC_DIV_EXPR:
1299 call = soft_idiv_node;
1301 case TRUNC_MOD_EXPR:
1302 call = soft_irem_node;
1308 else if (type == long_type_node)
1312 case TRUNC_DIV_EXPR:
1313 call = soft_ldiv_node;
1315 case TRUNC_MOD_EXPR:
1316 call = soft_lrem_node;
1326 call = build (CALL_EXPR, type,
1327 build_address_of (call),
1328 tree_cons (NULL_TREE, arg1,
1329 build_tree_list (NULL_TREE, arg2)),
1336 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1343 tree u_type = java_unsigned_type (type);
1344 arg1 = convert (u_type, arg1);
1345 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1346 return convert (type, arg1);
1350 mask = build_int_2 (TYPE_PRECISION (TREE_TYPE (arg1)) - 1, 0);
1351 arg2 = fold (build (BIT_AND_EXPR, int_type_node, arg2, mask));
1354 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1355 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1356 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1358 tree ifexp1 = fold ( build (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1359 boolean_type_node, arg1, arg2));
1360 tree ifexp2 = fold ( build (EQ_EXPR, boolean_type_node, arg1, arg2));
1361 tree second_compare = fold (build (COND_EXPR, int_type_node,
1362 ifexp2, integer_zero_node,
1363 op == COMPARE_L_EXPR
1364 ? integer_minus_one_node
1365 : integer_one_node));
1366 return fold (build (COND_EXPR, int_type_node, ifexp1,
1367 op == COMPARE_L_EXPR ? integer_one_node
1368 : integer_minus_one_node,
1372 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1374 tree ifexp1 = fold ( build (LT_EXPR, boolean_type_node, arg1, arg2));
1375 tree ifexp2 = fold ( build (GT_EXPR, boolean_type_node, arg1, arg2));
1376 tree second_compare = fold ( build (COND_EXPR, int_type_node,
1377 ifexp2, integer_one_node,
1378 integer_zero_node));
1379 return fold (build (COND_EXPR, int_type_node,
1380 ifexp1, integer_minus_one_node, second_compare));
1382 case TRUNC_DIV_EXPR:
1383 case TRUNC_MOD_EXPR:
1384 if (TREE_CODE (type) == REAL_TYPE
1385 && op == TRUNC_MOD_EXPR)
1388 if (type != double_type_node)
1390 arg1 = convert (double_type_node, arg1);
1391 arg2 = convert (double_type_node, arg2);
1393 call = build (CALL_EXPR, double_type_node,
1394 build_address_of (soft_fmod_node),
1395 tree_cons (NULL_TREE, arg1,
1396 build_tree_list (NULL_TREE, arg2)),
1398 if (type != double_type_node)
1399 call = convert (type, call);
1403 if (TREE_CODE (type) == INTEGER_TYPE
1404 && flag_use_divide_subroutine
1405 && ! flag_syntax_only)
1406 return build_java_soft_divmod (op, type, arg1, arg2);
1411 return fold (build (op, type, arg1, arg2));
1415 expand_java_binop (tree type, enum tree_code op)
1425 rtype = int_type_node;
1426 rarg = pop_value (rtype);
1429 rarg = pop_value (rtype);
1431 larg = pop_value (ltype);
1432 push_value (build_java_binop (op, type, larg, rarg));
1435 /* Lookup the field named NAME in *TYPEP or its super classes.
1436 If not found, return NULL_TREE.
1437 (If the *TYPEP is not found, or if the field reference is
1438 ambiguous, return error_mark_node.)
1439 If found, return the FIELD_DECL, and set *TYPEP to the
1440 class containing the field. */
1443 lookup_field (tree *typep, tree name)
1445 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1447 load_class (*typep, 1);
1448 safe_layout_class (*typep);
1449 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1450 return error_mark_node;
1454 tree field, basetype_vec;
1458 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1459 if (DECL_NAME (field) == name)
1462 /* Process implemented interfaces. */
1463 basetype_vec = TYPE_BINFO_BASETYPES (*typep);
1464 n = TREE_VEC_LENGTH (basetype_vec);
1465 save_field = NULL_TREE;
1466 for (i = 0; i < n; i++)
1468 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
1469 if ((field = lookup_field (&t, name)))
1471 if (save_field == field)
1473 if (save_field == NULL_TREE)
1477 tree i1 = DECL_CONTEXT (save_field);
1478 tree i2 = DECL_CONTEXT (field);
1479 error ("reference `%s' is ambiguous: appears in interface `%s' and interface `%s'",
1480 IDENTIFIER_POINTER (name),
1481 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1482 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1483 return error_mark_node;
1488 if (save_field != NULL_TREE)
1491 *typep = CLASSTYPE_SUPER (*typep);
1496 /* Look up the field named NAME in object SELF_VALUE,
1497 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1498 SELF_VALUE is NULL_TREE if looking for a static field. */
1501 build_field_ref (tree self_value, tree self_class, tree name)
1503 tree base_class = self_class;
1504 tree field_decl = lookup_field (&base_class, name);
1505 if (field_decl == NULL_TREE)
1507 error ("field `%s' not found", IDENTIFIER_POINTER (name));
1508 return error_mark_node;
1510 if (self_value == NULL_TREE)
1512 return build_static_field_ref (field_decl);
1516 int check = (flag_check_references
1517 && ! (DECL_P (self_value)
1518 && DECL_NAME (self_value) == this_identifier_node));
1520 tree base_type = promote_type (base_class);
1521 if (base_type != TREE_TYPE (self_value))
1522 self_value = fold (build1 (NOP_EXPR, base_type, self_value));
1523 if (flag_indirect_dispatch
1524 && output_class != self_class)
1525 /* FIXME: output_class != self_class is not exactly the right
1526 test. What we really want to know is whether self_class is
1527 in the same translation unit as output_class. If it is,
1528 we can make a direct reference. */
1531 build_int_2 (get_symbol_table_index
1532 (field_decl, &TYPE_OTABLE_METHODS (output_class)), 0);
1534 build (ARRAY_REF, integer_type_node, TYPE_OTABLE_DECL (output_class),
1537 field_offset = fold (convert (sizetype, field_offset));
1539 = fold (build (PLUS_EXPR,
1540 build_pointer_type (TREE_TYPE (field_decl)),
1541 self_value, field_offset));
1542 return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
1545 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1547 return fold (build (COMPONENT_REF, TREE_TYPE (field_decl),
1548 self_value, field_decl));
1553 lookup_label (int pc)
1557 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1558 name = get_identifier (buf);
1559 if (IDENTIFIER_LOCAL_VALUE (name))
1560 return IDENTIFIER_LOCAL_VALUE (name);
1563 /* The type of the address of a label is return_address_type_node. */
1564 tree decl = create_label_decl (name);
1565 LABEL_PC (decl) = pc;
1566 return pushdecl (decl);
1570 /* Generate a unique name for the purpose of loops and switches
1571 labels, and try-catch-finally blocks label or temporary variables. */
1574 generate_name (void)
1576 static int l_number = 0;
1578 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1580 return get_identifier (buff);
1584 create_label_decl (tree name)
1587 decl = build_decl (LABEL_DECL, name,
1588 TREE_TYPE (return_address_type_node));
1589 DECL_CONTEXT (decl) = current_function_decl;
1590 DECL_IGNORED_P (decl) = 1;
1594 /* This maps a bytecode offset (PC) to various flags. */
1595 char *instruction_bits;
1598 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1600 lookup_label (target_pc);
1601 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1604 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1605 where CONDITION is one of one the compare operators. */
1608 expand_compare (enum tree_code condition, tree value1, tree value2,
1611 tree target = lookup_label (target_pc);
1612 tree cond = fold (build (condition, boolean_type_node, value1, value2));
1614 (build (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1615 build (GOTO_EXPR, void_type_node, target),
1616 build_java_empty_stmt ()));
1619 /* Emit code for a TEST-type opcode. */
1622 expand_test (enum tree_code condition, tree type, int target_pc)
1624 tree value1, value2;
1625 flush_quick_stack ();
1626 value1 = pop_value (type);
1627 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1628 expand_compare (condition, value1, value2, target_pc);
1631 /* Emit code for a COND-type opcode. */
1634 expand_cond (enum tree_code condition, tree type, int target_pc)
1636 tree value1, value2;
1637 flush_quick_stack ();
1638 /* note: pop values in opposite order */
1639 value2 = pop_value (type);
1640 value1 = pop_value (type);
1641 /* Maybe should check value1 and value2 for type compatibility ??? */
1642 expand_compare (condition, value1, value2, target_pc);
1646 expand_java_goto (int target_pc)
1648 tree target_label = lookup_label (target_pc);
1649 flush_quick_stack ();
1650 java_add_stmt (build (GOTO_EXPR, void_type_node, target_label));
1654 expand_java_switch (tree selector, int default_pc)
1656 tree switch_expr, x;
1658 flush_quick_stack ();
1659 switch_expr = build (SWITCH_EXPR, TREE_TYPE (selector), selector,
1660 NULL_TREE, NULL_TREE);
1661 java_add_stmt (switch_expr);
1663 x = build (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1664 create_artificial_label ());
1665 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1667 x = build (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1668 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1674 expand_java_add_case (tree switch_expr, int match, int target_pc)
1678 value = build_int_2 (match, match < 0 ? -1 : 0);
1679 TREE_TYPE (value) = TREE_TYPE (switch_expr);
1681 x = build (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1682 create_artificial_label ());
1683 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1685 x = build (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1686 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1691 expand_java_call (int target_pc, int return_address)
1692 int target_pc, return_address;
1694 tree target_label = lookup_label (target_pc);
1695 tree value = build_int_2 (return_address, return_address < 0 ? -1 : 0);
1697 flush_quick_stack ();
1698 expand_goto (target_label);
1702 expand_java_ret (tree return_address ATTRIBUTE_UNUSED)
1704 warning ("ret instruction not implemented");
1706 tree target_label = lookup_label (target_pc);
1707 flush_quick_stack ();
1708 expand_goto (target_label);
1714 pop_arguments (tree arg_types)
1716 if (arg_types == end_params_node)
1718 if (TREE_CODE (arg_types) == TREE_LIST)
1720 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1721 tree type = TREE_VALUE (arg_types);
1722 tree arg = pop_value (type);
1723 if (targetm.calls.promote_prototypes (type)
1724 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1725 && INTEGRAL_TYPE_P (type))
1726 arg = convert (integer_type_node, arg);
1727 return tree_cons (NULL_TREE, arg, tail);
1732 /* Build an expression to initialize the class CLAS.
1733 if EXPR is non-NULL, returns an expression to first call the initializer
1734 (if it is needed) and then calls EXPR. */
1737 build_class_init (tree clas, tree expr)
1741 /* An optimization: if CLAS is a superclass of the class we're
1742 compiling, we don't need to initialize it. However, if CLAS is
1743 an interface, it won't necessarily be initialized, even if we
1745 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1746 && inherits_from_p (current_class, clas))
1747 || current_class == clas)
1750 if (always_initialize_class_p)
1752 init = build (CALL_EXPR, void_type_node,
1753 build_address_of (soft_initclass_node),
1754 build_tree_list (NULL_TREE, build_class_ref (clas)),
1756 TREE_SIDE_EFFECTS (init) = 1;
1760 tree *init_test_decl;
1761 init_test_decl = java_treetreehash_new
1762 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1764 if (*init_test_decl == NULL)
1766 /* Build a declaration and mark it as a flag used to track
1767 static class initializations. */
1768 *init_test_decl = build_decl (VAR_DECL, NULL_TREE,
1770 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (*init_test_decl);
1771 LOCAL_CLASS_INITIALIZATION_FLAG (*init_test_decl) = 1;
1772 DECL_CONTEXT (*init_test_decl) = current_function_decl;
1773 DECL_FUNCTION_INIT_TEST_CLASS (*init_test_decl) = clas;
1774 /* Tell the check-init code to ignore this decl when not
1775 optimizing class initialization. */
1776 if (!STATIC_CLASS_INIT_OPT_P ())
1777 DECL_BIT_INDEX(*init_test_decl) = -1;
1778 DECL_INITIAL (*init_test_decl) = integer_zero_node;
1779 /* Don't emit any symbolic debugging info for this decl. */
1780 DECL_IGNORED_P (*init_test_decl) = 1;
1783 init = build (CALL_EXPR, void_type_node,
1784 build_address_of (soft_initclass_node),
1785 build_tree_list (NULL_TREE, build_class_ref (clas)),
1787 TREE_SIDE_EFFECTS (init) = 1;
1788 init = build (COND_EXPR, void_type_node,
1789 build (EQ_EXPR, boolean_type_node,
1790 *init_test_decl, boolean_false_node),
1791 init, integer_zero_node);
1792 TREE_SIDE_EFFECTS (init) = 1;
1793 init = build (COMPOUND_EXPR, TREE_TYPE (expr), init,
1794 build (MODIFY_EXPR, boolean_type_node,
1795 *init_test_decl, boolean_true_node));
1796 TREE_SIDE_EFFECTS (init) = 1;
1799 if (expr != NULL_TREE)
1801 expr = build (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1802 TREE_SIDE_EFFECTS (expr) = 1;
1809 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
1810 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
1811 tree arg_list ATTRIBUTE_UNUSED)
1814 if (is_compiled_class (self_type))
1816 if (!flag_indirect_dispatch
1817 || (!TREE_PUBLIC (method) && DECL_CONTEXT (method)))
1819 make_decl_rtl (method, NULL);
1820 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
1826 build_int_2 (get_symbol_table_index
1827 (method, &TYPE_ATABLE_METHODS (output_class)), 0);
1829 build (ARRAY_REF, method_ptr_type_node,
1830 TYPE_ATABLE_DECL (output_class), table_index);
1832 func = convert (method_ptr_type_node, func);
1836 /* We don't know whether the method has been (statically) compiled.
1837 Compile this code to get a reference to the method's code:
1839 SELF_TYPE->methods[METHOD_INDEX].ncode
1843 int method_index = 0;
1846 /* The method might actually be declared in some superclass, so
1847 we have to use its class context, not the caller's notion of
1848 where the method is. */
1849 self_type = DECL_CONTEXT (method);
1850 ref = build_class_ref (self_type);
1851 ref = build1 (INDIRECT_REF, class_type_node, ref);
1852 if (ncode_ident == NULL_TREE)
1853 ncode_ident = get_identifier ("ncode");
1854 if (methods_ident == NULL_TREE)
1855 methods_ident = get_identifier ("methods");
1856 ref = build (COMPONENT_REF, method_ptr_type_node, ref,
1857 lookup_field (&class_type_node, methods_ident));
1858 for (meth = TYPE_METHODS (self_type);
1859 ; meth = TREE_CHAIN (meth))
1863 if (meth == NULL_TREE)
1864 fatal_error ("method '%s' not found in class",
1865 IDENTIFIER_POINTER (DECL_NAME (method)));
1868 method_index *= int_size_in_bytes (method_type_node);
1869 ref = fold (build (PLUS_EXPR, method_ptr_type_node,
1870 ref, build_int_2 (method_index, 0)));
1871 ref = build1 (INDIRECT_REF, method_type_node, ref);
1872 func = build (COMPONENT_REF, nativecode_ptr_type_node,
1874 lookup_field (&method_type_node, ncode_ident));
1880 invoke_build_dtable (int is_invoke_interface, tree arg_list)
1882 tree dtable, objectref;
1884 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
1886 /* If we're dealing with interfaces and if the objectref
1887 argument is an array then get the dispatch table of the class
1888 Object rather than the one from the objectref. */
1889 objectref = (is_invoke_interface
1890 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list))) ?
1891 object_type_node : TREE_VALUE (arg_list));
1893 if (dtable_ident == NULL_TREE)
1894 dtable_ident = get_identifier ("vtable");
1895 dtable = build_java_indirect_ref (object_type_node, objectref,
1896 flag_check_references);
1897 dtable = build (COMPONENT_REF, dtable_ptr_type, dtable,
1898 lookup_field (&object_type_node, dtable_ident));
1903 /* Determine the index in SYMBOL_TABLE for a reference to the decl
1904 T. If this decl has not been seen before, it will be added to the
1905 otable_methods. If it has, the existing table slot will be
1909 get_symbol_table_index (tree t, tree *symbol_table)
1914 if (*symbol_table == NULL_TREE)
1916 *symbol_table = build_tree_list (t, t);
1920 method_list = *symbol_table;
1924 tree value = TREE_VALUE (method_list);
1928 if (TREE_CHAIN (method_list) == NULL_TREE)
1931 method_list = TREE_CHAIN (method_list);
1934 TREE_CHAIN (method_list) = build_tree_list (t, t);
1939 build_invokevirtual (tree dtable, tree method)
1942 tree nativecode_ptr_ptr_type_node
1943 = build_pointer_type (nativecode_ptr_type_node);
1947 if (flag_indirect_dispatch)
1950 = build_int_2 (get_symbol_table_index
1951 (method, &TYPE_OTABLE_METHODS (output_class)), 0);
1952 method_index = build (ARRAY_REF, integer_type_node,
1953 TYPE_OTABLE_DECL (output_class),
1958 /* We fetch the DECL_VINDEX field directly here, rather than
1959 using get_method_index(). DECL_VINDEX is the true offset
1960 from the vtable base to a method, regrdless of any extra
1961 words inserted at the start of the vtable. */
1962 method_index = DECL_VINDEX (method);
1963 method_index = size_binop (MULT_EXPR, method_index,
1964 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
1965 if (TARGET_VTABLE_USES_DESCRIPTORS)
1966 method_index = size_binop (MULT_EXPR, method_index,
1967 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
1970 func = fold (build (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
1971 convert (nativecode_ptr_ptr_type_node, method_index)));
1973 if (TARGET_VTABLE_USES_DESCRIPTORS)
1974 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
1976 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
1981 static GTY(()) tree class_ident;
1983 build_invokeinterface (tree dtable, tree method)
1990 /* We expand invokeinterface here. _Jv_LookupInterfaceMethod() will
1991 ensure that the selected method exists, is public and not
1992 abstract nor static. */
1994 if (class_ident == NULL_TREE)
1995 class_ident = get_identifier ("class");
1997 dtable = build_java_indirect_ref (dtable_type, dtable,
1998 flag_check_references);
1999 dtable = build (COMPONENT_REF, class_ptr_type, dtable,
2000 lookup_field (&dtable_type, class_ident));
2002 interface = DECL_CONTEXT (method);
2003 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2005 layout_class_methods (interface);
2007 if (flag_indirect_dispatch)
2010 build_int_2 (get_symbol_table_index
2011 (method, &TYPE_OTABLE_METHODS (output_class)), 0);
2013 build (ARRAY_REF, integer_type_node, TYPE_OTABLE_DECL (output_class),
2018 idx = build_int_2 (get_interface_method_index (method, interface), 0);
2021 lookup_arg = tree_cons (NULL_TREE, dtable,
2022 tree_cons (NULL_TREE, build_class_ref (interface),
2023 build_tree_list (NULL_TREE, idx)));
2025 return build (CALL_EXPR, ptr_type_node,
2026 build_address_of (soft_lookupinterfacemethod_node),
2027 lookup_arg, NULL_TREE);
2030 /* Expand one of the invoke_* opcodes.
2031 OCPODE is the specific opcode.
2032 METHOD_REF_INDEX is an index into the constant pool.
2033 NARGS is the number of arguments, or -1 if not specified. */
2036 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2038 tree method_signature = COMPONENT_REF_SIGNATURE(¤t_jcf->cpool, method_ref_index);
2039 tree method_name = COMPONENT_REF_NAME (¤t_jcf->cpool, method_ref_index);
2040 tree self_type = get_class_constant
2041 (current_jcf, COMPONENT_REF_CLASS_INDEX(¤t_jcf->cpool, method_ref_index));
2042 const char *const self_name
2043 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2044 tree call, func, method, arg_list, method_type;
2045 tree check = NULL_TREE;
2047 if (! CLASS_LOADED_P (self_type))
2049 load_class (self_type, 1);
2050 safe_layout_class (self_type);
2051 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2052 fatal_error ("failed to find class '%s'", self_name);
2054 layout_class_methods (self_type);
2056 if (ID_INIT_P (method_name))
2057 method = lookup_java_constructor (self_type, method_signature);
2059 method = lookup_java_method (self_type, method_name, method_signature);
2060 if (method == NULL_TREE)
2062 error ("class '%s' has no method named '%s' matching signature '%s'",
2064 IDENTIFIER_POINTER (method_name),
2065 IDENTIFIER_POINTER (method_signature));
2067 /* Invoke static can't invoke static/abstract method */
2068 else if (opcode == OPCODE_invokestatic)
2070 if (!METHOD_STATIC (method))
2072 error ("invokestatic on non static method");
2075 else if (METHOD_ABSTRACT (method))
2077 error ("invokestatic on abstract method");
2083 if (METHOD_STATIC (method))
2085 error ("invoke[non-static] on static method");
2090 if (method == NULL_TREE)
2092 method_type = get_type_from_signature (method_signature);
2093 pop_arguments (TYPE_ARG_TYPES (method_type));
2094 if (opcode != OPCODE_invokestatic)
2095 pop_type (self_type);
2096 method_type = promote_type (TREE_TYPE (method_type));
2097 push_value (convert (method_type, integer_zero_node));
2101 method_type = TREE_TYPE (method);
2102 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2103 flush_quick_stack ();
2106 if (opcode == OPCODE_invokestatic)
2107 func = build_known_method_ref (method, method_type, self_type,
2108 method_signature, arg_list);
2109 else if (opcode == OPCODE_invokespecial
2110 || (opcode == OPCODE_invokevirtual
2111 && (METHOD_PRIVATE (method)
2112 || METHOD_FINAL (method)
2113 || CLASS_FINAL (TYPE_NAME (self_type)))))
2115 /* If the object for the method call is null, we throw an
2116 exception. We don't do this if the object is the current
2117 method's `this'. In other cases we just rely on an
2118 optimization pass to eliminate redundant checks. FIXME:
2119 Unfortunately there doesn't seem to be a way to determine
2120 what the current method is right now.
2121 We do omit the check if we're calling <init>. */
2122 /* We use a SAVE_EXPR here to make sure we only evaluate
2123 the new `self' expression once. */
2124 tree save_arg = save_expr (TREE_VALUE (arg_list));
2125 TREE_VALUE (arg_list) = save_arg;
2126 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2127 func = build_known_method_ref (method, method_type, self_type,
2128 method_signature, arg_list);
2132 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2134 if (opcode == OPCODE_invokevirtual)
2135 func = build_invokevirtual (dtable, method);
2137 func = build_invokeinterface (dtable, method);
2140 if (TREE_CODE (func) == ADDR_EXPR)
2141 TREE_TYPE (func) = build_pointer_type (method_type);
2143 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2145 call = build (CALL_EXPR, TREE_TYPE (method_type), func, arg_list, NULL_TREE);
2146 TREE_SIDE_EFFECTS (call) = 1;
2147 call = check_for_builtin (method, call);
2149 if (check != NULL_TREE)
2151 call = build (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2152 TREE_SIDE_EFFECTS (call) = 1;
2155 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2156 java_add_stmt (call);
2160 flush_quick_stack ();
2164 /* Create a stub which will be put into the vtable but which will call
2168 build_jni_stub (tree method)
2170 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2171 tree jni_func_type, tem;
2172 tree env_var, res_var = NULL_TREE, block;
2173 tree method_args, res_type;
2179 tree klass = DECL_CONTEXT (method);
2180 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2181 klass = build_class_ref (klass);
2183 if (! METHOD_NATIVE (method) || ! flag_jni)
2186 DECL_ARTIFICIAL (method) = 1;
2187 DECL_EXTERNAL (method) = 0;
2189 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2190 DECL_CONTEXT (env_var) = method;
2192 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2194 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2195 TREE_TYPE (TREE_TYPE (method)));
2196 DECL_CONTEXT (res_var) = method;
2197 TREE_CHAIN (env_var) = res_var;
2200 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2201 TREE_STATIC (meth_var) = 1;
2202 TREE_PUBLIC (meth_var) = 0;
2203 DECL_EXTERNAL (meth_var) = 0;
2204 DECL_CONTEXT (meth_var) = method;
2205 DECL_ARTIFICIAL (meth_var) = 1;
2206 DECL_INITIAL (meth_var) = null_pointer_node;
2207 TREE_USED (meth_var) = 1;
2208 chainon (env_var, meth_var);
2209 build_result_decl (method);
2211 /* One strange way that the front ends are different is that they
2212 store arguments differently. */
2214 method_args = DECL_ARGUMENTS (method);
2216 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2217 block = build_block (env_var, NULL_TREE, NULL_TREE,
2218 method_args, NULL_TREE);
2219 TREE_SIDE_EFFECTS (block) = 1;
2220 /* When compiling from source we don't set the type of the block,
2221 because that will prevent patch_return from ever being run. */
2223 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2225 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2226 body = build (MODIFY_EXPR, ptr_type_node, env_var,
2227 build (CALL_EXPR, ptr_type_node,
2228 build_address_of (soft_getjnienvnewframe_node),
2229 build_tree_list (NULL_TREE, klass),
2231 CAN_COMPLETE_NORMALLY (body) = 1;
2233 /* All the arguments to this method become arguments to the
2234 underlying JNI function. If we had to wrap object arguments in a
2235 special way, we would do that here. */
2237 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2239 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (tem)));
2240 #ifdef PARM_BOUNDARY
2241 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2244 args_size += (arg_bits / BITS_PER_UNIT);
2246 args = tree_cons (NULL_TREE, tem, args);
2248 args = nreverse (args);
2249 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2251 /* For a static method the second argument is the class. For a
2252 non-static method the second argument is `this'; that is already
2253 available in the argument list. */
2254 if (METHOD_STATIC (method))
2256 args_size += int_size_in_bytes (TREE_TYPE (klass));
2257 args = tree_cons (NULL_TREE, klass, args);
2258 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2261 /* The JNIEnv structure is the first argument to the JNI function. */
2262 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2263 args = tree_cons (NULL_TREE, env_var, args);
2264 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2266 /* We call _Jv_LookupJNIMethod to find the actual underlying
2267 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2268 exception if this function is not found at runtime. */
2269 tem = build_tree_list (NULL_TREE, build_int_2 (args_size, 0));
2270 method_sig = build_java_signature (TREE_TYPE (method));
2271 lookup_arg = tree_cons (NULL_TREE,
2272 build_utf8_ref (unmangle_classname
2273 (IDENTIFIER_POINTER (method_sig),
2274 IDENTIFIER_LENGTH (method_sig))),
2276 tem = DECL_NAME (method);
2278 = tree_cons (NULL_TREE, klass,
2279 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2281 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2283 #ifdef MODIFY_JNI_METHOD_CALL
2284 tem = MODIFY_JNI_METHOD_CALL (tem);
2287 jni_func_type = build_pointer_type (tem);
2289 jnifunc = build (COND_EXPR, ptr_type_node,
2291 build (MODIFY_EXPR, ptr_type_node,
2293 build (CALL_EXPR, ptr_type_node,
2294 build_address_of (soft_lookupjnimethod_node),
2295 lookup_arg, NULL_TREE)));
2297 /* Now we make the actual JNI call via the resulting function
2299 call = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2300 build1 (NOP_EXPR, jni_func_type, jnifunc),
2303 /* If the JNI call returned a result, capture it here. If we had to
2304 unwrap JNI object results, we would do that here. */
2305 if (res_var != NULL_TREE)
2306 call = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2309 TREE_SIDE_EFFECTS (call) = 1;
2310 CAN_COMPLETE_NORMALLY (call) = 1;
2312 body = build (COMPOUND_EXPR, void_type_node, body, call);
2313 TREE_SIDE_EFFECTS (body) = 1;
2315 /* Now free the environment we allocated. */
2316 call = build (CALL_EXPR, ptr_type_node,
2317 build_address_of (soft_jnipopsystemframe_node),
2318 build_tree_list (NULL_TREE, env_var),
2320 TREE_SIDE_EFFECTS (call) = 1;
2321 CAN_COMPLETE_NORMALLY (call) = 1;
2322 body = build (COMPOUND_EXPR, void_type_node, body, call);
2323 TREE_SIDE_EFFECTS (body) = 1;
2325 /* Finally, do the return. */
2326 res_type = void_type_node;
2327 if (res_var != NULL_TREE)
2330 if (! DECL_RESULT (method))
2332 /* Make sure we copy the result variable to the actual
2333 result. We use the type of the DECL_RESULT because it
2334 might be different from the return type of the function:
2335 it might be promoted. */
2336 drt = TREE_TYPE (DECL_RESULT (method));
2337 if (drt != TREE_TYPE (res_var))
2338 res_var = build1 (CONVERT_EXPR, drt, res_var);
2339 res_var = build (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2340 TREE_SIDE_EFFECTS (res_var) = 1;
2343 body = build (COMPOUND_EXPR, void_type_node, body,
2344 build1 (RETURN_EXPR, res_type, res_var));
2345 TREE_SIDE_EFFECTS (body) = 1;
2347 bind = build (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2352 /* Expand an operation to extract from or store into a field.
2353 IS_STATIC is 1 iff the field is static.
2354 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2355 FIELD_REF_INDEX is an index into the constant pool. */
2358 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2361 get_class_constant (current_jcf,
2362 COMPONENT_REF_CLASS_INDEX (¤t_jcf->cpool,
2364 const char *self_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2365 tree field_name = COMPONENT_REF_NAME (¤t_jcf->cpool, field_ref_index);
2366 tree field_signature = COMPONENT_REF_SIGNATURE (¤t_jcf->cpool,
2368 tree field_type = get_type_from_signature (field_signature);
2369 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2372 tree field_decl = lookup_field (&self_type, field_name);
2373 if (field_decl == error_mark_node)
2377 else if (field_decl == NULL_TREE)
2379 error ("missing field '%s' in '%s'",
2380 IDENTIFIER_POINTER (field_name), self_name);
2383 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2385 error ("mismatching signature for field '%s' in '%s'",
2386 IDENTIFIER_POINTER (field_name), self_name);
2389 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2393 push_value (convert (field_type, integer_zero_node));
2394 flush_quick_stack ();
2398 field_ref = build_field_ref (field_ref, self_type, field_name);
2400 field_ref = build_class_init (self_type, field_ref);
2403 flush_quick_stack ();
2404 if (FIELD_FINAL (field_decl))
2406 if (DECL_CONTEXT (field_decl) != current_class)
2407 error ("%Jassignment to final field '%D' not in field's class",
2408 field_decl, field_decl);
2409 else if (FIELD_STATIC (field_decl))
2411 if (!DECL_CLINIT_P (current_function_decl))
2412 warning ("%Jassignment to final static field `%D' not in "
2413 "class initializer",
2414 field_decl, field_decl);
2418 tree cfndecl_name = DECL_NAME (current_function_decl);
2419 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2420 && !ID_FINIT_P (cfndecl_name))
2421 warning ("%Jassignment to final field '%D' not in constructor",
2422 field_decl, field_decl);
2425 java_add_stmt (build (MODIFY_EXPR,
2426 TREE_TYPE (field_ref), field_ref, new_value));
2429 push_value (field_ref);
2433 load_type_state (tree label)
2436 tree vec = LABEL_TYPE_STATE (label);
2437 int cur_length = TREE_VEC_LENGTH (vec);
2438 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2439 for (i = 0; i < cur_length; i++)
2440 type_map [i] = TREE_VEC_ELT (vec, i);
2443 /* Do the expansion of a Java switch. With Gcc, switches are front-end
2444 dependent things, but they rely on gcc routines. This function is
2445 placed here because it uses things defined locally in parse.y. */
2448 case_identity (tree t __attribute__ ((__unused__)), tree v)
2453 /* Return the name of the vtable for an array of a given primitive
2456 get_primitive_array_vtable (tree elt)
2459 if (elt == boolean_type_node)
2460 r = boolean_array_vtable;
2461 else if (elt == byte_type_node)
2462 r = byte_array_vtable;
2463 else if (elt == char_type_node)
2464 r = char_array_vtable;
2465 else if (elt == short_type_node)
2466 r = short_array_vtable;
2467 else if (elt == int_type_node)
2468 r = int_array_vtable;
2469 else if (elt == long_type_node)
2470 r = long_array_vtable;
2471 else if (elt == float_type_node)
2472 r = float_array_vtable;
2473 else if (elt == double_type_node)
2474 r = double_array_vtable;
2477 return build_address_of (r);
2481 java_expand_expr (tree exp, rtx target, enum machine_mode tmode,
2482 int modifier /* Actually an enum expand_modifier. */,
2483 rtx *alt_rtl ATTRIBUTE_UNUSED)
2489 switch (TREE_CODE (exp))
2492 case EXPR_WITH_FILE_LOCATION:
2495 const char *saved_input_filename = input_filename;
2496 int saved_lineno = input_line;
2497 input_filename = EXPR_WFL_FILENAME (exp);
2498 input_line = EXPR_WFL_LINENO (exp);
2499 if (EXPR_WFL_EMIT_LINE_NOTE (exp))
2500 emit_line_note (input_location);
2501 /* Possibly avoid switching back and forth here. */
2502 to_return = expand_expr (EXPR_WFL_NODE (exp), target, tmode, modifier);
2503 input_filename = saved_input_filename;
2504 input_line = saved_lineno;
2508 case NEW_ARRAY_INIT:
2511 tree array_type = TREE_TYPE (TREE_TYPE (exp));
2512 tree element_type = TYPE_ARRAY_ELEMENT (array_type);
2513 tree data_fld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
2514 HOST_WIDE_INT ilength = java_array_type_length (array_type);
2515 tree length = build_int_2 (ilength, 0);
2516 tree init = TREE_OPERAND (exp, 0);
2519 /* See if we can generate the array statically. */
2520 if (TREE_CONSTANT (init) && TREE_STATIC (exp)
2521 && JPRIMITIVE_TYPE_P (element_type))
2523 tree temp, value, init_decl;
2525 START_RECORD_CONSTRUCTOR (temp, object_type_node);
2526 PUSH_FIELD_VALUE (temp, "vtable",
2527 get_primitive_array_vtable (element_type));
2528 if (! flag_hash_synchronization)
2529 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
2530 FINISH_RECORD_CONSTRUCTOR (temp);
2531 START_RECORD_CONSTRUCTOR (value, array_type);
2532 PUSH_SUPER_VALUE (value, temp);
2533 PUSH_FIELD_VALUE (value, "length", length);
2534 PUSH_FIELD_VALUE (value, "data", init);
2535 FINISH_RECORD_CONSTRUCTOR (value);
2537 init_decl = build_decl (VAR_DECL, generate_name (), array_type);
2538 pushdecl_top_level (init_decl);
2539 TREE_STATIC (init_decl) = 1;
2540 DECL_INITIAL (init_decl) = value;
2541 DECL_IGNORED_P (init_decl) = 1;
2542 TREE_READONLY (init_decl) = 1;
2543 /* Hash synchronization requires at least 64-bit alignment. */
2544 if (flag_hash_synchronization && POINTER_SIZE < 64)
2545 DECL_ALIGN (init_decl) = 64;
2546 rest_of_decl_compilation (init_decl, NULL, 1, 0);
2547 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl)) = 1;
2548 init = build1 (ADDR_EXPR, TREE_TYPE (exp), init_decl);
2549 r = expand_expr (init, target, tmode, modifier);
2553 array_decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (exp));
2554 expand_decl (array_decl);
2555 tmp = expand_assignment (array_decl,
2556 build_new_array (element_type, length),
2558 if (TREE_CONSTANT (init)
2559 && ilength >= 10 && JPRIMITIVE_TYPE_P (element_type))
2562 init_decl = build_decl (VAR_DECL, generate_name (),
2564 pushdecl_top_level (init_decl);
2565 TREE_STATIC (init_decl) = 1;
2566 DECL_INITIAL (init_decl) = init;
2567 DECL_IGNORED_P (init_decl) = 1;
2568 TREE_READONLY (init_decl) = 1;
2569 rest_of_decl_compilation (init_decl, NULL, 1, 0);
2570 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl)) = 1;
2573 expand_assignment (build (COMPONENT_REF, TREE_TYPE (data_fld),
2574 build_java_indirect_ref (array_type,
2575 array_decl, flag_check_references),
2576 data_fld), init, 0);
2580 if (BLOCK_EXPR_BODY (exp))
2584 tree body = BLOCK_EXPR_BODY (exp);
2585 /* Set to 1 or more when we found a static class
2586 initialization flag. */
2587 int found_class_initialization_flag = 0;
2589 pushlevel (2); /* 2 and above */
2590 expand_start_bindings (0);
2591 local = BLOCK_EXPR_DECLS (exp);
2594 tree next = TREE_CHAIN (local);
2595 found_class_initialization_flag +=
2596 LOCAL_CLASS_INITIALIZATION_FLAG_P (local);
2597 layout_decl (local, 0);
2598 expand_decl (pushdecl (local));
2602 /* Emit initialization code for test flags if we saw one. */
2603 if (! always_initialize_class_p
2604 && current_function_decl
2605 && found_class_initialization_flag)
2607 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
2608 emit_init_test_initialization, NULL);
2610 /* Avoid deep recursion for long block. */
2611 while (TREE_CODE (body) == COMPOUND_EXPR)
2613 expand_expr (TREE_OPERAND (body, 0), const0_rtx, VOIDmode, 0);
2615 body = TREE_OPERAND (body, 1);
2617 last = expand_expr (body, NULL_RTX, VOIDmode, 0);
2619 expand_end_bindings (getdecls (), 1, 0);
2628 if (pushcase (TREE_OPERAND (exp, 0), case_identity,
2629 build_decl (LABEL_DECL, NULL_TREE, NULL_TREE),
2632 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (exp);
2634 (wfl_operator, "Duplicate case label: `%s'",
2635 print_int_node (TREE_OPERAND (exp, 0)));
2641 pushcase (NULL_TREE, 0,
2642 build_decl (LABEL_DECL, NULL_TREE, NULL_TREE), NULL);
2646 /* We expand a try[-catch] block */
2648 /* Expand the try block */
2649 expand_eh_region_start ();
2650 expand_expr_stmt (TREE_OPERAND (exp, 0));
2651 expand_start_all_catch ();
2653 /* Expand all catch clauses (EH handlers) */
2654 for (current = TREE_OPERAND (exp, 1); current;
2655 current = TREE_CHAIN (current))
2657 tree catch = TREE_OPERAND (current, 0);
2658 tree decl = BLOCK_EXPR_DECLS (catch);
2659 tree type = (decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE);
2661 expand_start_catch (prepare_eh_table_type (type));
2662 expand_expr_stmt (TREE_OPERAND (current, 0));
2663 expand_end_catch ();
2665 expand_end_all_catch ();
2668 case JAVA_EXC_OBJ_EXPR:
2669 return expand_expr (build_exception_object_ref (TREE_TYPE (exp)),
2670 target, tmode, modifier);
2673 internal_error ("can't expand %s", tree_code_name [TREE_CODE (exp)]);
2677 /* Go over METHOD's bytecode and note instruction starts in
2678 instruction_bits[]. */
2681 note_instructions (JCF *jcf, tree method)
2684 unsigned char* byte_ops;
2685 long length = DECL_CODE_LENGTH (method);
2690 #undef RET /* Defined by config/i386/i386.h */
2692 #define BCODE byte_ops
2693 #define BYTE_type_node byte_type_node
2694 #define SHORT_type_node short_type_node
2695 #define INT_type_node int_type_node
2696 #define LONG_type_node long_type_node
2697 #define CHAR_type_node char_type_node
2698 #define PTR_type_node ptr_type_node
2699 #define FLOAT_type_node float_type_node
2700 #define DOUBLE_type_node double_type_node
2701 #define VOID_type_node void_type_node
2702 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2703 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2704 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2705 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2707 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2709 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2710 byte_ops = jcf->read_ptr;
2711 instruction_bits = xrealloc (instruction_bits, length + 1);
2712 memset (instruction_bits, 0, length + 1);
2714 /* This pass figures out which PC can be the targets of jumps. */
2715 for (PC = 0; PC < length;)
2717 int oldpc = PC; /* PC at instruction start. */
2718 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2719 switch (byte_ops[PC++])
2721 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2723 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2726 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2728 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2729 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2730 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2731 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2732 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2733 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2734 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2735 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2737 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2738 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2739 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2740 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2741 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2742 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2743 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2744 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2746 /* two forms of wide instructions */
2747 #define PRE_SPECIAL_WIDE(IGNORE) \
2749 int modified_opcode = IMMEDIATE_u1; \
2750 if (modified_opcode == OPCODE_iinc) \
2752 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2753 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2757 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2761 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2763 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2765 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2766 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2767 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2768 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2769 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2770 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2771 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2772 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2773 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2774 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2776 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2777 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2778 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2779 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2780 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2781 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2782 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2784 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2786 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2788 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2789 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2791 #define PRE_LOOKUP_SWITCH \
2792 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2793 NOTE_LABEL (default_offset+oldpc); \
2795 while (--npairs >= 0) { \
2796 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2797 jint offset = IMMEDIATE_s4; \
2798 NOTE_LABEL (offset+oldpc); } \
2801 #define PRE_TABLE_SWITCH \
2802 { jint default_offset = IMMEDIATE_s4; \
2803 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2804 NOTE_LABEL (default_offset+oldpc); \
2806 while (low++ <= high) { \
2807 jint offset = IMMEDIATE_s4; \
2808 NOTE_LABEL (offset+oldpc); } \
2811 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2812 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2813 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2814 (void)(IMMEDIATE_u2); \
2815 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2817 #include "javaop.def"
2824 expand_byte_code (JCF *jcf, tree method)
2828 const unsigned char *linenumber_pointer;
2829 int dead_code_index = -1;
2830 unsigned char* byte_ops;
2831 long length = DECL_CODE_LENGTH (method);
2834 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2835 byte_ops = jcf->read_ptr;
2837 /* We make an initial pass of the line number table, to note
2838 which instructions have associated line number entries. */
2839 linenumber_pointer = linenumber_table;
2840 for (i = 0; i < linenumber_count; i++)
2842 int pc = GET_u2 (linenumber_pointer);
2843 linenumber_pointer += 4;
2845 warning ("invalid PC in line number table");
2848 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2849 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2850 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2854 if (! verify_jvm_instructions (jcf, byte_ops, length))
2857 /* Translate bytecodes. */
2858 linenumber_pointer = linenumber_table;
2859 for (PC = 0; PC < length;)
2861 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2863 tree label = lookup_label (PC);
2864 flush_quick_stack ();
2865 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2866 java_add_stmt (build (LABEL_EXPR, void_type_node, label));
2867 if (LABEL_VERIFIED (label) || PC == 0)
2868 load_type_state (label);
2871 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2873 if (dead_code_index == -1)
2875 /* This is the start of a region of unreachable bytecodes.
2876 They still need to be processed in order for EH ranges
2877 to get handled correctly. However, we can simply
2878 replace these bytecodes with nops. */
2879 dead_code_index = PC;
2882 /* Turn this bytecode into a nop. */
2887 if (dead_code_index != -1)
2889 /* We've just reached the end of a region of dead code. */
2891 warning ("unreachable bytecode from %d to before %d",
2892 dead_code_index, PC);
2893 dead_code_index = -1;
2897 /* Handle possible line number entry for this PC.
2899 This code handles out-of-order and multiple linenumbers per PC,
2900 but is optimized for the case of line numbers increasing
2901 monotonically with PC. */
2902 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2904 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2905 || GET_u2 (linenumber_pointer) != PC)
2906 linenumber_pointer = linenumber_table;
2907 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2909 int pc = GET_u2 (linenumber_pointer);
2910 linenumber_pointer += 4;
2913 input_location.line = GET_u2 (linenumber_pointer - 2);
2914 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2919 maybe_pushlevels (PC);
2920 PC = process_jvm_instruction (PC, byte_ops, length);
2921 maybe_poplevels (PC);
2924 if (dead_code_index != -1)
2926 /* We've just reached the end of a region of dead code. */
2928 warning ("unreachable bytecode from %d to the end of the method",
2934 java_push_constant_from_pool (JCF *jcf, int index)
2937 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
2940 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
2941 index = alloc_name_constant (CONSTANT_String, name);
2942 c = build_ref_from_constant_pool (index);
2943 c = convert (promote_type (string_type_node), c);
2946 c = get_constant (jcf, index);
2951 process_jvm_instruction (int PC, const unsigned char* byte_ops,
2952 long length ATTRIBUTE_UNUSED)
2954 const char *opname; /* Temporary ??? */
2955 int oldpc = PC; /* PC at instruction start. */
2957 /* If the instruction is at the beginning of a exception handler,
2958 replace the top of the stack with the thrown object reference */
2959 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
2961 tree type = pop_type (ptr_type_node);
2962 push_value (build_exception_object_ref (type));
2965 switch (byte_ops[PC++])
2967 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2970 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2973 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
2975 int saw_index = 0; \
2976 int index = OPERAND_VALUE; \
2977 build_java_ret (find_local_variable (index, ptr_type_node, oldpc)); \
2980 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
2982 /* OPERAND_VALUE may have side-effects on PC */ \
2983 int opvalue = OPERAND_VALUE; \
2984 build_java_jsr (oldpc + opvalue, PC); \
2987 /* Push a constant onto the stack. */
2988 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
2989 { int saw_index = 0; int ival = (OPERAND_VALUE); \
2990 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
2991 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
2993 /* internal macro added for use by the WIDE case */
2994 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
2995 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
2997 /* Push local variable onto the opcode stack. */
2998 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3000 /* have to do this since OPERAND_VALUE may have side-effects */ \
3001 int opvalue = OPERAND_VALUE; \
3002 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3005 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3006 expand_java_return (OPERAND_TYPE##_type_node)
3008 #define REM_EXPR TRUNC_MOD_EXPR
3009 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3010 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3012 #define FIELD(IS_STATIC, IS_PUT) \
3013 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3015 #define TEST(OPERAND_TYPE, CONDITION) \
3016 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3018 #define COND(OPERAND_TYPE, CONDITION) \
3019 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3021 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3022 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3024 #define BRANCH_GOTO(OPERAND_VALUE) \
3025 expand_java_goto (oldpc + OPERAND_VALUE)
3027 #define BRANCH_CALL(OPERAND_VALUE) \
3028 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3031 #define BRANCH_RETURN(OPERAND_VALUE) \
3033 tree type = OPERAND_TYPE##_type_node; \
3034 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3035 expand_java_ret (value); \
3039 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3040 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3041 fprintf (stderr, "(not implemented)\n")
3042 #define NOT_IMPL1(OPERAND_VALUE) \
3043 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3044 fprintf (stderr, "(not implemented)\n")
3046 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3048 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3050 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3052 #define STACK_SWAP(COUNT) java_stack_swap()
3054 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3055 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3056 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3058 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3059 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3061 #define LOOKUP_SWITCH \
3062 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3063 tree selector = pop_value (INT_type_node); \
3064 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3065 while (--npairs >= 0) \
3067 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3068 expand_java_add_case (switch_expr, match, oldpc + offset); \
3072 #define TABLE_SWITCH \
3073 { jint default_offset = IMMEDIATE_s4; \
3074 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3075 tree selector = pop_value (INT_type_node); \
3076 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3077 for (; low <= high; low++) \
3079 jint offset = IMMEDIATE_s4; \
3080 expand_java_add_case (switch_expr, low, oldpc + offset); \
3084 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3085 { int opcode = byte_ops[PC-1]; \
3086 int method_ref_index = IMMEDIATE_u2; \
3088 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3090 expand_invoke (opcode, method_ref_index, nargs); \
3093 /* Handle new, checkcast, instanceof */
3094 #define OBJECT(TYPE, OP) \
3095 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3097 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3099 #define ARRAY_LOAD(OPERAND_TYPE) \
3101 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3104 #define ARRAY_STORE(OPERAND_TYPE) \
3106 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3109 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3110 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3111 #define ARRAY_NEW_PTR() \
3112 push_value (build_anewarray (get_class_constant (current_jcf, \
3114 pop_value (int_type_node)));
3115 #define ARRAY_NEW_NUM() \
3117 int atype = IMMEDIATE_u1; \
3118 push_value (build_newarray (atype, pop_value (int_type_node)));\
3120 #define ARRAY_NEW_MULTI() \
3122 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3123 int ndims = IMMEDIATE_u1; \
3124 expand_java_multianewarray( class, ndims ); \
3127 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3128 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3129 pop_value (OPERAND_TYPE##_type_node))));
3131 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3133 push_value (build1 (NOP_EXPR, int_type_node, \
3134 (convert (TO_TYPE##_type_node, \
3135 pop_value (FROM_TYPE##_type_node))))); \
3138 #define CONVERT(FROM_TYPE, TO_TYPE) \
3140 push_value (convert (TO_TYPE##_type_node, \
3141 pop_value (FROM_TYPE##_type_node))); \
3144 /* internal macro added for use by the WIDE case
3145 Added TREE_TYPE (decl) assignment, apbianco */
3146 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3149 int index = OPVALUE; \
3150 tree type = OPTYPE; \
3151 value = pop_value (type); \
3152 type = TREE_TYPE (value); \
3153 decl = find_local_variable (index, type, oldpc); \
3154 set_local_type (index, type); \
3155 java_add_stmt (build (MODIFY_EXPR, type, decl, value)); \
3156 update_aliases (decl, index); \
3159 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3161 /* have to do this since OPERAND_VALUE may have side-effects */ \
3162 int opvalue = OPERAND_VALUE; \
3163 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3166 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3167 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3169 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3170 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3172 #define MONITOR_OPERATION(call) \
3174 tree o = pop_value (ptr_type_node); \
3176 flush_quick_stack (); \
3177 c = build_java_monitor (call, o); \
3178 TREE_SIDE_EFFECTS (c) = 1; \
3179 java_add_stmt (c); \
3182 #define SPECIAL_IINC(IGNORED) \
3184 unsigned int local_var_index = IMMEDIATE_u1; \
3185 int ival = IMMEDIATE_s1; \
3186 expand_iinc(local_var_index, ival, oldpc); \
3189 #define SPECIAL_WIDE(IGNORED) \
3191 int modified_opcode = IMMEDIATE_u1; \
3192 unsigned int local_var_index = IMMEDIATE_u2; \
3193 switch (modified_opcode) \
3197 int ival = IMMEDIATE_s2; \
3198 expand_iinc (local_var_index, ival, oldpc); \
3201 case OPCODE_iload: \
3202 case OPCODE_lload: \
3203 case OPCODE_fload: \
3204 case OPCODE_dload: \
3205 case OPCODE_aload: \
3207 /* duplicate code from LOAD macro */ \
3208 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3211 case OPCODE_istore: \
3212 case OPCODE_lstore: \
3213 case OPCODE_fstore: \
3214 case OPCODE_dstore: \
3215 case OPCODE_astore: \
3217 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3221 error ("unrecogized wide sub-instruction"); \
3225 #define SPECIAL_THROW(IGNORED) \
3226 build_java_athrow (pop_value (throwable_type_node))
3228 #define SPECIAL_BREAK NOT_IMPL1
3229 #define IMPL NOT_IMPL
3231 #include "javaop.def"
3234 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3239 /* Return the opcode at PC in the code section pointed to by
3242 static unsigned char
3243 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3245 unsigned char opcode;
3246 long absolute_offset = (long)JCF_TELL (jcf);
3248 JCF_SEEK (jcf, code_offset);
3249 opcode = jcf->read_ptr [pc];
3250 JCF_SEEK (jcf, absolute_offset);
3254 /* Some bytecode compilers are emitting accurate LocalVariableTable
3255 attributes. Here's an example:
3260 Attribute "LocalVariableTable"
3261 slot #<n>: ... (PC: PC+1 length: L)
3263 This is accurate because the local in slot <n> really exists after
3264 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3266 This procedure recognizes this situation and extends the live range
3267 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3268 length of the store instruction.)
3270 This function is used by `give_name_to_locals' so that a local's
3271 DECL features a DECL_LOCAL_START_PC such that the first related
3272 store operation will use DECL as a destination, not a unrelated
3273 temporary created for the occasion.
3275 This function uses a global (instruction_bits) `note_instructions' should
3276 have allocated and filled properly. */
3279 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3280 int start_pc, int slot)
3282 int first, index, opcode;
3291 /* Find last previous instruction and remember it */
3292 for (pc = start_pc-1; pc; pc--)
3293 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3297 /* Retrieve the instruction, handle `wide'. */
3298 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3299 if (opcode == OPCODE_wide)
3302 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3307 case OPCODE_astore_0:
3308 case OPCODE_astore_1:
3309 case OPCODE_astore_2:
3310 case OPCODE_astore_3:
3311 first = OPCODE_astore_0;
3314 case OPCODE_istore_0:
3315 case OPCODE_istore_1:
3316 case OPCODE_istore_2:
3317 case OPCODE_istore_3:
3318 first = OPCODE_istore_0;
3321 case OPCODE_lstore_0:
3322 case OPCODE_lstore_1:
3323 case OPCODE_lstore_2:
3324 case OPCODE_lstore_3:
3325 first = OPCODE_lstore_0;
3328 case OPCODE_fstore_0:
3329 case OPCODE_fstore_1:
3330 case OPCODE_fstore_2:
3331 case OPCODE_fstore_3:
3332 first = OPCODE_fstore_0;
3335 case OPCODE_dstore_0:
3336 case OPCODE_dstore_1:
3337 case OPCODE_dstore_2:
3338 case OPCODE_dstore_3:
3339 first = OPCODE_dstore_0;
3347 index = peek_opcode_at_pc (jcf, code_offset, pc);
3350 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3351 index = (other << 8) + index;
3356 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3357 means we have a <t>store. */
3358 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3364 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3365 order, as specified by Java Language Specification.
3367 The problem is that while expand_expr will evaluate its sub-operands in
3368 left-to-right order, for variables it will just return an rtx (i.e.
3369 an lvalue) for the variable (rather than an rvalue). So it is possible
3370 that a later sub-operand will change the register, and when the
3371 actual operation is done, it will use the new value, when it should
3372 have used the original value.
3374 We fix this by using save_expr. This forces the sub-operand to be
3375 copied into a fresh virtual register,
3377 For method invocation, we modify the arguments so that a
3378 left-to-right order evaluation is performed. Saved expressions
3379 will, in CALL_EXPR order, be reused when the call will be expanded.
3383 force_evaluation_order (tree node)
3385 if (flag_syntax_only)
3387 if (TREE_CODE (node) == CALL_EXPR
3388 || TREE_CODE (node) == NEW_CLASS_EXPR
3389 || (TREE_CODE (node) == COMPOUND_EXPR
3390 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3391 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3395 if (!TREE_OPERAND (node, 1))
3400 /* Position arg properly, account for wrapped around ctors. */
3401 if (TREE_CODE (node) == COMPOUND_EXPR)
3402 arg = TREE_OPERAND (node, 0);
3404 arg = TREE_OPERAND (arg, 1);
3406 /* Not having a list of argument here is an error. */
3407 if (TREE_CODE (arg) != TREE_LIST)
3410 /* This reverses the evaluation order. This is a desired effect. */
3411 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3413 tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3414 cmp = (cmp == NULL_TREE ? saved :
3415 build (COMPOUND_EXPR, void_type_node, cmp, saved));
3416 TREE_VALUE (arg) = saved;
3419 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3420 TREE_SIDE_EFFECTS (cmp) = 1;
3424 cmp = build (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3425 if (TREE_TYPE (cmp) != void_type_node)
3426 cmp = save_expr (cmp);
3427 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3428 TREE_SIDE_EFFECTS (cmp) = 1;
3435 /* Called for every element in DECL_FUNCTION_INIT_TEST_TABLE of a
3436 method in order to emit initialization code for each test flag. */
3439 emit_init_test_initialization (void **entry, void *x ATTRIBUTE_UNUSED)
3441 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
3442 tree klass = build_class_ref (ite->key);
3445 /* If the DECL_INITIAL of the test flag is set to true, it
3446 means that the class is already initialized the time it
3448 if (DECL_INITIAL (ite->value) == boolean_true_node)
3449 rhs = boolean_true_node;
3450 /* Otherwise, we initialize the class init check variable by looking
3451 at the `state' field of the class to see if it is already
3452 initialized. This makes things a bit faster if the class is
3453 already initialized, which should be the common case. */
3455 rhs = build (GE_EXPR, boolean_type_node,
3456 build (COMPONENT_REF, byte_type_node,
3457 build1 (INDIRECT_REF, class_type_node, klass),
3458 lookup_field (&class_type_node,
3459 get_identifier ("state"))),
3460 build_int_2 (JV_STATE_DONE, 0));
3462 expand_expr_stmt (build (MODIFY_EXPR, boolean_type_node,
3467 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3468 location where an expression or an identifier were encountered. It
3469 is necessary for languages where the frontend parser will handle
3470 recursively more than one file (Java is one of them). */
3473 build_expr_wfl (tree node, const char *file, int line, int col)
3475 static const char *last_file = 0;
3476 static tree last_filenode = NULL_TREE;
3477 tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
3479 EXPR_WFL_NODE (wfl) = node;
3480 EXPR_WFL_SET_LINECOL (wfl, line, col);
3481 if (file != last_file)
3484 last_filenode = file ? get_identifier (file) : NULL_TREE;
3487 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3490 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3491 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3492 TREE_TYPE (wfl) = TREE_TYPE (node);
3499 /* Build a node to represent empty statements and blocks. */
3502 build_java_empty_stmt (void)
3504 tree t = build_empty_stmt ();
3505 CAN_COMPLETE_NORMALLY (t) = 1;
3509 #include "gt-java-expr.h"