1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC 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 2, or (at your option)
11 GNU CC 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 GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
34 #include "java-tree.h"
36 #include "java-opcodes.h"
38 #include "java-except.h"
44 static void flush_quick_stack
PARAMS ((void));
45 static void push_value
PARAMS ((tree
));
46 static tree pop_value
PARAMS ((tree
));
47 static void java_stack_swap
PARAMS ((void));
48 static void java_stack_dup
PARAMS ((int, int));
49 static void build_java_athrow
PARAMS ((tree
));
50 static void build_java_jsr
PARAMS ((tree
, tree
));
51 static void build_java_ret
PARAMS ((tree
));
52 static void expand_java_multianewarray
PARAMS ((tree
, int));
53 static void expand_java_arraystore
PARAMS ((tree
));
54 static void expand_java_arrayload
PARAMS ((tree
));
55 static void expand_java_array_length
PARAMS ((void));
56 static tree build_java_monitor
PARAMS ((tree
, tree
));
57 static void expand_java_pushc
PARAMS ((int, tree
));
58 static void expand_java_return
PARAMS ((tree
));
59 static void expand_java_NEW
PARAMS ((tree
));
60 static void expand_java_INSTANCEOF
PARAMS ((tree
));
61 static void expand_java_CHECKCAST
PARAMS ((tree
));
62 static void expand_iinc
PARAMS ((unsigned int, int, int));
63 static void expand_java_binop
PARAMS ((tree
, enum tree_code
));
64 static void note_label
PARAMS ((int, int));
65 static void expand_compare
PARAMS ((enum tree_code
, tree
, tree
, int));
66 static void expand_test
PARAMS ((enum tree_code
, tree
, int));
67 static void expand_cond
PARAMS ((enum tree_code
, tree
, int));
68 static void expand_java_goto
PARAMS ((int));
70 static void expand_java_call
PARAMS ((int, int));
71 static void expand_java_ret
PARAMS ((tree
));
73 static tree pop_arguments
PARAMS ((tree
));
74 static void expand_invoke
PARAMS ((int, int, int));
75 static void expand_java_field_op
PARAMS ((int, int, int));
76 static void java_push_constant_from_pool
PARAMS ((struct JCF
*, int));
77 static void java_stack_pop
PARAMS ((int));
78 static tree build_java_throw_out_of_bounds_exception
PARAMS ((tree
));
79 static tree build_java_check_indexed_type
PARAMS ((tree
, tree
));
80 static tree java_array_data_offset
PARAMS ((tree
));
81 static tree case_identity
PARAMS ((tree
, tree
));
83 static tree operand_type
[59];
84 extern struct obstack permanent_obstack
;
86 /* Set to non-zero value in order to emit class initilization code
87 before static field references. */
88 int always_initialize_class_p
;
91 init_expr_processing()
93 operand_type
[21] = operand_type
[54] = int_type_node
;
94 operand_type
[22] = operand_type
[55] = long_type_node
;
95 operand_type
[23] = operand_type
[56] = float_type_node
;
96 operand_type
[24] = operand_type
[57] = double_type_node
;
97 operand_type
[25] = operand_type
[58] = ptr_type_node
;
100 /* We store the stack state in two places:
101 Within a basic block, we use the quick_stack, which is a
102 pushdown list (TREE_LISTs) of expression nodes.
103 This is the top part of the stack; below that we use find_stack_slot.
104 At the end of a basic block, the quick_stack must be flushed
105 to the stack slot array (as handled by find_stack_slot).
106 Using quick_stack generates better code (especially when
107 compiled without optimization), because we do not have to
108 explicitly store and load trees to temporary variables.
110 If a variable is on the quick stack, it means the value of variable
111 when the quick stack was last flushed. Conceptually, flush_quick_stack
112 saves all the the quick_stack elements in parellel. However, that is
113 complicated, so it actually saves them (i.e. copies each stack value
114 to is home virtual register) from low indexes. This allows a quick_stack
115 element at index i (counting from the bottom of stack the) to references
116 slot virtuals for register that are >= i, but not those that are deeper.
117 This convention makes most operations easier. For example iadd works
118 even when the stack contains (reg[0], reg[1]): It results in the
119 stack containing (reg[0]+reg[1]), which is OK. However, some stack
120 operations are more complicated. For example dup given a stack
121 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
122 the convention, since stack value 1 would refer to a register with
123 lower index (reg[0]), which flush_quick_stack does not safely handle.
124 So dup cannot just add an extra element to the quick_stack, but iadd can.
127 tree quick_stack
= NULL_TREE
;
129 /* A free-list of unused permamnet TREE_LIST nodes. */
130 tree tree_list_free_list
= NULL_TREE
;
132 /* The stack pointer of the Java virtual machine.
133 This does include the size of the quick_stack. */
137 const unsigned char *linenumber_table
;
138 int linenumber_count
;
141 truthvalue_conversion (expr
)
144 /* It is simpler and generates better code to have only TRUTH_*_EXPR
145 or comparison expressions as truth values at this level.
147 This function should normally be identity for Java. */
149 switch (TREE_CODE (expr
))
152 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
153 case TRUTH_ANDIF_EXPR
:
154 case TRUTH_ORIF_EXPR
:
161 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
164 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
166 /* are these legal? XXX JH */
171 /* These don't change whether an object is non-zero or zero. */
172 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
175 /* Distribute the conversion into the arms of a COND_EXPR. */
176 return fold (build (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
177 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
178 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
181 /* If this is widening the argument, we can ignore it. */
182 if (TYPE_PRECISION (TREE_TYPE (expr
))
183 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
184 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
185 /* fall through to default */
188 return fold (build (NE_EXPR
, boolean_type_node
, expr
, boolean_false_node
));
192 #ifdef JAVA_USE_HANDLES
193 /* Given a pointer to a handle, get a pointer to an object. */
199 tree field
, handle_type
;
200 expr
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
201 handle_type
= TREE_TYPE (expr
);
202 field
= TYPE_FIELDS (handle_type
);
203 expr
= build (COMPONENT_REF
, TREE_TYPE (field
), expr
, field
);
208 /* Save any stack slots that happen to be in the quick_stack into their
209 home virtual register slots.
211 The copy order is from low stack index to high, to support the invariant
212 that the expression for a slot may contain decls for stack slots with
213 higher (or the same) index, but not lower. */
218 int stack_index
= stack_pointer
;
219 register tree prev
, cur
, next
;
221 /* First reverse the quick_stack, and count the number of slots it has. */
222 for (cur
= quick_stack
, prev
= NULL_TREE
; cur
!= NULL_TREE
; cur
= next
)
224 next
= TREE_CHAIN (cur
);
225 TREE_CHAIN (cur
) = prev
;
227 stack_index
-= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur
)));
231 while (quick_stack
!= NULL_TREE
)
234 tree node
= quick_stack
, type
;
235 quick_stack
= TREE_CHAIN (node
);
236 TREE_CHAIN (node
) = tree_list_free_list
;
237 tree_list_free_list
= node
;
238 node
= TREE_VALUE (node
);
239 type
= TREE_TYPE (node
);
241 decl
= find_stack_slot (stack_index
, type
);
243 expand_assignment (decl
, node
, 0, 0);
244 stack_index
+= 1 + TYPE_IS_WIDE (type
);
253 type
= promote_type (type
);
254 n_words
= 1 + TYPE_IS_WIDE (type
);
255 if (stack_pointer
+ n_words
> DECL_MAX_STACK (current_function_decl
))
256 fatal ("stack overflow");
257 stack_type_map
[stack_pointer
++] = type
;
259 while (--n_words
>= 0)
260 stack_type_map
[stack_pointer
++] = TYPE_SECOND
;
267 tree type
= TREE_TYPE (value
);
268 if (TYPE_PRECISION (type
) < 32 && INTEGRAL_TYPE_P (type
))
270 type
= promote_type (type
);
271 value
= convert (type
, value
);
274 if (tree_list_free_list
== NULL_TREE
)
275 quick_stack
= perm_tree_cons (NULL_TREE
, value
, quick_stack
);
278 tree node
= tree_list_free_list
;
279 tree_list_free_list
= TREE_CHAIN (tree_list_free_list
);
280 TREE_VALUE (node
) = value
;
281 TREE_CHAIN (node
) = quick_stack
;
286 /* Pop a type from the type stack.
287 TYPE is the expected type. Return the actual type, which must be
288 convertible to TYPE, otherwise NULL_TREE is returned. */
296 if (TREE_CODE (type
) == RECORD_TYPE
)
297 type
= promote_type (type
);
298 n_words
= 1 + TYPE_IS_WIDE (type
);
299 if (stack_pointer
< n_words
)
300 fatal ("stack underflow");
301 while (--n_words
> 0)
303 if (stack_type_map
[--stack_pointer
] != void_type_node
)
304 fatal ("Invalid multi-word value on type stack");
306 t
= stack_type_map
[--stack_pointer
];
307 if (type
== NULL_TREE
|| t
== type
)
309 if (INTEGRAL_TYPE_P (type
) && INTEGRAL_TYPE_P (t
)
310 && TYPE_PRECISION (type
) <= 32 && TYPE_PRECISION (t
) <= 32)
312 if (TREE_CODE (type
) == POINTER_TYPE
&& TREE_CODE (t
) == POINTER_TYPE
)
314 if (type
== ptr_type_node
|| type
== object_ptr_type_node
)
316 else if (t
== ptr_type_node
) /* Special case for null reference. */
318 else if (can_widen_reference_to (t
, type
))
320 /* This is a kludge, but matches what Sun's verifier does.
321 It can be tricked, but is safe as long as type errors
322 (i.e. interface method calls) are caught at run-time. */
323 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type
)))
324 && t
== object_ptr_type_node
)
330 /* Pop a type from the type stack.
331 TYPE is the expected type. Return the actual type, which must be
332 convertible to TYPE, otherwise call error. */
338 tree t
= pop_type_0 (type
);
341 error ("unexpected type on stack");
345 /* Return 1f if SOURCE_TYPE can be safely widened to TARGET_TYPE.
346 Handles array types and interfaces. */
349 can_widen_reference_to (source_type
, target_type
)
350 tree source_type
, target_type
;
352 if (source_type
== ptr_type_node
|| target_type
== object_ptr_type_node
)
355 /* Get rid of pointers */
356 if (TREE_CODE (source_type
) == POINTER_TYPE
)
357 source_type
= TREE_TYPE (source_type
);
358 if (TREE_CODE (target_type
) == POINTER_TYPE
)
359 target_type
= TREE_TYPE (target_type
);
361 if (source_type
== target_type
)
365 source_type
= HANDLE_TO_CLASS_TYPE (source_type
);
366 target_type
= HANDLE_TO_CLASS_TYPE (target_type
);
367 if (TYPE_ARRAY_P (source_type
) || TYPE_ARRAY_P (target_type
))
369 HOST_WIDE_INT source_length
, target_length
;
370 if (TYPE_ARRAY_P (source_type
) != TYPE_ARRAY_P (target_type
))
372 target_length
= java_array_type_length (target_type
);
373 if (target_length
>= 0)
375 source_length
= java_array_type_length (source_type
);
376 if (source_length
!= target_length
)
379 source_type
= TYPE_ARRAY_ELEMENT (source_type
);
380 target_type
= TYPE_ARRAY_ELEMENT (target_type
);
381 if (source_type
== target_type
)
383 if (TREE_CODE (source_type
) != POINTER_TYPE
384 || TREE_CODE (target_type
) != POINTER_TYPE
)
386 return can_widen_reference_to (source_type
, target_type
);
390 int source_depth
= class_depth (source_type
);
391 int target_depth
= class_depth (target_type
);
393 /* class_depth can return a negative depth if an error occurred */
394 if (source_depth
< 0 || target_depth
< 0)
397 if (CLASS_INTERFACE (TYPE_NAME (target_type
)))
399 /* target_type is OK if source_type or source_type ancestors
400 implement target_type. We handle multiple sub-interfaces */
402 tree basetype_vec
= TYPE_BINFO_BASETYPES (source_type
);
403 int n
= TREE_VEC_LENGTH (basetype_vec
), i
;
404 for (i
=0 ; i
< n
; i
++)
405 if (can_widen_reference_to
406 (TREE_TYPE (TREE_VEC_ELT (basetype_vec
, i
)),
413 for ( ; source_depth
> target_depth
; source_depth
--)
415 source_type
= TYPE_BINFO_BASETYPE (source_type
, 0);
417 return source_type
== target_type
;
426 type
= pop_type (type
);
429 tree node
= quick_stack
;
430 quick_stack
= TREE_CHAIN (quick_stack
);
431 TREE_CHAIN (node
) = tree_list_free_list
;
432 tree_list_free_list
= node
;
433 node
= TREE_VALUE (node
);
437 return find_stack_slot (stack_pointer
, promote_type (type
));
441 /* Pop and discrad the top COUNT stack slots. */
444 java_stack_pop (count
)
450 if (stack_pointer
== 0)
451 fatal ("stack underflow");
452 type
= stack_type_map
[stack_pointer
- 1];
453 if (type
== TYPE_SECOND
)
456 if (stack_pointer
== 1 || count
<= 0)
457 fatal ("stack underflow");
458 type
= stack_type_map
[stack_pointer
- 2];
460 val
= pop_value (type
);
465 /* Implement the 'swap' operator (to swap two top stack slots). */
474 if (stack_pointer
< 2
475 || (type1
= stack_type_map
[stack_pointer
- 1]) == TYPE_UNKNOWN
476 || (type2
= stack_type_map
[stack_pointer
- 2]) == TYPE_UNKNOWN
477 || type1
== TYPE_SECOND
|| type2
== TYPE_SECOND
478 || TYPE_IS_WIDE (type1
) || TYPE_IS_WIDE (type2
))
479 fatal ("bad stack swap");
481 flush_quick_stack ();
482 decl1
= find_stack_slot (stack_pointer
- 1, type1
);
483 decl2
= find_stack_slot (stack_pointer
- 2, type2
);
484 temp
= copy_to_reg (DECL_RTL (decl1
));
485 emit_move_insn (DECL_RTL (decl1
), DECL_RTL (decl2
));
486 emit_move_insn (DECL_RTL (decl2
), temp
);
487 stack_type_map
[stack_pointer
- 1] = type2
;
488 stack_type_map
[stack_pointer
- 2] = type1
;
492 java_stack_dup (size
, offset
)
495 int low_index
= stack_pointer
- size
- offset
;
498 error ("stack underflow - dup* operation");
500 flush_quick_stack ();
502 stack_pointer
+= size
;
503 dst_index
= stack_pointer
;
505 for (dst_index
= stack_pointer
; --dst_index
>= low_index
; )
508 int src_index
= dst_index
- size
;
509 if (src_index
< low_index
)
510 src_index
= dst_index
+ size
+ offset
;
511 type
= stack_type_map
[src_index
];
512 if (type
== TYPE_SECOND
)
514 if (src_index
<= low_index
)
515 fatal ("dup operation splits 64-bit number");
516 stack_type_map
[dst_index
] = type
;
517 src_index
--; dst_index
--;
518 type
= stack_type_map
[src_index
];
519 if (! TYPE_IS_WIDE (type
))
520 fatal ("internal error - dup operation");
522 else if (TYPE_IS_WIDE (type
))
523 fatal ("internal error - dup operation");
524 if (src_index
!= dst_index
)
526 tree src_decl
= find_stack_slot (src_index
, type
);
527 tree dst_decl
= find_stack_slot (dst_index
, type
);
528 emit_move_insn (DECL_RTL (dst_decl
), DECL_RTL (src_decl
));
529 stack_type_map
[dst_index
] = type
;
534 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
538 build_java_athrow (node
)
543 call
= build (CALL_EXPR
,
545 build_address_of (throw_node
[exceptions_via_longjmp
? 1 : 0]),
546 build_tree_list (NULL_TREE
, node
),
548 TREE_SIDE_EFFECTS (call
) = 1;
549 expand_expr_stmt (call
);
550 java_stack_pop (stack_pointer
);
553 /* Implementation for jsr/ret */
556 build_java_jsr (where
, ret
)
560 tree ret_label
= fold (build1 (ADDR_EXPR
, return_address_type_node
, ret
));
561 push_value (ret_label
);
562 flush_quick_stack ();
563 emit_jump (label_rtx (where
));
568 build_java_ret (location
)
571 expand_computed_goto (location
);
574 /* Implementation of operations on array: new, load, store, length */
576 /* Array core info access macros */
578 #define JAVA_ARRAY_LENGTH_OFFSET(A) \
579 byte_position (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (TREE_TYPE (A)))))
582 decode_newarray_type (atype
)
587 case 4: return boolean_type_node
;
588 case 5: return char_type_node
;
589 case 6: return float_type_node
;
590 case 7: return double_type_node
;
591 case 8: return byte_type_node
;
592 case 9: return short_type_node
;
593 case 10: return int_type_node
;
594 case 11: return long_type_node
;
595 default: return NULL_TREE
;
599 /* Map primitive type to the code used by OPCODE_newarray. */
602 encode_newarray_type (type
)
605 if (type
== boolean_type_node
)
607 else if (type
== char_type_node
)
609 else if (type
== float_type_node
)
611 else if (type
== double_type_node
)
613 else if (type
== byte_type_node
)
615 else if (type
== short_type_node
)
617 else if (type
== int_type_node
)
619 else if (type
== long_type_node
)
622 fatal ("Can't compute type code - patch_newarray");
625 /* Build a call to _Jv_ThrowBadArrayIndex(), the
626 ArrayIndexOfBoundsException exception handler. */
629 build_java_throw_out_of_bounds_exception (index
)
632 tree node
= build (CALL_EXPR
, int_type_node
,
633 build_address_of (soft_badarrayindex_node
),
634 build_tree_list (NULL_TREE
, index
), NULL_TREE
);
635 TREE_SIDE_EFFECTS (node
) = 1; /* Allows expansion within ANDIF */
639 /* Return the length of an array. Doesn't perform any checking on the nature
640 or value of the array NODE. May be used to implement some bytecodes. */
643 build_java_array_length_access (node
)
646 tree type
= TREE_TYPE (node
);
647 HOST_WIDE_INT length
;
648 if (!is_array_type_p (type
))
649 fatal ("array length on a non-array reference");
650 length
= java_array_type_length (type
);
652 return build_int_2 (length
, 0);
653 return fold (build1 (INDIRECT_REF
,
655 fold (build (PLUS_EXPR
, ptr_type_node
,
657 JAVA_ARRAY_LENGTH_OFFSET(node
)))));
660 /* Optionally checks an array against the NULL pointer, eventually throwing a
661 NullPointerException. It could replace signal handling, but tied to NULL.
662 ARG1: the pointer to check, ARG2: the expression to use if
663 the pointer is non-null and ARG3 the type that should be returned. */
666 build_java_arraynull_check (node
, expr
, type
)
667 tree node ATTRIBUTE_UNUSED
;
669 tree type ATTRIBUTE_UNUSED
;
672 static int java_array_access_throws_null_exception
= 0;
674 if (java_array_access_throws_null_exception
)
675 return (build (COND_EXPR
,
677 build (EQ_EXPR
, int_type_node
, node
, null_pointer_node
),
678 build_java_athrow (node
), expr
));
685 java_array_data_offset (array
)
688 tree array_type
= TREE_TYPE (TREE_TYPE (array
));
689 tree data_fld
= TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type
)));
691 if (data_fld
== NULL_TREE
)
692 return size_in_bytes (array_type
);
694 return byte_position (data_fld
);
697 /* Implement array indexing (either as l-value or r-value).
698 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
699 Optionally performs bounds checking and/or test to NULL.
700 At this point, ARRAY should have been verified as an array. */
703 build_java_arrayaccess (array
, type
, index
)
704 tree array
, type
, index
;
706 tree arith
, node
, throw = NULL_TREE
;
708 arith
= fold (build (PLUS_EXPR
, int_type_node
,
709 java_array_data_offset (array
),
710 fold (build (MULT_EXPR
, int_type_node
,
711 index
, size_in_bytes(type
)))));
713 if (flag_bounds_check
)
716 * (unsigned jint) INDEX >= (unsigned jint) LEN
717 * && throw ArrayIndexOutOfBoundsException.
718 * Note this is equivalent to and more efficient than:
719 * INDEX < 0 || INDEX >= LEN && throw ... */
721 tree len
= build_java_array_length_access (array
);
722 TREE_TYPE (len
) = unsigned_int_type_node
;
723 test
= fold (build (GE_EXPR
, boolean_type_node
,
724 convert (unsigned_int_type_node
, index
),
726 if (! integer_zerop (test
))
728 throw = build (TRUTH_ANDIF_EXPR
, int_type_node
, test
,
729 build_java_throw_out_of_bounds_exception (index
));
730 /* allows expansion within COMPOUND */
731 TREE_SIDE_EFFECTS( throw ) = 1;
735 node
= build1 (INDIRECT_REF
, type
,
736 fold (build (PLUS_EXPR
, ptr_type_node
,
738 (throw ? build (COMPOUND_EXPR
, int_type_node
,
742 return (fold (build_java_arraynull_check (array
, node
, type
)));
745 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
746 ARRAY_NODE. This function is used to retrieve something less vague than
747 a pointer type when indexing the first dimension of something like [[<t>.
748 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
750 As a side effect, it also makes sure that ARRAY_NODE is an array. */
753 build_java_check_indexed_type (array_node
, indexed_type
)
759 if (!is_array_type_p (TREE_TYPE (array_node
)))
760 fatal ("array indexing on a non-array reference");
762 elt_type
= (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node
))));
764 if (indexed_type
== ptr_type_node
)
765 return promote_type (elt_type
);
767 /* BYTE/BOOLEAN store and load are used for both type */
768 if (indexed_type
== byte_type_node
&& elt_type
== boolean_type_node
)
769 return boolean_type_node
;
771 if (indexed_type
!= elt_type
)
772 fatal ("type array element mismatch");
777 /* newarray triggers a call to _Jv_NewArray. This function should be called
778 with an integer code (the type of array to create) and get from the stack
779 the size of the dimmension. */
782 build_newarray (atype_value
, length
)
787 = build_java_array_type (decode_newarray_type (atype_value
),
788 host_integerp (length
, 0) == INTEGER_CST
789 ? tree_low_cst (length
, 0) : -1);
791 return build (CALL_EXPR
, promote_type (type
),
792 build_address_of (soft_newarray_node
),
793 tree_cons (NULL_TREE
,
794 build_int_2 (atype_value
, 0),
795 build_tree_list (NULL_TREE
, length
)),
799 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
803 build_anewarray (class_type
, length
)
808 = build_java_array_type (class_type
,
809 host_integerp (length
, 0)
810 ? tree_low_cst (length
, 0) : -1);
812 return build (CALL_EXPR
, promote_type (type
),
813 build_address_of (soft_anewarray_node
),
814 tree_cons (NULL_TREE
, length
,
815 tree_cons (NULL_TREE
, build_class_ref (class_type
),
816 build_tree_list (NULL_TREE
,
817 null_pointer_node
))),
821 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
824 build_new_array (type
, length
)
828 if (JPRIMITIVE_TYPE_P (type
))
829 return build_newarray (encode_newarray_type (type
), length
);
831 return build_anewarray (TREE_TYPE (type
), length
);
834 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
835 class pointer, a number of dimensions and the matching number of
836 dimensions. The argument list is NULL terminated. */
839 expand_java_multianewarray (class_type
, ndim
)
844 tree args
= build_tree_list( NULL_TREE
, null_pointer_node
);
846 for( i
= 0; i
< ndim
; i
++ )
847 args
= tree_cons (NULL_TREE
, pop_value (int_type_node
), args
);
849 push_value (build (CALL_EXPR
,
850 promote_type (class_type
),
851 build_address_of (soft_multianewarray_node
),
852 tree_cons (NULL_TREE
, build_class_ref (class_type
),
853 tree_cons (NULL_TREE
,
854 build_int_2 (ndim
, 0), args
)),
858 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
859 ARRAY is an array type. May expand some bound checking and NULL
860 pointer checking. RHS_TYPE_NODE we are going to store. In the case
861 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
862 INT. In those cases, we make the convertion.
864 if ARRAy is a reference type, the assignment is checked at run-time
865 to make sure that the RHS can be assigned to the array element
866 type. It is not necessary to generate this code if ARRAY is final. */
869 expand_java_arraystore (rhs_type_node
)
872 tree rhs_node
= pop_value ((INTEGRAL_TYPE_P (rhs_type_node
)
873 && TYPE_PRECISION (rhs_type_node
) <= 32) ?
874 int_type_node
: rhs_type_node
);
875 tree index
= pop_value (int_type_node
);
876 tree array
= pop_value (ptr_type_node
);
878 rhs_type_node
= build_java_check_indexed_type (array
, rhs_type_node
);
880 flush_quick_stack ();
882 index
= save_expr (index
);
883 array
= save_expr (array
);
885 if (TREE_CODE (rhs_type_node
) == POINTER_TYPE
)
887 tree check
= build (CALL_EXPR
, void_type_node
,
888 build_address_of (soft_checkarraystore_node
),
889 tree_cons (NULL_TREE
, array
,
890 build_tree_list (NULL_TREE
, rhs_node
)),
892 TREE_SIDE_EFFECTS (check
) = 1;
893 expand_expr_stmt (check
);
896 expand_assignment (build_java_arrayaccess (array
,
902 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
903 sure that LHS is an array type. May expand some bound checking and NULL
905 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
906 BOOLEAN/SHORT, we push a promoted type back to the stack.
910 expand_java_arrayload (lhs_type_node
)
914 tree index_node
= pop_value (int_type_node
);
915 tree array_node
= pop_value (ptr_type_node
);
917 index_node
= save_expr (index_node
);
918 array_node
= save_expr (array_node
);
919 lhs_type_node
= build_java_check_indexed_type (array_node
, lhs_type_node
);
921 load_node
= build_java_arrayaccess (array_node
,
925 if (INTEGRAL_TYPE_P (lhs_type_node
) && TYPE_PRECISION (lhs_type_node
) <= 32)
926 load_node
= fold (build1 (NOP_EXPR
, int_type_node
, load_node
));
927 push_value (load_node
);
930 /* Expands .length. Makes sure that we deal with and array and may expand
931 a NULL check on the array object. */
934 expand_java_array_length ()
936 tree array
= pop_value (ptr_type_node
);
937 tree length
= build_java_array_length_access (array
);
939 push_value (build_java_arraynull_check (array
, length
, int_type_node
));
942 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
943 either soft_monitorenter_node or soft_monitorexit_node. */
946 build_java_monitor (call
, object
)
950 return (build (CALL_EXPR
,
952 build_address_of (call
),
953 build_tree_list (NULL_TREE
, object
),
957 /* Emit code for one of the PUSHC instructions. */
960 expand_java_pushc (ival
, type
)
965 if (type
== ptr_type_node
&& ival
== 0)
966 value
= null_pointer_node
;
967 else if (type
== int_type_node
|| type
== long_type_node
)
969 value
= build_int_2 (ival
, ival
< 0 ? -1 : 0);
970 TREE_TYPE (value
) = type
;
972 else if (type
== float_type_node
|| type
== double_type_node
)
975 #ifdef REAL_ARITHMETIC
976 REAL_VALUE_FROM_INT (x
, ival
, 0, TYPE_MODE (type
));
980 value
= build_real (type
, x
);
983 fatal ("internal error in expand_java_pushc");
987 #ifndef INT_TYPE_SIZE
988 #define INT_TYPE_SIZE BITS_PER_WORD
992 expand_java_return (type
)
995 if (type
== void_type_node
)
996 expand_null_return ();
999 tree retval
= pop_value (type
);
1000 tree res
= DECL_RESULT (current_function_decl
);
1001 retval
= build (MODIFY_EXPR
, TREE_TYPE (res
), res
, retval
);
1003 /* Handle the situation where the native integer type is smaller
1004 than the JVM integer. It can happen for many cross compilers.
1005 The whole if expression just goes away if INT_TYPE_SIZE < 32
1007 if (INT_TYPE_SIZE
< 32
1008 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res
)))
1009 < GET_MODE_SIZE (TYPE_MODE (type
))))
1010 retval
= build1(NOP_EXPR
, TREE_TYPE(res
), retval
);
1012 TREE_SIDE_EFFECTS (retval
) = 1;
1013 expand_return (retval
);
1018 build_address_of (value
)
1021 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (value
)), value
);
1025 expand_java_NEW (type
)
1028 if (! CLASS_LOADED_P (type
))
1029 load_class (type
, 1);
1030 safe_layout_class (type
);
1031 push_value (build (CALL_EXPR
, promote_type (type
),
1032 build_address_of (alloc_object_node
),
1033 tree_cons (NULL_TREE
, build_class_ref (type
),
1034 build_tree_list (NULL_TREE
,
1035 size_in_bytes (type
))),
1039 /* This returns an expression which will extract the class of an
1043 build_get_class (value
)
1046 tree class_field
= lookup_field (&dtable_type
, get_identifier ("class"));
1047 tree vtable_field
= lookup_field (&object_type_node
,
1048 get_identifier ("vtable"));
1049 return build (COMPONENT_REF
, class_ptr_type
,
1050 build1 (INDIRECT_REF
, dtable_type
,
1051 build (COMPONENT_REF
, dtable_ptr_type
,
1052 build1 (INDIRECT_REF
, object_type_node
, value
),
1057 /* This builds the tree representation of the `instanceof' operator.
1058 It tries various tricks to optimize this in cases where types are
1062 build_instanceof (value
, type
)
1066 tree itype
= TREE_TYPE (TREE_TYPE (soft_instanceof_node
));
1067 tree valtype
= TREE_TYPE (TREE_TYPE (value
));
1068 tree valclass
= TYPE_NAME (valtype
);
1071 /* When compiling from bytecode, we need to ensure that TYPE has
1073 if (CLASS_P (type
) && ! CLASS_LOADED_P (type
))
1075 load_class (type
, 1);
1076 safe_layout_class (type
);
1077 if (! TYPE_SIZE (type
) || TREE_CODE (TYPE_SIZE (type
)) == ERROR_MARK
)
1078 return error_mark_node
;
1080 klass
= TYPE_NAME (type
);
1082 if (type
== object_type_node
|| inherits_from_p (valtype
, type
))
1084 /* Anything except `null' is an instance of Object. Likewise,
1085 if the object is known to be an instance of the class, then
1086 we only need to check for `null'. */
1087 expr
= build (COND_EXPR
, itype
,
1089 boolean_true_node
, boolean_false_node
);
1091 else if (DECL_P (klass
) && DECL_P (valclass
)
1092 && ! CLASS_INTERFACE (valclass
)
1093 && ! CLASS_INTERFACE (klass
)
1094 && ! inherits_from_p (type
, valtype
)
1095 && (CLASS_FINAL (klass
)
1096 || ! inherits_from_p (valtype
, type
)))
1098 /* The classes are from different branches of the derivation
1099 tree, so we immediately know the answer. */
1100 expr
= boolean_false_node
;
1102 else if (DECL_P (klass
) && CLASS_FINAL (klass
))
1104 tree save
= save_expr (value
);
1105 expr
= build (COND_EXPR
, itype
,
1107 build (EQ_EXPR
, itype
,
1108 build_get_class (save
),
1109 build_class_ref (type
)),
1110 boolean_false_node
);
1114 expr
= build (CALL_EXPR
, itype
,
1115 build_address_of (soft_instanceof_node
),
1116 tree_cons (NULL_TREE
, value
,
1117 build_tree_list (NULL_TREE
,
1118 build_class_ref (type
))),
1121 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (value
);
1126 expand_java_INSTANCEOF (type
)
1129 tree value
= pop_value (object_ptr_type_node
);
1130 value
= build_instanceof (value
, type
);
1135 expand_java_CHECKCAST (type
)
1138 tree value
= pop_value (ptr_type_node
);
1139 value
= build (CALL_EXPR
, promote_type (type
),
1140 build_address_of (soft_checkcast_node
),
1141 tree_cons (NULL_TREE
, build_class_ref (type
),
1142 build_tree_list (NULL_TREE
, value
)),
1148 expand_iinc (local_var_index
, ival
, pc
)
1149 unsigned int local_var_index
;
1153 tree local_var
, res
;
1154 tree constant_value
;
1156 flush_quick_stack ();
1157 local_var
= find_local_variable (local_var_index
, int_type_node
, pc
);
1158 constant_value
= build_int_2 (ival
, ival
< 0 ? -1 : 0);
1159 res
= fold (build (PLUS_EXPR
, int_type_node
, local_var
, constant_value
));
1160 expand_assignment (local_var
, res
, 0, 0);
1165 build_java_soft_divmod (op
, type
, op1
, op2
)
1167 tree type
, op1
, op2
;
1170 tree arg1
= convert (type
, op1
);
1171 tree arg2
= convert (type
, op2
);
1173 if (type
== int_type_node
)
1177 case TRUNC_DIV_EXPR
:
1178 call
= soft_idiv_node
;
1180 case TRUNC_MOD_EXPR
:
1181 call
= soft_irem_node
;
1187 else if (type
== long_type_node
)
1191 case TRUNC_DIV_EXPR
:
1192 call
= soft_ldiv_node
;
1194 case TRUNC_MOD_EXPR
:
1195 call
= soft_lrem_node
;
1203 fatal ("Internal compiler error in build_java_soft_divmod");
1205 call
= build (CALL_EXPR
, type
,
1206 build_address_of (call
),
1207 tree_cons (NULL_TREE
, arg1
,
1208 build_tree_list (NULL_TREE
, arg2
)),
1215 build_java_binop (op
, type
, arg1
, arg2
)
1217 tree type
, arg1
, arg2
;
1224 tree u_type
= unsigned_type (type
);
1225 arg1
= convert (u_type
, arg1
);
1226 arg1
= build_java_binop (RSHIFT_EXPR
, u_type
, arg1
, arg2
);
1227 return convert (type
, arg1
);
1231 mask
= build_int_2 (TYPE_PRECISION (TREE_TYPE (arg1
)) - 1, 0);
1232 arg2
= fold (build (BIT_AND_EXPR
, int_type_node
, arg2
, mask
));
1235 case COMPARE_L_EXPR
: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1236 case COMPARE_G_EXPR
: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1237 arg1
= save_expr (arg1
); arg2
= save_expr (arg2
);
1239 tree ifexp1
= fold ( build (op
== COMPARE_L_EXPR
? GT_EXPR
: LT_EXPR
,
1240 boolean_type_node
, arg1
, arg2
));
1241 tree ifexp2
= fold ( build (EQ_EXPR
, boolean_type_node
, arg1
, arg2
));
1242 tree second_compare
= fold (build (COND_EXPR
, int_type_node
,
1243 ifexp2
, integer_zero_node
,
1244 op
== COMPARE_L_EXPR
1245 ? integer_negative_one_node
1246 : integer_one_node
));
1247 return fold (build (COND_EXPR
, int_type_node
, ifexp1
,
1248 op
== COMPARE_L_EXPR
? integer_one_node
1249 : integer_negative_one_node
,
1253 arg1
= save_expr (arg1
); arg2
= save_expr (arg2
);
1255 tree ifexp1
= fold ( build (LT_EXPR
, boolean_type_node
, arg1
, arg2
));
1256 tree ifexp2
= fold ( build (GT_EXPR
, boolean_type_node
, arg1
, arg2
));
1257 tree second_compare
= fold ( build (COND_EXPR
, int_type_node
,
1258 ifexp2
, integer_one_node
,
1259 integer_zero_node
));
1260 return fold (build (COND_EXPR
, int_type_node
,
1261 ifexp1
, integer_negative_one_node
, second_compare
));
1263 case TRUNC_DIV_EXPR
:
1264 case TRUNC_MOD_EXPR
:
1265 if (TREE_CODE (type
) == REAL_TYPE
1266 && op
== TRUNC_MOD_EXPR
)
1269 if (type
!= double_type_node
)
1271 arg1
= convert (double_type_node
, arg1
);
1272 arg2
= convert (double_type_node
, arg2
);
1274 call
= build (CALL_EXPR
, double_type_node
,
1275 build_address_of (soft_fmod_node
),
1276 tree_cons (NULL_TREE
, arg1
,
1277 build_tree_list (NULL_TREE
, arg2
)),
1279 if (type
!= double_type_node
)
1280 call
= convert (type
, call
);
1284 if (TREE_CODE (type
) == INTEGER_TYPE
1285 && flag_use_divide_subroutine
1286 && ! flag_syntax_only
)
1287 return build_java_soft_divmod (op
, type
, arg1
, arg2
);
1292 return fold (build (op
, type
, arg1
, arg2
));
1296 expand_java_binop (type
, op
)
1297 tree type
; enum tree_code op
;
1307 rtype
= int_type_node
;
1308 rarg
= pop_value (rtype
);
1311 rarg
= pop_value (rtype
);
1313 larg
= pop_value (ltype
);
1314 push_value (build_java_binop (op
, type
, larg
, rarg
));
1317 /* Lookup the field named NAME in *TYPEP or its super classes.
1318 If not found, return NULL_TREE.
1319 (If the *TYPEP is not found, or if the field reference is
1320 ambiguous, return error_mark_node.)
1321 If found, return the FIELD_DECL, and set *TYPEP to the
1322 class containing the field. */
1325 lookup_field (typep
, name
)
1329 if (CLASS_P (*typep
) && !CLASS_LOADED_P (*typep
))
1331 load_class (*typep
, 1);
1332 safe_layout_class (*typep
);
1333 if (!TYPE_SIZE (*typep
) || TREE_CODE (TYPE_SIZE (*typep
)) == ERROR_MARK
)
1334 return error_mark_node
;
1338 tree field
, basetype_vec
;
1342 for (field
= TYPE_FIELDS (*typep
); field
; field
= TREE_CHAIN (field
))
1343 if (DECL_NAME (field
) == name
)
1346 /* If *typep is an innerclass, lookup the field in its enclosing
1348 if (INNER_CLASS_TYPE_P (*typep
))
1350 tree outer_type
= TREE_TYPE (DECL_CONTEXT (TYPE_NAME (*typep
)));
1352 if ((field
= lookup_field (&outer_type
, name
)))
1356 /* Process implemented interfaces. */
1357 basetype_vec
= TYPE_BINFO_BASETYPES (*typep
);
1358 n
= TREE_VEC_LENGTH (basetype_vec
);
1359 save_field
= NULL_TREE
;
1360 for (i
= 0; i
< n
; i
++)
1362 tree t
= BINFO_TYPE (TREE_VEC_ELT (basetype_vec
, i
));
1363 if ((field
= lookup_field (&t
, name
)))
1365 if (save_field
== field
)
1367 if (save_field
== NULL_TREE
)
1371 tree i1
= DECL_CONTEXT (save_field
);
1372 tree i2
= DECL_CONTEXT (field
);
1373 error ("reference `%s' is ambiguous: appears in interface `%s' and interface `%s'",
1374 IDENTIFIER_POINTER (name
),
1375 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1
))),
1376 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2
))));
1377 return error_mark_node
;
1382 if (save_field
!= NULL_TREE
)
1385 *typep
= CLASSTYPE_SUPER (*typep
);
1390 /* Look up the field named NAME in object SELF_VALUE,
1391 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1392 SELF_VALUE is NULL_TREE if looking for a static field. */
1395 build_field_ref (self_value
, self_class
, name
)
1396 tree self_value
, self_class
, name
;
1398 tree base_class
= self_class
;
1399 tree field_decl
= lookup_field (&base_class
, name
);
1400 if (field_decl
== NULL_TREE
)
1402 error ("field `%s' not found", IDENTIFIER_POINTER (name
));
1403 return error_mark_node
;
1405 if (self_value
== NULL_TREE
)
1407 return build_static_field_ref (field_decl
);
1411 tree base_handle_type
= promote_type (base_class
);
1412 if (base_handle_type
!= TREE_TYPE (self_value
))
1413 self_value
= fold (build1 (NOP_EXPR
, base_handle_type
, self_value
));
1414 #ifdef JAVA_USE_HANDLES
1415 self_value
= unhand_expr (self_value
);
1417 self_value
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (self_value
)),
1419 return fold (build (COMPONENT_REF
, TREE_TYPE (field_decl
),
1420 self_value
, field_decl
));
1430 ASM_GENERATE_INTERNAL_LABEL(buf
, "LJpc=", pc
);
1431 name
= get_identifier (buf
);
1432 if (IDENTIFIER_LOCAL_VALUE (name
))
1433 return IDENTIFIER_LOCAL_VALUE (name
);
1436 /* The type of the address of a label is return_address_type_node. */
1437 tree decl
= create_label_decl (name
);
1438 LABEL_PC (decl
) = pc
;
1440 return pushdecl (decl
);
1444 /* Generate a unique name for the purpose of loops and switches
1445 labels, and try-catch-finally blocks label or temporary variables. */
1450 static int l_number
= 0;
1452 ASM_GENERATE_INTERNAL_LABEL(buff
, "LJv", l_number
);
1454 return get_identifier (buff
);
1458 create_label_decl (name
)
1462 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1463 decl
= build_decl (LABEL_DECL
, name
,
1464 TREE_TYPE (return_address_type_node
));
1466 DECL_CONTEXT (decl
) = current_function_decl
;
1467 DECL_IGNORED_P (decl
) = 1;
1471 /* This maps a bytecode offset (PC) to various flags. */
1472 char *instruction_bits
;
1475 note_label (current_pc
, target_pc
)
1476 int current_pc ATTRIBUTE_UNUSED
, target_pc
;
1478 lookup_label (target_pc
);
1479 instruction_bits
[target_pc
] |= BCODE_JUMP_TARGET
;
1482 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1483 where CONDITION is one of one the compare operators. */
1486 expand_compare (condition
, value1
, value2
, target_pc
)
1487 enum tree_code condition
;
1488 tree value1
, value2
;
1491 tree target
= lookup_label (target_pc
);
1492 tree cond
= fold (build (condition
, boolean_type_node
, value1
, value2
));
1493 expand_start_cond (truthvalue_conversion (cond
), 0);
1494 expand_goto (target
);
1498 /* Emit code for a TEST-type opcode. */
1501 expand_test (condition
, type
, target_pc
)
1502 enum tree_code condition
;
1506 tree value1
, value2
;
1507 flush_quick_stack ();
1508 value1
= pop_value (type
);
1509 value2
= (type
== ptr_type_node
) ? null_pointer_node
: integer_zero_node
;
1510 expand_compare (condition
, value1
, value2
, target_pc
);
1513 /* Emit code for a COND-type opcode. */
1516 expand_cond (condition
, type
, target_pc
)
1517 enum tree_code condition
;
1521 tree value1
, value2
;
1522 flush_quick_stack ();
1523 /* note: pop values in opposite order */
1524 value2
= pop_value (type
);
1525 value1
= pop_value (type
);
1526 /* Maybe should check value1 and value2 for type compatibility ??? */
1527 expand_compare (condition
, value1
, value2
, target_pc
);
1531 expand_java_goto (target_pc
)
1534 tree target_label
= lookup_label (target_pc
);
1535 flush_quick_stack ();
1536 expand_goto (target_label
);
1541 expand_java_call (target_pc
, return_address
)
1542 int target_pc
, return_address
;
1544 tree target_label
= lookup_label (target_pc
);
1545 tree value
= build_int_2 (return_address
, return_address
< 0 ? -1 : 0);
1547 flush_quick_stack ();
1548 expand_goto (target_label
);
1552 expand_java_ret (return_address
)
1553 tree return_address ATTRIBUTE_UNUSED
;
1555 warning ("ret instruction not implemented");
1557 tree target_label
= lookup_label (target_pc
);
1558 flush_quick_stack ();
1559 expand_goto (target_label
);
1564 /* Recursive helper function to pop argument types during verifiation. */
1567 pop_argument_types (arg_types
)
1570 if (arg_types
== end_params_node
)
1572 if (TREE_CODE (arg_types
) == TREE_LIST
)
1574 pop_argument_types (TREE_CHAIN (arg_types
));
1575 pop_type (TREE_VALUE (arg_types
));
1582 pop_arguments (arg_types
)
1585 if (arg_types
== end_params_node
)
1587 if (TREE_CODE (arg_types
) == TREE_LIST
)
1589 tree tail
= pop_arguments (TREE_CHAIN (arg_types
));
1590 tree type
= TREE_VALUE (arg_types
);
1591 tree arg
= pop_value (type
);
1592 if (PROMOTE_PROTOTYPES
1593 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
)
1594 && INTEGRAL_TYPE_P (type
))
1595 arg
= convert (integer_type_node
, arg
);
1596 return tree_cons (NULL_TREE
, arg
, tail
);
1601 /* Build an expression to initialize the class CLAS.
1602 if EXPR is non-NULL, returns an expression to first call the initializer
1603 (if it is needed) and then calls EXPR. */
1606 build_class_init (clas
, expr
)
1610 struct init_test_hash_entry
*ite
;
1611 if (inherits_from_p (current_class
, clas
))
1614 if (always_initialize_class_p
)
1616 init
= build (CALL_EXPR
, void_type_node
,
1617 build_address_of (soft_initclass_node
),
1618 build_tree_list (NULL_TREE
, build_class_ref (clas
)),
1620 TREE_SIDE_EFFECTS (init
) = 1;
1624 ite
= (struct init_test_hash_entry
*)
1625 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl
),
1626 (const hash_table_key
) clas
,
1629 if (ite
->init_test_decl
== 0)
1630 ite
->init_test_decl
= build_decl (VAR_DECL
, NULL_TREE
,
1632 /* Tell the check-init code to ignore this decl. */
1633 DECL_BIT_INDEX(ite
->init_test_decl
) = -1;
1635 init
= build (CALL_EXPR
, void_type_node
,
1636 build_address_of (soft_initclass_node
),
1637 build_tree_list (NULL_TREE
, build_class_ref (clas
)),
1639 TREE_SIDE_EFFECTS (init
) = 1;
1640 call
= build (COMPOUND_EXPR
, TREE_TYPE (expr
), init
,
1641 build (MODIFY_EXPR
, boolean_type_node
,
1642 ite
->init_test_decl
, boolean_true_node
));
1643 TREE_SIDE_EFFECTS (call
) = 1;
1644 init
= build (COND_EXPR
, void_type_node
,
1645 build (EQ_EXPR
, boolean_type_node
,
1646 ite
->init_test_decl
, boolean_false_node
),
1647 call
, integer_zero_node
);
1648 TREE_SIDE_EFFECTS (init
) = 1;
1651 if (expr
!= NULL_TREE
)
1653 expr
= build (COMPOUND_EXPR
, TREE_TYPE (expr
), init
, expr
);
1654 TREE_SIDE_EFFECTS (expr
) = 1;
1660 static tree methods_ident
= NULL_TREE
;
1661 static tree ncode_ident
= NULL_TREE
;
1662 tree dtable_ident
= NULL_TREE
;
1665 build_known_method_ref (method
, method_type
, self_type
, method_signature
, arg_list
)
1666 tree method
, method_type ATTRIBUTE_UNUSED
, self_type
,
1667 method_signature ATTRIBUTE_UNUSED
, arg_list ATTRIBUTE_UNUSED
;
1670 if (is_compiled_class (self_type
))
1672 make_decl_rtl (method
, NULL
, 1);
1673 func
= build1 (ADDR_EXPR
, method_ptr_type_node
, method
);
1677 /* We don't know whether the method has been (statically) compiled.
1678 Compile this code to get a reference to the method's code:
1680 SELF_TYPE->methods[METHOD_INDEX].ncode
1682 This is guaranteed to work (assuming SELF_TYPE has
1683 been initialized), since if the method is not compiled yet,
1684 its ncode points to a trampoline that forces compilation. */
1686 int method_index
= 0;
1688 tree ref
= build_class_ref (self_type
);
1689 ref
= build1 (INDIRECT_REF
, class_type_node
, ref
);
1690 if (ncode_ident
== NULL_TREE
)
1691 ncode_ident
= get_identifier ("ncode");
1692 if (methods_ident
== NULL_TREE
)
1693 methods_ident
= get_identifier ("methods");
1694 ref
= build (COMPONENT_REF
, method_ptr_type_node
, ref
,
1695 lookup_field (&class_type_node
, methods_ident
));
1696 for (meth
= TYPE_METHODS (CLASS_TO_HANDLE_TYPE (self_type
));
1697 ; meth
= TREE_CHAIN (meth
))
1701 if (meth
== NULL_TREE
)
1702 fatal ("method '%s' not found in class",
1703 IDENTIFIER_POINTER (DECL_NAME (method
)));
1706 method_index
*= int_size_in_bytes (method_type_node
);
1707 ref
= fold (build (PLUS_EXPR
, method_ptr_type_node
,
1708 ref
, build_int_2 (method_index
, 0)));
1709 ref
= build1 (INDIRECT_REF
, method_type_node
, ref
);
1710 func
= build (COMPONENT_REF
, nativecode_ptr_type_node
,
1712 lookup_field (&method_type_node
, ncode_ident
));
1718 invoke_build_dtable (is_invoke_interface
, arg_list
)
1719 int is_invoke_interface
;
1722 tree dtable
, objectref
;
1724 TREE_VALUE (arg_list
) = save_expr (TREE_VALUE (arg_list
));
1726 /* If we're dealing with interfaces and if the objectref
1727 argument is an array then get the dispatch table of the class
1728 Object rather than the one from the objectref. */
1729 objectref
= (is_invoke_interface
1730 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list
))) ?
1731 object_type_node
: TREE_VALUE (arg_list
));
1733 if (dtable_ident
== NULL_TREE
)
1734 dtable_ident
= get_identifier ("vtable");
1735 dtable
= build1 (INDIRECT_REF
, object_type_node
, objectref
);
1736 dtable
= build (COMPONENT_REF
, dtable_ptr_type
, dtable
,
1737 lookup_field (&object_type_node
, dtable_ident
));
1743 build_invokevirtual (dtable
, method
)
1744 tree dtable
, method
;
1747 tree nativecode_ptr_ptr_type_node
1748 = build_pointer_type (nativecode_ptr_type_node
);
1749 tree method_index
= convert (sizetype
, DECL_VINDEX (method
));
1751 /* Add one to skip "class" field of dtable, and one to skip unused
1752 vtable entry (for C++ compatibility). */
1753 method_index
= size_binop (PLUS_EXPR
, method_index
, size_int (2));
1754 method_index
= size_binop (MULT_EXPR
, method_index
,
1755 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node
));
1756 func
= fold (build (PLUS_EXPR
, nativecode_ptr_ptr_type_node
, dtable
,
1757 convert (nativecode_ptr_ptr_type_node
, method_index
)));
1758 func
= build1 (INDIRECT_REF
, nativecode_ptr_type_node
, func
);
1764 build_invokeinterface (dtable
, method
)
1765 tree dtable
, method
;
1767 static tree class_ident
= NULL_TREE
;
1774 /* We expand invokeinterface here. _Jv_LookupInterfaceMethod() will
1775 ensure that the selected method exists, is public and not
1776 abstract nor static. */
1778 if (class_ident
== NULL_TREE
)
1779 class_ident
= get_identifier ("class");
1781 dtable
= build1 (INDIRECT_REF
, dtable_type
, dtable
);
1782 dtable
= build (COMPONENT_REF
, class_ptr_type
, dtable
,
1783 lookup_field (&dtable_type
, class_ident
));
1785 interface
= DECL_CONTEXT (method
);
1786 layout_class_methods (interface
);
1789 for (meth
= TYPE_METHODS (interface
); ; meth
= TREE_CHAIN (meth
), i
++)
1793 idx
= build_int_2 (i
, 0);
1796 if (meth
== NULL_TREE
)
1797 fatal ("internal error in build_invokeinterface");
1800 lookup_arg
= tree_cons (NULL_TREE
, dtable
,
1801 tree_cons (NULL_TREE
, build_class_ref (interface
),
1802 build_tree_list (NULL_TREE
, idx
)));
1804 return build (CALL_EXPR
, ptr_type_node
,
1805 build_address_of (soft_lookupinterfacemethod_node
),
1806 lookup_arg
, NULL_TREE
);
1809 /* Expand one of the invoke_* opcodes.
1810 OCPODE is the specific opcode.
1811 METHOD_REF_INDEX is an index into the constant pool.
1812 NARGS is the number of arguments, or -1 if not specified. */
1815 expand_invoke (opcode
, method_ref_index
, nargs
)
1817 int method_ref_index
;
1818 int nargs ATTRIBUTE_UNUSED
;
1820 tree method_signature
= COMPONENT_REF_SIGNATURE(¤t_jcf
->cpool
, method_ref_index
);
1821 tree method_name
= COMPONENT_REF_NAME (¤t_jcf
->cpool
, method_ref_index
);
1822 tree self_type
= get_class_constant
1823 (current_jcf
, COMPONENT_REF_CLASS_INDEX(¤t_jcf
->cpool
, method_ref_index
));
1824 const char *self_name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type
)));
1825 tree call
, func
, method
, arg_list
, method_type
;
1826 tree cond
= NULL_TREE
;
1828 if (! CLASS_LOADED_P (self_type
))
1830 load_class (self_type
, 1);
1831 safe_layout_class (self_type
);
1832 if (TREE_CODE (TYPE_SIZE (self_type
)) == ERROR_MARK
)
1833 fatal ("failed to find class '%s'", self_name
);
1835 layout_class_methods (self_type
);
1837 if (ID_INIT_P (method_name
))
1838 method
= lookup_java_constructor (CLASS_TO_HANDLE_TYPE (self_type
),
1841 method
= lookup_java_method (CLASS_TO_HANDLE_TYPE (self_type
),
1842 method_name
, method_signature
);
1843 if (method
== NULL_TREE
)
1845 error ("Class '%s' has no method named '%s' matching signature '%s'",
1847 IDENTIFIER_POINTER (method_name
),
1848 IDENTIFIER_POINTER (method_signature
));
1850 /* Invoke static can't invoke static/abstract method */
1851 else if (opcode
== OPCODE_invokestatic
)
1853 if (!METHOD_STATIC (method
))
1855 error ("invokestatic on non static method");
1858 else if (METHOD_ABSTRACT (method
))
1860 error ("invokestatic on abstract method");
1866 if (METHOD_STATIC (method
))
1868 error ("invoke[non-static] on static method");
1873 if (method
== NULL_TREE
)
1875 method_type
= get_type_from_signature (method_signature
);
1876 pop_arguments (TYPE_ARG_TYPES (method_type
));
1877 if (opcode
!= OPCODE_invokestatic
)
1878 pop_type (self_type
);
1879 method_type
= promote_type (TREE_TYPE (method_type
));
1880 push_value (convert (method_type
, integer_zero_node
));
1884 method_type
= TREE_TYPE (method
);
1885 arg_list
= pop_arguments (TYPE_ARG_TYPES (method_type
));
1886 flush_quick_stack ();
1889 if (opcode
== OPCODE_invokestatic
)
1890 func
= build_known_method_ref (method
, method_type
, self_type
,
1891 method_signature
, arg_list
);
1892 else if (opcode
== OPCODE_invokespecial
1893 || (opcode
== OPCODE_invokevirtual
1894 && (METHOD_PRIVATE (method
)
1895 || METHOD_FINAL (method
)
1896 || CLASS_FINAL (TYPE_NAME (self_type
)))))
1898 /* If the object for the method call is null, we throw an
1899 exception. We don't do this if the object is the current
1900 method's `this'. In other cases we just rely on an
1901 optimization pass to eliminate redundant checks. FIXME:
1902 Unfortunately there doesn't seem to be a way to determine
1903 what the current method is right now. */
1904 /* We use a SAVE_EXPR here to make sure we only evaluate
1905 the new `self' expression once. */
1906 tree save_arg
= save_expr (TREE_VALUE (arg_list
));
1907 TREE_VALUE (arg_list
) = save_arg
;
1908 cond
= build (EQ_EXPR
, boolean_type_node
, save_arg
, null_pointer_node
);
1909 func
= build_known_method_ref (method
, method_type
, self_type
,
1910 method_signature
, arg_list
);
1914 tree dtable
= invoke_build_dtable (opcode
== OPCODE_invokeinterface
,
1916 if (opcode
== OPCODE_invokevirtual
)
1917 func
= build_invokevirtual (dtable
, method
);
1919 func
= build_invokeinterface (dtable
, method
);
1921 func
= build1 (NOP_EXPR
, build_pointer_type (method_type
), func
);
1922 call
= build (CALL_EXPR
, TREE_TYPE (method_type
), func
, arg_list
, NULL_TREE
);
1923 TREE_SIDE_EFFECTS (call
) = 1;
1925 if (cond
!= NULL_TREE
)
1927 /* We have to make the `then' branch a compound expression to
1928 make the types turn out right. This seems bizarre. */
1929 call
= build (COND_EXPR
, TREE_TYPE (call
), cond
,
1930 build (COMPOUND_EXPR
, TREE_TYPE (call
),
1931 build (CALL_EXPR
, void_type_node
,
1932 build_address_of (soft_nullpointer_node
),
1933 NULL_TREE
, NULL_TREE
),
1934 (FLOAT_TYPE_P (TREE_TYPE (call
))
1935 ? build_real (TREE_TYPE (call
), dconst0
)
1936 : build1 (CONVERT_EXPR
, TREE_TYPE (call
),
1937 integer_zero_node
))),
1939 TREE_SIDE_EFFECTS (call
) = 1;
1942 if (TREE_CODE (TREE_TYPE (method_type
)) == VOID_TYPE
)
1943 expand_expr_stmt (call
);
1947 flush_quick_stack ();
1951 /* Create a stub which will be put into the vtable but which will call
1955 build_jni_stub (method
)
1958 tree jnifunc
, call
, args
, body
, lookup_arg
, method_sig
, arg_types
;
1959 tree jni_func_type
, tem
;
1960 tree env_var
, res_var
= NULL_TREE
, block
;
1961 tree method_args
, res_type
;
1964 tree klass
= DECL_CONTEXT (method
);
1965 int from_class
= ! CLASS_FROM_SOURCE_P (klass
);
1966 klass
= build_class_ref (klass
);
1968 if (! METHOD_NATIVE (method
) || ! flag_jni
)
1971 DECL_ARTIFICIAL (method
) = 1;
1972 DECL_EXTERNAL (method
) = 0;
1974 env_var
= build_decl (VAR_DECL
, get_identifier ("env"), ptr_type_node
);
1975 DECL_CONTEXT (env_var
) = method
;
1977 if (TREE_TYPE (TREE_TYPE (method
)) != void_type_node
)
1979 res_var
= build_decl (VAR_DECL
, get_identifier ("res"),
1980 TREE_TYPE (TREE_TYPE (method
)));
1981 DECL_CONTEXT (res_var
) = method
;
1982 TREE_CHAIN (env_var
) = res_var
;
1985 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1986 meth_var
= build_decl (VAR_DECL
, get_identifier ("meth"), ptr_type_node
);
1987 TREE_STATIC (meth_var
) = 1;
1988 TREE_PUBLIC (meth_var
) = 0;
1989 DECL_EXTERNAL (meth_var
) = 0;
1990 make_decl_rtl (meth_var
, NULL
, 0);
1991 meth_var
= pushdecl_top_level (meth_var
);
1994 /* One strange way that the front ends are different is that they
1995 store arguments differently. */
1997 method_args
= DECL_ARGUMENTS (method
);
1999 method_args
= BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method
));
2000 block
= build_block (env_var
, NULL_TREE
, NULL_TREE
,
2001 method_args
, NULL_TREE
);
2002 TREE_SIDE_EFFECTS (block
) = 1;
2003 /* When compiling from source we don't set the type of the block,
2004 because that will prevent patch_return from ever being run. */
2006 TREE_TYPE (block
) = TREE_TYPE (TREE_TYPE (method
));
2008 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2009 body
= build (MODIFY_EXPR
, ptr_type_node
, env_var
,
2010 build (CALL_EXPR
, ptr_type_node
,
2011 build_address_of (soft_getjnienvnewframe_node
),
2012 build_tree_list (NULL_TREE
, klass
),
2014 CAN_COMPLETE_NORMALLY (body
) = 1;
2016 /* All the arguments to this method become arguments to the
2017 underlying JNI function. If we had to wrap object arguments in a
2018 special way, we would do that here. */
2020 for (tem
= method_args
; tem
!= NULL_TREE
; tem
= TREE_CHAIN (tem
))
2021 args
= tree_cons (NULL_TREE
, tem
, args
);
2022 args
= nreverse (args
);
2023 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
2025 /* For a static method the second argument is the class. For a
2026 non-static method the second argument is `this'; that is already
2027 available in the argument list. */
2028 if (METHOD_STATIC (method
))
2030 args
= tree_cons (NULL_TREE
, klass
, args
);
2031 arg_types
= tree_cons (NULL_TREE
, object_ptr_type_node
, arg_types
);
2034 /* The JNIEnv structure is the first argument to the JNI function. */
2035 args
= tree_cons (NULL_TREE
, env_var
, args
);
2036 arg_types
= tree_cons (NULL_TREE
, ptr_type_node
, arg_types
);
2038 /* We call _Jv_LookupJNIMethod to find the actual underlying
2039 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2040 exception if this function is not found at runtime. */
2041 method_sig
= build_java_signature (TREE_TYPE (method
));
2043 build_tree_list (NULL_TREE
,
2044 build_utf8_ref (unmangle_classname
2045 (IDENTIFIER_POINTER (method_sig
),
2046 IDENTIFIER_LENGTH (method_sig
))));
2047 tem
= DECL_NAME (method
);
2049 = tree_cons (NULL_TREE
, klass
,
2050 tree_cons (NULL_TREE
, build_utf8_ref (tem
), lookup_arg
));
2053 = build_pointer_type (build_function_type (TREE_TYPE (TREE_TYPE (method
)),
2056 jnifunc
= build (COND_EXPR
, ptr_type_node
,
2058 build (MODIFY_EXPR
, ptr_type_node
,
2060 build (CALL_EXPR
, ptr_type_node
,
2061 build_address_of (soft_lookupjnimethod_node
),
2062 lookup_arg
, NULL_TREE
)));
2064 /* Now we make the actual JNI call via the resulting function
2066 call
= build (CALL_EXPR
, TREE_TYPE (TREE_TYPE (method
)),
2067 build1 (NOP_EXPR
, jni_func_type
, jnifunc
),
2070 /* If the JNI call returned a result, capture it here. If we had to
2071 unwrap JNI object results, we would do that here. */
2072 if (res_var
!= NULL_TREE
)
2073 call
= build (MODIFY_EXPR
, TREE_TYPE (TREE_TYPE (method
)),
2076 TREE_SIDE_EFFECTS (call
) = 1;
2077 CAN_COMPLETE_NORMALLY (call
) = 1;
2079 body
= build (COMPOUND_EXPR
, void_type_node
, body
, call
);
2080 TREE_SIDE_EFFECTS (body
) = 1;
2082 /* Now free the environment we allocated. */
2083 call
= build (CALL_EXPR
, ptr_type_node
,
2084 build_address_of (soft_jnipopsystemframe_node
),
2085 build_tree_list (NULL_TREE
, env_var
),
2087 TREE_SIDE_EFFECTS (call
) = 1;
2088 CAN_COMPLETE_NORMALLY (call
) = 1;
2089 body
= build (COMPOUND_EXPR
, void_type_node
, body
, call
);
2090 TREE_SIDE_EFFECTS (body
) = 1;
2092 /* Finally, do the return. When compiling from source we rely on
2093 patch_return to patch the return value -- because DECL_RESULT is
2094 not set at the time this function is called. */
2097 res_type
= void_type_node
;
2098 if (res_var
!= NULL_TREE
)
2101 if (! DECL_RESULT (method
))
2103 /* Make sure we copy the result variable to the actual
2104 result. We use the type of the DECL_RESULT because it
2105 might be different from the return type of the function:
2106 it might be promoted. */
2107 drt
= TREE_TYPE (DECL_RESULT (method
));
2108 if (drt
!= TREE_TYPE (res_var
))
2109 res_var
= build1 (CONVERT_EXPR
, drt
, res_var
);
2110 res_var
= build (MODIFY_EXPR
, drt
, DECL_RESULT (method
), res_var
);
2111 TREE_SIDE_EFFECTS (res_var
) = 1;
2116 /* This is necessary to get patch_return to run. */
2117 res_type
= NULL_TREE
;
2119 body
= build (COMPOUND_EXPR
, void_type_node
, body
,
2120 build1 (RETURN_EXPR
, res_type
, res_var
));
2121 TREE_SIDE_EFFECTS (body
) = 1;
2123 BLOCK_EXPR_BODY (block
) = body
;
2127 /* Expand an operation to extract from or store into a field.
2128 IS_STATIC is 1 iff the field is static.
2129 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2130 FIELD_REF_INDEX is an index into the constant pool. */
2133 expand_java_field_op (is_static
, is_putting
, field_ref_index
)
2136 int field_ref_index
;
2139 get_class_constant (current_jcf
,
2140 COMPONENT_REF_CLASS_INDEX (¤t_jcf
->cpool
,
2142 const char *self_name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type
)));
2143 tree field_name
= COMPONENT_REF_NAME (¤t_jcf
->cpool
, field_ref_index
);
2144 tree field_signature
= COMPONENT_REF_SIGNATURE (¤t_jcf
->cpool
,
2146 tree field_type
= get_type_from_signature (field_signature
);
2147 tree new_value
= is_putting
? pop_value (field_type
) : NULL_TREE
;
2150 tree field_decl
= lookup_field (&self_type
, field_name
);
2151 if (field_decl
== error_mark_node
)
2155 else if (field_decl
== NULL_TREE
)
2157 error ("Missing field '%s' in '%s'",
2158 IDENTIFIER_POINTER (field_name
), self_name
);
2161 else if (build_java_signature (TREE_TYPE (field_decl
)) != field_signature
)
2163 error ("Mismatching signature for field '%s' in '%s'",
2164 IDENTIFIER_POINTER (field_name
), self_name
);
2167 field_ref
= is_static
? NULL_TREE
: pop_value (self_type
);
2171 push_value (convert (field_type
, integer_zero_node
));
2172 flush_quick_stack ();
2176 /* Inline references to java.lang.PRIMTYPE.TYPE.
2177 In addition to being a useful (minor) optimization,
2178 this is also needed to avoid circularities in the implementation
2179 of these fields in libjava. */
2180 if (field_name
== TYPE_identifier_node
&& ! is_putting
2181 && ! flag_emit_class_files
&& field_type
== class_ptr_type
2182 && strncmp (self_name
, "java.lang.", 10) == 0)
2184 tree typ
= build_primtype_type_ref (self_name
);
2192 field_ref
= build_field_ref (field_ref
, self_type
, field_name
);
2194 field_ref
= build_class_init (self_type
, field_ref
);
2197 flush_quick_stack ();
2198 if (FIELD_FINAL (field_decl
))
2200 if (DECL_CONTEXT (field_decl
) != current_class
)
2201 error_with_decl (field_decl
,
2202 "assignment to final field `%s' not in field's class");
2203 else if (FIELD_STATIC (field_decl
))
2205 if (!DECL_CLINIT_P (current_function_decl
))
2206 error_with_decl (field_decl
,
2207 "assignment to final static field `%s' not in class initializer");
2211 tree cfndecl_name
= DECL_NAME (current_function_decl
);
2212 if (! DECL_CONSTRUCTOR_P (current_function_decl
)
2213 && !ID_FINIT_P (cfndecl_name
))
2214 error_with_decl (field_decl
, "assignment to final field `%s' not in constructor");
2217 expand_assignment (field_ref
, new_value
, 0, 0);
2220 push_value (field_ref
);
2224 build_primtype_type_ref (self_name
)
2225 const char *self_name
;
2227 const char *class_name
= self_name
+10;
2229 if (strncmp(class_name
, "Byte", 4) == 0)
2230 typ
= byte_type_node
;
2231 else if (strncmp(class_name
, "Short", 5) == 0)
2232 typ
= short_type_node
;
2233 else if (strncmp(class_name
, "Integer", 7) == 0)
2234 typ
= int_type_node
;
2235 else if (strncmp(class_name
, "Long", 4) == 0)
2236 typ
= long_type_node
;
2237 else if (strncmp(class_name
, "Float", 5) == 0)
2238 typ
= float_type_node
;
2239 else if (strncmp(class_name
, "Double", 6) == 0)
2240 typ
= double_type_node
;
2241 else if (strncmp(class_name
, "Boolean", 7) == 0)
2242 typ
= boolean_type_node
;
2243 else if (strncmp(class_name
, "Char", 4) == 0)
2244 typ
= char_type_node
;
2245 else if (strncmp(class_name
, "Void", 4) == 0)
2246 typ
= void_type_node
;
2249 if (typ
!= NULL_TREE
)
2250 return build_class_ref (typ
);
2256 load_type_state (label
)
2260 tree vec
= LABEL_TYPE_STATE (label
);
2261 int cur_length
= TREE_VEC_LENGTH (vec
);
2262 stack_pointer
= cur_length
- DECL_MAX_LOCALS(current_function_decl
);
2263 for (i
= 0; i
< cur_length
; i
++)
2264 type_map
[i
] = TREE_VEC_ELT (vec
, i
);
2267 /* Do the expansion of a Java switch. With Gcc, switches are front-end
2268 dependant things, but they rely on gcc routines. This function is
2269 placed here because it uses things defined locally in parse.y. */
2272 case_identity (t
, v
)
2273 tree t
__attribute__ ((__unused__
));
2279 /* Return the name of the vtable for an array of a given primitive
2282 get_primitive_array_vtable (tree elt
)
2285 if (elt
== boolean_type_node
)
2286 r
= boolean_array_vtable
;
2287 else if (elt
== byte_type_node
)
2288 r
= byte_array_vtable
;
2289 else if (elt
== char_type_node
)
2290 r
= char_array_vtable
;
2291 else if (elt
== short_type_node
)
2292 r
= short_array_vtable
;
2293 else if (elt
== int_type_node
)
2294 r
= int_array_vtable
;
2295 else if (elt
== long_type_node
)
2296 r
= long_array_vtable
;
2297 else if (elt
== float_type_node
)
2298 r
= float_array_vtable
;
2299 else if (elt
== double_type_node
)
2300 r
= double_array_vtable
;
2303 return build_address_of (r
);
2307 java_lang_expand_expr (exp
, target
, tmode
, modifier
)
2309 rtx target ATTRIBUTE_UNUSED
;
2310 enum machine_mode tmode ATTRIBUTE_UNUSED
;
2311 enum expand_modifier modifier ATTRIBUTE_UNUSED
;
2315 switch (TREE_CODE (exp
))
2317 case NEW_ARRAY_INIT
:
2320 tree array_type
= TREE_TYPE (TREE_TYPE (exp
));
2321 tree element_type
= TYPE_ARRAY_ELEMENT (array_type
);
2322 tree data_fld
= TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type
)));
2323 HOST_WIDE_INT ilength
= java_array_type_length (array_type
);
2324 tree length
= build_int_2 (ilength
, 0);
2325 tree init
= TREE_OPERAND (exp
, 0);
2328 /* See if we can generate the array statically. */
2329 if (TREE_CONSTANT (init
) && TREE_STATIC (exp
)
2330 && JPRIMITIVE_TYPE_P (element_type
))
2332 tree temp
, value
, init_decl
;
2334 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2335 START_RECORD_CONSTRUCTOR (temp
, object_type_node
);
2336 PUSH_FIELD_VALUE (temp
, "vtable",
2337 get_primitive_array_vtable (element_type
));
2338 if (! flag_hash_synchronization
)
2339 PUSH_FIELD_VALUE (temp
, "sync_info", null_pointer_node
);
2340 FINISH_RECORD_CONSTRUCTOR (temp
);
2341 START_RECORD_CONSTRUCTOR (value
, array_type
);
2342 PUSH_SUPER_VALUE (value
, temp
);
2343 /* FIXME: build a new `length' here to get it on the right
2345 PUSH_FIELD_VALUE (value
, "length", build_int_2 (ilength
, 0));
2346 PUSH_FIELD_VALUE (value
, "data", init
);
2347 FINISH_RECORD_CONSTRUCTOR (value
);
2349 init_decl
= build_decl (VAR_DECL
, generate_name (), array_type
);
2350 pushdecl_top_level (init_decl
);
2351 TREE_STATIC (init_decl
) = 1;
2352 DECL_INITIAL (init_decl
) = value
;
2353 DECL_IGNORED_P (init_decl
) = 1;
2354 TREE_READONLY (init_decl
) = 1;
2355 make_decl_rtl (init_decl
, NULL
, 1);
2356 init
= build1 (ADDR_EXPR
, TREE_TYPE (exp
), init_decl
);
2357 r
= expand_expr (init
, target
, tmode
, modifier
);
2362 array_decl
= build_decl (VAR_DECL
, NULL_TREE
, TREE_TYPE (exp
));
2363 expand_decl (array_decl
);
2364 tmp
= expand_assignment (array_decl
,
2365 build_new_array (element_type
, length
),
2367 if (TREE_CONSTANT (init
)
2368 && ilength
>= 10 && JPRIMITIVE_TYPE_P (element_type
))
2371 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2372 init_decl
= build_decl (VAR_DECL
, generate_name (),
2374 pushdecl_top_level (init_decl
);
2375 TREE_STATIC (init_decl
) = 1;
2376 DECL_INITIAL (init_decl
) = init
;
2377 DECL_IGNORED_P (init_decl
) = 1;
2378 TREE_READONLY (init_decl
) = 1;
2379 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (init_decl
)) = 1;
2380 make_decl_rtl (init_decl
, NULL
, 1);
2384 expand_assignment (build (COMPONENT_REF
, TREE_TYPE (data_fld
),
2385 build1 (INDIRECT_REF
, array_type
,
2386 array_decl
), data_fld
), init
, 0, 0);
2390 if (BLOCK_EXPR_BODY (exp
))
2393 tree body
= BLOCK_EXPR_BODY (exp
);
2394 pushlevel (2); /* 2 and above */
2395 expand_start_bindings (0);
2396 local
= BLOCK_EXPR_DECLS (exp
);
2399 tree next
= TREE_CHAIN (local
);
2400 layout_decl (local
, 0);
2401 expand_decl (pushdecl (local
));
2404 /* Avoid deep recursion for long block. */
2405 while (TREE_CODE (body
) == COMPOUND_EXPR
)
2407 expand_expr (TREE_OPERAND (body
, 0), const0_rtx
, VOIDmode
, 0);
2409 body
= TREE_OPERAND (body
, 1);
2411 expand_expr (body
, const0_rtx
, VOIDmode
, 0);
2414 expand_end_bindings (getdecls (), 1, 0);
2422 if (pushcase (TREE_OPERAND (exp
, 0), case_identity
,
2423 build_decl (LABEL_DECL
, NULL_TREE
, NULL_TREE
),
2426 EXPR_WFL_LINECOL (wfl_operator
) = EXPR_WFL_LINECOL (exp
);
2428 (wfl_operator
, "Duplicate case label: `%s'",
2429 print_int_node (TREE_OPERAND (exp
, 0)));
2435 pushcase (NULL_TREE
, 0,
2436 build_decl (LABEL_DECL
, NULL_TREE
, NULL_TREE
), NULL
);
2440 expand_start_case (0, TREE_OPERAND (exp
, 0), int_type_node
, "switch");
2441 expand_expr_stmt (TREE_OPERAND (exp
, 1));
2442 expand_end_case (TREE_OPERAND (exp
, 0));
2446 /* We expand a try[-catch] block */
2448 /* Expand the try block */
2449 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2450 expand_eh_region_start ();
2452 expand_expr_stmt (TREE_OPERAND (exp
, 0));
2453 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2454 expand_start_all_catch ();
2457 /* Expand all catch clauses (EH handlers) */
2458 for (current
= TREE_OPERAND (exp
, 1); current
;
2459 current
= TREE_CHAIN (current
))
2462 tree
catch = TREE_OPERAND (current
, 0);
2463 tree decl
= BLOCK_EXPR_DECLS (catch);
2464 type
= (decl
? TREE_TYPE (TREE_TYPE (decl
)) : NULL_TREE
);
2465 start_catch_handler (prepare_eh_table_type (type
));
2466 expand_expr_stmt (TREE_OPERAND (current
, 0));
2468 expand_resume_after_catch ();
2469 end_catch_handler ();
2471 expand_end_all_catch ();
2475 fatal ("Can't expand '%s' tree - java_lang_expand_expr",
2476 tree_code_name
[TREE_CODE (exp
)]);
2481 expand_byte_code (jcf
, method
)
2488 const unsigned char *linenumber_pointer
;
2489 int dead_code_index
= -1;
2491 #undef RET /* Defined by config/i386/i386.h */
2492 #undef AND /* Causes problems with opcodes for iand and land. */
2494 #define BCODE byte_ops
2495 #define BYTE_type_node byte_type_node
2496 #define SHORT_type_node short_type_node
2497 #define INT_type_node int_type_node
2498 #define LONG_type_node long_type_node
2499 #define CHAR_type_node char_type_node
2500 #define PTR_type_node ptr_type_node
2501 #define FLOAT_type_node float_type_node
2502 #define DOUBLE_type_node double_type_node
2503 #define VOID_type_node void_type_node
2505 unsigned char* byte_ops
;
2506 long length
= DECL_CODE_LENGTH (method
);
2509 JCF_SEEK (jcf
, DECL_CODE_OFFSET (method
));
2510 byte_ops
= jcf
->read_ptr
;
2512 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2513 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2514 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2515 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2517 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2519 instruction_bits
= oballoc (length
+ 1);
2520 bzero (instruction_bits
, length
+ 1);
2522 /* We make an initial pass of the line number table, to note
2523 which instructions have associated line number entries. */
2524 linenumber_pointer
= linenumber_table
;
2525 for (i
= 0; i
< linenumber_count
; i
++)
2527 int pc
= GET_u2 (linenumber_pointer
);
2528 linenumber_pointer
+= 4;
2530 warning ("invalid PC in line number table");
2533 if ((instruction_bits
[pc
] & BCODE_HAS_LINENUMBER
) != 0)
2534 instruction_bits
[pc
] |= BCODE_HAS_MULTI_LINENUMBERS
;
2535 instruction_bits
[pc
] |= BCODE_HAS_LINENUMBER
;
2539 /* Do a preliminary pass.
2540 * This figures out which PC can be the targets of jumps. */
2541 for (PC
= 0; PC
< length
;)
2543 int oldpc
= PC
; /* PC at instruction start. */
2544 instruction_bits
[PC
] |= BCODE_INSTRUCTION_START
;
2545 switch (byte_ops
[PC
++])
2547 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2549 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2552 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2554 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2555 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2556 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2557 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2558 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2559 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2560 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2561 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2563 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2564 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2565 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2566 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2567 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2568 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2569 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2570 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2572 /* two forms of wide instructions */
2573 #define PRE_SPECIAL_WIDE(IGNORE) \
2575 int modified_opcode = IMMEDIATE_u1; \
2576 if (modified_opcode == OPCODE_iinc) \
2578 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2579 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2583 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2587 /* nothing */ /* XXX JH */
2589 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2591 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2593 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2594 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2595 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2596 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2597 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2598 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2599 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2600 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2601 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2602 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2604 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2605 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2606 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2607 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2608 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2609 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2610 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2611 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2613 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2615 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2616 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2618 #define PRE_LOOKUP_SWITCH \
2619 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2620 NOTE_LABEL (default_offset+oldpc); \
2622 while (--npairs >= 0) { \
2623 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2624 jint offset = IMMEDIATE_s4; \
2625 NOTE_LABEL (offset+oldpc); } \
2628 #define PRE_TABLE_SWITCH \
2629 { jint default_offset = IMMEDIATE_s4; \
2630 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2631 NOTE_LABEL (default_offset+oldpc); \
2633 while (low++ <= high) { \
2634 jint offset = IMMEDIATE_s4; \
2635 NOTE_LABEL (offset+oldpc); } \
2638 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2639 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2640 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2641 (void)(IMMEDIATE_u2); \
2642 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2644 #include "javaop.def"
2649 if (! verify_jvm_instructions (jcf
, byte_ops
, length
))
2652 /* Translate bytecodes to rtl instructions. */
2653 linenumber_pointer
= linenumber_table
;
2654 for (PC
= 0; PC
< length
;)
2656 if ((instruction_bits
[PC
] & BCODE_TARGET
) != 0 || PC
== 0)
2658 tree label
= lookup_label (PC
);
2659 flush_quick_stack ();
2660 if ((instruction_bits
[PC
] & BCODE_TARGET
) != 0)
2661 expand_label (label
);
2662 if (LABEL_VERIFIED (label
) || PC
== 0)
2663 load_type_state (label
);
2666 if (! (instruction_bits
[PC
] & BCODE_VERIFIED
))
2668 if (dead_code_index
== -1)
2670 /* This is the start of a region of unreachable bytecodes.
2671 They still need to be processed in order for EH ranges
2672 to get handled correctly. However, we can simply
2673 replace these bytecodes with nops. */
2674 dead_code_index
= PC
;
2677 /* Turn this bytecode into a nop. */
2682 if (dead_code_index
!= -1)
2684 /* We've just reached the end of a region of dead code. */
2685 warning ("Unreachable bytecode from %d to before %d.",
2686 dead_code_index
, PC
);
2687 dead_code_index
= -1;
2691 /* Handle possible line number entry for this PC.
2693 This code handles out-of-order and multiple linenumbers per PC,
2694 but is optimized for the case of line numbers increasing
2695 monotonically with PC. */
2696 if ((instruction_bits
[PC
] & BCODE_HAS_LINENUMBER
) != 0)
2698 if ((instruction_bits
[PC
] & BCODE_HAS_MULTI_LINENUMBERS
) != 0
2699 || GET_u2 (linenumber_pointer
) != PC
)
2700 linenumber_pointer
= linenumber_table
;
2701 while (linenumber_pointer
< linenumber_table
+ linenumber_count
* 4)
2703 int pc
= GET_u2 (linenumber_pointer
);
2704 linenumber_pointer
+= 4;
2707 lineno
= GET_u2 (linenumber_pointer
- 2);
2708 emit_line_note (input_filename
, lineno
);
2709 if (!(instruction_bits
[PC
] & BCODE_HAS_MULTI_LINENUMBERS
))
2714 maybe_pushlevels (PC
);
2715 PC
= process_jvm_instruction (PC
, byte_ops
, length
);
2716 maybe_poplevels (PC
);
2719 if (dead_code_index
!= -1)
2721 /* We've just reached the end of a region of dead code. */
2722 warning ("Unreachable bytecode from %d to the end of the method.",
2728 java_push_constant_from_pool (jcf
, index
)
2733 if (JPOOL_TAG (jcf
, index
) == CONSTANT_String
)
2736 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2737 name
= get_name_constant (jcf
, JPOOL_USHORT1 (jcf
, index
));
2738 index
= alloc_name_constant (CONSTANT_String
, name
);
2739 c
= build_ref_from_constant_pool (index
);
2740 TREE_TYPE (c
) = promote_type (string_type_node
);
2744 c
= get_constant (jcf
, index
);
2749 process_jvm_instruction (PC
, byte_ops
, length
)
2751 const unsigned char* byte_ops
;
2752 long length ATTRIBUTE_UNUSED
;
2754 const char *opname
; /* Temporary ??? */
2755 int oldpc
= PC
; /* PC at instruction start. */
2757 /* If the instruction is at the beginning of a exception handler,
2758 replace the top of the stack with the thrown object reference */
2759 if (instruction_bits
[PC
] & BCODE_EXCEPTION_TARGET
)
2761 tree type
= pop_type (ptr_type_node
);
2762 push_value (build1 (NOP_EXPR
, type
, soft_exceptioninfo_call_node
));
2765 switch (byte_ops
[PC
++])
2767 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2770 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2773 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
2775 int saw_index = 0; \
2776 int index = OPERAND_VALUE; \
2777 build_java_ret (find_local_variable (index, ptr_type_node, oldpc)); \
2780 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
2782 tree where = lookup_label (oldpc+OPERAND_VALUE); \
2783 tree ret = lookup_label (PC); \
2784 build_java_jsr (where, ret); \
2785 load_type_state (ret); \
2788 /* Push a constant onto the stack. */
2789 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
2790 { int saw_index = 0; int ival = (OPERAND_VALUE); \
2791 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
2792 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
2794 /* internal macro added for use by the WIDE case */
2795 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
2796 push_value (find_local_variable (OPVALUE, type_map[OPVALUE], oldpc));
2798 /* Push local variable onto the opcode stack. */
2799 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
2801 /* have to do this since OPERAND_VALUE may have side-effects */ \
2802 int opvalue = OPERAND_VALUE; \
2803 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2806 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
2807 expand_java_return (OPERAND_TYPE##_type_node)
2809 #define REM_EXPR TRUNC_MOD_EXPR
2810 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
2811 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
2813 #define FIELD(IS_STATIC, IS_PUT) \
2814 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
2816 #define TEST(OPERAND_TYPE, CONDITION) \
2817 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2819 #define COND(OPERAND_TYPE, CONDITION) \
2820 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2822 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2823 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
2825 #define BRANCH_GOTO(OPERAND_VALUE) \
2826 expand_java_goto (oldpc + OPERAND_VALUE)
2828 #define BRANCH_CALL(OPERAND_VALUE) \
2829 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
2832 #define BRANCH_RETURN(OPERAND_VALUE) \
2834 tree type = OPERAND_TYPE##_type_node; \
2835 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
2836 expand_java_ret (value); \
2840 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
2841 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2842 fprintf (stderr, "(not implemented)\n")
2843 #define NOT_IMPL1(OPERAND_VALUE) \
2844 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2845 fprintf (stderr, "(not implemented)\n")
2847 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
2849 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
2851 #define STACK_POP(COUNT) java_stack_pop (COUNT)
2853 #define STACK_SWAP(COUNT) java_stack_swap()
2855 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
2856 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
2857 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
2859 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2860 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
2862 #define LOOKUP_SWITCH \
2863 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2864 tree selector = pop_value (INT_type_node); \
2865 tree duplicate, label; \
2866 tree type = TREE_TYPE (selector); \
2867 flush_quick_stack (); \
2868 expand_start_case (0, selector, type, "switch statement");\
2869 push_momentary (); \
2870 while (--npairs >= 0) \
2872 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
2873 tree value = build_int_2 (match, match < 0 ? -1 : 0); \
2874 TREE_TYPE (value) = type; \
2875 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2876 pushcase (value, convert, label, &duplicate); \
2877 expand_java_goto (oldpc + offset); \
2879 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2880 pushcase (NULL_TREE, 0, label, &duplicate); \
2881 expand_java_goto (oldpc + default_offset); \
2883 expand_end_case (selector); \
2886 #define TABLE_SWITCH \
2887 { jint default_offset = IMMEDIATE_s4; \
2888 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2889 tree selector = pop_value (INT_type_node); \
2890 tree duplicate, label; \
2891 tree type = TREE_TYPE (selector); \
2892 flush_quick_stack (); \
2893 expand_start_case (0, selector, type, "switch statement");\
2894 push_momentary (); \
2895 for (; low <= high; low++) \
2897 jint offset = IMMEDIATE_s4; \
2898 tree value = build_int_2 (low, low < 0 ? -1 : 0); \
2899 TREE_TYPE (value) = type; \
2900 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2901 pushcase (value, convert, label, &duplicate); \
2902 expand_java_goto (oldpc + offset); \
2904 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); \
2905 pushcase (NULL_TREE, 0, label, &duplicate); \
2906 expand_java_goto (oldpc + default_offset); \
2908 expand_end_case (selector); \
2911 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2912 { int opcode = byte_ops[PC-1]; \
2913 int method_ref_index = IMMEDIATE_u2; \
2915 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
2917 expand_invoke (opcode, method_ref_index, nargs); \
2920 /* Handle new, checkcast, instanceof */
2921 #define OBJECT(TYPE, OP) \
2922 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
2924 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
2926 #define ARRAY_LOAD(OPERAND_TYPE) \
2928 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
2931 #define ARRAY_STORE(OPERAND_TYPE) \
2933 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
2936 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
2937 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
2938 #define ARRAY_NEW_PTR() \
2939 push_value (build_anewarray (get_class_constant (current_jcf, \
2941 pop_value (int_type_node)));
2942 #define ARRAY_NEW_NUM() \
2944 int atype = IMMEDIATE_u1; \
2945 push_value (build_newarray (atype, pop_value (int_type_node)));\
2947 #define ARRAY_NEW_MULTI() \
2949 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
2950 int ndims = IMMEDIATE_u1; \
2951 expand_java_multianewarray( class, ndims ); \
2954 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
2955 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
2956 pop_value (OPERAND_TYPE##_type_node))));
2958 #define CONVERT2(FROM_TYPE, TO_TYPE) \
2960 push_value (build1 (NOP_EXPR, int_type_node, \
2961 (convert (TO_TYPE##_type_node, \
2962 pop_value (FROM_TYPE##_type_node))))); \
2965 #define CONVERT(FROM_TYPE, TO_TYPE) \
2967 push_value (convert (TO_TYPE##_type_node, \
2968 pop_value (FROM_TYPE##_type_node))); \
2971 /* internal macro added for use by the WIDE case
2972 Added TREE_TYPE (decl) assignment, apbianco */
2973 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
2976 int var = OPVALUE; \
2977 tree type = OPTYPE; \
2978 value = pop_value (type); \
2979 type = TREE_TYPE (value); \
2980 decl = find_local_variable (var, type, oldpc); \
2981 set_local_type (var, type ); \
2982 expand_assignment (decl, value, 0, 0); \
2985 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
2987 /* have to do this since OPERAND_VALUE may have side-effects */ \
2988 int opvalue = OPERAND_VALUE; \
2989 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2992 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2993 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2995 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
2996 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
2998 #define MONITOR_OPERATION(call) \
3000 tree o = pop_value (ptr_type_node); \
3002 flush_quick_stack (); \
3003 c = build_java_monitor (call, o); \
3004 TREE_SIDE_EFFECTS (c) = 1; \
3005 expand_expr_stmt (c); \
3008 #define SPECIAL_IINC(IGNORED) \
3010 unsigned int local_var_index = IMMEDIATE_u1; \
3011 int ival = IMMEDIATE_s1; \
3012 expand_iinc(local_var_index, ival, oldpc); \
3015 #define SPECIAL_WIDE(IGNORED) \
3017 int modified_opcode = IMMEDIATE_u1; \
3018 unsigned int local_var_index = IMMEDIATE_u2; \
3019 switch (modified_opcode) \
3023 int ival = IMMEDIATE_s2; \
3024 expand_iinc (local_var_index, ival, oldpc); \
3027 case OPCODE_iload: \
3028 case OPCODE_lload: \
3029 case OPCODE_fload: \
3030 case OPCODE_dload: \
3031 case OPCODE_aload: \
3033 /* duplicate code from LOAD macro */ \
3034 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3037 case OPCODE_istore: \
3038 case OPCODE_lstore: \
3039 case OPCODE_fstore: \
3040 case OPCODE_dstore: \
3041 case OPCODE_astore: \
3043 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3047 error ("unrecogized wide sub-instruction"); \
3051 #define SPECIAL_THROW(IGNORED) \
3052 build_java_athrow (pop_value (throwable_type_node))
3054 #define SPECIAL_BREAK NOT_IMPL1
3055 #define IMPL NOT_IMPL
3057 #include "javaop.def"
3060 fprintf (stderr
, "%3d: unknown(%3d)\n", oldpc
, byte_ops
[PC
]);
3065 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3066 order, as specified by Java Language Specification.
3068 The problem is that while expand_expr will evaluate its sub-operands in
3069 left-to-right order, for variables it will just return an rtx (i.e.
3070 an lvalue) for the variable (rather than an rvalue). So it is possible
3071 that a later sub-operand will change the register, and when the
3072 actual operation is done, it will use the new value, when it should
3073 have used the original value.
3075 We fix this by using save_expr. This forces the sub-operand to be
3076 copied into a fresh virtual register,
3078 For method invocation, we modify the arguments so that a
3079 left-to-right order evaluation is performed. Saved expressions
3080 will, in CALL_EXPR order, be reused when the call will be expanded.
3084 force_evaluation_order (node
)
3087 if (flag_syntax_only
)
3089 if (TREE_CODE_CLASS (TREE_CODE (node
)) == '2')
3091 if (TREE_SIDE_EFFECTS (TREE_OPERAND (node
, 1)))
3092 TREE_OPERAND (node
, 0) = save_expr (TREE_OPERAND (node
, 0));
3094 else if (TREE_CODE (node
) == CALL_EXPR
|| TREE_CODE (node
) == NEW_CLASS_EXPR
)
3098 if (!TREE_OPERAND (node
, 1))
3101 /* This reverses the evaluation order. This is a desired effect. */
3102 for (cmp
= NULL_TREE
, arg
= TREE_OPERAND (node
, 1);
3103 arg
; arg
= TREE_CHAIN (arg
))
3105 tree saved
= save_expr (force_evaluation_order (TREE_VALUE (arg
)));
3106 cmp
= (cmp
== NULL_TREE
? saved
:
3107 build (COMPOUND_EXPR
, void_type_node
, cmp
, saved
));
3108 TREE_VALUE (arg
) = saved
;
3111 if (cmp
&& TREE_CODE (cmp
) == COMPOUND_EXPR
)
3112 TREE_SIDE_EFFECTS (cmp
) = 1;
3116 cmp
= save_expr (build (COMPOUND_EXPR
, TREE_TYPE (node
), cmp
, node
));
3117 CAN_COMPLETE_NORMALLY (cmp
) = CAN_COMPLETE_NORMALLY (node
);
3118 TREE_SIDE_EFFECTS (cmp
) = 1;