2005-01-22 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / java / expr.c
blob626b9bcadd262b44bf659910270a0926b7b109fa
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
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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);
90 static GTY(()) tree operand_type[59];
92 static GTY(()) tree methods_ident;
93 static GTY(()) tree ncode_ident;
94 tree dtable_ident = NULL_TREE;
96 /* Set to nonzero value in order to emit class initialization code
97 before static field references. */
98 int always_initialize_class_p = 0;
100 /* We store the stack state in two places:
101 Within a basic block, we use the quick_stack, which is a
102 pushdown list (TREE_LISTs) of expression nodes.
103 This is the top part of the stack; below that we use find_stack_slot.
104 At the end of a basic block, the quick_stack must be flushed
105 to the stack slot array (as handled by find_stack_slot).
106 Using quick_stack generates better code (especially when
107 compiled without optimization), because we do not have to
108 explicitly store and load trees to temporary variables.
110 If a variable is on the quick stack, it means the value of variable
111 when the quick stack was last flushed. Conceptually, flush_quick_stack
112 saves all the quick_stack elements in parallel. However, that is
113 complicated, so it actually saves them (i.e. copies each stack value
114 to is home virtual register) from low indexes. This allows a quick_stack
115 element at index i (counting from the bottom of stack the) to references
116 slot virtuals for register that are >= i, but not those that are deeper.
117 This convention makes most operations easier. For example iadd works
118 even when the stack contains (reg[0], reg[1]): It results in the
119 stack containing (reg[0]+reg[1]), which is OK. However, some stack
120 operations are more complicated. For example dup given a stack
121 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
122 the convention, since stack value 1 would refer to a register with
123 lower index (reg[0]), which flush_quick_stack does not safely handle.
124 So dup cannot just add an extra element to the quick_stack, but iadd can.
127 static GTY(()) tree quick_stack;
129 /* A free-list of unused permanent TREE_LIST nodes. */
130 static GTY((deletable)) tree tree_list_free_list;
132 /* The stack pointer of the Java virtual machine.
133 This does include the size of the quick_stack. */
135 int stack_pointer;
137 const unsigned char *linenumber_table;
138 int linenumber_count;
140 void
141 init_expr_processing (void)
143 operand_type[21] = operand_type[54] = int_type_node;
144 operand_type[22] = operand_type[55] = long_type_node;
145 operand_type[23] = operand_type[56] = float_type_node;
146 operand_type[24] = operand_type[57] = double_type_node;
147 operand_type[25] = operand_type[58] = ptr_type_node;
150 tree
151 java_truthvalue_conversion (tree expr)
153 /* It is simpler and generates better code to have only TRUTH_*_EXPR
154 or comparison expressions as truth values at this level.
156 This function should normally be identity for Java. */
158 switch (TREE_CODE (expr))
160 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
161 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
162 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
163 case ORDERED_EXPR: case UNORDERED_EXPR:
164 case TRUTH_ANDIF_EXPR:
165 case TRUTH_ORIF_EXPR:
166 case TRUTH_AND_EXPR:
167 case TRUTH_OR_EXPR:
168 case TRUTH_XOR_EXPR:
169 case TRUTH_NOT_EXPR:
170 case ERROR_MARK:
171 return expr;
173 case INTEGER_CST:
174 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
176 case REAL_CST:
177 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
179 /* are these legal? XXX JH */
180 case NEGATE_EXPR:
181 case ABS_EXPR:
182 case FLOAT_EXPR:
183 /* These don't change whether an object is nonzero or zero. */
184 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
186 case COND_EXPR:
187 /* Distribute the conversion into the arms of a COND_EXPR. */
188 return fold
189 (build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
190 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
191 java_truthvalue_conversion (TREE_OPERAND (expr, 2))));
193 case NOP_EXPR:
194 /* If this is widening the argument, we can ignore it. */
195 if (TYPE_PRECISION (TREE_TYPE (expr))
196 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
197 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
198 /* fall through to default */
200 default:
201 return fold (build2 (NE_EXPR, boolean_type_node,
202 expr, boolean_false_node));
206 /* Save any stack slots that happen to be in the quick_stack into their
207 home virtual register slots.
209 The copy order is from low stack index to high, to support the invariant
210 that the expression for a slot may contain decls for stack slots with
211 higher (or the same) index, but not lower. */
213 static void
214 flush_quick_stack (void)
216 int stack_index = stack_pointer;
217 tree prev, cur, next;
219 /* First reverse the quick_stack, and count the number of slots it has. */
220 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
222 next = TREE_CHAIN (cur);
223 TREE_CHAIN (cur) = prev;
224 prev = cur;
225 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
227 quick_stack = prev;
229 while (quick_stack != NULL_TREE)
231 tree decl;
232 tree node = quick_stack, type;
233 quick_stack = TREE_CHAIN (node);
234 TREE_CHAIN (node) = tree_list_free_list;
235 tree_list_free_list = node;
236 node = TREE_VALUE (node);
237 type = TREE_TYPE (node);
239 decl = find_stack_slot (stack_index, type);
240 if (decl != node)
241 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
242 stack_index += 1 + TYPE_IS_WIDE (type);
246 /* Push TYPE on the type stack.
247 Return true on success, 0 on overflow. */
250 push_type_0 (tree type)
252 int n_words;
253 type = promote_type (type);
254 n_words = 1 + TYPE_IS_WIDE (type);
255 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
256 return 0;
257 /* Allocate decl for this variable now, so we get a temporary that
258 survives the whole method. */
259 find_stack_slot (stack_pointer, type);
260 stack_type_map[stack_pointer++] = type;
261 n_words--;
262 while (--n_words >= 0)
263 stack_type_map[stack_pointer++] = TYPE_SECOND;
264 return 1;
267 void
268 push_type (tree type)
270 if (! push_type_0 (type))
271 abort ();
274 static void
275 push_value (tree value)
277 tree type = TREE_TYPE (value);
278 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
280 type = promote_type (type);
281 value = convert (type, value);
283 push_type (type);
284 if (tree_list_free_list == NULL_TREE)
285 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
286 else
288 tree node = tree_list_free_list;
289 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
290 TREE_VALUE (node) = value;
291 TREE_CHAIN (node) = quick_stack;
292 quick_stack = node;
296 /* Pop a type from the type stack.
297 TYPE is the expected type. Return the actual type, which must be
298 convertible to TYPE.
299 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
301 tree
302 pop_type_0 (tree type, char **messagep)
304 int n_words;
305 tree t;
306 *messagep = NULL;
307 if (TREE_CODE (type) == RECORD_TYPE)
308 type = promote_type (type);
309 n_words = 1 + TYPE_IS_WIDE (type);
310 if (stack_pointer < n_words)
312 *messagep = xstrdup ("stack underflow");
313 return type;
315 while (--n_words > 0)
317 if (stack_type_map[--stack_pointer] != void_type_node)
319 *messagep = xstrdup ("Invalid multi-word value on type stack");
320 return type;
323 t = stack_type_map[--stack_pointer];
324 if (type == NULL_TREE || t == type)
325 return t;
326 if (TREE_CODE (t) == TREE_LIST)
330 tree tt = TREE_PURPOSE (t);
331 if (! can_widen_reference_to (tt, type))
333 t = tt;
334 goto fail;
336 t = TREE_CHAIN (t);
338 while (t);
339 return t;
341 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
342 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
343 return t;
344 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
346 if (flag_new_verifier)
348 /* Since the verifier has already run, we know that any
349 types we see will be compatible. In BC mode, this fact
350 may be checked at runtime, but if that is so then we can
351 assume its truth here as well. So, we always succeed
352 here, with the expected type. */
353 return type;
355 else
357 if (type == ptr_type_node || type == object_ptr_type_node)
358 return t;
359 else if (t == ptr_type_node) /* Special case for null reference. */
360 return type;
361 /* This is a kludge, but matches what Sun's verifier does.
362 It can be tricked, but is safe as long as type errors
363 (i.e. interface method calls) are caught at run-time. */
364 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
365 return object_ptr_type_node;
366 else if (can_widen_reference_to (t, type))
367 return t;
371 if (! flag_verify_invocations && flag_indirect_dispatch
372 && t == object_ptr_type_node)
374 if (type != ptr_type_node)
375 warning ("need to insert runtime check for %s",
376 xstrdup (lang_printable_name (type, 0)));
377 return type;
380 /* lang_printable_name uses a static buffer, so we must save the result
381 from calling it the first time. */
382 fail:
384 char *temp = xstrdup (lang_printable_name (type, 0));
385 *messagep = concat ("expected type '", temp,
386 "' but stack contains '", lang_printable_name (t, 0),
387 "'", NULL);
388 free (temp);
390 return type;
393 /* Pop a type from the type stack.
394 TYPE is the expected type. Return the actual type, which must be
395 convertible to TYPE, otherwise call error. */
397 tree
398 pop_type (tree type)
400 char *message = NULL;
401 type = pop_type_0 (type, &message);
402 if (message != NULL)
404 error ("%s", message);
405 free (message);
407 return type;
411 /* Return true if two type assertions are equal. */
413 static int
414 type_assertion_eq (const void * k1_p, const void * k2_p)
416 type_assertion k1 = *(type_assertion *)k1_p;
417 type_assertion k2 = *(type_assertion *)k2_p;
418 return (k1.assertion_code == k2.assertion_code
419 && k1.op1 == k2.op1
420 && k1.op2 == k2.op2);
423 /* Hash a type assertion. */
425 static hashval_t
426 type_assertion_hash (const void *p)
428 const type_assertion *k_p = p;
429 hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
430 k_p->assertion_code, 0);
431 hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
432 return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
435 /* Add an entry to the type assertion table for the given class.
436 CLASS is the class for which this assertion will be evaluated by the
437 runtime during loading/initialization.
438 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
439 OP1 and OP2 are the operands. The tree type of these arguments may be
440 specific to each assertion_code. */
442 void
443 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
445 htab_t assertions_htab;
446 type_assertion as;
447 void **as_pp;
449 assertions_htab = TYPE_ASSERTIONS (class);
450 if (assertions_htab == NULL)
452 assertions_htab = htab_create_ggc (7, type_assertion_hash,
453 type_assertion_eq, NULL);
454 TYPE_ASSERTIONS (current_class) = assertions_htab;
457 as.assertion_code = assertion_code;
458 as.op1 = op1;
459 as.op2 = op2;
461 as_pp = htab_find_slot (assertions_htab, &as, true);
463 /* Don't add the same assertion twice. */
464 if (*as_pp)
465 return;
467 *as_pp = ggc_alloc (sizeof (type_assertion));
468 **(type_assertion **)as_pp = as;
472 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
473 Handles array types and interfaces. */
476 can_widen_reference_to (tree source_type, tree target_type)
478 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
479 return 1;
481 /* Get rid of pointers */
482 if (TREE_CODE (source_type) == POINTER_TYPE)
483 source_type = TREE_TYPE (source_type);
484 if (TREE_CODE (target_type) == POINTER_TYPE)
485 target_type = TREE_TYPE (target_type);
487 if (source_type == target_type)
488 return 1;
490 /* FIXME: This is very pessimistic, in that it checks everything,
491 even if we already know that the types are compatible. If we're
492 to support full Java class loader semantics, we need this.
493 However, we could do something more optimal. */
494 if (! flag_verify_invocations)
496 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
497 source_type, target_type);
499 if (!quiet_flag)
500 warning ("assert: %s is assign compatible with %s",
501 xstrdup (lang_printable_name (target_type, 0)),
502 xstrdup (lang_printable_name (source_type, 0)));
503 /* Punt everything to runtime. */
504 return 1;
507 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
509 return 1;
511 else
513 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
515 HOST_WIDE_INT source_length, target_length;
516 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
518 /* An array implements Cloneable and Serializable. */
519 tree name = DECL_NAME (TYPE_NAME (target_type));
520 return (name == java_lang_cloneable_identifier_node
521 || name == java_io_serializable_identifier_node);
523 target_length = java_array_type_length (target_type);
524 if (target_length >= 0)
526 source_length = java_array_type_length (source_type);
527 if (source_length != target_length)
528 return 0;
530 source_type = TYPE_ARRAY_ELEMENT (source_type);
531 target_type = TYPE_ARRAY_ELEMENT (target_type);
532 if (source_type == target_type)
533 return 1;
534 if (TREE_CODE (source_type) != POINTER_TYPE
535 || TREE_CODE (target_type) != POINTER_TYPE)
536 return 0;
537 return can_widen_reference_to (source_type, target_type);
539 else
541 int source_depth = class_depth (source_type);
542 int target_depth = class_depth (target_type);
544 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
546 if (! quiet_flag)
547 warning ("assert: %s is assign compatible with %s",
548 xstrdup (lang_printable_name (target_type, 0)),
549 xstrdup (lang_printable_name (source_type, 0)));
550 return 1;
553 /* class_depth can return a negative depth if an error occurred */
554 if (source_depth < 0 || target_depth < 0)
555 return 0;
557 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
559 /* target_type is OK if source_type or source_type ancestors
560 implement target_type. We handle multiple sub-interfaces */
561 tree binfo, base_binfo;
562 int i;
564 for (binfo = TYPE_BINFO (source_type), i = 0;
565 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
566 if (can_widen_reference_to
567 (BINFO_TYPE (base_binfo), target_type))
568 return 1;
570 if (!i)
571 return 0;
574 for ( ; source_depth > target_depth; source_depth--)
576 source_type
577 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
579 return source_type == target_type;
584 static tree
585 pop_value (tree type)
587 type = pop_type (type);
588 if (quick_stack)
590 tree node = quick_stack;
591 quick_stack = TREE_CHAIN (quick_stack);
592 TREE_CHAIN (node) = tree_list_free_list;
593 tree_list_free_list = node;
594 node = TREE_VALUE (node);
595 return node;
597 else
598 return find_stack_slot (stack_pointer, promote_type (type));
602 /* Pop and discard the top COUNT stack slots. */
604 static void
605 java_stack_pop (int count)
607 while (count > 0)
609 tree type, val;
611 if (stack_pointer == 0)
612 abort ();
614 type = stack_type_map[stack_pointer - 1];
615 if (type == TYPE_SECOND)
617 count--;
618 if (stack_pointer == 1 || count <= 0)
619 abort ();
621 type = stack_type_map[stack_pointer - 2];
623 val = pop_value (type);
624 count--;
628 /* Implement the 'swap' operator (to swap two top stack slots). */
630 static void
631 java_stack_swap (void)
633 tree type1, type2;
634 tree temp;
635 tree decl1, decl2;
637 if (stack_pointer < 2
638 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
639 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
640 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
641 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
642 /* Bad stack swap. */
643 abort ();
645 flush_quick_stack ();
646 decl1 = find_stack_slot (stack_pointer - 1, type1);
647 decl2 = find_stack_slot (stack_pointer - 2, type2);
648 temp = build_decl (VAR_DECL, NULL_TREE, type1);
649 java_add_local_var (temp);
650 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
651 java_add_stmt (build2 (MODIFY_EXPR, type2,
652 find_stack_slot (stack_pointer - 1, type2),
653 decl2));
654 java_add_stmt (build2 (MODIFY_EXPR, type1,
655 find_stack_slot (stack_pointer - 2, type1),
656 temp));
657 stack_type_map[stack_pointer - 1] = type2;
658 stack_type_map[stack_pointer - 2] = type1;
661 static void
662 java_stack_dup (int size, int offset)
664 int low_index = stack_pointer - size - offset;
665 int dst_index;
666 if (low_index < 0)
667 error ("stack underflow - dup* operation");
669 flush_quick_stack ();
671 stack_pointer += size;
672 dst_index = stack_pointer;
674 for (dst_index = stack_pointer; --dst_index >= low_index; )
676 tree type;
677 int src_index = dst_index - size;
678 if (src_index < low_index)
679 src_index = dst_index + size + offset;
680 type = stack_type_map [src_index];
681 if (type == TYPE_SECOND)
683 if (src_index <= low_index)
684 /* Dup operation splits 64-bit number. */
685 abort ();
687 stack_type_map[dst_index] = type;
688 src_index--; dst_index--;
689 type = stack_type_map[src_index];
690 if (! TYPE_IS_WIDE (type))
691 abort ();
693 else if (TYPE_IS_WIDE (type))
694 abort ();
696 if (src_index != dst_index)
698 tree src_decl = find_stack_slot (src_index, type);
699 tree dst_decl = find_stack_slot (dst_index, type);
701 java_add_stmt
702 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
703 stack_type_map[dst_index] = type;
708 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
709 value stack. */
711 static void
712 build_java_athrow (tree node)
714 tree call;
716 call = build3 (CALL_EXPR,
717 void_type_node,
718 build_address_of (throw_node),
719 build_tree_list (NULL_TREE, node),
720 NULL_TREE);
721 TREE_SIDE_EFFECTS (call) = 1;
722 java_add_stmt (call);
723 java_stack_pop (stack_pointer);
726 /* Implementation for jsr/ret */
728 static void
729 build_java_jsr (int target_pc, int return_pc)
731 tree where = lookup_label (target_pc);
732 tree ret = lookup_label (return_pc);
733 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
734 push_value (ret_label);
735 flush_quick_stack ();
736 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
738 /* Do not need to emit the label here. We noted the existence of the
739 label as a jump target in note_instructions; we'll emit the label
740 for real at the beginning of the expand_byte_code loop. */
743 static void
744 build_java_ret (tree location)
746 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
749 /* Implementation of operations on array: new, load, store, length */
751 tree
752 decode_newarray_type (int atype)
754 switch (atype)
756 case 4: return boolean_type_node;
757 case 5: return char_type_node;
758 case 6: return float_type_node;
759 case 7: return double_type_node;
760 case 8: return byte_type_node;
761 case 9: return short_type_node;
762 case 10: return int_type_node;
763 case 11: return long_type_node;
764 default: return NULL_TREE;
768 /* Map primitive type to the code used by OPCODE_newarray. */
771 encode_newarray_type (tree type)
773 if (type == boolean_type_node)
774 return 4;
775 else if (type == char_type_node)
776 return 5;
777 else if (type == float_type_node)
778 return 6;
779 else if (type == double_type_node)
780 return 7;
781 else if (type == byte_type_node)
782 return 8;
783 else if (type == short_type_node)
784 return 9;
785 else if (type == int_type_node)
786 return 10;
787 else if (type == long_type_node)
788 return 11;
789 else
790 abort ();
793 /* Build a call to _Jv_ThrowBadArrayIndex(), the
794 ArrayIndexOfBoundsException exception handler. */
796 static tree
797 build_java_throw_out_of_bounds_exception (tree index)
799 tree node = build3 (CALL_EXPR, int_type_node,
800 build_address_of (soft_badarrayindex_node),
801 build_tree_list (NULL_TREE, index), NULL_TREE);
802 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
803 return (node);
806 /* Return the length of an array. Doesn't perform any checking on the nature
807 or value of the array NODE. May be used to implement some bytecodes. */
809 tree
810 build_java_array_length_access (tree node)
812 tree type = TREE_TYPE (node);
813 tree array_type = TREE_TYPE (type);
814 HOST_WIDE_INT length;
816 /* JVM spec: If the arrayref is null, the arraylength instruction
817 throws a NullPointerException. The only way we could get a node
818 of type ptr_type_node at this point is `aconst_null; arraylength'
819 or something equivalent. */
820 if (!flag_new_verifier && type == ptr_type_node)
821 return build3 (CALL_EXPR, int_type_node,
822 build_address_of (soft_nullpointer_node),
823 NULL_TREE, NULL_TREE);
825 if (!is_array_type_p (type))
827 /* With the new verifier, we will see an ordinary pointer type
828 here. In this case, we just use an arbitrary array type. */
829 array_type = build_java_array_type (object_ptr_type_node, -1);
830 type = promote_type (array_type);
833 length = java_array_type_length (type);
834 if (length >= 0)
835 return build_int_cst (NULL_TREE, length);
837 node = build3 (COMPONENT_REF, int_type_node,
838 build_java_indirect_ref (array_type, node,
839 flag_check_references),
840 lookup_field (&array_type, get_identifier ("length")),
841 NULL_TREE);
842 IS_ARRAY_LENGTH_ACCESS (node) = 1;
843 return node;
846 /* Optionally checks a reference against the NULL pointer. ARG1: the
847 expr, ARG2: we should check the reference. Don't generate extra
848 checks if we're not generating code. */
850 tree
851 java_check_reference (tree expr, int check)
853 if (!flag_syntax_only && check)
855 expr = save_expr (expr);
856 expr = build3 (COND_EXPR, TREE_TYPE (expr),
857 build2 (EQ_EXPR, boolean_type_node,
858 expr, null_pointer_node),
859 build3 (CALL_EXPR, void_type_node,
860 build_address_of (soft_nullpointer_node),
861 NULL_TREE, NULL_TREE),
862 expr);
865 return expr;
868 /* Reference an object: just like an INDIRECT_REF, but with checking. */
870 tree
871 build_java_indirect_ref (tree type, tree expr, int check)
873 tree t;
874 t = java_check_reference (expr, check);
875 t = convert (build_pointer_type (type), t);
876 return build1 (INDIRECT_REF, type, t);
879 /* Implement array indexing (either as l-value or r-value).
880 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
881 Optionally performs bounds checking and/or test to NULL.
882 At this point, ARRAY should have been verified as an array. */
884 tree
885 build_java_arrayaccess (tree array, tree type, tree index)
887 tree node, throw = NULL_TREE;
888 tree data_field;
889 tree ref;
890 tree array_type = TREE_TYPE (TREE_TYPE (array));
892 if (!is_array_type_p (TREE_TYPE (array)))
894 /* With the new verifier, we will see an ordinary pointer type
895 here. In this case, we just use the correct array type. */
896 array_type = build_java_array_type (type, -1);
899 if (flag_bounds_check)
901 /* Generate:
902 * (unsigned jint) INDEX >= (unsigned jint) LEN
903 * && throw ArrayIndexOutOfBoundsException.
904 * Note this is equivalent to and more efficient than:
905 * INDEX < 0 || INDEX >= LEN && throw ... */
906 tree test;
907 tree len = convert (unsigned_int_type_node,
908 build_java_array_length_access (array));
909 test = fold (build2 (GE_EXPR, boolean_type_node,
910 convert (unsigned_int_type_node, index),
911 len));
912 if (! integer_zerop (test))
914 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
915 build_java_throw_out_of_bounds_exception (index));
916 /* allows expansion within COMPOUND */
917 TREE_SIDE_EFFECTS( throw ) = 1;
921 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
922 to have the bounds check evaluated first. */
923 if (throw != NULL_TREE)
924 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
926 data_field = lookup_field (&array_type, get_identifier ("data"));
928 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
929 build_java_indirect_ref (array_type, array,
930 flag_check_references),
931 data_field, NULL_TREE);
933 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
934 return node;
937 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
938 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
939 determine that no check is required. */
941 tree
942 build_java_arraystore_check (tree array, tree object)
944 tree check, element_type, source;
945 tree array_type_p = TREE_TYPE (array);
946 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
948 if (! flag_verify_invocations)
950 /* With the new verifier, we don't track precise types. FIXME:
951 performance regression here. */
952 element_type = TYPE_NAME (object_type_node);
954 else
956 if (! is_array_type_p (array_type_p))
957 abort ();
959 /* Get the TYPE_DECL for ARRAY's element type. */
960 element_type
961 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
964 if (TREE_CODE (element_type) != TYPE_DECL
965 || TREE_CODE (object_type) != TYPE_DECL)
966 abort ();
968 if (!flag_store_check)
969 return build1 (NOP_EXPR, array_type_p, array);
971 /* No check is needed if the element type is final. Also check that
972 element_type matches object_type, since in the bytecode
973 compilation case element_type may be the actual element type of
974 the array rather than its declared type. However, if we're doing
975 indirect dispatch, we can't do the `final' optimization. */
976 if (element_type == object_type
977 && ! flag_indirect_dispatch
978 && CLASS_FINAL (element_type))
979 return build1 (NOP_EXPR, array_type_p, array);
981 /* OBJECT might be wrapped by a SAVE_EXPR. */
982 if (TREE_CODE (object) == SAVE_EXPR)
983 source = TREE_OPERAND (object, 0);
984 else
985 source = object;
987 /* Avoid the check if OBJECT was just loaded from the same array. */
988 if (TREE_CODE (source) == ARRAY_REF)
990 tree target;
991 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
992 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
993 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
994 if (TREE_CODE (source) == SAVE_EXPR)
995 source = TREE_OPERAND (source, 0);
997 target = array;
998 if (TREE_CODE (target) == SAVE_EXPR)
999 target = TREE_OPERAND (target, 0);
1001 if (source == target)
1002 return build1 (NOP_EXPR, array_type_p, array);
1005 /* Build an invocation of _Jv_CheckArrayStore */
1006 check = build3 (CALL_EXPR, void_type_node,
1007 build_address_of (soft_checkarraystore_node),
1008 tree_cons (NULL_TREE, array,
1009 build_tree_list (NULL_TREE, object)),
1010 NULL_TREE);
1011 TREE_SIDE_EFFECTS (check) = 1;
1013 return check;
1016 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1017 ARRAY_NODE. This function is used to retrieve something less vague than
1018 a pointer type when indexing the first dimension of something like [[<t>.
1019 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1020 return unchanged. */
1022 static tree
1023 build_java_check_indexed_type (tree array_node, tree indexed_type)
1025 tree elt_type;
1027 /* We used to check to see if ARRAY_NODE really had array type.
1028 However, with the new verifier, this is not necessary, as we know
1029 that the object will be an array of the appropriate type. */
1031 if (flag_new_verifier)
1032 return indexed_type;
1034 if (!is_array_type_p (TREE_TYPE (array_node)))
1035 abort ();
1037 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
1039 if (indexed_type == ptr_type_node)
1040 return promote_type (elt_type);
1042 /* BYTE/BOOLEAN store and load are used for both type */
1043 if (indexed_type == byte_type_node && elt_type == boolean_type_node)
1044 return boolean_type_node;
1046 if (indexed_type != elt_type )
1047 abort ();
1048 else
1049 return indexed_type;
1052 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1053 called with an integer code (the type of array to create), and the length
1054 of the array to create. */
1056 tree
1057 build_newarray (int atype_value, tree length)
1059 tree type_arg;
1061 tree prim_type = decode_newarray_type (atype_value);
1062 tree type
1063 = build_java_array_type (prim_type,
1064 host_integerp (length, 0) == INTEGER_CST
1065 ? tree_low_cst (length, 0) : -1);
1067 /* If compiling to native, pass a reference to the primitive type class
1068 and save the runtime some work. However, the bytecode generator
1069 expects to find the type_code int here. */
1070 if (flag_emit_class_files)
1071 type_arg = build_int_cst (NULL_TREE, atype_value);
1072 else
1073 type_arg = build_class_ref (prim_type);
1075 return build3 (CALL_EXPR, promote_type (type),
1076 build_address_of (soft_newarray_node),
1077 tree_cons (NULL_TREE,
1078 type_arg,
1079 build_tree_list (NULL_TREE, length)),
1080 NULL_TREE);
1083 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1084 of the dimension. */
1086 tree
1087 build_anewarray (tree class_type, tree length)
1089 tree type
1090 = build_java_array_type (class_type,
1091 host_integerp (length, 0)
1092 ? tree_low_cst (length, 0) : -1);
1094 return build3 (CALL_EXPR, promote_type (type),
1095 build_address_of (soft_anewarray_node),
1096 tree_cons (NULL_TREE, length,
1097 tree_cons (NULL_TREE, build_class_ref (class_type),
1098 build_tree_list (NULL_TREE,
1099 null_pointer_node))),
1100 NULL_TREE);
1103 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1105 tree
1106 build_new_array (tree type, tree length)
1108 if (JPRIMITIVE_TYPE_P (type))
1109 return build_newarray (encode_newarray_type (type), length);
1110 else
1111 return build_anewarray (TREE_TYPE (type), length);
1114 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1115 class pointer, a number of dimensions and the matching number of
1116 dimensions. The argument list is NULL terminated. */
1118 static void
1119 expand_java_multianewarray (tree class_type, int ndim)
1121 int i;
1122 tree args = build_tree_list( NULL_TREE, null_pointer_node );
1124 for( i = 0; i < ndim; i++ )
1125 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1127 push_value (build3 (CALL_EXPR,
1128 promote_type (class_type),
1129 build_address_of (soft_multianewarray_node),
1130 tree_cons (NULL_TREE, build_class_ref (class_type),
1131 tree_cons (NULL_TREE,
1132 build_int_cst (NULL_TREE, ndim),
1133 args)),
1134 NULL_TREE));
1137 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1138 ARRAY is an array type. May expand some bound checking and NULL
1139 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1140 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1141 INT. In those cases, we make the conversion.
1143 if ARRAy is a reference type, the assignment is checked at run-time
1144 to make sure that the RHS can be assigned to the array element
1145 type. It is not necessary to generate this code if ARRAY is final. */
1147 static void
1148 expand_java_arraystore (tree rhs_type_node)
1150 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
1151 && TYPE_PRECISION (rhs_type_node) <= 32) ?
1152 int_type_node : rhs_type_node);
1153 tree index = pop_value (int_type_node);
1154 tree array_type, array;
1156 if (flag_new_verifier)
1158 /* If we're processing an `aaload' we might as well just pick
1159 `Object'. */
1160 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1162 array_type = build_java_array_type (object_ptr_type_node, -1);
1163 rhs_type_node = object_ptr_type_node;
1165 else
1166 array_type = build_java_array_type (rhs_type_node, -1);
1168 else
1169 array_type = ptr_type_node;
1170 array = pop_value (array_type);
1171 if (flag_new_verifier)
1172 array = build1 (NOP_EXPR, promote_type (array_type), array);
1174 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1176 flush_quick_stack ();
1178 index = save_expr (index);
1179 array = save_expr (array);
1181 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1183 tree check = build_java_arraystore_check (array, rhs_node);
1184 java_add_stmt (check);
1187 array = build_java_arrayaccess (array, rhs_type_node, index);
1188 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1191 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1192 sure that LHS is an array type. May expand some bound checking and NULL
1193 pointer checking.
1194 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1195 BOOLEAN/SHORT, we push a promoted type back to the stack.
1198 static void
1199 expand_java_arrayload (tree lhs_type_node)
1201 tree load_node;
1202 tree index_node = pop_value (int_type_node);
1203 tree array_type;
1204 tree array_node;
1206 if (flag_new_verifier)
1208 /* If we're processing an `aaload' we might as well just pick
1209 `Object'. */
1210 if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1212 array_type = build_java_array_type (object_ptr_type_node, -1);
1213 lhs_type_node = object_ptr_type_node;
1215 else
1216 array_type = build_java_array_type (lhs_type_node, -1);
1218 else
1219 array_type = ptr_type_node;
1220 array_node = pop_value (array_type);
1221 if (flag_new_verifier)
1222 array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1224 index_node = save_expr (index_node);
1225 array_node = save_expr (array_node);
1227 if (TREE_TYPE (array_node) == ptr_type_node)
1228 /* The only way we could get a node of type ptr_type_node at this
1229 point is `aconst_null; arraylength' or something equivalent, so
1230 unconditionally throw NullPointerException. */
1231 load_node = build3 (CALL_EXPR, lhs_type_node,
1232 build_address_of (soft_nullpointer_node),
1233 NULL_TREE, NULL_TREE);
1234 else
1236 lhs_type_node = build_java_check_indexed_type (array_node,
1237 lhs_type_node);
1238 load_node = build_java_arrayaccess (array_node,
1239 lhs_type_node,
1240 index_node);
1242 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1243 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
1244 push_value (load_node);
1247 /* Expands .length. Makes sure that we deal with and array and may expand
1248 a NULL check on the array object. */
1250 static void
1251 expand_java_array_length (void)
1253 tree array = pop_value (ptr_type_node);
1254 tree length = build_java_array_length_access (array);
1256 push_value (length);
1259 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1260 either soft_monitorenter_node or soft_monitorexit_node. */
1262 static tree
1263 build_java_monitor (tree call, tree object)
1265 return build3 (CALL_EXPR,
1266 void_type_node,
1267 build_address_of (call),
1268 build_tree_list (NULL_TREE, object),
1269 NULL_TREE);
1272 /* Emit code for one of the PUSHC instructions. */
1274 static void
1275 expand_java_pushc (int ival, tree type)
1277 tree value;
1278 if (type == ptr_type_node && ival == 0)
1279 value = null_pointer_node;
1280 else if (type == int_type_node || type == long_type_node)
1281 value = build_int_cst (type, ival);
1282 else if (type == float_type_node || type == double_type_node)
1284 REAL_VALUE_TYPE x;
1285 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1286 value = build_real (type, x);
1288 else
1289 abort ();
1291 push_value (value);
1294 static void
1295 expand_java_return (tree type)
1297 if (type == void_type_node)
1298 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1299 else
1301 tree retval = pop_value (type);
1302 tree res = DECL_RESULT (current_function_decl);
1303 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1305 /* Handle the situation where the native integer type is smaller
1306 than the JVM integer. It can happen for many cross compilers.
1307 The whole if expression just goes away if INT_TYPE_SIZE < 32
1308 is false. */
1309 if (INT_TYPE_SIZE < 32
1310 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1311 < GET_MODE_SIZE (TYPE_MODE (type))))
1312 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1314 TREE_SIDE_EFFECTS (retval) = 1;
1315 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1319 static void
1320 expand_load_internal (int index, tree type, int pc)
1322 tree copy;
1323 tree var = find_local_variable (index, type, pc);
1325 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1326 on the stack. If there is an assignment to this VAR_DECL between
1327 the stack push and the use, then the wrong code could be
1328 generated. To avoid this we create a new local and copy our
1329 value into it. Then we push this new local on the stack.
1330 Hopefully this all gets optimized out. */
1331 copy = build_decl (VAR_DECL, NULL_TREE, type);
1332 if (INTEGRAL_TYPE_P (type)
1333 && TREE_TYPE (copy) != TREE_TYPE (var))
1334 var = convert (type, var);
1335 java_add_local_var (copy);
1336 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1338 push_value (copy);
1341 tree
1342 build_address_of (tree value)
1344 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1347 bool
1348 class_has_finalize_method (tree type)
1350 tree super = CLASSTYPE_SUPER (type);
1352 if (super == NULL_TREE)
1353 return false; /* Every class with a real finalizer inherits */
1354 /* from java.lang.Object. */
1355 else
1356 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1359 tree
1360 java_create_object (tree type)
1362 tree alloc_node = (class_has_finalize_method (type)
1363 ? alloc_object_node
1364 : alloc_no_finalizer_node);
1366 return build (CALL_EXPR, promote_type (type),
1367 build_address_of (alloc_node),
1368 build_tree_list (NULL_TREE, build_class_ref (type)),
1369 NULL_TREE);
1372 static void
1373 expand_java_NEW (tree type)
1375 tree alloc_node;
1377 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1378 : alloc_no_finalizer_node);
1379 if (! CLASS_LOADED_P (type))
1380 load_class (type, 1);
1381 safe_layout_class (type);
1382 push_value (build3 (CALL_EXPR, promote_type (type),
1383 build_address_of (alloc_node),
1384 build_tree_list (NULL_TREE, build_class_ref (type)),
1385 NULL_TREE));
1388 /* This returns an expression which will extract the class of an
1389 object. */
1391 tree
1392 build_get_class (tree value)
1394 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1395 tree vtable_field = lookup_field (&object_type_node,
1396 get_identifier ("vtable"));
1397 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1398 build_java_indirect_ref (object_type_node, value,
1399 flag_check_references),
1400 vtable_field, NULL_TREE);
1401 return build3 (COMPONENT_REF, class_ptr_type,
1402 build1 (INDIRECT_REF, dtable_type, tmp),
1403 class_field, NULL_TREE);
1406 /* This builds the tree representation of the `instanceof' operator.
1407 It tries various tricks to optimize this in cases where types are
1408 known. */
1410 tree
1411 build_instanceof (tree value, tree type)
1413 tree expr;
1414 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1415 tree valtype = TREE_TYPE (TREE_TYPE (value));
1416 tree valclass = TYPE_NAME (valtype);
1417 tree klass;
1419 /* When compiling from bytecode, we need to ensure that TYPE has
1420 been loaded. */
1421 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1423 load_class (type, 1);
1424 safe_layout_class (type);
1425 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1426 return error_mark_node;
1428 klass = TYPE_NAME (type);
1430 if (type == object_type_node || inherits_from_p (valtype, type))
1432 /* Anything except `null' is an instance of Object. Likewise,
1433 if the object is known to be an instance of the class, then
1434 we only need to check for `null'. */
1435 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1437 else if (flag_verify_invocations
1438 && ! TYPE_ARRAY_P (type)
1439 && ! TYPE_ARRAY_P (valtype)
1440 && DECL_P (klass) && DECL_P (valclass)
1441 && ! CLASS_INTERFACE (valclass)
1442 && ! CLASS_INTERFACE (klass)
1443 && ! inherits_from_p (type, valtype)
1444 && (CLASS_FINAL (klass)
1445 || ! inherits_from_p (valtype, type)))
1447 /* The classes are from different branches of the derivation
1448 tree, so we immediately know the answer. */
1449 expr = boolean_false_node;
1451 else if (DECL_P (klass) && CLASS_FINAL (klass))
1453 tree save = save_expr (value);
1454 expr = build3 (COND_EXPR, itype,
1455 build2 (NE_EXPR, boolean_type_node,
1456 save, null_pointer_node),
1457 build2 (EQ_EXPR, itype,
1458 build_get_class (save),
1459 build_class_ref (type)),
1460 boolean_false_node);
1462 else
1464 expr = build3 (CALL_EXPR, itype,
1465 build_address_of (soft_instanceof_node),
1466 tree_cons (NULL_TREE, value,
1467 build_tree_list (NULL_TREE,
1468 build_class_ref (type))),
1469 NULL_TREE);
1471 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1472 return expr;
1475 static void
1476 expand_java_INSTANCEOF (tree type)
1478 tree value = pop_value (object_ptr_type_node);
1479 value = build_instanceof (value, type);
1480 push_value (value);
1483 static void
1484 expand_java_CHECKCAST (tree type)
1486 tree value = pop_value (ptr_type_node);
1487 value = build3 (CALL_EXPR, promote_type (type),
1488 build_address_of (soft_checkcast_node),
1489 tree_cons (NULL_TREE, build_class_ref (type),
1490 build_tree_list (NULL_TREE, value)),
1491 NULL_TREE);
1492 push_value (value);
1495 static void
1496 expand_iinc (unsigned int local_var_index, int ival, int pc)
1498 tree local_var, res;
1499 tree constant_value;
1501 flush_quick_stack ();
1502 local_var = find_local_variable (local_var_index, int_type_node, pc);
1503 constant_value = build_int_cst (NULL_TREE, ival);
1504 res = fold (build2 (PLUS_EXPR, int_type_node, local_var, constant_value));
1505 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1506 update_aliases (local_var, local_var_index, pc);
1510 tree
1511 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1513 tree call = NULL;
1514 tree arg1 = convert (type, op1);
1515 tree arg2 = convert (type, op2);
1517 if (type == int_type_node)
1519 switch (op)
1521 case TRUNC_DIV_EXPR:
1522 call = soft_idiv_node;
1523 break;
1524 case TRUNC_MOD_EXPR:
1525 call = soft_irem_node;
1526 break;
1527 default:
1528 break;
1531 else if (type == long_type_node)
1533 switch (op)
1535 case TRUNC_DIV_EXPR:
1536 call = soft_ldiv_node;
1537 break;
1538 case TRUNC_MOD_EXPR:
1539 call = soft_lrem_node;
1540 break;
1541 default:
1542 break;
1546 if (! call)
1547 abort ();
1549 call = build3 (CALL_EXPR, type,
1550 build_address_of (call),
1551 tree_cons (NULL_TREE, arg1,
1552 build_tree_list (NULL_TREE, arg2)),
1553 NULL_TREE);
1555 return call;
1558 tree
1559 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1561 tree mask;
1562 switch (op)
1564 case URSHIFT_EXPR:
1566 tree u_type = java_unsigned_type (type);
1567 arg1 = convert (u_type, arg1);
1568 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1569 return convert (type, arg1);
1571 case LSHIFT_EXPR:
1572 case RSHIFT_EXPR:
1573 mask = build_int_cst (NULL_TREE,
1574 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1575 arg2 = fold (build2 (BIT_AND_EXPR, int_type_node, arg2, mask));
1576 break;
1578 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1579 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1580 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1582 tree ifexp1 = fold (build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1583 boolean_type_node, arg1, arg2));
1584 tree ifexp2 = fold (build2 (EQ_EXPR, boolean_type_node, arg1, arg2));
1585 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1586 ifexp2, integer_zero_node,
1587 op == COMPARE_L_EXPR
1588 ? integer_minus_one_node
1589 : integer_one_node));
1590 return fold (build3 (COND_EXPR, int_type_node, ifexp1,
1591 op == COMPARE_L_EXPR ? integer_one_node
1592 : integer_minus_one_node,
1593 second_compare));
1595 case COMPARE_EXPR:
1596 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1598 tree ifexp1 = fold (build2 (LT_EXPR, boolean_type_node, arg1, arg2));
1599 tree ifexp2 = fold (build2 (GT_EXPR, boolean_type_node, arg1, arg2));
1600 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1601 ifexp2, integer_one_node,
1602 integer_zero_node));
1603 return fold (build3 (COND_EXPR, int_type_node,
1604 ifexp1, integer_minus_one_node, second_compare));
1606 case TRUNC_DIV_EXPR:
1607 case TRUNC_MOD_EXPR:
1608 if (TREE_CODE (type) == REAL_TYPE
1609 && op == TRUNC_MOD_EXPR)
1611 tree call;
1612 if (type != double_type_node)
1614 arg1 = convert (double_type_node, arg1);
1615 arg2 = convert (double_type_node, arg2);
1617 call = build3 (CALL_EXPR, double_type_node,
1618 build_address_of (soft_fmod_node),
1619 tree_cons (NULL_TREE, arg1,
1620 build_tree_list (NULL_TREE, arg2)),
1621 NULL_TREE);
1622 if (type != double_type_node)
1623 call = convert (type, call);
1624 return call;
1627 if (TREE_CODE (type) == INTEGER_TYPE
1628 && flag_use_divide_subroutine
1629 && ! flag_syntax_only)
1630 return build_java_soft_divmod (op, type, arg1, arg2);
1632 break;
1633 default: ;
1635 return fold (build2 (op, type, arg1, arg2));
1638 static void
1639 expand_java_binop (tree type, enum tree_code op)
1641 tree larg, rarg;
1642 tree ltype = type;
1643 tree rtype = type;
1644 switch (op)
1646 case LSHIFT_EXPR:
1647 case RSHIFT_EXPR:
1648 case URSHIFT_EXPR:
1649 rtype = int_type_node;
1650 rarg = pop_value (rtype);
1651 break;
1652 default:
1653 rarg = pop_value (rtype);
1655 larg = pop_value (ltype);
1656 push_value (build_java_binop (op, type, larg, rarg));
1659 /* Lookup the field named NAME in *TYPEP or its super classes.
1660 If not found, return NULL_TREE.
1661 (If the *TYPEP is not found, or if the field reference is
1662 ambiguous, return error_mark_node.)
1663 If found, return the FIELD_DECL, and set *TYPEP to the
1664 class containing the field. */
1666 tree
1667 lookup_field (tree *typep, tree name)
1669 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1671 load_class (*typep, 1);
1672 safe_layout_class (*typep);
1673 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1674 return error_mark_node;
1678 tree field, binfo, base_binfo;
1679 tree save_field;
1680 int i;
1682 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1683 if (DECL_NAME (field) == name)
1684 return field;
1686 /* Process implemented interfaces. */
1687 save_field = NULL_TREE;
1688 for (binfo = TYPE_BINFO (*typep), i = 0;
1689 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1691 tree t = BINFO_TYPE (base_binfo);
1692 if ((field = lookup_field (&t, name)))
1694 if (save_field == field)
1695 continue;
1696 if (save_field == NULL_TREE)
1697 save_field = field;
1698 else
1700 tree i1 = DECL_CONTEXT (save_field);
1701 tree i2 = DECL_CONTEXT (field);
1702 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1703 IDENTIFIER_POINTER (name),
1704 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1705 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1706 return error_mark_node;
1711 if (save_field != NULL_TREE)
1712 return save_field;
1714 *typep = CLASSTYPE_SUPER (*typep);
1715 } while (*typep);
1716 return NULL_TREE;
1719 /* Look up the field named NAME in object SELF_VALUE,
1720 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1721 SELF_VALUE is NULL_TREE if looking for a static field. */
1723 tree
1724 build_field_ref (tree self_value, tree self_class, tree name)
1726 tree base_class = self_class;
1727 tree field_decl = lookup_field (&base_class, name);
1728 if (field_decl == NULL_TREE)
1730 error ("field %qs not found", IDENTIFIER_POINTER (name));
1731 return error_mark_node;
1733 if (self_value == NULL_TREE)
1735 return build_static_field_ref (field_decl);
1737 else
1739 int check = (flag_check_references
1740 && ! (DECL_P (self_value)
1741 && DECL_NAME (self_value) == this_identifier_node));
1743 tree base_type = promote_type (base_class);
1744 if (base_type != TREE_TYPE (self_value))
1745 self_value = fold (build1 (NOP_EXPR, base_type, self_value));
1746 if (! flag_syntax_only
1747 && (flag_indirect_dispatch
1748 /* DECL_FIELD_OFFSET == 0 if we have no reference for
1749 the field, perhaps because we couldn't find the class
1750 in which the field is defined.
1751 FIXME: We should investigate this. */
1752 || DECL_FIELD_OFFSET (field_decl) == 0))
1754 tree otable_index
1755 = build_int_cst (NULL_TREE, get_symbol_table_index
1756 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1757 tree field_offset
1758 = build4 (ARRAY_REF, integer_type_node,
1759 TYPE_OTABLE_DECL (output_class), otable_index,
1760 NULL_TREE, NULL_TREE);
1761 tree address;
1763 field_offset = fold (convert (sizetype, field_offset));
1764 address
1765 = fold (build2 (PLUS_EXPR,
1766 build_pointer_type (TREE_TYPE (field_decl)),
1767 self_value, field_offset));
1768 return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
1771 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1772 self_value, check);
1773 return fold (build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1774 self_value, field_decl, NULL_TREE));
1778 tree
1779 lookup_label (int pc)
1781 tree name;
1782 char buf[32];
1783 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1784 name = get_identifier (buf);
1785 if (IDENTIFIER_LOCAL_VALUE (name))
1786 return IDENTIFIER_LOCAL_VALUE (name);
1787 else
1789 /* The type of the address of a label is return_address_type_node. */
1790 tree decl = create_label_decl (name);
1791 LABEL_PC (decl) = pc;
1792 return pushdecl (decl);
1796 /* Generate a unique name for the purpose of loops and switches
1797 labels, and try-catch-finally blocks label or temporary variables. */
1799 tree
1800 generate_name (void)
1802 static int l_number = 0;
1803 char buff [32];
1804 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1805 l_number++;
1806 return get_identifier (buff);
1809 tree
1810 create_label_decl (tree name)
1812 tree decl;
1813 decl = build_decl (LABEL_DECL, name,
1814 TREE_TYPE (return_address_type_node));
1815 DECL_CONTEXT (decl) = current_function_decl;
1816 DECL_IGNORED_P (decl) = 1;
1817 return decl;
1820 /* This maps a bytecode offset (PC) to various flags. */
1821 char *instruction_bits;
1823 static void
1824 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1826 lookup_label (target_pc);
1827 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1830 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1831 where CONDITION is one of one the compare operators. */
1833 static void
1834 expand_compare (enum tree_code condition, tree value1, tree value2,
1835 int target_pc)
1837 tree target = lookup_label (target_pc);
1838 tree cond = fold (build2 (condition, boolean_type_node, value1, value2));
1839 java_add_stmt
1840 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1841 build1 (GOTO_EXPR, void_type_node, target),
1842 build_java_empty_stmt ()));
1845 /* Emit code for a TEST-type opcode. */
1847 static void
1848 expand_test (enum tree_code condition, tree type, int target_pc)
1850 tree value1, value2;
1851 flush_quick_stack ();
1852 value1 = pop_value (type);
1853 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1854 expand_compare (condition, value1, value2, target_pc);
1857 /* Emit code for a COND-type opcode. */
1859 static void
1860 expand_cond (enum tree_code condition, tree type, int target_pc)
1862 tree value1, value2;
1863 flush_quick_stack ();
1864 /* note: pop values in opposite order */
1865 value2 = pop_value (type);
1866 value1 = pop_value (type);
1867 /* Maybe should check value1 and value2 for type compatibility ??? */
1868 expand_compare (condition, value1, value2, target_pc);
1871 static void
1872 expand_java_goto (int target_pc)
1874 tree target_label = lookup_label (target_pc);
1875 flush_quick_stack ();
1876 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1879 static tree
1880 expand_java_switch (tree selector, int default_pc)
1882 tree switch_expr, x;
1884 flush_quick_stack ();
1885 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1886 NULL_TREE, NULL_TREE);
1887 java_add_stmt (switch_expr);
1889 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1890 create_artificial_label ());
1891 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1893 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1894 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1896 return switch_expr;
1899 static void
1900 expand_java_add_case (tree switch_expr, int match, int target_pc)
1902 tree value, x;
1904 value = build_int_cst (TREE_TYPE (switch_expr), match);
1906 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1907 create_artificial_label ());
1908 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1910 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1911 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1914 static tree
1915 pop_arguments (tree arg_types)
1917 if (arg_types == end_params_node)
1918 return NULL_TREE;
1919 if (TREE_CODE (arg_types) == TREE_LIST)
1921 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1922 tree type = TREE_VALUE (arg_types);
1923 tree arg = pop_value (type);
1925 /* With the new verifier we simply cast each argument to its
1926 proper type. This is needed since we lose type information
1927 coming out of the verifier. We also have to do this with the
1928 old verifier when we pop an integer type that must be
1929 promoted for the function call. */
1930 if (flag_new_verifier && TREE_CODE (type) == POINTER_TYPE)
1931 arg = build1 (NOP_EXPR, type, arg);
1932 else if (targetm.calls.promote_prototypes (type)
1933 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1934 && INTEGRAL_TYPE_P (type))
1935 arg = convert (integer_type_node, arg);
1936 return tree_cons (NULL_TREE, arg, tail);
1938 abort ();
1941 /* Attach to PTR (a block) the declaration found in ENTRY. */
1944 attach_init_test_initialization_flags (void **entry, void *ptr)
1946 tree block = (tree)ptr;
1947 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1949 if (block != error_mark_node)
1951 if (TREE_CODE (block) == BIND_EXPR)
1953 tree body = BIND_EXPR_BODY (block);
1954 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1955 BIND_EXPR_VARS (block) = ite->value;
1956 body = build2 (COMPOUND_EXPR, void_type_node,
1957 build1 (DECL_EXPR, void_type_node, ite->value), body);
1958 BIND_EXPR_BODY (block) = body;
1960 else
1962 tree body = BLOCK_SUBBLOCKS (block);
1963 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1964 BLOCK_EXPR_DECLS (block) = ite->value;
1965 body = build2 (COMPOUND_EXPR, void_type_node,
1966 build1 (DECL_EXPR, void_type_node, ite->value), body);
1967 BLOCK_SUBBLOCKS (block) = body;
1971 return true;
1974 /* Build an expression to initialize the class CLAS.
1975 if EXPR is non-NULL, returns an expression to first call the initializer
1976 (if it is needed) and then calls EXPR. */
1978 tree
1979 build_class_init (tree clas, tree expr)
1981 tree init;
1983 /* An optimization: if CLAS is a superclass of the class we're
1984 compiling, we don't need to initialize it. However, if CLAS is
1985 an interface, it won't necessarily be initialized, even if we
1986 implement it. */
1987 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1988 && inherits_from_p (current_class, clas))
1989 || current_class == clas)
1990 return expr;
1992 if (always_initialize_class_p)
1994 init = build3 (CALL_EXPR, void_type_node,
1995 build_address_of (soft_initclass_node),
1996 build_tree_list (NULL_TREE, build_class_ref (clas)),
1997 NULL_TREE);
1998 TREE_SIDE_EFFECTS (init) = 1;
2000 else
2002 tree *init_test_decl;
2003 tree decl;
2004 init_test_decl = java_treetreehash_new
2005 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
2007 if (*init_test_decl == NULL)
2009 /* Build a declaration and mark it as a flag used to track
2010 static class initializations. */
2011 decl = build_decl (VAR_DECL, NULL_TREE,
2012 boolean_type_node);
2013 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2014 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
2015 DECL_CONTEXT (decl) = current_function_decl;
2016 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
2017 /* Tell the check-init code to ignore this decl when not
2018 optimizing class initialization. */
2019 if (!STATIC_CLASS_INIT_OPT_P ())
2020 DECL_BIT_INDEX (decl) = -1;
2021 DECL_INITIAL (decl) = boolean_false_node;
2022 /* Don't emit any symbolic debugging info for this decl. */
2023 DECL_IGNORED_P (decl) = 1;
2024 *init_test_decl = decl;
2027 init = build3 (CALL_EXPR, void_type_node,
2028 build_address_of (soft_initclass_node),
2029 build_tree_list (NULL_TREE, build_class_ref (clas)),
2030 NULL_TREE);
2031 TREE_SIDE_EFFECTS (init) = 1;
2032 init = build3 (COND_EXPR, void_type_node,
2033 build2 (EQ_EXPR, boolean_type_node,
2034 *init_test_decl, boolean_false_node),
2035 init, integer_zero_node);
2036 TREE_SIDE_EFFECTS (init) = 1;
2037 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
2038 build2 (MODIFY_EXPR, boolean_type_node,
2039 *init_test_decl, boolean_true_node));
2040 TREE_SIDE_EFFECTS (init) = 1;
2043 if (expr != NULL_TREE)
2045 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2046 TREE_SIDE_EFFECTS (expr) = 1;
2047 return expr;
2049 return init;
2052 tree
2053 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2054 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2055 tree arg_list ATTRIBUTE_UNUSED)
2057 tree func;
2058 if (is_compiled_class (self_type))
2060 /* With indirect dispatch we have to use indirect calls for all
2061 publicly visible methods or gcc will use PLT indirections
2062 to reach them. We also have to use indirect dispatch for all
2063 external methods. */
2064 if (! flag_indirect_dispatch
2065 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2067 make_decl_rtl (method);
2068 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2069 method);
2071 else
2073 tree table_index
2074 = build_int_cst (NULL_TREE, get_symbol_table_index
2075 (method, &TYPE_ATABLE_METHODS (output_class)));
2076 func
2077 = build4 (ARRAY_REF,
2078 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2079 TYPE_ATABLE_DECL (output_class), table_index,
2080 NULL_TREE, NULL_TREE);
2082 func = convert (method_ptr_type_node, func);
2084 else
2086 /* We don't know whether the method has been (statically) compiled.
2087 Compile this code to get a reference to the method's code:
2089 SELF_TYPE->methods[METHOD_INDEX].ncode
2093 int method_index = 0;
2094 tree meth, ref;
2096 /* The method might actually be declared in some superclass, so
2097 we have to use its class context, not the caller's notion of
2098 where the method is. */
2099 self_type = DECL_CONTEXT (method);
2100 ref = build_class_ref (self_type);
2101 ref = build1 (INDIRECT_REF, class_type_node, ref);
2102 if (ncode_ident == NULL_TREE)
2103 ncode_ident = get_identifier ("ncode");
2104 if (methods_ident == NULL_TREE)
2105 methods_ident = get_identifier ("methods");
2106 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2107 lookup_field (&class_type_node, methods_ident),
2108 NULL_TREE);
2109 for (meth = TYPE_METHODS (self_type);
2110 ; meth = TREE_CHAIN (meth))
2112 if (method == meth)
2113 break;
2114 if (meth == NULL_TREE)
2115 fatal_error ("method '%s' not found in class",
2116 IDENTIFIER_POINTER (DECL_NAME (method)));
2117 method_index++;
2119 method_index *= int_size_in_bytes (method_type_node);
2120 ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
2121 ref, build_int_cst (NULL_TREE, method_index)));
2122 ref = build1 (INDIRECT_REF, method_type_node, ref);
2123 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2124 ref, lookup_field (&method_type_node, ncode_ident),
2125 NULL_TREE);
2127 return func;
2130 tree
2131 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2133 tree dtable, objectref;
2135 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2137 /* If we're dealing with interfaces and if the objectref
2138 argument is an array then get the dispatch table of the class
2139 Object rather than the one from the objectref. */
2140 objectref = (is_invoke_interface
2141 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2142 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2144 if (dtable_ident == NULL_TREE)
2145 dtable_ident = get_identifier ("vtable");
2146 dtable = build_java_indirect_ref (object_type_node, objectref,
2147 flag_check_references);
2148 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2149 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2151 return dtable;
2154 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2155 T. If this decl has not been seen before, it will be added to the
2156 [oa]table_methods. If it has, the existing table slot will be
2157 reused. */
2160 get_symbol_table_index (tree t, tree *symbol_table)
2162 int i = 1;
2163 tree method_list;
2165 if (*symbol_table == NULL_TREE)
2167 *symbol_table = build_tree_list (t, t);
2168 return 1;
2171 method_list = *symbol_table;
2173 while (1)
2175 tree value = TREE_VALUE (method_list);
2176 if (value == t)
2177 return i;
2178 i++;
2179 if (TREE_CHAIN (method_list) == NULL_TREE)
2180 break;
2181 else
2182 method_list = TREE_CHAIN (method_list);
2185 TREE_CHAIN (method_list) = build_tree_list (t, t);
2186 return i;
2189 tree
2190 build_invokevirtual (tree dtable, tree method)
2192 tree func;
2193 tree nativecode_ptr_ptr_type_node
2194 = build_pointer_type (nativecode_ptr_type_node);
2195 tree method_index;
2196 tree otable_index;
2198 if (flag_indirect_dispatch)
2200 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2201 abort ();
2203 otable_index
2204 = build_int_cst (NULL_TREE, get_symbol_table_index
2205 (method, &TYPE_OTABLE_METHODS (output_class)));
2206 method_index = build4 (ARRAY_REF, integer_type_node,
2207 TYPE_OTABLE_DECL (output_class),
2208 otable_index, NULL_TREE, NULL_TREE);
2210 else
2212 /* We fetch the DECL_VINDEX field directly here, rather than
2213 using get_method_index(). DECL_VINDEX is the true offset
2214 from the vtable base to a method, regrdless of any extra
2215 words inserted at the start of the vtable. */
2216 method_index = DECL_VINDEX (method);
2217 method_index = size_binop (MULT_EXPR, method_index,
2218 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2219 if (TARGET_VTABLE_USES_DESCRIPTORS)
2220 method_index = size_binop (MULT_EXPR, method_index,
2221 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2224 func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2225 convert (nativecode_ptr_ptr_type_node, method_index)));
2227 if (TARGET_VTABLE_USES_DESCRIPTORS)
2228 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2229 else
2230 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2232 return func;
2235 static GTY(()) tree class_ident;
2236 tree
2237 build_invokeinterface (tree dtable, tree method)
2239 tree lookup_arg;
2240 tree interface;
2241 tree idx;
2243 /* We expand invokeinterface here. */
2245 if (class_ident == NULL_TREE)
2246 class_ident = get_identifier ("class");
2248 dtable = build_java_indirect_ref (dtable_type, dtable,
2249 flag_check_references);
2250 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2251 lookup_field (&dtable_type, class_ident), NULL_TREE);
2253 interface = DECL_CONTEXT (method);
2254 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2255 abort ();
2256 layout_class_methods (interface);
2258 if (flag_indirect_dispatch)
2260 int itable_index
2261 = 2 * (get_symbol_table_index
2262 (method, &TYPE_ITABLE_METHODS (output_class)));
2263 interface
2264 = build4 (ARRAY_REF,
2265 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2266 TYPE_ITABLE_DECL (output_class),
2267 build_int_cst (NULL_TREE, itable_index-1),
2268 NULL_TREE, NULL_TREE);
2269 idx
2270 = build4 (ARRAY_REF,
2271 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2272 TYPE_ITABLE_DECL (output_class),
2273 build_int_cst (NULL_TREE, itable_index),
2274 NULL_TREE, NULL_TREE);
2275 interface = convert (class_ptr_type, interface);
2276 idx = convert (integer_type_node, idx);
2278 else
2280 idx = build_int_cst (NULL_TREE,
2281 get_interface_method_index (method, interface));
2282 interface = build_class_ref (interface);
2285 lookup_arg = tree_cons (NULL_TREE, dtable,
2286 tree_cons (NULL_TREE, interface,
2287 build_tree_list (NULL_TREE, idx)));
2289 return build3 (CALL_EXPR, ptr_type_node,
2290 build_address_of (soft_lookupinterfacemethod_node),
2291 lookup_arg, NULL_TREE);
2294 /* Expand one of the invoke_* opcodes.
2295 OPCODE is the specific opcode.
2296 METHOD_REF_INDEX is an index into the constant pool.
2297 NARGS is the number of arguments, or -1 if not specified. */
2299 static void
2300 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2302 tree method_signature
2303 = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2304 tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2305 method_ref_index);
2306 tree self_type
2307 = get_class_constant (current_jcf,
2308 COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2309 method_ref_index));
2310 const char *const self_name
2311 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2312 tree call, func, method, arg_list, method_type;
2313 tree check = NULL_TREE;
2315 if (! CLASS_LOADED_P (self_type))
2317 load_class (self_type, 1);
2318 safe_layout_class (self_type);
2319 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2320 fatal_error ("failed to find class '%s'", self_name);
2322 layout_class_methods (self_type);
2324 if (ID_INIT_P (method_name))
2325 method = lookup_java_constructor (self_type, method_signature);
2326 else
2327 method = lookup_java_method (self_type, method_name, method_signature);
2329 /* We've found a method in an interface, but this isn't an interface
2330 call. */
2331 if (opcode != OPCODE_invokeinterface
2332 && method
2333 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2334 method = NULL_TREE;
2336 /* We've found a non-interface method but we are making an
2337 interface call. This can happen if the interface overrides a
2338 method in Object. */
2339 if (! flag_verify_invocations
2340 && opcode == OPCODE_invokeinterface
2341 && method
2342 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2343 method = NULL_TREE;
2345 if (method == NULL_TREE)
2347 if (flag_verify_invocations || ! flag_indirect_dispatch)
2349 error ("class '%s' has no method named '%s' matching signature '%s'",
2350 self_name,
2351 IDENTIFIER_POINTER (method_name),
2352 IDENTIFIER_POINTER (method_signature));
2354 else
2356 int flags = ACC_PUBLIC;
2357 if (opcode == OPCODE_invokestatic)
2358 flags |= ACC_STATIC;
2359 if (opcode == OPCODE_invokeinterface)
2361 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2362 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2364 method = add_method (self_type, flags, method_name,
2365 method_signature);
2366 DECL_ARTIFICIAL (method) = 1;
2367 METHOD_DUMMY (method) = 1;
2368 layout_class_method (self_type, NULL,
2369 method, NULL);
2373 /* Invoke static can't invoke static/abstract method */
2374 if (method != NULL_TREE)
2376 if (opcode == OPCODE_invokestatic)
2378 if (!METHOD_STATIC (method))
2380 error ("invokestatic on non static method");
2381 method = NULL_TREE;
2383 else if (METHOD_ABSTRACT (method))
2385 error ("invokestatic on abstract method");
2386 method = NULL_TREE;
2389 else
2391 if (METHOD_STATIC (method))
2393 error ("invoke[non-static] on static method");
2394 method = NULL_TREE;
2399 if (method == NULL_TREE)
2401 /* If we got here, we emitted an error message above. So we
2402 just pop the arguments, push a properly-typed zero, and
2403 continue. */
2404 method_type = get_type_from_signature (method_signature);
2405 pop_arguments (TYPE_ARG_TYPES (method_type));
2406 if (opcode != OPCODE_invokestatic)
2407 pop_type (self_type);
2408 method_type = promote_type (TREE_TYPE (method_type));
2409 push_value (convert (method_type, integer_zero_node));
2410 return;
2413 method_type = TREE_TYPE (method);
2414 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2415 flush_quick_stack ();
2417 func = NULL_TREE;
2418 if (opcode == OPCODE_invokestatic)
2419 func = build_known_method_ref (method, method_type, self_type,
2420 method_signature, arg_list);
2421 else if (opcode == OPCODE_invokespecial
2422 || (opcode == OPCODE_invokevirtual
2423 && (METHOD_PRIVATE (method)
2424 || METHOD_FINAL (method)
2425 || CLASS_FINAL (TYPE_NAME (self_type)))))
2427 /* If the object for the method call is null, we throw an
2428 exception. We don't do this if the object is the current
2429 method's `this'. In other cases we just rely on an
2430 optimization pass to eliminate redundant checks. FIXME:
2431 Unfortunately there doesn't seem to be a way to determine
2432 what the current method is right now.
2433 We do omit the check if we're calling <init>. */
2434 /* We use a SAVE_EXPR here to make sure we only evaluate
2435 the new `self' expression once. */
2436 tree save_arg = save_expr (TREE_VALUE (arg_list));
2437 TREE_VALUE (arg_list) = save_arg;
2438 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2439 func = build_known_method_ref (method, method_type, self_type,
2440 method_signature, arg_list);
2442 else
2444 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2445 arg_list);
2446 if (opcode == OPCODE_invokevirtual)
2447 func = build_invokevirtual (dtable, method);
2448 else
2449 func = build_invokeinterface (dtable, method);
2452 if (TREE_CODE (func) == ADDR_EXPR)
2453 TREE_TYPE (func) = build_pointer_type (method_type);
2454 else
2455 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2457 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2458 func, arg_list, NULL_TREE);
2459 TREE_SIDE_EFFECTS (call) = 1;
2460 call = check_for_builtin (method, call);
2462 if (check != NULL_TREE)
2464 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2465 TREE_SIDE_EFFECTS (call) = 1;
2468 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2469 java_add_stmt (call);
2470 else
2472 push_value (call);
2473 flush_quick_stack ();
2477 /* Create a stub which will be put into the vtable but which will call
2478 a JNI function. */
2480 tree
2481 build_jni_stub (tree method)
2483 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2484 tree jni_func_type, tem;
2485 tree env_var, res_var = NULL_TREE, block;
2486 tree method_args, res_type;
2487 tree meth_var;
2488 tree bind;
2490 int args_size = 0;
2492 tree klass = DECL_CONTEXT (method);
2493 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2494 klass = build_class_ref (klass);
2496 if (! METHOD_NATIVE (method) || ! flag_jni)
2497 abort ();
2499 DECL_ARTIFICIAL (method) = 1;
2500 DECL_EXTERNAL (method) = 0;
2502 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2503 DECL_CONTEXT (env_var) = method;
2505 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2507 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2508 TREE_TYPE (TREE_TYPE (method)));
2509 DECL_CONTEXT (res_var) = method;
2510 TREE_CHAIN (env_var) = res_var;
2513 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2514 TREE_STATIC (meth_var) = 1;
2515 TREE_PUBLIC (meth_var) = 0;
2516 DECL_EXTERNAL (meth_var) = 0;
2517 DECL_CONTEXT (meth_var) = method;
2518 DECL_ARTIFICIAL (meth_var) = 1;
2519 DECL_INITIAL (meth_var) = null_pointer_node;
2520 TREE_USED (meth_var) = 1;
2521 chainon (env_var, meth_var);
2522 build_result_decl (method);
2524 /* One strange way that the front ends are different is that they
2525 store arguments differently. */
2526 if (from_class)
2527 method_args = DECL_ARGUMENTS (method);
2528 else
2529 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2530 block = build_block (env_var, NULL_TREE, NULL_TREE,
2531 method_args, NULL_TREE);
2532 TREE_SIDE_EFFECTS (block) = 1;
2533 /* When compiling from source we don't set the type of the block,
2534 because that will prevent patch_return from ever being run. */
2535 if (from_class)
2536 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2538 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2539 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2540 build3 (CALL_EXPR, ptr_type_node,
2541 build_address_of (soft_getjnienvnewframe_node),
2542 build_tree_list (NULL_TREE, klass),
2543 NULL_TREE));
2544 CAN_COMPLETE_NORMALLY (body) = 1;
2546 /* All the arguments to this method become arguments to the
2547 underlying JNI function. If we had to wrap object arguments in a
2548 special way, we would do that here. */
2549 args = NULL_TREE;
2550 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2552 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2553 #ifdef PARM_BOUNDARY
2554 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2555 * PARM_BOUNDARY);
2556 #endif
2557 args_size += (arg_bits / BITS_PER_UNIT);
2559 args = tree_cons (NULL_TREE, tem, args);
2561 args = nreverse (args);
2562 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2564 /* For a static method the second argument is the class. For a
2565 non-static method the second argument is `this'; that is already
2566 available in the argument list. */
2567 if (METHOD_STATIC (method))
2569 args_size += int_size_in_bytes (TREE_TYPE (klass));
2570 args = tree_cons (NULL_TREE, klass, args);
2571 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2574 /* The JNIEnv structure is the first argument to the JNI function. */
2575 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2576 args = tree_cons (NULL_TREE, env_var, args);
2577 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2579 /* We call _Jv_LookupJNIMethod to find the actual underlying
2580 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2581 exception if this function is not found at runtime. */
2582 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2583 method_sig = build_java_signature (TREE_TYPE (method));
2584 lookup_arg = tree_cons (NULL_TREE,
2585 build_utf8_ref (unmangle_classname
2586 (IDENTIFIER_POINTER (method_sig),
2587 IDENTIFIER_LENGTH (method_sig))),
2588 tem);
2589 tem = DECL_NAME (method);
2590 lookup_arg
2591 = tree_cons (NULL_TREE, klass,
2592 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2594 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2596 #ifdef MODIFY_JNI_METHOD_CALL
2597 tem = MODIFY_JNI_METHOD_CALL (tem);
2598 #endif
2600 jni_func_type = build_pointer_type (tem);
2602 jnifunc = build3 (COND_EXPR, ptr_type_node,
2603 meth_var, meth_var,
2604 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2605 build3 (CALL_EXPR, ptr_type_node,
2606 build_address_of
2607 (soft_lookupjnimethod_node),
2608 lookup_arg, NULL_TREE)));
2610 /* Now we make the actual JNI call via the resulting function
2611 pointer. */
2612 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2613 build1 (NOP_EXPR, jni_func_type, jnifunc),
2614 args, NULL_TREE);
2616 /* If the JNI call returned a result, capture it here. If we had to
2617 unwrap JNI object results, we would do that here. */
2618 if (res_var != NULL_TREE)
2619 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2620 res_var, call);
2622 TREE_SIDE_EFFECTS (call) = 1;
2623 CAN_COMPLETE_NORMALLY (call) = 1;
2625 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2626 TREE_SIDE_EFFECTS (body) = 1;
2628 /* Now free the environment we allocated. */
2629 call = build3 (CALL_EXPR, ptr_type_node,
2630 build_address_of (soft_jnipopsystemframe_node),
2631 build_tree_list (NULL_TREE, env_var),
2632 NULL_TREE);
2633 TREE_SIDE_EFFECTS (call) = 1;
2634 CAN_COMPLETE_NORMALLY (call) = 1;
2635 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2636 TREE_SIDE_EFFECTS (body) = 1;
2638 /* Finally, do the return. */
2639 res_type = void_type_node;
2640 if (res_var != NULL_TREE)
2642 tree drt;
2643 if (! DECL_RESULT (method))
2644 abort ();
2645 /* Make sure we copy the result variable to the actual
2646 result. We use the type of the DECL_RESULT because it
2647 might be different from the return type of the function:
2648 it might be promoted. */
2649 drt = TREE_TYPE (DECL_RESULT (method));
2650 if (drt != TREE_TYPE (res_var))
2651 res_var = build1 (CONVERT_EXPR, drt, res_var);
2652 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2653 TREE_SIDE_EFFECTS (res_var) = 1;
2656 body = build2 (COMPOUND_EXPR, void_type_node, body,
2657 build1 (RETURN_EXPR, res_type, res_var));
2658 TREE_SIDE_EFFECTS (body) = 1;
2660 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2661 body, block);
2662 return bind;
2665 /* Expand an operation to extract from or store into a field.
2666 IS_STATIC is 1 iff the field is static.
2667 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2668 FIELD_REF_INDEX is an index into the constant pool. */
2670 static void
2671 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2673 tree self_type
2674 = get_class_constant (current_jcf,
2675 COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2676 field_ref_index));
2677 const char *self_name
2678 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2679 tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2680 tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
2681 field_ref_index);
2682 tree field_type = get_type_from_signature (field_signature);
2683 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2684 tree field_ref;
2685 int is_error = 0;
2686 tree original_self_type = self_type;
2687 tree field_decl;
2689 if (! CLASS_LOADED_P (self_type))
2690 load_class (self_type, 1);
2691 field_decl = lookup_field (&self_type, field_name);
2692 if (field_decl == error_mark_node)
2694 is_error = 1;
2696 else if (field_decl == NULL_TREE)
2698 if (! flag_verify_invocations)
2700 int flags = ACC_PUBLIC;
2701 if (is_static)
2702 flags |= ACC_STATIC;
2703 self_type = original_self_type;
2704 field_decl = add_field (original_self_type, field_name,
2705 field_type, flags);
2706 DECL_ARTIFICIAL (field_decl) = 1;
2707 DECL_IGNORED_P (field_decl) = 1;
2709 else
2711 error ("missing field '%s' in '%s'",
2712 IDENTIFIER_POINTER (field_name), self_name);
2713 is_error = 1;
2716 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2718 error ("mismatching signature for field '%s' in '%s'",
2719 IDENTIFIER_POINTER (field_name), self_name);
2720 is_error = 1;
2722 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2723 if (is_error)
2725 if (! is_putting)
2726 push_value (convert (field_type, integer_zero_node));
2727 flush_quick_stack ();
2728 return;
2731 field_ref = build_field_ref (field_ref, self_type, field_name);
2732 if (is_static)
2733 field_ref = build_class_init (self_type, field_ref);
2734 if (is_putting)
2736 flush_quick_stack ();
2737 if (FIELD_FINAL (field_decl))
2739 if (DECL_CONTEXT (field_decl) != current_class)
2740 error ("%Jassignment to final field '%D' not in field's class",
2741 field_decl, field_decl);
2742 else if (FIELD_STATIC (field_decl))
2744 if (!DECL_CLINIT_P (current_function_decl))
2745 warning ("%Jassignment to final static field %qD not in "
2746 "class initializer",
2747 field_decl, field_decl);
2749 else
2751 tree cfndecl_name = DECL_NAME (current_function_decl);
2752 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2753 && !ID_FINIT_P (cfndecl_name))
2754 warning ("%Jassignment to final field '%D' not in constructor",
2755 field_decl, field_decl);
2758 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2759 field_ref, new_value));
2761 else
2762 push_value (field_ref);
2765 void
2766 load_type_state (tree label)
2768 int i;
2769 tree vec = LABEL_TYPE_STATE (label);
2770 int cur_length = TREE_VEC_LENGTH (vec);
2771 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2772 for (i = 0; i < cur_length; i++)
2773 type_map [i] = TREE_VEC_ELT (vec, i);
2776 /* Go over METHOD's bytecode and note instruction starts in
2777 instruction_bits[]. */
2779 void
2780 note_instructions (JCF *jcf, tree method)
2782 int PC;
2783 unsigned char* byte_ops;
2784 long length = DECL_CODE_LENGTH (method);
2786 int saw_index;
2787 jint INT_temp;
2789 #undef RET /* Defined by config/i386/i386.h */
2790 #undef PTR
2791 #define BCODE byte_ops
2792 #define BYTE_type_node byte_type_node
2793 #define SHORT_type_node short_type_node
2794 #define INT_type_node int_type_node
2795 #define LONG_type_node long_type_node
2796 #define CHAR_type_node char_type_node
2797 #define PTR_type_node ptr_type_node
2798 #define FLOAT_type_node float_type_node
2799 #define DOUBLE_type_node double_type_node
2800 #define VOID_type_node void_type_node
2801 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2802 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2803 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2804 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2806 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2808 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2809 byte_ops = jcf->read_ptr;
2810 instruction_bits = xrealloc (instruction_bits, length + 1);
2811 memset (instruction_bits, 0, length + 1);
2813 /* This pass figures out which PC can be the targets of jumps. */
2814 for (PC = 0; PC < length;)
2816 int oldpc = PC; /* PC at instruction start. */
2817 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2818 switch (byte_ops[PC++])
2820 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2821 case OPCODE: \
2822 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2823 break;
2825 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2827 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2828 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2829 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2830 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2831 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2832 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2833 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2834 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2836 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2837 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2838 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2839 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2840 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2841 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2842 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2843 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2845 /* two forms of wide instructions */
2846 #define PRE_SPECIAL_WIDE(IGNORE) \
2848 int modified_opcode = IMMEDIATE_u1; \
2849 if (modified_opcode == OPCODE_iinc) \
2851 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2852 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2854 else \
2856 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2860 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2862 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2864 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2865 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2866 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2867 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2868 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2869 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2870 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2871 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2872 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2873 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2875 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2876 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2877 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2878 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2879 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2880 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2881 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2882 NOTE_LABEL (PC); \
2883 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2885 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2887 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2888 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2890 #define PRE_LOOKUP_SWITCH \
2891 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2892 NOTE_LABEL (default_offset+oldpc); \
2893 if (npairs >= 0) \
2894 while (--npairs >= 0) { \
2895 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2896 jint offset = IMMEDIATE_s4; \
2897 NOTE_LABEL (offset+oldpc); } \
2900 #define PRE_TABLE_SWITCH \
2901 { jint default_offset = IMMEDIATE_s4; \
2902 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2903 NOTE_LABEL (default_offset+oldpc); \
2904 if (low <= high) \
2905 while (low++ <= high) { \
2906 jint offset = IMMEDIATE_s4; \
2907 NOTE_LABEL (offset+oldpc); } \
2910 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2911 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2912 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2913 (void)(IMMEDIATE_u2); \
2914 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2916 #include "javaop.def"
2917 #undef JAVAOP
2919 } /* for */
2922 void
2923 expand_byte_code (JCF *jcf, tree method)
2925 int PC;
2926 int i;
2927 const unsigned char *linenumber_pointer;
2928 int dead_code_index = -1;
2929 unsigned char* byte_ops;
2930 long length = DECL_CODE_LENGTH (method);
2932 stack_pointer = 0;
2933 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2934 byte_ops = jcf->read_ptr;
2936 /* We make an initial pass of the line number table, to note
2937 which instructions have associated line number entries. */
2938 linenumber_pointer = linenumber_table;
2939 for (i = 0; i < linenumber_count; i++)
2941 int pc = GET_u2 (linenumber_pointer);
2942 linenumber_pointer += 4;
2943 if (pc >= length)
2944 warning ("invalid PC in line number table");
2945 else
2947 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2948 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2949 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2953 if (flag_new_verifier)
2955 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2956 return;
2958 else
2960 if (! verify_jvm_instructions (jcf, byte_ops, length))
2961 return;
2964 /* Translate bytecodes. */
2965 linenumber_pointer = linenumber_table;
2966 for (PC = 0; PC < length;)
2968 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2970 tree label = lookup_label (PC);
2971 flush_quick_stack ();
2972 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2973 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2974 if (LABEL_VERIFIED (label) || PC == 0)
2975 load_type_state (label);
2978 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2980 if (dead_code_index == -1)
2982 /* This is the start of a region of unreachable bytecodes.
2983 They still need to be processed in order for EH ranges
2984 to get handled correctly. However, we can simply
2985 replace these bytecodes with nops. */
2986 dead_code_index = PC;
2989 /* Turn this bytecode into a nop. */
2990 byte_ops[PC] = 0x0;
2992 else
2994 if (dead_code_index != -1)
2996 /* We've just reached the end of a region of dead code. */
2997 if (extra_warnings)
2998 warning ("unreachable bytecode from %d to before %d",
2999 dead_code_index, PC);
3000 dead_code_index = -1;
3004 /* Handle possible line number entry for this PC.
3006 This code handles out-of-order and multiple linenumbers per PC,
3007 but is optimized for the case of line numbers increasing
3008 monotonically with PC. */
3009 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
3011 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
3012 || GET_u2 (linenumber_pointer) != PC)
3013 linenumber_pointer = linenumber_table;
3014 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
3016 int pc = GET_u2 (linenumber_pointer);
3017 linenumber_pointer += 4;
3018 if (pc == PC)
3020 int line = GET_u2 (linenumber_pointer - 2);
3021 #ifdef USE_MAPPED_LOCATION
3022 input_location = linemap_line_start (&line_table, line, 1);
3023 #else
3024 input_location.line = line;
3025 #endif
3026 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
3027 break;
3031 maybe_pushlevels (PC);
3032 PC = process_jvm_instruction (PC, byte_ops, length);
3033 maybe_poplevels (PC);
3034 } /* for */
3036 if (dead_code_index != -1)
3038 /* We've just reached the end of a region of dead code. */
3039 if (extra_warnings)
3040 warning ("unreachable bytecode from %d to the end of the method",
3041 dead_code_index);
3045 static void
3046 java_push_constant_from_pool (JCF *jcf, int index)
3048 tree c;
3049 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3051 tree name;
3052 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3053 index = alloc_name_constant (CONSTANT_String, name);
3054 c = build_ref_from_constant_pool (index);
3055 c = convert (promote_type (string_type_node), c);
3057 else
3058 c = get_constant (jcf, index);
3059 push_value (c);
3063 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3064 long length ATTRIBUTE_UNUSED)
3066 const char *opname; /* Temporary ??? */
3067 int oldpc = PC; /* PC at instruction start. */
3069 /* If the instruction is at the beginning of a exception handler,
3070 replace the top of the stack with the thrown object reference */
3071 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3073 /* Note that the new verifier will not emit a type map at all
3074 for dead exception handlers. In this case we just ignore
3075 the situation. */
3076 if (! flag_new_verifier || (instruction_bits[PC] & BCODE_VERIFIED) != 0)
3078 tree type = pop_type (promote_type (throwable_type_node));
3079 push_value (build_exception_object_ref (type));
3083 switch (byte_ops[PC++])
3085 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3086 case OPCODE: \
3087 opname = #OPNAME; \
3088 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3089 break;
3091 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3093 int saw_index = 0; \
3094 int index = OPERAND_VALUE; \
3095 build_java_ret \
3096 (find_local_variable (index, return_address_type_node, oldpc)); \
3099 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3101 /* OPERAND_VALUE may have side-effects on PC */ \
3102 int opvalue = OPERAND_VALUE; \
3103 build_java_jsr (oldpc + opvalue, PC); \
3106 /* Push a constant onto the stack. */
3107 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3108 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3109 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3110 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3112 /* internal macro added for use by the WIDE case */
3113 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3114 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3116 /* Push local variable onto the opcode stack. */
3117 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3119 /* have to do this since OPERAND_VALUE may have side-effects */ \
3120 int opvalue = OPERAND_VALUE; \
3121 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3124 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3125 expand_java_return (OPERAND_TYPE##_type_node)
3127 #define REM_EXPR TRUNC_MOD_EXPR
3128 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3129 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3131 #define FIELD(IS_STATIC, IS_PUT) \
3132 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3134 #define TEST(OPERAND_TYPE, CONDITION) \
3135 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3137 #define COND(OPERAND_TYPE, CONDITION) \
3138 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3140 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3141 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3143 #define BRANCH_GOTO(OPERAND_VALUE) \
3144 expand_java_goto (oldpc + OPERAND_VALUE)
3146 #define BRANCH_CALL(OPERAND_VALUE) \
3147 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3149 #if 0
3150 #define BRANCH_RETURN(OPERAND_VALUE) \
3152 tree type = OPERAND_TYPE##_type_node; \
3153 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3154 expand_java_ret (value); \
3156 #endif
3158 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3159 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3160 fprintf (stderr, "(not implemented)\n")
3161 #define NOT_IMPL1(OPERAND_VALUE) \
3162 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3163 fprintf (stderr, "(not implemented)\n")
3165 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3167 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3169 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3171 #define STACK_SWAP(COUNT) java_stack_swap()
3173 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3174 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3175 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3177 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3178 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3180 #define LOOKUP_SWITCH \
3181 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3182 tree selector = pop_value (INT_type_node); \
3183 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3184 while (--npairs >= 0) \
3186 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3187 expand_java_add_case (switch_expr, match, oldpc + offset); \
3191 #define TABLE_SWITCH \
3192 { jint default_offset = IMMEDIATE_s4; \
3193 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3194 tree selector = pop_value (INT_type_node); \
3195 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3196 for (; low <= high; low++) \
3198 jint offset = IMMEDIATE_s4; \
3199 expand_java_add_case (switch_expr, low, oldpc + offset); \
3203 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3204 { int opcode = byte_ops[PC-1]; \
3205 int method_ref_index = IMMEDIATE_u2; \
3206 int nargs; \
3207 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3208 else nargs = -1; \
3209 expand_invoke (opcode, method_ref_index, nargs); \
3212 /* Handle new, checkcast, instanceof */
3213 #define OBJECT(TYPE, OP) \
3214 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3216 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3218 #define ARRAY_LOAD(OPERAND_TYPE) \
3220 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3223 #define ARRAY_STORE(OPERAND_TYPE) \
3225 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3228 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3229 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3230 #define ARRAY_NEW_PTR() \
3231 push_value (build_anewarray (get_class_constant (current_jcf, \
3232 IMMEDIATE_u2), \
3233 pop_value (int_type_node)));
3234 #define ARRAY_NEW_NUM() \
3236 int atype = IMMEDIATE_u1; \
3237 push_value (build_newarray (atype, pop_value (int_type_node)));\
3239 #define ARRAY_NEW_MULTI() \
3241 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3242 int ndims = IMMEDIATE_u1; \
3243 expand_java_multianewarray( class, ndims ); \
3246 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3247 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3248 pop_value (OPERAND_TYPE##_type_node))));
3250 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3252 push_value (build1 (NOP_EXPR, int_type_node, \
3253 (convert (TO_TYPE##_type_node, \
3254 pop_value (FROM_TYPE##_type_node))))); \
3257 #define CONVERT(FROM_TYPE, TO_TYPE) \
3259 push_value (convert (TO_TYPE##_type_node, \
3260 pop_value (FROM_TYPE##_type_node))); \
3263 /* internal macro added for use by the WIDE case
3264 Added TREE_TYPE (decl) assignment, apbianco */
3265 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3267 tree decl, value; \
3268 int index = OPVALUE; \
3269 tree type = OPTYPE; \
3270 value = pop_value (type); \
3271 type = TREE_TYPE (value); \
3272 decl = find_local_variable (index, type, oldpc); \
3273 set_local_type (index, type); \
3274 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3275 update_aliases (decl, index, PC); \
3278 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3280 /* have to do this since OPERAND_VALUE may have side-effects */ \
3281 int opvalue = OPERAND_VALUE; \
3282 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3285 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3286 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3288 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3289 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3291 #define MONITOR_OPERATION(call) \
3293 tree o = pop_value (ptr_type_node); \
3294 tree c; \
3295 flush_quick_stack (); \
3296 c = build_java_monitor (call, o); \
3297 TREE_SIDE_EFFECTS (c) = 1; \
3298 java_add_stmt (c); \
3301 #define SPECIAL_IINC(IGNORED) \
3303 unsigned int local_var_index = IMMEDIATE_u1; \
3304 int ival = IMMEDIATE_s1; \
3305 expand_iinc(local_var_index, ival, oldpc); \
3308 #define SPECIAL_WIDE(IGNORED) \
3310 int modified_opcode = IMMEDIATE_u1; \
3311 unsigned int local_var_index = IMMEDIATE_u2; \
3312 switch (modified_opcode) \
3314 case OPCODE_iinc: \
3316 int ival = IMMEDIATE_s2; \
3317 expand_iinc (local_var_index, ival, oldpc); \
3318 break; \
3320 case OPCODE_iload: \
3321 case OPCODE_lload: \
3322 case OPCODE_fload: \
3323 case OPCODE_dload: \
3324 case OPCODE_aload: \
3326 /* duplicate code from LOAD macro */ \
3327 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3328 break; \
3330 case OPCODE_istore: \
3331 case OPCODE_lstore: \
3332 case OPCODE_fstore: \
3333 case OPCODE_dstore: \
3334 case OPCODE_astore: \
3336 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3337 break; \
3339 default: \
3340 error ("unrecogized wide sub-instruction"); \
3344 #define SPECIAL_THROW(IGNORED) \
3345 build_java_athrow (pop_value (throwable_type_node))
3347 #define SPECIAL_BREAK NOT_IMPL1
3348 #define IMPL NOT_IMPL
3350 #include "javaop.def"
3351 #undef JAVAOP
3352 default:
3353 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3355 return PC;
3358 /* Return the opcode at PC in the code section pointed to by
3359 CODE_OFFSET. */
3361 static unsigned char
3362 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3364 unsigned char opcode;
3365 long absolute_offset = (long)JCF_TELL (jcf);
3367 JCF_SEEK (jcf, code_offset);
3368 opcode = jcf->read_ptr [pc];
3369 JCF_SEEK (jcf, absolute_offset);
3370 return opcode;
3373 /* Some bytecode compilers are emitting accurate LocalVariableTable
3374 attributes. Here's an example:
3376 PC <t>store_<n>
3377 PC+1 ...
3379 Attribute "LocalVariableTable"
3380 slot #<n>: ... (PC: PC+1 length: L)
3382 This is accurate because the local in slot <n> really exists after
3383 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3385 This procedure recognizes this situation and extends the live range
3386 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3387 length of the store instruction.)
3389 This function is used by `give_name_to_locals' so that a local's
3390 DECL features a DECL_LOCAL_START_PC such that the first related
3391 store operation will use DECL as a destination, not a unrelated
3392 temporary created for the occasion.
3394 This function uses a global (instruction_bits) `note_instructions' should
3395 have allocated and filled properly. */
3398 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3399 int start_pc, int slot)
3401 int first, index, opcode;
3402 int pc, insn_pc;
3403 int wide_found = 0;
3405 if (!start_pc)
3406 return start_pc;
3408 first = index = -1;
3410 /* Find last previous instruction and remember it */
3411 for (pc = start_pc-1; pc; pc--)
3412 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3413 break;
3414 insn_pc = pc;
3416 /* Retrieve the instruction, handle `wide'. */
3417 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3418 if (opcode == OPCODE_wide)
3420 wide_found = 1;
3421 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3424 switch (opcode)
3426 case OPCODE_astore_0:
3427 case OPCODE_astore_1:
3428 case OPCODE_astore_2:
3429 case OPCODE_astore_3:
3430 first = OPCODE_astore_0;
3431 break;
3433 case OPCODE_istore_0:
3434 case OPCODE_istore_1:
3435 case OPCODE_istore_2:
3436 case OPCODE_istore_3:
3437 first = OPCODE_istore_0;
3438 break;
3440 case OPCODE_lstore_0:
3441 case OPCODE_lstore_1:
3442 case OPCODE_lstore_2:
3443 case OPCODE_lstore_3:
3444 first = OPCODE_lstore_0;
3445 break;
3447 case OPCODE_fstore_0:
3448 case OPCODE_fstore_1:
3449 case OPCODE_fstore_2:
3450 case OPCODE_fstore_3:
3451 first = OPCODE_fstore_0;
3452 break;
3454 case OPCODE_dstore_0:
3455 case OPCODE_dstore_1:
3456 case OPCODE_dstore_2:
3457 case OPCODE_dstore_3:
3458 first = OPCODE_dstore_0;
3459 break;
3461 case OPCODE_astore:
3462 case OPCODE_istore:
3463 case OPCODE_lstore:
3464 case OPCODE_fstore:
3465 case OPCODE_dstore:
3466 index = peek_opcode_at_pc (jcf, code_offset, pc);
3467 if (wide_found)
3469 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3470 index = (other << 8) + index;
3472 break;
3475 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3476 means we have a <t>store. */
3477 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3478 start_pc = insn_pc;
3480 return start_pc;
3483 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3484 order, as specified by Java Language Specification.
3486 The problem is that while expand_expr will evaluate its sub-operands in
3487 left-to-right order, for variables it will just return an rtx (i.e.
3488 an lvalue) for the variable (rather than an rvalue). So it is possible
3489 that a later sub-operand will change the register, and when the
3490 actual operation is done, it will use the new value, when it should
3491 have used the original value.
3493 We fix this by using save_expr. This forces the sub-operand to be
3494 copied into a fresh virtual register,
3496 For method invocation, we modify the arguments so that a
3497 left-to-right order evaluation is performed. Saved expressions
3498 will, in CALL_EXPR order, be reused when the call will be expanded.
3501 tree
3502 force_evaluation_order (tree node)
3504 if (flag_syntax_only)
3505 return node;
3506 if (TREE_CODE (node) == CALL_EXPR
3507 || TREE_CODE (node) == NEW_CLASS_EXPR
3508 || (TREE_CODE (node) == COMPOUND_EXPR
3509 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3510 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3512 tree arg, cmp;
3514 arg = node;
3516 /* Position arg properly, account for wrapped around ctors. */
3517 if (TREE_CODE (node) == COMPOUND_EXPR)
3518 arg = TREE_OPERAND (node, 0);
3520 arg = TREE_OPERAND (arg, 1);
3522 /* An empty argument list is ok, just ignore it. */
3523 if (!arg)
3524 return node;
3526 /* Not having a list of arguments here is an error. */
3527 if (TREE_CODE (arg) != TREE_LIST)
3528 abort ();
3530 /* This reverses the evaluation order. This is a desired effect. */
3531 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3533 tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3534 cmp = (cmp == NULL_TREE ? saved :
3535 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3536 TREE_VALUE (arg) = saved;
3539 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3540 TREE_SIDE_EFFECTS (cmp) = 1;
3542 if (cmp)
3544 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3545 if (TREE_TYPE (cmp) != void_type_node)
3546 cmp = save_expr (cmp);
3547 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3548 TREE_SIDE_EFFECTS (cmp) = 1;
3549 node = cmp;
3552 return node;
3555 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3556 location where an expression or an identifier were encountered. It
3557 is necessary for languages where the frontend parser will handle
3558 recursively more than one file (Java is one of them). */
3560 tree
3561 build_expr_wfl (tree node,
3562 #ifdef USE_MAPPED_LOCATION
3563 source_location location
3564 #else
3565 const char *file, int line, int col
3566 #endif
3569 tree wfl;
3571 #ifdef USE_MAPPED_LOCATION
3572 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3573 SET_EXPR_LOCATION (wfl, location);
3574 #else
3575 static const char *last_file = 0;
3576 static tree last_filenode = NULL_TREE;
3578 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3580 EXPR_WFL_SET_LINECOL (wfl, line, col);
3581 if (file != last_file)
3583 last_file = file;
3584 last_filenode = file ? get_identifier (file) : NULL_TREE;
3586 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3587 #endif
3588 EXPR_WFL_NODE (wfl) = node;
3589 if (node)
3591 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3592 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3593 TREE_TYPE (wfl) = TREE_TYPE (node);
3596 return wfl;
3599 #ifdef USE_MAPPED_LOCATION
3600 tree
3601 expr_add_location (tree node, source_location location, bool statement)
3603 tree wfl;
3604 #if 0
3605 /* FIXME. This optimization causes failures in code that expects an
3606 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3607 if (node && ! (statement && flag_emit_class_files))
3609 source_location node_loc = EXPR_LOCATION (node);
3610 if (node_loc == location || location == UNKNOWN_LOCATION)
3611 return node;
3612 if (node_loc == UNKNOWN_LOCATION
3613 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3615 SET_EXPR_LOCATION (node, location);
3616 return node;
3619 #endif
3620 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3621 SET_EXPR_LOCATION (wfl, location);
3622 EXPR_WFL_NODE (wfl) = node;
3623 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3624 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3625 if (node)
3627 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3628 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3629 TREE_TYPE (wfl) = TREE_TYPE (node);
3632 return wfl;
3634 #endif
3636 /* Build a node to represent empty statements and blocks. */
3638 tree
3639 build_java_empty_stmt (void)
3641 tree t = build_empty_stmt ();
3642 CAN_COMPLETE_NORMALLY (t) = 1;
3643 return t;
3646 #include "gt-java-expr.h"