2006-11-15 Rask Ingemann Lambertsen <rask@sygehus.dk>
[official-gcc.git] / gcc / java / expr.c
blob3cb3db7bb2c00c3e3a1515ed95e1d071493c2dfb
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));
1464 tree
1465 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1467 tree call = NULL;
1468 tree arg1 = convert (type, op1);
1469 tree arg2 = convert (type, op2);
1471 if (type == int_type_node)
1473 switch (op)
1475 case TRUNC_DIV_EXPR:
1476 call = soft_idiv_node;
1477 break;
1478 case TRUNC_MOD_EXPR:
1479 call = soft_irem_node;
1480 break;
1481 default:
1482 break;
1485 else if (type == long_type_node)
1487 switch (op)
1489 case TRUNC_DIV_EXPR:
1490 call = soft_ldiv_node;
1491 break;
1492 case TRUNC_MOD_EXPR:
1493 call = soft_lrem_node;
1494 break;
1495 default:
1496 break;
1500 gcc_assert (call);
1501 call = build3 (CALL_EXPR, type,
1502 build_address_of (call),
1503 tree_cons (NULL_TREE, arg1,
1504 build_tree_list (NULL_TREE, arg2)),
1505 NULL_TREE);
1507 return call;
1510 tree
1511 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1513 tree mask;
1514 switch (op)
1516 case URSHIFT_EXPR:
1518 tree u_type = java_unsigned_type (type);
1519 arg1 = convert (u_type, arg1);
1520 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1521 return convert (type, arg1);
1523 case LSHIFT_EXPR:
1524 case RSHIFT_EXPR:
1525 mask = build_int_cst (NULL_TREE,
1526 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1527 arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
1528 break;
1530 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1531 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1532 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1534 tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1535 boolean_type_node, arg1, arg2);
1536 tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
1537 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1538 ifexp2, integer_zero_node,
1539 op == COMPARE_L_EXPR
1540 ? integer_minus_one_node
1541 : integer_one_node);
1542 return fold_build3 (COND_EXPR, int_type_node, ifexp1,
1543 op == COMPARE_L_EXPR ? integer_one_node
1544 : integer_minus_one_node,
1545 second_compare);
1547 case COMPARE_EXPR:
1548 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1550 tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
1551 tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
1552 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1553 ifexp2, integer_one_node,
1554 integer_zero_node);
1555 return fold_build3 (COND_EXPR, int_type_node,
1556 ifexp1, integer_minus_one_node, second_compare);
1558 case TRUNC_DIV_EXPR:
1559 case TRUNC_MOD_EXPR:
1560 if (TREE_CODE (type) == REAL_TYPE
1561 && op == TRUNC_MOD_EXPR)
1563 tree call;
1564 if (type != double_type_node)
1566 arg1 = convert (double_type_node, arg1);
1567 arg2 = convert (double_type_node, arg2);
1569 call = build3 (CALL_EXPR, double_type_node,
1570 build_address_of (soft_fmod_node),
1571 tree_cons (NULL_TREE, arg1,
1572 build_tree_list (NULL_TREE, arg2)),
1573 NULL_TREE);
1574 if (type != double_type_node)
1575 call = convert (type, call);
1576 return call;
1579 if (TREE_CODE (type) == INTEGER_TYPE
1580 && flag_use_divide_subroutine
1581 && ! flag_syntax_only)
1582 return build_java_soft_divmod (op, type, arg1, arg2);
1584 break;
1585 default: ;
1587 return fold_build2 (op, type, arg1, arg2);
1590 static void
1591 expand_java_binop (tree type, enum tree_code op)
1593 tree larg, rarg;
1594 tree ltype = type;
1595 tree rtype = type;
1596 switch (op)
1598 case LSHIFT_EXPR:
1599 case RSHIFT_EXPR:
1600 case URSHIFT_EXPR:
1601 rtype = int_type_node;
1602 rarg = pop_value (rtype);
1603 break;
1604 default:
1605 rarg = pop_value (rtype);
1607 larg = pop_value (ltype);
1608 push_value (build_java_binop (op, type, larg, rarg));
1611 /* Lookup the field named NAME in *TYPEP or its super classes.
1612 If not found, return NULL_TREE.
1613 (If the *TYPEP is not found, or if the field reference is
1614 ambiguous, return error_mark_node.)
1615 If found, return the FIELD_DECL, and set *TYPEP to the
1616 class containing the field. */
1618 tree
1619 lookup_field (tree *typep, tree name)
1621 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1623 load_class (*typep, 1);
1624 safe_layout_class (*typep);
1625 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1626 return error_mark_node;
1630 tree field, binfo, base_binfo;
1631 tree save_field;
1632 int i;
1634 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1635 if (DECL_NAME (field) == name)
1636 return field;
1638 /* Process implemented interfaces. */
1639 save_field = NULL_TREE;
1640 for (binfo = TYPE_BINFO (*typep), i = 0;
1641 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1643 tree t = BINFO_TYPE (base_binfo);
1644 if ((field = lookup_field (&t, name)))
1646 if (save_field == field)
1647 continue;
1648 if (save_field == NULL_TREE)
1649 save_field = field;
1650 else
1652 tree i1 = DECL_CONTEXT (save_field);
1653 tree i2 = DECL_CONTEXT (field);
1654 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1655 IDENTIFIER_POINTER (name),
1656 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1657 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1658 return error_mark_node;
1663 if (save_field != NULL_TREE)
1664 return save_field;
1666 *typep = CLASSTYPE_SUPER (*typep);
1667 } while (*typep);
1668 return NULL_TREE;
1671 /* Look up the field named NAME in object SELF_VALUE,
1672 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1673 SELF_VALUE is NULL_TREE if looking for a static field. */
1675 tree
1676 build_field_ref (tree self_value, tree self_class, tree name)
1678 tree base_class = self_class;
1679 tree field_decl = lookup_field (&base_class, name);
1680 if (field_decl == NULL_TREE)
1682 error ("field %qs not found", IDENTIFIER_POINTER (name));
1683 return error_mark_node;
1685 if (self_value == NULL_TREE)
1687 return build_static_field_ref (field_decl);
1689 else
1691 tree base_type = promote_type (base_class);
1693 /* CHECK is true if self_value is not the this pointer. */
1694 int check = (! (DECL_P (self_value)
1695 && DECL_NAME (self_value) == this_identifier_node));
1697 /* Determine whether a field offset from NULL will lie within
1698 Page 0: this is necessary on those GNU/Linux/BSD systems that
1699 trap SEGV to generate NullPointerExceptions.
1701 We assume that Page 0 will be mapped with NOPERM, and that
1702 memory may be allocated from any other page, so only field
1703 offsets < pagesize are guaranteed to trap. We also assume
1704 the smallest page size we'll encounter is 4k bytes. */
1705 if (! flag_syntax_only && check && ! flag_check_references
1706 && ! flag_indirect_dispatch)
1708 tree field_offset = byte_position (field_decl);
1709 if (! page_size)
1710 page_size = size_int (4096);
1711 check = ! INT_CST_LT_UNSIGNED (field_offset, page_size);
1714 if (base_type != TREE_TYPE (self_value))
1715 self_value = fold_build1 (NOP_EXPR, base_type, self_value);
1716 if (! flag_syntax_only && flag_indirect_dispatch)
1718 tree otable_index
1719 = build_int_cst (NULL_TREE, get_symbol_table_index
1720 (field_decl, NULL_TREE,
1721 &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. If the rewrite results in an access to a private
2071 method, update SPECIAL.*/
2073 void
2074 maybe_rewrite_invocation (tree *method_p, tree *arg_list_p,
2075 tree *method_signature_p, tree *special)
2077 tree context = DECL_NAME (TYPE_NAME (DECL_CONTEXT (*method_p)));
2078 rewrite_rule *p;
2079 *special = NULL_TREE;
2081 for (p = rules; p->classname; p++)
2083 if (get_identifier (p->classname) == context)
2085 tree method = DECL_NAME (*method_p);
2086 if (get_identifier (p->method) == method
2087 && get_identifier (p->signature) == *method_signature_p)
2089 tree maybe_method
2090 = lookup_java_method (DECL_CONTEXT (*method_p),
2091 method,
2092 get_identifier (p->new_signature));
2093 if (! maybe_method && ! flag_verify_invocations)
2095 maybe_method
2096 = add_method (DECL_CONTEXT (*method_p), p->flags,
2097 method, get_identifier (p->new_signature));
2098 DECL_EXTERNAL (maybe_method) = 1;
2100 *method_p = maybe_method;
2101 gcc_assert (*method_p);
2102 *arg_list_p = p->rewrite_arglist (*arg_list_p);
2103 *method_signature_p = get_identifier (p->new_signature);
2104 *special = integer_one_node;
2106 break;
2114 tree
2115 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2116 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2117 tree arg_list ATTRIBUTE_UNUSED, tree special)
2119 tree func;
2120 if (is_compiled_class (self_type))
2122 /* With indirect dispatch we have to use indirect calls for all
2123 publicly visible methods or gcc will use PLT indirections
2124 to reach them. We also have to use indirect dispatch for all
2125 external methods. */
2126 if (! flag_indirect_dispatch
2127 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2129 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2130 method);
2132 else
2134 tree table_index
2135 = build_int_cst (NULL_TREE,
2136 (get_symbol_table_index
2137 (method, special,
2138 &TYPE_ATABLE_METHODS (output_class))));
2139 func
2140 = build4 (ARRAY_REF,
2141 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2142 TYPE_ATABLE_DECL (output_class), table_index,
2143 NULL_TREE, NULL_TREE);
2145 func = convert (method_ptr_type_node, func);
2147 else
2149 /* We don't know whether the method has been (statically) compiled.
2150 Compile this code to get a reference to the method's code:
2152 SELF_TYPE->methods[METHOD_INDEX].ncode
2156 int method_index = 0;
2157 tree meth, ref;
2159 /* The method might actually be declared in some superclass, so
2160 we have to use its class context, not the caller's notion of
2161 where the method is. */
2162 self_type = DECL_CONTEXT (method);
2163 ref = build_class_ref (self_type);
2164 ref = build1 (INDIRECT_REF, class_type_node, ref);
2165 if (ncode_ident == NULL_TREE)
2166 ncode_ident = get_identifier ("ncode");
2167 if (methods_ident == NULL_TREE)
2168 methods_ident = get_identifier ("methods");
2169 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2170 lookup_field (&class_type_node, methods_ident),
2171 NULL_TREE);
2172 for (meth = TYPE_METHODS (self_type);
2173 ; meth = TREE_CHAIN (meth))
2175 if (method == meth)
2176 break;
2177 if (meth == NULL_TREE)
2178 fatal_error ("method '%s' not found in class",
2179 IDENTIFIER_POINTER (DECL_NAME (method)));
2180 method_index++;
2182 method_index *= int_size_in_bytes (method_type_node);
2183 ref = fold_build2 (PLUS_EXPR, method_ptr_type_node,
2184 ref, build_int_cst (NULL_TREE, method_index));
2185 ref = build1 (INDIRECT_REF, method_type_node, ref);
2186 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2187 ref, lookup_field (&method_type_node, ncode_ident),
2188 NULL_TREE);
2190 return func;
2193 tree
2194 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2196 tree dtable, objectref;
2198 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2200 /* If we're dealing with interfaces and if the objectref
2201 argument is an array then get the dispatch table of the class
2202 Object rather than the one from the objectref. */
2203 objectref = (is_invoke_interface
2204 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2205 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2207 if (dtable_ident == NULL_TREE)
2208 dtable_ident = get_identifier ("vtable");
2209 dtable = build_java_indirect_ref (object_type_node, objectref,
2210 flag_check_references);
2211 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2212 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2214 return dtable;
2217 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2218 T. If this decl has not been seen before, it will be added to the
2219 [oa]table_methods. If it has, the existing table slot will be
2220 reused. */
2223 get_symbol_table_index (tree t, tree special, tree *symbol_table)
2225 int i = 1;
2226 tree method_list;
2228 if (*symbol_table == NULL_TREE)
2230 *symbol_table = build_tree_list (special, t);
2231 return 1;
2234 method_list = *symbol_table;
2236 while (1)
2238 tree value = TREE_VALUE (method_list);
2239 tree purpose = TREE_PURPOSE (method_list);
2240 if (value == t && purpose == special)
2241 return i;
2242 i++;
2243 if (TREE_CHAIN (method_list) == NULL_TREE)
2244 break;
2245 else
2246 method_list = TREE_CHAIN (method_list);
2249 TREE_CHAIN (method_list) = build_tree_list (special, t);
2250 return i;
2253 tree
2254 build_invokevirtual (tree dtable, tree method, tree special)
2256 tree func;
2257 tree nativecode_ptr_ptr_type_node
2258 = build_pointer_type (nativecode_ptr_type_node);
2259 tree method_index;
2260 tree otable_index;
2262 if (flag_indirect_dispatch)
2264 gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))));
2266 otable_index
2267 = build_int_cst (NULL_TREE, get_symbol_table_index
2268 (method, special,
2269 &TYPE_OTABLE_METHODS (output_class)));
2270 method_index = build4 (ARRAY_REF, integer_type_node,
2271 TYPE_OTABLE_DECL (output_class),
2272 otable_index, NULL_TREE, NULL_TREE);
2274 else
2276 /* We fetch the DECL_VINDEX field directly here, rather than
2277 using get_method_index(). DECL_VINDEX is the true offset
2278 from the vtable base to a method, regrdless of any extra
2279 words inserted at the start of the vtable. */
2280 method_index = DECL_VINDEX (method);
2281 method_index = size_binop (MULT_EXPR, method_index,
2282 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2283 if (TARGET_VTABLE_USES_DESCRIPTORS)
2284 method_index = size_binop (MULT_EXPR, method_index,
2285 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2288 func = fold_build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2289 convert (nativecode_ptr_ptr_type_node, method_index));
2291 if (TARGET_VTABLE_USES_DESCRIPTORS)
2292 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2293 else
2294 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2296 return func;
2299 static GTY(()) tree class_ident;
2300 tree
2301 build_invokeinterface (tree dtable, tree method)
2303 tree lookup_arg;
2304 tree interface;
2305 tree idx;
2307 /* We expand invokeinterface here. */
2309 if (class_ident == NULL_TREE)
2310 class_ident = get_identifier ("class");
2312 dtable = build_java_indirect_ref (dtable_type, dtable,
2313 flag_check_references);
2314 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2315 lookup_field (&dtable_type, class_ident), NULL_TREE);
2317 interface = DECL_CONTEXT (method);
2318 gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface)));
2319 layout_class_methods (interface);
2321 if (flag_indirect_dispatch)
2323 int itable_index
2324 = 2 * (get_symbol_table_index
2325 (method, NULL_TREE, &TYPE_ITABLE_METHODS (output_class)));
2326 interface
2327 = build4 (ARRAY_REF,
2328 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2329 TYPE_ITABLE_DECL (output_class),
2330 build_int_cst (NULL_TREE, itable_index-1),
2331 NULL_TREE, NULL_TREE);
2332 idx
2333 = build4 (ARRAY_REF,
2334 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2335 TYPE_ITABLE_DECL (output_class),
2336 build_int_cst (NULL_TREE, itable_index),
2337 NULL_TREE, NULL_TREE);
2338 interface = convert (class_ptr_type, interface);
2339 idx = convert (integer_type_node, idx);
2341 else
2343 idx = build_int_cst (NULL_TREE,
2344 get_interface_method_index (method, interface));
2345 interface = build_class_ref (interface);
2348 lookup_arg = tree_cons (NULL_TREE, dtable,
2349 tree_cons (NULL_TREE, interface,
2350 build_tree_list (NULL_TREE, idx)));
2352 return build3 (CALL_EXPR, ptr_type_node,
2353 build_address_of (soft_lookupinterfacemethod_node),
2354 lookup_arg, NULL_TREE);
2357 /* Expand one of the invoke_* opcodes.
2358 OPCODE is the specific opcode.
2359 METHOD_REF_INDEX is an index into the constant pool.
2360 NARGS is the number of arguments, or -1 if not specified. */
2362 static void
2363 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2365 tree method_signature
2366 = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2367 tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2368 method_ref_index);
2369 tree self_type
2370 = get_class_constant (current_jcf,
2371 COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2372 method_ref_index));
2373 const char *const self_name
2374 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2375 tree call, func, method, arg_list, method_type;
2376 tree check = NULL_TREE;
2378 tree special = NULL_TREE;
2380 if (! CLASS_LOADED_P (self_type))
2382 load_class (self_type, 1);
2383 safe_layout_class (self_type);
2384 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2385 fatal_error ("failed to find class '%s'", self_name);
2387 layout_class_methods (self_type);
2389 if (ID_INIT_P (method_name))
2390 method = lookup_java_constructor (self_type, method_signature);
2391 else
2392 method = lookup_java_method (self_type, method_name, method_signature);
2394 /* We've found a method in a class other than the one in which it
2395 was wanted. This can happen if, for instance, we're trying to
2396 compile invokespecial super.equals().
2397 FIXME: This is a kludge. Rather than nullifying the result, we
2398 should change lookup_java_method() so that it doesn't search the
2399 superclass chain when we're BC-compiling. */
2400 if (! flag_verify_invocations
2401 && method
2402 && ! TYPE_ARRAY_P (self_type)
2403 && self_type != DECL_CONTEXT (method))
2404 method = NULL_TREE;
2406 /* We've found a method in an interface, but this isn't an interface
2407 call. */
2408 if (opcode != OPCODE_invokeinterface
2409 && method
2410 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2411 method = NULL_TREE;
2413 /* We've found a non-interface method but we are making an
2414 interface call. This can happen if the interface overrides a
2415 method in Object. */
2416 if (! flag_verify_invocations
2417 && opcode == OPCODE_invokeinterface
2418 && method
2419 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2420 method = NULL_TREE;
2422 if (method == NULL_TREE)
2424 if (flag_verify_invocations || ! flag_indirect_dispatch)
2426 error ("class '%s' has no method named '%s' matching signature '%s'",
2427 self_name,
2428 IDENTIFIER_POINTER (method_name),
2429 IDENTIFIER_POINTER (method_signature));
2431 else
2433 int flags = ACC_PUBLIC;
2434 if (opcode == OPCODE_invokestatic)
2435 flags |= ACC_STATIC;
2436 if (opcode == OPCODE_invokeinterface)
2438 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2439 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2441 method = add_method (self_type, flags, method_name,
2442 method_signature);
2443 DECL_ARTIFICIAL (method) = 1;
2444 METHOD_DUMMY (method) = 1;
2445 layout_class_method (self_type, NULL,
2446 method, NULL);
2450 /* Invoke static can't invoke static/abstract method */
2451 if (method != NULL_TREE)
2453 if (opcode == OPCODE_invokestatic)
2455 if (!METHOD_STATIC (method))
2457 error ("invokestatic on non static method");
2458 method = NULL_TREE;
2460 else if (METHOD_ABSTRACT (method))
2462 error ("invokestatic on abstract method");
2463 method = NULL_TREE;
2466 else
2468 if (METHOD_STATIC (method))
2470 error ("invoke[non-static] on static method");
2471 method = NULL_TREE;
2476 if (method == NULL_TREE)
2478 /* If we got here, we emitted an error message above. So we
2479 just pop the arguments, push a properly-typed zero, and
2480 continue. */
2481 method_type = get_type_from_signature (method_signature);
2482 pop_arguments (TYPE_ARG_TYPES (method_type));
2483 if (opcode != OPCODE_invokestatic)
2484 pop_type (self_type);
2485 method_type = promote_type (TREE_TYPE (method_type));
2486 push_value (convert (method_type, integer_zero_node));
2487 return;
2490 method_type = TREE_TYPE (method);
2491 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2492 flush_quick_stack ();
2494 maybe_rewrite_invocation (&method, &arg_list, &method_signature,
2495 &special);
2497 func = NULL_TREE;
2498 if (opcode == OPCODE_invokestatic)
2499 func = build_known_method_ref (method, method_type, self_type,
2500 method_signature, arg_list, special);
2501 else if (opcode == OPCODE_invokespecial
2502 || (opcode == OPCODE_invokevirtual
2503 && (METHOD_PRIVATE (method)
2504 || METHOD_FINAL (method)
2505 || CLASS_FINAL (TYPE_NAME (self_type)))))
2507 /* If the object for the method call is null, we throw an
2508 exception. We don't do this if the object is the current
2509 method's `this'. In other cases we just rely on an
2510 optimization pass to eliminate redundant checks. FIXME:
2511 Unfortunately there doesn't seem to be a way to determine
2512 what the current method is right now.
2513 We do omit the check if we're calling <init>. */
2514 /* We use a SAVE_EXPR here to make sure we only evaluate
2515 the new `self' expression once. */
2516 tree save_arg = save_expr (TREE_VALUE (arg_list));
2517 TREE_VALUE (arg_list) = save_arg;
2518 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2519 func = build_known_method_ref (method, method_type, self_type,
2520 method_signature, arg_list, special);
2522 else
2524 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2525 arg_list);
2526 if (opcode == OPCODE_invokevirtual)
2527 func = build_invokevirtual (dtable, method, special);
2528 else
2529 func = build_invokeinterface (dtable, method);
2532 if (TREE_CODE (func) == ADDR_EXPR)
2533 TREE_TYPE (func) = build_pointer_type (method_type);
2534 else
2535 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2537 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2538 func, arg_list, NULL_TREE);
2539 TREE_SIDE_EFFECTS (call) = 1;
2540 call = check_for_builtin (method, call);
2542 if (check != NULL_TREE)
2544 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2545 TREE_SIDE_EFFECTS (call) = 1;
2548 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2549 java_add_stmt (call);
2550 else
2552 push_value (call);
2553 flush_quick_stack ();
2557 /* Create a stub which will be put into the vtable but which will call
2558 a JNI function. */
2560 tree
2561 build_jni_stub (tree method)
2563 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2564 tree jni_func_type, tem;
2565 tree env_var, res_var = NULL_TREE, block;
2566 tree method_args, res_type;
2567 tree meth_var;
2568 tree bind;
2570 int args_size = 0;
2572 tree klass = DECL_CONTEXT (method);
2573 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2574 klass = build_class_ref (klass);
2576 gcc_assert (METHOD_NATIVE (method) && flag_jni);
2578 DECL_ARTIFICIAL (method) = 1;
2579 DECL_EXTERNAL (method) = 0;
2581 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2582 DECL_CONTEXT (env_var) = method;
2584 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2586 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2587 TREE_TYPE (TREE_TYPE (method)));
2588 DECL_CONTEXT (res_var) = method;
2589 TREE_CHAIN (env_var) = res_var;
2592 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2593 TREE_STATIC (meth_var) = 1;
2594 TREE_PUBLIC (meth_var) = 0;
2595 DECL_EXTERNAL (meth_var) = 0;
2596 DECL_CONTEXT (meth_var) = method;
2597 DECL_ARTIFICIAL (meth_var) = 1;
2598 DECL_INITIAL (meth_var) = null_pointer_node;
2599 TREE_USED (meth_var) = 1;
2600 chainon (env_var, meth_var);
2601 build_result_decl (method);
2603 /* One strange way that the front ends are different is that they
2604 store arguments differently. */
2605 if (from_class)
2606 method_args = DECL_ARGUMENTS (method);
2607 else
2608 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2609 block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
2610 TREE_SIDE_EFFECTS (block) = 1;
2611 /* When compiling from source we don't set the type of the block,
2612 because that will prevent patch_return from ever being run. */
2613 if (from_class)
2614 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2616 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2617 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2618 build3 (CALL_EXPR, ptr_type_node,
2619 build_address_of (soft_getjnienvnewframe_node),
2620 build_tree_list (NULL_TREE, klass),
2621 NULL_TREE));
2622 CAN_COMPLETE_NORMALLY (body) = 1;
2624 /* All the arguments to this method become arguments to the
2625 underlying JNI function. If we had to wrap object arguments in a
2626 special way, we would do that here. */
2627 args = NULL_TREE;
2628 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2630 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2631 #ifdef PARM_BOUNDARY
2632 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2633 * PARM_BOUNDARY);
2634 #endif
2635 args_size += (arg_bits / BITS_PER_UNIT);
2637 args = tree_cons (NULL_TREE, tem, args);
2639 args = nreverse (args);
2640 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2642 /* For a static method the second argument is the class. For a
2643 non-static method the second argument is `this'; that is already
2644 available in the argument list. */
2645 if (METHOD_STATIC (method))
2647 args_size += int_size_in_bytes (TREE_TYPE (klass));
2648 args = tree_cons (NULL_TREE, klass, args);
2649 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2652 /* The JNIEnv structure is the first argument to the JNI function. */
2653 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2654 args = tree_cons (NULL_TREE, env_var, args);
2655 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2657 /* We call _Jv_LookupJNIMethod to find the actual underlying
2658 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2659 exception if this function is not found at runtime. */
2660 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2661 method_sig = build_java_signature (TREE_TYPE (method));
2662 lookup_arg = tree_cons (NULL_TREE,
2663 build_utf8_ref (unmangle_classname
2664 (IDENTIFIER_POINTER (method_sig),
2665 IDENTIFIER_LENGTH (method_sig))),
2666 tem);
2667 tem = DECL_NAME (method);
2668 lookup_arg
2669 = tree_cons (NULL_TREE, klass,
2670 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2672 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2674 #ifdef MODIFY_JNI_METHOD_CALL
2675 tem = MODIFY_JNI_METHOD_CALL (tem);
2676 #endif
2678 jni_func_type = build_pointer_type (tem);
2680 jnifunc = build3 (COND_EXPR, ptr_type_node,
2681 meth_var, meth_var,
2682 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2683 build3 (CALL_EXPR, ptr_type_node,
2684 build_address_of
2685 (soft_lookupjnimethod_node),
2686 lookup_arg, NULL_TREE)));
2688 /* Now we make the actual JNI call via the resulting function
2689 pointer. */
2690 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2691 build1 (NOP_EXPR, jni_func_type, jnifunc),
2692 args, NULL_TREE);
2694 /* If the JNI call returned a result, capture it here. If we had to
2695 unwrap JNI object results, we would do that here. */
2696 if (res_var != NULL_TREE)
2698 /* If the call returns an object, it may return a JNI weak
2699 reference, in which case we must unwrap it. */
2700 if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
2701 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2702 build_address_of (soft_unwrapjni_node),
2703 build_tree_list (NULL_TREE, call),
2704 NULL_TREE);
2705 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2706 res_var, call);
2709 TREE_SIDE_EFFECTS (call) = 1;
2710 CAN_COMPLETE_NORMALLY (call) = 1;
2712 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2713 TREE_SIDE_EFFECTS (body) = 1;
2715 /* Now free the environment we allocated. */
2716 call = build3 (CALL_EXPR, ptr_type_node,
2717 build_address_of (soft_jnipopsystemframe_node),
2718 build_tree_list (NULL_TREE, env_var),
2719 NULL_TREE);
2720 TREE_SIDE_EFFECTS (call) = 1;
2721 CAN_COMPLETE_NORMALLY (call) = 1;
2722 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2723 TREE_SIDE_EFFECTS (body) = 1;
2725 /* Finally, do the return. */
2726 res_type = void_type_node;
2727 if (res_var != NULL_TREE)
2729 tree drt;
2730 gcc_assert (DECL_RESULT (method));
2731 /* Make sure we copy the result variable to the actual
2732 result. We use the type of the DECL_RESULT because it
2733 might be different from the return type of the function:
2734 it might be promoted. */
2735 drt = TREE_TYPE (DECL_RESULT (method));
2736 if (drt != TREE_TYPE (res_var))
2737 res_var = build1 (CONVERT_EXPR, drt, res_var);
2738 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2739 TREE_SIDE_EFFECTS (res_var) = 1;
2742 body = build2 (COMPOUND_EXPR, void_type_node, body,
2743 build1 (RETURN_EXPR, res_type, res_var));
2744 TREE_SIDE_EFFECTS (body) = 1;
2746 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2747 body, block);
2748 return bind;
2752 /* Given lvalue EXP, return a volatile expression that references the
2753 same object. */
2755 tree
2756 java_modify_addr_for_volatile (tree exp)
2758 tree exp_type = TREE_TYPE (exp);
2759 tree v_type
2760 = build_qualified_type (exp_type,
2761 TYPE_QUALS (exp_type) | TYPE_QUAL_VOLATILE);
2762 tree addr = build_fold_addr_expr (exp);
2763 v_type = build_pointer_type (v_type);
2764 addr = fold_convert (v_type, addr);
2765 exp = build_fold_indirect_ref (addr);
2766 return exp;
2770 /* Expand an operation to extract from or store into a field.
2771 IS_STATIC is 1 iff the field is static.
2772 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2773 FIELD_REF_INDEX is an index into the constant pool. */
2775 static void
2776 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2778 tree self_type
2779 = get_class_constant (current_jcf,
2780 COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2781 field_ref_index));
2782 const char *self_name
2783 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2784 tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2785 tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
2786 field_ref_index);
2787 tree field_type = get_type_from_signature (field_signature);
2788 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2789 tree field_ref;
2790 int is_error = 0;
2791 tree original_self_type = self_type;
2792 tree field_decl;
2793 tree modify_expr;
2795 if (! CLASS_LOADED_P (self_type))
2796 load_class (self_type, 1);
2797 field_decl = lookup_field (&self_type, field_name);
2798 if (field_decl == error_mark_node)
2800 is_error = 1;
2802 else if (field_decl == NULL_TREE)
2804 if (! flag_verify_invocations)
2806 int flags = ACC_PUBLIC;
2807 if (is_static)
2808 flags |= ACC_STATIC;
2809 self_type = original_self_type;
2810 field_decl = add_field (original_self_type, field_name,
2811 field_type, flags);
2812 DECL_ARTIFICIAL (field_decl) = 1;
2813 DECL_IGNORED_P (field_decl) = 1;
2814 #if 0
2815 /* FIXME: We should be pessimistic about volatility. We
2816 don't know one way or another, but this is safe.
2817 However, doing this has bad effects on code quality. We
2818 need to look at better ways to do this. */
2819 TREE_THIS_VOLATILE (field_decl) = 1;
2820 #endif
2822 else
2824 error ("missing field '%s' in '%s'",
2825 IDENTIFIER_POINTER (field_name), self_name);
2826 is_error = 1;
2829 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2831 error ("mismatching signature for field '%s' in '%s'",
2832 IDENTIFIER_POINTER (field_name), self_name);
2833 is_error = 1;
2835 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2836 if (is_error)
2838 if (! is_putting)
2839 push_value (convert (field_type, integer_zero_node));
2840 flush_quick_stack ();
2841 return;
2844 field_ref = build_field_ref (field_ref, self_type, field_name);
2845 if (is_static
2846 && ! flag_indirect_dispatch)
2848 tree context = DECL_CONTEXT (field_ref);
2849 if (context != self_type && CLASS_INTERFACE (TYPE_NAME (context)))
2850 field_ref = build_class_init (context, field_ref);
2851 field_ref = build_class_init (self_type, field_ref);
2853 if (is_putting)
2855 flush_quick_stack ();
2856 if (FIELD_FINAL (field_decl))
2858 if (DECL_CONTEXT (field_decl) != current_class)
2859 error ("assignment to final field %q+D not in field's class",
2860 field_decl);
2861 /* We used to check for assignments to final fields not
2862 occurring in the class initializer or in a constructor
2863 here. However, this constraint doesn't seem to be
2864 enforced by the JVM. */
2867 if (TREE_THIS_VOLATILE (field_decl))
2868 field_ref = java_modify_addr_for_volatile (field_ref);
2870 modify_expr = build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2871 field_ref, new_value);
2873 if (TREE_THIS_VOLATILE (field_decl))
2874 java_add_stmt
2875 (build3
2876 (CALL_EXPR, void_type_node,
2877 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
2878 NULL_TREE, NULL_TREE));
2880 java_add_stmt (modify_expr);
2882 else
2884 tree temp = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (field_ref));
2885 java_add_local_var (temp);
2887 if (TREE_THIS_VOLATILE (field_decl))
2888 field_ref = java_modify_addr_for_volatile (field_ref);
2890 modify_expr
2891 = build2 (MODIFY_EXPR, TREE_TYPE (field_ref), temp, field_ref);
2892 java_add_stmt (modify_expr);
2894 if (TREE_THIS_VOLATILE (field_decl))
2895 java_add_stmt
2896 (build3
2897 (CALL_EXPR, void_type_node,
2898 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
2899 NULL_TREE, NULL_TREE));
2901 push_value (temp);
2903 TREE_THIS_VOLATILE (field_ref) = TREE_THIS_VOLATILE (field_decl);
2906 void
2907 load_type_state (tree label)
2909 int i;
2910 tree vec = LABEL_TYPE_STATE (label);
2911 int cur_length = TREE_VEC_LENGTH (vec);
2912 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2913 for (i = 0; i < cur_length; i++)
2914 type_map [i] = TREE_VEC_ELT (vec, i);
2917 /* Go over METHOD's bytecode and note instruction starts in
2918 instruction_bits[]. */
2920 void
2921 note_instructions (JCF *jcf, tree method)
2923 int PC;
2924 unsigned char* byte_ops;
2925 long length = DECL_CODE_LENGTH (method);
2927 int saw_index;
2928 jint INT_temp;
2930 #undef RET /* Defined by config/i386/i386.h */
2931 #undef PTR
2932 #define BCODE byte_ops
2933 #define BYTE_type_node byte_type_node
2934 #define SHORT_type_node short_type_node
2935 #define INT_type_node int_type_node
2936 #define LONG_type_node long_type_node
2937 #define CHAR_type_node char_type_node
2938 #define PTR_type_node ptr_type_node
2939 #define FLOAT_type_node float_type_node
2940 #define DOUBLE_type_node double_type_node
2941 #define VOID_type_node void_type_node
2942 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2943 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2944 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2945 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2947 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2949 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2950 byte_ops = jcf->read_ptr;
2951 instruction_bits = xrealloc (instruction_bits, length + 1);
2952 memset (instruction_bits, 0, length + 1);
2954 /* This pass figures out which PC can be the targets of jumps. */
2955 for (PC = 0; PC < length;)
2957 int oldpc = PC; /* PC at instruction start. */
2958 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2959 switch (byte_ops[PC++])
2961 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2962 case OPCODE: \
2963 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2964 break;
2966 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2968 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2969 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2970 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2971 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2972 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2973 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2974 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2975 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2977 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2978 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2979 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2980 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2981 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2982 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2983 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2984 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2986 /* two forms of wide instructions */
2987 #define PRE_SPECIAL_WIDE(IGNORE) \
2989 int modified_opcode = IMMEDIATE_u1; \
2990 if (modified_opcode == OPCODE_iinc) \
2992 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2993 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2995 else \
2997 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
3001 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
3003 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3005 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
3006 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
3007 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
3008 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
3009 #define PRE_ARRAY_STORE(TYPE) /* nothing */
3010 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
3011 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
3012 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
3013 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
3014 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
3016 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
3017 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
3018 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3019 saw_index = 0; INT_temp = (OPERAND_VALUE); \
3020 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
3021 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
3022 saw_index = 0; INT_temp = (OPERAND_VALUE); \
3023 NOTE_LABEL (PC); \
3024 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
3026 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
3028 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3029 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
3031 #define PRE_LOOKUP_SWITCH \
3032 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3033 NOTE_LABEL (default_offset+oldpc); \
3034 if (npairs >= 0) \
3035 while (--npairs >= 0) { \
3036 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
3037 jint offset = IMMEDIATE_s4; \
3038 NOTE_LABEL (offset+oldpc); } \
3041 #define PRE_TABLE_SWITCH \
3042 { jint default_offset = IMMEDIATE_s4; \
3043 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3044 NOTE_LABEL (default_offset+oldpc); \
3045 if (low <= high) \
3046 while (low++ <= high) { \
3047 jint offset = IMMEDIATE_s4; \
3048 NOTE_LABEL (offset+oldpc); } \
3051 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
3052 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
3053 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3054 (void)(IMMEDIATE_u2); \
3055 PC += 2 * IS_INTERFACE /* for invokeinterface */;
3057 #include "javaop.def"
3058 #undef JAVAOP
3060 } /* for */
3063 void
3064 expand_byte_code (JCF *jcf, tree method)
3066 int PC;
3067 int i;
3068 const unsigned char *linenumber_pointer;
3069 int dead_code_index = -1;
3070 unsigned char* byte_ops;
3071 long length = DECL_CODE_LENGTH (method);
3073 stack_pointer = 0;
3074 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
3075 byte_ops = jcf->read_ptr;
3077 /* We make an initial pass of the line number table, to note
3078 which instructions have associated line number entries. */
3079 linenumber_pointer = linenumber_table;
3080 for (i = 0; i < linenumber_count; i++)
3082 int pc = GET_u2 (linenumber_pointer);
3083 linenumber_pointer += 4;
3084 if (pc >= length)
3085 warning (0, "invalid PC in line number table");
3086 else
3088 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
3089 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
3090 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
3094 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
3095 return;
3097 promote_arguments ();
3099 /* Translate bytecodes. */
3100 linenumber_pointer = linenumber_table;
3101 for (PC = 0; PC < length;)
3103 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
3105 tree label = lookup_label (PC);
3106 flush_quick_stack ();
3107 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
3108 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
3109 if (LABEL_VERIFIED (label) || PC == 0)
3110 load_type_state (label);
3113 if (! (instruction_bits [PC] & BCODE_VERIFIED))
3115 if (dead_code_index == -1)
3117 /* This is the start of a region of unreachable bytecodes.
3118 They still need to be processed in order for EH ranges
3119 to get handled correctly. However, we can simply
3120 replace these bytecodes with nops. */
3121 dead_code_index = PC;
3124 /* Turn this bytecode into a nop. */
3125 byte_ops[PC] = 0x0;
3127 else
3129 if (dead_code_index != -1)
3131 /* We've just reached the end of a region of dead code. */
3132 if (extra_warnings)
3133 warning (0, "unreachable bytecode from %d to before %d",
3134 dead_code_index, PC);
3135 dead_code_index = -1;
3139 /* Handle possible line number entry for this PC.
3141 This code handles out-of-order and multiple linenumbers per PC,
3142 but is optimized for the case of line numbers increasing
3143 monotonically with PC. */
3144 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
3146 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
3147 || GET_u2 (linenumber_pointer) != PC)
3148 linenumber_pointer = linenumber_table;
3149 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
3151 int pc = GET_u2 (linenumber_pointer);
3152 linenumber_pointer += 4;
3153 if (pc == PC)
3155 int line = GET_u2 (linenumber_pointer - 2);
3156 #ifdef USE_MAPPED_LOCATION
3157 input_location = linemap_line_start (&line_table, line, 1);
3158 #else
3159 input_location.line = line;
3160 #endif
3161 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
3162 break;
3166 maybe_pushlevels (PC);
3167 PC = process_jvm_instruction (PC, byte_ops, length);
3168 maybe_poplevels (PC);
3169 } /* for */
3171 if (dead_code_index != -1)
3173 /* We've just reached the end of a region of dead code. */
3174 if (extra_warnings)
3175 warning (0, "unreachable bytecode from %d to the end of the method",
3176 dead_code_index);
3180 static void
3181 java_push_constant_from_pool (JCF *jcf, int index)
3183 tree c;
3184 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3186 tree name;
3187 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3188 index = alloc_name_constant (CONSTANT_String, name);
3189 c = build_ref_from_constant_pool (index);
3190 c = convert (promote_type (string_type_node), c);
3192 else if (JPOOL_TAG (jcf, index) == CONSTANT_Class
3193 || JPOOL_TAG (jcf, index) == CONSTANT_ResolvedClass)
3195 tree record = get_class_constant (jcf, index);
3196 c = build_class_ref (record);
3198 else
3199 c = get_constant (jcf, index);
3200 push_value (c);
3204 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3205 long length ATTRIBUTE_UNUSED)
3207 const char *opname; /* Temporary ??? */
3208 int oldpc = PC; /* PC at instruction start. */
3210 /* If the instruction is at the beginning of an exception handler,
3211 replace the top of the stack with the thrown object reference. */
3212 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3214 /* Note that the verifier will not emit a type map at all for
3215 dead exception handlers. In this case we just ignore the
3216 situation. */
3217 if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
3219 tree type = pop_type (promote_type (throwable_type_node));
3220 push_value (build_exception_object_ref (type));
3224 switch (byte_ops[PC++])
3226 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3227 case OPCODE: \
3228 opname = #OPNAME; \
3229 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3230 break;
3232 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3234 int saw_index = 0; \
3235 int index = OPERAND_VALUE; \
3236 build_java_ret \
3237 (find_local_variable (index, return_address_type_node, oldpc)); \
3240 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3242 /* OPERAND_VALUE may have side-effects on PC */ \
3243 int opvalue = OPERAND_VALUE; \
3244 build_java_jsr (oldpc + opvalue, PC); \
3247 /* Push a constant onto the stack. */
3248 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3249 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3250 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3251 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3253 /* internal macro added for use by the WIDE case */
3254 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3255 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3257 /* Push local variable onto the opcode stack. */
3258 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3260 /* have to do this since OPERAND_VALUE may have side-effects */ \
3261 int opvalue = OPERAND_VALUE; \
3262 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3265 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3266 expand_java_return (OPERAND_TYPE##_type_node)
3268 #define REM_EXPR TRUNC_MOD_EXPR
3269 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3270 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3272 #define FIELD(IS_STATIC, IS_PUT) \
3273 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3275 #define TEST(OPERAND_TYPE, CONDITION) \
3276 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3278 #define COND(OPERAND_TYPE, CONDITION) \
3279 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3281 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3282 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3284 #define BRANCH_GOTO(OPERAND_VALUE) \
3285 expand_java_goto (oldpc + OPERAND_VALUE)
3287 #define BRANCH_CALL(OPERAND_VALUE) \
3288 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3290 #if 0
3291 #define BRANCH_RETURN(OPERAND_VALUE) \
3293 tree type = OPERAND_TYPE##_type_node; \
3294 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3295 expand_java_ret (value); \
3297 #endif
3299 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3300 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3301 fprintf (stderr, "(not implemented)\n")
3302 #define NOT_IMPL1(OPERAND_VALUE) \
3303 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3304 fprintf (stderr, "(not implemented)\n")
3306 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3308 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3310 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3312 #define STACK_SWAP(COUNT) java_stack_swap()
3314 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3315 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3316 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3318 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3319 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3321 #define LOOKUP_SWITCH \
3322 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3323 tree selector = pop_value (INT_type_node); \
3324 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3325 while (--npairs >= 0) \
3327 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3328 expand_java_add_case (switch_expr, match, oldpc + offset); \
3332 #define TABLE_SWITCH \
3333 { jint default_offset = IMMEDIATE_s4; \
3334 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3335 tree selector = pop_value (INT_type_node); \
3336 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3337 for (; low <= high; low++) \
3339 jint offset = IMMEDIATE_s4; \
3340 expand_java_add_case (switch_expr, low, oldpc + offset); \
3344 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3345 { int opcode = byte_ops[PC-1]; \
3346 int method_ref_index = IMMEDIATE_u2; \
3347 int nargs; \
3348 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3349 else nargs = -1; \
3350 expand_invoke (opcode, method_ref_index, nargs); \
3353 /* Handle new, checkcast, instanceof */
3354 #define OBJECT(TYPE, OP) \
3355 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3357 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3359 #define ARRAY_LOAD(OPERAND_TYPE) \
3361 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3364 #define ARRAY_STORE(OPERAND_TYPE) \
3366 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3369 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3370 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3371 #define ARRAY_NEW_PTR() \
3372 push_value (build_anewarray (get_class_constant (current_jcf, \
3373 IMMEDIATE_u2), \
3374 pop_value (int_type_node)));
3375 #define ARRAY_NEW_NUM() \
3377 int atype = IMMEDIATE_u1; \
3378 push_value (build_newarray (atype, pop_value (int_type_node)));\
3380 #define ARRAY_NEW_MULTI() \
3382 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3383 int ndims = IMMEDIATE_u1; \
3384 expand_java_multianewarray( class, ndims ); \
3387 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3388 push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3389 pop_value (OPERAND_TYPE##_type_node)));
3391 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3393 push_value (build1 (NOP_EXPR, int_type_node, \
3394 (convert (TO_TYPE##_type_node, \
3395 pop_value (FROM_TYPE##_type_node))))); \
3398 #define CONVERT(FROM_TYPE, TO_TYPE) \
3400 push_value (convert (TO_TYPE##_type_node, \
3401 pop_value (FROM_TYPE##_type_node))); \
3404 /* internal macro added for use by the WIDE case
3405 Added TREE_TYPE (decl) assignment, apbianco */
3406 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3408 tree decl, value; \
3409 int index = OPVALUE; \
3410 tree type = OPTYPE; \
3411 value = pop_value (type); \
3412 type = TREE_TYPE (value); \
3413 decl = find_local_variable (index, type, oldpc); \
3414 set_local_type (index, type); \
3415 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3418 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3420 /* have to do this since OPERAND_VALUE may have side-effects */ \
3421 int opvalue = OPERAND_VALUE; \
3422 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3425 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3426 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3428 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3429 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3431 #define MONITOR_OPERATION(call) \
3433 tree o = pop_value (ptr_type_node); \
3434 tree c; \
3435 flush_quick_stack (); \
3436 c = build_java_monitor (call, o); \
3437 TREE_SIDE_EFFECTS (c) = 1; \
3438 java_add_stmt (c); \
3441 #define SPECIAL_IINC(IGNORED) \
3443 unsigned int local_var_index = IMMEDIATE_u1; \
3444 int ival = IMMEDIATE_s1; \
3445 expand_iinc(local_var_index, ival, oldpc); \
3448 #define SPECIAL_WIDE(IGNORED) \
3450 int modified_opcode = IMMEDIATE_u1; \
3451 unsigned int local_var_index = IMMEDIATE_u2; \
3452 switch (modified_opcode) \
3454 case OPCODE_iinc: \
3456 int ival = IMMEDIATE_s2; \
3457 expand_iinc (local_var_index, ival, oldpc); \
3458 break; \
3460 case OPCODE_iload: \
3461 case OPCODE_lload: \
3462 case OPCODE_fload: \
3463 case OPCODE_dload: \
3464 case OPCODE_aload: \
3466 /* duplicate code from LOAD macro */ \
3467 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3468 break; \
3470 case OPCODE_istore: \
3471 case OPCODE_lstore: \
3472 case OPCODE_fstore: \
3473 case OPCODE_dstore: \
3474 case OPCODE_astore: \
3476 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3477 break; \
3479 default: \
3480 error ("unrecogized wide sub-instruction"); \
3484 #define SPECIAL_THROW(IGNORED) \
3485 build_java_athrow (pop_value (throwable_type_node))
3487 #define SPECIAL_BREAK NOT_IMPL1
3488 #define IMPL NOT_IMPL
3490 #include "javaop.def"
3491 #undef JAVAOP
3492 default:
3493 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3495 return PC;
3498 /* Return the opcode at PC in the code section pointed to by
3499 CODE_OFFSET. */
3501 static unsigned char
3502 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3504 unsigned char opcode;
3505 long absolute_offset = (long)JCF_TELL (jcf);
3507 JCF_SEEK (jcf, code_offset);
3508 opcode = jcf->read_ptr [pc];
3509 JCF_SEEK (jcf, absolute_offset);
3510 return opcode;
3513 /* Some bytecode compilers are emitting accurate LocalVariableTable
3514 attributes. Here's an example:
3516 PC <t>store_<n>
3517 PC+1 ...
3519 Attribute "LocalVariableTable"
3520 slot #<n>: ... (PC: PC+1 length: L)
3522 This is accurate because the local in slot <n> really exists after
3523 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3525 This procedure recognizes this situation and extends the live range
3526 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3527 length of the store instruction.)
3529 This function is used by `give_name_to_locals' so that a local's
3530 DECL features a DECL_LOCAL_START_PC such that the first related
3531 store operation will use DECL as a destination, not an unrelated
3532 temporary created for the occasion.
3534 This function uses a global (instruction_bits) `note_instructions' should
3535 have allocated and filled properly. */
3538 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3539 int start_pc, int slot)
3541 int first, index, opcode;
3542 int pc, insn_pc;
3543 int wide_found = 0;
3545 if (!start_pc)
3546 return start_pc;
3548 first = index = -1;
3550 /* Find last previous instruction and remember it */
3551 for (pc = start_pc-1; pc; pc--)
3552 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3553 break;
3554 insn_pc = pc;
3556 /* Retrieve the instruction, handle `wide'. */
3557 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3558 if (opcode == OPCODE_wide)
3560 wide_found = 1;
3561 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3564 switch (opcode)
3566 case OPCODE_astore_0:
3567 case OPCODE_astore_1:
3568 case OPCODE_astore_2:
3569 case OPCODE_astore_3:
3570 first = OPCODE_astore_0;
3571 break;
3573 case OPCODE_istore_0:
3574 case OPCODE_istore_1:
3575 case OPCODE_istore_2:
3576 case OPCODE_istore_3:
3577 first = OPCODE_istore_0;
3578 break;
3580 case OPCODE_lstore_0:
3581 case OPCODE_lstore_1:
3582 case OPCODE_lstore_2:
3583 case OPCODE_lstore_3:
3584 first = OPCODE_lstore_0;
3585 break;
3587 case OPCODE_fstore_0:
3588 case OPCODE_fstore_1:
3589 case OPCODE_fstore_2:
3590 case OPCODE_fstore_3:
3591 first = OPCODE_fstore_0;
3592 break;
3594 case OPCODE_dstore_0:
3595 case OPCODE_dstore_1:
3596 case OPCODE_dstore_2:
3597 case OPCODE_dstore_3:
3598 first = OPCODE_dstore_0;
3599 break;
3601 case OPCODE_astore:
3602 case OPCODE_istore:
3603 case OPCODE_lstore:
3604 case OPCODE_fstore:
3605 case OPCODE_dstore:
3606 index = peek_opcode_at_pc (jcf, code_offset, pc);
3607 if (wide_found)
3609 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3610 index = (other << 8) + index;
3612 break;
3615 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3616 means we have a <t>store. */
3617 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3618 start_pc = insn_pc;
3620 return start_pc;
3623 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3624 order, as specified by Java Language Specification.
3626 The problem is that while expand_expr will evaluate its sub-operands in
3627 left-to-right order, for variables it will just return an rtx (i.e.
3628 an lvalue) for the variable (rather than an rvalue). So it is possible
3629 that a later sub-operand will change the register, and when the
3630 actual operation is done, it will use the new value, when it should
3631 have used the original value.
3633 We fix this by using save_expr. This forces the sub-operand to be
3634 copied into a fresh virtual register,
3636 For method invocation, we modify the arguments so that a
3637 left-to-right order evaluation is performed. Saved expressions
3638 will, in CALL_EXPR order, be reused when the call will be expanded.
3640 We also promote outgoing args if needed. */
3642 tree
3643 force_evaluation_order (tree node)
3645 if (flag_syntax_only)
3646 return node;
3647 if (TREE_CODE (node) == CALL_EXPR
3648 || TREE_CODE (node) == NEW_CLASS_EXPR
3649 || (TREE_CODE (node) == COMPOUND_EXPR
3650 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3651 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3653 tree arg, cmp;
3655 arg = node;
3657 /* Position arg properly, account for wrapped around ctors. */
3658 if (TREE_CODE (node) == COMPOUND_EXPR)
3659 arg = TREE_OPERAND (node, 0);
3661 arg = TREE_OPERAND (arg, 1);
3663 /* An empty argument list is ok, just ignore it. */
3664 if (!arg)
3665 return node;
3667 /* Not having a list of arguments here is an error. */
3668 gcc_assert (TREE_CODE (arg) == TREE_LIST);
3670 /* This reverses the evaluation order. This is a desired effect. */
3671 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3673 /* Promote types smaller than integer. This is required by
3674 some ABIs. */
3675 tree type = TREE_TYPE (TREE_VALUE (arg));
3676 tree saved;
3677 if (targetm.calls.promote_prototypes (type)
3678 && INTEGRAL_TYPE_P (type)
3679 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
3680 TYPE_SIZE (integer_type_node)))
3681 TREE_VALUE (arg) = fold_convert (integer_type_node, TREE_VALUE (arg));
3683 saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3684 cmp = (cmp == NULL_TREE ? saved :
3685 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3686 TREE_VALUE (arg) = saved;
3689 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3690 TREE_SIDE_EFFECTS (cmp) = 1;
3692 if (cmp)
3694 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3695 if (TREE_TYPE (cmp) != void_type_node)
3696 cmp = save_expr (cmp);
3697 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3698 TREE_SIDE_EFFECTS (cmp) = 1;
3699 node = cmp;
3702 return node;
3705 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3706 location where an expression or an identifier were encountered. It
3707 is necessary for languages where the frontend parser will handle
3708 recursively more than one file (Java is one of them). */
3710 tree
3711 build_expr_wfl (tree node,
3712 #ifdef USE_MAPPED_LOCATION
3713 source_location location
3714 #else
3715 const char *file, int line, int col
3716 #endif
3719 tree wfl;
3721 #ifdef USE_MAPPED_LOCATION
3722 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3723 SET_EXPR_LOCATION (wfl, location);
3724 #else
3725 static const char *last_file = 0;
3726 static tree last_filenode = NULL_TREE;
3728 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3730 EXPR_WFL_SET_LINECOL (wfl, line, col);
3731 if (file != last_file)
3733 last_file = file;
3734 last_filenode = file ? get_identifier (file) : NULL_TREE;
3736 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3737 #endif
3738 EXPR_WFL_NODE (wfl) = node;
3739 if (node)
3741 if (!TYPE_P (node))
3742 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3743 TREE_TYPE (wfl) = TREE_TYPE (node);
3746 return wfl;
3749 #ifdef USE_MAPPED_LOCATION
3750 tree
3751 expr_add_location (tree node, source_location location, bool statement)
3753 tree wfl;
3754 #if 0
3755 /* FIXME. This optimization causes failures in code that expects an
3756 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3757 if (node && ! (statement && flag_emit_class_files))
3759 source_location node_loc = EXPR_LOCATION (node);
3760 if (node_loc == location || location == UNKNOWN_LOCATION)
3761 return node;
3762 if (node_loc == UNKNOWN_LOCATION
3763 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3765 SET_EXPR_LOCATION (node, location);
3766 return node;
3769 #endif
3770 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3771 SET_EXPR_LOCATION (wfl, location);
3772 EXPR_WFL_NODE (wfl) = node;
3773 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3774 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3775 if (node)
3777 if (!TYPE_P (node))
3778 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3779 TREE_TYPE (wfl) = TREE_TYPE (node);
3782 return wfl;
3784 #endif
3786 /* Build a node to represent empty statements and blocks. */
3788 tree
3789 build_java_empty_stmt (void)
3791 tree t = build_empty_stmt ();
3792 CAN_COMPLETE_NORMALLY (t) = 1;
3793 return t;
3796 /* Promote all args of integral type before generating any code. */
3798 static void
3799 promote_arguments (void)
3801 int i;
3802 tree arg;
3803 for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
3804 arg != NULL_TREE; arg = TREE_CHAIN (arg), i++)
3806 tree arg_type = TREE_TYPE (arg);
3807 if (INTEGRAL_TYPE_P (arg_type)
3808 && TYPE_PRECISION (arg_type) < 32)
3810 tree copy = find_local_variable (i, integer_type_node, -1);
3811 java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
3812 copy,
3813 fold_convert (integer_type_node, arg)));
3815 if (TYPE_IS_WIDE (arg_type))
3816 i++;
3820 #include "gt-java-expr.h"