java-tree.h: Moved comment for TYPE_DOT_CLASS adjacent to its declaration.
[official-gcc.git] / gcc / java / expr.c
blob785ccc485bf63b7f368342883849f774ad4b8307
1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "rtl.h"
35 #include "flags.h"
36 #include "expr.h"
37 #include "java-tree.h"
38 #include "javaop.h"
39 #include "java-opcodes.h"
40 #include "jcf.h"
41 #include "java-except.h"
42 #include "parse.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "ggc.h"
46 #include "tree-gimple.h"
47 #include "target.h"
49 static void flush_quick_stack (void);
50 static void push_value (tree);
51 static tree pop_value (tree);
52 static void java_stack_swap (void);
53 static void java_stack_dup (int, int);
54 static void build_java_athrow (tree);
55 static void build_java_jsr (int, int);
56 static void build_java_ret (tree);
57 static void expand_java_multianewarray (tree, int);
58 static void expand_java_arraystore (tree);
59 static void expand_java_arrayload (tree);
60 static void expand_java_array_length (void);
61 static tree build_java_monitor (tree, tree);
62 static void expand_java_pushc (int, tree);
63 static void expand_java_return (tree);
64 static void expand_load_internal (int, tree, int);
65 static void expand_java_NEW (tree);
66 static void expand_java_INSTANCEOF (tree);
67 static void expand_java_CHECKCAST (tree);
68 static void expand_iinc (unsigned int, int, int);
69 static void expand_java_binop (tree, enum tree_code);
70 static void note_label (int, int);
71 static void expand_compare (enum tree_code, tree, tree, int);
72 static void expand_test (enum tree_code, tree, int);
73 static void expand_cond (enum tree_code, tree, int);
74 static void expand_java_goto (int);
75 static tree expand_java_switch (tree, int);
76 static void expand_java_add_case (tree, int, int);
77 #if 0
78 static void expand_java_call (int, int);
79 static void expand_java_ret (tree);
80 #endif
81 static tree pop_arguments (tree);
82 static void expand_invoke (int, int, int);
83 static void expand_java_field_op (int, int, int);
84 static void java_push_constant_from_pool (struct JCF *, int);
85 static void java_stack_pop (int);
86 static tree build_java_throw_out_of_bounds_exception (tree);
87 static tree build_java_check_indexed_type (tree, tree);
88 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
89 static void promote_arguments (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
103 pushdown list (TREE_LISTs) of expression nodes.
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(()) tree quick_stack;
130 /* A free-list of unused permanent TREE_LIST nodes. */
131 static GTY((deletable)) tree tree_list_free_list;
133 /* The stack pointer of the Java virtual machine.
134 This does include the size of the quick_stack. */
136 int stack_pointer;
138 const unsigned char *linenumber_table;
139 int linenumber_count;
141 /* Largest pc so far in this method that has been passed to lookup_label. */
142 int highest_label_pc_this_method = -1;
144 /* Base value for this method to add to pc to get generated label. */
145 int start_label_pc_this_method = 0;
147 void
148 init_expr_processing (void)
150 operand_type[21] = operand_type[54] = int_type_node;
151 operand_type[22] = operand_type[55] = long_type_node;
152 operand_type[23] = operand_type[56] = float_type_node;
153 operand_type[24] = operand_type[57] = double_type_node;
154 operand_type[25] = operand_type[58] = ptr_type_node;
157 tree
158 java_truthvalue_conversion (tree expr)
160 /* It is simpler and generates better code to have only TRUTH_*_EXPR
161 or comparison expressions as truth values at this level.
163 This function should normally be identity for Java. */
165 switch (TREE_CODE (expr))
167 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
168 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
169 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
170 case ORDERED_EXPR: case UNORDERED_EXPR:
171 case TRUTH_ANDIF_EXPR:
172 case TRUTH_ORIF_EXPR:
173 case TRUTH_AND_EXPR:
174 case TRUTH_OR_EXPR:
175 case TRUTH_XOR_EXPR:
176 case TRUTH_NOT_EXPR:
177 case ERROR_MARK:
178 return expr;
180 case INTEGER_CST:
181 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
183 case REAL_CST:
184 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
186 /* are these legal? XXX JH */
187 case NEGATE_EXPR:
188 case ABS_EXPR:
189 case FLOAT_EXPR:
190 /* These don't change whether an object is nonzero or zero. */
191 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
193 case COND_EXPR:
194 /* Distribute the conversion into the arms of a COND_EXPR. */
195 return fold_build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
196 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
197 java_truthvalue_conversion (TREE_OPERAND (expr, 2)));
199 case NOP_EXPR:
200 /* If this is widening the argument, we can ignore it. */
201 if (TYPE_PRECISION (TREE_TYPE (expr))
202 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
203 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
204 /* fall through to default */
206 default:
207 return fold_build2 (NE_EXPR, boolean_type_node,
208 expr, boolean_false_node);
212 /* Save any stack slots that happen to be in the quick_stack into their
213 home virtual register slots.
215 The copy order is from low stack index to high, to support the invariant
216 that the expression for a slot may contain decls for stack slots with
217 higher (or the same) index, but not lower. */
219 static void
220 flush_quick_stack (void)
222 int stack_index = stack_pointer;
223 tree prev, cur, next;
225 /* First reverse the quick_stack, and count the number of slots it has. */
226 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
228 next = TREE_CHAIN (cur);
229 TREE_CHAIN (cur) = prev;
230 prev = cur;
231 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
233 quick_stack = prev;
235 while (quick_stack != NULL_TREE)
237 tree decl;
238 tree node = quick_stack, type;
239 quick_stack = TREE_CHAIN (node);
240 TREE_CHAIN (node) = tree_list_free_list;
241 tree_list_free_list = node;
242 node = TREE_VALUE (node);
243 type = TREE_TYPE (node);
245 decl = find_stack_slot (stack_index, type);
246 if (decl != node)
247 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
248 stack_index += 1 + TYPE_IS_WIDE (type);
252 /* Push TYPE on the type stack.
253 Return true on success, 0 on overflow. */
256 push_type_0 (tree type)
258 int n_words;
259 type = promote_type (type);
260 n_words = 1 + TYPE_IS_WIDE (type);
261 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
262 return 0;
263 /* Allocate decl for this variable now, so we get a temporary that
264 survives the whole method. */
265 find_stack_slot (stack_pointer, type);
266 stack_type_map[stack_pointer++] = type;
267 n_words--;
268 while (--n_words >= 0)
269 stack_type_map[stack_pointer++] = TYPE_SECOND;
270 return 1;
273 void
274 push_type (tree type)
276 int r = push_type_0 (type);
277 gcc_assert (r);
280 static void
281 push_value (tree value)
283 tree type = TREE_TYPE (value);
284 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
286 type = promote_type (type);
287 value = convert (type, value);
289 push_type (type);
290 if (tree_list_free_list == NULL_TREE)
291 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
292 else
294 tree node = tree_list_free_list;
295 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
296 TREE_VALUE (node) = value;
297 TREE_CHAIN (node) = quick_stack;
298 quick_stack = node;
302 /* Pop a type from the type stack.
303 TYPE is the expected type. Return the actual type, which must be
304 convertible to TYPE.
305 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
307 tree
308 pop_type_0 (tree type, char **messagep)
310 int n_words;
311 tree t;
312 *messagep = NULL;
313 if (TREE_CODE (type) == RECORD_TYPE)
314 type = promote_type (type);
315 n_words = 1 + TYPE_IS_WIDE (type);
316 if (stack_pointer < n_words)
318 *messagep = xstrdup ("stack underflow");
319 return type;
321 while (--n_words > 0)
323 if (stack_type_map[--stack_pointer] != void_type_node)
325 *messagep = xstrdup ("Invalid multi-word value on type stack");
326 return type;
329 t = stack_type_map[--stack_pointer];
330 if (type == NULL_TREE || t == type)
331 return t;
332 if (TREE_CODE (t) == TREE_LIST)
336 tree tt = TREE_PURPOSE (t);
337 if (! can_widen_reference_to (tt, type))
339 t = tt;
340 goto fail;
342 t = TREE_CHAIN (t);
344 while (t);
345 return t;
347 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
348 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
349 return t;
350 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
352 /* If the expected type we've been passed is object or ptr
353 (i.e. void*), the caller needs to know the real type. */
354 if (type == ptr_type_node || type == object_ptr_type_node)
355 return t;
357 /* Since the verifier has already run, we know that any
358 types we see will be compatible. In BC mode, this fact
359 may be checked at runtime, but if that is so then we can
360 assume its truth here as well. So, we always succeed
361 here, with the expected type. */
362 return type;
365 if (! flag_verify_invocations && flag_indirect_dispatch
366 && t == object_ptr_type_node)
368 if (type != ptr_type_node)
369 warning (0, "need to insert runtime check for %s",
370 xstrdup (lang_printable_name (type, 0)));
371 return type;
374 /* lang_printable_name uses a static buffer, so we must save the result
375 from calling it the first time. */
376 fail:
378 char *temp = xstrdup (lang_printable_name (type, 0));
379 /* If the stack contains a multi-word type, keep popping the stack until
380 the real type is found. */
381 while (t == void_type_node)
382 t = stack_type_map[--stack_pointer];
383 *messagep = concat ("expected type '", temp,
384 "' but stack contains '", lang_printable_name (t, 0),
385 "'", NULL);
386 free (temp);
388 return type;
391 /* Pop a type from the type stack.
392 TYPE is the expected type. Return the actual type, which must be
393 convertible to TYPE, otherwise call error. */
395 tree
396 pop_type (tree type)
398 char *message = NULL;
399 type = pop_type_0 (type, &message);
400 if (message != NULL)
402 error ("%s", message);
403 free (message);
405 return type;
409 /* Return true if two type assertions are equal. */
411 static int
412 type_assertion_eq (const void * k1_p, const void * k2_p)
414 type_assertion k1 = *(type_assertion *)k1_p;
415 type_assertion k2 = *(type_assertion *)k2_p;
416 return (k1.assertion_code == k2.assertion_code
417 && k1.op1 == k2.op1
418 && k1.op2 == k2.op2);
421 /* Hash a type assertion. */
423 static hashval_t
424 type_assertion_hash (const void *p)
426 const type_assertion *k_p = p;
427 hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
428 k_p->assertion_code, 0);
429 hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
430 return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
433 /* Add an entry to the type assertion table for the given class.
434 CLASS is the class for which this assertion will be evaluated by the
435 runtime during loading/initialization.
436 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
437 OP1 and OP2 are the operands. The tree type of these arguments may be
438 specific to each assertion_code. */
440 void
441 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
443 htab_t assertions_htab;
444 type_assertion as;
445 void **as_pp;
447 assertions_htab = TYPE_ASSERTIONS (class);
448 if (assertions_htab == NULL)
450 assertions_htab = htab_create_ggc (7, type_assertion_hash,
451 type_assertion_eq, NULL);
452 TYPE_ASSERTIONS (current_class) = assertions_htab;
455 as.assertion_code = assertion_code;
456 as.op1 = op1;
457 as.op2 = op2;
459 as_pp = htab_find_slot (assertions_htab, &as, INSERT);
461 /* Don't add the same assertion twice. */
462 if (*as_pp)
463 return;
465 *as_pp = ggc_alloc (sizeof (type_assertion));
466 **(type_assertion **)as_pp = as;
470 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
471 Handles array types and interfaces. */
474 can_widen_reference_to (tree source_type, tree target_type)
476 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
477 return 1;
479 /* Get rid of pointers */
480 if (TREE_CODE (source_type) == POINTER_TYPE)
481 source_type = TREE_TYPE (source_type);
482 if (TREE_CODE (target_type) == POINTER_TYPE)
483 target_type = TREE_TYPE (target_type);
485 if (source_type == target_type)
486 return 1;
488 /* FIXME: This is very pessimistic, in that it checks everything,
489 even if we already know that the types are compatible. If we're
490 to support full Java class loader semantics, we need this.
491 However, we could do something more optimal. */
492 if (! flag_verify_invocations)
494 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
495 source_type, target_type);
497 if (!quiet_flag)
498 warning (0, "assert: %s is assign compatible with %s",
499 xstrdup (lang_printable_name (target_type, 0)),
500 xstrdup (lang_printable_name (source_type, 0)));
501 /* Punt everything to runtime. */
502 return 1;
505 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
507 return 1;
509 else
511 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
513 HOST_WIDE_INT source_length, target_length;
514 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
516 /* An array implements Cloneable and Serializable. */
517 tree name = DECL_NAME (TYPE_NAME (target_type));
518 return (name == java_lang_cloneable_identifier_node
519 || name == java_io_serializable_identifier_node);
521 target_length = java_array_type_length (target_type);
522 if (target_length >= 0)
524 source_length = java_array_type_length (source_type);
525 if (source_length != target_length)
526 return 0;
528 source_type = TYPE_ARRAY_ELEMENT (source_type);
529 target_type = TYPE_ARRAY_ELEMENT (target_type);
530 if (source_type == target_type)
531 return 1;
532 if (TREE_CODE (source_type) != POINTER_TYPE
533 || TREE_CODE (target_type) != POINTER_TYPE)
534 return 0;
535 return can_widen_reference_to (source_type, target_type);
537 else
539 int source_depth = class_depth (source_type);
540 int target_depth = class_depth (target_type);
542 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
544 if (! quiet_flag)
545 warning (0, "assert: %s is assign compatible with %s",
546 xstrdup (lang_printable_name (target_type, 0)),
547 xstrdup (lang_printable_name (source_type, 0)));
548 return 1;
551 /* class_depth can return a negative depth if an error occurred */
552 if (source_depth < 0 || target_depth < 0)
553 return 0;
555 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
557 /* target_type is OK if source_type or source_type ancestors
558 implement target_type. We handle multiple sub-interfaces */
559 tree binfo, base_binfo;
560 int i;
562 for (binfo = TYPE_BINFO (source_type), i = 0;
563 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
564 if (can_widen_reference_to
565 (BINFO_TYPE (base_binfo), target_type))
566 return 1;
568 if (!i)
569 return 0;
572 for ( ; source_depth > target_depth; source_depth--)
574 source_type
575 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
577 return source_type == target_type;
582 static tree
583 pop_value (tree type)
585 type = pop_type (type);
586 if (quick_stack)
588 tree node = quick_stack;
589 quick_stack = TREE_CHAIN (quick_stack);
590 TREE_CHAIN (node) = tree_list_free_list;
591 tree_list_free_list = node;
592 node = TREE_VALUE (node);
593 return node;
595 else
596 return find_stack_slot (stack_pointer, promote_type (type));
600 /* Pop and discard the top COUNT stack slots. */
602 static void
603 java_stack_pop (int count)
605 while (count > 0)
607 tree type, val;
609 gcc_assert (stack_pointer != 0);
611 type = stack_type_map[stack_pointer - 1];
612 if (type == TYPE_SECOND)
614 count--;
615 gcc_assert (stack_pointer != 1 && count > 0);
617 type = stack_type_map[stack_pointer - 2];
619 val = pop_value (type);
620 count--;
624 /* Implement the 'swap' operator (to swap two top stack slots). */
626 static void
627 java_stack_swap (void)
629 tree type1, type2;
630 tree temp;
631 tree decl1, decl2;
633 if (stack_pointer < 2
634 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
635 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
636 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
637 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
638 /* Bad stack swap. */
639 abort ();
640 /* Bad stack swap. */
642 flush_quick_stack ();
643 decl1 = find_stack_slot (stack_pointer - 1, type1);
644 decl2 = find_stack_slot (stack_pointer - 2, type2);
645 temp = build_decl (VAR_DECL, NULL_TREE, type1);
646 java_add_local_var (temp);
647 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
648 java_add_stmt (build2 (MODIFY_EXPR, type2,
649 find_stack_slot (stack_pointer - 1, type2),
650 decl2));
651 java_add_stmt (build2 (MODIFY_EXPR, type1,
652 find_stack_slot (stack_pointer - 2, type1),
653 temp));
654 stack_type_map[stack_pointer - 1] = type2;
655 stack_type_map[stack_pointer - 2] = type1;
658 static void
659 java_stack_dup (int size, int offset)
661 int low_index = stack_pointer - size - offset;
662 int dst_index;
663 if (low_index < 0)
664 error ("stack underflow - dup* operation");
666 flush_quick_stack ();
668 stack_pointer += size;
669 dst_index = stack_pointer;
671 for (dst_index = stack_pointer; --dst_index >= low_index; )
673 tree type;
674 int src_index = dst_index - size;
675 if (src_index < low_index)
676 src_index = dst_index + size + offset;
677 type = stack_type_map [src_index];
678 if (type == TYPE_SECOND)
680 /* Dup operation splits 64-bit number. */
681 gcc_assert (src_index > low_index);
683 stack_type_map[dst_index] = type;
684 src_index--; dst_index--;
685 type = stack_type_map[src_index];
686 gcc_assert (TYPE_IS_WIDE (type));
688 else
689 gcc_assert (! TYPE_IS_WIDE (type));
691 if (src_index != dst_index)
693 tree src_decl = find_stack_slot (src_index, type);
694 tree dst_decl = find_stack_slot (dst_index, type);
696 java_add_stmt
697 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
698 stack_type_map[dst_index] = type;
703 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
704 value stack. */
706 static void
707 build_java_athrow (tree node)
709 tree call;
711 call = build3 (CALL_EXPR,
712 void_type_node,
713 build_address_of (throw_node),
714 build_tree_list (NULL_TREE, node),
715 NULL_TREE);
716 TREE_SIDE_EFFECTS (call) = 1;
717 java_add_stmt (call);
718 java_stack_pop (stack_pointer);
721 /* Implementation for jsr/ret */
723 static void
724 build_java_jsr (int target_pc, int return_pc)
726 tree where = lookup_label (target_pc);
727 tree ret = lookup_label (return_pc);
728 tree ret_label = fold_build1 (ADDR_EXPR, return_address_type_node, ret);
729 push_value (ret_label);
730 flush_quick_stack ();
731 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
733 /* Do not need to emit the label here. We noted the existence of the
734 label as a jump target in note_instructions; we'll emit the label
735 for real at the beginning of the expand_byte_code loop. */
738 static void
739 build_java_ret (tree location)
741 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
744 /* Implementation of operations on array: new, load, store, length */
746 tree
747 decode_newarray_type (int atype)
749 switch (atype)
751 case 4: return boolean_type_node;
752 case 5: return char_type_node;
753 case 6: return float_type_node;
754 case 7: return double_type_node;
755 case 8: return byte_type_node;
756 case 9: return short_type_node;
757 case 10: return int_type_node;
758 case 11: return long_type_node;
759 default: return NULL_TREE;
763 /* Map primitive type to the code used by OPCODE_newarray. */
766 encode_newarray_type (tree type)
768 if (type == boolean_type_node)
769 return 4;
770 else if (type == char_type_node)
771 return 5;
772 else if (type == float_type_node)
773 return 6;
774 else if (type == double_type_node)
775 return 7;
776 else if (type == byte_type_node)
777 return 8;
778 else if (type == short_type_node)
779 return 9;
780 else if (type == int_type_node)
781 return 10;
782 else if (type == long_type_node)
783 return 11;
784 else
785 gcc_unreachable ();
788 /* Build a call to _Jv_ThrowBadArrayIndex(), the
789 ArrayIndexOfBoundsException exception handler. */
791 static tree
792 build_java_throw_out_of_bounds_exception (tree index)
794 tree node = build3 (CALL_EXPR, int_type_node,
795 build_address_of (soft_badarrayindex_node),
796 build_tree_list (NULL_TREE, index), NULL_TREE);
797 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
798 return (node);
801 /* Return the length of an array. Doesn't perform any checking on the nature
802 or value of the array NODE. May be used to implement some bytecodes. */
804 tree
805 build_java_array_length_access (tree node)
807 tree type = TREE_TYPE (node);
808 tree array_type = TREE_TYPE (type);
809 HOST_WIDE_INT length;
811 if (!is_array_type_p (type))
813 /* With the new verifier, we will see an ordinary pointer type
814 here. In this case, we just use an arbitrary array type. */
815 array_type = build_java_array_type (object_ptr_type_node, -1);
816 type = promote_type (array_type);
819 length = java_array_type_length (type);
820 if (length >= 0)
821 return build_int_cst (NULL_TREE, length);
823 node = build3 (COMPONENT_REF, int_type_node,
824 build_java_indirect_ref (array_type, node,
825 flag_check_references),
826 lookup_field (&array_type, get_identifier ("length")),
827 NULL_TREE);
828 IS_ARRAY_LENGTH_ACCESS (node) = 1;
829 return node;
832 /* Optionally checks a reference against the NULL pointer. ARG1: the
833 expr, ARG2: we should check the reference. Don't generate extra
834 checks if we're not generating code. */
836 tree
837 java_check_reference (tree expr, int check)
839 if (!flag_syntax_only && check)
841 expr = save_expr (expr);
842 expr = build3 (COND_EXPR, TREE_TYPE (expr),
843 build2 (EQ_EXPR, boolean_type_node,
844 expr, null_pointer_node),
845 build3 (CALL_EXPR, void_type_node,
846 build_address_of (soft_nullpointer_node),
847 NULL_TREE, NULL_TREE),
848 expr);
851 return expr;
854 /* Reference an object: just like an INDIRECT_REF, but with checking. */
856 tree
857 build_java_indirect_ref (tree type, tree expr, int check)
859 tree t;
860 t = java_check_reference (expr, check);
861 t = convert (build_pointer_type (type), t);
862 return build1 (INDIRECT_REF, type, t);
865 /* Implement array indexing (either as l-value or r-value).
866 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
867 Optionally performs bounds checking and/or test to NULL.
868 At this point, ARRAY should have been verified as an array. */
870 tree
871 build_java_arrayaccess (tree array, tree type, tree index)
873 tree node, throw = NULL_TREE;
874 tree data_field;
875 tree ref;
876 tree array_type = TREE_TYPE (TREE_TYPE (array));
878 if (!is_array_type_p (TREE_TYPE (array)))
880 /* With the new verifier, we will see an ordinary pointer type
881 here. In this case, we just use the correct array type. */
882 array_type = build_java_array_type (type, -1);
885 if (flag_bounds_check)
887 /* Generate:
888 * (unsigned jint) INDEX >= (unsigned jint) LEN
889 * && throw ArrayIndexOutOfBoundsException.
890 * Note this is equivalent to and more efficient than:
891 * INDEX < 0 || INDEX >= LEN && throw ... */
892 tree test;
893 tree len = convert (unsigned_int_type_node,
894 build_java_array_length_access (array));
895 test = fold_build2 (GE_EXPR, boolean_type_node,
896 convert (unsigned_int_type_node, index),
897 len);
898 if (! integer_zerop (test))
900 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
901 build_java_throw_out_of_bounds_exception (index));
902 /* allows expansion within COMPOUND */
903 TREE_SIDE_EFFECTS( throw ) = 1;
907 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
908 to have the bounds check evaluated first. */
909 if (throw != NULL_TREE)
910 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
912 data_field = lookup_field (&array_type, get_identifier ("data"));
914 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
915 build_java_indirect_ref (array_type, array,
916 flag_check_references),
917 data_field, NULL_TREE);
919 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
920 return node;
923 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
924 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
925 determine that no check is required. */
927 tree
928 build_java_arraystore_check (tree array, tree object)
930 tree check, element_type, source;
931 tree array_type_p = TREE_TYPE (array);
932 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
934 if (! flag_verify_invocations)
936 /* With the new verifier, we don't track precise types. FIXME:
937 performance regression here. */
938 element_type = TYPE_NAME (object_type_node);
940 else
942 gcc_assert (is_array_type_p (array_type_p));
944 /* Get the TYPE_DECL for ARRAY's element type. */
945 element_type
946 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
949 gcc_assert (TREE_CODE (element_type) == TYPE_DECL
950 && TREE_CODE (object_type) == TYPE_DECL);
952 if (!flag_store_check)
953 return build1 (NOP_EXPR, array_type_p, array);
955 /* No check is needed if the element type is final. Also check that
956 element_type matches object_type, since in the bytecode
957 compilation case element_type may be the actual element type of
958 the array rather than its declared type. However, if we're doing
959 indirect dispatch, we can't do the `final' optimization. */
960 if (element_type == object_type
961 && ! flag_indirect_dispatch
962 && CLASS_FINAL (element_type))
963 return build1 (NOP_EXPR, array_type_p, array);
965 /* OBJECT might be wrapped by a SAVE_EXPR. */
966 if (TREE_CODE (object) == SAVE_EXPR)
967 source = TREE_OPERAND (object, 0);
968 else
969 source = object;
971 /* Avoid the check if OBJECT was just loaded from the same array. */
972 if (TREE_CODE (source) == ARRAY_REF)
974 tree target;
975 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
976 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
977 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
978 if (TREE_CODE (source) == SAVE_EXPR)
979 source = TREE_OPERAND (source, 0);
981 target = array;
982 if (TREE_CODE (target) == SAVE_EXPR)
983 target = TREE_OPERAND (target, 0);
985 if (source == target)
986 return build1 (NOP_EXPR, array_type_p, array);
989 /* Build an invocation of _Jv_CheckArrayStore */
990 check = build3 (CALL_EXPR, void_type_node,
991 build_address_of (soft_checkarraystore_node),
992 tree_cons (NULL_TREE, array,
993 build_tree_list (NULL_TREE, object)),
994 NULL_TREE);
995 TREE_SIDE_EFFECTS (check) = 1;
997 return check;
1000 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1001 ARRAY_NODE. This function is used to retrieve something less vague than
1002 a pointer type when indexing the first dimension of something like [[<t>.
1003 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1004 return unchanged. */
1006 static tree
1007 build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED,
1008 tree indexed_type)
1010 /* We used to check to see if ARRAY_NODE really had array type.
1011 However, with the new verifier, this is not necessary, as we know
1012 that the object will be an array of the appropriate type. */
1014 return indexed_type;
1017 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1018 called with an integer code (the type of array to create), and the length
1019 of the array to create. */
1021 tree
1022 build_newarray (int atype_value, tree length)
1024 tree type_arg;
1026 tree prim_type = decode_newarray_type (atype_value);
1027 tree type
1028 = build_java_array_type (prim_type,
1029 host_integerp (length, 0) == INTEGER_CST
1030 ? tree_low_cst (length, 0) : -1);
1032 /* If compiling to native, pass a reference to the primitive type class
1033 and save the runtime some work. However, the bytecode generator
1034 expects to find the type_code int here. */
1035 if (flag_emit_class_files)
1036 type_arg = build_int_cst (NULL_TREE, atype_value);
1037 else
1038 type_arg = build_class_ref (prim_type);
1040 return build3 (CALL_EXPR, promote_type (type),
1041 build_address_of (soft_newarray_node),
1042 tree_cons (NULL_TREE,
1043 type_arg,
1044 build_tree_list (NULL_TREE, length)),
1045 NULL_TREE);
1048 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1049 of the dimension. */
1051 tree
1052 build_anewarray (tree class_type, tree length)
1054 tree type
1055 = build_java_array_type (class_type,
1056 host_integerp (length, 0)
1057 ? tree_low_cst (length, 0) : -1);
1059 return build3 (CALL_EXPR, promote_type (type),
1060 build_address_of (soft_anewarray_node),
1061 tree_cons (NULL_TREE, length,
1062 tree_cons (NULL_TREE, build_class_ref (class_type),
1063 build_tree_list (NULL_TREE,
1064 null_pointer_node))),
1065 NULL_TREE);
1068 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1070 tree
1071 build_new_array (tree type, tree length)
1073 if (JPRIMITIVE_TYPE_P (type))
1074 return build_newarray (encode_newarray_type (type), length);
1075 else
1076 return build_anewarray (TREE_TYPE (type), length);
1079 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1080 class pointer, a number of dimensions and the matching number of
1081 dimensions. The argument list is NULL terminated. */
1083 static void
1084 expand_java_multianewarray (tree class_type, int ndim)
1086 int i;
1087 tree args = build_tree_list( NULL_TREE, null_pointer_node );
1089 for( i = 0; i < ndim; i++ )
1090 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1092 push_value (build3 (CALL_EXPR,
1093 promote_type (class_type),
1094 build_address_of (soft_multianewarray_node),
1095 tree_cons (NULL_TREE, build_class_ref (class_type),
1096 tree_cons (NULL_TREE,
1097 build_int_cst (NULL_TREE, ndim),
1098 args)),
1099 NULL_TREE));
1102 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1103 ARRAY is an array type. May expand some bound checking and NULL
1104 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1105 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1106 INT. In those cases, we make the conversion.
1108 if ARRAy is a reference type, the assignment is checked at run-time
1109 to make sure that the RHS can be assigned to the array element
1110 type. It is not necessary to generate this code if ARRAY is final. */
1112 static void
1113 expand_java_arraystore (tree rhs_type_node)
1115 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
1116 && TYPE_PRECISION (rhs_type_node) <= 32) ?
1117 int_type_node : rhs_type_node);
1118 tree index = pop_value (int_type_node);
1119 tree array_type, array;
1121 /* If we're processing an `aaload' we might as well just pick
1122 `Object'. */
1123 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1125 array_type = build_java_array_type (object_ptr_type_node, -1);
1126 rhs_type_node = object_ptr_type_node;
1128 else
1129 array_type = build_java_array_type (rhs_type_node, -1);
1131 array = pop_value (array_type);
1132 array = build1 (NOP_EXPR, promote_type (array_type), array);
1134 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1136 flush_quick_stack ();
1138 index = save_expr (index);
1139 array = save_expr (array);
1141 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1143 tree check = build_java_arraystore_check (array, rhs_node);
1144 java_add_stmt (check);
1147 array = build_java_arrayaccess (array, rhs_type_node, index);
1148 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1151 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1152 sure that LHS is an array type. May expand some bound checking and NULL
1153 pointer checking.
1154 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1155 BOOLEAN/SHORT, we push a promoted type back to the stack.
1158 static void
1159 expand_java_arrayload (tree lhs_type_node)
1161 tree load_node;
1162 tree index_node = pop_value (int_type_node);
1163 tree array_type;
1164 tree array_node;
1166 /* If we're processing an `aaload' we might as well just pick
1167 `Object'. */
1168 if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1170 array_type = build_java_array_type (object_ptr_type_node, -1);
1171 lhs_type_node = object_ptr_type_node;
1173 else
1174 array_type = build_java_array_type (lhs_type_node, -1);
1175 array_node = pop_value (array_type);
1176 array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1178 index_node = save_expr (index_node);
1179 array_node = save_expr (array_node);
1181 lhs_type_node = build_java_check_indexed_type (array_node,
1182 lhs_type_node);
1183 load_node = build_java_arrayaccess (array_node,
1184 lhs_type_node,
1185 index_node);
1186 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1187 load_node = fold_build1 (NOP_EXPR, int_type_node, load_node);
1188 push_value (load_node);
1191 /* Expands .length. Makes sure that we deal with and array and may expand
1192 a NULL check on the array object. */
1194 static void
1195 expand_java_array_length (void)
1197 tree array = pop_value (ptr_type_node);
1198 tree length = build_java_array_length_access (array);
1200 push_value (length);
1203 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1204 either soft_monitorenter_node or soft_monitorexit_node. */
1206 static tree
1207 build_java_monitor (tree call, tree object)
1209 return build3 (CALL_EXPR,
1210 void_type_node,
1211 build_address_of (call),
1212 build_tree_list (NULL_TREE, object),
1213 NULL_TREE);
1216 /* Emit code for one of the PUSHC instructions. */
1218 static void
1219 expand_java_pushc (int ival, tree type)
1221 tree value;
1222 if (type == ptr_type_node && ival == 0)
1223 value = null_pointer_node;
1224 else if (type == int_type_node || type == long_type_node)
1225 value = build_int_cst (type, ival);
1226 else if (type == float_type_node || type == double_type_node)
1228 REAL_VALUE_TYPE x;
1229 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1230 value = build_real (type, x);
1232 else
1233 gcc_unreachable ();
1235 push_value (value);
1238 static void
1239 expand_java_return (tree type)
1241 if (type == void_type_node)
1242 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1243 else
1245 tree retval = pop_value (type);
1246 tree res = DECL_RESULT (current_function_decl);
1247 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1249 /* Handle the situation where the native integer type is smaller
1250 than the JVM integer. It can happen for many cross compilers.
1251 The whole if expression just goes away if INT_TYPE_SIZE < 32
1252 is false. */
1253 if (INT_TYPE_SIZE < 32
1254 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1255 < GET_MODE_SIZE (TYPE_MODE (type))))
1256 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1258 TREE_SIDE_EFFECTS (retval) = 1;
1259 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1263 static void
1264 expand_load_internal (int index, tree type, int pc)
1266 tree copy;
1267 tree var = find_local_variable (index, type, pc);
1269 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1270 on the stack. If there is an assignment to this VAR_DECL between
1271 the stack push and the use, then the wrong code could be
1272 generated. To avoid this we create a new local and copy our
1273 value into it. Then we push this new local on the stack.
1274 Hopefully this all gets optimized out. */
1275 copy = build_decl (VAR_DECL, NULL_TREE, type);
1276 if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1277 && TREE_TYPE (copy) != TREE_TYPE (var))
1278 var = convert (type, var);
1279 java_add_local_var (copy);
1280 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1282 push_value (copy);
1285 tree
1286 build_address_of (tree value)
1288 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1291 bool
1292 class_has_finalize_method (tree type)
1294 tree super = CLASSTYPE_SUPER (type);
1296 if (super == NULL_TREE)
1297 return false; /* Every class with a real finalizer inherits */
1298 /* from java.lang.Object. */
1299 else
1300 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1303 tree
1304 java_create_object (tree type)
1306 tree alloc_node = (class_has_finalize_method (type)
1307 ? alloc_object_node
1308 : alloc_no_finalizer_node);
1310 return build3 (CALL_EXPR, promote_type (type),
1311 build_address_of (alloc_node),
1312 build_tree_list (NULL_TREE, build_class_ref (type)),
1313 NULL_TREE);
1316 static void
1317 expand_java_NEW (tree type)
1319 tree alloc_node;
1321 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1322 : alloc_no_finalizer_node);
1323 if (! CLASS_LOADED_P (type))
1324 load_class (type, 1);
1325 safe_layout_class (type);
1326 push_value (build3 (CALL_EXPR, promote_type (type),
1327 build_address_of (alloc_node),
1328 build_tree_list (NULL_TREE, build_class_ref (type)),
1329 NULL_TREE));
1332 /* This returns an expression which will extract the class of an
1333 object. */
1335 tree
1336 build_get_class (tree value)
1338 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1339 tree vtable_field = lookup_field (&object_type_node,
1340 get_identifier ("vtable"));
1341 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1342 build_java_indirect_ref (object_type_node, value,
1343 flag_check_references),
1344 vtable_field, NULL_TREE);
1345 return build3 (COMPONENT_REF, class_ptr_type,
1346 build1 (INDIRECT_REF, dtable_type, tmp),
1347 class_field, NULL_TREE);
1350 /* This builds the tree representation of the `instanceof' operator.
1351 It tries various tricks to optimize this in cases where types are
1352 known. */
1354 tree
1355 build_instanceof (tree value, tree type)
1357 tree expr;
1358 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1359 tree valtype = TREE_TYPE (TREE_TYPE (value));
1360 tree valclass = TYPE_NAME (valtype);
1361 tree klass;
1363 /* When compiling from bytecode, we need to ensure that TYPE has
1364 been loaded. */
1365 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1367 load_class (type, 1);
1368 safe_layout_class (type);
1369 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1370 return error_mark_node;
1372 klass = TYPE_NAME (type);
1374 if (type == object_type_node || inherits_from_p (valtype, type))
1376 /* Anything except `null' is an instance of Object. Likewise,
1377 if the object is known to be an instance of the class, then
1378 we only need to check for `null'. */
1379 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1381 else if (flag_verify_invocations
1382 && ! TYPE_ARRAY_P (type)
1383 && ! TYPE_ARRAY_P (valtype)
1384 && DECL_P (klass) && DECL_P (valclass)
1385 && ! CLASS_INTERFACE (valclass)
1386 && ! CLASS_INTERFACE (klass)
1387 && ! inherits_from_p (type, valtype)
1388 && (CLASS_FINAL (klass)
1389 || ! inherits_from_p (valtype, type)))
1391 /* The classes are from different branches of the derivation
1392 tree, so we immediately know the answer. */
1393 expr = boolean_false_node;
1395 else if (DECL_P (klass) && CLASS_FINAL (klass))
1397 tree save = save_expr (value);
1398 expr = build3 (COND_EXPR, itype,
1399 build2 (NE_EXPR, boolean_type_node,
1400 save, null_pointer_node),
1401 build2 (EQ_EXPR, itype,
1402 build_get_class (save),
1403 build_class_ref (type)),
1404 boolean_false_node);
1406 else
1408 expr = build3 (CALL_EXPR, itype,
1409 build_address_of (soft_instanceof_node),
1410 tree_cons (NULL_TREE, value,
1411 build_tree_list (NULL_TREE,
1412 build_class_ref (type))),
1413 NULL_TREE);
1415 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1416 return expr;
1419 static void
1420 expand_java_INSTANCEOF (tree type)
1422 tree value = pop_value (object_ptr_type_node);
1423 value = build_instanceof (value, type);
1424 push_value (value);
1427 static void
1428 expand_java_CHECKCAST (tree type)
1430 tree value = pop_value (ptr_type_node);
1431 value = build3 (CALL_EXPR, promote_type (type),
1432 build_address_of (soft_checkcast_node),
1433 tree_cons (NULL_TREE, build_class_ref (type),
1434 build_tree_list (NULL_TREE, value)),
1435 NULL_TREE);
1436 push_value (value);
1439 static void
1440 expand_iinc (unsigned int local_var_index, int ival, int pc)
1442 tree local_var, res;
1443 tree constant_value;
1445 flush_quick_stack ();
1446 local_var = find_local_variable (local_var_index, int_type_node, pc);
1447 constant_value = build_int_cst (NULL_TREE, ival);
1448 res = fold_build2 (PLUS_EXPR, int_type_node, local_var, constant_value);
1449 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1450 update_aliases (local_var, local_var_index, pc);
1454 tree
1455 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1457 tree call = NULL;
1458 tree arg1 = convert (type, op1);
1459 tree arg2 = convert (type, op2);
1461 if (type == int_type_node)
1463 switch (op)
1465 case TRUNC_DIV_EXPR:
1466 call = soft_idiv_node;
1467 break;
1468 case TRUNC_MOD_EXPR:
1469 call = soft_irem_node;
1470 break;
1471 default:
1472 break;
1475 else if (type == long_type_node)
1477 switch (op)
1479 case TRUNC_DIV_EXPR:
1480 call = soft_ldiv_node;
1481 break;
1482 case TRUNC_MOD_EXPR:
1483 call = soft_lrem_node;
1484 break;
1485 default:
1486 break;
1490 gcc_assert (call);
1491 call = build3 (CALL_EXPR, type,
1492 build_address_of (call),
1493 tree_cons (NULL_TREE, arg1,
1494 build_tree_list (NULL_TREE, arg2)),
1495 NULL_TREE);
1497 return call;
1500 tree
1501 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1503 tree mask;
1504 switch (op)
1506 case URSHIFT_EXPR:
1508 tree u_type = java_unsigned_type (type);
1509 arg1 = convert (u_type, arg1);
1510 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1511 return convert (type, arg1);
1513 case LSHIFT_EXPR:
1514 case RSHIFT_EXPR:
1515 mask = build_int_cst (NULL_TREE,
1516 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1517 arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
1518 break;
1520 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1521 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1522 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1524 tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1525 boolean_type_node, arg1, arg2);
1526 tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
1527 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1528 ifexp2, integer_zero_node,
1529 op == COMPARE_L_EXPR
1530 ? integer_minus_one_node
1531 : integer_one_node);
1532 return fold_build3 (COND_EXPR, int_type_node, ifexp1,
1533 op == COMPARE_L_EXPR ? integer_one_node
1534 : integer_minus_one_node,
1535 second_compare);
1537 case COMPARE_EXPR:
1538 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1540 tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
1541 tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
1542 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1543 ifexp2, integer_one_node,
1544 integer_zero_node);
1545 return fold_build3 (COND_EXPR, int_type_node,
1546 ifexp1, integer_minus_one_node, second_compare);
1548 case TRUNC_DIV_EXPR:
1549 case TRUNC_MOD_EXPR:
1550 if (TREE_CODE (type) == REAL_TYPE
1551 && op == TRUNC_MOD_EXPR)
1553 tree call;
1554 if (type != double_type_node)
1556 arg1 = convert (double_type_node, arg1);
1557 arg2 = convert (double_type_node, arg2);
1559 call = build3 (CALL_EXPR, double_type_node,
1560 build_address_of (soft_fmod_node),
1561 tree_cons (NULL_TREE, arg1,
1562 build_tree_list (NULL_TREE, arg2)),
1563 NULL_TREE);
1564 if (type != double_type_node)
1565 call = convert (type, call);
1566 return call;
1569 if (TREE_CODE (type) == INTEGER_TYPE
1570 && flag_use_divide_subroutine
1571 && ! flag_syntax_only)
1572 return build_java_soft_divmod (op, type, arg1, arg2);
1574 break;
1575 default: ;
1577 return fold_build2 (op, type, arg1, arg2);
1580 static void
1581 expand_java_binop (tree type, enum tree_code op)
1583 tree larg, rarg;
1584 tree ltype = type;
1585 tree rtype = type;
1586 switch (op)
1588 case LSHIFT_EXPR:
1589 case RSHIFT_EXPR:
1590 case URSHIFT_EXPR:
1591 rtype = int_type_node;
1592 rarg = pop_value (rtype);
1593 break;
1594 default:
1595 rarg = pop_value (rtype);
1597 larg = pop_value (ltype);
1598 push_value (build_java_binop (op, type, larg, rarg));
1601 /* Lookup the field named NAME in *TYPEP or its super classes.
1602 If not found, return NULL_TREE.
1603 (If the *TYPEP is not found, or if the field reference is
1604 ambiguous, return error_mark_node.)
1605 If found, return the FIELD_DECL, and set *TYPEP to the
1606 class containing the field. */
1608 tree
1609 lookup_field (tree *typep, tree name)
1611 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1613 load_class (*typep, 1);
1614 safe_layout_class (*typep);
1615 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1616 return error_mark_node;
1620 tree field, binfo, base_binfo;
1621 tree save_field;
1622 int i;
1624 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1625 if (DECL_NAME (field) == name)
1626 return field;
1628 /* Process implemented interfaces. */
1629 save_field = NULL_TREE;
1630 for (binfo = TYPE_BINFO (*typep), i = 0;
1631 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1633 tree t = BINFO_TYPE (base_binfo);
1634 if ((field = lookup_field (&t, name)))
1636 if (save_field == field)
1637 continue;
1638 if (save_field == NULL_TREE)
1639 save_field = field;
1640 else
1642 tree i1 = DECL_CONTEXT (save_field);
1643 tree i2 = DECL_CONTEXT (field);
1644 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1645 IDENTIFIER_POINTER (name),
1646 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1647 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1648 return error_mark_node;
1653 if (save_field != NULL_TREE)
1654 return save_field;
1656 *typep = CLASSTYPE_SUPER (*typep);
1657 } while (*typep);
1658 return NULL_TREE;
1661 /* Look up the field named NAME in object SELF_VALUE,
1662 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1663 SELF_VALUE is NULL_TREE if looking for a static field. */
1665 tree
1666 build_field_ref (tree self_value, tree self_class, tree name)
1668 tree base_class = self_class;
1669 tree field_decl = lookup_field (&base_class, name);
1670 if (field_decl == NULL_TREE)
1672 error ("field %qs not found", IDENTIFIER_POINTER (name));
1673 return error_mark_node;
1675 if (self_value == NULL_TREE)
1677 return build_static_field_ref (field_decl);
1679 else
1681 int check = (flag_check_references
1682 && ! (DECL_P (self_value)
1683 && DECL_NAME (self_value) == this_identifier_node));
1685 tree base_type = promote_type (base_class);
1686 if (base_type != TREE_TYPE (self_value))
1687 self_value = fold_build1 (NOP_EXPR, base_type, self_value);
1688 if (! flag_syntax_only && flag_indirect_dispatch)
1690 tree otable_index
1691 = build_int_cst (NULL_TREE, get_symbol_table_index
1692 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1693 tree field_offset
1694 = build4 (ARRAY_REF, integer_type_node,
1695 TYPE_OTABLE_DECL (output_class), otable_index,
1696 NULL_TREE, NULL_TREE);
1697 tree address;
1699 if (DECL_CONTEXT (field_decl) != output_class)
1700 field_offset
1701 = build3 (COND_EXPR, TREE_TYPE (field_offset),
1702 build2 (EQ_EXPR, boolean_type_node,
1703 field_offset, integer_zero_node),
1704 build3 (CALL_EXPR, void_type_node,
1705 build_address_of (soft_nosuchfield_node),
1706 build_tree_list (NULL_TREE, otable_index),
1707 NULL_TREE),
1708 field_offset);
1710 field_offset = fold (convert (sizetype, field_offset));
1711 address
1712 = fold_build2 (PLUS_EXPR,
1713 build_pointer_type (TREE_TYPE (field_decl)),
1714 self_value, field_offset);
1715 return fold_build1 (INDIRECT_REF, TREE_TYPE (field_decl), address);
1718 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1719 self_value, check);
1720 return fold_build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1721 self_value, field_decl, NULL_TREE);
1725 tree
1726 lookup_label (int pc)
1728 tree name;
1729 char buf[32];
1730 if (pc > highest_label_pc_this_method)
1731 highest_label_pc_this_method = pc;
1732 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", start_label_pc_this_method + pc);
1733 name = get_identifier (buf);
1734 if (IDENTIFIER_LOCAL_VALUE (name))
1735 return IDENTIFIER_LOCAL_VALUE (name);
1736 else
1738 /* The type of the address of a label is return_address_type_node. */
1739 tree decl = create_label_decl (name);
1740 LABEL_PC (decl) = pc;
1741 return pushdecl (decl);
1745 /* Generate a unique name for the purpose of loops and switches
1746 labels, and try-catch-finally blocks label or temporary variables. */
1748 tree
1749 generate_name (void)
1751 static int l_number = 0;
1752 char buff [32];
1753 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1754 l_number++;
1755 return get_identifier (buff);
1758 tree
1759 create_label_decl (tree name)
1761 tree decl;
1762 decl = build_decl (LABEL_DECL, name,
1763 TREE_TYPE (return_address_type_node));
1764 DECL_CONTEXT (decl) = current_function_decl;
1765 DECL_IGNORED_P (decl) = 1;
1766 return decl;
1769 /* This maps a bytecode offset (PC) to various flags. */
1770 char *instruction_bits;
1772 static void
1773 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1775 lookup_label (target_pc);
1776 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1779 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1780 where CONDITION is one of one the compare operators. */
1782 static void
1783 expand_compare (enum tree_code condition, tree value1, tree value2,
1784 int target_pc)
1786 tree target = lookup_label (target_pc);
1787 tree cond = fold_build2 (condition, boolean_type_node, value1, value2);
1788 java_add_stmt
1789 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1790 build1 (GOTO_EXPR, void_type_node, target),
1791 build_java_empty_stmt ()));
1794 /* Emit code for a TEST-type opcode. */
1796 static void
1797 expand_test (enum tree_code condition, tree type, int target_pc)
1799 tree value1, value2;
1800 flush_quick_stack ();
1801 value1 = pop_value (type);
1802 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1803 expand_compare (condition, value1, value2, target_pc);
1806 /* Emit code for a COND-type opcode. */
1808 static void
1809 expand_cond (enum tree_code condition, tree type, int target_pc)
1811 tree value1, value2;
1812 flush_quick_stack ();
1813 /* note: pop values in opposite order */
1814 value2 = pop_value (type);
1815 value1 = pop_value (type);
1816 /* Maybe should check value1 and value2 for type compatibility ??? */
1817 expand_compare (condition, value1, value2, target_pc);
1820 static void
1821 expand_java_goto (int target_pc)
1823 tree target_label = lookup_label (target_pc);
1824 flush_quick_stack ();
1825 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1828 static tree
1829 expand_java_switch (tree selector, int default_pc)
1831 tree switch_expr, x;
1833 flush_quick_stack ();
1834 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1835 NULL_TREE, NULL_TREE);
1836 java_add_stmt (switch_expr);
1838 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1839 create_artificial_label ());
1840 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1842 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1843 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1845 return switch_expr;
1848 static void
1849 expand_java_add_case (tree switch_expr, int match, int target_pc)
1851 tree value, x;
1853 value = build_int_cst (TREE_TYPE (switch_expr), match);
1855 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1856 create_artificial_label ());
1857 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1859 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1860 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1863 static tree
1864 pop_arguments (tree arg_types)
1866 if (arg_types == end_params_node)
1867 return NULL_TREE;
1868 if (TREE_CODE (arg_types) == TREE_LIST)
1870 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1871 tree type = TREE_VALUE (arg_types);
1872 tree arg = pop_value (type);
1874 /* We simply cast each argument to its proper type. This is
1875 needed since we lose type information coming out of the
1876 verifier. We also have to do this when we pop an integer
1877 type that must be promoted for the function call. */
1878 if (TREE_CODE (type) == POINTER_TYPE)
1879 arg = build1 (NOP_EXPR, type, arg);
1880 else if (targetm.calls.promote_prototypes (type)
1881 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1882 && INTEGRAL_TYPE_P (type))
1883 arg = convert (integer_type_node, arg);
1884 return tree_cons (NULL_TREE, arg, tail);
1886 gcc_unreachable ();
1889 /* Attach to PTR (a block) the declaration found in ENTRY. */
1892 attach_init_test_initialization_flags (void **entry, void *ptr)
1894 tree block = (tree)ptr;
1895 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1897 if (block != error_mark_node)
1899 if (TREE_CODE (block) == BIND_EXPR)
1901 tree body = BIND_EXPR_BODY (block);
1902 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1903 BIND_EXPR_VARS (block) = ite->value;
1904 body = build2 (COMPOUND_EXPR, void_type_node,
1905 build1 (DECL_EXPR, void_type_node, ite->value), body);
1906 BIND_EXPR_BODY (block) = body;
1908 else
1910 tree body = BLOCK_SUBBLOCKS (block);
1911 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1912 BLOCK_EXPR_DECLS (block) = ite->value;
1913 body = build2 (COMPOUND_EXPR, void_type_node,
1914 build1 (DECL_EXPR, void_type_node, ite->value), body);
1915 BLOCK_SUBBLOCKS (block) = body;
1919 return true;
1922 /* Build an expression to initialize the class CLAS.
1923 if EXPR is non-NULL, returns an expression to first call the initializer
1924 (if it is needed) and then calls EXPR. */
1926 tree
1927 build_class_init (tree clas, tree expr)
1929 tree init;
1931 /* An optimization: if CLAS is a superclass of the class we're
1932 compiling, we don't need to initialize it. However, if CLAS is
1933 an interface, it won't necessarily be initialized, even if we
1934 implement it. */
1935 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1936 && inherits_from_p (current_class, clas))
1937 || current_class == clas)
1938 return expr;
1940 if (always_initialize_class_p)
1942 init = build3 (CALL_EXPR, void_type_node,
1943 build_address_of (soft_initclass_node),
1944 build_tree_list (NULL_TREE, build_class_ref (clas)),
1945 NULL_TREE);
1946 TREE_SIDE_EFFECTS (init) = 1;
1948 else
1950 tree *init_test_decl;
1951 tree decl;
1952 init_test_decl = java_treetreehash_new
1953 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1955 if (*init_test_decl == NULL)
1957 /* Build a declaration and mark it as a flag used to track
1958 static class initializations. */
1959 decl = build_decl (VAR_DECL, NULL_TREE,
1960 boolean_type_node);
1961 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1962 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
1963 DECL_CONTEXT (decl) = current_function_decl;
1964 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
1965 /* Tell the check-init code to ignore this decl when not
1966 optimizing class initialization. */
1967 if (!STATIC_CLASS_INIT_OPT_P ())
1968 DECL_BIT_INDEX (decl) = -1;
1969 DECL_INITIAL (decl) = boolean_false_node;
1970 /* Don't emit any symbolic debugging info for this decl. */
1971 DECL_IGNORED_P (decl) = 1;
1972 *init_test_decl = decl;
1975 init = build3 (CALL_EXPR, void_type_node,
1976 build_address_of (soft_initclass_node),
1977 build_tree_list (NULL_TREE, build_class_ref (clas)),
1978 NULL_TREE);
1979 TREE_SIDE_EFFECTS (init) = 1;
1980 init = build3 (COND_EXPR, void_type_node,
1981 build2 (EQ_EXPR, boolean_type_node,
1982 *init_test_decl, boolean_false_node),
1983 init, integer_zero_node);
1984 TREE_SIDE_EFFECTS (init) = 1;
1985 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
1986 build2 (MODIFY_EXPR, boolean_type_node,
1987 *init_test_decl, boolean_true_node));
1988 TREE_SIDE_EFFECTS (init) = 1;
1991 if (expr != NULL_TREE)
1993 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1994 TREE_SIDE_EFFECTS (expr) = 1;
1995 return expr;
1997 return init;
2000 tree
2001 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2002 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2003 tree arg_list ATTRIBUTE_UNUSED)
2005 tree func;
2006 if (is_compiled_class (self_type))
2008 /* With indirect dispatch we have to use indirect calls for all
2009 publicly visible methods or gcc will use PLT indirections
2010 to reach them. We also have to use indirect dispatch for all
2011 external methods. */
2012 if (! flag_indirect_dispatch
2013 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2015 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2016 method);
2018 else
2020 tree table_index
2021 = build_int_cst (NULL_TREE, get_symbol_table_index
2022 (method, &TYPE_ATABLE_METHODS (output_class)));
2023 func
2024 = build4 (ARRAY_REF,
2025 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2026 TYPE_ATABLE_DECL (output_class), table_index,
2027 NULL_TREE, NULL_TREE);
2029 func = convert (method_ptr_type_node, func);
2031 else
2033 /* We don't know whether the method has been (statically) compiled.
2034 Compile this code to get a reference to the method's code:
2036 SELF_TYPE->methods[METHOD_INDEX].ncode
2040 int method_index = 0;
2041 tree meth, ref;
2043 /* The method might actually be declared in some superclass, so
2044 we have to use its class context, not the caller's notion of
2045 where the method is. */
2046 self_type = DECL_CONTEXT (method);
2047 ref = build_class_ref (self_type);
2048 ref = build1 (INDIRECT_REF, class_type_node, ref);
2049 if (ncode_ident == NULL_TREE)
2050 ncode_ident = get_identifier ("ncode");
2051 if (methods_ident == NULL_TREE)
2052 methods_ident = get_identifier ("methods");
2053 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2054 lookup_field (&class_type_node, methods_ident),
2055 NULL_TREE);
2056 for (meth = TYPE_METHODS (self_type);
2057 ; meth = TREE_CHAIN (meth))
2059 if (method == meth)
2060 break;
2061 if (meth == NULL_TREE)
2062 fatal_error ("method '%s' not found in class",
2063 IDENTIFIER_POINTER (DECL_NAME (method)));
2064 method_index++;
2066 method_index *= int_size_in_bytes (method_type_node);
2067 ref = fold_build2 (PLUS_EXPR, method_ptr_type_node,
2068 ref, build_int_cst (NULL_TREE, method_index));
2069 ref = build1 (INDIRECT_REF, method_type_node, ref);
2070 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2071 ref, lookup_field (&method_type_node, ncode_ident),
2072 NULL_TREE);
2074 return func;
2077 tree
2078 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2080 tree dtable, objectref;
2082 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2084 /* If we're dealing with interfaces and if the objectref
2085 argument is an array then get the dispatch table of the class
2086 Object rather than the one from the objectref. */
2087 objectref = (is_invoke_interface
2088 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2089 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2091 if (dtable_ident == NULL_TREE)
2092 dtable_ident = get_identifier ("vtable");
2093 dtable = build_java_indirect_ref (object_type_node, objectref,
2094 flag_check_references);
2095 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2096 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2098 return dtable;
2101 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2102 T. If this decl has not been seen before, it will be added to the
2103 [oa]table_methods. If it has, the existing table slot will be
2104 reused. */
2107 get_symbol_table_index (tree t, tree *symbol_table)
2109 int i = 1;
2110 tree method_list;
2112 if (*symbol_table == NULL_TREE)
2114 *symbol_table = build_tree_list (t, t);
2115 return 1;
2118 method_list = *symbol_table;
2120 while (1)
2122 tree value = TREE_VALUE (method_list);
2123 if (value == t)
2124 return i;
2125 i++;
2126 if (TREE_CHAIN (method_list) == NULL_TREE)
2127 break;
2128 else
2129 method_list = TREE_CHAIN (method_list);
2132 TREE_CHAIN (method_list) = build_tree_list (t, t);
2133 return i;
2136 tree
2137 build_invokevirtual (tree dtable, tree method)
2139 tree func;
2140 tree nativecode_ptr_ptr_type_node
2141 = build_pointer_type (nativecode_ptr_type_node);
2142 tree method_index;
2143 tree otable_index;
2145 if (flag_indirect_dispatch)
2147 gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))));
2149 otable_index
2150 = build_int_cst (NULL_TREE, get_symbol_table_index
2151 (method, &TYPE_OTABLE_METHODS (output_class)));
2152 method_index = build4 (ARRAY_REF, integer_type_node,
2153 TYPE_OTABLE_DECL (output_class),
2154 otable_index, NULL_TREE, NULL_TREE);
2156 else
2158 /* We fetch the DECL_VINDEX field directly here, rather than
2159 using get_method_index(). DECL_VINDEX is the true offset
2160 from the vtable base to a method, regrdless of any extra
2161 words inserted at the start of the vtable. */
2162 method_index = DECL_VINDEX (method);
2163 method_index = size_binop (MULT_EXPR, method_index,
2164 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2165 if (TARGET_VTABLE_USES_DESCRIPTORS)
2166 method_index = size_binop (MULT_EXPR, method_index,
2167 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2170 func = fold_build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2171 convert (nativecode_ptr_ptr_type_node, method_index));
2173 if (TARGET_VTABLE_USES_DESCRIPTORS)
2174 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2175 else
2176 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2178 return func;
2181 static GTY(()) tree class_ident;
2182 tree
2183 build_invokeinterface (tree dtable, tree method)
2185 tree lookup_arg;
2186 tree interface;
2187 tree idx;
2189 /* We expand invokeinterface here. */
2191 if (class_ident == NULL_TREE)
2192 class_ident = get_identifier ("class");
2194 dtable = build_java_indirect_ref (dtable_type, dtable,
2195 flag_check_references);
2196 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2197 lookup_field (&dtable_type, class_ident), NULL_TREE);
2199 interface = DECL_CONTEXT (method);
2200 gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface)));
2201 layout_class_methods (interface);
2203 if (flag_indirect_dispatch)
2205 int itable_index
2206 = 2 * (get_symbol_table_index
2207 (method, &TYPE_ITABLE_METHODS (output_class)));
2208 interface
2209 = build4 (ARRAY_REF,
2210 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2211 TYPE_ITABLE_DECL (output_class),
2212 build_int_cst (NULL_TREE, itable_index-1),
2213 NULL_TREE, NULL_TREE);
2214 idx
2215 = build4 (ARRAY_REF,
2216 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2217 TYPE_ITABLE_DECL (output_class),
2218 build_int_cst (NULL_TREE, itable_index),
2219 NULL_TREE, NULL_TREE);
2220 interface = convert (class_ptr_type, interface);
2221 idx = convert (integer_type_node, idx);
2223 else
2225 idx = build_int_cst (NULL_TREE,
2226 get_interface_method_index (method, interface));
2227 interface = build_class_ref (interface);
2230 lookup_arg = tree_cons (NULL_TREE, dtable,
2231 tree_cons (NULL_TREE, interface,
2232 build_tree_list (NULL_TREE, idx)));
2234 return build3 (CALL_EXPR, ptr_type_node,
2235 build_address_of (soft_lookupinterfacemethod_node),
2236 lookup_arg, NULL_TREE);
2239 /* Expand one of the invoke_* opcodes.
2240 OPCODE is the specific opcode.
2241 METHOD_REF_INDEX is an index into the constant pool.
2242 NARGS is the number of arguments, or -1 if not specified. */
2244 static void
2245 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2247 tree method_signature
2248 = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2249 tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2250 method_ref_index);
2251 tree self_type
2252 = get_class_constant (current_jcf,
2253 COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2254 method_ref_index));
2255 const char *const self_name
2256 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2257 tree call, func, method, arg_list, method_type;
2258 tree check = NULL_TREE;
2260 if (! CLASS_LOADED_P (self_type))
2262 load_class (self_type, 1);
2263 safe_layout_class (self_type);
2264 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2265 fatal_error ("failed to find class '%s'", self_name);
2267 layout_class_methods (self_type);
2269 if (ID_INIT_P (method_name))
2270 method = lookup_java_constructor (self_type, method_signature);
2271 else
2272 method = lookup_java_method (self_type, method_name, method_signature);
2274 /* We've found a method in a class other than the one in which it
2275 was wanted. This can happen if, for instance, we're trying to
2276 compile invokespecial super.equals().
2277 FIXME: This is a kludge. Rather than nullifying the result, we
2278 should change lookup_java_method() so that it doesn't search the
2279 superclass chain when we're BC-compiling. */
2280 if (! flag_verify_invocations
2281 && method
2282 && ! TYPE_ARRAY_P (self_type)
2283 && self_type != DECL_CONTEXT (method))
2284 method = NULL_TREE;
2286 /* We've found a method in an interface, but this isn't an interface
2287 call. */
2288 if (opcode != OPCODE_invokeinterface
2289 && method
2290 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2291 method = NULL_TREE;
2293 /* We've found a non-interface method but we are making an
2294 interface call. This can happen if the interface overrides a
2295 method in Object. */
2296 if (! flag_verify_invocations
2297 && opcode == OPCODE_invokeinterface
2298 && method
2299 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2300 method = NULL_TREE;
2302 if (method == NULL_TREE)
2304 if (flag_verify_invocations || ! flag_indirect_dispatch)
2306 error ("class '%s' has no method named '%s' matching signature '%s'",
2307 self_name,
2308 IDENTIFIER_POINTER (method_name),
2309 IDENTIFIER_POINTER (method_signature));
2311 else
2313 int flags = ACC_PUBLIC;
2314 if (opcode == OPCODE_invokestatic)
2315 flags |= ACC_STATIC;
2316 if (opcode == OPCODE_invokeinterface)
2318 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2319 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2321 method = add_method (self_type, flags, method_name,
2322 method_signature);
2323 DECL_ARTIFICIAL (method) = 1;
2324 METHOD_DUMMY (method) = 1;
2325 layout_class_method (self_type, NULL,
2326 method, NULL);
2330 /* Invoke static can't invoke static/abstract method */
2331 if (method != NULL_TREE)
2333 if (opcode == OPCODE_invokestatic)
2335 if (!METHOD_STATIC (method))
2337 error ("invokestatic on non static method");
2338 method = NULL_TREE;
2340 else if (METHOD_ABSTRACT (method))
2342 error ("invokestatic on abstract method");
2343 method = NULL_TREE;
2346 else
2348 if (METHOD_STATIC (method))
2350 error ("invoke[non-static] on static method");
2351 method = NULL_TREE;
2356 if (method == NULL_TREE)
2358 /* If we got here, we emitted an error message above. So we
2359 just pop the arguments, push a properly-typed zero, and
2360 continue. */
2361 method_type = get_type_from_signature (method_signature);
2362 pop_arguments (TYPE_ARG_TYPES (method_type));
2363 if (opcode != OPCODE_invokestatic)
2364 pop_type (self_type);
2365 method_type = promote_type (TREE_TYPE (method_type));
2366 push_value (convert (method_type, integer_zero_node));
2367 return;
2370 method_type = TREE_TYPE (method);
2371 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2372 flush_quick_stack ();
2374 func = NULL_TREE;
2375 if (opcode == OPCODE_invokestatic)
2376 func = build_known_method_ref (method, method_type, self_type,
2377 method_signature, arg_list);
2378 else if (opcode == OPCODE_invokespecial
2379 || (opcode == OPCODE_invokevirtual
2380 && (METHOD_PRIVATE (method)
2381 || METHOD_FINAL (method)
2382 || CLASS_FINAL (TYPE_NAME (self_type)))))
2384 /* If the object for the method call is null, we throw an
2385 exception. We don't do this if the object is the current
2386 method's `this'. In other cases we just rely on an
2387 optimization pass to eliminate redundant checks. FIXME:
2388 Unfortunately there doesn't seem to be a way to determine
2389 what the current method is right now.
2390 We do omit the check if we're calling <init>. */
2391 /* We use a SAVE_EXPR here to make sure we only evaluate
2392 the new `self' expression once. */
2393 tree save_arg = save_expr (TREE_VALUE (arg_list));
2394 TREE_VALUE (arg_list) = save_arg;
2395 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2396 func = build_known_method_ref (method, method_type, self_type,
2397 method_signature, arg_list);
2399 else
2401 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2402 arg_list);
2403 if (opcode == OPCODE_invokevirtual)
2404 func = build_invokevirtual (dtable, method);
2405 else
2406 func = build_invokeinterface (dtable, method);
2409 if (TREE_CODE (func) == ADDR_EXPR)
2410 TREE_TYPE (func) = build_pointer_type (method_type);
2411 else
2412 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2414 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2415 func, arg_list, NULL_TREE);
2416 TREE_SIDE_EFFECTS (call) = 1;
2417 call = check_for_builtin (method, call);
2419 if (check != NULL_TREE)
2421 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2422 TREE_SIDE_EFFECTS (call) = 1;
2425 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2426 java_add_stmt (call);
2427 else
2429 push_value (call);
2430 flush_quick_stack ();
2434 /* Create a stub which will be put into the vtable but which will call
2435 a JNI function. */
2437 tree
2438 build_jni_stub (tree method)
2440 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2441 tree jni_func_type, tem;
2442 tree env_var, res_var = NULL_TREE, block;
2443 tree method_args, res_type;
2444 tree meth_var;
2445 tree bind;
2447 int args_size = 0;
2449 tree klass = DECL_CONTEXT (method);
2450 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2451 klass = build_class_ref (klass);
2453 gcc_assert (METHOD_NATIVE (method) && flag_jni);
2455 DECL_ARTIFICIAL (method) = 1;
2456 DECL_EXTERNAL (method) = 0;
2458 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2459 DECL_CONTEXT (env_var) = method;
2461 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2463 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2464 TREE_TYPE (TREE_TYPE (method)));
2465 DECL_CONTEXT (res_var) = method;
2466 TREE_CHAIN (env_var) = res_var;
2469 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2470 TREE_STATIC (meth_var) = 1;
2471 TREE_PUBLIC (meth_var) = 0;
2472 DECL_EXTERNAL (meth_var) = 0;
2473 DECL_CONTEXT (meth_var) = method;
2474 DECL_ARTIFICIAL (meth_var) = 1;
2475 DECL_INITIAL (meth_var) = null_pointer_node;
2476 TREE_USED (meth_var) = 1;
2477 chainon (env_var, meth_var);
2478 build_result_decl (method);
2480 /* One strange way that the front ends are different is that they
2481 store arguments differently. */
2482 if (from_class)
2483 method_args = DECL_ARGUMENTS (method);
2484 else
2485 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2486 block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
2487 TREE_SIDE_EFFECTS (block) = 1;
2488 /* When compiling from source we don't set the type of the block,
2489 because that will prevent patch_return from ever being run. */
2490 if (from_class)
2491 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2493 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2494 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2495 build3 (CALL_EXPR, ptr_type_node,
2496 build_address_of (soft_getjnienvnewframe_node),
2497 build_tree_list (NULL_TREE, klass),
2498 NULL_TREE));
2499 CAN_COMPLETE_NORMALLY (body) = 1;
2501 /* All the arguments to this method become arguments to the
2502 underlying JNI function. If we had to wrap object arguments in a
2503 special way, we would do that here. */
2504 args = NULL_TREE;
2505 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2507 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2508 #ifdef PARM_BOUNDARY
2509 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2510 * PARM_BOUNDARY);
2511 #endif
2512 args_size += (arg_bits / BITS_PER_UNIT);
2514 args = tree_cons (NULL_TREE, tem, args);
2516 args = nreverse (args);
2517 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2519 /* For a static method the second argument is the class. For a
2520 non-static method the second argument is `this'; that is already
2521 available in the argument list. */
2522 if (METHOD_STATIC (method))
2524 args_size += int_size_in_bytes (TREE_TYPE (klass));
2525 args = tree_cons (NULL_TREE, klass, args);
2526 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2529 /* The JNIEnv structure is the first argument to the JNI function. */
2530 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2531 args = tree_cons (NULL_TREE, env_var, args);
2532 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2534 /* We call _Jv_LookupJNIMethod to find the actual underlying
2535 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2536 exception if this function is not found at runtime. */
2537 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2538 method_sig = build_java_signature (TREE_TYPE (method));
2539 lookup_arg = tree_cons (NULL_TREE,
2540 build_utf8_ref (unmangle_classname
2541 (IDENTIFIER_POINTER (method_sig),
2542 IDENTIFIER_LENGTH (method_sig))),
2543 tem);
2544 tem = DECL_NAME (method);
2545 lookup_arg
2546 = tree_cons (NULL_TREE, klass,
2547 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2549 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2551 #ifdef MODIFY_JNI_METHOD_CALL
2552 tem = MODIFY_JNI_METHOD_CALL (tem);
2553 #endif
2555 jni_func_type = build_pointer_type (tem);
2557 jnifunc = build3 (COND_EXPR, ptr_type_node,
2558 meth_var, meth_var,
2559 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2560 build3 (CALL_EXPR, ptr_type_node,
2561 build_address_of
2562 (soft_lookupjnimethod_node),
2563 lookup_arg, NULL_TREE)));
2565 /* Now we make the actual JNI call via the resulting function
2566 pointer. */
2567 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2568 build1 (NOP_EXPR, jni_func_type, jnifunc),
2569 args, NULL_TREE);
2571 /* If the JNI call returned a result, capture it here. If we had to
2572 unwrap JNI object results, we would do that here. */
2573 if (res_var != NULL_TREE)
2575 /* If the call returns an object, it may return a JNI weak
2576 reference, in which case we must unwrap it. */
2577 if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
2578 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2579 build_address_of (soft_unwrapjni_node),
2580 build_tree_list (NULL_TREE, call),
2581 NULL_TREE);
2582 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2583 res_var, call);
2586 TREE_SIDE_EFFECTS (call) = 1;
2587 CAN_COMPLETE_NORMALLY (call) = 1;
2589 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2590 TREE_SIDE_EFFECTS (body) = 1;
2592 /* Now free the environment we allocated. */
2593 call = build3 (CALL_EXPR, ptr_type_node,
2594 build_address_of (soft_jnipopsystemframe_node),
2595 build_tree_list (NULL_TREE, env_var),
2596 NULL_TREE);
2597 TREE_SIDE_EFFECTS (call) = 1;
2598 CAN_COMPLETE_NORMALLY (call) = 1;
2599 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2600 TREE_SIDE_EFFECTS (body) = 1;
2602 /* Finally, do the return. */
2603 res_type = void_type_node;
2604 if (res_var != NULL_TREE)
2606 tree drt;
2607 gcc_assert (DECL_RESULT (method));
2608 /* Make sure we copy the result variable to the actual
2609 result. We use the type of the DECL_RESULT because it
2610 might be different from the return type of the function:
2611 it might be promoted. */
2612 drt = TREE_TYPE (DECL_RESULT (method));
2613 if (drt != TREE_TYPE (res_var))
2614 res_var = build1 (CONVERT_EXPR, drt, res_var);
2615 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2616 TREE_SIDE_EFFECTS (res_var) = 1;
2619 body = build2 (COMPOUND_EXPR, void_type_node, body,
2620 build1 (RETURN_EXPR, res_type, res_var));
2621 TREE_SIDE_EFFECTS (body) = 1;
2623 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2624 body, block);
2625 return bind;
2628 /* Expand an operation to extract from or store into a field.
2629 IS_STATIC is 1 iff the field is static.
2630 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2631 FIELD_REF_INDEX is an index into the constant pool. */
2633 static void
2634 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2636 tree self_type
2637 = get_class_constant (current_jcf,
2638 COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2639 field_ref_index));
2640 const char *self_name
2641 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2642 tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2643 tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
2644 field_ref_index);
2645 tree field_type = get_type_from_signature (field_signature);
2646 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2647 tree field_ref;
2648 int is_error = 0;
2649 tree original_self_type = self_type;
2650 tree field_decl;
2652 if (! CLASS_LOADED_P (self_type))
2653 load_class (self_type, 1);
2654 field_decl = lookup_field (&self_type, field_name);
2655 if (field_decl == error_mark_node)
2657 is_error = 1;
2659 else if (field_decl == NULL_TREE)
2661 if (! flag_verify_invocations)
2663 int flags = ACC_PUBLIC;
2664 if (is_static)
2665 flags |= ACC_STATIC;
2666 self_type = original_self_type;
2667 field_decl = add_field (original_self_type, field_name,
2668 field_type, flags);
2669 DECL_ARTIFICIAL (field_decl) = 1;
2670 DECL_IGNORED_P (field_decl) = 1;
2672 else
2674 error ("missing field '%s' in '%s'",
2675 IDENTIFIER_POINTER (field_name), self_name);
2676 is_error = 1;
2679 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2681 error ("mismatching signature for field '%s' in '%s'",
2682 IDENTIFIER_POINTER (field_name), self_name);
2683 is_error = 1;
2685 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2686 if (is_error)
2688 if (! is_putting)
2689 push_value (convert (field_type, integer_zero_node));
2690 flush_quick_stack ();
2691 return;
2694 field_ref = build_field_ref (field_ref, self_type, field_name);
2695 if (is_static
2696 && ! flag_indirect_dispatch)
2697 field_ref = build_class_init (self_type, field_ref);
2698 if (is_putting)
2700 flush_quick_stack ();
2701 if (FIELD_FINAL (field_decl))
2703 if (DECL_CONTEXT (field_decl) != current_class)
2704 error ("assignment to final field %q+D not in field's class",
2705 field_decl);
2706 else if (FIELD_STATIC (field_decl))
2708 if (!DECL_CLINIT_P (current_function_decl))
2709 warning (0, "assignment to final static field %q+D not in "
2710 "class initializer",
2711 field_decl);
2713 else
2715 tree cfndecl_name = DECL_NAME (current_function_decl);
2716 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2717 && !ID_FINIT_P (cfndecl_name))
2718 warning (0, "assignment to final field %q+D not in constructor",
2719 field_decl);
2722 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2723 field_ref, new_value));
2725 else
2726 push_value (field_ref);
2729 void
2730 load_type_state (tree label)
2732 int i;
2733 tree vec = LABEL_TYPE_STATE (label);
2734 int cur_length = TREE_VEC_LENGTH (vec);
2735 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2736 for (i = 0; i < cur_length; i++)
2737 type_map [i] = TREE_VEC_ELT (vec, i);
2740 /* Go over METHOD's bytecode and note instruction starts in
2741 instruction_bits[]. */
2743 void
2744 note_instructions (JCF *jcf, tree method)
2746 int PC;
2747 unsigned char* byte_ops;
2748 long length = DECL_CODE_LENGTH (method);
2750 int saw_index;
2751 jint INT_temp;
2753 #undef RET /* Defined by config/i386/i386.h */
2754 #undef PTR
2755 #define BCODE byte_ops
2756 #define BYTE_type_node byte_type_node
2757 #define SHORT_type_node short_type_node
2758 #define INT_type_node int_type_node
2759 #define LONG_type_node long_type_node
2760 #define CHAR_type_node char_type_node
2761 #define PTR_type_node ptr_type_node
2762 #define FLOAT_type_node float_type_node
2763 #define DOUBLE_type_node double_type_node
2764 #define VOID_type_node void_type_node
2765 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2766 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2767 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2768 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2770 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2772 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2773 byte_ops = jcf->read_ptr;
2774 instruction_bits = xrealloc (instruction_bits, length + 1);
2775 memset (instruction_bits, 0, length + 1);
2777 /* This pass figures out which PC can be the targets of jumps. */
2778 for (PC = 0; PC < length;)
2780 int oldpc = PC; /* PC at instruction start. */
2781 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2782 switch (byte_ops[PC++])
2784 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2785 case OPCODE: \
2786 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2787 break;
2789 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2791 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2792 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2793 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2794 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2795 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2796 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2797 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2798 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2800 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2801 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2802 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2803 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2804 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2805 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2806 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2807 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2809 /* two forms of wide instructions */
2810 #define PRE_SPECIAL_WIDE(IGNORE) \
2812 int modified_opcode = IMMEDIATE_u1; \
2813 if (modified_opcode == OPCODE_iinc) \
2815 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2816 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2818 else \
2820 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2824 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2826 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2828 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2829 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2830 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2831 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2832 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2833 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2834 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2835 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2836 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2837 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2839 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2840 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2841 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2842 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2843 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2844 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2845 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2846 NOTE_LABEL (PC); \
2847 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2849 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2851 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2852 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2854 #define PRE_LOOKUP_SWITCH \
2855 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2856 NOTE_LABEL (default_offset+oldpc); \
2857 if (npairs >= 0) \
2858 while (--npairs >= 0) { \
2859 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2860 jint offset = IMMEDIATE_s4; \
2861 NOTE_LABEL (offset+oldpc); } \
2864 #define PRE_TABLE_SWITCH \
2865 { jint default_offset = IMMEDIATE_s4; \
2866 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2867 NOTE_LABEL (default_offset+oldpc); \
2868 if (low <= high) \
2869 while (low++ <= high) { \
2870 jint offset = IMMEDIATE_s4; \
2871 NOTE_LABEL (offset+oldpc); } \
2874 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2875 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2876 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2877 (void)(IMMEDIATE_u2); \
2878 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2880 #include "javaop.def"
2881 #undef JAVAOP
2883 } /* for */
2886 void
2887 expand_byte_code (JCF *jcf, tree method)
2889 int PC;
2890 int i;
2891 const unsigned char *linenumber_pointer;
2892 int dead_code_index = -1;
2893 unsigned char* byte_ops;
2894 long length = DECL_CODE_LENGTH (method);
2896 stack_pointer = 0;
2897 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2898 byte_ops = jcf->read_ptr;
2900 /* We make an initial pass of the line number table, to note
2901 which instructions have associated line number entries. */
2902 linenumber_pointer = linenumber_table;
2903 for (i = 0; i < linenumber_count; i++)
2905 int pc = GET_u2 (linenumber_pointer);
2906 linenumber_pointer += 4;
2907 if (pc >= length)
2908 warning (0, "invalid PC in line number table");
2909 else
2911 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2912 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2913 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2917 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2918 return;
2920 promote_arguments ();
2922 /* Translate bytecodes. */
2923 linenumber_pointer = linenumber_table;
2924 for (PC = 0; PC < length;)
2926 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2928 tree label = lookup_label (PC);
2929 flush_quick_stack ();
2930 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2931 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2932 if (LABEL_VERIFIED (label) || PC == 0)
2933 load_type_state (label);
2936 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2938 if (dead_code_index == -1)
2940 /* This is the start of a region of unreachable bytecodes.
2941 They still need to be processed in order for EH ranges
2942 to get handled correctly. However, we can simply
2943 replace these bytecodes with nops. */
2944 dead_code_index = PC;
2947 /* Turn this bytecode into a nop. */
2948 byte_ops[PC] = 0x0;
2950 else
2952 if (dead_code_index != -1)
2954 /* We've just reached the end of a region of dead code. */
2955 if (extra_warnings)
2956 warning (0, "unreachable bytecode from %d to before %d",
2957 dead_code_index, PC);
2958 dead_code_index = -1;
2962 /* Handle possible line number entry for this PC.
2964 This code handles out-of-order and multiple linenumbers per PC,
2965 but is optimized for the case of line numbers increasing
2966 monotonically with PC. */
2967 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2969 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2970 || GET_u2 (linenumber_pointer) != PC)
2971 linenumber_pointer = linenumber_table;
2972 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2974 int pc = GET_u2 (linenumber_pointer);
2975 linenumber_pointer += 4;
2976 if (pc == PC)
2978 int line = GET_u2 (linenumber_pointer - 2);
2979 #ifdef USE_MAPPED_LOCATION
2980 input_location = linemap_line_start (&line_table, line, 1);
2981 #else
2982 input_location.line = line;
2983 #endif
2984 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2985 break;
2989 maybe_pushlevels (PC);
2990 PC = process_jvm_instruction (PC, byte_ops, length);
2991 maybe_poplevels (PC);
2992 } /* for */
2994 if (dead_code_index != -1)
2996 /* We've just reached the end of a region of dead code. */
2997 if (extra_warnings)
2998 warning (0, "unreachable bytecode from %d to the end of the method",
2999 dead_code_index);
3003 static void
3004 java_push_constant_from_pool (JCF *jcf, int index)
3006 tree c;
3007 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3009 tree name;
3010 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3011 index = alloc_name_constant (CONSTANT_String, name);
3012 c = build_ref_from_constant_pool (index);
3013 c = convert (promote_type (string_type_node), c);
3015 else
3016 c = get_constant (jcf, index);
3017 push_value (c);
3021 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3022 long length ATTRIBUTE_UNUSED)
3024 const char *opname; /* Temporary ??? */
3025 int oldpc = PC; /* PC at instruction start. */
3027 /* If the instruction is at the beginning of an exception handler,
3028 replace the top of the stack with the thrown object reference. */
3029 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3031 /* Note that the verifier will not emit a type map at all for
3032 dead exception handlers. In this case we just ignore the
3033 situation. */
3034 if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
3036 tree type = pop_type (promote_type (throwable_type_node));
3037 push_value (build_exception_object_ref (type));
3041 switch (byte_ops[PC++])
3043 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3044 case OPCODE: \
3045 opname = #OPNAME; \
3046 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3047 break;
3049 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3051 int saw_index = 0; \
3052 int index = OPERAND_VALUE; \
3053 build_java_ret \
3054 (find_local_variable (index, return_address_type_node, oldpc)); \
3057 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3059 /* OPERAND_VALUE may have side-effects on PC */ \
3060 int opvalue = OPERAND_VALUE; \
3061 build_java_jsr (oldpc + opvalue, PC); \
3064 /* Push a constant onto the stack. */
3065 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3066 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3067 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3068 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3070 /* internal macro added for use by the WIDE case */
3071 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3072 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3074 /* Push local variable onto the opcode stack. */
3075 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3077 /* have to do this since OPERAND_VALUE may have side-effects */ \
3078 int opvalue = OPERAND_VALUE; \
3079 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3082 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3083 expand_java_return (OPERAND_TYPE##_type_node)
3085 #define REM_EXPR TRUNC_MOD_EXPR
3086 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3087 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3089 #define FIELD(IS_STATIC, IS_PUT) \
3090 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3092 #define TEST(OPERAND_TYPE, CONDITION) \
3093 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3095 #define COND(OPERAND_TYPE, CONDITION) \
3096 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3098 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3099 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3101 #define BRANCH_GOTO(OPERAND_VALUE) \
3102 expand_java_goto (oldpc + OPERAND_VALUE)
3104 #define BRANCH_CALL(OPERAND_VALUE) \
3105 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3107 #if 0
3108 #define BRANCH_RETURN(OPERAND_VALUE) \
3110 tree type = OPERAND_TYPE##_type_node; \
3111 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3112 expand_java_ret (value); \
3114 #endif
3116 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3117 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3118 fprintf (stderr, "(not implemented)\n")
3119 #define NOT_IMPL1(OPERAND_VALUE) \
3120 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3121 fprintf (stderr, "(not implemented)\n")
3123 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3125 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3127 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3129 #define STACK_SWAP(COUNT) java_stack_swap()
3131 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3132 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3133 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3135 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3136 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3138 #define LOOKUP_SWITCH \
3139 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3140 tree selector = pop_value (INT_type_node); \
3141 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3142 while (--npairs >= 0) \
3144 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3145 expand_java_add_case (switch_expr, match, oldpc + offset); \
3149 #define TABLE_SWITCH \
3150 { jint default_offset = IMMEDIATE_s4; \
3151 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3152 tree selector = pop_value (INT_type_node); \
3153 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3154 for (; low <= high; low++) \
3156 jint offset = IMMEDIATE_s4; \
3157 expand_java_add_case (switch_expr, low, oldpc + offset); \
3161 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3162 { int opcode = byte_ops[PC-1]; \
3163 int method_ref_index = IMMEDIATE_u2; \
3164 int nargs; \
3165 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3166 else nargs = -1; \
3167 expand_invoke (opcode, method_ref_index, nargs); \
3170 /* Handle new, checkcast, instanceof */
3171 #define OBJECT(TYPE, OP) \
3172 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3174 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3176 #define ARRAY_LOAD(OPERAND_TYPE) \
3178 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3181 #define ARRAY_STORE(OPERAND_TYPE) \
3183 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3186 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3187 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3188 #define ARRAY_NEW_PTR() \
3189 push_value (build_anewarray (get_class_constant (current_jcf, \
3190 IMMEDIATE_u2), \
3191 pop_value (int_type_node)));
3192 #define ARRAY_NEW_NUM() \
3194 int atype = IMMEDIATE_u1; \
3195 push_value (build_newarray (atype, pop_value (int_type_node)));\
3197 #define ARRAY_NEW_MULTI() \
3199 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3200 int ndims = IMMEDIATE_u1; \
3201 expand_java_multianewarray( class, ndims ); \
3204 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3205 push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3206 pop_value (OPERAND_TYPE##_type_node)));
3208 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3210 push_value (build1 (NOP_EXPR, int_type_node, \
3211 (convert (TO_TYPE##_type_node, \
3212 pop_value (FROM_TYPE##_type_node))))); \
3215 #define CONVERT(FROM_TYPE, TO_TYPE) \
3217 push_value (convert (TO_TYPE##_type_node, \
3218 pop_value (FROM_TYPE##_type_node))); \
3221 /* internal macro added for use by the WIDE case
3222 Added TREE_TYPE (decl) assignment, apbianco */
3223 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3225 tree decl, value; \
3226 int index = OPVALUE; \
3227 tree type = OPTYPE; \
3228 value = pop_value (type); \
3229 type = TREE_TYPE (value); \
3230 decl = find_local_variable (index, type, oldpc); \
3231 set_local_type (index, type); \
3232 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3233 update_aliases (decl, index, PC); \
3236 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3238 /* have to do this since OPERAND_VALUE may have side-effects */ \
3239 int opvalue = OPERAND_VALUE; \
3240 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3243 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3244 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3246 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3247 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3249 #define MONITOR_OPERATION(call) \
3251 tree o = pop_value (ptr_type_node); \
3252 tree c; \
3253 flush_quick_stack (); \
3254 c = build_java_monitor (call, o); \
3255 TREE_SIDE_EFFECTS (c) = 1; \
3256 java_add_stmt (c); \
3259 #define SPECIAL_IINC(IGNORED) \
3261 unsigned int local_var_index = IMMEDIATE_u1; \
3262 int ival = IMMEDIATE_s1; \
3263 expand_iinc(local_var_index, ival, oldpc); \
3266 #define SPECIAL_WIDE(IGNORED) \
3268 int modified_opcode = IMMEDIATE_u1; \
3269 unsigned int local_var_index = IMMEDIATE_u2; \
3270 switch (modified_opcode) \
3272 case OPCODE_iinc: \
3274 int ival = IMMEDIATE_s2; \
3275 expand_iinc (local_var_index, ival, oldpc); \
3276 break; \
3278 case OPCODE_iload: \
3279 case OPCODE_lload: \
3280 case OPCODE_fload: \
3281 case OPCODE_dload: \
3282 case OPCODE_aload: \
3284 /* duplicate code from LOAD macro */ \
3285 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3286 break; \
3288 case OPCODE_istore: \
3289 case OPCODE_lstore: \
3290 case OPCODE_fstore: \
3291 case OPCODE_dstore: \
3292 case OPCODE_astore: \
3294 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3295 break; \
3297 default: \
3298 error ("unrecogized wide sub-instruction"); \
3302 #define SPECIAL_THROW(IGNORED) \
3303 build_java_athrow (pop_value (throwable_type_node))
3305 #define SPECIAL_BREAK NOT_IMPL1
3306 #define IMPL NOT_IMPL
3308 #include "javaop.def"
3309 #undef JAVAOP
3310 default:
3311 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3313 return PC;
3316 /* Return the opcode at PC in the code section pointed to by
3317 CODE_OFFSET. */
3319 static unsigned char
3320 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3322 unsigned char opcode;
3323 long absolute_offset = (long)JCF_TELL (jcf);
3325 JCF_SEEK (jcf, code_offset);
3326 opcode = jcf->read_ptr [pc];
3327 JCF_SEEK (jcf, absolute_offset);
3328 return opcode;
3331 /* Some bytecode compilers are emitting accurate LocalVariableTable
3332 attributes. Here's an example:
3334 PC <t>store_<n>
3335 PC+1 ...
3337 Attribute "LocalVariableTable"
3338 slot #<n>: ... (PC: PC+1 length: L)
3340 This is accurate because the local in slot <n> really exists after
3341 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3343 This procedure recognizes this situation and extends the live range
3344 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3345 length of the store instruction.)
3347 This function is used by `give_name_to_locals' so that a local's
3348 DECL features a DECL_LOCAL_START_PC such that the first related
3349 store operation will use DECL as a destination, not an unrelated
3350 temporary created for the occasion.
3352 This function uses a global (instruction_bits) `note_instructions' should
3353 have allocated and filled properly. */
3356 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3357 int start_pc, int slot)
3359 int first, index, opcode;
3360 int pc, insn_pc;
3361 int wide_found = 0;
3363 if (!start_pc)
3364 return start_pc;
3366 first = index = -1;
3368 /* Find last previous instruction and remember it */
3369 for (pc = start_pc-1; pc; pc--)
3370 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3371 break;
3372 insn_pc = pc;
3374 /* Retrieve the instruction, handle `wide'. */
3375 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3376 if (opcode == OPCODE_wide)
3378 wide_found = 1;
3379 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3382 switch (opcode)
3384 case OPCODE_astore_0:
3385 case OPCODE_astore_1:
3386 case OPCODE_astore_2:
3387 case OPCODE_astore_3:
3388 first = OPCODE_astore_0;
3389 break;
3391 case OPCODE_istore_0:
3392 case OPCODE_istore_1:
3393 case OPCODE_istore_2:
3394 case OPCODE_istore_3:
3395 first = OPCODE_istore_0;
3396 break;
3398 case OPCODE_lstore_0:
3399 case OPCODE_lstore_1:
3400 case OPCODE_lstore_2:
3401 case OPCODE_lstore_3:
3402 first = OPCODE_lstore_0;
3403 break;
3405 case OPCODE_fstore_0:
3406 case OPCODE_fstore_1:
3407 case OPCODE_fstore_2:
3408 case OPCODE_fstore_3:
3409 first = OPCODE_fstore_0;
3410 break;
3412 case OPCODE_dstore_0:
3413 case OPCODE_dstore_1:
3414 case OPCODE_dstore_2:
3415 case OPCODE_dstore_3:
3416 first = OPCODE_dstore_0;
3417 break;
3419 case OPCODE_astore:
3420 case OPCODE_istore:
3421 case OPCODE_lstore:
3422 case OPCODE_fstore:
3423 case OPCODE_dstore:
3424 index = peek_opcode_at_pc (jcf, code_offset, pc);
3425 if (wide_found)
3427 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3428 index = (other << 8) + index;
3430 break;
3433 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3434 means we have a <t>store. */
3435 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3436 start_pc = insn_pc;
3438 return start_pc;
3441 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3442 order, as specified by Java Language Specification.
3444 The problem is that while expand_expr will evaluate its sub-operands in
3445 left-to-right order, for variables it will just return an rtx (i.e.
3446 an lvalue) for the variable (rather than an rvalue). So it is possible
3447 that a later sub-operand will change the register, and when the
3448 actual operation is done, it will use the new value, when it should
3449 have used the original value.
3451 We fix this by using save_expr. This forces the sub-operand to be
3452 copied into a fresh virtual register,
3454 For method invocation, we modify the arguments so that a
3455 left-to-right order evaluation is performed. Saved expressions
3456 will, in CALL_EXPR order, be reused when the call will be expanded.
3458 We also promote outgoing args if needed. */
3460 tree
3461 force_evaluation_order (tree node)
3463 if (flag_syntax_only)
3464 return node;
3465 if (TREE_CODE (node) == CALL_EXPR
3466 || TREE_CODE (node) == NEW_CLASS_EXPR
3467 || (TREE_CODE (node) == COMPOUND_EXPR
3468 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3469 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3471 tree arg, cmp;
3473 arg = node;
3475 /* Position arg properly, account for wrapped around ctors. */
3476 if (TREE_CODE (node) == COMPOUND_EXPR)
3477 arg = TREE_OPERAND (node, 0);
3479 arg = TREE_OPERAND (arg, 1);
3481 /* An empty argument list is ok, just ignore it. */
3482 if (!arg)
3483 return node;
3485 /* Not having a list of arguments here is an error. */
3486 gcc_assert (TREE_CODE (arg) == TREE_LIST);
3488 /* This reverses the evaluation order. This is a desired effect. */
3489 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3491 /* Promote types smaller than integer. This is required by
3492 some ABIs. */
3493 tree type = TREE_TYPE (TREE_VALUE (arg));
3494 tree saved;
3495 if (targetm.calls.promote_prototypes (type)
3496 && INTEGRAL_TYPE_P (type)
3497 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
3498 TYPE_SIZE (integer_type_node)))
3499 TREE_VALUE (arg) = fold_convert (integer_type_node, TREE_VALUE (arg));
3501 saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3502 cmp = (cmp == NULL_TREE ? saved :
3503 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3504 TREE_VALUE (arg) = saved;
3507 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3508 TREE_SIDE_EFFECTS (cmp) = 1;
3510 if (cmp)
3512 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3513 if (TREE_TYPE (cmp) != void_type_node)
3514 cmp = save_expr (cmp);
3515 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3516 TREE_SIDE_EFFECTS (cmp) = 1;
3517 node = cmp;
3520 return node;
3523 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3524 location where an expression or an identifier were encountered. It
3525 is necessary for languages where the frontend parser will handle
3526 recursively more than one file (Java is one of them). */
3528 tree
3529 build_expr_wfl (tree node,
3530 #ifdef USE_MAPPED_LOCATION
3531 source_location location
3532 #else
3533 const char *file, int line, int col
3534 #endif
3537 tree wfl;
3539 #ifdef USE_MAPPED_LOCATION
3540 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3541 SET_EXPR_LOCATION (wfl, location);
3542 #else
3543 static const char *last_file = 0;
3544 static tree last_filenode = NULL_TREE;
3546 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3548 EXPR_WFL_SET_LINECOL (wfl, line, col);
3549 if (file != last_file)
3551 last_file = file;
3552 last_filenode = file ? get_identifier (file) : NULL_TREE;
3554 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3555 #endif
3556 EXPR_WFL_NODE (wfl) = node;
3557 if (node)
3559 if (!TYPE_P (node))
3560 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3561 TREE_TYPE (wfl) = TREE_TYPE (node);
3564 return wfl;
3567 #ifdef USE_MAPPED_LOCATION
3568 tree
3569 expr_add_location (tree node, source_location location, bool statement)
3571 tree wfl;
3572 #if 0
3573 /* FIXME. This optimization causes failures in code that expects an
3574 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3575 if (node && ! (statement && flag_emit_class_files))
3577 source_location node_loc = EXPR_LOCATION (node);
3578 if (node_loc == location || location == UNKNOWN_LOCATION)
3579 return node;
3580 if (node_loc == UNKNOWN_LOCATION
3581 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3583 SET_EXPR_LOCATION (node, location);
3584 return node;
3587 #endif
3588 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3589 SET_EXPR_LOCATION (wfl, location);
3590 EXPR_WFL_NODE (wfl) = node;
3591 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3592 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3593 if (node)
3595 if (!TYPE_P (node))
3596 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3597 TREE_TYPE (wfl) = TREE_TYPE (node);
3600 return wfl;
3602 #endif
3604 /* Build a node to represent empty statements and blocks. */
3606 tree
3607 build_java_empty_stmt (void)
3609 tree t = build_empty_stmt ();
3610 CAN_COMPLETE_NORMALLY (t) = 1;
3611 return t;
3614 /* Promote all args of integral type before generating any code. */
3616 static void
3617 promote_arguments (void)
3619 int i;
3620 tree arg;
3621 for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
3622 arg != NULL_TREE; arg = TREE_CHAIN (arg), i++)
3624 tree arg_type = TREE_TYPE (arg);
3625 if (INTEGRAL_TYPE_P (arg_type)
3626 && TYPE_PRECISION (arg_type) < 32)
3628 tree copy = find_local_variable (i, integer_type_node, -1);
3629 java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
3630 copy,
3631 fold_convert (integer_type_node, arg)));
3633 if (TYPE_IS_WIDE (arg_type))
3634 i++;
3638 #include "gt-java-expr.h"