2006-08-07 Andrew John Hughes <gnu_andrew@member.fsf.org>
[official-gcc.git] / gcc / java / expr.c
blob838b7e546ae8cbce235c1586dc59756635b6e13f
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 physical memory page size used in this computer. See
134 build_field_ref(). */
135 static GTY(()) tree page_size;
137 /* The stack pointer of the Java virtual machine.
138 This does include the size of the quick_stack. */
140 int stack_pointer;
142 const unsigned char *linenumber_table;
143 int linenumber_count;
145 /* Largest pc so far in this method that has been passed to lookup_label. */
146 int highest_label_pc_this_method = -1;
148 /* Base value for this method to add to pc to get generated label. */
149 int start_label_pc_this_method = 0;
151 void
152 init_expr_processing (void)
154 operand_type[21] = operand_type[54] = int_type_node;
155 operand_type[22] = operand_type[55] = long_type_node;
156 operand_type[23] = operand_type[56] = float_type_node;
157 operand_type[24] = operand_type[57] = double_type_node;
158 operand_type[25] = operand_type[58] = ptr_type_node;
161 tree
162 java_truthvalue_conversion (tree expr)
164 /* It is simpler and generates better code to have only TRUTH_*_EXPR
165 or comparison expressions as truth values at this level.
167 This function should normally be identity for Java. */
169 switch (TREE_CODE (expr))
171 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
172 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
173 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
174 case ORDERED_EXPR: case UNORDERED_EXPR:
175 case TRUTH_ANDIF_EXPR:
176 case TRUTH_ORIF_EXPR:
177 case TRUTH_AND_EXPR:
178 case TRUTH_OR_EXPR:
179 case TRUTH_XOR_EXPR:
180 case TRUTH_NOT_EXPR:
181 case ERROR_MARK:
182 return expr;
184 case INTEGER_CST:
185 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
187 case REAL_CST:
188 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
190 /* are these legal? XXX JH */
191 case NEGATE_EXPR:
192 case ABS_EXPR:
193 case FLOAT_EXPR:
194 /* These don't change whether an object is nonzero or zero. */
195 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
197 case COND_EXPR:
198 /* Distribute the conversion into the arms of a COND_EXPR. */
199 return fold_build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
200 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
201 java_truthvalue_conversion (TREE_OPERAND (expr, 2)));
203 case NOP_EXPR:
204 /* If this is widening the argument, we can ignore it. */
205 if (TYPE_PRECISION (TREE_TYPE (expr))
206 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
207 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
208 /* fall through to default */
210 default:
211 return fold_build2 (NE_EXPR, boolean_type_node,
212 expr, boolean_false_node);
216 /* Save any stack slots that happen to be in the quick_stack into their
217 home virtual register slots.
219 The copy order is from low stack index to high, to support the invariant
220 that the expression for a slot may contain decls for stack slots with
221 higher (or the same) index, but not lower. */
223 static void
224 flush_quick_stack (void)
226 int stack_index = stack_pointer;
227 tree prev, cur, next;
229 /* First reverse the quick_stack, and count the number of slots it has. */
230 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
232 next = TREE_CHAIN (cur);
233 TREE_CHAIN (cur) = prev;
234 prev = cur;
235 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
237 quick_stack = prev;
239 while (quick_stack != NULL_TREE)
241 tree decl;
242 tree node = quick_stack, type;
243 quick_stack = TREE_CHAIN (node);
244 TREE_CHAIN (node) = tree_list_free_list;
245 tree_list_free_list = node;
246 node = TREE_VALUE (node);
247 type = TREE_TYPE (node);
249 decl = find_stack_slot (stack_index, type);
250 if (decl != node)
251 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
252 stack_index += 1 + TYPE_IS_WIDE (type);
256 /* Push TYPE on the type stack.
257 Return true on success, 0 on overflow. */
260 push_type_0 (tree type)
262 int n_words;
263 type = promote_type (type);
264 n_words = 1 + TYPE_IS_WIDE (type);
265 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
266 return 0;
267 /* Allocate decl for this variable now, so we get a temporary that
268 survives the whole method. */
269 find_stack_slot (stack_pointer, type);
270 stack_type_map[stack_pointer++] = type;
271 n_words--;
272 while (--n_words >= 0)
273 stack_type_map[stack_pointer++] = TYPE_SECOND;
274 return 1;
277 void
278 push_type (tree type)
280 int r = push_type_0 (type);
281 gcc_assert (r);
284 static void
285 push_value (tree value)
287 tree type = TREE_TYPE (value);
288 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
290 type = promote_type (type);
291 value = convert (type, value);
293 push_type (type);
294 if (tree_list_free_list == NULL_TREE)
295 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
296 else
298 tree node = tree_list_free_list;
299 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
300 TREE_VALUE (node) = value;
301 TREE_CHAIN (node) = quick_stack;
302 quick_stack = node;
304 /* If the value has a side effect, then we need to evaluate it
305 whether or not the result is used. If the value ends up on the
306 quick stack and is then popped, this won't happen -- so we flush
307 the quick stack. It is safest to simply always flush, though,
308 since TREE_SIDE_EFFECTS doesn't capture COMPONENT_REF, and for
309 the latter we may need to strip conversions. */
310 flush_quick_stack ();
313 /* Pop a type from the type stack.
314 TYPE is the expected type. Return the actual type, which must be
315 convertible to TYPE.
316 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
318 tree
319 pop_type_0 (tree type, char **messagep)
321 int n_words;
322 tree t;
323 *messagep = NULL;
324 if (TREE_CODE (type) == RECORD_TYPE)
325 type = promote_type (type);
326 n_words = 1 + TYPE_IS_WIDE (type);
327 if (stack_pointer < n_words)
329 *messagep = xstrdup ("stack underflow");
330 return type;
332 while (--n_words > 0)
334 if (stack_type_map[--stack_pointer] != void_type_node)
336 *messagep = xstrdup ("Invalid multi-word value on type stack");
337 return type;
340 t = stack_type_map[--stack_pointer];
341 if (type == NULL_TREE || t == type)
342 return t;
343 if (TREE_CODE (t) == TREE_LIST)
347 tree tt = TREE_PURPOSE (t);
348 if (! can_widen_reference_to (tt, type))
350 t = tt;
351 goto fail;
353 t = TREE_CHAIN (t);
355 while (t);
356 return t;
358 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
359 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
360 return t;
361 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
363 /* If the expected type we've been passed is object or ptr
364 (i.e. void*), the caller needs to know the real type. */
365 if (type == ptr_type_node || type == object_ptr_type_node)
366 return t;
368 /* Since the verifier has already run, we know that any
369 types we see will be compatible. In BC mode, this fact
370 may be checked at runtime, but if that is so then we can
371 assume its truth here as well. So, we always succeed
372 here, with the expected type. */
373 return type;
376 if (! flag_verify_invocations && flag_indirect_dispatch
377 && t == object_ptr_type_node)
379 if (type != ptr_type_node)
380 warning (0, "need to insert runtime check for %s",
381 xstrdup (lang_printable_name (type, 0)));
382 return type;
385 /* lang_printable_name uses a static buffer, so we must save the result
386 from calling it the first time. */
387 fail:
389 char *temp = xstrdup (lang_printable_name (type, 0));
390 /* If the stack contains a multi-word type, keep popping the stack until
391 the real type is found. */
392 while (t == void_type_node)
393 t = stack_type_map[--stack_pointer];
394 *messagep = concat ("expected type '", temp,
395 "' but stack contains '", lang_printable_name (t, 0),
396 "'", NULL);
397 free (temp);
399 return type;
402 /* Pop a type from the type stack.
403 TYPE is the expected type. Return the actual type, which must be
404 convertible to TYPE, otherwise call error. */
406 tree
407 pop_type (tree type)
409 char *message = NULL;
410 type = pop_type_0 (type, &message);
411 if (message != NULL)
413 error ("%s", message);
414 free (message);
416 return type;
420 /* Return true if two type assertions are equal. */
422 static int
423 type_assertion_eq (const void * k1_p, const void * k2_p)
425 type_assertion k1 = *(type_assertion *)k1_p;
426 type_assertion k2 = *(type_assertion *)k2_p;
427 return (k1.assertion_code == k2.assertion_code
428 && k1.op1 == k2.op1
429 && k1.op2 == k2.op2);
432 /* Hash a type assertion. */
434 static hashval_t
435 type_assertion_hash (const void *p)
437 const type_assertion *k_p = p;
438 hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
439 k_p->assertion_code, 0);
440 hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
441 return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
444 /* Add an entry to the type assertion table for the given class.
445 CLASS is the class for which this assertion will be evaluated by the
446 runtime during loading/initialization.
447 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
448 OP1 and OP2 are the operands. The tree type of these arguments may be
449 specific to each assertion_code. */
451 void
452 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
454 htab_t assertions_htab;
455 type_assertion as;
456 void **as_pp;
458 assertions_htab = TYPE_ASSERTIONS (class);
459 if (assertions_htab == NULL)
461 assertions_htab = htab_create_ggc (7, type_assertion_hash,
462 type_assertion_eq, NULL);
463 TYPE_ASSERTIONS (current_class) = assertions_htab;
466 as.assertion_code = assertion_code;
467 as.op1 = op1;
468 as.op2 = op2;
470 as_pp = htab_find_slot (assertions_htab, &as, INSERT);
472 /* Don't add the same assertion twice. */
473 if (*as_pp)
474 return;
476 *as_pp = ggc_alloc (sizeof (type_assertion));
477 **(type_assertion **)as_pp = as;
481 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
482 Handles array types and interfaces. */
485 can_widen_reference_to (tree source_type, tree target_type)
487 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
488 return 1;
490 /* Get rid of pointers */
491 if (TREE_CODE (source_type) == POINTER_TYPE)
492 source_type = TREE_TYPE (source_type);
493 if (TREE_CODE (target_type) == POINTER_TYPE)
494 target_type = TREE_TYPE (target_type);
496 if (source_type == target_type)
497 return 1;
499 /* FIXME: This is very pessimistic, in that it checks everything,
500 even if we already know that the types are compatible. If we're
501 to support full Java class loader semantics, we need this.
502 However, we could do something more optimal. */
503 if (! flag_verify_invocations)
505 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
506 source_type, target_type);
508 if (!quiet_flag)
509 warning (0, "assert: %s is assign compatible with %s",
510 xstrdup (lang_printable_name (target_type, 0)),
511 xstrdup (lang_printable_name (source_type, 0)));
512 /* Punt everything to runtime. */
513 return 1;
516 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
518 return 1;
520 else
522 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
524 HOST_WIDE_INT source_length, target_length;
525 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
527 /* An array implements Cloneable and Serializable. */
528 tree name = DECL_NAME (TYPE_NAME (target_type));
529 return (name == java_lang_cloneable_identifier_node
530 || name == java_io_serializable_identifier_node);
532 target_length = java_array_type_length (target_type);
533 if (target_length >= 0)
535 source_length = java_array_type_length (source_type);
536 if (source_length != target_length)
537 return 0;
539 source_type = TYPE_ARRAY_ELEMENT (source_type);
540 target_type = TYPE_ARRAY_ELEMENT (target_type);
541 if (source_type == target_type)
542 return 1;
543 if (TREE_CODE (source_type) != POINTER_TYPE
544 || TREE_CODE (target_type) != POINTER_TYPE)
545 return 0;
546 return can_widen_reference_to (source_type, target_type);
548 else
550 int source_depth = class_depth (source_type);
551 int target_depth = class_depth (target_type);
553 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
555 if (! quiet_flag)
556 warning (0, "assert: %s is assign compatible with %s",
557 xstrdup (lang_printable_name (target_type, 0)),
558 xstrdup (lang_printable_name (source_type, 0)));
559 return 1;
562 /* class_depth can return a negative depth if an error occurred */
563 if (source_depth < 0 || target_depth < 0)
564 return 0;
566 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
568 /* target_type is OK if source_type or source_type ancestors
569 implement target_type. We handle multiple sub-interfaces */
570 tree binfo, base_binfo;
571 int i;
573 for (binfo = TYPE_BINFO (source_type), i = 0;
574 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
575 if (can_widen_reference_to
576 (BINFO_TYPE (base_binfo), target_type))
577 return 1;
579 if (!i)
580 return 0;
583 for ( ; source_depth > target_depth; source_depth--)
585 source_type
586 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
588 return source_type == target_type;
593 static tree
594 pop_value (tree type)
596 type = pop_type (type);
597 if (quick_stack)
599 tree node = quick_stack;
600 quick_stack = TREE_CHAIN (quick_stack);
601 TREE_CHAIN (node) = tree_list_free_list;
602 tree_list_free_list = node;
603 node = TREE_VALUE (node);
604 return node;
606 else
607 return find_stack_slot (stack_pointer, promote_type (type));
611 /* Pop and discard the top COUNT stack slots. */
613 static void
614 java_stack_pop (int count)
616 while (count > 0)
618 tree type, val;
620 gcc_assert (stack_pointer != 0);
622 type = stack_type_map[stack_pointer - 1];
623 if (type == TYPE_SECOND)
625 count--;
626 gcc_assert (stack_pointer != 1 && count > 0);
628 type = stack_type_map[stack_pointer - 2];
630 val = pop_value (type);
631 count--;
635 /* Implement the 'swap' operator (to swap two top stack slots). */
637 static void
638 java_stack_swap (void)
640 tree type1, type2;
641 tree temp;
642 tree decl1, decl2;
644 if (stack_pointer < 2
645 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
646 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
647 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
648 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
649 /* Bad stack swap. */
650 abort ();
651 /* Bad stack swap. */
653 flush_quick_stack ();
654 decl1 = find_stack_slot (stack_pointer - 1, type1);
655 decl2 = find_stack_slot (stack_pointer - 2, type2);
656 temp = build_decl (VAR_DECL, NULL_TREE, type1);
657 java_add_local_var (temp);
658 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
659 java_add_stmt (build2 (MODIFY_EXPR, type2,
660 find_stack_slot (stack_pointer - 1, type2),
661 decl2));
662 java_add_stmt (build2 (MODIFY_EXPR, type1,
663 find_stack_slot (stack_pointer - 2, type1),
664 temp));
665 stack_type_map[stack_pointer - 1] = type2;
666 stack_type_map[stack_pointer - 2] = type1;
669 static void
670 java_stack_dup (int size, int offset)
672 int low_index = stack_pointer - size - offset;
673 int dst_index;
674 if (low_index < 0)
675 error ("stack underflow - dup* operation");
677 flush_quick_stack ();
679 stack_pointer += size;
680 dst_index = stack_pointer;
682 for (dst_index = stack_pointer; --dst_index >= low_index; )
684 tree type;
685 int src_index = dst_index - size;
686 if (src_index < low_index)
687 src_index = dst_index + size + offset;
688 type = stack_type_map [src_index];
689 if (type == TYPE_SECOND)
691 /* Dup operation splits 64-bit number. */
692 gcc_assert (src_index > low_index);
694 stack_type_map[dst_index] = type;
695 src_index--; dst_index--;
696 type = stack_type_map[src_index];
697 gcc_assert (TYPE_IS_WIDE (type));
699 else
700 gcc_assert (! TYPE_IS_WIDE (type));
702 if (src_index != dst_index)
704 tree src_decl = find_stack_slot (src_index, type);
705 tree dst_decl = find_stack_slot (dst_index, type);
707 java_add_stmt
708 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
709 stack_type_map[dst_index] = type;
714 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
715 value stack. */
717 static void
718 build_java_athrow (tree node)
720 tree call;
722 call = build3 (CALL_EXPR,
723 void_type_node,
724 build_address_of (throw_node),
725 build_tree_list (NULL_TREE, node),
726 NULL_TREE);
727 TREE_SIDE_EFFECTS (call) = 1;
728 java_add_stmt (call);
729 java_stack_pop (stack_pointer);
732 /* Implementation for jsr/ret */
734 static void
735 build_java_jsr (int target_pc, int return_pc)
737 tree where = lookup_label (target_pc);
738 tree ret = lookup_label (return_pc);
739 tree ret_label = fold_build1 (ADDR_EXPR, return_address_type_node, ret);
740 push_value (ret_label);
741 flush_quick_stack ();
742 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
744 /* Do not need to emit the label here. We noted the existence of the
745 label as a jump target in note_instructions; we'll emit the label
746 for real at the beginning of the expand_byte_code loop. */
749 static void
750 build_java_ret (tree location)
752 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
755 /* Implementation of operations on array: new, load, store, length */
757 tree
758 decode_newarray_type (int atype)
760 switch (atype)
762 case 4: return boolean_type_node;
763 case 5: return char_type_node;
764 case 6: return float_type_node;
765 case 7: return double_type_node;
766 case 8: return byte_type_node;
767 case 9: return short_type_node;
768 case 10: return int_type_node;
769 case 11: return long_type_node;
770 default: return NULL_TREE;
774 /* Map primitive type to the code used by OPCODE_newarray. */
777 encode_newarray_type (tree type)
779 if (type == boolean_type_node)
780 return 4;
781 else if (type == char_type_node)
782 return 5;
783 else if (type == float_type_node)
784 return 6;
785 else if (type == double_type_node)
786 return 7;
787 else if (type == byte_type_node)
788 return 8;
789 else if (type == short_type_node)
790 return 9;
791 else if (type == int_type_node)
792 return 10;
793 else if (type == long_type_node)
794 return 11;
795 else
796 gcc_unreachable ();
799 /* Build a call to _Jv_ThrowBadArrayIndex(), the
800 ArrayIndexOfBoundsException exception handler. */
802 static tree
803 build_java_throw_out_of_bounds_exception (tree index)
805 tree node = build3 (CALL_EXPR, int_type_node,
806 build_address_of (soft_badarrayindex_node),
807 build_tree_list (NULL_TREE, index), NULL_TREE);
808 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
809 return (node);
812 /* Return the length of an array. Doesn't perform any checking on the nature
813 or value of the array NODE. May be used to implement some bytecodes. */
815 tree
816 build_java_array_length_access (tree node)
818 tree type = TREE_TYPE (node);
819 tree array_type = TREE_TYPE (type);
820 HOST_WIDE_INT length;
822 if (!is_array_type_p (type))
824 /* With the new verifier, we will see an ordinary pointer type
825 here. In this case, we just use an arbitrary array type. */
826 array_type = build_java_array_type (object_ptr_type_node, -1);
827 type = promote_type (array_type);
830 length = java_array_type_length (type);
831 if (length >= 0)
832 return build_int_cst (NULL_TREE, length);
834 node = build3 (COMPONENT_REF, int_type_node,
835 build_java_indirect_ref (array_type, node,
836 flag_check_references),
837 lookup_field (&array_type, get_identifier ("length")),
838 NULL_TREE);
839 IS_ARRAY_LENGTH_ACCESS (node) = 1;
840 return node;
843 /* Optionally checks a reference against the NULL pointer. ARG1: the
844 expr, ARG2: we should check the reference. Don't generate extra
845 checks if we're not generating code. */
847 tree
848 java_check_reference (tree expr, int check)
850 if (!flag_syntax_only && check)
852 expr = save_expr (expr);
853 expr = build3 (COND_EXPR, TREE_TYPE (expr),
854 build2 (EQ_EXPR, boolean_type_node,
855 expr, null_pointer_node),
856 build3 (CALL_EXPR, void_type_node,
857 build_address_of (soft_nullpointer_node),
858 NULL_TREE, NULL_TREE),
859 expr);
862 return expr;
865 /* Reference an object: just like an INDIRECT_REF, but with checking. */
867 tree
868 build_java_indirect_ref (tree type, tree expr, int check)
870 tree t;
871 t = java_check_reference (expr, check);
872 t = convert (build_pointer_type (type), t);
873 return build1 (INDIRECT_REF, type, t);
876 /* Implement array indexing (either as l-value or r-value).
877 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
878 Optionally performs bounds checking and/or test to NULL.
879 At this point, ARRAY should have been verified as an array. */
881 tree
882 build_java_arrayaccess (tree array, tree type, tree index)
884 tree node, throw = NULL_TREE;
885 tree data_field;
886 tree ref;
887 tree array_type = TREE_TYPE (TREE_TYPE (array));
889 if (!is_array_type_p (TREE_TYPE (array)))
891 /* With the new verifier, we will see an ordinary pointer type
892 here. In this case, we just use the correct array type. */
893 array_type = build_java_array_type (type, -1);
896 if (flag_bounds_check)
898 /* Generate:
899 * (unsigned jint) INDEX >= (unsigned jint) LEN
900 * && throw ArrayIndexOutOfBoundsException.
901 * Note this is equivalent to and more efficient than:
902 * INDEX < 0 || INDEX >= LEN && throw ... */
903 tree test;
904 tree len = convert (unsigned_int_type_node,
905 build_java_array_length_access (array));
906 test = fold_build2 (GE_EXPR, boolean_type_node,
907 convert (unsigned_int_type_node, index),
908 len);
909 if (! integer_zerop (test))
911 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
912 build_java_throw_out_of_bounds_exception (index));
913 /* allows expansion within COMPOUND */
914 TREE_SIDE_EFFECTS( throw ) = 1;
918 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
919 to have the bounds check evaluated first. */
920 if (throw != NULL_TREE)
921 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
923 data_field = lookup_field (&array_type, get_identifier ("data"));
925 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
926 build_java_indirect_ref (array_type, array,
927 flag_check_references),
928 data_field, NULL_TREE);
930 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
931 return node;
934 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
935 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
936 determine that no check is required. */
938 tree
939 build_java_arraystore_check (tree array, tree object)
941 tree check, element_type, source;
942 tree array_type_p = TREE_TYPE (array);
943 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
945 if (! flag_verify_invocations)
947 /* With the new verifier, we don't track precise types. FIXME:
948 performance regression here. */
949 element_type = TYPE_NAME (object_type_node);
951 else
953 gcc_assert (is_array_type_p (array_type_p));
955 /* Get the TYPE_DECL for ARRAY's element type. */
956 element_type
957 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
960 gcc_assert (TREE_CODE (element_type) == TYPE_DECL
961 && TREE_CODE (object_type) == TYPE_DECL);
963 if (!flag_store_check)
964 return build1 (NOP_EXPR, array_type_p, array);
966 /* No check is needed if the element type is final. Also check that
967 element_type matches object_type, since in the bytecode
968 compilation case element_type may be the actual element type of
969 the array rather than its declared type. However, if we're doing
970 indirect dispatch, we can't do the `final' optimization. */
971 if (element_type == object_type
972 && ! flag_indirect_dispatch
973 && CLASS_FINAL (element_type))
974 return build1 (NOP_EXPR, array_type_p, array);
976 /* OBJECT might be wrapped by a SAVE_EXPR. */
977 if (TREE_CODE (object) == SAVE_EXPR)
978 source = TREE_OPERAND (object, 0);
979 else
980 source = object;
982 /* Avoid the check if OBJECT was just loaded from the same array. */
983 if (TREE_CODE (source) == ARRAY_REF)
985 tree target;
986 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
987 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
988 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
989 if (TREE_CODE (source) == SAVE_EXPR)
990 source = TREE_OPERAND (source, 0);
992 target = array;
993 if (TREE_CODE (target) == SAVE_EXPR)
994 target = TREE_OPERAND (target, 0);
996 if (source == target)
997 return build1 (NOP_EXPR, array_type_p, array);
1000 /* Build an invocation of _Jv_CheckArrayStore */
1001 check = build3 (CALL_EXPR, void_type_node,
1002 build_address_of (soft_checkarraystore_node),
1003 tree_cons (NULL_TREE, array,
1004 build_tree_list (NULL_TREE, object)),
1005 NULL_TREE);
1006 TREE_SIDE_EFFECTS (check) = 1;
1008 return check;
1011 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1012 ARRAY_NODE. This function is used to retrieve something less vague than
1013 a pointer type when indexing the first dimension of something like [[<t>.
1014 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1015 return unchanged. */
1017 static tree
1018 build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED,
1019 tree indexed_type)
1021 /* We used to check to see if ARRAY_NODE really had array type.
1022 However, with the new verifier, this is not necessary, as we know
1023 that the object will be an array of the appropriate type. */
1025 return indexed_type;
1028 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1029 called with an integer code (the type of array to create), and the length
1030 of the array to create. */
1032 tree
1033 build_newarray (int atype_value, tree length)
1035 tree type_arg;
1037 tree prim_type = decode_newarray_type (atype_value);
1038 tree type
1039 = build_java_array_type (prim_type,
1040 host_integerp (length, 0) == INTEGER_CST
1041 ? tree_low_cst (length, 0) : -1);
1043 /* If compiling to native, pass a reference to the primitive type class
1044 and save the runtime some work. However, the bytecode generator
1045 expects to find the type_code int here. */
1046 if (flag_emit_class_files)
1047 type_arg = build_int_cst (NULL_TREE, atype_value);
1048 else
1049 type_arg = build_class_ref (prim_type);
1051 return build3 (CALL_EXPR, promote_type (type),
1052 build_address_of (soft_newarray_node),
1053 tree_cons (NULL_TREE,
1054 type_arg,
1055 build_tree_list (NULL_TREE, length)),
1056 NULL_TREE);
1059 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1060 of the dimension. */
1062 tree
1063 build_anewarray (tree class_type, tree length)
1065 tree type
1066 = build_java_array_type (class_type,
1067 host_integerp (length, 0)
1068 ? tree_low_cst (length, 0) : -1);
1070 return build3 (CALL_EXPR, promote_type (type),
1071 build_address_of (soft_anewarray_node),
1072 tree_cons (NULL_TREE, length,
1073 tree_cons (NULL_TREE, build_class_ref (class_type),
1074 build_tree_list (NULL_TREE,
1075 null_pointer_node))),
1076 NULL_TREE);
1079 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1081 tree
1082 build_new_array (tree type, tree length)
1084 if (JPRIMITIVE_TYPE_P (type))
1085 return build_newarray (encode_newarray_type (type), length);
1086 else
1087 return build_anewarray (TREE_TYPE (type), length);
1090 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1091 class pointer, a number of dimensions and the matching number of
1092 dimensions. The argument list is NULL terminated. */
1094 static void
1095 expand_java_multianewarray (tree class_type, int ndim)
1097 int i;
1098 tree args = build_tree_list( NULL_TREE, null_pointer_node );
1100 for( i = 0; i < ndim; i++ )
1101 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1103 push_value (build3 (CALL_EXPR,
1104 promote_type (class_type),
1105 build_address_of (soft_multianewarray_node),
1106 tree_cons (NULL_TREE, build_class_ref (class_type),
1107 tree_cons (NULL_TREE,
1108 build_int_cst (NULL_TREE, ndim),
1109 args)),
1110 NULL_TREE));
1113 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1114 ARRAY is an array type. May expand some bound checking and NULL
1115 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1116 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1117 INT. In those cases, we make the conversion.
1119 if ARRAy is a reference type, the assignment is checked at run-time
1120 to make sure that the RHS can be assigned to the array element
1121 type. It is not necessary to generate this code if ARRAY is final. */
1123 static void
1124 expand_java_arraystore (tree rhs_type_node)
1126 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
1127 && TYPE_PRECISION (rhs_type_node) <= 32) ?
1128 int_type_node : rhs_type_node);
1129 tree index = pop_value (int_type_node);
1130 tree array_type, array;
1132 /* If we're processing an `aaload' we might as well just pick
1133 `Object'. */
1134 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1136 array_type = build_java_array_type (object_ptr_type_node, -1);
1137 rhs_type_node = object_ptr_type_node;
1139 else
1140 array_type = build_java_array_type (rhs_type_node, -1);
1142 array = pop_value (array_type);
1143 array = build1 (NOP_EXPR, promote_type (array_type), array);
1145 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1147 flush_quick_stack ();
1149 index = save_expr (index);
1150 array = save_expr (array);
1152 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1154 tree check = build_java_arraystore_check (array, rhs_node);
1155 java_add_stmt (check);
1158 array = build_java_arrayaccess (array, rhs_type_node, index);
1159 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1162 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1163 sure that LHS is an array type. May expand some bound checking and NULL
1164 pointer checking.
1165 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1166 BOOLEAN/SHORT, we push a promoted type back to the stack.
1169 static void
1170 expand_java_arrayload (tree lhs_type_node)
1172 tree load_node;
1173 tree index_node = pop_value (int_type_node);
1174 tree array_type;
1175 tree array_node;
1177 /* If we're processing an `aaload' we might as well just pick
1178 `Object'. */
1179 if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1181 array_type = build_java_array_type (object_ptr_type_node, -1);
1182 lhs_type_node = object_ptr_type_node;
1184 else
1185 array_type = build_java_array_type (lhs_type_node, -1);
1186 array_node = pop_value (array_type);
1187 array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1189 index_node = save_expr (index_node);
1190 array_node = save_expr (array_node);
1192 lhs_type_node = build_java_check_indexed_type (array_node,
1193 lhs_type_node);
1194 load_node = build_java_arrayaccess (array_node,
1195 lhs_type_node,
1196 index_node);
1197 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1198 load_node = fold_build1 (NOP_EXPR, int_type_node, load_node);
1199 push_value (load_node);
1202 /* Expands .length. Makes sure that we deal with and array and may expand
1203 a NULL check on the array object. */
1205 static void
1206 expand_java_array_length (void)
1208 tree array = pop_value (ptr_type_node);
1209 tree length = build_java_array_length_access (array);
1211 push_value (length);
1214 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1215 either soft_monitorenter_node or soft_monitorexit_node. */
1217 static tree
1218 build_java_monitor (tree call, tree object)
1220 return build3 (CALL_EXPR,
1221 void_type_node,
1222 build_address_of (call),
1223 build_tree_list (NULL_TREE, object),
1224 NULL_TREE);
1227 /* Emit code for one of the PUSHC instructions. */
1229 static void
1230 expand_java_pushc (int ival, tree type)
1232 tree value;
1233 if (type == ptr_type_node && ival == 0)
1234 value = null_pointer_node;
1235 else if (type == int_type_node || type == long_type_node)
1236 value = build_int_cst (type, ival);
1237 else if (type == float_type_node || type == double_type_node)
1239 REAL_VALUE_TYPE x;
1240 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1241 value = build_real (type, x);
1243 else
1244 gcc_unreachable ();
1246 push_value (value);
1249 static void
1250 expand_java_return (tree type)
1252 if (type == void_type_node)
1253 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1254 else
1256 tree retval = pop_value (type);
1257 tree res = DECL_RESULT (current_function_decl);
1258 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1260 /* Handle the situation where the native integer type is smaller
1261 than the JVM integer. It can happen for many cross compilers.
1262 The whole if expression just goes away if INT_TYPE_SIZE < 32
1263 is false. */
1264 if (INT_TYPE_SIZE < 32
1265 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1266 < GET_MODE_SIZE (TYPE_MODE (type))))
1267 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1269 TREE_SIDE_EFFECTS (retval) = 1;
1270 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1274 static void
1275 expand_load_internal (int index, tree type, int pc)
1277 tree copy;
1278 tree var = find_local_variable (index, type, pc);
1280 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1281 on the stack. If there is an assignment to this VAR_DECL between
1282 the stack push and the use, then the wrong code could be
1283 generated. To avoid this we create a new local and copy our
1284 value into it. Then we push this new local on the stack.
1285 Hopefully this all gets optimized out. */
1286 copy = build_decl (VAR_DECL, NULL_TREE, type);
1287 if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1288 && TREE_TYPE (copy) != TREE_TYPE (var))
1289 var = convert (type, var);
1290 java_add_local_var (copy);
1291 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1293 push_value (copy);
1296 tree
1297 build_address_of (tree value)
1299 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1302 bool
1303 class_has_finalize_method (tree type)
1305 tree super = CLASSTYPE_SUPER (type);
1307 if (super == NULL_TREE)
1308 return false; /* Every class with a real finalizer inherits */
1309 /* from java.lang.Object. */
1310 else
1311 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1314 tree
1315 java_create_object (tree type)
1317 tree alloc_node = (class_has_finalize_method (type)
1318 ? alloc_object_node
1319 : alloc_no_finalizer_node);
1321 return build3 (CALL_EXPR, promote_type (type),
1322 build_address_of (alloc_node),
1323 build_tree_list (NULL_TREE, build_class_ref (type)),
1324 NULL_TREE);
1327 static void
1328 expand_java_NEW (tree type)
1330 tree alloc_node;
1332 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1333 : alloc_no_finalizer_node);
1334 if (! CLASS_LOADED_P (type))
1335 load_class (type, 1);
1336 safe_layout_class (type);
1337 push_value (build3 (CALL_EXPR, promote_type (type),
1338 build_address_of (alloc_node),
1339 build_tree_list (NULL_TREE, build_class_ref (type)),
1340 NULL_TREE));
1343 /* This returns an expression which will extract the class of an
1344 object. */
1346 tree
1347 build_get_class (tree value)
1349 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1350 tree vtable_field = lookup_field (&object_type_node,
1351 get_identifier ("vtable"));
1352 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1353 build_java_indirect_ref (object_type_node, value,
1354 flag_check_references),
1355 vtable_field, NULL_TREE);
1356 return build3 (COMPONENT_REF, class_ptr_type,
1357 build1 (INDIRECT_REF, dtable_type, tmp),
1358 class_field, NULL_TREE);
1361 /* This builds the tree representation of the `instanceof' operator.
1362 It tries various tricks to optimize this in cases where types are
1363 known. */
1365 tree
1366 build_instanceof (tree value, tree type)
1368 tree expr;
1369 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1370 tree valtype = TREE_TYPE (TREE_TYPE (value));
1371 tree valclass = TYPE_NAME (valtype);
1372 tree klass;
1374 /* When compiling from bytecode, we need to ensure that TYPE has
1375 been loaded. */
1376 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1378 load_class (type, 1);
1379 safe_layout_class (type);
1380 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1381 return error_mark_node;
1383 klass = TYPE_NAME (type);
1385 if (type == object_type_node || inherits_from_p (valtype, type))
1387 /* Anything except `null' is an instance of Object. Likewise,
1388 if the object is known to be an instance of the class, then
1389 we only need to check for `null'. */
1390 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1392 else if (flag_verify_invocations
1393 && ! TYPE_ARRAY_P (type)
1394 && ! TYPE_ARRAY_P (valtype)
1395 && DECL_P (klass) && DECL_P (valclass)
1396 && ! CLASS_INTERFACE (valclass)
1397 && ! CLASS_INTERFACE (klass)
1398 && ! inherits_from_p (type, valtype)
1399 && (CLASS_FINAL (klass)
1400 || ! inherits_from_p (valtype, type)))
1402 /* The classes are from different branches of the derivation
1403 tree, so we immediately know the answer. */
1404 expr = boolean_false_node;
1406 else if (DECL_P (klass) && CLASS_FINAL (klass))
1408 tree save = save_expr (value);
1409 expr = build3 (COND_EXPR, itype,
1410 build2 (NE_EXPR, boolean_type_node,
1411 save, null_pointer_node),
1412 build2 (EQ_EXPR, itype,
1413 build_get_class (save),
1414 build_class_ref (type)),
1415 boolean_false_node);
1417 else
1419 expr = build3 (CALL_EXPR, itype,
1420 build_address_of (soft_instanceof_node),
1421 tree_cons (NULL_TREE, value,
1422 build_tree_list (NULL_TREE,
1423 build_class_ref (type))),
1424 NULL_TREE);
1426 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1427 return expr;
1430 static void
1431 expand_java_INSTANCEOF (tree type)
1433 tree value = pop_value (object_ptr_type_node);
1434 value = build_instanceof (value, type);
1435 push_value (value);
1438 static void
1439 expand_java_CHECKCAST (tree type)
1441 tree value = pop_value (ptr_type_node);
1442 value = build3 (CALL_EXPR, promote_type (type),
1443 build_address_of (soft_checkcast_node),
1444 tree_cons (NULL_TREE, build_class_ref (type),
1445 build_tree_list (NULL_TREE, value)),
1446 NULL_TREE);
1447 push_value (value);
1450 static void
1451 expand_iinc (unsigned int local_var_index, int ival, int pc)
1453 tree local_var, res;
1454 tree constant_value;
1456 flush_quick_stack ();
1457 local_var = find_local_variable (local_var_index, int_type_node, pc);
1458 constant_value = build_int_cst (NULL_TREE, ival);
1459 res = fold_build2 (PLUS_EXPR, int_type_node, local_var, constant_value);
1460 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1461 update_aliases (local_var, local_var_index, pc);
1465 tree
1466 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1468 tree call = NULL;
1469 tree arg1 = convert (type, op1);
1470 tree arg2 = convert (type, op2);
1472 if (type == int_type_node)
1474 switch (op)
1476 case TRUNC_DIV_EXPR:
1477 call = soft_idiv_node;
1478 break;
1479 case TRUNC_MOD_EXPR:
1480 call = soft_irem_node;
1481 break;
1482 default:
1483 break;
1486 else if (type == long_type_node)
1488 switch (op)
1490 case TRUNC_DIV_EXPR:
1491 call = soft_ldiv_node;
1492 break;
1493 case TRUNC_MOD_EXPR:
1494 call = soft_lrem_node;
1495 break;
1496 default:
1497 break;
1501 gcc_assert (call);
1502 call = build3 (CALL_EXPR, type,
1503 build_address_of (call),
1504 tree_cons (NULL_TREE, arg1,
1505 build_tree_list (NULL_TREE, arg2)),
1506 NULL_TREE);
1508 return call;
1511 tree
1512 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1514 tree mask;
1515 switch (op)
1517 case URSHIFT_EXPR:
1519 tree u_type = java_unsigned_type (type);
1520 arg1 = convert (u_type, arg1);
1521 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1522 return convert (type, arg1);
1524 case LSHIFT_EXPR:
1525 case RSHIFT_EXPR:
1526 mask = build_int_cst (NULL_TREE,
1527 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1528 arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
1529 break;
1531 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1532 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1533 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1535 tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1536 boolean_type_node, arg1, arg2);
1537 tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
1538 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1539 ifexp2, integer_zero_node,
1540 op == COMPARE_L_EXPR
1541 ? integer_minus_one_node
1542 : integer_one_node);
1543 return fold_build3 (COND_EXPR, int_type_node, ifexp1,
1544 op == COMPARE_L_EXPR ? integer_one_node
1545 : integer_minus_one_node,
1546 second_compare);
1548 case COMPARE_EXPR:
1549 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1551 tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
1552 tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
1553 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1554 ifexp2, integer_one_node,
1555 integer_zero_node);
1556 return fold_build3 (COND_EXPR, int_type_node,
1557 ifexp1, integer_minus_one_node, second_compare);
1559 case TRUNC_DIV_EXPR:
1560 case TRUNC_MOD_EXPR:
1561 if (TREE_CODE (type) == REAL_TYPE
1562 && op == TRUNC_MOD_EXPR)
1564 tree call;
1565 if (type != double_type_node)
1567 arg1 = convert (double_type_node, arg1);
1568 arg2 = convert (double_type_node, arg2);
1570 call = build3 (CALL_EXPR, double_type_node,
1571 build_address_of (soft_fmod_node),
1572 tree_cons (NULL_TREE, arg1,
1573 build_tree_list (NULL_TREE, arg2)),
1574 NULL_TREE);
1575 if (type != double_type_node)
1576 call = convert (type, call);
1577 return call;
1580 if (TREE_CODE (type) == INTEGER_TYPE
1581 && flag_use_divide_subroutine
1582 && ! flag_syntax_only)
1583 return build_java_soft_divmod (op, type, arg1, arg2);
1585 break;
1586 default: ;
1588 return fold_build2 (op, type, arg1, arg2);
1591 static void
1592 expand_java_binop (tree type, enum tree_code op)
1594 tree larg, rarg;
1595 tree ltype = type;
1596 tree rtype = type;
1597 switch (op)
1599 case LSHIFT_EXPR:
1600 case RSHIFT_EXPR:
1601 case URSHIFT_EXPR:
1602 rtype = int_type_node;
1603 rarg = pop_value (rtype);
1604 break;
1605 default:
1606 rarg = pop_value (rtype);
1608 larg = pop_value (ltype);
1609 push_value (build_java_binop (op, type, larg, rarg));
1612 /* Lookup the field named NAME in *TYPEP or its super classes.
1613 If not found, return NULL_TREE.
1614 (If the *TYPEP is not found, or if the field reference is
1615 ambiguous, return error_mark_node.)
1616 If found, return the FIELD_DECL, and set *TYPEP to the
1617 class containing the field. */
1619 tree
1620 lookup_field (tree *typep, tree name)
1622 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1624 load_class (*typep, 1);
1625 safe_layout_class (*typep);
1626 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1627 return error_mark_node;
1631 tree field, binfo, base_binfo;
1632 tree save_field;
1633 int i;
1635 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1636 if (DECL_NAME (field) == name)
1637 return field;
1639 /* Process implemented interfaces. */
1640 save_field = NULL_TREE;
1641 for (binfo = TYPE_BINFO (*typep), i = 0;
1642 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1644 tree t = BINFO_TYPE (base_binfo);
1645 if ((field = lookup_field (&t, name)))
1647 if (save_field == field)
1648 continue;
1649 if (save_field == NULL_TREE)
1650 save_field = field;
1651 else
1653 tree i1 = DECL_CONTEXT (save_field);
1654 tree i2 = DECL_CONTEXT (field);
1655 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1656 IDENTIFIER_POINTER (name),
1657 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1658 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1659 return error_mark_node;
1664 if (save_field != NULL_TREE)
1665 return save_field;
1667 *typep = CLASSTYPE_SUPER (*typep);
1668 } while (*typep);
1669 return NULL_TREE;
1672 /* Look up the field named NAME in object SELF_VALUE,
1673 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1674 SELF_VALUE is NULL_TREE if looking for a static field. */
1676 tree
1677 build_field_ref (tree self_value, tree self_class, tree name)
1679 tree base_class = self_class;
1680 tree field_decl = lookup_field (&base_class, name);
1681 if (field_decl == NULL_TREE)
1683 error ("field %qs not found", IDENTIFIER_POINTER (name));
1684 return error_mark_node;
1686 if (self_value == NULL_TREE)
1688 return build_static_field_ref (field_decl);
1690 else
1692 tree base_type = promote_type (base_class);
1694 /* CHECK is true if self_value is not the this pointer. */
1695 int check = (! (DECL_P (self_value)
1696 && DECL_NAME (self_value) == this_identifier_node));
1698 /* Determine whether a field offset from NULL will lie within
1699 Page 0: this is necessary on those GNU/Linux/BSD systems that
1700 trap SEGV to generate NullPointerExceptions.
1702 We assume that Page 0 will be mapped with NOPERM, and that
1703 memory may be allocated from any other page, so only field
1704 offsets < pagesize are guaranteed to trap. We also assume
1705 the smallest page size we'll encounter is 4k bytes. */
1706 if (! flag_syntax_only && check && ! flag_check_references
1707 && ! flag_indirect_dispatch)
1709 tree field_offset = byte_position (field_decl);
1710 if (! page_size)
1711 page_size = size_int (4096);
1712 check = ! INT_CST_LT_UNSIGNED (field_offset, page_size);
1715 if (base_type != TREE_TYPE (self_value))
1716 self_value = fold_build1 (NOP_EXPR, base_type, self_value);
1717 if (! flag_syntax_only && flag_indirect_dispatch)
1719 tree otable_index
1720 = build_int_cst (NULL_TREE, get_symbol_table_index
1721 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1722 tree field_offset
1723 = build4 (ARRAY_REF, integer_type_node,
1724 TYPE_OTABLE_DECL (output_class), otable_index,
1725 NULL_TREE, NULL_TREE);
1726 tree address;
1728 if (DECL_CONTEXT (field_decl) != output_class)
1729 field_offset
1730 = build3 (COND_EXPR, TREE_TYPE (field_offset),
1731 build2 (EQ_EXPR, boolean_type_node,
1732 field_offset, integer_zero_node),
1733 build3 (CALL_EXPR, void_type_node,
1734 build_address_of (soft_nosuchfield_node),
1735 build_tree_list (NULL_TREE, otable_index),
1736 NULL_TREE),
1737 field_offset);
1739 field_offset = fold (convert (sizetype, field_offset));
1740 self_value = java_check_reference (self_value, check);
1741 address
1742 = fold_build2 (PLUS_EXPR,
1743 build_pointer_type (TREE_TYPE (field_decl)),
1744 self_value, field_offset);
1745 return fold_build1 (INDIRECT_REF, TREE_TYPE (field_decl), address);
1748 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1749 self_value, check);
1750 return fold_build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1751 self_value, field_decl, NULL_TREE);
1755 tree
1756 lookup_label (int pc)
1758 tree name;
1759 char buf[32];
1760 if (pc > highest_label_pc_this_method)
1761 highest_label_pc_this_method = pc;
1762 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", start_label_pc_this_method + pc);
1763 name = get_identifier (buf);
1764 if (IDENTIFIER_LOCAL_VALUE (name))
1765 return IDENTIFIER_LOCAL_VALUE (name);
1766 else
1768 /* The type of the address of a label is return_address_type_node. */
1769 tree decl = create_label_decl (name);
1770 LABEL_PC (decl) = pc;
1771 return pushdecl (decl);
1775 /* Generate a unique name for the purpose of loops and switches
1776 labels, and try-catch-finally blocks label or temporary variables. */
1778 tree
1779 generate_name (void)
1781 static int l_number = 0;
1782 char buff [32];
1783 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1784 l_number++;
1785 return get_identifier (buff);
1788 tree
1789 create_label_decl (tree name)
1791 tree decl;
1792 decl = build_decl (LABEL_DECL, name,
1793 TREE_TYPE (return_address_type_node));
1794 DECL_CONTEXT (decl) = current_function_decl;
1795 DECL_IGNORED_P (decl) = 1;
1796 return decl;
1799 /* This maps a bytecode offset (PC) to various flags. */
1800 char *instruction_bits;
1802 static void
1803 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1805 lookup_label (target_pc);
1806 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1809 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1810 where CONDITION is one of one the compare operators. */
1812 static void
1813 expand_compare (enum tree_code condition, tree value1, tree value2,
1814 int target_pc)
1816 tree target = lookup_label (target_pc);
1817 tree cond = fold_build2 (condition, boolean_type_node, value1, value2);
1818 java_add_stmt
1819 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1820 build1 (GOTO_EXPR, void_type_node, target),
1821 build_java_empty_stmt ()));
1824 /* Emit code for a TEST-type opcode. */
1826 static void
1827 expand_test (enum tree_code condition, tree type, int target_pc)
1829 tree value1, value2;
1830 flush_quick_stack ();
1831 value1 = pop_value (type);
1832 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1833 expand_compare (condition, value1, value2, target_pc);
1836 /* Emit code for a COND-type opcode. */
1838 static void
1839 expand_cond (enum tree_code condition, tree type, int target_pc)
1841 tree value1, value2;
1842 flush_quick_stack ();
1843 /* note: pop values in opposite order */
1844 value2 = pop_value (type);
1845 value1 = pop_value (type);
1846 /* Maybe should check value1 and value2 for type compatibility ??? */
1847 expand_compare (condition, value1, value2, target_pc);
1850 static void
1851 expand_java_goto (int target_pc)
1853 tree target_label = lookup_label (target_pc);
1854 flush_quick_stack ();
1855 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1858 static tree
1859 expand_java_switch (tree selector, int default_pc)
1861 tree switch_expr, x;
1863 flush_quick_stack ();
1864 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1865 NULL_TREE, NULL_TREE);
1866 java_add_stmt (switch_expr);
1868 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1869 create_artificial_label ());
1870 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1872 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1873 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1875 return switch_expr;
1878 static void
1879 expand_java_add_case (tree switch_expr, int match, int target_pc)
1881 tree value, x;
1883 value = build_int_cst (TREE_TYPE (switch_expr), match);
1885 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1886 create_artificial_label ());
1887 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1889 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1890 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1893 static tree
1894 pop_arguments (tree arg_types)
1896 if (arg_types == end_params_node)
1897 return NULL_TREE;
1898 if (TREE_CODE (arg_types) == TREE_LIST)
1900 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1901 tree type = TREE_VALUE (arg_types);
1902 tree arg = pop_value (type);
1904 /* We simply cast each argument to its proper type. This is
1905 needed since we lose type information coming out of the
1906 verifier. We also have to do this when we pop an integer
1907 type that must be promoted for the function call. */
1908 if (TREE_CODE (type) == POINTER_TYPE)
1909 arg = build1 (NOP_EXPR, type, arg);
1910 else if (targetm.calls.promote_prototypes (type)
1911 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1912 && INTEGRAL_TYPE_P (type))
1913 arg = convert (integer_type_node, arg);
1914 return tree_cons (NULL_TREE, arg, tail);
1916 gcc_unreachable ();
1919 /* Attach to PTR (a block) the declaration found in ENTRY. */
1922 attach_init_test_initialization_flags (void **entry, void *ptr)
1924 tree block = (tree)ptr;
1925 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1927 if (block != error_mark_node)
1929 if (TREE_CODE (block) == BIND_EXPR)
1931 tree body = BIND_EXPR_BODY (block);
1932 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1933 BIND_EXPR_VARS (block) = ite->value;
1934 body = build2 (COMPOUND_EXPR, void_type_node,
1935 build1 (DECL_EXPR, void_type_node, ite->value), body);
1936 BIND_EXPR_BODY (block) = body;
1938 else
1940 tree body = BLOCK_SUBBLOCKS (block);
1941 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1942 BLOCK_EXPR_DECLS (block) = ite->value;
1943 body = build2 (COMPOUND_EXPR, void_type_node,
1944 build1 (DECL_EXPR, void_type_node, ite->value), body);
1945 BLOCK_SUBBLOCKS (block) = body;
1949 return true;
1952 /* Build an expression to initialize the class CLAS.
1953 if EXPR is non-NULL, returns an expression to first call the initializer
1954 (if it is needed) and then calls EXPR. */
1956 tree
1957 build_class_init (tree clas, tree expr)
1959 tree init;
1961 /* An optimization: if CLAS is a superclass of the class we're
1962 compiling, we don't need to initialize it. However, if CLAS is
1963 an interface, it won't necessarily be initialized, even if we
1964 implement it. */
1965 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1966 && inherits_from_p (current_class, clas))
1967 || current_class == clas)
1968 return expr;
1970 if (always_initialize_class_p)
1972 init = build3 (CALL_EXPR, void_type_node,
1973 build_address_of (soft_initclass_node),
1974 build_tree_list (NULL_TREE, build_class_ref (clas)),
1975 NULL_TREE);
1976 TREE_SIDE_EFFECTS (init) = 1;
1978 else
1980 tree *init_test_decl;
1981 tree decl;
1982 init_test_decl = java_treetreehash_new
1983 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1985 if (*init_test_decl == NULL)
1987 /* Build a declaration and mark it as a flag used to track
1988 static class initializations. */
1989 decl = build_decl (VAR_DECL, NULL_TREE,
1990 boolean_type_node);
1991 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1992 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
1993 DECL_CONTEXT (decl) = current_function_decl;
1994 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
1995 /* Tell the check-init code to ignore this decl when not
1996 optimizing class initialization. */
1997 if (!STATIC_CLASS_INIT_OPT_P ())
1998 DECL_BIT_INDEX (decl) = -1;
1999 DECL_INITIAL (decl) = boolean_false_node;
2000 /* Don't emit any symbolic debugging info for this decl. */
2001 DECL_IGNORED_P (decl) = 1;
2002 *init_test_decl = decl;
2005 init = build3 (CALL_EXPR, void_type_node,
2006 build_address_of (soft_initclass_node),
2007 build_tree_list (NULL_TREE, build_class_ref (clas)),
2008 NULL_TREE);
2009 TREE_SIDE_EFFECTS (init) = 1;
2010 init = build3 (COND_EXPR, void_type_node,
2011 build2 (EQ_EXPR, boolean_type_node,
2012 *init_test_decl, boolean_false_node),
2013 init, integer_zero_node);
2014 TREE_SIDE_EFFECTS (init) = 1;
2015 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
2016 build2 (MODIFY_EXPR, boolean_type_node,
2017 *init_test_decl, boolean_true_node));
2018 TREE_SIDE_EFFECTS (init) = 1;
2021 if (expr != NULL_TREE)
2023 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2024 TREE_SIDE_EFFECTS (expr) = 1;
2025 return expr;
2027 return init;
2032 /* Rewrite expensive calls that require stack unwinding at runtime to
2033 cheaper alternatives. The logic here performs these
2034 transformations:
2036 java.lang.Class.forName("foo") -> java.lang.Class.forName("foo", class$)
2037 java.lang.Class.getClassLoader() -> java.lang.Class.getClassLoader(class$)
2041 typedef struct
2043 const char *classname;
2044 const char *method;
2045 const char *signature;
2046 const char *new_signature;
2047 int flags;
2048 tree (*rewrite_arglist) (tree arglist);
2049 } rewrite_rule;
2051 /* Add this.class to the end of an arglist. */
2053 static tree
2054 rewrite_arglist_getclass (tree arglist)
2056 return chainon (arglist,
2057 tree_cons (NULL_TREE, build_class_ref (output_class), NULL_TREE));
2060 static rewrite_rule rules[] =
2061 {{"java.lang.Class", "getClassLoader", "()Ljava/lang/ClassLoader;",
2062 "(Ljava/lang/Class;)Ljava/lang/ClassLoader;",
2063 ACC_FINAL|ACC_PRIVATE, rewrite_arglist_getclass},
2064 {"java.lang.Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
2065 "(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class;",
2066 ACC_FINAL|ACC_PRIVATE|ACC_STATIC, rewrite_arglist_getclass},
2067 {NULL, NULL, NULL, NULL, 0, NULL}};
2069 /* Scan the rules list for replacements for *METHOD_P and replace the
2070 args accordingly. */
2072 void
2073 maybe_rewrite_invocation (tree *method_p, tree *arg_list_p,
2074 tree *method_signature_p)
2076 tree context = DECL_NAME (TYPE_NAME (DECL_CONTEXT (*method_p)));
2077 rewrite_rule *p;
2078 for (p = rules; p->classname; p++)
2080 if (get_identifier (p->classname) == context)
2082 tree method = DECL_NAME (*method_p);
2083 if (get_identifier (p->method) == method
2084 && get_identifier (p->signature) == *method_signature_p)
2086 tree maybe_method
2087 = lookup_java_method (DECL_CONTEXT (*method_p),
2088 method,
2089 get_identifier (p->new_signature));
2090 if (! maybe_method && ! flag_verify_invocations)
2092 maybe_method
2093 = add_method (DECL_CONTEXT (*method_p), p->flags,
2094 method, get_identifier (p->new_signature));
2095 DECL_EXTERNAL (maybe_method) = 1;
2097 *method_p = maybe_method;
2098 gcc_assert (*method_p);
2099 *arg_list_p = p->rewrite_arglist (*arg_list_p);
2100 *method_signature_p = get_identifier (p->new_signature);
2102 break;
2110 tree
2111 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2112 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2113 tree arg_list ATTRIBUTE_UNUSED)
2115 tree func;
2116 if (is_compiled_class (self_type))
2118 /* With indirect dispatch we have to use indirect calls for all
2119 publicly visible methods or gcc will use PLT indirections
2120 to reach them. We also have to use indirect dispatch for all
2121 external methods. */
2122 if (! flag_indirect_dispatch
2123 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2125 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2126 method);
2128 else
2130 tree table_index
2131 = build_int_cst (NULL_TREE, get_symbol_table_index
2132 (method, &TYPE_ATABLE_METHODS (output_class)));
2133 func
2134 = build4 (ARRAY_REF,
2135 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2136 TYPE_ATABLE_DECL (output_class), table_index,
2137 NULL_TREE, NULL_TREE);
2139 func = convert (method_ptr_type_node, func);
2141 else
2143 /* We don't know whether the method has been (statically) compiled.
2144 Compile this code to get a reference to the method's code:
2146 SELF_TYPE->methods[METHOD_INDEX].ncode
2150 int method_index = 0;
2151 tree meth, ref;
2153 /* The method might actually be declared in some superclass, so
2154 we have to use its class context, not the caller's notion of
2155 where the method is. */
2156 self_type = DECL_CONTEXT (method);
2157 ref = build_class_ref (self_type);
2158 ref = build1 (INDIRECT_REF, class_type_node, ref);
2159 if (ncode_ident == NULL_TREE)
2160 ncode_ident = get_identifier ("ncode");
2161 if (methods_ident == NULL_TREE)
2162 methods_ident = get_identifier ("methods");
2163 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2164 lookup_field (&class_type_node, methods_ident),
2165 NULL_TREE);
2166 for (meth = TYPE_METHODS (self_type);
2167 ; meth = TREE_CHAIN (meth))
2169 if (method == meth)
2170 break;
2171 if (meth == NULL_TREE)
2172 fatal_error ("method '%s' not found in class",
2173 IDENTIFIER_POINTER (DECL_NAME (method)));
2174 method_index++;
2176 method_index *= int_size_in_bytes (method_type_node);
2177 ref = fold_build2 (PLUS_EXPR, method_ptr_type_node,
2178 ref, build_int_cst (NULL_TREE, method_index));
2179 ref = build1 (INDIRECT_REF, method_type_node, ref);
2180 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2181 ref, lookup_field (&method_type_node, ncode_ident),
2182 NULL_TREE);
2184 return func;
2187 tree
2188 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2190 tree dtable, objectref;
2192 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2194 /* If we're dealing with interfaces and if the objectref
2195 argument is an array then get the dispatch table of the class
2196 Object rather than the one from the objectref. */
2197 objectref = (is_invoke_interface
2198 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2199 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2201 if (dtable_ident == NULL_TREE)
2202 dtable_ident = get_identifier ("vtable");
2203 dtable = build_java_indirect_ref (object_type_node, objectref,
2204 flag_check_references);
2205 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2206 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2208 return dtable;
2211 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2212 T. If this decl has not been seen before, it will be added to the
2213 [oa]table_methods. If it has, the existing table slot will be
2214 reused. */
2217 get_symbol_table_index (tree t, tree *symbol_table)
2219 int i = 1;
2220 tree method_list;
2222 if (*symbol_table == NULL_TREE)
2224 *symbol_table = build_tree_list (t, t);
2225 return 1;
2228 method_list = *symbol_table;
2230 while (1)
2232 tree value = TREE_VALUE (method_list);
2233 if (value == t)
2234 return i;
2235 i++;
2236 if (TREE_CHAIN (method_list) == NULL_TREE)
2237 break;
2238 else
2239 method_list = TREE_CHAIN (method_list);
2242 TREE_CHAIN (method_list) = build_tree_list (t, t);
2243 return i;
2246 tree
2247 build_invokevirtual (tree dtable, tree method)
2249 tree func;
2250 tree nativecode_ptr_ptr_type_node
2251 = build_pointer_type (nativecode_ptr_type_node);
2252 tree method_index;
2253 tree otable_index;
2255 if (flag_indirect_dispatch)
2257 gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))));
2259 otable_index
2260 = build_int_cst (NULL_TREE, get_symbol_table_index
2261 (method, &TYPE_OTABLE_METHODS (output_class)));
2262 method_index = build4 (ARRAY_REF, integer_type_node,
2263 TYPE_OTABLE_DECL (output_class),
2264 otable_index, NULL_TREE, NULL_TREE);
2266 else
2268 /* We fetch the DECL_VINDEX field directly here, rather than
2269 using get_method_index(). DECL_VINDEX is the true offset
2270 from the vtable base to a method, regrdless of any extra
2271 words inserted at the start of the vtable. */
2272 method_index = DECL_VINDEX (method);
2273 method_index = size_binop (MULT_EXPR, method_index,
2274 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2275 if (TARGET_VTABLE_USES_DESCRIPTORS)
2276 method_index = size_binop (MULT_EXPR, method_index,
2277 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2280 func = fold_build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2281 convert (nativecode_ptr_ptr_type_node, method_index));
2283 if (TARGET_VTABLE_USES_DESCRIPTORS)
2284 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2285 else
2286 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2288 return func;
2291 static GTY(()) tree class_ident;
2292 tree
2293 build_invokeinterface (tree dtable, tree method)
2295 tree lookup_arg;
2296 tree interface;
2297 tree idx;
2299 /* We expand invokeinterface here. */
2301 if (class_ident == NULL_TREE)
2302 class_ident = get_identifier ("class");
2304 dtable = build_java_indirect_ref (dtable_type, dtable,
2305 flag_check_references);
2306 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2307 lookup_field (&dtable_type, class_ident), NULL_TREE);
2309 interface = DECL_CONTEXT (method);
2310 gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface)));
2311 layout_class_methods (interface);
2313 if (flag_indirect_dispatch)
2315 int itable_index
2316 = 2 * (get_symbol_table_index
2317 (method, &TYPE_ITABLE_METHODS (output_class)));
2318 interface
2319 = build4 (ARRAY_REF,
2320 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2321 TYPE_ITABLE_DECL (output_class),
2322 build_int_cst (NULL_TREE, itable_index-1),
2323 NULL_TREE, NULL_TREE);
2324 idx
2325 = build4 (ARRAY_REF,
2326 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2327 TYPE_ITABLE_DECL (output_class),
2328 build_int_cst (NULL_TREE, itable_index),
2329 NULL_TREE, NULL_TREE);
2330 interface = convert (class_ptr_type, interface);
2331 idx = convert (integer_type_node, idx);
2333 else
2335 idx = build_int_cst (NULL_TREE,
2336 get_interface_method_index (method, interface));
2337 interface = build_class_ref (interface);
2340 lookup_arg = tree_cons (NULL_TREE, dtable,
2341 tree_cons (NULL_TREE, interface,
2342 build_tree_list (NULL_TREE, idx)));
2344 return build3 (CALL_EXPR, ptr_type_node,
2345 build_address_of (soft_lookupinterfacemethod_node),
2346 lookup_arg, NULL_TREE);
2349 /* Expand one of the invoke_* opcodes.
2350 OPCODE is the specific opcode.
2351 METHOD_REF_INDEX is an index into the constant pool.
2352 NARGS is the number of arguments, or -1 if not specified. */
2354 static void
2355 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2357 tree method_signature
2358 = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2359 tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2360 method_ref_index);
2361 tree self_type
2362 = get_class_constant (current_jcf,
2363 COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2364 method_ref_index));
2365 const char *const self_name
2366 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2367 tree call, func, method, arg_list, method_type;
2368 tree check = NULL_TREE;
2370 if (! CLASS_LOADED_P (self_type))
2372 load_class (self_type, 1);
2373 safe_layout_class (self_type);
2374 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2375 fatal_error ("failed to find class '%s'", self_name);
2377 layout_class_methods (self_type);
2379 if (ID_INIT_P (method_name))
2380 method = lookup_java_constructor (self_type, method_signature);
2381 else
2382 method = lookup_java_method (self_type, method_name, method_signature);
2384 /* We've found a method in a class other than the one in which it
2385 was wanted. This can happen if, for instance, we're trying to
2386 compile invokespecial super.equals().
2387 FIXME: This is a kludge. Rather than nullifying the result, we
2388 should change lookup_java_method() so that it doesn't search the
2389 superclass chain when we're BC-compiling. */
2390 if (! flag_verify_invocations
2391 && method
2392 && ! TYPE_ARRAY_P (self_type)
2393 && self_type != DECL_CONTEXT (method))
2394 method = NULL_TREE;
2396 /* We've found a method in an interface, but this isn't an interface
2397 call. */
2398 if (opcode != OPCODE_invokeinterface
2399 && method
2400 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2401 method = NULL_TREE;
2403 /* We've found a non-interface method but we are making an
2404 interface call. This can happen if the interface overrides a
2405 method in Object. */
2406 if (! flag_verify_invocations
2407 && opcode == OPCODE_invokeinterface
2408 && method
2409 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2410 method = NULL_TREE;
2412 if (method == NULL_TREE)
2414 if (flag_verify_invocations || ! flag_indirect_dispatch)
2416 error ("class '%s' has no method named '%s' matching signature '%s'",
2417 self_name,
2418 IDENTIFIER_POINTER (method_name),
2419 IDENTIFIER_POINTER (method_signature));
2421 else
2423 int flags = ACC_PUBLIC;
2424 if (opcode == OPCODE_invokestatic)
2425 flags |= ACC_STATIC;
2426 if (opcode == OPCODE_invokeinterface)
2428 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2429 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2431 method = add_method (self_type, flags, method_name,
2432 method_signature);
2433 DECL_ARTIFICIAL (method) = 1;
2434 METHOD_DUMMY (method) = 1;
2435 layout_class_method (self_type, NULL,
2436 method, NULL);
2440 /* Invoke static can't invoke static/abstract method */
2441 if (method != NULL_TREE)
2443 if (opcode == OPCODE_invokestatic)
2445 if (!METHOD_STATIC (method))
2447 error ("invokestatic on non static method");
2448 method = NULL_TREE;
2450 else if (METHOD_ABSTRACT (method))
2452 error ("invokestatic on abstract method");
2453 method = NULL_TREE;
2456 else
2458 if (METHOD_STATIC (method))
2460 error ("invoke[non-static] on static method");
2461 method = NULL_TREE;
2466 if (method == NULL_TREE)
2468 /* If we got here, we emitted an error message above. So we
2469 just pop the arguments, push a properly-typed zero, and
2470 continue. */
2471 method_type = get_type_from_signature (method_signature);
2472 pop_arguments (TYPE_ARG_TYPES (method_type));
2473 if (opcode != OPCODE_invokestatic)
2474 pop_type (self_type);
2475 method_type = promote_type (TREE_TYPE (method_type));
2476 push_value (convert (method_type, integer_zero_node));
2477 return;
2480 method_type = TREE_TYPE (method);
2481 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2482 flush_quick_stack ();
2484 maybe_rewrite_invocation (&method, &arg_list, &method_signature);
2486 func = NULL_TREE;
2487 if (opcode == OPCODE_invokestatic)
2488 func = build_known_method_ref (method, method_type, self_type,
2489 method_signature, arg_list);
2490 else if (opcode == OPCODE_invokespecial
2491 || (opcode == OPCODE_invokevirtual
2492 && (METHOD_PRIVATE (method)
2493 || METHOD_FINAL (method)
2494 || CLASS_FINAL (TYPE_NAME (self_type)))))
2496 /* If the object for the method call is null, we throw an
2497 exception. We don't do this if the object is the current
2498 method's `this'. In other cases we just rely on an
2499 optimization pass to eliminate redundant checks. FIXME:
2500 Unfortunately there doesn't seem to be a way to determine
2501 what the current method is right now.
2502 We do omit the check if we're calling <init>. */
2503 /* We use a SAVE_EXPR here to make sure we only evaluate
2504 the new `self' expression once. */
2505 tree save_arg = save_expr (TREE_VALUE (arg_list));
2506 TREE_VALUE (arg_list) = save_arg;
2507 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2508 func = build_known_method_ref (method, method_type, self_type,
2509 method_signature, arg_list);
2511 else
2513 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2514 arg_list);
2515 if (opcode == OPCODE_invokevirtual)
2516 func = build_invokevirtual (dtable, method);
2517 else
2518 func = build_invokeinterface (dtable, method);
2521 if (TREE_CODE (func) == ADDR_EXPR)
2522 TREE_TYPE (func) = build_pointer_type (method_type);
2523 else
2524 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2526 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2527 func, arg_list, NULL_TREE);
2528 TREE_SIDE_EFFECTS (call) = 1;
2529 call = check_for_builtin (method, call);
2531 if (check != NULL_TREE)
2533 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2534 TREE_SIDE_EFFECTS (call) = 1;
2537 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2538 java_add_stmt (call);
2539 else
2541 push_value (call);
2542 flush_quick_stack ();
2546 /* Create a stub which will be put into the vtable but which will call
2547 a JNI function. */
2549 tree
2550 build_jni_stub (tree method)
2552 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2553 tree jni_func_type, tem;
2554 tree env_var, res_var = NULL_TREE, block;
2555 tree method_args, res_type;
2556 tree meth_var;
2557 tree bind;
2559 int args_size = 0;
2561 tree klass = DECL_CONTEXT (method);
2562 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2563 klass = build_class_ref (klass);
2565 gcc_assert (METHOD_NATIVE (method) && flag_jni);
2567 DECL_ARTIFICIAL (method) = 1;
2568 DECL_EXTERNAL (method) = 0;
2570 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2571 DECL_CONTEXT (env_var) = method;
2573 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2575 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2576 TREE_TYPE (TREE_TYPE (method)));
2577 DECL_CONTEXT (res_var) = method;
2578 TREE_CHAIN (env_var) = res_var;
2581 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2582 TREE_STATIC (meth_var) = 1;
2583 TREE_PUBLIC (meth_var) = 0;
2584 DECL_EXTERNAL (meth_var) = 0;
2585 DECL_CONTEXT (meth_var) = method;
2586 DECL_ARTIFICIAL (meth_var) = 1;
2587 DECL_INITIAL (meth_var) = null_pointer_node;
2588 TREE_USED (meth_var) = 1;
2589 chainon (env_var, meth_var);
2590 build_result_decl (method);
2592 /* One strange way that the front ends are different is that they
2593 store arguments differently. */
2594 if (from_class)
2595 method_args = DECL_ARGUMENTS (method);
2596 else
2597 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2598 block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
2599 TREE_SIDE_EFFECTS (block) = 1;
2600 /* When compiling from source we don't set the type of the block,
2601 because that will prevent patch_return from ever being run. */
2602 if (from_class)
2603 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2605 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2606 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2607 build3 (CALL_EXPR, ptr_type_node,
2608 build_address_of (soft_getjnienvnewframe_node),
2609 build_tree_list (NULL_TREE, klass),
2610 NULL_TREE));
2611 CAN_COMPLETE_NORMALLY (body) = 1;
2613 /* All the arguments to this method become arguments to the
2614 underlying JNI function. If we had to wrap object arguments in a
2615 special way, we would do that here. */
2616 args = NULL_TREE;
2617 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2619 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2620 #ifdef PARM_BOUNDARY
2621 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2622 * PARM_BOUNDARY);
2623 #endif
2624 args_size += (arg_bits / BITS_PER_UNIT);
2626 args = tree_cons (NULL_TREE, tem, args);
2628 args = nreverse (args);
2629 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2631 /* For a static method the second argument is the class. For a
2632 non-static method the second argument is `this'; that is already
2633 available in the argument list. */
2634 if (METHOD_STATIC (method))
2636 args_size += int_size_in_bytes (TREE_TYPE (klass));
2637 args = tree_cons (NULL_TREE, klass, args);
2638 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2641 /* The JNIEnv structure is the first argument to the JNI function. */
2642 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2643 args = tree_cons (NULL_TREE, env_var, args);
2644 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2646 /* We call _Jv_LookupJNIMethod to find the actual underlying
2647 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2648 exception if this function is not found at runtime. */
2649 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2650 method_sig = build_java_signature (TREE_TYPE (method));
2651 lookup_arg = tree_cons (NULL_TREE,
2652 build_utf8_ref (unmangle_classname
2653 (IDENTIFIER_POINTER (method_sig),
2654 IDENTIFIER_LENGTH (method_sig))),
2655 tem);
2656 tem = DECL_NAME (method);
2657 lookup_arg
2658 = tree_cons (NULL_TREE, klass,
2659 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2661 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2663 #ifdef MODIFY_JNI_METHOD_CALL
2664 tem = MODIFY_JNI_METHOD_CALL (tem);
2665 #endif
2667 jni_func_type = build_pointer_type (tem);
2669 jnifunc = build3 (COND_EXPR, ptr_type_node,
2670 meth_var, meth_var,
2671 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2672 build3 (CALL_EXPR, ptr_type_node,
2673 build_address_of
2674 (soft_lookupjnimethod_node),
2675 lookup_arg, NULL_TREE)));
2677 /* Now we make the actual JNI call via the resulting function
2678 pointer. */
2679 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2680 build1 (NOP_EXPR, jni_func_type, jnifunc),
2681 args, NULL_TREE);
2683 /* If the JNI call returned a result, capture it here. If we had to
2684 unwrap JNI object results, we would do that here. */
2685 if (res_var != NULL_TREE)
2687 /* If the call returns an object, it may return a JNI weak
2688 reference, in which case we must unwrap it. */
2689 if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
2690 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2691 build_address_of (soft_unwrapjni_node),
2692 build_tree_list (NULL_TREE, call),
2693 NULL_TREE);
2694 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2695 res_var, call);
2698 TREE_SIDE_EFFECTS (call) = 1;
2699 CAN_COMPLETE_NORMALLY (call) = 1;
2701 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2702 TREE_SIDE_EFFECTS (body) = 1;
2704 /* Now free the environment we allocated. */
2705 call = build3 (CALL_EXPR, ptr_type_node,
2706 build_address_of (soft_jnipopsystemframe_node),
2707 build_tree_list (NULL_TREE, env_var),
2708 NULL_TREE);
2709 TREE_SIDE_EFFECTS (call) = 1;
2710 CAN_COMPLETE_NORMALLY (call) = 1;
2711 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2712 TREE_SIDE_EFFECTS (body) = 1;
2714 /* Finally, do the return. */
2715 res_type = void_type_node;
2716 if (res_var != NULL_TREE)
2718 tree drt;
2719 gcc_assert (DECL_RESULT (method));
2720 /* Make sure we copy the result variable to the actual
2721 result. We use the type of the DECL_RESULT because it
2722 might be different from the return type of the function:
2723 it might be promoted. */
2724 drt = TREE_TYPE (DECL_RESULT (method));
2725 if (drt != TREE_TYPE (res_var))
2726 res_var = build1 (CONVERT_EXPR, drt, res_var);
2727 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2728 TREE_SIDE_EFFECTS (res_var) = 1;
2731 body = build2 (COMPOUND_EXPR, void_type_node, body,
2732 build1 (RETURN_EXPR, res_type, res_var));
2733 TREE_SIDE_EFFECTS (body) = 1;
2735 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2736 body, block);
2737 return bind;
2740 /* Expand an operation to extract from or store into a field.
2741 IS_STATIC is 1 iff the field is static.
2742 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2743 FIELD_REF_INDEX is an index into the constant pool. */
2745 static void
2746 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2748 tree self_type
2749 = get_class_constant (current_jcf,
2750 COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2751 field_ref_index));
2752 const char *self_name
2753 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2754 tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2755 tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
2756 field_ref_index);
2757 tree field_type = get_type_from_signature (field_signature);
2758 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2759 tree field_ref;
2760 int is_error = 0;
2761 tree original_self_type = self_type;
2762 tree field_decl;
2764 if (! CLASS_LOADED_P (self_type))
2765 load_class (self_type, 1);
2766 field_decl = lookup_field (&self_type, field_name);
2767 if (field_decl == error_mark_node)
2769 is_error = 1;
2771 else if (field_decl == NULL_TREE)
2773 if (! flag_verify_invocations)
2775 int flags = ACC_PUBLIC;
2776 if (is_static)
2777 flags |= ACC_STATIC;
2778 self_type = original_self_type;
2779 field_decl = add_field (original_self_type, field_name,
2780 field_type, flags);
2781 DECL_ARTIFICIAL (field_decl) = 1;
2782 DECL_IGNORED_P (field_decl) = 1;
2784 else
2786 error ("missing field '%s' in '%s'",
2787 IDENTIFIER_POINTER (field_name), self_name);
2788 is_error = 1;
2791 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2793 error ("mismatching signature for field '%s' in '%s'",
2794 IDENTIFIER_POINTER (field_name), self_name);
2795 is_error = 1;
2797 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2798 if (is_error)
2800 if (! is_putting)
2801 push_value (convert (field_type, integer_zero_node));
2802 flush_quick_stack ();
2803 return;
2806 field_ref = build_field_ref (field_ref, self_type, field_name);
2807 if (is_static && ! flag_indirect_dispatch)
2809 /* Initialize the declaring class of the field. */
2810 field_ref = build_class_init (DECL_CONTEXT (field_ref), field_ref);
2812 if (is_putting)
2814 flush_quick_stack ();
2815 if (FIELD_FINAL (field_decl))
2817 if (DECL_CONTEXT (field_decl) != current_class)
2818 error ("assignment to final field %q+D not in field's class",
2819 field_decl);
2820 else if (FIELD_STATIC (field_decl))
2822 if (!DECL_CLINIT_P (current_function_decl))
2823 warning (0, "assignment to final static field %q+D not in "
2824 "class initializer",
2825 field_decl);
2827 else
2829 tree cfndecl_name = DECL_NAME (current_function_decl);
2830 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2831 && !ID_FINIT_P (cfndecl_name))
2832 warning (0, "assignment to final field %q+D not in constructor",
2833 field_decl);
2836 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2837 field_ref, new_value));
2839 else
2840 push_value (field_ref);
2843 void
2844 load_type_state (tree label)
2846 int i;
2847 tree vec = LABEL_TYPE_STATE (label);
2848 int cur_length = TREE_VEC_LENGTH (vec);
2849 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2850 for (i = 0; i < cur_length; i++)
2851 type_map [i] = TREE_VEC_ELT (vec, i);
2854 /* Go over METHOD's bytecode and note instruction starts in
2855 instruction_bits[]. */
2857 void
2858 note_instructions (JCF *jcf, tree method)
2860 int PC;
2861 unsigned char* byte_ops;
2862 long length = DECL_CODE_LENGTH (method);
2864 int saw_index;
2865 jint INT_temp;
2867 #undef RET /* Defined by config/i386/i386.h */
2868 #undef PTR
2869 #define BCODE byte_ops
2870 #define BYTE_type_node byte_type_node
2871 #define SHORT_type_node short_type_node
2872 #define INT_type_node int_type_node
2873 #define LONG_type_node long_type_node
2874 #define CHAR_type_node char_type_node
2875 #define PTR_type_node ptr_type_node
2876 #define FLOAT_type_node float_type_node
2877 #define DOUBLE_type_node double_type_node
2878 #define VOID_type_node void_type_node
2879 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2880 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2881 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2882 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2884 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2886 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2887 byte_ops = jcf->read_ptr;
2888 instruction_bits = xrealloc (instruction_bits, length + 1);
2889 memset (instruction_bits, 0, length + 1);
2891 /* This pass figures out which PC can be the targets of jumps. */
2892 for (PC = 0; PC < length;)
2894 int oldpc = PC; /* PC at instruction start. */
2895 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2896 switch (byte_ops[PC++])
2898 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2899 case OPCODE: \
2900 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2901 break;
2903 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2905 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2906 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2907 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2908 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2909 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2910 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2911 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2912 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2914 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2915 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2916 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2917 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2918 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2919 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2920 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2921 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2923 /* two forms of wide instructions */
2924 #define PRE_SPECIAL_WIDE(IGNORE) \
2926 int modified_opcode = IMMEDIATE_u1; \
2927 if (modified_opcode == OPCODE_iinc) \
2929 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2930 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2932 else \
2934 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2938 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2940 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2942 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2943 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2944 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2945 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2946 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2947 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2948 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2949 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2950 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2951 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2953 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2954 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2955 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2956 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2957 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2958 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2959 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2960 NOTE_LABEL (PC); \
2961 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2963 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2965 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2966 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2968 #define PRE_LOOKUP_SWITCH \
2969 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2970 NOTE_LABEL (default_offset+oldpc); \
2971 if (npairs >= 0) \
2972 while (--npairs >= 0) { \
2973 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2974 jint offset = IMMEDIATE_s4; \
2975 NOTE_LABEL (offset+oldpc); } \
2978 #define PRE_TABLE_SWITCH \
2979 { jint default_offset = IMMEDIATE_s4; \
2980 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2981 NOTE_LABEL (default_offset+oldpc); \
2982 if (low <= high) \
2983 while (low++ <= high) { \
2984 jint offset = IMMEDIATE_s4; \
2985 NOTE_LABEL (offset+oldpc); } \
2988 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2989 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2990 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2991 (void)(IMMEDIATE_u2); \
2992 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2994 #include "javaop.def"
2995 #undef JAVAOP
2997 } /* for */
3000 void
3001 expand_byte_code (JCF *jcf, tree method)
3003 int PC;
3004 int i;
3005 const unsigned char *linenumber_pointer;
3006 int dead_code_index = -1;
3007 unsigned char* byte_ops;
3008 long length = DECL_CODE_LENGTH (method);
3010 stack_pointer = 0;
3011 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
3012 byte_ops = jcf->read_ptr;
3014 /* We make an initial pass of the line number table, to note
3015 which instructions have associated line number entries. */
3016 linenumber_pointer = linenumber_table;
3017 for (i = 0; i < linenumber_count; i++)
3019 int pc = GET_u2 (linenumber_pointer);
3020 linenumber_pointer += 4;
3021 if (pc >= length)
3022 warning (0, "invalid PC in line number table");
3023 else
3025 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
3026 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
3027 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
3031 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
3032 return;
3034 promote_arguments ();
3036 /* Translate bytecodes. */
3037 linenumber_pointer = linenumber_table;
3038 for (PC = 0; PC < length;)
3040 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
3042 tree label = lookup_label (PC);
3043 flush_quick_stack ();
3044 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
3045 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
3046 if (LABEL_VERIFIED (label) || PC == 0)
3047 load_type_state (label);
3050 if (! (instruction_bits [PC] & BCODE_VERIFIED))
3052 if (dead_code_index == -1)
3054 /* This is the start of a region of unreachable bytecodes.
3055 They still need to be processed in order for EH ranges
3056 to get handled correctly. However, we can simply
3057 replace these bytecodes with nops. */
3058 dead_code_index = PC;
3061 /* Turn this bytecode into a nop. */
3062 byte_ops[PC] = 0x0;
3064 else
3066 if (dead_code_index != -1)
3068 /* We've just reached the end of a region of dead code. */
3069 if (extra_warnings)
3070 warning (0, "unreachable bytecode from %d to before %d",
3071 dead_code_index, PC);
3072 dead_code_index = -1;
3076 /* Handle possible line number entry for this PC.
3078 This code handles out-of-order and multiple linenumbers per PC,
3079 but is optimized for the case of line numbers increasing
3080 monotonically with PC. */
3081 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
3083 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
3084 || GET_u2 (linenumber_pointer) != PC)
3085 linenumber_pointer = linenumber_table;
3086 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
3088 int pc = GET_u2 (linenumber_pointer);
3089 linenumber_pointer += 4;
3090 if (pc == PC)
3092 int line = GET_u2 (linenumber_pointer - 2);
3093 #ifdef USE_MAPPED_LOCATION
3094 input_location = linemap_line_start (&line_table, line, 1);
3095 #else
3096 input_location.line = line;
3097 #endif
3098 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
3099 break;
3103 maybe_pushlevels (PC);
3104 PC = process_jvm_instruction (PC, byte_ops, length);
3105 maybe_poplevels (PC);
3106 } /* for */
3108 if (dead_code_index != -1)
3110 /* We've just reached the end of a region of dead code. */
3111 if (extra_warnings)
3112 warning (0, "unreachable bytecode from %d to the end of the method",
3113 dead_code_index);
3117 static void
3118 java_push_constant_from_pool (JCF *jcf, int index)
3120 tree c;
3121 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3123 tree name;
3124 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3125 index = alloc_name_constant (CONSTANT_String, name);
3126 c = build_ref_from_constant_pool (index);
3127 c = convert (promote_type (string_type_node), c);
3129 else if (JPOOL_TAG (jcf, index) == CONSTANT_Class
3130 || JPOOL_TAG (jcf, index) == CONSTANT_ResolvedClass)
3132 tree record = get_class_constant (jcf, index);
3133 c = build_class_ref (record);
3135 else
3136 c = get_constant (jcf, index);
3137 push_value (c);
3141 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3142 long length ATTRIBUTE_UNUSED)
3144 const char *opname; /* Temporary ??? */
3145 int oldpc = PC; /* PC at instruction start. */
3147 /* If the instruction is at the beginning of an exception handler,
3148 replace the top of the stack with the thrown object reference. */
3149 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3151 /* Note that the verifier will not emit a type map at all for
3152 dead exception handlers. In this case we just ignore the
3153 situation. */
3154 if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
3156 tree type = pop_type (promote_type (throwable_type_node));
3157 push_value (build_exception_object_ref (type));
3161 switch (byte_ops[PC++])
3163 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3164 case OPCODE: \
3165 opname = #OPNAME; \
3166 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3167 break;
3169 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3171 int saw_index = 0; \
3172 int index = OPERAND_VALUE; \
3173 build_java_ret \
3174 (find_local_variable (index, return_address_type_node, oldpc)); \
3177 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3179 /* OPERAND_VALUE may have side-effects on PC */ \
3180 int opvalue = OPERAND_VALUE; \
3181 build_java_jsr (oldpc + opvalue, PC); \
3184 /* Push a constant onto the stack. */
3185 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3186 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3187 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3188 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3190 /* internal macro added for use by the WIDE case */
3191 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3192 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3194 /* Push local variable onto the opcode stack. */
3195 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3197 /* have to do this since OPERAND_VALUE may have side-effects */ \
3198 int opvalue = OPERAND_VALUE; \
3199 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3202 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3203 expand_java_return (OPERAND_TYPE##_type_node)
3205 #define REM_EXPR TRUNC_MOD_EXPR
3206 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3207 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3209 #define FIELD(IS_STATIC, IS_PUT) \
3210 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3212 #define TEST(OPERAND_TYPE, CONDITION) \
3213 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3215 #define COND(OPERAND_TYPE, CONDITION) \
3216 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3218 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3219 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3221 #define BRANCH_GOTO(OPERAND_VALUE) \
3222 expand_java_goto (oldpc + OPERAND_VALUE)
3224 #define BRANCH_CALL(OPERAND_VALUE) \
3225 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3227 #if 0
3228 #define BRANCH_RETURN(OPERAND_VALUE) \
3230 tree type = OPERAND_TYPE##_type_node; \
3231 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3232 expand_java_ret (value); \
3234 #endif
3236 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3237 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3238 fprintf (stderr, "(not implemented)\n")
3239 #define NOT_IMPL1(OPERAND_VALUE) \
3240 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3241 fprintf (stderr, "(not implemented)\n")
3243 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3245 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3247 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3249 #define STACK_SWAP(COUNT) java_stack_swap()
3251 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3252 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3253 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3255 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3256 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3258 #define LOOKUP_SWITCH \
3259 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3260 tree selector = pop_value (INT_type_node); \
3261 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3262 while (--npairs >= 0) \
3264 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3265 expand_java_add_case (switch_expr, match, oldpc + offset); \
3269 #define TABLE_SWITCH \
3270 { jint default_offset = IMMEDIATE_s4; \
3271 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3272 tree selector = pop_value (INT_type_node); \
3273 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3274 for (; low <= high; low++) \
3276 jint offset = IMMEDIATE_s4; \
3277 expand_java_add_case (switch_expr, low, oldpc + offset); \
3281 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3282 { int opcode = byte_ops[PC-1]; \
3283 int method_ref_index = IMMEDIATE_u2; \
3284 int nargs; \
3285 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3286 else nargs = -1; \
3287 expand_invoke (opcode, method_ref_index, nargs); \
3290 /* Handle new, checkcast, instanceof */
3291 #define OBJECT(TYPE, OP) \
3292 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3294 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3296 #define ARRAY_LOAD(OPERAND_TYPE) \
3298 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3301 #define ARRAY_STORE(OPERAND_TYPE) \
3303 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3306 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3307 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3308 #define ARRAY_NEW_PTR() \
3309 push_value (build_anewarray (get_class_constant (current_jcf, \
3310 IMMEDIATE_u2), \
3311 pop_value (int_type_node)));
3312 #define ARRAY_NEW_NUM() \
3314 int atype = IMMEDIATE_u1; \
3315 push_value (build_newarray (atype, pop_value (int_type_node)));\
3317 #define ARRAY_NEW_MULTI() \
3319 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3320 int ndims = IMMEDIATE_u1; \
3321 expand_java_multianewarray( class, ndims ); \
3324 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3325 push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3326 pop_value (OPERAND_TYPE##_type_node)));
3328 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3330 push_value (build1 (NOP_EXPR, int_type_node, \
3331 (convert (TO_TYPE##_type_node, \
3332 pop_value (FROM_TYPE##_type_node))))); \
3335 #define CONVERT(FROM_TYPE, TO_TYPE) \
3337 push_value (convert (TO_TYPE##_type_node, \
3338 pop_value (FROM_TYPE##_type_node))); \
3341 /* internal macro added for use by the WIDE case
3342 Added TREE_TYPE (decl) assignment, apbianco */
3343 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3345 tree decl, value; \
3346 int index = OPVALUE; \
3347 tree type = OPTYPE; \
3348 value = pop_value (type); \
3349 type = TREE_TYPE (value); \
3350 decl = find_local_variable (index, type, oldpc); \
3351 set_local_type (index, type); \
3352 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3353 update_aliases (decl, index, PC); \
3356 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3358 /* have to do this since OPERAND_VALUE may have side-effects */ \
3359 int opvalue = OPERAND_VALUE; \
3360 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3363 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3364 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3366 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3367 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3369 #define MONITOR_OPERATION(call) \
3371 tree o = pop_value (ptr_type_node); \
3372 tree c; \
3373 flush_quick_stack (); \
3374 c = build_java_monitor (call, o); \
3375 TREE_SIDE_EFFECTS (c) = 1; \
3376 java_add_stmt (c); \
3379 #define SPECIAL_IINC(IGNORED) \
3381 unsigned int local_var_index = IMMEDIATE_u1; \
3382 int ival = IMMEDIATE_s1; \
3383 expand_iinc(local_var_index, ival, oldpc); \
3386 #define SPECIAL_WIDE(IGNORED) \
3388 int modified_opcode = IMMEDIATE_u1; \
3389 unsigned int local_var_index = IMMEDIATE_u2; \
3390 switch (modified_opcode) \
3392 case OPCODE_iinc: \
3394 int ival = IMMEDIATE_s2; \
3395 expand_iinc (local_var_index, ival, oldpc); \
3396 break; \
3398 case OPCODE_iload: \
3399 case OPCODE_lload: \
3400 case OPCODE_fload: \
3401 case OPCODE_dload: \
3402 case OPCODE_aload: \
3404 /* duplicate code from LOAD macro */ \
3405 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3406 break; \
3408 case OPCODE_istore: \
3409 case OPCODE_lstore: \
3410 case OPCODE_fstore: \
3411 case OPCODE_dstore: \
3412 case OPCODE_astore: \
3414 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3415 break; \
3417 default: \
3418 error ("unrecogized wide sub-instruction"); \
3422 #define SPECIAL_THROW(IGNORED) \
3423 build_java_athrow (pop_value (throwable_type_node))
3425 #define SPECIAL_BREAK NOT_IMPL1
3426 #define IMPL NOT_IMPL
3428 #include "javaop.def"
3429 #undef JAVAOP
3430 default:
3431 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3433 return PC;
3436 /* Return the opcode at PC in the code section pointed to by
3437 CODE_OFFSET. */
3439 static unsigned char
3440 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3442 unsigned char opcode;
3443 long absolute_offset = (long)JCF_TELL (jcf);
3445 JCF_SEEK (jcf, code_offset);
3446 opcode = jcf->read_ptr [pc];
3447 JCF_SEEK (jcf, absolute_offset);
3448 return opcode;
3451 /* Some bytecode compilers are emitting accurate LocalVariableTable
3452 attributes. Here's an example:
3454 PC <t>store_<n>
3455 PC+1 ...
3457 Attribute "LocalVariableTable"
3458 slot #<n>: ... (PC: PC+1 length: L)
3460 This is accurate because the local in slot <n> really exists after
3461 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3463 This procedure recognizes this situation and extends the live range
3464 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3465 length of the store instruction.)
3467 This function is used by `give_name_to_locals' so that a local's
3468 DECL features a DECL_LOCAL_START_PC such that the first related
3469 store operation will use DECL as a destination, not an unrelated
3470 temporary created for the occasion.
3472 This function uses a global (instruction_bits) `note_instructions' should
3473 have allocated and filled properly. */
3476 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3477 int start_pc, int slot)
3479 int first, index, opcode;
3480 int pc, insn_pc;
3481 int wide_found = 0;
3483 if (!start_pc)
3484 return start_pc;
3486 first = index = -1;
3488 /* Find last previous instruction and remember it */
3489 for (pc = start_pc-1; pc; pc--)
3490 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3491 break;
3492 insn_pc = pc;
3494 /* Retrieve the instruction, handle `wide'. */
3495 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3496 if (opcode == OPCODE_wide)
3498 wide_found = 1;
3499 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3502 switch (opcode)
3504 case OPCODE_astore_0:
3505 case OPCODE_astore_1:
3506 case OPCODE_astore_2:
3507 case OPCODE_astore_3:
3508 first = OPCODE_astore_0;
3509 break;
3511 case OPCODE_istore_0:
3512 case OPCODE_istore_1:
3513 case OPCODE_istore_2:
3514 case OPCODE_istore_3:
3515 first = OPCODE_istore_0;
3516 break;
3518 case OPCODE_lstore_0:
3519 case OPCODE_lstore_1:
3520 case OPCODE_lstore_2:
3521 case OPCODE_lstore_3:
3522 first = OPCODE_lstore_0;
3523 break;
3525 case OPCODE_fstore_0:
3526 case OPCODE_fstore_1:
3527 case OPCODE_fstore_2:
3528 case OPCODE_fstore_3:
3529 first = OPCODE_fstore_0;
3530 break;
3532 case OPCODE_dstore_0:
3533 case OPCODE_dstore_1:
3534 case OPCODE_dstore_2:
3535 case OPCODE_dstore_3:
3536 first = OPCODE_dstore_0;
3537 break;
3539 case OPCODE_astore:
3540 case OPCODE_istore:
3541 case OPCODE_lstore:
3542 case OPCODE_fstore:
3543 case OPCODE_dstore:
3544 index = peek_opcode_at_pc (jcf, code_offset, pc);
3545 if (wide_found)
3547 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3548 index = (other << 8) + index;
3550 break;
3553 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3554 means we have a <t>store. */
3555 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3556 start_pc = insn_pc;
3558 return start_pc;
3561 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3562 order, as specified by Java Language Specification.
3564 The problem is that while expand_expr will evaluate its sub-operands in
3565 left-to-right order, for variables it will just return an rtx (i.e.
3566 an lvalue) for the variable (rather than an rvalue). So it is possible
3567 that a later sub-operand will change the register, and when the
3568 actual operation is done, it will use the new value, when it should
3569 have used the original value.
3571 We fix this by using save_expr. This forces the sub-operand to be
3572 copied into a fresh virtual register,
3574 For method invocation, we modify the arguments so that a
3575 left-to-right order evaluation is performed. Saved expressions
3576 will, in CALL_EXPR order, be reused when the call will be expanded.
3578 We also promote outgoing args if needed. */
3580 tree
3581 force_evaluation_order (tree node)
3583 if (flag_syntax_only)
3584 return node;
3585 if (TREE_CODE (node) == CALL_EXPR
3586 || TREE_CODE (node) == NEW_CLASS_EXPR
3587 || (TREE_CODE (node) == COMPOUND_EXPR
3588 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3589 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3591 tree arg, cmp;
3593 arg = node;
3595 /* Position arg properly, account for wrapped around ctors. */
3596 if (TREE_CODE (node) == COMPOUND_EXPR)
3597 arg = TREE_OPERAND (node, 0);
3599 arg = TREE_OPERAND (arg, 1);
3601 /* An empty argument list is ok, just ignore it. */
3602 if (!arg)
3603 return node;
3605 /* Not having a list of arguments here is an error. */
3606 gcc_assert (TREE_CODE (arg) == TREE_LIST);
3608 /* This reverses the evaluation order. This is a desired effect. */
3609 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3611 /* Promote types smaller than integer. This is required by
3612 some ABIs. */
3613 tree type = TREE_TYPE (TREE_VALUE (arg));
3614 tree saved;
3615 if (targetm.calls.promote_prototypes (type)
3616 && INTEGRAL_TYPE_P (type)
3617 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
3618 TYPE_SIZE (integer_type_node)))
3619 TREE_VALUE (arg) = fold_convert (integer_type_node, TREE_VALUE (arg));
3621 saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3622 cmp = (cmp == NULL_TREE ? saved :
3623 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3624 TREE_VALUE (arg) = saved;
3627 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3628 TREE_SIDE_EFFECTS (cmp) = 1;
3630 if (cmp)
3632 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3633 if (TREE_TYPE (cmp) != void_type_node)
3634 cmp = save_expr (cmp);
3635 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3636 TREE_SIDE_EFFECTS (cmp) = 1;
3637 node = cmp;
3640 return node;
3643 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3644 location where an expression or an identifier were encountered. It
3645 is necessary for languages where the frontend parser will handle
3646 recursively more than one file (Java is one of them). */
3648 tree
3649 build_expr_wfl (tree node,
3650 #ifdef USE_MAPPED_LOCATION
3651 source_location location
3652 #else
3653 const char *file, int line, int col
3654 #endif
3657 tree wfl;
3659 #ifdef USE_MAPPED_LOCATION
3660 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3661 SET_EXPR_LOCATION (wfl, location);
3662 #else
3663 static const char *last_file = 0;
3664 static tree last_filenode = NULL_TREE;
3666 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3668 EXPR_WFL_SET_LINECOL (wfl, line, col);
3669 if (file != last_file)
3671 last_file = file;
3672 last_filenode = file ? get_identifier (file) : NULL_TREE;
3674 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3675 #endif
3676 EXPR_WFL_NODE (wfl) = node;
3677 if (node)
3679 if (!TYPE_P (node))
3680 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3681 TREE_TYPE (wfl) = TREE_TYPE (node);
3684 return wfl;
3687 #ifdef USE_MAPPED_LOCATION
3688 tree
3689 expr_add_location (tree node, source_location location, bool statement)
3691 tree wfl;
3692 #if 0
3693 /* FIXME. This optimization causes failures in code that expects an
3694 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3695 if (node && ! (statement && flag_emit_class_files))
3697 source_location node_loc = EXPR_LOCATION (node);
3698 if (node_loc == location || location == UNKNOWN_LOCATION)
3699 return node;
3700 if (node_loc == UNKNOWN_LOCATION
3701 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3703 SET_EXPR_LOCATION (node, location);
3704 return node;
3707 #endif
3708 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3709 SET_EXPR_LOCATION (wfl, location);
3710 EXPR_WFL_NODE (wfl) = node;
3711 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3712 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3713 if (node)
3715 if (!TYPE_P (node))
3716 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3717 TREE_TYPE (wfl) = TREE_TYPE (node);
3720 return wfl;
3722 #endif
3724 /* Build a node to represent empty statements and blocks. */
3726 tree
3727 build_java_empty_stmt (void)
3729 tree t = build_empty_stmt ();
3730 CAN_COMPLETE_NORMALLY (t) = 1;
3731 return t;
3734 /* Promote all args of integral type before generating any code. */
3736 static void
3737 promote_arguments (void)
3739 int i;
3740 tree arg;
3741 for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
3742 arg != NULL_TREE; arg = TREE_CHAIN (arg), i++)
3744 tree arg_type = TREE_TYPE (arg);
3745 if (INTEGRAL_TYPE_P (arg_type)
3746 && TYPE_PRECISION (arg_type) < 32)
3748 tree copy = find_local_variable (i, integer_type_node, -1);
3749 java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
3750 copy,
3751 fold_convert (integer_type_node, arg)));
3753 if (TYPE_IS_WIDE (arg_type))
3754 i++;
3758 #include "gt-java-expr.h"