1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
28 #include "coretypes.h"
29 #include "tm.h" /* For INT_TYPE_SIZE,
30 TARGET_VTABLE_USES_DESCRIPTORS,
32 MODIFY_JNI_METHOD_CALL and
38 #include "fold-const.h"
39 #include "stringpool.h"
40 #include "stor-layout.h"
42 #include "java-tree.h"
44 #include "java-opcodes.h"
46 #include "java-except.h"
48 #include "diagnostic-core.h"
49 #include "tree-iterator.h"
52 static void flush_quick_stack (void);
53 static void push_value (tree
);
54 static tree
pop_value (tree
);
55 static void java_stack_swap (void);
56 static void java_stack_dup (int, int);
57 static void build_java_athrow (tree
);
58 static void build_java_jsr (int, int);
59 static void build_java_ret (tree
);
60 static void expand_java_multianewarray (tree
, int);
61 static void expand_java_arraystore (tree
);
62 static void expand_java_arrayload (tree
);
63 static void expand_java_array_length (void);
64 static tree
build_java_monitor (tree
, tree
);
65 static void expand_java_pushc (int, tree
);
66 static void expand_java_return (tree
);
67 static void expand_load_internal (int, tree
, int);
68 static void expand_java_NEW (tree
);
69 static void expand_java_INSTANCEOF (tree
);
70 static void expand_java_CHECKCAST (tree
);
71 static void expand_iinc (unsigned int, int, int);
72 static void expand_java_binop (tree
, enum tree_code
);
73 static void note_label (int, int);
74 static void expand_compare (enum tree_code
, tree
, tree
, int);
75 static void expand_test (enum tree_code
, tree
, int);
76 static void expand_cond (enum tree_code
, tree
, int);
77 static void expand_java_goto (int);
78 static tree
expand_java_switch (tree
, int);
79 static void expand_java_add_case (tree
, int, int);
80 static vec
<tree
, va_gc
> *pop_arguments (tree
);
81 static void expand_invoke (int, int, int);
82 static void expand_java_field_op (int, int, int);
83 static void java_push_constant_from_pool (struct JCF
*, int);
84 static void java_stack_pop (int);
85 static tree
build_java_throw_out_of_bounds_exception (tree
);
86 static tree
build_java_check_indexed_type (tree
, tree
);
87 static unsigned char peek_opcode_at_pc (struct JCF
*, int, int);
88 static void promote_arguments (void);
89 static void cache_cpool_data_ref (void);
91 static GTY(()) tree operand_type
[59];
93 static GTY(()) tree methods_ident
;
94 static GTY(()) tree ncode_ident
;
95 tree dtable_ident
= NULL_TREE
;
97 /* Set to nonzero value in order to emit class initialization code
98 before static field references. */
99 int always_initialize_class_p
= 0;
101 /* We store the stack state in two places:
102 Within a basic block, we use the quick_stack, which is a vec of expression
104 This is the top part of the stack; below that we use find_stack_slot.
105 At the end of a basic block, the quick_stack must be flushed
106 to the stack slot array (as handled by find_stack_slot).
107 Using quick_stack generates better code (especially when
108 compiled without optimization), because we do not have to
109 explicitly store and load trees to temporary variables.
111 If a variable is on the quick stack, it means the value of variable
112 when the quick stack was last flushed. Conceptually, flush_quick_stack
113 saves all the quick_stack elements in parallel. However, that is
114 complicated, so it actually saves them (i.e. copies each stack value
115 to is home virtual register) from low indexes. This allows a quick_stack
116 element at index i (counting from the bottom of stack the) to references
117 slot virtuals for register that are >= i, but not those that are deeper.
118 This convention makes most operations easier. For example iadd works
119 even when the stack contains (reg[0], reg[1]): It results in the
120 stack containing (reg[0]+reg[1]), which is OK. However, some stack
121 operations are more complicated. For example dup given a stack
122 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
123 the convention, since stack value 1 would refer to a register with
124 lower index (reg[0]), which flush_quick_stack does not safely handle.
125 So dup cannot just add an extra element to the quick_stack, but iadd can.
128 static GTY(()) vec
<tree
, va_gc
> *quick_stack
;
130 /* The physical memory page size used in this computer. See
131 build_field_ref(). */
132 static GTY(()) tree page_size
;
134 /* The stack pointer of the Java virtual machine.
135 This does include the size of the quick_stack. */
139 const unsigned char *linenumber_table
;
140 int linenumber_count
;
142 /* Largest pc so far in this method that has been passed to lookup_label. */
143 int highest_label_pc_this_method
= -1;
145 /* Base value for this method to add to pc to get generated label. */
146 int start_label_pc_this_method
= 0;
149 init_expr_processing (void)
151 operand_type
[21] = operand_type
[54] = int_type_node
;
152 operand_type
[22] = operand_type
[55] = long_type_node
;
153 operand_type
[23] = operand_type
[56] = float_type_node
;
154 operand_type
[24] = operand_type
[57] = double_type_node
;
155 operand_type
[25] = operand_type
[58] = ptr_type_node
;
159 java_truthvalue_conversion (tree expr
)
161 /* It is simpler and generates better code to have only TRUTH_*_EXPR
162 or comparison expressions as truth values at this level.
164 This function should normally be identity for Java. */
166 switch (TREE_CODE (expr
))
168 case EQ_EXPR
: case NE_EXPR
: case UNEQ_EXPR
: case LTGT_EXPR
:
169 case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
170 case UNLE_EXPR
: case UNGE_EXPR
: case UNLT_EXPR
: case UNGT_EXPR
:
171 case ORDERED_EXPR
: case UNORDERED_EXPR
:
172 case TRUTH_ANDIF_EXPR
:
173 case TRUTH_ORIF_EXPR
:
182 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
185 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
187 /* are these legal? XXX JH */
191 /* These don't change whether an object is nonzero or zero. */
192 return java_truthvalue_conversion (TREE_OPERAND (expr
, 0));
195 /* Distribute the conversion into the arms of a COND_EXPR. */
196 return fold_build3 (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
197 java_truthvalue_conversion (TREE_OPERAND (expr
, 1)),
198 java_truthvalue_conversion (TREE_OPERAND (expr
, 2)));
201 /* If this is widening the argument, we can ignore it. */
202 if (TYPE_PRECISION (TREE_TYPE (expr
))
203 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
204 return java_truthvalue_conversion (TREE_OPERAND (expr
, 0));
205 /* fall through to default */
208 return fold_build2 (NE_EXPR
, boolean_type_node
,
209 expr
, boolean_false_node
);
213 /* Save any stack slots that happen to be in the quick_stack into their
214 home virtual register slots.
216 The copy order is from low stack index to high, to support the invariant
217 that the expression for a slot may contain decls for stack slots with
218 higher (or the same) index, but not lower. */
221 flush_quick_stack (void)
223 int stack_index
= stack_pointer
;
227 /* Count the number of slots the quick stack is holding. */
228 for (ix
= 0; vec_safe_iterate (quick_stack
, ix
, &t
); ix
++)
229 stack_index
-= 1 + TYPE_IS_WIDE (TREE_TYPE (t
));
231 for (ix
= 0; vec_safe_iterate (quick_stack
, ix
, &t
); ix
++)
233 tree decl
, type
= TREE_TYPE (t
);
235 decl
= find_stack_slot (stack_index
, type
);
237 java_add_stmt (build2 (MODIFY_EXPR
, TREE_TYPE (t
), decl
, t
));
238 stack_index
+= 1 + TYPE_IS_WIDE (type
);
241 vec_safe_truncate (quick_stack
, 0);
244 /* Push TYPE on the type stack.
245 Return true on success, 0 on overflow. */
248 push_type_0 (tree type
)
251 type
= promote_type (type
);
252 n_words
= 1 + TYPE_IS_WIDE (type
);
253 if (stack_pointer
+ n_words
> DECL_MAX_STACK (current_function_decl
))
255 /* Allocate decl for this variable now, so we get a temporary that
256 survives the whole method. */
257 find_stack_slot (stack_pointer
, type
);
258 stack_type_map
[stack_pointer
++] = type
;
260 while (--n_words
>= 0)
261 stack_type_map
[stack_pointer
++] = TYPE_SECOND
;
266 push_type (tree type
)
268 int r
= push_type_0 (type
);
273 push_value (tree value
)
275 tree type
= TREE_TYPE (value
);
276 if (TYPE_PRECISION (type
) < 32 && INTEGRAL_TYPE_P (type
))
278 type
= promote_type (type
);
279 value
= convert (type
, value
);
282 vec_safe_push (quick_stack
, value
);
284 /* If the value has a side effect, then we need to evaluate it
285 whether or not the result is used. If the value ends up on the
286 quick stack and is then popped, this won't happen -- so we flush
287 the quick stack. It is safest to simply always flush, though,
288 since TREE_SIDE_EFFECTS doesn't capture COMPONENT_REF, and for
289 the latter we may need to strip conversions. */
290 flush_quick_stack ();
293 /* Pop a type from the type stack.
294 TYPE is the expected type. Return the actual type, which must be
296 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
299 pop_type_0 (tree type
, char **messagep
)
304 if (TREE_CODE (type
) == RECORD_TYPE
)
305 type
= promote_type (type
);
306 n_words
= 1 + TYPE_IS_WIDE (type
);
307 if (stack_pointer
< n_words
)
309 *messagep
= xstrdup ("stack underflow");
312 while (--n_words
> 0)
314 if (stack_type_map
[--stack_pointer
] != void_type_node
)
316 *messagep
= xstrdup ("Invalid multi-word value on type stack");
320 t
= stack_type_map
[--stack_pointer
];
321 if (type
== NULL_TREE
|| t
== type
)
323 if (TREE_CODE (t
) == TREE_LIST
)
327 tree tt
= TREE_PURPOSE (t
);
328 if (! can_widen_reference_to (tt
, type
))
338 if (INTEGRAL_TYPE_P (type
) && INTEGRAL_TYPE_P (t
)
339 && TYPE_PRECISION (type
) <= 32 && TYPE_PRECISION (t
) <= 32)
341 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (t
) == POINTER_TYPE
)
343 /* If the expected type we've been passed is object or ptr
344 (i.e. void*), the caller needs to know the real type. */
345 if (type
== ptr_type_node
|| type
== object_ptr_type_node
)
348 /* Since the verifier has already run, we know that any
349 types we see will be compatible. In BC mode, this fact
350 may be checked at runtime, but if that is so then we can
351 assume its truth here as well. So, we always succeed
352 here, with the expected type. */
356 if (! flag_verify_invocations
&& flag_indirect_dispatch
357 && t
== object_ptr_type_node
)
359 if (type
!= ptr_type_node
)
360 warning (0, "need to insert runtime check for %s",
361 xstrdup (lang_printable_name (type
, 0)));
365 /* lang_printable_name uses a static buffer, so we must save the result
366 from calling it the first time. */
369 char *temp
= xstrdup (lang_printable_name (type
, 0));
370 /* If the stack contains a multi-word type, keep popping the stack until
371 the real type is found. */
372 while (t
== void_type_node
)
373 t
= stack_type_map
[--stack_pointer
];
374 *messagep
= concat ("expected type '", temp
,
375 "' but stack contains '", lang_printable_name (t
, 0),
382 /* Pop a type from the type stack.
383 TYPE is the expected type. Return the actual type, which must be
384 convertible to TYPE, otherwise call error. */
389 char *message
= NULL
;
390 type
= pop_type_0 (type
, &message
);
393 error ("%s", message
);
400 /* Return true if two type assertions are equal. */
403 type_assertion_hasher::equal (type_assertion
*k1
, type_assertion
*k2
)
405 return (k1
->assertion_code
== k2
->assertion_code
406 && k1
->op1
== k2
->op1
407 && k1
->op2
== k2
->op2
);
410 /* Hash a type assertion. */
413 type_assertion_hasher::hash (type_assertion
*k_p
)
415 hashval_t hash
= iterative_hash (&k_p
->assertion_code
, sizeof
416 k_p
->assertion_code
, 0);
418 switch (k_p
->assertion_code
)
420 case JV_ASSERT_TYPES_COMPATIBLE
:
421 hash
= iterative_hash (&TYPE_UID (k_p
->op2
), sizeof TYPE_UID (k_p
->op2
),
425 case JV_ASSERT_IS_INSTANTIABLE
:
426 hash
= iterative_hash (&TYPE_UID (k_p
->op1
), sizeof TYPE_UID (k_p
->op1
),
430 case JV_ASSERT_END_OF_TABLE
:
440 /* Add an entry to the type assertion table for the given class.
441 KLASS is the class for which this assertion will be evaluated by the
442 runtime during loading/initialization.
443 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
444 OP1 and OP2 are the operands. The tree type of these arguments may be
445 specific to each assertion_code. */
448 add_type_assertion (tree klass
, int assertion_code
, tree op1
, tree op2
)
450 hash_table
<type_assertion_hasher
> *assertions_htab
;
452 type_assertion
**as_pp
;
454 assertions_htab
= TYPE_ASSERTIONS (klass
);
455 if (assertions_htab
== NULL
)
457 assertions_htab
= hash_table
<type_assertion_hasher
>::create_ggc (7);
458 TYPE_ASSERTIONS (current_class
) = assertions_htab
;
461 as
.assertion_code
= assertion_code
;
465 as_pp
= assertions_htab
->find_slot (&as
, INSERT
);
467 /* Don't add the same assertion twice. */
471 *as_pp
= ggc_alloc
<type_assertion
> ();
476 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
477 Handles array types and interfaces. */
480 can_widen_reference_to (tree source_type
, tree target_type
)
482 if (source_type
== ptr_type_node
|| target_type
== object_ptr_type_node
)
485 /* Get rid of pointers */
486 if (TREE_CODE (source_type
) == POINTER_TYPE
)
487 source_type
= TREE_TYPE (source_type
);
488 if (TREE_CODE (target_type
) == POINTER_TYPE
)
489 target_type
= TREE_TYPE (target_type
);
491 if (source_type
== target_type
)
494 /* FIXME: This is very pessimistic, in that it checks everything,
495 even if we already know that the types are compatible. If we're
496 to support full Java class loader semantics, we need this.
497 However, we could do something more optimal. */
498 if (! flag_verify_invocations
)
500 add_type_assertion (current_class
, JV_ASSERT_TYPES_COMPATIBLE
,
501 source_type
, target_type
);
504 warning (0, "assert: %s is assign compatible with %s",
505 xstrdup (lang_printable_name (target_type
, 0)),
506 xstrdup (lang_printable_name (source_type
, 0)));
507 /* Punt everything to runtime. */
511 if (TYPE_DUMMY (source_type
) || TYPE_DUMMY (target_type
))
517 if (TYPE_ARRAY_P (source_type
) || TYPE_ARRAY_P (target_type
))
519 HOST_WIDE_INT source_length
, target_length
;
520 if (TYPE_ARRAY_P (source_type
) != TYPE_ARRAY_P (target_type
))
522 /* An array implements Cloneable and Serializable. */
523 tree name
= DECL_NAME (TYPE_NAME (target_type
));
524 return (name
== java_lang_cloneable_identifier_node
525 || name
== java_io_serializable_identifier_node
);
527 target_length
= java_array_type_length (target_type
);
528 if (target_length
>= 0)
530 source_length
= java_array_type_length (source_type
);
531 if (source_length
!= target_length
)
534 source_type
= TYPE_ARRAY_ELEMENT (source_type
);
535 target_type
= TYPE_ARRAY_ELEMENT (target_type
);
536 if (source_type
== target_type
)
538 if (TREE_CODE (source_type
) != POINTER_TYPE
539 || TREE_CODE (target_type
) != POINTER_TYPE
)
541 return can_widen_reference_to (source_type
, target_type
);
545 int source_depth
= class_depth (source_type
);
546 int target_depth
= class_depth (target_type
);
548 if (TYPE_DUMMY (source_type
) || TYPE_DUMMY (target_type
))
551 warning (0, "assert: %s is assign compatible with %s",
552 xstrdup (lang_printable_name (target_type
, 0)),
553 xstrdup (lang_printable_name (source_type
, 0)));
557 /* class_depth can return a negative depth if an error occurred */
558 if (source_depth
< 0 || target_depth
< 0)
561 if (CLASS_INTERFACE (TYPE_NAME (target_type
)))
563 /* target_type is OK if source_type or source_type ancestors
564 implement target_type. We handle multiple sub-interfaces */
565 tree binfo
, base_binfo
;
568 for (binfo
= TYPE_BINFO (source_type
), i
= 0;
569 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
570 if (can_widen_reference_to
571 (BINFO_TYPE (base_binfo
), target_type
))
578 for ( ; source_depth
> target_depth
; source_depth
--)
581 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type
), 0));
583 return source_type
== target_type
;
589 pop_value (tree type
)
591 type
= pop_type (type
);
592 if (vec_safe_length (quick_stack
) != 0)
593 return quick_stack
->pop ();
595 return find_stack_slot (stack_pointer
, promote_type (type
));
599 /* Pop and discard the top COUNT stack slots. */
602 java_stack_pop (int count
)
608 gcc_assert (stack_pointer
!= 0);
610 type
= stack_type_map
[stack_pointer
- 1];
611 if (type
== TYPE_SECOND
)
614 gcc_assert (stack_pointer
!= 1 && count
> 0);
616 type
= stack_type_map
[stack_pointer
- 2];
623 /* Implement the 'swap' operator (to swap two top stack slots). */
626 java_stack_swap (void)
632 if (stack_pointer
< 2
633 || (type1
= stack_type_map
[stack_pointer
- 1]) == TYPE_SECOND
634 || (type2
= stack_type_map
[stack_pointer
- 2]) == TYPE_SECOND
635 || TYPE_IS_WIDE (type1
) || TYPE_IS_WIDE (type2
))
636 /* Bad stack swap. */
638 /* Bad stack swap. */
640 flush_quick_stack ();
641 decl1
= find_stack_slot (stack_pointer
- 1, type1
);
642 decl2
= find_stack_slot (stack_pointer
- 2, type2
);
643 temp
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, type1
);
644 java_add_local_var (temp
);
645 java_add_stmt (build2 (MODIFY_EXPR
, type1
, temp
, decl1
));
646 java_add_stmt (build2 (MODIFY_EXPR
, type2
,
647 find_stack_slot (stack_pointer
- 1, type2
),
649 java_add_stmt (build2 (MODIFY_EXPR
, type1
,
650 find_stack_slot (stack_pointer
- 2, type1
),
652 stack_type_map
[stack_pointer
- 1] = type2
;
653 stack_type_map
[stack_pointer
- 2] = type1
;
657 java_stack_dup (int size
, int offset
)
659 int low_index
= stack_pointer
- size
- offset
;
662 error ("stack underflow - dup* operation");
664 flush_quick_stack ();
666 stack_pointer
+= size
;
667 dst_index
= stack_pointer
;
669 for (dst_index
= stack_pointer
; --dst_index
>= low_index
; )
672 int src_index
= dst_index
- size
;
673 if (src_index
< low_index
)
674 src_index
= dst_index
+ size
+ offset
;
675 type
= stack_type_map
[src_index
];
676 if (type
== TYPE_SECOND
)
678 /* Dup operation splits 64-bit number. */
679 gcc_assert (src_index
> low_index
);
681 stack_type_map
[dst_index
] = type
;
682 src_index
--; dst_index
--;
683 type
= stack_type_map
[src_index
];
684 gcc_assert (TYPE_IS_WIDE (type
));
687 gcc_assert (! TYPE_IS_WIDE (type
));
689 if (src_index
!= dst_index
)
691 tree src_decl
= find_stack_slot (src_index
, type
);
692 tree dst_decl
= find_stack_slot (dst_index
, type
);
695 (build2 (MODIFY_EXPR
, TREE_TYPE (dst_decl
), dst_decl
, src_decl
));
696 stack_type_map
[dst_index
] = type
;
701 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
705 build_java_athrow (tree node
)
709 call
= build_call_nary (void_type_node
,
710 build_address_of (throw_node
),
712 TREE_SIDE_EFFECTS (call
) = 1;
713 java_add_stmt (call
);
714 java_stack_pop (stack_pointer
);
717 /* Implementation for jsr/ret */
720 build_java_jsr (int target_pc
, int return_pc
)
722 tree where
= lookup_label (target_pc
);
723 tree ret
= lookup_label (return_pc
);
724 tree ret_label
= fold_build1 (ADDR_EXPR
, return_address_type_node
, ret
);
725 push_value (ret_label
);
726 flush_quick_stack ();
727 java_add_stmt (build1 (GOTO_EXPR
, void_type_node
, where
));
729 /* Do not need to emit the label here. We noted the existence of the
730 label as a jump target in note_instructions; we'll emit the label
731 for real at the beginning of the expand_byte_code loop. */
735 build_java_ret (tree location
)
737 java_add_stmt (build1 (GOTO_EXPR
, void_type_node
, location
));
740 /* Implementation of operations on array: new, load, store, length */
743 decode_newarray_type (int atype
)
747 case 4: return boolean_type_node
;
748 case 5: return char_type_node
;
749 case 6: return float_type_node
;
750 case 7: return double_type_node
;
751 case 8: return byte_type_node
;
752 case 9: return short_type_node
;
753 case 10: return int_type_node
;
754 case 11: return long_type_node
;
755 default: return NULL_TREE
;
759 /* Map primitive type to the code used by OPCODE_newarray. */
762 encode_newarray_type (tree type
)
764 if (type
== boolean_type_node
)
766 else if (type
== char_type_node
)
768 else if (type
== float_type_node
)
770 else if (type
== double_type_node
)
772 else if (type
== byte_type_node
)
774 else if (type
== short_type_node
)
776 else if (type
== int_type_node
)
778 else if (type
== long_type_node
)
784 /* Build a call to _Jv_ThrowBadArrayIndex(), the
785 ArrayIndexOfBoundsException exception handler. */
788 build_java_throw_out_of_bounds_exception (tree index
)
792 /* We need to build a COMPOUND_EXPR because _Jv_ThrowBadArrayIndex()
793 has void return type. We cannot just set the type of the CALL_EXPR below
794 to int_type_node because we would lose it during gimplification. */
795 gcc_assert (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (soft_badarrayindex_node
))));
796 node
= build_call_nary (void_type_node
,
797 build_address_of (soft_badarrayindex_node
),
799 TREE_SIDE_EFFECTS (node
) = 1;
801 node
= build2 (COMPOUND_EXPR
, int_type_node
, node
, integer_zero_node
);
802 TREE_SIDE_EFFECTS (node
) = 1; /* Allows expansion within ANDIF */
807 /* Return the length of an array. Doesn't perform any checking on the nature
808 or value of the array NODE. May be used to implement some bytecodes. */
811 build_java_array_length_access (tree node
)
813 tree type
= TREE_TYPE (node
);
814 tree array_type
= TREE_TYPE (type
);
815 HOST_WIDE_INT length
;
817 if (!is_array_type_p (type
))
819 /* With the new verifier, we will see an ordinary pointer type
820 here. In this case, we just use an arbitrary array type. */
821 array_type
= build_java_array_type (object_ptr_type_node
, -1);
822 type
= promote_type (array_type
);
825 length
= java_array_type_length (type
);
827 return build_int_cst (NULL_TREE
, length
);
829 node
= build3 (COMPONENT_REF
, int_type_node
,
830 build_java_indirect_ref (array_type
, node
,
831 flag_check_references
),
832 lookup_field (&array_type
, get_identifier ("length")),
834 IS_ARRAY_LENGTH_ACCESS (node
) = 1;
838 /* Optionally checks a reference against the NULL pointer. ARG1: the
839 expr, ARG2: we should check the reference. Don't generate extra
840 checks if we're not generating code. */
843 java_check_reference (tree expr
, int check
)
845 if (!flag_syntax_only
&& check
)
847 expr
= save_expr (expr
);
848 expr
= build3 (COND_EXPR
, TREE_TYPE (expr
),
849 build2 (EQ_EXPR
, boolean_type_node
,
850 expr
, null_pointer_node
),
851 build_call_nary (void_type_node
,
852 build_address_of (soft_nullpointer_node
),
860 /* Reference an object: just like an INDIRECT_REF, but with checking. */
863 build_java_indirect_ref (tree type
, tree expr
, int check
)
866 t
= java_check_reference (expr
, check
);
867 t
= convert (build_pointer_type (type
), t
);
868 return build1 (INDIRECT_REF
, type
, t
);
871 /* Implement array indexing (either as l-value or r-value).
872 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
873 Optionally performs bounds checking and/or test to NULL.
874 At this point, ARRAY should have been verified as an array. */
877 build_java_arrayaccess (tree array
, tree type
, tree index
)
879 tree node
, throw_expr
= NULL_TREE
;
882 tree array_type
= TREE_TYPE (TREE_TYPE (array
));
883 tree size_exp
= fold_convert (sizetype
, size_in_bytes (type
));
885 if (!is_array_type_p (TREE_TYPE (array
)))
887 /* With the new verifier, we will see an ordinary pointer type
888 here. In this case, we just use the correct array type. */
889 array_type
= build_java_array_type (type
, -1);
892 if (flag_bounds_check
)
895 * (unsigned jint) INDEX >= (unsigned jint) LEN
896 * && throw ArrayIndexOutOfBoundsException.
897 * Note this is equivalent to and more efficient than:
898 * INDEX < 0 || INDEX >= LEN && throw ... */
900 tree len
= convert (unsigned_int_type_node
,
901 build_java_array_length_access (array
));
902 test
= fold_build2 (GE_EXPR
, boolean_type_node
,
903 convert (unsigned_int_type_node
, index
),
905 if (! integer_zerop (test
))
908 = build2 (TRUTH_ANDIF_EXPR
, int_type_node
, test
,
909 build_java_throw_out_of_bounds_exception (index
));
910 /* allows expansion within COMPOUND */
911 TREE_SIDE_EFFECTS( throw_expr
) = 1;
915 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
916 to have the bounds check evaluated first. */
917 if (throw_expr
!= NULL_TREE
)
918 index
= build2 (COMPOUND_EXPR
, int_type_node
, throw_expr
, index
);
920 data_field
= lookup_field (&array_type
, get_identifier ("data"));
922 ref
= build3 (COMPONENT_REF
, TREE_TYPE (data_field
),
923 build_java_indirect_ref (array_type
, array
,
924 flag_check_references
),
925 data_field
, NULL_TREE
);
927 /* Take the address of the data field and convert it to a pointer to
929 node
= build1 (NOP_EXPR
, build_pointer_type (type
), build_address_of (ref
));
931 /* Multiply the index by the size of an element to obtain a byte
932 offset. Convert the result to a pointer to the element type. */
933 index
= build2 (MULT_EXPR
, sizetype
,
934 fold_convert (sizetype
, index
),
937 /* Sum the byte offset and the address of the data field. */
938 node
= fold_build_pointer_plus (node
, index
);
942 *((&array->data) + index*size_exp)
945 return build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (node
)), node
);
948 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
949 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
950 determine that no check is required. */
953 build_java_arraystore_check (tree array
, tree object
)
955 tree check
, element_type
, source
;
956 tree array_type_p
= TREE_TYPE (array
);
957 tree object_type
= TYPE_NAME (TREE_TYPE (TREE_TYPE (object
)));
959 if (! flag_verify_invocations
)
961 /* With the new verifier, we don't track precise types. FIXME:
962 performance regression here. */
963 element_type
= TYPE_NAME (object_type_node
);
967 gcc_assert (is_array_type_p (array_type_p
));
969 /* Get the TYPE_DECL for ARRAY's element type. */
971 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p
))));
974 gcc_assert (TREE_CODE (element_type
) == TYPE_DECL
975 && TREE_CODE (object_type
) == TYPE_DECL
);
977 if (!flag_store_check
)
978 return build1 (NOP_EXPR
, array_type_p
, array
);
980 /* No check is needed if the element type is final. Also check that
981 element_type matches object_type, since in the bytecode
982 compilation case element_type may be the actual element type of
983 the array rather than its declared type. However, if we're doing
984 indirect dispatch, we can't do the `final' optimization. */
985 if (element_type
== object_type
986 && ! flag_indirect_dispatch
987 && CLASS_FINAL (element_type
))
988 return build1 (NOP_EXPR
, array_type_p
, array
);
990 /* OBJECT might be wrapped by a SAVE_EXPR. */
991 if (TREE_CODE (object
) == SAVE_EXPR
)
992 source
= TREE_OPERAND (object
, 0);
996 /* Avoid the check if OBJECT was just loaded from the same array. */
997 if (TREE_CODE (source
) == ARRAY_REF
)
1000 source
= TREE_OPERAND (source
, 0); /* COMPONENT_REF. */
1001 source
= TREE_OPERAND (source
, 0); /* INDIRECT_REF. */
1002 source
= TREE_OPERAND (source
, 0); /* Source array's DECL or SAVE_EXPR. */
1003 if (TREE_CODE (source
) == SAVE_EXPR
)
1004 source
= TREE_OPERAND (source
, 0);
1007 if (TREE_CODE (target
) == SAVE_EXPR
)
1008 target
= TREE_OPERAND (target
, 0);
1010 if (source
== target
)
1011 return build1 (NOP_EXPR
, array_type_p
, array
);
1014 /* Build an invocation of _Jv_CheckArrayStore */
1015 check
= build_call_nary (void_type_node
,
1016 build_address_of (soft_checkarraystore_node
),
1018 TREE_SIDE_EFFECTS (check
) = 1;
1023 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1024 ARRAY_NODE. This function is used to retrieve something less vague than
1025 a pointer type when indexing the first dimension of something like [[<t>.
1026 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1027 return unchanged. */
1030 build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED
,
1033 /* We used to check to see if ARRAY_NODE really had array type.
1034 However, with the new verifier, this is not necessary, as we know
1035 that the object will be an array of the appropriate type. */
1037 return indexed_type
;
1040 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1041 called with an integer code (the type of array to create), and the length
1042 of the array to create. */
1045 build_newarray (int atype_value
, tree length
)
1049 tree prim_type
= decode_newarray_type (atype_value
);
1051 = build_java_array_type (prim_type
,
1052 tree_fits_shwi_p (length
)
1053 ? tree_to_shwi (length
) : -1);
1055 /* Pass a reference to the primitive type class and save the runtime
1057 type_arg
= build_class_ref (prim_type
);
1059 return build_call_nary (promote_type (type
),
1060 build_address_of (soft_newarray_node
),
1061 2, type_arg
, length
);
1064 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1065 of the dimension. */
1068 build_anewarray (tree class_type
, tree length
)
1071 = build_java_array_type (class_type
,
1072 tree_fits_shwi_p (length
)
1073 ? tree_to_shwi (length
) : -1);
1075 return build_call_nary (promote_type (type
),
1076 build_address_of (soft_anewarray_node
),
1079 build_class_ref (class_type
),
1083 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1086 build_new_array (tree type
, tree length
)
1088 if (JPRIMITIVE_TYPE_P (type
))
1089 return build_newarray (encode_newarray_type (type
), length
);
1091 return build_anewarray (TREE_TYPE (type
), length
);
1094 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1095 class pointer, a number of dimensions and the matching number of
1096 dimensions. The argument list is NULL terminated. */
1099 expand_java_multianewarray (tree class_type
, int ndim
)
1102 vec
<tree
, va_gc
> *args
= NULL
;
1104 vec_safe_grow (args
, 3 + ndim
);
1106 (*args
)[0] = build_class_ref (class_type
);
1107 (*args
)[1] = build_int_cst (NULL_TREE
, ndim
);
1109 for(i
= ndim
- 1; i
>= 0; i
-- )
1110 (*args
)[(unsigned)(2 + i
)] = pop_value (int_type_node
);
1112 (*args
)[2 + ndim
] = null_pointer_node
;
1114 push_value (build_call_vec (promote_type (class_type
),
1115 build_address_of (soft_multianewarray_node
),
1119 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1120 ARRAY is an array type. May expand some bound checking and NULL
1121 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1122 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1123 INT. In those cases, we make the conversion.
1125 if ARRAy is a reference type, the assignment is checked at run-time
1126 to make sure that the RHS can be assigned to the array element
1127 type. It is not necessary to generate this code if ARRAY is final. */
1130 expand_java_arraystore (tree rhs_type_node
)
1132 tree rhs_node
= pop_value ((INTEGRAL_TYPE_P (rhs_type_node
)
1133 && TYPE_PRECISION (rhs_type_node
) <= 32) ?
1134 int_type_node
: rhs_type_node
);
1135 tree index
= pop_value (int_type_node
);
1136 tree array_type
, array
, temp
, access
;
1138 /* If we're processing an `aaload' we might as well just pick
1140 if (TREE_CODE (rhs_type_node
) == POINTER_TYPE
)
1142 array_type
= build_java_array_type (object_ptr_type_node
, -1);
1143 rhs_type_node
= object_ptr_type_node
;
1146 array_type
= build_java_array_type (rhs_type_node
, -1);
1148 array
= pop_value (array_type
);
1149 array
= build1 (NOP_EXPR
, promote_type (array_type
), array
);
1151 rhs_type_node
= build_java_check_indexed_type (array
, rhs_type_node
);
1153 flush_quick_stack ();
1155 index
= save_expr (index
);
1156 array
= save_expr (array
);
1158 /* We want to perform the bounds check (done by
1159 build_java_arrayaccess) before the type check (done by
1160 build_java_arraystore_check). So, we call build_java_arrayaccess
1161 -- which returns an ARRAY_REF lvalue -- and we then generate code
1162 to stash the address of that lvalue in a temp. Then we call
1163 build_java_arraystore_check, and finally we generate a
1164 MODIFY_EXPR to set the array element. */
1166 access
= build_java_arrayaccess (array
, rhs_type_node
, index
);
1167 temp
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
1168 build_pointer_type (TREE_TYPE (access
)));
1169 java_add_local_var (temp
);
1170 java_add_stmt (build2 (MODIFY_EXPR
, TREE_TYPE (temp
),
1172 build_fold_addr_expr (access
)));
1174 if (TREE_CODE (rhs_type_node
) == POINTER_TYPE
)
1176 tree check
= build_java_arraystore_check (array
, rhs_node
);
1177 java_add_stmt (check
);
1180 java_add_stmt (build2 (MODIFY_EXPR
, TREE_TYPE (access
),
1181 build1 (INDIRECT_REF
, TREE_TYPE (access
), temp
),
1185 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1186 sure that LHS is an array type. May expand some bound checking and NULL
1188 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1189 BOOLEAN/SHORT, we push a promoted type back to the stack.
1193 expand_java_arrayload (tree lhs_type_node
)
1196 tree index_node
= pop_value (int_type_node
);
1200 /* If we're processing an `aaload' we might as well just pick
1202 if (TREE_CODE (lhs_type_node
) == POINTER_TYPE
)
1204 array_type
= build_java_array_type (object_ptr_type_node
, -1);
1205 lhs_type_node
= object_ptr_type_node
;
1208 array_type
= build_java_array_type (lhs_type_node
, -1);
1209 array_node
= pop_value (array_type
);
1210 array_node
= build1 (NOP_EXPR
, promote_type (array_type
), array_node
);
1212 index_node
= save_expr (index_node
);
1213 array_node
= save_expr (array_node
);
1215 lhs_type_node
= build_java_check_indexed_type (array_node
,
1217 load_node
= build_java_arrayaccess (array_node
,
1220 if (INTEGRAL_TYPE_P (lhs_type_node
) && TYPE_PRECISION (lhs_type_node
) <= 32)
1221 load_node
= fold_build1 (NOP_EXPR
, int_type_node
, load_node
);
1222 push_value (load_node
);
1225 /* Expands .length. Makes sure that we deal with and array and may expand
1226 a NULL check on the array object. */
1229 expand_java_array_length (void)
1231 tree array
= pop_value (ptr_type_node
);
1232 tree length
= build_java_array_length_access (array
);
1234 push_value (length
);
1237 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1238 either soft_monitorenter_node or soft_monitorexit_node. */
1241 build_java_monitor (tree call
, tree object
)
1243 return build_call_nary (void_type_node
,
1244 build_address_of (call
),
1248 /* Emit code for one of the PUSHC instructions. */
1251 expand_java_pushc (int ival
, tree type
)
1254 if (type
== ptr_type_node
&& ival
== 0)
1255 value
= null_pointer_node
;
1256 else if (type
== int_type_node
|| type
== long_type_node
)
1257 value
= build_int_cst (type
, ival
);
1258 else if (type
== float_type_node
|| type
== double_type_node
)
1261 real_from_integer (&x
, TYPE_MODE (type
), ival
, SIGNED
);
1262 value
= build_real (type
, x
);
1271 expand_java_return (tree type
)
1273 if (type
== void_type_node
)
1274 java_add_stmt (build1 (RETURN_EXPR
, void_type_node
, NULL
));
1277 tree retval
= pop_value (type
);
1278 tree res
= DECL_RESULT (current_function_decl
);
1279 retval
= build2 (MODIFY_EXPR
, TREE_TYPE (res
), res
, retval
);
1281 /* Handle the situation where the native integer type is smaller
1282 than the JVM integer. It can happen for many cross compilers.
1283 The whole if expression just goes away if INT_TYPE_SIZE < 32
1285 if (INT_TYPE_SIZE
< 32
1286 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res
)))
1287 < GET_MODE_SIZE (TYPE_MODE (type
))))
1288 retval
= build1(NOP_EXPR
, TREE_TYPE(res
), retval
);
1290 TREE_SIDE_EFFECTS (retval
) = 1;
1291 java_add_stmt (build1 (RETURN_EXPR
, void_type_node
, retval
));
1296 expand_load_internal (int index
, tree type
, int pc
)
1299 tree var
= find_local_variable (index
, type
, pc
);
1301 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1302 on the stack. If there is an assignment to this VAR_DECL between
1303 the stack push and the use, then the wrong code could be
1304 generated. To avoid this we create a new local and copy our
1305 value into it. Then we push this new local on the stack.
1306 Hopefully this all gets optimized out. */
1307 copy
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, type
);
1308 if ((INTEGRAL_TYPE_P (type
) || POINTER_TYPE_P (type
))
1309 && TREE_TYPE (copy
) != TREE_TYPE (var
))
1310 var
= convert (type
, var
);
1311 java_add_local_var (copy
);
1312 java_add_stmt (build2 (MODIFY_EXPR
, TREE_TYPE (var
), copy
, var
));
1318 build_address_of (tree value
)
1320 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (value
)), value
);
1324 class_has_finalize_method (tree type
)
1326 tree super
= CLASSTYPE_SUPER (type
);
1328 if (super
== NULL_TREE
)
1329 return false; /* Every class with a real finalizer inherits */
1330 /* from java.lang.Object. */
1332 return HAS_FINALIZER_P (type
) || class_has_finalize_method (super
);
1336 java_create_object (tree type
)
1338 tree alloc_node
= (class_has_finalize_method (type
)
1340 : alloc_no_finalizer_node
);
1342 return build_call_nary (promote_type (type
),
1343 build_address_of (alloc_node
),
1344 1, build_class_ref (type
));
1348 expand_java_NEW (tree type
)
1352 alloc_node
= (class_has_finalize_method (type
) ? alloc_object_node
1353 : alloc_no_finalizer_node
);
1354 if (! CLASS_LOADED_P (type
))
1355 load_class (type
, 1);
1356 safe_layout_class (type
);
1357 push_value (build_call_nary (promote_type (type
),
1358 build_address_of (alloc_node
),
1359 1, build_class_ref (type
)));
1362 /* This returns an expression which will extract the class of an
1366 build_get_class (tree value
)
1368 tree class_field
= lookup_field (&dtable_type
, get_identifier ("class"));
1369 tree vtable_field
= lookup_field (&object_type_node
,
1370 get_identifier ("vtable"));
1371 tree tmp
= build3 (COMPONENT_REF
, dtable_ptr_type
,
1372 build_java_indirect_ref (object_type_node
, value
,
1373 flag_check_references
),
1374 vtable_field
, NULL_TREE
);
1375 return build3 (COMPONENT_REF
, class_ptr_type
,
1376 build1 (INDIRECT_REF
, dtable_type
, tmp
),
1377 class_field
, NULL_TREE
);
1380 /* This builds the tree representation of the `instanceof' operator.
1381 It tries various tricks to optimize this in cases where types are
1385 build_instanceof (tree value
, tree type
)
1388 tree itype
= TREE_TYPE (TREE_TYPE (soft_instanceof_node
));
1389 tree valtype
= TREE_TYPE (TREE_TYPE (value
));
1390 tree valclass
= TYPE_NAME (valtype
);
1393 /* When compiling from bytecode, we need to ensure that TYPE has
1395 if (CLASS_P (type
) && ! CLASS_LOADED_P (type
))
1397 load_class (type
, 1);
1398 safe_layout_class (type
);
1399 if (! TYPE_SIZE (type
) || TREE_CODE (TYPE_SIZE (type
)) == ERROR_MARK
)
1400 return error_mark_node
;
1402 klass
= TYPE_NAME (type
);
1404 if (type
== object_type_node
|| inherits_from_p (valtype
, type
))
1406 /* Anything except `null' is an instance of Object. Likewise,
1407 if the object is known to be an instance of the class, then
1408 we only need to check for `null'. */
1409 expr
= build2 (NE_EXPR
, itype
, value
, null_pointer_node
);
1411 else if (flag_verify_invocations
1412 && ! TYPE_ARRAY_P (type
)
1413 && ! TYPE_ARRAY_P (valtype
)
1414 && DECL_P (klass
) && DECL_P (valclass
)
1415 && ! CLASS_INTERFACE (valclass
)
1416 && ! CLASS_INTERFACE (klass
)
1417 && ! inherits_from_p (type
, valtype
)
1418 && (CLASS_FINAL (klass
)
1419 || ! inherits_from_p (valtype
, type
)))
1421 /* The classes are from different branches of the derivation
1422 tree, so we immediately know the answer. */
1423 expr
= boolean_false_node
;
1425 else if (DECL_P (klass
) && CLASS_FINAL (klass
))
1427 tree save
= save_expr (value
);
1428 expr
= build3 (COND_EXPR
, itype
,
1429 build2 (NE_EXPR
, boolean_type_node
,
1430 save
, null_pointer_node
),
1431 build2 (EQ_EXPR
, itype
,
1432 build_get_class (save
),
1433 build_class_ref (type
)),
1434 boolean_false_node
);
1438 expr
= build_call_nary (itype
,
1439 build_address_of (soft_instanceof_node
),
1440 2, value
, build_class_ref (type
));
1442 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (value
);
1447 expand_java_INSTANCEOF (tree type
)
1449 tree value
= pop_value (object_ptr_type_node
);
1450 value
= build_instanceof (value
, type
);
1455 expand_java_CHECKCAST (tree type
)
1457 tree value
= pop_value (ptr_type_node
);
1458 value
= build_call_nary (promote_type (type
),
1459 build_address_of (soft_checkcast_node
),
1460 2, build_class_ref (type
), value
);
1465 expand_iinc (unsigned int local_var_index
, int ival
, int pc
)
1467 tree local_var
, res
;
1468 tree constant_value
;
1470 flush_quick_stack ();
1471 local_var
= find_local_variable (local_var_index
, int_type_node
, pc
);
1472 constant_value
= build_int_cst (NULL_TREE
, ival
);
1473 res
= fold_build2 (PLUS_EXPR
, int_type_node
, local_var
, constant_value
);
1474 java_add_stmt (build2 (MODIFY_EXPR
, TREE_TYPE (local_var
), local_var
, res
));
1479 build_java_soft_divmod (enum tree_code op
, tree type
, tree op1
, tree op2
)
1482 tree arg1
= convert (type
, op1
);
1483 tree arg2
= convert (type
, op2
);
1485 if (type
== int_type_node
)
1489 case TRUNC_DIV_EXPR
:
1490 call
= soft_idiv_node
;
1492 case TRUNC_MOD_EXPR
:
1493 call
= soft_irem_node
;
1499 else if (type
== long_type_node
)
1503 case TRUNC_DIV_EXPR
:
1504 call
= soft_ldiv_node
;
1506 case TRUNC_MOD_EXPR
:
1507 call
= soft_lrem_node
;
1515 call
= build_call_nary (type
, build_address_of (call
), 2, arg1
, arg2
);
1520 build_java_binop (enum tree_code op
, tree type
, tree arg1
, tree arg2
)
1527 tree u_type
= unsigned_type_for (type
);
1528 arg1
= convert (u_type
, arg1
);
1529 arg1
= build_java_binop (RSHIFT_EXPR
, u_type
, arg1
, arg2
);
1530 return convert (type
, arg1
);
1534 mask
= build_int_cst (int_type_node
,
1535 TYPE_PRECISION (TREE_TYPE (arg1
)) - 1);
1536 arg2
= fold_build2 (BIT_AND_EXPR
, int_type_node
, arg2
, mask
);
1539 case COMPARE_L_EXPR
: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1540 case COMPARE_G_EXPR
: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1541 arg1
= save_expr (arg1
); arg2
= save_expr (arg2
);
1543 tree ifexp1
= fold_build2 (op
== COMPARE_L_EXPR
? GT_EXPR
: LT_EXPR
,
1544 boolean_type_node
, arg1
, arg2
);
1545 tree ifexp2
= fold_build2 (EQ_EXPR
, boolean_type_node
, arg1
, arg2
);
1546 tree second_compare
= fold_build3 (COND_EXPR
, int_type_node
,
1547 ifexp2
, integer_zero_node
,
1548 op
== COMPARE_L_EXPR
1549 ? integer_minus_one_node
1550 : integer_one_node
);
1551 return fold_build3 (COND_EXPR
, int_type_node
, ifexp1
,
1552 op
== COMPARE_L_EXPR
? integer_one_node
1553 : integer_minus_one_node
,
1557 arg1
= save_expr (arg1
); arg2
= save_expr (arg2
);
1559 tree ifexp1
= fold_build2 (LT_EXPR
, boolean_type_node
, arg1
, arg2
);
1560 tree ifexp2
= fold_build2 (GT_EXPR
, boolean_type_node
, arg1
, arg2
);
1561 tree second_compare
= fold_build3 (COND_EXPR
, int_type_node
,
1562 ifexp2
, integer_one_node
,
1564 return fold_build3 (COND_EXPR
, int_type_node
,
1565 ifexp1
, integer_minus_one_node
, second_compare
);
1567 case TRUNC_DIV_EXPR
:
1568 case TRUNC_MOD_EXPR
:
1569 if (TREE_CODE (type
) == REAL_TYPE
1570 && op
== TRUNC_MOD_EXPR
)
1573 if (type
!= double_type_node
)
1575 arg1
= convert (double_type_node
, arg1
);
1576 arg2
= convert (double_type_node
, arg2
);
1578 call
= build_call_nary (double_type_node
,
1579 build_address_of (soft_fmod_node
),
1581 if (type
!= double_type_node
)
1582 call
= convert (type
, call
);
1586 if (TREE_CODE (type
) == INTEGER_TYPE
1587 && flag_use_divide_subroutine
1588 && ! flag_syntax_only
)
1589 return build_java_soft_divmod (op
, type
, arg1
, arg2
);
1594 return fold_build2 (op
, type
, arg1
, arg2
);
1598 expand_java_binop (tree type
, enum tree_code op
)
1608 rtype
= int_type_node
;
1609 rarg
= pop_value (rtype
);
1612 rarg
= pop_value (rtype
);
1614 larg
= pop_value (ltype
);
1615 push_value (build_java_binop (op
, type
, larg
, rarg
));
1618 /* Lookup the field named NAME in *TYPEP or its super classes.
1619 If not found, return NULL_TREE.
1620 (If the *TYPEP is not found, or if the field reference is
1621 ambiguous, return error_mark_node.)
1622 If found, return the FIELD_DECL, and set *TYPEP to the
1623 class containing the field. */
1626 lookup_field (tree
*typep
, tree name
)
1628 if (CLASS_P (*typep
) && !CLASS_LOADED_P (*typep
))
1630 load_class (*typep
, 1);
1631 safe_layout_class (*typep
);
1632 if (!TYPE_SIZE (*typep
) || TREE_CODE (TYPE_SIZE (*typep
)) == ERROR_MARK
)
1633 return error_mark_node
;
1637 tree field
, binfo
, base_binfo
;
1641 for (field
= TYPE_FIELDS (*typep
); field
; field
= DECL_CHAIN (field
))
1642 if (DECL_NAME (field
) == name
)
1645 /* Process implemented interfaces. */
1646 save_field
= NULL_TREE
;
1647 for (binfo
= TYPE_BINFO (*typep
), i
= 0;
1648 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1650 tree t
= BINFO_TYPE (base_binfo
);
1651 if ((field
= lookup_field (&t
, name
)))
1653 if (save_field
== field
)
1655 if (save_field
== NULL_TREE
)
1659 tree i1
= DECL_CONTEXT (save_field
);
1660 tree i2
= DECL_CONTEXT (field
);
1661 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1662 IDENTIFIER_POINTER (name
),
1663 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1
))),
1664 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2
))));
1665 return error_mark_node
;
1670 if (save_field
!= NULL_TREE
)
1673 *typep
= CLASSTYPE_SUPER (*typep
);
1678 /* Look up the field named NAME in object SELF_VALUE,
1679 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1680 SELF_VALUE is NULL_TREE if looking for a static field. */
1683 build_field_ref (tree self_value
, tree self_class
, tree name
)
1685 tree base_class
= self_class
;
1686 tree field_decl
= lookup_field (&base_class
, name
);
1687 if (field_decl
== NULL_TREE
)
1689 error ("field %qs not found", IDENTIFIER_POINTER (name
));
1690 return error_mark_node
;
1692 if (self_value
== NULL_TREE
)
1694 return build_static_field_ref (field_decl
);
1698 tree base_type
= promote_type (base_class
);
1700 /* CHECK is true if self_value is not the this pointer. */
1701 int check
= (! (DECL_P (self_value
)
1702 && DECL_NAME (self_value
) == this_identifier_node
));
1704 /* Determine whether a field offset from NULL will lie within
1705 Page 0: this is necessary on those GNU/Linux/BSD systems that
1706 trap SEGV to generate NullPointerExceptions.
1708 We assume that Page 0 will be mapped with NOPERM, and that
1709 memory may be allocated from any other page, so only field
1710 offsets < pagesize are guaranteed to trap. We also assume
1711 the smallest page size we'll encounter is 4k bytes. */
1712 if (! flag_syntax_only
&& check
&& ! flag_check_references
1713 && ! flag_indirect_dispatch
)
1715 tree field_offset
= byte_position (field_decl
);
1717 page_size
= size_int (4096);
1718 check
= !tree_int_cst_lt (field_offset
, page_size
);
1721 if (base_type
!= TREE_TYPE (self_value
))
1722 self_value
= fold_build1 (NOP_EXPR
, base_type
, self_value
);
1723 if (! flag_syntax_only
&& flag_indirect_dispatch
)
1726 = build_int_cst (NULL_TREE
, get_symbol_table_index
1727 (field_decl
, NULL_TREE
,
1728 &TYPE_OTABLE_METHODS (output_class
)));
1730 = build4 (ARRAY_REF
, integer_type_node
,
1731 TYPE_OTABLE_DECL (output_class
), otable_index
,
1732 NULL_TREE
, NULL_TREE
);
1735 if (DECL_CONTEXT (field_decl
) != output_class
)
1737 = build3 (COND_EXPR
, TREE_TYPE (field_offset
),
1738 build2 (EQ_EXPR
, boolean_type_node
,
1739 field_offset
, integer_zero_node
),
1740 build_call_nary (void_type_node
,
1741 build_address_of (soft_nosuchfield_node
),
1745 self_value
= java_check_reference (self_value
, check
);
1746 address
= fold_build_pointer_plus (self_value
, field_offset
);
1747 address
= fold_convert (build_pointer_type (TREE_TYPE (field_decl
)),
1749 return fold_build1 (INDIRECT_REF
, TREE_TYPE (field_decl
), address
);
1752 self_value
= build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value
)),
1754 return fold_build3 (COMPONENT_REF
, TREE_TYPE (field_decl
),
1755 self_value
, field_decl
, NULL_TREE
);
1760 lookup_label (int pc
)
1764 if (pc
> highest_label_pc_this_method
)
1765 highest_label_pc_this_method
= pc
;
1766 targetm
.asm_out
.generate_internal_label (buf
, "LJpc=",
1767 start_label_pc_this_method
+ pc
);
1768 name
= get_identifier (buf
);
1769 if (IDENTIFIER_LOCAL_VALUE (name
))
1770 return IDENTIFIER_LOCAL_VALUE (name
);
1773 /* The type of the address of a label is return_address_type_node. */
1774 tree decl
= create_label_decl (name
);
1775 return pushdecl (decl
);
1779 /* Generate a unique name for the purpose of loops and switches
1780 labels, and try-catch-finally blocks label or temporary variables. */
1783 generate_name (void)
1785 static int l_number
= 0;
1787 targetm
.asm_out
.generate_internal_label (buff
, "LJv", l_number
);
1789 return get_identifier (buff
);
1793 create_label_decl (tree name
)
1796 decl
= build_decl (input_location
, LABEL_DECL
, name
,
1797 TREE_TYPE (return_address_type_node
));
1798 DECL_CONTEXT (decl
) = current_function_decl
;
1799 DECL_IGNORED_P (decl
) = 1;
1803 /* This maps a bytecode offset (PC) to various flags. */
1804 char *instruction_bits
;
1806 /* This is a vector of type states for the current method. It is
1807 indexed by PC. Each element is a tree vector holding the type
1808 state at that PC. We only note type states at basic block
1810 vec
<tree
, va_gc
> *type_states
;
1813 note_label (int current_pc ATTRIBUTE_UNUSED
, int target_pc
)
1815 lookup_label (target_pc
);
1816 instruction_bits
[target_pc
] |= BCODE_JUMP_TARGET
;
1819 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1820 where CONDITION is one of one the compare operators. */
1823 expand_compare (enum tree_code condition
, tree value1
, tree value2
,
1826 tree target
= lookup_label (target_pc
);
1827 tree cond
= fold_build2 (condition
, boolean_type_node
, value1
, value2
);
1829 (build3 (COND_EXPR
, void_type_node
, java_truthvalue_conversion (cond
),
1830 build1 (GOTO_EXPR
, void_type_node
, target
),
1831 build_java_empty_stmt ()));
1834 /* Emit code for a TEST-type opcode. */
1837 expand_test (enum tree_code condition
, tree type
, int target_pc
)
1839 tree value1
, value2
;
1840 flush_quick_stack ();
1841 value1
= pop_value (type
);
1842 value2
= (type
== ptr_type_node
) ? null_pointer_node
: integer_zero_node
;
1843 expand_compare (condition
, value1
, value2
, target_pc
);
1846 /* Emit code for a COND-type opcode. */
1849 expand_cond (enum tree_code condition
, tree type
, int target_pc
)
1851 tree value1
, value2
;
1852 flush_quick_stack ();
1853 /* note: pop values in opposite order */
1854 value2
= pop_value (type
);
1855 value1
= pop_value (type
);
1856 /* Maybe should check value1 and value2 for type compatibility ??? */
1857 expand_compare (condition
, value1
, value2
, target_pc
);
1861 expand_java_goto (int target_pc
)
1863 tree target_label
= lookup_label (target_pc
);
1864 flush_quick_stack ();
1865 java_add_stmt (build1 (GOTO_EXPR
, void_type_node
, target_label
));
1869 expand_java_switch (tree selector
, int default_pc
)
1871 tree switch_expr
, x
;
1873 flush_quick_stack ();
1874 switch_expr
= build3 (SWITCH_EXPR
, TREE_TYPE (selector
), selector
,
1875 NULL_TREE
, NULL_TREE
);
1876 java_add_stmt (switch_expr
);
1878 x
= build_case_label (NULL_TREE
, NULL_TREE
,
1879 create_artificial_label (input_location
));
1880 append_to_statement_list (x
, &SWITCH_BODY (switch_expr
));
1882 x
= build1 (GOTO_EXPR
, void_type_node
, lookup_label (default_pc
));
1883 append_to_statement_list (x
, &SWITCH_BODY (switch_expr
));
1889 expand_java_add_case (tree switch_expr
, int match
, int target_pc
)
1893 value
= build_int_cst (TREE_TYPE (switch_expr
), match
);
1895 x
= build_case_label (value
, NULL_TREE
,
1896 create_artificial_label (input_location
));
1897 append_to_statement_list (x
, &SWITCH_BODY (switch_expr
));
1899 x
= build1 (GOTO_EXPR
, void_type_node
, lookup_label (target_pc
));
1900 append_to_statement_list (x
, &SWITCH_BODY (switch_expr
));
1903 static vec
<tree
, va_gc
> *
1904 pop_arguments (tree method_type
)
1906 function_args_iterator fnai
;
1908 vec
<tree
, va_gc
> *args
= NULL
;
1911 FOREACH_FUNCTION_ARGS (method_type
, type
, fnai
)
1913 /* XXX: leaky abstraction. */
1914 if (type
== void_type_node
)
1917 vec_safe_push (args
, type
);
1920 arity
= vec_safe_length (args
);
1924 tree arg
= pop_value ((*args
)[arity
]);
1926 /* We simply cast each argument to its proper type. This is
1927 needed since we lose type information coming out of the
1928 verifier. We also have to do this when we pop an integer
1929 type that must be promoted for the function call. */
1930 if (TREE_CODE (type
) == POINTER_TYPE
)
1931 arg
= build1 (NOP_EXPR
, type
, arg
);
1932 else if (targetm
.calls
.promote_prototypes (type
)
1933 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)
1934 && INTEGRAL_TYPE_P (type
))
1935 arg
= convert (integer_type_node
, arg
);
1937 (*args
)[arity
] = arg
;
1943 /* Attach to PTR (a block) the declaration found in ENTRY. */
1946 attach_init_test_initialization_flags (treetreehash_entry
**slot
, tree block
)
1948 treetreehash_entry
*ite
= *slot
;
1950 if (block
!= error_mark_node
)
1952 if (TREE_CODE (block
) == BIND_EXPR
)
1954 tree body
= BIND_EXPR_BODY (block
);
1955 DECL_CHAIN (ite
->value
) = BIND_EXPR_VARS (block
);
1956 BIND_EXPR_VARS (block
) = ite
->value
;
1957 body
= build2 (COMPOUND_EXPR
, void_type_node
,
1958 build1 (DECL_EXPR
, void_type_node
, ite
->value
), body
);
1959 BIND_EXPR_BODY (block
) = body
;
1963 tree body
= BLOCK_SUBBLOCKS (block
);
1964 TREE_CHAIN (ite
->value
) = BLOCK_EXPR_DECLS (block
);
1965 BLOCK_EXPR_DECLS (block
) = ite
->value
;
1966 body
= build2 (COMPOUND_EXPR
, void_type_node
,
1967 build1 (DECL_EXPR
, void_type_node
, ite
->value
), body
);
1968 BLOCK_SUBBLOCKS (block
) = body
;
1975 /* Build an expression to initialize the class CLAS.
1976 if EXPR is non-NULL, returns an expression to first call the initializer
1977 (if it is needed) and then calls EXPR. */
1980 build_class_init (tree clas
, tree expr
)
1984 /* An optimization: if CLAS is a superclass of the class we're
1985 compiling, we don't need to initialize it. However, if CLAS is
1986 an interface, it won't necessarily be initialized, even if we
1988 if ((! CLASS_INTERFACE (TYPE_NAME (clas
))
1989 && inherits_from_p (current_class
, clas
))
1990 || current_class
== clas
)
1993 if (always_initialize_class_p
)
1995 init
= build_call_nary (void_type_node
,
1996 build_address_of (soft_initclass_node
),
1997 1, build_class_ref (clas
));
1998 TREE_SIDE_EFFECTS (init
) = 1;
2002 tree
*init_test_decl
;
2004 init_test_decl
= java_treetreehash_new
2005 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl
), clas
);
2007 if (*init_test_decl
== NULL
)
2009 /* Build a declaration and mark it as a flag used to track
2010 static class initializations. */
2011 decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
2013 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl
);
2014 DECL_CONTEXT (decl
) = current_function_decl
;
2015 DECL_INITIAL (decl
) = boolean_false_node
;
2016 /* Don't emit any symbolic debugging info for this decl. */
2017 DECL_IGNORED_P (decl
) = 1;
2018 *init_test_decl
= decl
;
2021 init
= build_call_nary (void_type_node
,
2022 build_address_of (soft_initclass_node
),
2023 1, build_class_ref (clas
));
2024 TREE_SIDE_EFFECTS (init
) = 1;
2025 init
= build3 (COND_EXPR
, void_type_node
,
2026 build2 (EQ_EXPR
, boolean_type_node
,
2027 *init_test_decl
, boolean_false_node
),
2028 init
, integer_zero_node
);
2029 TREE_SIDE_EFFECTS (init
) = 1;
2030 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), init
,
2031 build2 (MODIFY_EXPR
, boolean_type_node
,
2032 *init_test_decl
, boolean_true_node
));
2033 TREE_SIDE_EFFECTS (init
) = 1;
2036 if (expr
!= NULL_TREE
)
2038 expr
= build2 (COMPOUND_EXPR
, TREE_TYPE (expr
), init
, expr
);
2039 TREE_SIDE_EFFECTS (expr
) = 1;
2047 /* Rewrite expensive calls that require stack unwinding at runtime to
2048 cheaper alternatives. The logic here performs these
2051 java.lang.Class.forName("foo") -> java.lang.Class.forName("foo", class$)
2052 java.lang.Class.getClassLoader() -> java.lang.Class.getClassLoader(class$)
2058 const char *classname
;
2060 const char *signature
;
2061 const char *new_classname
;
2062 const char *new_signature
;
2064 void (*rewrite_arglist
) (vec
<tree
, va_gc
> **);
2067 /* Add __builtin_return_address(0) to the end of an arglist. */
2071 rewrite_arglist_getcaller (vec
<tree
, va_gc
> **arglist
)
2074 = build_call_expr (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS
),
2075 1, integer_zero_node
);
2077 DECL_UNINLINABLE (current_function_decl
) = 1;
2079 vec_safe_push (*arglist
, retaddr
);
2082 /* Add this.class to the end of an arglist. */
2085 rewrite_arglist_getclass (vec
<tree
, va_gc
> **arglist
)
2087 vec_safe_push (*arglist
, build_class_ref (output_class
));
2090 static rewrite_rule rules
[] =
2091 {{"java.lang.Class", "getClassLoader", "()Ljava/lang/ClassLoader;",
2092 "java.lang.Class", "(Ljava/lang/Class;)Ljava/lang/ClassLoader;",
2093 ACC_FINAL
|ACC_PRIVATE
, rewrite_arglist_getclass
},
2095 {"java.lang.Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
2096 "java.lang.Class", "(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class;",
2097 ACC_FINAL
|ACC_PRIVATE
|ACC_STATIC
, rewrite_arglist_getclass
},
2099 {"gnu.classpath.VMStackWalker", "getCallingClass", "()Ljava/lang/Class;",
2100 "gnu.classpath.VMStackWalker", "(Lgnu/gcj/RawData;)Ljava/lang/Class;",
2101 ACC_FINAL
|ACC_PRIVATE
|ACC_STATIC
, rewrite_arglist_getcaller
},
2103 {"gnu.classpath.VMStackWalker", "getCallingClassLoader",
2104 "()Ljava/lang/ClassLoader;",
2105 "gnu.classpath.VMStackWalker", "(Lgnu/gcj/RawData;)Ljava/lang/ClassLoader;",
2106 ACC_FINAL
|ACC_PRIVATE
|ACC_STATIC
, rewrite_arglist_getcaller
},
2108 {"gnu.java.lang.VMCPStringBuilder", "toString", "([CII)Ljava/lang/String;",
2109 "java.lang.String", "([CII)Ljava/lang/String;",
2110 ACC_FINAL
|ACC_PRIVATE
|ACC_STATIC
, NULL
},
2112 {NULL
, NULL
, NULL
, NULL
, NULL
, 0, NULL
}};
2114 /* True if this method is special, i.e. it's a private method that
2115 should be exported from a DSO. */
2118 special_method_p (tree candidate_method
)
2120 tree context
= DECL_NAME (TYPE_NAME (DECL_CONTEXT (candidate_method
)));
2121 tree method
= DECL_NAME (candidate_method
);
2124 for (p
= rules
; p
->classname
; p
++)
2126 if (get_identifier (p
->classname
) == context
2127 && get_identifier (p
->method
) == method
)
2133 /* Scan the rules list for replacements for *METHOD_P and replace the
2134 args accordingly. If the rewrite results in an access to a private
2135 method, update SPECIAL.*/
2138 maybe_rewrite_invocation (tree
*method_p
, vec
<tree
, va_gc
> **arg_list_p
,
2139 tree
*method_signature_p
, tree
*special
)
2141 tree context
= DECL_NAME (TYPE_NAME (DECL_CONTEXT (*method_p
)));
2143 *special
= NULL_TREE
;
2145 for (p
= rules
; p
->classname
; p
++)
2147 if (get_identifier (p
->classname
) == context
)
2149 tree method
= DECL_NAME (*method_p
);
2150 if (get_identifier (p
->method
) == method
2151 && get_identifier (p
->signature
) == *method_signature_p
)
2154 tree destination_class
2155 = lookup_class (get_identifier (p
->new_classname
));
2156 gcc_assert (destination_class
);
2158 = lookup_java_method (destination_class
,
2160 get_identifier (p
->new_signature
));
2161 if (! maybe_method
&& ! flag_verify_invocations
)
2164 = add_method (destination_class
, p
->flags
,
2165 method
, get_identifier (p
->new_signature
));
2166 DECL_EXTERNAL (maybe_method
) = 1;
2168 *method_p
= maybe_method
;
2169 gcc_assert (*method_p
);
2170 if (p
->rewrite_arglist
)
2171 p
->rewrite_arglist (arg_list_p
);
2172 *method_signature_p
= get_identifier (p
->new_signature
);
2173 *special
= integer_one_node
;
2184 build_known_method_ref (tree method
, tree method_type ATTRIBUTE_UNUSED
,
2185 tree self_type
, tree method_signature ATTRIBUTE_UNUSED
,
2186 vec
<tree
, va_gc
> *arg_list ATTRIBUTE_UNUSED
, tree special
)
2189 if (is_compiled_class (self_type
))
2191 /* With indirect dispatch we have to use indirect calls for all
2192 publicly visible methods or gcc will use PLT indirections
2193 to reach them. We also have to use indirect dispatch for all
2194 external methods. */
2195 if (! flag_indirect_dispatch
2196 || (! DECL_EXTERNAL (method
) && ! TREE_PUBLIC (method
)))
2198 func
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (method
)),
2204 = build_int_cst (NULL_TREE
,
2205 (get_symbol_table_index
2207 &TYPE_ATABLE_METHODS (output_class
))));
2209 = build4 (ARRAY_REF
,
2210 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class
))),
2211 TYPE_ATABLE_DECL (output_class
), table_index
,
2212 NULL_TREE
, NULL_TREE
);
2214 func
= convert (method_ptr_type_node
, func
);
2218 /* We don't know whether the method has been (statically) compiled.
2219 Compile this code to get a reference to the method's code:
2221 SELF_TYPE->methods[METHOD_INDEX].ncode
2225 int method_index
= 0;
2228 /* The method might actually be declared in some superclass, so
2229 we have to use its class context, not the caller's notion of
2230 where the method is. */
2231 self_type
= DECL_CONTEXT (method
);
2232 ref
= build_class_ref (self_type
);
2233 ref
= build1 (INDIRECT_REF
, class_type_node
, ref
);
2234 if (ncode_ident
== NULL_TREE
)
2235 ncode_ident
= get_identifier ("ncode");
2236 if (methods_ident
== NULL_TREE
)
2237 methods_ident
= get_identifier ("methods");
2238 ref
= build3 (COMPONENT_REF
, method_ptr_type_node
, ref
,
2239 lookup_field (&class_type_node
, methods_ident
),
2241 for (meth
= TYPE_METHODS (self_type
);
2242 ; meth
= DECL_CHAIN (meth
))
2246 if (meth
== NULL_TREE
)
2247 fatal_error (input_location
, "method '%s' not found in class",
2248 IDENTIFIER_POINTER (DECL_NAME (method
)));
2251 method_index
*= int_size_in_bytes (method_type_node
);
2252 ref
= fold_build_pointer_plus_hwi (ref
, method_index
);
2253 ref
= build1 (INDIRECT_REF
, method_type_node
, ref
);
2254 func
= build3 (COMPONENT_REF
, nativecode_ptr_type_node
,
2255 ref
, lookup_field (&method_type_node
, ncode_ident
),
2262 invoke_build_dtable (int is_invoke_interface
, vec
<tree
, va_gc
> *arg_list
)
2264 tree dtable
, objectref
;
2265 tree saved
= save_expr ((*arg_list
)[0]);
2267 (*arg_list
)[0] = saved
;
2269 /* If we're dealing with interfaces and if the objectref
2270 argument is an array then get the dispatch table of the class
2271 Object rather than the one from the objectref. */
2272 objectref
= (is_invoke_interface
2273 && is_array_type_p (TREE_TYPE (saved
))
2274 ? build_class_ref (object_type_node
) : saved
);
2276 if (dtable_ident
== NULL_TREE
)
2277 dtable_ident
= get_identifier ("vtable");
2278 dtable
= build_java_indirect_ref (object_type_node
, objectref
,
2279 flag_check_references
);
2280 dtable
= build3 (COMPONENT_REF
, dtable_ptr_type
, dtable
,
2281 lookup_field (&object_type_node
, dtable_ident
), NULL_TREE
);
2286 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2287 T. If this decl has not been seen before, it will be added to the
2288 [oa]table_methods. If it has, the existing table slot will be
2292 get_symbol_table_index (tree t
, tree special
,
2293 vec
<method_entry
, va_gc
> **symbol_table
)
2297 method_entry elem
= {t
, special
};
2299 FOR_EACH_VEC_SAFE_ELT (*symbol_table
, i
, e
)
2300 if (t
== e
->method
&& special
== e
->special
)
2303 vec_safe_push (*symbol_table
, elem
);
2310 build_invokevirtual (tree dtable
, tree method
, tree special
)
2313 tree nativecode_ptr_ptr_type_node
2314 = build_pointer_type (nativecode_ptr_type_node
);
2318 if (flag_indirect_dispatch
)
2320 gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method
))));
2323 = build_int_cst (NULL_TREE
, get_symbol_table_index
2325 &TYPE_OTABLE_METHODS (output_class
)));
2326 method_index
= build4 (ARRAY_REF
, integer_type_node
,
2327 TYPE_OTABLE_DECL (output_class
),
2328 otable_index
, NULL_TREE
, NULL_TREE
);
2332 /* We fetch the DECL_VINDEX field directly here, rather than
2333 using get_method_index(). DECL_VINDEX is the true offset
2334 from the vtable base to a method, regrdless of any extra
2335 words inserted at the start of the vtable. */
2336 method_index
= DECL_VINDEX (method
);
2337 method_index
= size_binop (MULT_EXPR
, method_index
,
2338 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node
));
2339 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2340 method_index
= size_binop (MULT_EXPR
, method_index
,
2341 size_int (TARGET_VTABLE_USES_DESCRIPTORS
));
2344 func
= fold_build_pointer_plus (dtable
, method_index
);
2346 if (TARGET_VTABLE_USES_DESCRIPTORS
)
2347 func
= build1 (NOP_EXPR
, nativecode_ptr_type_node
, func
);
2350 func
= fold_convert (nativecode_ptr_ptr_type_node
, func
);
2351 func
= build1 (INDIRECT_REF
, nativecode_ptr_type_node
, func
);
2357 static GTY(()) tree class_ident
;
2359 build_invokeinterface (tree dtable
, tree method
)
2364 /* We expand invokeinterface here. */
2366 if (class_ident
== NULL_TREE
)
2367 class_ident
= get_identifier ("class");
2369 dtable
= build_java_indirect_ref (dtable_type
, dtable
,
2370 flag_check_references
);
2371 dtable
= build3 (COMPONENT_REF
, class_ptr_type
, dtable
,
2372 lookup_field (&dtable_type
, class_ident
), NULL_TREE
);
2374 interface
= DECL_CONTEXT (method
);
2375 gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface
)));
2376 layout_class_methods (interface
);
2378 if (flag_indirect_dispatch
)
2381 = 2 * (get_symbol_table_index
2382 (method
, NULL_TREE
, &TYPE_ITABLE_METHODS (output_class
)));
2384 = build4 (ARRAY_REF
,
2385 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class
))),
2386 TYPE_ITABLE_DECL (output_class
),
2387 build_int_cst (NULL_TREE
, itable_index
-1),
2388 NULL_TREE
, NULL_TREE
);
2390 = build4 (ARRAY_REF
,
2391 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class
))),
2392 TYPE_ITABLE_DECL (output_class
),
2393 build_int_cst (NULL_TREE
, itable_index
),
2394 NULL_TREE
, NULL_TREE
);
2395 interface
= convert (class_ptr_type
, interface
);
2396 idx
= convert (integer_type_node
, idx
);
2400 idx
= build_int_cst (NULL_TREE
,
2401 get_interface_method_index (method
, interface
));
2402 interface
= build_class_ref (interface
);
2405 return build_call_nary (ptr_type_node
,
2406 build_address_of (soft_lookupinterfacemethod_node
),
2407 3, dtable
, interface
, idx
);
2410 /* Expand one of the invoke_* opcodes.
2411 OPCODE is the specific opcode.
2412 METHOD_REF_INDEX is an index into the constant pool.
2413 NARGS is the number of arguments, or -1 if not specified. */
2416 expand_invoke (int opcode
, int method_ref_index
, int nargs ATTRIBUTE_UNUSED
)
2418 tree method_signature
2419 = COMPONENT_REF_SIGNATURE(¤t_jcf
->cpool
, method_ref_index
);
2420 tree method_name
= COMPONENT_REF_NAME (¤t_jcf
->cpool
,
2423 = get_class_constant (current_jcf
,
2424 COMPONENT_REF_CLASS_INDEX(¤t_jcf
->cpool
,
2426 const char *const self_name
2427 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type
)));
2428 tree call
, func
, method
, method_type
;
2429 vec
<tree
, va_gc
> *arg_list
;
2430 tree check
= NULL_TREE
;
2432 tree special
= NULL_TREE
;
2434 if (! CLASS_LOADED_P (self_type
))
2436 load_class (self_type
, 1);
2437 safe_layout_class (self_type
);
2438 if (TREE_CODE (TYPE_SIZE (self_type
)) == ERROR_MARK
)
2439 fatal_error (input_location
, "failed to find class '%s'", self_name
);
2441 layout_class_methods (self_type
);
2443 if (ID_INIT_P (method_name
))
2444 method
= lookup_java_constructor (self_type
, method_signature
);
2446 method
= lookup_java_method (self_type
, method_name
, method_signature
);
2448 /* We've found a method in a class other than the one in which it
2449 was wanted. This can happen if, for instance, we're trying to
2450 compile invokespecial super.equals().
2451 FIXME: This is a kludge. Rather than nullifying the result, we
2452 should change lookup_java_method() so that it doesn't search the
2453 superclass chain when we're BC-compiling. */
2454 if (! flag_verify_invocations
2456 && ! TYPE_ARRAY_P (self_type
)
2457 && self_type
!= DECL_CONTEXT (method
))
2460 /* We've found a method in an interface, but this isn't an interface
2462 if (opcode
!= OPCODE_invokeinterface
2464 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method
)))))
2467 /* We've found a non-interface method but we are making an
2468 interface call. This can happen if the interface overrides a
2469 method in Object. */
2470 if (! flag_verify_invocations
2471 && opcode
== OPCODE_invokeinterface
2473 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method
))))
2476 if (method
== NULL_TREE
)
2478 if (flag_verify_invocations
|| ! flag_indirect_dispatch
)
2480 error ("class '%s' has no method named '%s' matching signature '%s'",
2482 IDENTIFIER_POINTER (method_name
),
2483 IDENTIFIER_POINTER (method_signature
));
2487 int flags
= ACC_PUBLIC
;
2488 if (opcode
== OPCODE_invokestatic
)
2489 flags
|= ACC_STATIC
;
2490 if (opcode
== OPCODE_invokeinterface
)
2492 flags
|= ACC_INTERFACE
| ACC_ABSTRACT
;
2493 CLASS_INTERFACE (TYPE_NAME (self_type
)) = 1;
2495 method
= add_method (self_type
, flags
, method_name
,
2497 DECL_ARTIFICIAL (method
) = 1;
2498 METHOD_DUMMY (method
) = 1;
2499 layout_class_method (self_type
, NULL
,
2504 /* Invoke static can't invoke static/abstract method */
2505 if (method
!= NULL_TREE
)
2507 if (opcode
== OPCODE_invokestatic
)
2509 if (!METHOD_STATIC (method
))
2511 error ("invokestatic on non static method");
2514 else if (METHOD_ABSTRACT (method
))
2516 error ("invokestatic on abstract method");
2522 if (METHOD_STATIC (method
))
2524 error ("invoke[non-static] on static method");
2530 if (method
== NULL_TREE
)
2532 /* If we got here, we emitted an error message above. So we
2533 just pop the arguments, push a properly-typed zero, and
2535 method_type
= get_type_from_signature (method_signature
);
2536 pop_arguments (method_type
);
2537 if (opcode
!= OPCODE_invokestatic
)
2538 pop_type (self_type
);
2539 method_type
= promote_type (TREE_TYPE (method_type
));
2540 push_value (convert (method_type
, integer_zero_node
));
2544 arg_list
= pop_arguments (TREE_TYPE (method
));
2545 flush_quick_stack ();
2547 maybe_rewrite_invocation (&method
, &arg_list
, &method_signature
,
2549 method_type
= TREE_TYPE (method
);
2552 if (opcode
== OPCODE_invokestatic
)
2553 func
= build_known_method_ref (method
, method_type
, self_type
,
2554 method_signature
, arg_list
, special
);
2555 else if (opcode
== OPCODE_invokespecial
2556 || (opcode
== OPCODE_invokevirtual
2557 && (METHOD_PRIVATE (method
)
2558 || METHOD_FINAL (method
)
2559 || CLASS_FINAL (TYPE_NAME (self_type
)))))
2561 /* If the object for the method call is null, we throw an
2562 exception. We don't do this if the object is the current
2563 method's `this'. In other cases we just rely on an
2564 optimization pass to eliminate redundant checks. FIXME:
2565 Unfortunately there doesn't seem to be a way to determine
2566 what the current method is right now.
2567 We do omit the check if we're calling <init>. */
2568 /* We use a SAVE_EXPR here to make sure we only evaluate
2569 the new `self' expression once. */
2570 tree save_arg
= save_expr ((*arg_list
)[0]);
2571 (*arg_list
)[0] = save_arg
;
2572 check
= java_check_reference (save_arg
, ! DECL_INIT_P (method
));
2573 func
= build_known_method_ref (method
, method_type
, self_type
,
2574 method_signature
, arg_list
, special
);
2578 tree dtable
= invoke_build_dtable (opcode
== OPCODE_invokeinterface
,
2580 if (opcode
== OPCODE_invokevirtual
)
2581 func
= build_invokevirtual (dtable
, method
, special
);
2583 func
= build_invokeinterface (dtable
, method
);
2586 if (TREE_CODE (func
) == ADDR_EXPR
)
2587 TREE_TYPE (func
) = build_pointer_type (method_type
);
2589 func
= build1 (NOP_EXPR
, build_pointer_type (method_type
), func
);
2591 call
= build_call_vec (TREE_TYPE (method_type
), func
, arg_list
);
2592 TREE_SIDE_EFFECTS (call
) = 1;
2593 call
= check_for_builtin (method
, call
);
2595 if (check
!= NULL_TREE
)
2597 call
= build2 (COMPOUND_EXPR
, TREE_TYPE (call
), check
, call
);
2598 TREE_SIDE_EFFECTS (call
) = 1;
2601 if (TREE_CODE (TREE_TYPE (method_type
)) == VOID_TYPE
)
2602 java_add_stmt (call
);
2606 flush_quick_stack ();
2610 /* Create a stub which will be put into the vtable but which will call
2614 build_jni_stub (tree method
)
2616 tree jnifunc
, call
, body
, method_sig
, arg_types
;
2617 tree jniarg0
, jniarg1
, jniarg2
, jniarg3
;
2618 tree jni_func_type
, tem
;
2619 tree env_var
, res_var
= NULL_TREE
, block
;
2623 vec
<tree
, va_gc
> *args
= NULL
;
2626 tree klass
= DECL_CONTEXT (method
);
2627 klass
= build_class_ref (klass
);
2629 gcc_assert (METHOD_NATIVE (method
) && flag_jni
);
2631 DECL_ARTIFICIAL (method
) = 1;
2632 DECL_EXTERNAL (method
) = 0;
2634 env_var
= build_decl (input_location
,
2635 VAR_DECL
, get_identifier ("env"), ptr_type_node
);
2636 DECL_CONTEXT (env_var
) = method
;
2638 if (TREE_TYPE (TREE_TYPE (method
)) != void_type_node
)
2640 res_var
= build_decl (input_location
, VAR_DECL
, get_identifier ("res"),
2641 TREE_TYPE (TREE_TYPE (method
)));
2642 DECL_CONTEXT (res_var
) = method
;
2643 DECL_CHAIN (env_var
) = res_var
;
2646 method_args
= DECL_ARGUMENTS (method
);
2647 block
= build_block (env_var
, NULL_TREE
, method_args
, NULL_TREE
);
2648 TREE_SIDE_EFFECTS (block
) = 1;
2650 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2651 body
= build2 (MODIFY_EXPR
, ptr_type_node
, env_var
,
2652 build_call_nary (ptr_type_node
,
2653 build_address_of (soft_getjnienvnewframe_node
),
2656 /* The JNIEnv structure is the first argument to the JNI function. */
2657 args_size
+= int_size_in_bytes (TREE_TYPE (env_var
));
2658 vec_safe_push (args
, env_var
);
2660 /* For a static method the second argument is the class. For a
2661 non-static method the second argument is `this'; that is already
2662 available in the argument list. */
2663 if (METHOD_STATIC (method
))
2665 args_size
+= int_size_in_bytes (TREE_TYPE (klass
));
2666 vec_safe_push (args
, klass
);
2669 /* All the arguments to this method become arguments to the
2670 underlying JNI function. If we had to wrap object arguments in a
2671 special way, we would do that here. */
2672 for (tem
= method_args
; tem
!= NULL_TREE
; tem
= DECL_CHAIN (tem
))
2674 int arg_bits
= TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem
)));
2675 #ifdef PARM_BOUNDARY
2676 arg_bits
= (((arg_bits
+ PARM_BOUNDARY
- 1) / PARM_BOUNDARY
)
2679 args_size
+= (arg_bits
/ BITS_PER_UNIT
);
2681 vec_safe_push (args
, tem
);
2683 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
2685 /* Argument types for static methods and the JNIEnv structure.
2686 FIXME: Write and use build_function_type_vec to avoid this. */
2687 if (METHOD_STATIC (method
))
2688 arg_types
= tree_cons (NULL_TREE
, object_ptr_type_node
, arg_types
);
2689 arg_types
= tree_cons (NULL_TREE
, ptr_type_node
, arg_types
);
2691 /* We call _Jv_LookupJNIMethod to find the actual underlying
2692 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2693 exception if this function is not found at runtime. */
2694 method_sig
= build_java_signature (TREE_TYPE (method
));
2696 jniarg1
= build_utf8_ref (DECL_NAME (method
));
2697 jniarg2
= build_utf8_ref (unmangle_classname
2698 (IDENTIFIER_POINTER (method_sig
),
2699 IDENTIFIER_LENGTH (method_sig
)));
2700 jniarg3
= build_int_cst (NULL_TREE
, args_size
);
2702 tem
= build_function_type (TREE_TYPE (TREE_TYPE (method
)), arg_types
);
2704 #ifdef MODIFY_JNI_METHOD_CALL
2705 tem
= MODIFY_JNI_METHOD_CALL (tem
);
2708 jni_func_type
= build_pointer_type (tem
);
2710 /* Use the actual function type, rather than a generic pointer type,
2711 such that this decl keeps the actual pointer type from being
2712 garbage-collected. If it is, we end up using canonical types
2713 with different uids for equivalent function types, and this in
2714 turn causes utf8 identifiers and output order to vary. */
2715 meth_var
= build_decl (input_location
,
2716 VAR_DECL
, get_identifier ("meth"), jni_func_type
);
2717 TREE_STATIC (meth_var
) = 1;
2718 TREE_PUBLIC (meth_var
) = 0;
2719 DECL_EXTERNAL (meth_var
) = 0;
2720 DECL_CONTEXT (meth_var
) = method
;
2721 DECL_ARTIFICIAL (meth_var
) = 1;
2722 DECL_INITIAL (meth_var
) = null_pointer_node
;
2723 TREE_USED (meth_var
) = 1;
2724 chainon (env_var
, meth_var
);
2725 build_result_decl (method
);
2727 jnifunc
= build3 (COND_EXPR
, jni_func_type
,
2728 build2 (NE_EXPR
, boolean_type_node
,
2729 meth_var
, build_int_cst (TREE_TYPE (meth_var
), 0)),
2731 build2 (MODIFY_EXPR
, jni_func_type
, meth_var
,
2733 (NOP_EXPR
, jni_func_type
,
2734 build_call_nary (ptr_type_node
,
2736 (soft_lookupjnimethod_node
),
2739 jniarg2
, jniarg3
))));
2741 /* Now we make the actual JNI call via the resulting function
2743 call
= build_call_vec (TREE_TYPE (TREE_TYPE (method
)), jnifunc
, args
);
2745 /* If the JNI call returned a result, capture it here. If we had to
2746 unwrap JNI object results, we would do that here. */
2747 if (res_var
!= NULL_TREE
)
2749 /* If the call returns an object, it may return a JNI weak
2750 reference, in which case we must unwrap it. */
2751 if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method
))))
2752 call
= build_call_nary (TREE_TYPE (TREE_TYPE (method
)),
2753 build_address_of (soft_unwrapjni_node
),
2755 call
= build2 (MODIFY_EXPR
, TREE_TYPE (TREE_TYPE (method
)),
2759 TREE_SIDE_EFFECTS (call
) = 1;
2761 body
= build2 (COMPOUND_EXPR
, void_type_node
, body
, call
);
2762 TREE_SIDE_EFFECTS (body
) = 1;
2764 /* Now free the environment we allocated. */
2765 call
= build_call_nary (ptr_type_node
,
2766 build_address_of (soft_jnipopsystemframe_node
),
2768 TREE_SIDE_EFFECTS (call
) = 1;
2769 body
= build2 (COMPOUND_EXPR
, void_type_node
, body
, call
);
2770 TREE_SIDE_EFFECTS (body
) = 1;
2772 /* Finally, do the return. */
2773 if (res_var
!= NULL_TREE
)
2776 gcc_assert (DECL_RESULT (method
));
2777 /* Make sure we copy the result variable to the actual
2778 result. We use the type of the DECL_RESULT because it
2779 might be different from the return type of the function:
2780 it might be promoted. */
2781 drt
= TREE_TYPE (DECL_RESULT (method
));
2782 if (drt
!= TREE_TYPE (res_var
))
2783 res_var
= build1 (CONVERT_EXPR
, drt
, res_var
);
2784 res_var
= build2 (MODIFY_EXPR
, drt
, DECL_RESULT (method
), res_var
);
2785 TREE_SIDE_EFFECTS (res_var
) = 1;
2788 body
= build2 (COMPOUND_EXPR
, void_type_node
, body
,
2789 build1 (RETURN_EXPR
, void_type_node
, res_var
));
2790 TREE_SIDE_EFFECTS (body
) = 1;
2792 /* Prepend class initialization for static methods reachable from
2794 if (METHOD_STATIC (method
)
2795 && (! METHOD_PRIVATE (method
)
2796 || INNER_CLASS_P (DECL_CONTEXT (method
))))
2798 tree init
= build_call_expr (soft_initclass_node
, 1,
2800 body
= build2 (COMPOUND_EXPR
, void_type_node
, init
, body
);
2801 TREE_SIDE_EFFECTS (body
) = 1;
2804 bind
= build3 (BIND_EXPR
, void_type_node
, BLOCK_VARS (block
),
2810 /* Given lvalue EXP, return a volatile expression that references the
2814 java_modify_addr_for_volatile (tree exp
)
2816 tree exp_type
= TREE_TYPE (exp
);
2818 = build_qualified_type (exp_type
,
2819 TYPE_QUALS (exp_type
) | TYPE_QUAL_VOLATILE
);
2820 tree addr
= build_fold_addr_expr (exp
);
2821 v_type
= build_pointer_type (v_type
);
2822 addr
= fold_convert (v_type
, addr
);
2823 exp
= build_fold_indirect_ref (addr
);
2828 /* Expand an operation to extract from or store into a field.
2829 IS_STATIC is 1 iff the field is static.
2830 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2831 FIELD_REF_INDEX is an index into the constant pool. */
2834 expand_java_field_op (int is_static
, int is_putting
, int field_ref_index
)
2837 = get_class_constant (current_jcf
,
2838 COMPONENT_REF_CLASS_INDEX (¤t_jcf
->cpool
,
2840 const char *self_name
2841 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type
)));
2842 tree field_name
= COMPONENT_REF_NAME (¤t_jcf
->cpool
, field_ref_index
);
2843 tree field_signature
= COMPONENT_REF_SIGNATURE (¤t_jcf
->cpool
,
2845 tree field_type
= get_type_from_signature (field_signature
);
2846 tree new_value
= is_putting
? pop_value (field_type
) : NULL_TREE
;
2849 tree original_self_type
= self_type
;
2853 if (! CLASS_LOADED_P (self_type
))
2854 load_class (self_type
, 1);
2855 field_decl
= lookup_field (&self_type
, field_name
);
2856 if (field_decl
== error_mark_node
)
2860 else if (field_decl
== NULL_TREE
)
2862 if (! flag_verify_invocations
)
2864 int flags
= ACC_PUBLIC
;
2866 flags
|= ACC_STATIC
;
2867 self_type
= original_self_type
;
2868 field_decl
= add_field (original_self_type
, field_name
,
2870 DECL_ARTIFICIAL (field_decl
) = 1;
2871 DECL_IGNORED_P (field_decl
) = 1;
2873 /* FIXME: We should be pessimistic about volatility. We
2874 don't know one way or another, but this is safe.
2875 However, doing this has bad effects on code quality. We
2876 need to look at better ways to do this. */
2877 TREE_THIS_VOLATILE (field_decl
) = 1;
2882 error ("missing field '%s' in '%s'",
2883 IDENTIFIER_POINTER (field_name
), self_name
);
2887 else if (build_java_signature (TREE_TYPE (field_decl
)) != field_signature
)
2889 error ("mismatching signature for field '%s' in '%s'",
2890 IDENTIFIER_POINTER (field_name
), self_name
);
2893 field_ref
= is_static
? NULL_TREE
: pop_value (self_type
);
2897 push_value (convert (field_type
, integer_zero_node
));
2898 flush_quick_stack ();
2902 field_ref
= build_field_ref (field_ref
, self_type
, field_name
);
2904 && ! flag_indirect_dispatch
)
2906 tree context
= DECL_CONTEXT (field_ref
);
2907 if (context
!= self_type
&& CLASS_INTERFACE (TYPE_NAME (context
)))
2908 field_ref
= build_class_init (context
, field_ref
);
2910 field_ref
= build_class_init (self_type
, field_ref
);
2914 flush_quick_stack ();
2915 if (FIELD_FINAL (field_decl
))
2917 if (DECL_CONTEXT (field_decl
) != current_class
)
2918 error ("assignment to final field %q+D not in field%'s class",
2920 /* We used to check for assignments to final fields not
2921 occurring in the class initializer or in a constructor
2922 here. However, this constraint doesn't seem to be
2923 enforced by the JVM. */
2926 if (TREE_THIS_VOLATILE (field_decl
))
2927 field_ref
= java_modify_addr_for_volatile (field_ref
);
2929 modify_expr
= build2 (MODIFY_EXPR
, TREE_TYPE (field_ref
),
2930 field_ref
, new_value
);
2932 if (TREE_THIS_VOLATILE (field_decl
))
2934 tree sync
= builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE
);
2935 java_add_stmt (build_call_expr (sync
, 0));
2938 java_add_stmt (modify_expr
);
2942 tree temp
= build_decl (input_location
,
2943 VAR_DECL
, NULL_TREE
, TREE_TYPE (field_ref
));
2944 java_add_local_var (temp
);
2946 if (TREE_THIS_VOLATILE (field_decl
))
2947 field_ref
= java_modify_addr_for_volatile (field_ref
);
2950 = build2 (MODIFY_EXPR
, TREE_TYPE (field_ref
), temp
, field_ref
);
2951 java_add_stmt (modify_expr
);
2953 if (TREE_THIS_VOLATILE (field_decl
))
2955 tree sync
= builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE
);
2956 java_add_stmt (build_call_expr (sync
, 0));
2961 TREE_THIS_VOLATILE (field_ref
) = TREE_THIS_VOLATILE (field_decl
);
2965 load_type_state (int pc
)
2968 tree vec
= (*type_states
)[pc
];
2969 int cur_length
= TREE_VEC_LENGTH (vec
);
2970 stack_pointer
= cur_length
- DECL_MAX_LOCALS(current_function_decl
);
2971 for (i
= 0; i
< cur_length
; i
++)
2972 type_map
[i
] = TREE_VEC_ELT (vec
, i
);
2975 /* Go over METHOD's bytecode and note instruction starts in
2976 instruction_bits[]. */
2979 note_instructions (JCF
*jcf
, tree method
)
2982 unsigned char* byte_ops
;
2983 long length
= DECL_CODE_LENGTH (method
);
2988 #undef RET /* Defined by config/i386/i386.h */
2990 #define BCODE byte_ops
2991 #define BYTE_type_node byte_type_node
2992 #define SHORT_type_node short_type_node
2993 #define INT_type_node int_type_node
2994 #define LONG_type_node long_type_node
2995 #define CHAR_type_node char_type_node
2996 #define PTR_type_node ptr_type_node
2997 #define FLOAT_type_node float_type_node
2998 #define DOUBLE_type_node double_type_node
2999 #define VOID_type_node void_type_node
3000 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
3001 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
3002 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
3003 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
3005 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
3007 JCF_SEEK (jcf
, DECL_CODE_OFFSET (method
));
3008 byte_ops
= jcf
->read_ptr
;
3009 instruction_bits
= XRESIZEVAR (char, instruction_bits
, length
+ 1);
3010 memset (instruction_bits
, 0, length
+ 1);
3011 vec_alloc (type_states
, length
+ 1);
3012 type_states
->quick_grow_cleared (length
+ 1);
3014 /* This pass figures out which PC can be the targets of jumps. */
3015 for (PC
= 0; PC
< length
;)
3017 int oldpc
= PC
; /* PC at instruction start. */
3018 instruction_bits
[PC
] |= BCODE_INSTRUCTION_START
;
3019 switch (byte_ops
[PC
++])
3021 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3023 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3026 #define NOTE_LABEL(PC) note_label(oldpc, PC)
3028 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
3029 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
3030 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
3031 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3032 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3033 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3034 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3035 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3037 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3038 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3039 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
3040 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
3041 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
3042 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
3043 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
3044 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
3046 /* two forms of wide instructions */
3047 #define PRE_SPECIAL_WIDE(IGNORE) \
3049 int modified_opcode = IMMEDIATE_u1; \
3050 if (modified_opcode == OPCODE_iinc) \
3052 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
3053 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
3057 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
3061 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
3063 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3065 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3066 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
3067 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
3068 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
3069 #define PRE_ARRAY_STORE(TYPE) /* nothing */
3070 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
3071 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
3072 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
3073 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
3074 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
3076 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
3077 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
3078 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3079 saw_index = 0; INT_temp = (OPERAND_VALUE); \
3080 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
3081 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
3082 saw_index = 0; INT_temp = (OPERAND_VALUE); \
3084 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
3086 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
3088 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3089 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
3091 #define PRE_LOOKUP_SWITCH \
3092 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3093 NOTE_LABEL (default_offset+oldpc); \
3095 while (--npairs >= 0) { \
3096 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
3097 jint offset = IMMEDIATE_s4; \
3098 NOTE_LABEL (offset+oldpc); } \
3101 #define PRE_TABLE_SWITCH \
3102 { jint default_offset = IMMEDIATE_s4; \
3103 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3104 NOTE_LABEL (default_offset+oldpc); \
3106 while (low++ <= high) { \
3107 jint offset = IMMEDIATE_s4; \
3108 NOTE_LABEL (offset+oldpc); } \
3111 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
3112 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
3113 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3114 (void)(IMMEDIATE_u2); \
3115 PC += 2 * IS_INTERFACE /* for invokeinterface */;
3117 #include "javaop.def"
3124 expand_byte_code (JCF
*jcf
, tree method
)
3128 const unsigned char *linenumber_pointer
;
3129 int dead_code_index
= -1;
3130 unsigned char* byte_ops
;
3131 long length
= DECL_CODE_LENGTH (method
);
3132 location_t max_location
= input_location
;
3135 JCF_SEEK (jcf
, DECL_CODE_OFFSET (method
));
3136 byte_ops
= jcf
->read_ptr
;
3138 /* We make an initial pass of the line number table, to note
3139 which instructions have associated line number entries. */
3140 linenumber_pointer
= linenumber_table
;
3141 for (i
= 0; i
< linenumber_count
; i
++)
3143 int pc
= GET_u2 (linenumber_pointer
);
3144 linenumber_pointer
+= 4;
3146 warning (0, "invalid PC in line number table");
3149 if ((instruction_bits
[pc
] & BCODE_HAS_LINENUMBER
) != 0)
3150 instruction_bits
[pc
] |= BCODE_HAS_MULTI_LINENUMBERS
;
3151 instruction_bits
[pc
] |= BCODE_HAS_LINENUMBER
;
3155 if (! verify_jvm_instructions_new (jcf
, byte_ops
, length
))
3158 promote_arguments ();
3159 cache_this_class_ref (method
);
3160 cache_cpool_data_ref ();
3162 /* Translate bytecodes. */
3163 linenumber_pointer
= linenumber_table
;
3164 for (PC
= 0; PC
< length
;)
3166 if ((instruction_bits
[PC
] & BCODE_TARGET
) != 0 || PC
== 0)
3168 tree label
= lookup_label (PC
);
3169 flush_quick_stack ();
3170 if ((instruction_bits
[PC
] & BCODE_TARGET
) != 0)
3171 java_add_stmt (build1 (LABEL_EXPR
, void_type_node
, label
));
3172 if ((instruction_bits
[PC
] & BCODE_VERIFIED
) != 0)
3173 load_type_state (PC
);
3176 if (! (instruction_bits
[PC
] & BCODE_VERIFIED
))
3178 if (dead_code_index
== -1)
3180 /* This is the start of a region of unreachable bytecodes.
3181 They still need to be processed in order for EH ranges
3182 to get handled correctly. However, we can simply
3183 replace these bytecodes with nops. */
3184 dead_code_index
= PC
;
3187 /* Turn this bytecode into a nop. */
3192 if (dead_code_index
!= -1)
3194 /* We've just reached the end of a region of dead code. */
3196 warning (0, "unreachable bytecode from %d to before %d",
3197 dead_code_index
, PC
);
3198 dead_code_index
= -1;
3202 /* Handle possible line number entry for this PC.
3204 This code handles out-of-order and multiple linenumbers per PC,
3205 but is optimized for the case of line numbers increasing
3206 monotonically with PC. */
3207 if ((instruction_bits
[PC
] & BCODE_HAS_LINENUMBER
) != 0)
3209 if ((instruction_bits
[PC
] & BCODE_HAS_MULTI_LINENUMBERS
) != 0
3210 || GET_u2 (linenumber_pointer
) != PC
)
3211 linenumber_pointer
= linenumber_table
;
3212 while (linenumber_pointer
< linenumber_table
+ linenumber_count
* 4)
3214 int pc
= GET_u2 (linenumber_pointer
);
3215 linenumber_pointer
+= 4;
3218 int line
= GET_u2 (linenumber_pointer
- 2);
3219 input_location
= linemap_line_start (line_table
, line
, 1);
3220 if (input_location
> max_location
)
3221 max_location
= input_location
;
3222 if (!(instruction_bits
[PC
] & BCODE_HAS_MULTI_LINENUMBERS
))
3227 maybe_pushlevels (PC
);
3228 PC
= process_jvm_instruction (PC
, byte_ops
, length
);
3229 maybe_poplevels (PC
);
3232 uncache_this_class_ref (method
);
3234 if (dead_code_index
!= -1)
3236 /* We've just reached the end of a region of dead code. */
3238 warning (0, "unreachable bytecode from %d to the end of the method",
3242 DECL_FUNCTION_LAST_LINE (method
) = max_location
;
3246 java_push_constant_from_pool (JCF
*jcf
, int index
)
3249 if (JPOOL_TAG (jcf
, index
) == CONSTANT_String
)
3252 name
= get_name_constant (jcf
, JPOOL_USHORT1 (jcf
, index
));
3253 index
= alloc_name_constant (CONSTANT_String
, name
);
3254 c
= build_ref_from_constant_pool (index
);
3255 c
= convert (promote_type (string_type_node
), c
);
3257 else if (JPOOL_TAG (jcf
, index
) == CONSTANT_Class
3258 || JPOOL_TAG (jcf
, index
) == CONSTANT_ResolvedClass
)
3260 tree record
= get_class_constant (jcf
, index
);
3261 c
= build_class_ref (record
);
3264 c
= get_constant (jcf
, index
);
3269 process_jvm_instruction (int PC
, const unsigned char* byte_ops
,
3270 long length ATTRIBUTE_UNUSED
)
3272 const char *opname
; /* Temporary ??? */
3273 int oldpc
= PC
; /* PC at instruction start. */
3275 /* If the instruction is at the beginning of an exception handler,
3276 replace the top of the stack with the thrown object reference. */
3277 if (instruction_bits
[PC
] & BCODE_EXCEPTION_TARGET
)
3279 /* Note that the verifier will not emit a type map at all for
3280 dead exception handlers. In this case we just ignore the
3282 if ((instruction_bits
[PC
] & BCODE_VERIFIED
) != 0)
3284 tree type
= pop_type (promote_type (throwable_type_node
));
3285 push_value (build_exception_object_ref (type
));
3289 switch (byte_ops
[PC
++])
3291 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3294 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3297 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3299 int saw_index = 0; \
3300 int index = OPERAND_VALUE; \
3301 (void) saw_index; /* Avoid set but not used warning. */ \
3303 (find_local_variable (index, return_address_type_node, oldpc)); \
3306 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3308 /* OPERAND_VALUE may have side-effects on PC */ \
3309 int opvalue = OPERAND_VALUE; \
3310 build_java_jsr (oldpc + opvalue, PC); \
3313 /* Push a constant onto the stack. */
3314 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3315 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3316 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3317 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3319 /* internal macro added for use by the WIDE case */
3320 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3321 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3323 /* Push local variable onto the opcode stack. */
3324 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3326 /* have to do this since OPERAND_VALUE may have side-effects */ \
3327 int opvalue = OPERAND_VALUE; \
3328 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3331 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3332 expand_java_return (OPERAND_TYPE##_type_node)
3334 #define REM_EXPR TRUNC_MOD_EXPR
3335 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3336 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3338 #define FIELD(IS_STATIC, IS_PUT) \
3339 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3341 #define TEST(OPERAND_TYPE, CONDITION) \
3342 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3344 #define COND(OPERAND_TYPE, CONDITION) \
3345 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3347 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3348 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3350 #define BRANCH_GOTO(OPERAND_VALUE) \
3351 expand_java_goto (oldpc + OPERAND_VALUE)
3353 #define BRANCH_CALL(OPERAND_VALUE) \
3354 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3357 #define BRANCH_RETURN(OPERAND_VALUE) \
3359 tree type = OPERAND_TYPE##_type_node; \
3360 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3361 expand_java_ret (value); \
3365 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3366 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3367 fprintf (stderr, "(not implemented)\n")
3368 #define NOT_IMPL1(OPERAND_VALUE) \
3369 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3370 fprintf (stderr, "(not implemented)\n")
3372 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3374 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3376 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3378 #define STACK_SWAP(COUNT) java_stack_swap()
3380 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3381 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3382 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3384 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3385 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3387 #define LOOKUP_SWITCH \
3388 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3389 tree selector = pop_value (INT_type_node); \
3390 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3391 while (--npairs >= 0) \
3393 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3394 expand_java_add_case (switch_expr, match, oldpc + offset); \
3398 #define TABLE_SWITCH \
3399 { jint default_offset = IMMEDIATE_s4; \
3400 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3401 tree selector = pop_value (INT_type_node); \
3402 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3403 for (; low <= high; low++) \
3405 jint offset = IMMEDIATE_s4; \
3406 expand_java_add_case (switch_expr, low, oldpc + offset); \
3410 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3411 { int opcode = byte_ops[PC-1]; \
3412 int method_ref_index = IMMEDIATE_u2; \
3414 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3416 expand_invoke (opcode, method_ref_index, nargs); \
3419 /* Handle new, checkcast, instanceof */
3420 #define OBJECT(TYPE, OP) \
3421 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3423 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3425 #define ARRAY_LOAD(OPERAND_TYPE) \
3427 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3430 #define ARRAY_STORE(OPERAND_TYPE) \
3432 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3435 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3436 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3437 #define ARRAY_NEW_PTR() \
3438 push_value (build_anewarray (get_class_constant (current_jcf, \
3440 pop_value (int_type_node)));
3441 #define ARRAY_NEW_NUM() \
3443 int atype = IMMEDIATE_u1; \
3444 push_value (build_newarray (atype, pop_value (int_type_node)));\
3446 #define ARRAY_NEW_MULTI() \
3448 tree klass = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3449 int ndims = IMMEDIATE_u1; \
3450 expand_java_multianewarray( klass, ndims ); \
3453 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3454 push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3455 pop_value (OPERAND_TYPE##_type_node)));
3457 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3459 push_value (build1 (NOP_EXPR, int_type_node, \
3460 (convert (TO_TYPE##_type_node, \
3461 pop_value (FROM_TYPE##_type_node))))); \
3464 #define CONVERT(FROM_TYPE, TO_TYPE) \
3466 push_value (convert (TO_TYPE##_type_node, \
3467 pop_value (FROM_TYPE##_type_node))); \
3470 /* internal macro added for use by the WIDE case
3471 Added TREE_TYPE (decl) assignment, apbianco */
3472 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3475 int index = OPVALUE; \
3476 tree type = OPTYPE; \
3477 value = pop_value (type); \
3478 type = TREE_TYPE (value); \
3479 decl = find_local_variable (index, type, oldpc); \
3480 set_local_type (index, type); \
3481 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3484 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3486 /* have to do this since OPERAND_VALUE may have side-effects */ \
3487 int opvalue = OPERAND_VALUE; \
3488 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3491 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3492 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3494 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3495 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3497 #define MONITOR_OPERATION(call) \
3499 tree o = pop_value (ptr_type_node); \
3501 flush_quick_stack (); \
3502 c = build_java_monitor (call, o); \
3503 TREE_SIDE_EFFECTS (c) = 1; \
3504 java_add_stmt (c); \
3507 #define SPECIAL_IINC(IGNORED) \
3509 unsigned int local_var_index = IMMEDIATE_u1; \
3510 int ival = IMMEDIATE_s1; \
3511 expand_iinc(local_var_index, ival, oldpc); \
3514 #define SPECIAL_WIDE(IGNORED) \
3516 int modified_opcode = IMMEDIATE_u1; \
3517 unsigned int local_var_index = IMMEDIATE_u2; \
3518 switch (modified_opcode) \
3522 int ival = IMMEDIATE_s2; \
3523 expand_iinc (local_var_index, ival, oldpc); \
3526 case OPCODE_iload: \
3527 case OPCODE_lload: \
3528 case OPCODE_fload: \
3529 case OPCODE_dload: \
3530 case OPCODE_aload: \
3532 /* duplicate code from LOAD macro */ \
3533 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3536 case OPCODE_istore: \
3537 case OPCODE_lstore: \
3538 case OPCODE_fstore: \
3539 case OPCODE_dstore: \
3540 case OPCODE_astore: \
3542 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3546 error ("unrecognized wide sub-instruction"); \
3550 #define SPECIAL_THROW(IGNORED) \
3551 build_java_athrow (pop_value (throwable_type_node))
3553 #define SPECIAL_BREAK NOT_IMPL1
3554 #define IMPL NOT_IMPL
3556 #include "javaop.def"
3559 fprintf (stderr
, "%3d: unknown(%3d)\n", oldpc
, byte_ops
[PC
]);
3564 /* Return the opcode at PC in the code section pointed to by
3567 static unsigned char
3568 peek_opcode_at_pc (JCF
*jcf
, int code_offset
, int pc
)
3570 unsigned char opcode
;
3571 long absolute_offset
= (long)JCF_TELL (jcf
);
3573 JCF_SEEK (jcf
, code_offset
);
3574 opcode
= jcf
->read_ptr
[pc
];
3575 JCF_SEEK (jcf
, absolute_offset
);
3579 /* Some bytecode compilers are emitting accurate LocalVariableTable
3580 attributes. Here's an example:
3585 Attribute "LocalVariableTable"
3586 slot #<n>: ... (PC: PC+1 length: L)
3588 This is accurate because the local in slot <n> really exists after
3589 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3591 This procedure recognizes this situation and extends the live range
3592 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3593 length of the store instruction.)
3595 This function is used by `give_name_to_locals' so that a local's
3596 DECL features a DECL_LOCAL_START_PC such that the first related
3597 store operation will use DECL as a destination, not an unrelated
3598 temporary created for the occasion.
3600 This function uses a global (instruction_bits) `note_instructions' should
3601 have allocated and filled properly. */
3604 maybe_adjust_start_pc (struct JCF
*jcf
, int code_offset
,
3605 int start_pc
, int slot
)
3607 int first
, index
, opcode
;
3616 /* Find last previous instruction and remember it */
3617 for (pc
= start_pc
-1; pc
; pc
--)
3618 if (instruction_bits
[pc
] & BCODE_INSTRUCTION_START
)
3622 /* Retrieve the instruction, handle `wide'. */
3623 opcode
= (int) peek_opcode_at_pc (jcf
, code_offset
, pc
++);
3624 if (opcode
== OPCODE_wide
)
3627 opcode
= (int) peek_opcode_at_pc (jcf
, code_offset
, pc
++);
3632 case OPCODE_astore_0
:
3633 case OPCODE_astore_1
:
3634 case OPCODE_astore_2
:
3635 case OPCODE_astore_3
:
3636 first
= OPCODE_astore_0
;
3639 case OPCODE_istore_0
:
3640 case OPCODE_istore_1
:
3641 case OPCODE_istore_2
:
3642 case OPCODE_istore_3
:
3643 first
= OPCODE_istore_0
;
3646 case OPCODE_lstore_0
:
3647 case OPCODE_lstore_1
:
3648 case OPCODE_lstore_2
:
3649 case OPCODE_lstore_3
:
3650 first
= OPCODE_lstore_0
;
3653 case OPCODE_fstore_0
:
3654 case OPCODE_fstore_1
:
3655 case OPCODE_fstore_2
:
3656 case OPCODE_fstore_3
:
3657 first
= OPCODE_fstore_0
;
3660 case OPCODE_dstore_0
:
3661 case OPCODE_dstore_1
:
3662 case OPCODE_dstore_2
:
3663 case OPCODE_dstore_3
:
3664 first
= OPCODE_dstore_0
;
3672 index
= peek_opcode_at_pc (jcf
, code_offset
, pc
);
3675 int other
= peek_opcode_at_pc (jcf
, code_offset
, ++pc
);
3676 index
= (other
<< 8) + index
;
3681 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3682 means we have a <t>store. */
3683 if ((first
> 0 && opcode
- first
== slot
) || (index
> 0 && index
== slot
))
3689 /* Build a node to represent empty statements and blocks. */
3692 build_java_empty_stmt (void)
3694 tree t
= build_empty_stmt (input_location
);
3698 /* Promote all args of integral type before generating any code. */
3701 promote_arguments (void)
3705 for (arg
= DECL_ARGUMENTS (current_function_decl
), i
= 0;
3706 arg
!= NULL_TREE
; arg
= DECL_CHAIN (arg
), i
++)
3708 tree arg_type
= TREE_TYPE (arg
);
3709 if (INTEGRAL_TYPE_P (arg_type
)
3710 && TYPE_PRECISION (arg_type
) < 32)
3712 tree copy
= find_local_variable (i
, integer_type_node
, -1);
3713 java_add_stmt (build2 (MODIFY_EXPR
, integer_type_node
,
3715 fold_convert (integer_type_node
, arg
)));
3717 if (TYPE_IS_WIDE (arg_type
))
3722 /* Create a local variable that points to the constant pool. */
3725 cache_cpool_data_ref (void)
3730 tree d
= build_constant_data_ref (flag_indirect_classes
);
3731 tree cpool_ptr
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
3732 build_pointer_type (TREE_TYPE (d
)));
3733 java_add_local_var (cpool_ptr
);
3734 TREE_CONSTANT (cpool_ptr
) = 1;
3736 java_add_stmt (build2 (MODIFY_EXPR
, TREE_TYPE (cpool_ptr
),
3737 cpool_ptr
, build_address_of (d
)));
3738 cpool
= build1 (INDIRECT_REF
, TREE_TYPE (d
), cpool_ptr
);
3739 TREE_THIS_NOTRAP (cpool
) = 1;
3740 TYPE_CPOOL_DATA_REF (output_class
) = cpool
;
3744 #include "gt-java-expr.h"