Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / java / expr.c
blob7d515052ba0a5e6d4ce7fcc331d228e4579b4696
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);
89 static void promote_arguments (void);
91 static GTY(()) tree operand_type[59];
93 static GTY(()) tree methods_ident;
94 static GTY(()) tree ncode_ident;
95 tree dtable_ident = NULL_TREE;
97 /* Set to nonzero value in order to emit class initialization code
98 before static field references. */
99 int always_initialize_class_p = 0;
101 /* We store the stack state in two places:
102 Within a basic block, we use the quick_stack, which is a
103 pushdown list (TREE_LISTs) of expression nodes.
104 This is the top part of the stack; below that we use find_stack_slot.
105 At the end of a basic block, the quick_stack must be flushed
106 to the stack slot array (as handled by find_stack_slot).
107 Using quick_stack generates better code (especially when
108 compiled without optimization), because we do not have to
109 explicitly store and load trees to temporary variables.
111 If a variable is on the quick stack, it means the value of variable
112 when the quick stack was last flushed. Conceptually, flush_quick_stack
113 saves all the quick_stack elements in parallel. However, that is
114 complicated, so it actually saves them (i.e. copies each stack value
115 to is home virtual register) from low indexes. This allows a quick_stack
116 element at index i (counting from the bottom of stack the) to references
117 slot virtuals for register that are >= i, but not those that are deeper.
118 This convention makes most operations easier. For example iadd works
119 even when the stack contains (reg[0], reg[1]): It results in the
120 stack containing (reg[0]+reg[1]), which is OK. However, some stack
121 operations are more complicated. For example dup given a stack
122 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
123 the convention, since stack value 1 would refer to a register with
124 lower index (reg[0]), which flush_quick_stack does not safely handle.
125 So dup cannot just add an extra element to the quick_stack, but iadd can.
128 static GTY(()) tree quick_stack;
130 /* A free-list of unused permanent TREE_LIST nodes. */
131 static GTY((deletable)) tree tree_list_free_list;
133 /* The stack pointer of the Java virtual machine.
134 This does include the size of the quick_stack. */
136 int stack_pointer;
138 const unsigned char *linenumber_table;
139 int linenumber_count;
141 void
142 init_expr_processing (void)
144 operand_type[21] = operand_type[54] = int_type_node;
145 operand_type[22] = operand_type[55] = long_type_node;
146 operand_type[23] = operand_type[56] = float_type_node;
147 operand_type[24] = operand_type[57] = double_type_node;
148 operand_type[25] = operand_type[58] = ptr_type_node;
151 tree
152 java_truthvalue_conversion (tree expr)
154 /* It is simpler and generates better code to have only TRUTH_*_EXPR
155 or comparison expressions as truth values at this level.
157 This function should normally be identity for Java. */
159 switch (TREE_CODE (expr))
161 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
162 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
163 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
164 case ORDERED_EXPR: case UNORDERED_EXPR:
165 case TRUTH_ANDIF_EXPR:
166 case TRUTH_ORIF_EXPR:
167 case TRUTH_AND_EXPR:
168 case TRUTH_OR_EXPR:
169 case TRUTH_XOR_EXPR:
170 case TRUTH_NOT_EXPR:
171 case ERROR_MARK:
172 return expr;
174 case INTEGER_CST:
175 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
177 case REAL_CST:
178 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
180 /* are these legal? XXX JH */
181 case NEGATE_EXPR:
182 case ABS_EXPR:
183 case FLOAT_EXPR:
184 /* These don't change whether an object is nonzero or zero. */
185 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
187 case COND_EXPR:
188 /* Distribute the conversion into the arms of a COND_EXPR. */
189 return fold
190 (build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
191 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
192 java_truthvalue_conversion (TREE_OPERAND (expr, 2))));
194 case NOP_EXPR:
195 /* If this is widening the argument, we can ignore it. */
196 if (TYPE_PRECISION (TREE_TYPE (expr))
197 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
198 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
199 /* fall through to default */
201 default:
202 return fold (build2 (NE_EXPR, boolean_type_node,
203 expr, boolean_false_node));
207 /* Save any stack slots that happen to be in the quick_stack into their
208 home virtual register slots.
210 The copy order is from low stack index to high, to support the invariant
211 that the expression for a slot may contain decls for stack slots with
212 higher (or the same) index, but not lower. */
214 static void
215 flush_quick_stack (void)
217 int stack_index = stack_pointer;
218 tree prev, cur, next;
220 /* First reverse the quick_stack, and count the number of slots it has. */
221 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
223 next = TREE_CHAIN (cur);
224 TREE_CHAIN (cur) = prev;
225 prev = cur;
226 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
228 quick_stack = prev;
230 while (quick_stack != NULL_TREE)
232 tree decl;
233 tree node = quick_stack, type;
234 quick_stack = TREE_CHAIN (node);
235 TREE_CHAIN (node) = tree_list_free_list;
236 tree_list_free_list = node;
237 node = TREE_VALUE (node);
238 type = TREE_TYPE (node);
240 decl = find_stack_slot (stack_index, type);
241 if (decl != node)
242 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
243 stack_index += 1 + TYPE_IS_WIDE (type);
247 /* Push TYPE on the type stack.
248 Return true on success, 0 on overflow. */
251 push_type_0 (tree type)
253 int n_words;
254 type = promote_type (type);
255 n_words = 1 + TYPE_IS_WIDE (type);
256 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
257 return 0;
258 /* Allocate decl for this variable now, so we get a temporary that
259 survives the whole method. */
260 find_stack_slot (stack_pointer, type);
261 stack_type_map[stack_pointer++] = type;
262 n_words--;
263 while (--n_words >= 0)
264 stack_type_map[stack_pointer++] = TYPE_SECOND;
265 return 1;
268 void
269 push_type (tree type)
271 if (! push_type_0 (type))
272 abort ();
275 static void
276 push_value (tree value)
278 tree type = TREE_TYPE (value);
279 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
281 type = promote_type (type);
282 value = convert (type, value);
284 push_type (type);
285 if (tree_list_free_list == NULL_TREE)
286 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
287 else
289 tree node = tree_list_free_list;
290 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
291 TREE_VALUE (node) = value;
292 TREE_CHAIN (node) = quick_stack;
293 quick_stack = node;
297 /* Pop a type from the type stack.
298 TYPE is the expected type. Return the actual type, which must be
299 convertible to TYPE.
300 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
302 tree
303 pop_type_0 (tree type, char **messagep)
305 int n_words;
306 tree t;
307 *messagep = NULL;
308 if (TREE_CODE (type) == RECORD_TYPE)
309 type = promote_type (type);
310 n_words = 1 + TYPE_IS_WIDE (type);
311 if (stack_pointer < n_words)
313 *messagep = xstrdup ("stack underflow");
314 return type;
316 while (--n_words > 0)
318 if (stack_type_map[--stack_pointer] != void_type_node)
320 *messagep = xstrdup ("Invalid multi-word value on type stack");
321 return type;
324 t = stack_type_map[--stack_pointer];
325 if (type == NULL_TREE || t == type)
326 return t;
327 if (TREE_CODE (t) == TREE_LIST)
331 tree tt = TREE_PURPOSE (t);
332 if (! can_widen_reference_to (tt, type))
334 t = tt;
335 goto fail;
337 t = TREE_CHAIN (t);
339 while (t);
340 return t;
342 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
343 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
344 return t;
345 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
347 if (flag_new_verifier)
349 /* Since the verifier has already run, we know that any
350 types we see will be compatible. In BC mode, this fact
351 may be checked at runtime, but if that is so then we can
352 assume its truth here as well. So, we always succeed
353 here, with the expected type. */
354 return type;
356 else
358 if (type == ptr_type_node || type == object_ptr_type_node)
359 return t;
360 else if (t == ptr_type_node) /* Special case for null reference. */
361 return type;
362 /* This is a kludge, but matches what Sun's verifier does.
363 It can be tricked, but is safe as long as type errors
364 (i.e. interface method calls) are caught at run-time. */
365 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
366 return object_ptr_type_node;
367 else if (can_widen_reference_to (t, type))
368 return t;
372 if (! flag_verify_invocations && flag_indirect_dispatch
373 && t == object_ptr_type_node)
375 if (type != ptr_type_node)
376 warning ("need to insert runtime check for %s",
377 xstrdup (lang_printable_name (type, 0)));
378 return type;
381 /* lang_printable_name uses a static buffer, so we must save the result
382 from calling it the first time. */
383 fail:
385 char *temp = xstrdup (lang_printable_name (type, 0));
386 *messagep = concat ("expected type '", temp,
387 "' but stack contains '", lang_printable_name (t, 0),
388 "'", NULL);
389 free (temp);
391 return type;
394 /* Pop a type from the type stack.
395 TYPE is the expected type. Return the actual type, which must be
396 convertible to TYPE, otherwise call error. */
398 tree
399 pop_type (tree type)
401 char *message = NULL;
402 type = pop_type_0 (type, &message);
403 if (message != NULL)
405 error ("%s", message);
406 free (message);
408 return type;
412 /* Return true if two type assertions are equal. */
414 static int
415 type_assertion_eq (const void * k1_p, const void * k2_p)
417 type_assertion k1 = *(type_assertion *)k1_p;
418 type_assertion k2 = *(type_assertion *)k2_p;
419 return (k1.assertion_code == k2.assertion_code
420 && k1.op1 == k2.op1
421 && k1.op2 == k2.op2);
424 /* Hash a type assertion. */
426 static hashval_t
427 type_assertion_hash (const void *p)
429 const type_assertion *k_p = p;
430 hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
431 k_p->assertion_code, 0);
432 hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
433 return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
436 /* Add an entry to the type assertion table for the given class.
437 CLASS is the class for which this assertion will be evaluated by the
438 runtime during loading/initialization.
439 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
440 OP1 and OP2 are the operands. The tree type of these arguments may be
441 specific to each assertion_code. */
443 void
444 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
446 htab_t assertions_htab;
447 type_assertion as;
448 void **as_pp;
450 assertions_htab = TYPE_ASSERTIONS (class);
451 if (assertions_htab == NULL)
453 assertions_htab = htab_create_ggc (7, type_assertion_hash,
454 type_assertion_eq, NULL);
455 TYPE_ASSERTIONS (current_class) = assertions_htab;
458 as.assertion_code = assertion_code;
459 as.op1 = op1;
460 as.op2 = op2;
462 as_pp = htab_find_slot (assertions_htab, &as, INSERT);
464 /* Don't add the same assertion twice. */
465 if (*as_pp)
466 return;
468 *as_pp = ggc_alloc (sizeof (type_assertion));
469 **(type_assertion **)as_pp = as;
473 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
474 Handles array types and interfaces. */
477 can_widen_reference_to (tree source_type, tree target_type)
479 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
480 return 1;
482 /* Get rid of pointers */
483 if (TREE_CODE (source_type) == POINTER_TYPE)
484 source_type = TREE_TYPE (source_type);
485 if (TREE_CODE (target_type) == POINTER_TYPE)
486 target_type = TREE_TYPE (target_type);
488 if (source_type == target_type)
489 return 1;
491 /* FIXME: This is very pessimistic, in that it checks everything,
492 even if we already know that the types are compatible. If we're
493 to support full Java class loader semantics, we need this.
494 However, we could do something more optimal. */
495 if (! flag_verify_invocations)
497 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
498 source_type, target_type);
500 if (!quiet_flag)
501 warning ("assert: %s is assign compatible with %s",
502 xstrdup (lang_printable_name (target_type, 0)),
503 xstrdup (lang_printable_name (source_type, 0)));
504 /* Punt everything to runtime. */
505 return 1;
508 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
510 return 1;
512 else
514 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
516 HOST_WIDE_INT source_length, target_length;
517 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
519 /* An array implements Cloneable and Serializable. */
520 tree name = DECL_NAME (TYPE_NAME (target_type));
521 return (name == java_lang_cloneable_identifier_node
522 || name == java_io_serializable_identifier_node);
524 target_length = java_array_type_length (target_type);
525 if (target_length >= 0)
527 source_length = java_array_type_length (source_type);
528 if (source_length != target_length)
529 return 0;
531 source_type = TYPE_ARRAY_ELEMENT (source_type);
532 target_type = TYPE_ARRAY_ELEMENT (target_type);
533 if (source_type == target_type)
534 return 1;
535 if (TREE_CODE (source_type) != POINTER_TYPE
536 || TREE_CODE (target_type) != POINTER_TYPE)
537 return 0;
538 return can_widen_reference_to (source_type, target_type);
540 else
542 int source_depth = class_depth (source_type);
543 int target_depth = class_depth (target_type);
545 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
547 if (! quiet_flag)
548 warning ("assert: %s is assign compatible with %s",
549 xstrdup (lang_printable_name (target_type, 0)),
550 xstrdup (lang_printable_name (source_type, 0)));
551 return 1;
554 /* class_depth can return a negative depth if an error occurred */
555 if (source_depth < 0 || target_depth < 0)
556 return 0;
558 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
560 /* target_type is OK if source_type or source_type ancestors
561 implement target_type. We handle multiple sub-interfaces */
562 tree binfo, base_binfo;
563 int i;
565 for (binfo = TYPE_BINFO (source_type), i = 0;
566 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
567 if (can_widen_reference_to
568 (BINFO_TYPE (base_binfo), target_type))
569 return 1;
571 if (!i)
572 return 0;
575 for ( ; source_depth > target_depth; source_depth--)
577 source_type
578 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
580 return source_type == target_type;
585 static tree
586 pop_value (tree type)
588 type = pop_type (type);
589 if (quick_stack)
591 tree node = quick_stack;
592 quick_stack = TREE_CHAIN (quick_stack);
593 TREE_CHAIN (node) = tree_list_free_list;
594 tree_list_free_list = node;
595 node = TREE_VALUE (node);
596 return node;
598 else
599 return find_stack_slot (stack_pointer, promote_type (type));
603 /* Pop and discard the top COUNT stack slots. */
605 static void
606 java_stack_pop (int count)
608 while (count > 0)
610 tree type, val;
612 if (stack_pointer == 0)
613 abort ();
615 type = stack_type_map[stack_pointer - 1];
616 if (type == TYPE_SECOND)
618 count--;
619 if (stack_pointer == 1 || count <= 0)
620 abort ();
622 type = stack_type_map[stack_pointer - 2];
624 val = pop_value (type);
625 count--;
629 /* Implement the 'swap' operator (to swap two top stack slots). */
631 static void
632 java_stack_swap (void)
634 tree type1, type2;
635 tree temp;
636 tree decl1, decl2;
638 if (stack_pointer < 2
639 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
640 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
641 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
642 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
643 /* Bad stack swap. */
644 abort ();
646 flush_quick_stack ();
647 decl1 = find_stack_slot (stack_pointer - 1, type1);
648 decl2 = find_stack_slot (stack_pointer - 2, type2);
649 temp = build_decl (VAR_DECL, NULL_TREE, type1);
650 java_add_local_var (temp);
651 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
652 java_add_stmt (build2 (MODIFY_EXPR, type2,
653 find_stack_slot (stack_pointer - 1, type2),
654 decl2));
655 java_add_stmt (build2 (MODIFY_EXPR, type1,
656 find_stack_slot (stack_pointer - 2, type1),
657 temp));
658 stack_type_map[stack_pointer - 1] = type2;
659 stack_type_map[stack_pointer - 2] = type1;
662 static void
663 java_stack_dup (int size, int offset)
665 int low_index = stack_pointer - size - offset;
666 int dst_index;
667 if (low_index < 0)
668 error ("stack underflow - dup* operation");
670 flush_quick_stack ();
672 stack_pointer += size;
673 dst_index = stack_pointer;
675 for (dst_index = stack_pointer; --dst_index >= low_index; )
677 tree type;
678 int src_index = dst_index - size;
679 if (src_index < low_index)
680 src_index = dst_index + size + offset;
681 type = stack_type_map [src_index];
682 if (type == TYPE_SECOND)
684 if (src_index <= low_index)
685 /* Dup operation splits 64-bit number. */
686 abort ();
688 stack_type_map[dst_index] = type;
689 src_index--; dst_index--;
690 type = stack_type_map[src_index];
691 if (! TYPE_IS_WIDE (type))
692 abort ();
694 else if (TYPE_IS_WIDE (type))
695 abort ();
697 if (src_index != dst_index)
699 tree src_decl = find_stack_slot (src_index, type);
700 tree dst_decl = find_stack_slot (dst_index, type);
702 java_add_stmt
703 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
704 stack_type_map[dst_index] = type;
709 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
710 value stack. */
712 static void
713 build_java_athrow (tree node)
715 tree call;
717 call = build3 (CALL_EXPR,
718 void_type_node,
719 build_address_of (throw_node),
720 build_tree_list (NULL_TREE, node),
721 NULL_TREE);
722 TREE_SIDE_EFFECTS (call) = 1;
723 java_add_stmt (call);
724 java_stack_pop (stack_pointer);
727 /* Implementation for jsr/ret */
729 static void
730 build_java_jsr (int target_pc, int return_pc)
732 tree where = lookup_label (target_pc);
733 tree ret = lookup_label (return_pc);
734 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
735 push_value (ret_label);
736 flush_quick_stack ();
737 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
739 /* Do not need to emit the label here. We noted the existence of the
740 label as a jump target in note_instructions; we'll emit the label
741 for real at the beginning of the expand_byte_code loop. */
744 static void
745 build_java_ret (tree location)
747 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
750 /* Implementation of operations on array: new, load, store, length */
752 tree
753 decode_newarray_type (int atype)
755 switch (atype)
757 case 4: return boolean_type_node;
758 case 5: return char_type_node;
759 case 6: return float_type_node;
760 case 7: return double_type_node;
761 case 8: return byte_type_node;
762 case 9: return short_type_node;
763 case 10: return int_type_node;
764 case 11: return long_type_node;
765 default: return NULL_TREE;
769 /* Map primitive type to the code used by OPCODE_newarray. */
772 encode_newarray_type (tree type)
774 if (type == boolean_type_node)
775 return 4;
776 else if (type == char_type_node)
777 return 5;
778 else if (type == float_type_node)
779 return 6;
780 else if (type == double_type_node)
781 return 7;
782 else if (type == byte_type_node)
783 return 8;
784 else if (type == short_type_node)
785 return 9;
786 else if (type == int_type_node)
787 return 10;
788 else if (type == long_type_node)
789 return 11;
790 else
791 abort ();
794 /* Build a call to _Jv_ThrowBadArrayIndex(), the
795 ArrayIndexOfBoundsException exception handler. */
797 static tree
798 build_java_throw_out_of_bounds_exception (tree index)
800 tree node = build3 (CALL_EXPR, int_type_node,
801 build_address_of (soft_badarrayindex_node),
802 build_tree_list (NULL_TREE, index), NULL_TREE);
803 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
804 return (node);
807 /* Return the length of an array. Doesn't perform any checking on the nature
808 or value of the array NODE. May be used to implement some bytecodes. */
810 tree
811 build_java_array_length_access (tree node)
813 tree type = TREE_TYPE (node);
814 tree array_type = TREE_TYPE (type);
815 HOST_WIDE_INT length;
817 /* JVM spec: If the arrayref is null, the arraylength instruction
818 throws a NullPointerException. The only way we could get a node
819 of type ptr_type_node at this point is `aconst_null; arraylength'
820 or something equivalent. */
821 if (!flag_new_verifier && type == ptr_type_node)
822 return build3 (CALL_EXPR, int_type_node,
823 build_address_of (soft_nullpointer_node),
824 NULL_TREE, NULL_TREE);
826 if (!is_array_type_p (type))
828 /* With the new verifier, we will see an ordinary pointer type
829 here. In this case, we just use an arbitrary array type. */
830 array_type = build_java_array_type (object_ptr_type_node, -1);
831 type = promote_type (array_type);
834 length = java_array_type_length (type);
835 if (length >= 0)
836 return build_int_cst (NULL_TREE, length);
838 node = build3 (COMPONENT_REF, int_type_node,
839 build_java_indirect_ref (array_type, node,
840 flag_check_references),
841 lookup_field (&array_type, get_identifier ("length")),
842 NULL_TREE);
843 IS_ARRAY_LENGTH_ACCESS (node) = 1;
844 return node;
847 /* Optionally checks a reference against the NULL pointer. ARG1: the
848 expr, ARG2: we should check the reference. Don't generate extra
849 checks if we're not generating code. */
851 tree
852 java_check_reference (tree expr, int check)
854 if (!flag_syntax_only && check)
856 expr = save_expr (expr);
857 expr = build3 (COND_EXPR, TREE_TYPE (expr),
858 build2 (EQ_EXPR, boolean_type_node,
859 expr, null_pointer_node),
860 build3 (CALL_EXPR, void_type_node,
861 build_address_of (soft_nullpointer_node),
862 NULL_TREE, NULL_TREE),
863 expr);
866 return expr;
869 /* Reference an object: just like an INDIRECT_REF, but with checking. */
871 tree
872 build_java_indirect_ref (tree type, tree expr, int check)
874 tree t;
875 t = java_check_reference (expr, check);
876 t = convert (build_pointer_type (type), t);
877 return build1 (INDIRECT_REF, type, t);
880 /* Implement array indexing (either as l-value or r-value).
881 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
882 Optionally performs bounds checking and/or test to NULL.
883 At this point, ARRAY should have been verified as an array. */
885 tree
886 build_java_arrayaccess (tree array, tree type, tree index)
888 tree node, throw = NULL_TREE;
889 tree data_field;
890 tree ref;
891 tree array_type = TREE_TYPE (TREE_TYPE (array));
893 if (!is_array_type_p (TREE_TYPE (array)))
895 /* With the new verifier, we will see an ordinary pointer type
896 here. In this case, we just use the correct array type. */
897 array_type = build_java_array_type (type, -1);
900 if (flag_bounds_check)
902 /* Generate:
903 * (unsigned jint) INDEX >= (unsigned jint) LEN
904 * && throw ArrayIndexOutOfBoundsException.
905 * Note this is equivalent to and more efficient than:
906 * INDEX < 0 || INDEX >= LEN && throw ... */
907 tree test;
908 tree len = convert (unsigned_int_type_node,
909 build_java_array_length_access (array));
910 test = fold (build2 (GE_EXPR, boolean_type_node,
911 convert (unsigned_int_type_node, index),
912 len));
913 if (! integer_zerop (test))
915 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
916 build_java_throw_out_of_bounds_exception (index));
917 /* allows expansion within COMPOUND */
918 TREE_SIDE_EFFECTS( throw ) = 1;
922 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
923 to have the bounds check evaluated first. */
924 if (throw != NULL_TREE)
925 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
927 data_field = lookup_field (&array_type, get_identifier ("data"));
929 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
930 build_java_indirect_ref (array_type, array,
931 flag_check_references),
932 data_field, NULL_TREE);
934 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
935 return node;
938 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
939 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
940 determine that no check is required. */
942 tree
943 build_java_arraystore_check (tree array, tree object)
945 tree check, element_type, source;
946 tree array_type_p = TREE_TYPE (array);
947 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
949 if (! flag_verify_invocations)
951 /* With the new verifier, we don't track precise types. FIXME:
952 performance regression here. */
953 element_type = TYPE_NAME (object_type_node);
955 else
957 if (! is_array_type_p (array_type_p))
958 abort ();
960 /* Get the TYPE_DECL for ARRAY's element type. */
961 element_type
962 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
965 if (TREE_CODE (element_type) != TYPE_DECL
966 || TREE_CODE (object_type) != TYPE_DECL)
967 abort ();
969 if (!flag_store_check)
970 return build1 (NOP_EXPR, array_type_p, array);
972 /* No check is needed if the element type is final. Also check that
973 element_type matches object_type, since in the bytecode
974 compilation case element_type may be the actual element type of
975 the array rather than its declared type. However, if we're doing
976 indirect dispatch, we can't do the `final' optimization. */
977 if (element_type == object_type
978 && ! flag_indirect_dispatch
979 && CLASS_FINAL (element_type))
980 return build1 (NOP_EXPR, array_type_p, array);
982 /* OBJECT might be wrapped by a SAVE_EXPR. */
983 if (TREE_CODE (object) == SAVE_EXPR)
984 source = TREE_OPERAND (object, 0);
985 else
986 source = object;
988 /* Avoid the check if OBJECT was just loaded from the same array. */
989 if (TREE_CODE (source) == ARRAY_REF)
991 tree target;
992 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
993 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
994 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
995 if (TREE_CODE (source) == SAVE_EXPR)
996 source = TREE_OPERAND (source, 0);
998 target = array;
999 if (TREE_CODE (target) == SAVE_EXPR)
1000 target = TREE_OPERAND (target, 0);
1002 if (source == target)
1003 return build1 (NOP_EXPR, array_type_p, array);
1006 /* Build an invocation of _Jv_CheckArrayStore */
1007 check = build3 (CALL_EXPR, void_type_node,
1008 build_address_of (soft_checkarraystore_node),
1009 tree_cons (NULL_TREE, array,
1010 build_tree_list (NULL_TREE, object)),
1011 NULL_TREE);
1012 TREE_SIDE_EFFECTS (check) = 1;
1014 return check;
1017 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1018 ARRAY_NODE. This function is used to retrieve something less vague than
1019 a pointer type when indexing the first dimension of something like [[<t>.
1020 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1021 return unchanged. */
1023 static tree
1024 build_java_check_indexed_type (tree array_node, tree indexed_type)
1026 tree elt_type;
1028 /* We used to check to see if ARRAY_NODE really had array type.
1029 However, with the new verifier, this is not necessary, as we know
1030 that the object will be an array of the appropriate type. */
1032 if (flag_new_verifier)
1033 return indexed_type;
1035 if (!is_array_type_p (TREE_TYPE (array_node)))
1036 abort ();
1038 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
1040 if (indexed_type == ptr_type_node)
1041 return promote_type (elt_type);
1043 /* BYTE/BOOLEAN store and load are used for both type */
1044 if (indexed_type == byte_type_node && elt_type == boolean_type_node)
1045 return boolean_type_node;
1047 if (indexed_type != elt_type )
1048 abort ();
1049 else
1050 return indexed_type;
1053 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1054 called with an integer code (the type of array to create), and the length
1055 of the array to create. */
1057 tree
1058 build_newarray (int atype_value, tree length)
1060 tree type_arg;
1062 tree prim_type = decode_newarray_type (atype_value);
1063 tree type
1064 = build_java_array_type (prim_type,
1065 host_integerp (length, 0) == INTEGER_CST
1066 ? tree_low_cst (length, 0) : -1);
1068 /* If compiling to native, pass a reference to the primitive type class
1069 and save the runtime some work. However, the bytecode generator
1070 expects to find the type_code int here. */
1071 if (flag_emit_class_files)
1072 type_arg = build_int_cst (NULL_TREE, atype_value);
1073 else
1074 type_arg = build_class_ref (prim_type);
1076 return build3 (CALL_EXPR, promote_type (type),
1077 build_address_of (soft_newarray_node),
1078 tree_cons (NULL_TREE,
1079 type_arg,
1080 build_tree_list (NULL_TREE, length)),
1081 NULL_TREE);
1084 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1085 of the dimension. */
1087 tree
1088 build_anewarray (tree class_type, tree length)
1090 tree type
1091 = build_java_array_type (class_type,
1092 host_integerp (length, 0)
1093 ? tree_low_cst (length, 0) : -1);
1095 return build3 (CALL_EXPR, promote_type (type),
1096 build_address_of (soft_anewarray_node),
1097 tree_cons (NULL_TREE, length,
1098 tree_cons (NULL_TREE, build_class_ref (class_type),
1099 build_tree_list (NULL_TREE,
1100 null_pointer_node))),
1101 NULL_TREE);
1104 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1106 tree
1107 build_new_array (tree type, tree length)
1109 if (JPRIMITIVE_TYPE_P (type))
1110 return build_newarray (encode_newarray_type (type), length);
1111 else
1112 return build_anewarray (TREE_TYPE (type), length);
1115 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1116 class pointer, a number of dimensions and the matching number of
1117 dimensions. The argument list is NULL terminated. */
1119 static void
1120 expand_java_multianewarray (tree class_type, int ndim)
1122 int i;
1123 tree args = build_tree_list( NULL_TREE, null_pointer_node );
1125 for( i = 0; i < ndim; i++ )
1126 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1128 push_value (build3 (CALL_EXPR,
1129 promote_type (class_type),
1130 build_address_of (soft_multianewarray_node),
1131 tree_cons (NULL_TREE, build_class_ref (class_type),
1132 tree_cons (NULL_TREE,
1133 build_int_cst (NULL_TREE, ndim),
1134 args)),
1135 NULL_TREE));
1138 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1139 ARRAY is an array type. May expand some bound checking and NULL
1140 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1141 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1142 INT. In those cases, we make the conversion.
1144 if ARRAy is a reference type, the assignment is checked at run-time
1145 to make sure that the RHS can be assigned to the array element
1146 type. It is not necessary to generate this code if ARRAY is final. */
1148 static void
1149 expand_java_arraystore (tree rhs_type_node)
1151 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
1152 && TYPE_PRECISION (rhs_type_node) <= 32) ?
1153 int_type_node : rhs_type_node);
1154 tree index = pop_value (int_type_node);
1155 tree array_type, array;
1157 if (flag_new_verifier)
1159 /* If we're processing an `aaload' we might as well just pick
1160 `Object'. */
1161 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1163 array_type = build_java_array_type (object_ptr_type_node, -1);
1164 rhs_type_node = object_ptr_type_node;
1166 else
1167 array_type = build_java_array_type (rhs_type_node, -1);
1169 else
1170 array_type = ptr_type_node;
1171 array = pop_value (array_type);
1172 if (flag_new_verifier)
1173 array = build1 (NOP_EXPR, promote_type (array_type), array);
1175 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1177 flush_quick_stack ();
1179 index = save_expr (index);
1180 array = save_expr (array);
1182 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1184 tree check = build_java_arraystore_check (array, rhs_node);
1185 java_add_stmt (check);
1188 array = build_java_arrayaccess (array, rhs_type_node, index);
1189 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1192 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1193 sure that LHS is an array type. May expand some bound checking and NULL
1194 pointer checking.
1195 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1196 BOOLEAN/SHORT, we push a promoted type back to the stack.
1199 static void
1200 expand_java_arrayload (tree lhs_type_node)
1202 tree load_node;
1203 tree index_node = pop_value (int_type_node);
1204 tree array_type;
1205 tree array_node;
1207 if (flag_new_verifier)
1209 /* If we're processing an `aaload' we might as well just pick
1210 `Object'. */
1211 if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1213 array_type = build_java_array_type (object_ptr_type_node, -1);
1214 lhs_type_node = object_ptr_type_node;
1216 else
1217 array_type = build_java_array_type (lhs_type_node, -1);
1219 else
1220 array_type = ptr_type_node;
1221 array_node = pop_value (array_type);
1222 if (flag_new_verifier)
1223 array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1225 index_node = save_expr (index_node);
1226 array_node = save_expr (array_node);
1228 if (TREE_TYPE (array_node) == ptr_type_node)
1229 /* The only way we could get a node of type ptr_type_node at this
1230 point is `aconst_null; arraylength' or something equivalent, so
1231 unconditionally throw NullPointerException. */
1232 load_node = build3 (CALL_EXPR, lhs_type_node,
1233 build_address_of (soft_nullpointer_node),
1234 NULL_TREE, NULL_TREE);
1235 else
1237 lhs_type_node = build_java_check_indexed_type (array_node,
1238 lhs_type_node);
1239 load_node = build_java_arrayaccess (array_node,
1240 lhs_type_node,
1241 index_node);
1243 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1244 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
1245 push_value (load_node);
1248 /* Expands .length. Makes sure that we deal with and array and may expand
1249 a NULL check on the array object. */
1251 static void
1252 expand_java_array_length (void)
1254 tree array = pop_value (ptr_type_node);
1255 tree length = build_java_array_length_access (array);
1257 push_value (length);
1260 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1261 either soft_monitorenter_node or soft_monitorexit_node. */
1263 static tree
1264 build_java_monitor (tree call, tree object)
1266 return build3 (CALL_EXPR,
1267 void_type_node,
1268 build_address_of (call),
1269 build_tree_list (NULL_TREE, object),
1270 NULL_TREE);
1273 /* Emit code for one of the PUSHC instructions. */
1275 static void
1276 expand_java_pushc (int ival, tree type)
1278 tree value;
1279 if (type == ptr_type_node && ival == 0)
1280 value = null_pointer_node;
1281 else if (type == int_type_node || type == long_type_node)
1282 value = build_int_cst (type, ival);
1283 else if (type == float_type_node || type == double_type_node)
1285 REAL_VALUE_TYPE x;
1286 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1287 value = build_real (type, x);
1289 else
1290 abort ();
1292 push_value (value);
1295 static void
1296 expand_java_return (tree type)
1298 if (type == void_type_node)
1299 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1300 else
1302 tree retval = pop_value (type);
1303 tree res = DECL_RESULT (current_function_decl);
1304 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1306 /* Handle the situation where the native integer type is smaller
1307 than the JVM integer. It can happen for many cross compilers.
1308 The whole if expression just goes away if INT_TYPE_SIZE < 32
1309 is false. */
1310 if (INT_TYPE_SIZE < 32
1311 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1312 < GET_MODE_SIZE (TYPE_MODE (type))))
1313 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1315 TREE_SIDE_EFFECTS (retval) = 1;
1316 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1320 static void
1321 expand_load_internal (int index, tree type, int pc)
1323 tree copy;
1324 tree var = find_local_variable (index, type, pc);
1326 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1327 on the stack. If there is an assignment to this VAR_DECL between
1328 the stack push and the use, then the wrong code could be
1329 generated. To avoid this we create a new local and copy our
1330 value into it. Then we push this new local on the stack.
1331 Hopefully this all gets optimized out. */
1332 copy = build_decl (VAR_DECL, NULL_TREE, type);
1333 if (INTEGRAL_TYPE_P (type)
1334 && TREE_TYPE (copy) != TREE_TYPE (var))
1335 var = convert (type, var);
1336 java_add_local_var (copy);
1337 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1339 push_value (copy);
1342 tree
1343 build_address_of (tree value)
1345 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1348 bool
1349 class_has_finalize_method (tree type)
1351 tree super = CLASSTYPE_SUPER (type);
1353 if (super == NULL_TREE)
1354 return false; /* Every class with a real finalizer inherits */
1355 /* from java.lang.Object. */
1356 else
1357 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1360 tree
1361 java_create_object (tree type)
1363 tree alloc_node = (class_has_finalize_method (type)
1364 ? alloc_object_node
1365 : alloc_no_finalizer_node);
1367 return build (CALL_EXPR, promote_type (type),
1368 build_address_of (alloc_node),
1369 build_tree_list (NULL_TREE, build_class_ref (type)),
1370 NULL_TREE);
1373 static void
1374 expand_java_NEW (tree type)
1376 tree alloc_node;
1378 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1379 : alloc_no_finalizer_node);
1380 if (! CLASS_LOADED_P (type))
1381 load_class (type, 1);
1382 safe_layout_class (type);
1383 push_value (build3 (CALL_EXPR, promote_type (type),
1384 build_address_of (alloc_node),
1385 build_tree_list (NULL_TREE, build_class_ref (type)),
1386 NULL_TREE));
1389 /* This returns an expression which will extract the class of an
1390 object. */
1392 tree
1393 build_get_class (tree value)
1395 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1396 tree vtable_field = lookup_field (&object_type_node,
1397 get_identifier ("vtable"));
1398 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1399 build_java_indirect_ref (object_type_node, value,
1400 flag_check_references),
1401 vtable_field, NULL_TREE);
1402 return build3 (COMPONENT_REF, class_ptr_type,
1403 build1 (INDIRECT_REF, dtable_type, tmp),
1404 class_field, NULL_TREE);
1407 /* This builds the tree representation of the `instanceof' operator.
1408 It tries various tricks to optimize this in cases where types are
1409 known. */
1411 tree
1412 build_instanceof (tree value, tree type)
1414 tree expr;
1415 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1416 tree valtype = TREE_TYPE (TREE_TYPE (value));
1417 tree valclass = TYPE_NAME (valtype);
1418 tree klass;
1420 /* When compiling from bytecode, we need to ensure that TYPE has
1421 been loaded. */
1422 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1424 load_class (type, 1);
1425 safe_layout_class (type);
1426 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1427 return error_mark_node;
1429 klass = TYPE_NAME (type);
1431 if (type == object_type_node || inherits_from_p (valtype, type))
1433 /* Anything except `null' is an instance of Object. Likewise,
1434 if the object is known to be an instance of the class, then
1435 we only need to check for `null'. */
1436 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1438 else if (flag_verify_invocations
1439 && ! TYPE_ARRAY_P (type)
1440 && ! TYPE_ARRAY_P (valtype)
1441 && DECL_P (klass) && DECL_P (valclass)
1442 && ! CLASS_INTERFACE (valclass)
1443 && ! CLASS_INTERFACE (klass)
1444 && ! inherits_from_p (type, valtype)
1445 && (CLASS_FINAL (klass)
1446 || ! inherits_from_p (valtype, type)))
1448 /* The classes are from different branches of the derivation
1449 tree, so we immediately know the answer. */
1450 expr = boolean_false_node;
1452 else if (DECL_P (klass) && CLASS_FINAL (klass))
1454 tree save = save_expr (value);
1455 expr = build3 (COND_EXPR, itype,
1456 build2 (NE_EXPR, boolean_type_node,
1457 save, null_pointer_node),
1458 build2 (EQ_EXPR, itype,
1459 build_get_class (save),
1460 build_class_ref (type)),
1461 boolean_false_node);
1463 else
1465 expr = build3 (CALL_EXPR, itype,
1466 build_address_of (soft_instanceof_node),
1467 tree_cons (NULL_TREE, value,
1468 build_tree_list (NULL_TREE,
1469 build_class_ref (type))),
1470 NULL_TREE);
1472 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1473 return expr;
1476 static void
1477 expand_java_INSTANCEOF (tree type)
1479 tree value = pop_value (object_ptr_type_node);
1480 value = build_instanceof (value, type);
1481 push_value (value);
1484 static void
1485 expand_java_CHECKCAST (tree type)
1487 tree value = pop_value (ptr_type_node);
1488 value = build3 (CALL_EXPR, promote_type (type),
1489 build_address_of (soft_checkcast_node),
1490 tree_cons (NULL_TREE, build_class_ref (type),
1491 build_tree_list (NULL_TREE, value)),
1492 NULL_TREE);
1493 push_value (value);
1496 static void
1497 expand_iinc (unsigned int local_var_index, int ival, int pc)
1499 tree local_var, res;
1500 tree constant_value;
1502 flush_quick_stack ();
1503 local_var = find_local_variable (local_var_index, int_type_node, pc);
1504 constant_value = build_int_cst (NULL_TREE, ival);
1505 res = fold (build2 (PLUS_EXPR, int_type_node, local_var, constant_value));
1506 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1507 update_aliases (local_var, local_var_index, pc);
1511 tree
1512 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1514 tree call = NULL;
1515 tree arg1 = convert (type, op1);
1516 tree arg2 = convert (type, op2);
1518 if (type == int_type_node)
1520 switch (op)
1522 case TRUNC_DIV_EXPR:
1523 call = soft_idiv_node;
1524 break;
1525 case TRUNC_MOD_EXPR:
1526 call = soft_irem_node;
1527 break;
1528 default:
1529 break;
1532 else if (type == long_type_node)
1534 switch (op)
1536 case TRUNC_DIV_EXPR:
1537 call = soft_ldiv_node;
1538 break;
1539 case TRUNC_MOD_EXPR:
1540 call = soft_lrem_node;
1541 break;
1542 default:
1543 break;
1547 if (! call)
1548 abort ();
1550 call = build3 (CALL_EXPR, type,
1551 build_address_of (call),
1552 tree_cons (NULL_TREE, arg1,
1553 build_tree_list (NULL_TREE, arg2)),
1554 NULL_TREE);
1556 return call;
1559 tree
1560 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1562 tree mask;
1563 switch (op)
1565 case URSHIFT_EXPR:
1567 tree u_type = java_unsigned_type (type);
1568 arg1 = convert (u_type, arg1);
1569 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1570 return convert (type, arg1);
1572 case LSHIFT_EXPR:
1573 case RSHIFT_EXPR:
1574 mask = build_int_cst (NULL_TREE,
1575 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1576 arg2 = fold (build2 (BIT_AND_EXPR, int_type_node, arg2, mask));
1577 break;
1579 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1580 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1581 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1583 tree ifexp1 = fold (build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1584 boolean_type_node, arg1, arg2));
1585 tree ifexp2 = fold (build2 (EQ_EXPR, boolean_type_node, arg1, arg2));
1586 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1587 ifexp2, integer_zero_node,
1588 op == COMPARE_L_EXPR
1589 ? integer_minus_one_node
1590 : integer_one_node));
1591 return fold (build3 (COND_EXPR, int_type_node, ifexp1,
1592 op == COMPARE_L_EXPR ? integer_one_node
1593 : integer_minus_one_node,
1594 second_compare));
1596 case COMPARE_EXPR:
1597 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1599 tree ifexp1 = fold (build2 (LT_EXPR, boolean_type_node, arg1, arg2));
1600 tree ifexp2 = fold (build2 (GT_EXPR, boolean_type_node, arg1, arg2));
1601 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1602 ifexp2, integer_one_node,
1603 integer_zero_node));
1604 return fold (build3 (COND_EXPR, int_type_node,
1605 ifexp1, integer_minus_one_node, second_compare));
1607 case TRUNC_DIV_EXPR:
1608 case TRUNC_MOD_EXPR:
1609 if (TREE_CODE (type) == REAL_TYPE
1610 && op == TRUNC_MOD_EXPR)
1612 tree call;
1613 if (type != double_type_node)
1615 arg1 = convert (double_type_node, arg1);
1616 arg2 = convert (double_type_node, arg2);
1618 call = build3 (CALL_EXPR, double_type_node,
1619 build_address_of (soft_fmod_node),
1620 tree_cons (NULL_TREE, arg1,
1621 build_tree_list (NULL_TREE, arg2)),
1622 NULL_TREE);
1623 if (type != double_type_node)
1624 call = convert (type, call);
1625 return call;
1628 if (TREE_CODE (type) == INTEGER_TYPE
1629 && flag_use_divide_subroutine
1630 && ! flag_syntax_only)
1631 return build_java_soft_divmod (op, type, arg1, arg2);
1633 break;
1634 default: ;
1636 return fold (build2 (op, type, arg1, arg2));
1639 static void
1640 expand_java_binop (tree type, enum tree_code op)
1642 tree larg, rarg;
1643 tree ltype = type;
1644 tree rtype = type;
1645 switch (op)
1647 case LSHIFT_EXPR:
1648 case RSHIFT_EXPR:
1649 case URSHIFT_EXPR:
1650 rtype = int_type_node;
1651 rarg = pop_value (rtype);
1652 break;
1653 default:
1654 rarg = pop_value (rtype);
1656 larg = pop_value (ltype);
1657 push_value (build_java_binop (op, type, larg, rarg));
1660 /* Lookup the field named NAME in *TYPEP or its super classes.
1661 If not found, return NULL_TREE.
1662 (If the *TYPEP is not found, or if the field reference is
1663 ambiguous, return error_mark_node.)
1664 If found, return the FIELD_DECL, and set *TYPEP to the
1665 class containing the field. */
1667 tree
1668 lookup_field (tree *typep, tree name)
1670 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1672 load_class (*typep, 1);
1673 safe_layout_class (*typep);
1674 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1675 return error_mark_node;
1679 tree field, binfo, base_binfo;
1680 tree save_field;
1681 int i;
1683 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1684 if (DECL_NAME (field) == name)
1685 return field;
1687 /* Process implemented interfaces. */
1688 save_field = NULL_TREE;
1689 for (binfo = TYPE_BINFO (*typep), i = 0;
1690 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1692 tree t = BINFO_TYPE (base_binfo);
1693 if ((field = lookup_field (&t, name)))
1695 if (save_field == field)
1696 continue;
1697 if (save_field == NULL_TREE)
1698 save_field = field;
1699 else
1701 tree i1 = DECL_CONTEXT (save_field);
1702 tree i2 = DECL_CONTEXT (field);
1703 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1704 IDENTIFIER_POINTER (name),
1705 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1706 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1707 return error_mark_node;
1712 if (save_field != NULL_TREE)
1713 return save_field;
1715 *typep = CLASSTYPE_SUPER (*typep);
1716 } while (*typep);
1717 return NULL_TREE;
1720 /* Look up the field named NAME in object SELF_VALUE,
1721 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1722 SELF_VALUE is NULL_TREE if looking for a static field. */
1724 tree
1725 build_field_ref (tree self_value, tree self_class, tree name)
1727 tree base_class = self_class;
1728 tree field_decl = lookup_field (&base_class, name);
1729 if (field_decl == NULL_TREE)
1731 error ("field %qs not found", IDENTIFIER_POINTER (name));
1732 return error_mark_node;
1734 if (self_value == NULL_TREE)
1736 return build_static_field_ref (field_decl);
1738 else
1740 int check = (flag_check_references
1741 && ! (DECL_P (self_value)
1742 && DECL_NAME (self_value) == this_identifier_node));
1744 tree base_type = promote_type (base_class);
1745 if (base_type != TREE_TYPE (self_value))
1746 self_value = fold (build1 (NOP_EXPR, base_type, self_value));
1747 if (! flag_syntax_only
1748 && (flag_indirect_dispatch
1749 /* DECL_FIELD_OFFSET == 0 if we have no reference for
1750 the field, perhaps because we couldn't find the class
1751 in which the field is defined.
1752 FIXME: We should investigate this. */
1753 || DECL_FIELD_OFFSET (field_decl) == 0))
1755 tree otable_index
1756 = build_int_cst (NULL_TREE, get_symbol_table_index
1757 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1758 tree field_offset
1759 = build4 (ARRAY_REF, integer_type_node,
1760 TYPE_OTABLE_DECL (output_class), otable_index,
1761 NULL_TREE, NULL_TREE);
1762 tree address;
1764 field_offset = fold (convert (sizetype, field_offset));
1765 address
1766 = fold (build2 (PLUS_EXPR,
1767 build_pointer_type (TREE_TYPE (field_decl)),
1768 self_value, field_offset));
1769 return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
1772 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1773 self_value, check);
1774 return fold (build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1775 self_value, field_decl, NULL_TREE));
1779 tree
1780 lookup_label (int pc)
1782 tree name;
1783 char buf[32];
1784 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1785 name = get_identifier (buf);
1786 if (IDENTIFIER_LOCAL_VALUE (name))
1787 return IDENTIFIER_LOCAL_VALUE (name);
1788 else
1790 /* The type of the address of a label is return_address_type_node. */
1791 tree decl = create_label_decl (name);
1792 LABEL_PC (decl) = pc;
1793 return pushdecl (decl);
1797 /* Generate a unique name for the purpose of loops and switches
1798 labels, and try-catch-finally blocks label or temporary variables. */
1800 tree
1801 generate_name (void)
1803 static int l_number = 0;
1804 char buff [32];
1805 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1806 l_number++;
1807 return get_identifier (buff);
1810 tree
1811 create_label_decl (tree name)
1813 tree decl;
1814 decl = build_decl (LABEL_DECL, name,
1815 TREE_TYPE (return_address_type_node));
1816 DECL_CONTEXT (decl) = current_function_decl;
1817 DECL_IGNORED_P (decl) = 1;
1818 return decl;
1821 /* This maps a bytecode offset (PC) to various flags. */
1822 char *instruction_bits;
1824 static void
1825 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1827 lookup_label (target_pc);
1828 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1831 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1832 where CONDITION is one of one the compare operators. */
1834 static void
1835 expand_compare (enum tree_code condition, tree value1, tree value2,
1836 int target_pc)
1838 tree target = lookup_label (target_pc);
1839 tree cond = fold (build2 (condition, boolean_type_node, value1, value2));
1840 java_add_stmt
1841 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1842 build1 (GOTO_EXPR, void_type_node, target),
1843 build_java_empty_stmt ()));
1846 /* Emit code for a TEST-type opcode. */
1848 static void
1849 expand_test (enum tree_code condition, tree type, int target_pc)
1851 tree value1, value2;
1852 flush_quick_stack ();
1853 value1 = pop_value (type);
1854 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1855 expand_compare (condition, value1, value2, target_pc);
1858 /* Emit code for a COND-type opcode. */
1860 static void
1861 expand_cond (enum tree_code condition, tree type, int target_pc)
1863 tree value1, value2;
1864 flush_quick_stack ();
1865 /* note: pop values in opposite order */
1866 value2 = pop_value (type);
1867 value1 = pop_value (type);
1868 /* Maybe should check value1 and value2 for type compatibility ??? */
1869 expand_compare (condition, value1, value2, target_pc);
1872 static void
1873 expand_java_goto (int target_pc)
1875 tree target_label = lookup_label (target_pc);
1876 flush_quick_stack ();
1877 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1880 static tree
1881 expand_java_switch (tree selector, int default_pc)
1883 tree switch_expr, x;
1885 flush_quick_stack ();
1886 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1887 NULL_TREE, NULL_TREE);
1888 java_add_stmt (switch_expr);
1890 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1891 create_artificial_label ());
1892 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1894 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1895 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1897 return switch_expr;
1900 static void
1901 expand_java_add_case (tree switch_expr, int match, int target_pc)
1903 tree value, x;
1905 value = build_int_cst (TREE_TYPE (switch_expr), match);
1907 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1908 create_artificial_label ());
1909 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1911 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1912 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1915 static tree
1916 pop_arguments (tree arg_types)
1918 if (arg_types == end_params_node)
1919 return NULL_TREE;
1920 if (TREE_CODE (arg_types) == TREE_LIST)
1922 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1923 tree type = TREE_VALUE (arg_types);
1924 tree arg = pop_value (type);
1926 /* With the new verifier we simply cast each argument to its
1927 proper type. This is needed since we lose type information
1928 coming out of the verifier. We also have to do this with the
1929 old verifier when we pop an integer type that must be
1930 promoted for the function call. */
1931 if (flag_new_verifier && TREE_CODE (type) == POINTER_TYPE)
1932 arg = build1 (NOP_EXPR, type, arg);
1933 else if (targetm.calls.promote_prototypes (type)
1934 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1935 && INTEGRAL_TYPE_P (type))
1936 arg = convert (integer_type_node, arg);
1937 return tree_cons (NULL_TREE, arg, tail);
1939 abort ();
1942 /* Attach to PTR (a block) the declaration found in ENTRY. */
1945 attach_init_test_initialization_flags (void **entry, void *ptr)
1947 tree block = (tree)ptr;
1948 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1950 if (block != error_mark_node)
1952 if (TREE_CODE (block) == BIND_EXPR)
1954 tree body = BIND_EXPR_BODY (block);
1955 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1956 BIND_EXPR_VARS (block) = ite->value;
1957 body = build2 (COMPOUND_EXPR, void_type_node,
1958 build1 (DECL_EXPR, void_type_node, ite->value), body);
1959 BIND_EXPR_BODY (block) = body;
1961 else
1963 tree body = BLOCK_SUBBLOCKS (block);
1964 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1965 BLOCK_EXPR_DECLS (block) = ite->value;
1966 body = build2 (COMPOUND_EXPR, void_type_node,
1967 build1 (DECL_EXPR, void_type_node, ite->value), body);
1968 BLOCK_SUBBLOCKS (block) = body;
1972 return true;
1975 /* Build an expression to initialize the class CLAS.
1976 if EXPR is non-NULL, returns an expression to first call the initializer
1977 (if it is needed) and then calls EXPR. */
1979 tree
1980 build_class_init (tree clas, tree expr)
1982 tree init;
1984 /* An optimization: if CLAS is a superclass of the class we're
1985 compiling, we don't need to initialize it. However, if CLAS is
1986 an interface, it won't necessarily be initialized, even if we
1987 implement it. */
1988 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1989 && inherits_from_p (current_class, clas))
1990 || current_class == clas)
1991 return expr;
1993 if (always_initialize_class_p)
1995 init = build3 (CALL_EXPR, void_type_node,
1996 build_address_of (soft_initclass_node),
1997 build_tree_list (NULL_TREE, build_class_ref (clas)),
1998 NULL_TREE);
1999 TREE_SIDE_EFFECTS (init) = 1;
2001 else
2003 tree *init_test_decl;
2004 tree decl;
2005 init_test_decl = java_treetreehash_new
2006 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
2008 if (*init_test_decl == NULL)
2010 /* Build a declaration and mark it as a flag used to track
2011 static class initializations. */
2012 decl = build_decl (VAR_DECL, NULL_TREE,
2013 boolean_type_node);
2014 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2015 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
2016 DECL_CONTEXT (decl) = current_function_decl;
2017 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
2018 /* Tell the check-init code to ignore this decl when not
2019 optimizing class initialization. */
2020 if (!STATIC_CLASS_INIT_OPT_P ())
2021 DECL_BIT_INDEX (decl) = -1;
2022 DECL_INITIAL (decl) = boolean_false_node;
2023 /* Don't emit any symbolic debugging info for this decl. */
2024 DECL_IGNORED_P (decl) = 1;
2025 *init_test_decl = decl;
2028 init = build3 (CALL_EXPR, void_type_node,
2029 build_address_of (soft_initclass_node),
2030 build_tree_list (NULL_TREE, build_class_ref (clas)),
2031 NULL_TREE);
2032 TREE_SIDE_EFFECTS (init) = 1;
2033 init = build3 (COND_EXPR, void_type_node,
2034 build2 (EQ_EXPR, boolean_type_node,
2035 *init_test_decl, boolean_false_node),
2036 init, integer_zero_node);
2037 TREE_SIDE_EFFECTS (init) = 1;
2038 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
2039 build2 (MODIFY_EXPR, boolean_type_node,
2040 *init_test_decl, boolean_true_node));
2041 TREE_SIDE_EFFECTS (init) = 1;
2044 if (expr != NULL_TREE)
2046 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2047 TREE_SIDE_EFFECTS (expr) = 1;
2048 return expr;
2050 return init;
2053 tree
2054 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2055 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2056 tree arg_list ATTRIBUTE_UNUSED)
2058 tree func;
2059 if (is_compiled_class (self_type))
2061 /* With indirect dispatch we have to use indirect calls for all
2062 publicly visible methods or gcc will use PLT indirections
2063 to reach them. We also have to use indirect dispatch for all
2064 external methods. */
2065 if (! flag_indirect_dispatch
2066 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2068 make_decl_rtl (method);
2069 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2070 method);
2072 else
2074 tree table_index
2075 = build_int_cst (NULL_TREE, get_symbol_table_index
2076 (method, &TYPE_ATABLE_METHODS (output_class)));
2077 func
2078 = build4 (ARRAY_REF,
2079 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2080 TYPE_ATABLE_DECL (output_class), table_index,
2081 NULL_TREE, NULL_TREE);
2083 func = convert (method_ptr_type_node, func);
2085 else
2087 /* We don't know whether the method has been (statically) compiled.
2088 Compile this code to get a reference to the method's code:
2090 SELF_TYPE->methods[METHOD_INDEX].ncode
2094 int method_index = 0;
2095 tree meth, ref;
2097 /* The method might actually be declared in some superclass, so
2098 we have to use its class context, not the caller's notion of
2099 where the method is. */
2100 self_type = DECL_CONTEXT (method);
2101 ref = build_class_ref (self_type);
2102 ref = build1 (INDIRECT_REF, class_type_node, ref);
2103 if (ncode_ident == NULL_TREE)
2104 ncode_ident = get_identifier ("ncode");
2105 if (methods_ident == NULL_TREE)
2106 methods_ident = get_identifier ("methods");
2107 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2108 lookup_field (&class_type_node, methods_ident),
2109 NULL_TREE);
2110 for (meth = TYPE_METHODS (self_type);
2111 ; meth = TREE_CHAIN (meth))
2113 if (method == meth)
2114 break;
2115 if (meth == NULL_TREE)
2116 fatal_error ("method '%s' not found in class",
2117 IDENTIFIER_POINTER (DECL_NAME (method)));
2118 method_index++;
2120 method_index *= int_size_in_bytes (method_type_node);
2121 ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
2122 ref, build_int_cst (NULL_TREE, method_index)));
2123 ref = build1 (INDIRECT_REF, method_type_node, ref);
2124 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2125 ref, lookup_field (&method_type_node, ncode_ident),
2126 NULL_TREE);
2128 return func;
2131 tree
2132 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2134 tree dtable, objectref;
2136 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2138 /* If we're dealing with interfaces and if the objectref
2139 argument is an array then get the dispatch table of the class
2140 Object rather than the one from the objectref. */
2141 objectref = (is_invoke_interface
2142 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2143 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2145 if (dtable_ident == NULL_TREE)
2146 dtable_ident = get_identifier ("vtable");
2147 dtable = build_java_indirect_ref (object_type_node, objectref,
2148 flag_check_references);
2149 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2150 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2152 return dtable;
2155 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2156 T. If this decl has not been seen before, it will be added to the
2157 [oa]table_methods. If it has, the existing table slot will be
2158 reused. */
2161 get_symbol_table_index (tree t, tree *symbol_table)
2163 int i = 1;
2164 tree method_list;
2166 if (*symbol_table == NULL_TREE)
2168 *symbol_table = build_tree_list (t, t);
2169 return 1;
2172 method_list = *symbol_table;
2174 while (1)
2176 tree value = TREE_VALUE (method_list);
2177 if (value == t)
2178 return i;
2179 i++;
2180 if (TREE_CHAIN (method_list) == NULL_TREE)
2181 break;
2182 else
2183 method_list = TREE_CHAIN (method_list);
2186 TREE_CHAIN (method_list) = build_tree_list (t, t);
2187 return i;
2190 tree
2191 build_invokevirtual (tree dtable, tree method)
2193 tree func;
2194 tree nativecode_ptr_ptr_type_node
2195 = build_pointer_type (nativecode_ptr_type_node);
2196 tree method_index;
2197 tree otable_index;
2199 if (flag_indirect_dispatch)
2201 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2202 abort ();
2204 otable_index
2205 = build_int_cst (NULL_TREE, get_symbol_table_index
2206 (method, &TYPE_OTABLE_METHODS (output_class)));
2207 method_index = build4 (ARRAY_REF, integer_type_node,
2208 TYPE_OTABLE_DECL (output_class),
2209 otable_index, NULL_TREE, NULL_TREE);
2211 else
2213 /* We fetch the DECL_VINDEX field directly here, rather than
2214 using get_method_index(). DECL_VINDEX is the true offset
2215 from the vtable base to a method, regrdless of any extra
2216 words inserted at the start of the vtable. */
2217 method_index = DECL_VINDEX (method);
2218 method_index = size_binop (MULT_EXPR, method_index,
2219 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2220 if (TARGET_VTABLE_USES_DESCRIPTORS)
2221 method_index = size_binop (MULT_EXPR, method_index,
2222 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2225 func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2226 convert (nativecode_ptr_ptr_type_node, method_index)));
2228 if (TARGET_VTABLE_USES_DESCRIPTORS)
2229 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2230 else
2231 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2233 return func;
2236 static GTY(()) tree class_ident;
2237 tree
2238 build_invokeinterface (tree dtable, tree method)
2240 tree lookup_arg;
2241 tree interface;
2242 tree idx;
2244 /* We expand invokeinterface here. */
2246 if (class_ident == NULL_TREE)
2247 class_ident = get_identifier ("class");
2249 dtable = build_java_indirect_ref (dtable_type, dtable,
2250 flag_check_references);
2251 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2252 lookup_field (&dtable_type, class_ident), NULL_TREE);
2254 interface = DECL_CONTEXT (method);
2255 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2256 abort ();
2257 layout_class_methods (interface);
2259 if (flag_indirect_dispatch)
2261 int itable_index
2262 = 2 * (get_symbol_table_index
2263 (method, &TYPE_ITABLE_METHODS (output_class)));
2264 interface
2265 = build4 (ARRAY_REF,
2266 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2267 TYPE_ITABLE_DECL (output_class),
2268 build_int_cst (NULL_TREE, itable_index-1),
2269 NULL_TREE, NULL_TREE);
2270 idx
2271 = build4 (ARRAY_REF,
2272 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2273 TYPE_ITABLE_DECL (output_class),
2274 build_int_cst (NULL_TREE, itable_index),
2275 NULL_TREE, NULL_TREE);
2276 interface = convert (class_ptr_type, interface);
2277 idx = convert (integer_type_node, idx);
2279 else
2281 idx = build_int_cst (NULL_TREE,
2282 get_interface_method_index (method, interface));
2283 interface = build_class_ref (interface);
2286 lookup_arg = tree_cons (NULL_TREE, dtable,
2287 tree_cons (NULL_TREE, interface,
2288 build_tree_list (NULL_TREE, idx)));
2290 return build3 (CALL_EXPR, ptr_type_node,
2291 build_address_of (soft_lookupinterfacemethod_node),
2292 lookup_arg, NULL_TREE);
2295 /* Expand one of the invoke_* opcodes.
2296 OPCODE is the specific opcode.
2297 METHOD_REF_INDEX is an index into the constant pool.
2298 NARGS is the number of arguments, or -1 if not specified. */
2300 static void
2301 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2303 tree method_signature
2304 = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2305 tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2306 method_ref_index);
2307 tree self_type
2308 = get_class_constant (current_jcf,
2309 COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2310 method_ref_index));
2311 const char *const self_name
2312 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2313 tree call, func, method, arg_list, method_type;
2314 tree check = NULL_TREE;
2316 if (! CLASS_LOADED_P (self_type))
2318 load_class (self_type, 1);
2319 safe_layout_class (self_type);
2320 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2321 fatal_error ("failed to find class '%s'", self_name);
2323 layout_class_methods (self_type);
2325 if (ID_INIT_P (method_name))
2326 method = lookup_java_constructor (self_type, method_signature);
2327 else
2328 method = lookup_java_method (self_type, method_name, method_signature);
2330 /* We've found a method in an interface, but this isn't an interface
2331 call. */
2332 if (opcode != OPCODE_invokeinterface
2333 && method
2334 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2335 method = NULL_TREE;
2337 /* We've found a non-interface method but we are making an
2338 interface call. This can happen if the interface overrides a
2339 method in Object. */
2340 if (! flag_verify_invocations
2341 && opcode == OPCODE_invokeinterface
2342 && method
2343 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2344 method = NULL_TREE;
2346 if (method == NULL_TREE)
2348 if (flag_verify_invocations || ! flag_indirect_dispatch)
2350 error ("class '%s' has no method named '%s' matching signature '%s'",
2351 self_name,
2352 IDENTIFIER_POINTER (method_name),
2353 IDENTIFIER_POINTER (method_signature));
2355 else
2357 int flags = ACC_PUBLIC;
2358 if (opcode == OPCODE_invokestatic)
2359 flags |= ACC_STATIC;
2360 if (opcode == OPCODE_invokeinterface)
2362 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2363 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2365 method = add_method (self_type, flags, method_name,
2366 method_signature);
2367 DECL_ARTIFICIAL (method) = 1;
2368 METHOD_DUMMY (method) = 1;
2369 layout_class_method (self_type, NULL,
2370 method, NULL);
2374 /* Invoke static can't invoke static/abstract method */
2375 if (method != NULL_TREE)
2377 if (opcode == OPCODE_invokestatic)
2379 if (!METHOD_STATIC (method))
2381 error ("invokestatic on non static method");
2382 method = NULL_TREE;
2384 else if (METHOD_ABSTRACT (method))
2386 error ("invokestatic on abstract method");
2387 method = NULL_TREE;
2390 else
2392 if (METHOD_STATIC (method))
2394 error ("invoke[non-static] on static method");
2395 method = NULL_TREE;
2400 if (method == NULL_TREE)
2402 /* If we got here, we emitted an error message above. So we
2403 just pop the arguments, push a properly-typed zero, and
2404 continue. */
2405 method_type = get_type_from_signature (method_signature);
2406 pop_arguments (TYPE_ARG_TYPES (method_type));
2407 if (opcode != OPCODE_invokestatic)
2408 pop_type (self_type);
2409 method_type = promote_type (TREE_TYPE (method_type));
2410 push_value (convert (method_type, integer_zero_node));
2411 return;
2414 method_type = TREE_TYPE (method);
2415 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2416 flush_quick_stack ();
2418 func = NULL_TREE;
2419 if (opcode == OPCODE_invokestatic)
2420 func = build_known_method_ref (method, method_type, self_type,
2421 method_signature, arg_list);
2422 else if (opcode == OPCODE_invokespecial
2423 || (opcode == OPCODE_invokevirtual
2424 && (METHOD_PRIVATE (method)
2425 || METHOD_FINAL (method)
2426 || CLASS_FINAL (TYPE_NAME (self_type)))))
2428 /* If the object for the method call is null, we throw an
2429 exception. We don't do this if the object is the current
2430 method's `this'. In other cases we just rely on an
2431 optimization pass to eliminate redundant checks. FIXME:
2432 Unfortunately there doesn't seem to be a way to determine
2433 what the current method is right now.
2434 We do omit the check if we're calling <init>. */
2435 /* We use a SAVE_EXPR here to make sure we only evaluate
2436 the new `self' expression once. */
2437 tree save_arg = save_expr (TREE_VALUE (arg_list));
2438 TREE_VALUE (arg_list) = save_arg;
2439 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2440 func = build_known_method_ref (method, method_type, self_type,
2441 method_signature, arg_list);
2443 else
2445 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2446 arg_list);
2447 if (opcode == OPCODE_invokevirtual)
2448 func = build_invokevirtual (dtable, method);
2449 else
2450 func = build_invokeinterface (dtable, method);
2453 if (TREE_CODE (func) == ADDR_EXPR)
2454 TREE_TYPE (func) = build_pointer_type (method_type);
2455 else
2456 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2458 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2459 func, arg_list, NULL_TREE);
2460 TREE_SIDE_EFFECTS (call) = 1;
2461 call = check_for_builtin (method, call);
2463 if (check != NULL_TREE)
2465 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2466 TREE_SIDE_EFFECTS (call) = 1;
2469 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2470 java_add_stmt (call);
2471 else
2473 push_value (call);
2474 flush_quick_stack ();
2478 /* Create a stub which will be put into the vtable but which will call
2479 a JNI function. */
2481 tree
2482 build_jni_stub (tree method)
2484 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2485 tree jni_func_type, tem;
2486 tree env_var, res_var = NULL_TREE, block;
2487 tree method_args, res_type;
2488 tree meth_var;
2489 tree bind;
2491 int args_size = 0;
2493 tree klass = DECL_CONTEXT (method);
2494 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2495 klass = build_class_ref (klass);
2497 if (! METHOD_NATIVE (method) || ! flag_jni)
2498 abort ();
2500 DECL_ARTIFICIAL (method) = 1;
2501 DECL_EXTERNAL (method) = 0;
2503 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2504 DECL_CONTEXT (env_var) = method;
2506 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2508 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2509 TREE_TYPE (TREE_TYPE (method)));
2510 DECL_CONTEXT (res_var) = method;
2511 TREE_CHAIN (env_var) = res_var;
2514 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2515 TREE_STATIC (meth_var) = 1;
2516 TREE_PUBLIC (meth_var) = 0;
2517 DECL_EXTERNAL (meth_var) = 0;
2518 DECL_CONTEXT (meth_var) = method;
2519 DECL_ARTIFICIAL (meth_var) = 1;
2520 DECL_INITIAL (meth_var) = null_pointer_node;
2521 TREE_USED (meth_var) = 1;
2522 chainon (env_var, meth_var);
2523 build_result_decl (method);
2525 /* One strange way that the front ends are different is that they
2526 store arguments differently. */
2527 if (from_class)
2528 method_args = DECL_ARGUMENTS (method);
2529 else
2530 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2531 block = build_block (env_var, NULL_TREE, NULL_TREE,
2532 method_args, NULL_TREE);
2533 TREE_SIDE_EFFECTS (block) = 1;
2534 /* When compiling from source we don't set the type of the block,
2535 because that will prevent patch_return from ever being run. */
2536 if (from_class)
2537 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2539 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2540 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2541 build3 (CALL_EXPR, ptr_type_node,
2542 build_address_of (soft_getjnienvnewframe_node),
2543 build_tree_list (NULL_TREE, klass),
2544 NULL_TREE));
2545 CAN_COMPLETE_NORMALLY (body) = 1;
2547 /* All the arguments to this method become arguments to the
2548 underlying JNI function. If we had to wrap object arguments in a
2549 special way, we would do that here. */
2550 args = NULL_TREE;
2551 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2553 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2554 #ifdef PARM_BOUNDARY
2555 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2556 * PARM_BOUNDARY);
2557 #endif
2558 args_size += (arg_bits / BITS_PER_UNIT);
2560 args = tree_cons (NULL_TREE, tem, args);
2562 args = nreverse (args);
2563 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2565 /* For a static method the second argument is the class. For a
2566 non-static method the second argument is `this'; that is already
2567 available in the argument list. */
2568 if (METHOD_STATIC (method))
2570 args_size += int_size_in_bytes (TREE_TYPE (klass));
2571 args = tree_cons (NULL_TREE, klass, args);
2572 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2575 /* The JNIEnv structure is the first argument to the JNI function. */
2576 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2577 args = tree_cons (NULL_TREE, env_var, args);
2578 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2580 /* We call _Jv_LookupJNIMethod to find the actual underlying
2581 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2582 exception if this function is not found at runtime. */
2583 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2584 method_sig = build_java_signature (TREE_TYPE (method));
2585 lookup_arg = tree_cons (NULL_TREE,
2586 build_utf8_ref (unmangle_classname
2587 (IDENTIFIER_POINTER (method_sig),
2588 IDENTIFIER_LENGTH (method_sig))),
2589 tem);
2590 tem = DECL_NAME (method);
2591 lookup_arg
2592 = tree_cons (NULL_TREE, klass,
2593 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2595 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2597 #ifdef MODIFY_JNI_METHOD_CALL
2598 tem = MODIFY_JNI_METHOD_CALL (tem);
2599 #endif
2601 jni_func_type = build_pointer_type (tem);
2603 jnifunc = build3 (COND_EXPR, ptr_type_node,
2604 meth_var, meth_var,
2605 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2606 build3 (CALL_EXPR, ptr_type_node,
2607 build_address_of
2608 (soft_lookupjnimethod_node),
2609 lookup_arg, NULL_TREE)));
2611 /* Now we make the actual JNI call via the resulting function
2612 pointer. */
2613 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2614 build1 (NOP_EXPR, jni_func_type, jnifunc),
2615 args, NULL_TREE);
2617 /* If the JNI call returned a result, capture it here. If we had to
2618 unwrap JNI object results, we would do that here. */
2619 if (res_var != NULL_TREE)
2620 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2621 res_var, call);
2623 TREE_SIDE_EFFECTS (call) = 1;
2624 CAN_COMPLETE_NORMALLY (call) = 1;
2626 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2627 TREE_SIDE_EFFECTS (body) = 1;
2629 /* Now free the environment we allocated. */
2630 call = build3 (CALL_EXPR, ptr_type_node,
2631 build_address_of (soft_jnipopsystemframe_node),
2632 build_tree_list (NULL_TREE, env_var),
2633 NULL_TREE);
2634 TREE_SIDE_EFFECTS (call) = 1;
2635 CAN_COMPLETE_NORMALLY (call) = 1;
2636 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2637 TREE_SIDE_EFFECTS (body) = 1;
2639 /* Finally, do the return. */
2640 res_type = void_type_node;
2641 if (res_var != NULL_TREE)
2643 tree drt;
2644 if (! DECL_RESULT (method))
2645 abort ();
2646 /* Make sure we copy the result variable to the actual
2647 result. We use the type of the DECL_RESULT because it
2648 might be different from the return type of the function:
2649 it might be promoted. */
2650 drt = TREE_TYPE (DECL_RESULT (method));
2651 if (drt != TREE_TYPE (res_var))
2652 res_var = build1 (CONVERT_EXPR, drt, res_var);
2653 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2654 TREE_SIDE_EFFECTS (res_var) = 1;
2657 body = build2 (COMPOUND_EXPR, void_type_node, body,
2658 build1 (RETURN_EXPR, res_type, res_var));
2659 TREE_SIDE_EFFECTS (body) = 1;
2661 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2662 body, block);
2663 return bind;
2666 /* Expand an operation to extract from or store into a field.
2667 IS_STATIC is 1 iff the field is static.
2668 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2669 FIELD_REF_INDEX is an index into the constant pool. */
2671 static void
2672 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2674 tree self_type
2675 = get_class_constant (current_jcf,
2676 COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2677 field_ref_index));
2678 const char *self_name
2679 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2680 tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2681 tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
2682 field_ref_index);
2683 tree field_type = get_type_from_signature (field_signature);
2684 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2685 tree field_ref;
2686 int is_error = 0;
2687 tree original_self_type = self_type;
2688 tree field_decl;
2690 if (! CLASS_LOADED_P (self_type))
2691 load_class (self_type, 1);
2692 field_decl = lookup_field (&self_type, field_name);
2693 if (field_decl == error_mark_node)
2695 is_error = 1;
2697 else if (field_decl == NULL_TREE)
2699 if (! flag_verify_invocations)
2701 int flags = ACC_PUBLIC;
2702 if (is_static)
2703 flags |= ACC_STATIC;
2704 self_type = original_self_type;
2705 field_decl = add_field (original_self_type, field_name,
2706 field_type, flags);
2707 DECL_ARTIFICIAL (field_decl) = 1;
2708 DECL_IGNORED_P (field_decl) = 1;
2710 else
2712 error ("missing field '%s' in '%s'",
2713 IDENTIFIER_POINTER (field_name), self_name);
2714 is_error = 1;
2717 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2719 error ("mismatching signature for field '%s' in '%s'",
2720 IDENTIFIER_POINTER (field_name), self_name);
2721 is_error = 1;
2723 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2724 if (is_error)
2726 if (! is_putting)
2727 push_value (convert (field_type, integer_zero_node));
2728 flush_quick_stack ();
2729 return;
2732 field_ref = build_field_ref (field_ref, self_type, field_name);
2733 if (is_static)
2734 field_ref = build_class_init (self_type, field_ref);
2735 if (is_putting)
2737 flush_quick_stack ();
2738 if (FIELD_FINAL (field_decl))
2740 if (DECL_CONTEXT (field_decl) != current_class)
2741 error ("%Jassignment to final field '%D' not in field's class",
2742 field_decl, field_decl);
2743 else if (FIELD_STATIC (field_decl))
2745 if (!DECL_CLINIT_P (current_function_decl))
2746 warning ("%Jassignment to final static field %qD not in "
2747 "class initializer",
2748 field_decl, field_decl);
2750 else
2752 tree cfndecl_name = DECL_NAME (current_function_decl);
2753 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2754 && !ID_FINIT_P (cfndecl_name))
2755 warning ("%Jassignment to final field '%D' not in constructor",
2756 field_decl, field_decl);
2759 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2760 field_ref, new_value));
2762 else
2763 push_value (field_ref);
2766 void
2767 load_type_state (tree label)
2769 int i;
2770 tree vec = LABEL_TYPE_STATE (label);
2771 int cur_length = TREE_VEC_LENGTH (vec);
2772 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2773 for (i = 0; i < cur_length; i++)
2774 type_map [i] = TREE_VEC_ELT (vec, i);
2777 /* Go over METHOD's bytecode and note instruction starts in
2778 instruction_bits[]. */
2780 void
2781 note_instructions (JCF *jcf, tree method)
2783 int PC;
2784 unsigned char* byte_ops;
2785 long length = DECL_CODE_LENGTH (method);
2787 int saw_index;
2788 jint INT_temp;
2790 #undef RET /* Defined by config/i386/i386.h */
2791 #undef PTR
2792 #define BCODE byte_ops
2793 #define BYTE_type_node byte_type_node
2794 #define SHORT_type_node short_type_node
2795 #define INT_type_node int_type_node
2796 #define LONG_type_node long_type_node
2797 #define CHAR_type_node char_type_node
2798 #define PTR_type_node ptr_type_node
2799 #define FLOAT_type_node float_type_node
2800 #define DOUBLE_type_node double_type_node
2801 #define VOID_type_node void_type_node
2802 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2803 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2804 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2805 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2807 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2809 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2810 byte_ops = jcf->read_ptr;
2811 instruction_bits = xrealloc (instruction_bits, length + 1);
2812 memset (instruction_bits, 0, length + 1);
2814 /* This pass figures out which PC can be the targets of jumps. */
2815 for (PC = 0; PC < length;)
2817 int oldpc = PC; /* PC at instruction start. */
2818 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2819 switch (byte_ops[PC++])
2821 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2822 case OPCODE: \
2823 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2824 break;
2826 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2828 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2829 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2830 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2831 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2832 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2833 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2834 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2835 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2837 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2838 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2839 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2840 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2841 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2842 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2843 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2844 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2846 /* two forms of wide instructions */
2847 #define PRE_SPECIAL_WIDE(IGNORE) \
2849 int modified_opcode = IMMEDIATE_u1; \
2850 if (modified_opcode == OPCODE_iinc) \
2852 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2853 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2855 else \
2857 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2861 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2863 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2865 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2866 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2867 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2868 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2869 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2870 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2871 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2872 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2873 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2874 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2876 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2877 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2878 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2879 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2880 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2881 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2882 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2883 NOTE_LABEL (PC); \
2884 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2886 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2888 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2889 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2891 #define PRE_LOOKUP_SWITCH \
2892 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2893 NOTE_LABEL (default_offset+oldpc); \
2894 if (npairs >= 0) \
2895 while (--npairs >= 0) { \
2896 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2897 jint offset = IMMEDIATE_s4; \
2898 NOTE_LABEL (offset+oldpc); } \
2901 #define PRE_TABLE_SWITCH \
2902 { jint default_offset = IMMEDIATE_s4; \
2903 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2904 NOTE_LABEL (default_offset+oldpc); \
2905 if (low <= high) \
2906 while (low++ <= high) { \
2907 jint offset = IMMEDIATE_s4; \
2908 NOTE_LABEL (offset+oldpc); } \
2911 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2912 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2913 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2914 (void)(IMMEDIATE_u2); \
2915 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2917 #include "javaop.def"
2918 #undef JAVAOP
2920 } /* for */
2923 void
2924 expand_byte_code (JCF *jcf, tree method)
2926 int PC;
2927 int i;
2928 const unsigned char *linenumber_pointer;
2929 int dead_code_index = -1;
2930 unsigned char* byte_ops;
2931 long length = DECL_CODE_LENGTH (method);
2933 stack_pointer = 0;
2934 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2935 byte_ops = jcf->read_ptr;
2937 /* We make an initial pass of the line number table, to note
2938 which instructions have associated line number entries. */
2939 linenumber_pointer = linenumber_table;
2940 for (i = 0; i < linenumber_count; i++)
2942 int pc = GET_u2 (linenumber_pointer);
2943 linenumber_pointer += 4;
2944 if (pc >= length)
2945 warning ("invalid PC in line number table");
2946 else
2948 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2949 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2950 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2954 if (flag_new_verifier)
2956 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2957 return;
2959 else
2961 if (! verify_jvm_instructions (jcf, byte_ops, length))
2962 return;
2965 promote_arguments ();
2967 /* Translate bytecodes. */
2968 linenumber_pointer = linenumber_table;
2969 for (PC = 0; PC < length;)
2971 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2973 tree label = lookup_label (PC);
2974 flush_quick_stack ();
2975 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2976 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2977 if (LABEL_VERIFIED (label) || PC == 0)
2978 load_type_state (label);
2981 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2983 if (dead_code_index == -1)
2985 /* This is the start of a region of unreachable bytecodes.
2986 They still need to be processed in order for EH ranges
2987 to get handled correctly. However, we can simply
2988 replace these bytecodes with nops. */
2989 dead_code_index = PC;
2992 /* Turn this bytecode into a nop. */
2993 byte_ops[PC] = 0x0;
2995 else
2997 if (dead_code_index != -1)
2999 /* We've just reached the end of a region of dead code. */
3000 if (extra_warnings)
3001 warning ("unreachable bytecode from %d to before %d",
3002 dead_code_index, PC);
3003 dead_code_index = -1;
3007 /* Handle possible line number entry for this PC.
3009 This code handles out-of-order and multiple linenumbers per PC,
3010 but is optimized for the case of line numbers increasing
3011 monotonically with PC. */
3012 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
3014 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
3015 || GET_u2 (linenumber_pointer) != PC)
3016 linenumber_pointer = linenumber_table;
3017 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
3019 int pc = GET_u2 (linenumber_pointer);
3020 linenumber_pointer += 4;
3021 if (pc == PC)
3023 int line = GET_u2 (linenumber_pointer - 2);
3024 #ifdef USE_MAPPED_LOCATION
3025 input_location = linemap_line_start (&line_table, line, 1);
3026 #else
3027 input_location.line = line;
3028 #endif
3029 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
3030 break;
3034 maybe_pushlevels (PC);
3035 PC = process_jvm_instruction (PC, byte_ops, length);
3036 maybe_poplevels (PC);
3037 } /* for */
3039 if (dead_code_index != -1)
3041 /* We've just reached the end of a region of dead code. */
3042 if (extra_warnings)
3043 warning ("unreachable bytecode from %d to the end of the method",
3044 dead_code_index);
3048 static void
3049 java_push_constant_from_pool (JCF *jcf, int index)
3051 tree c;
3052 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3054 tree name;
3055 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3056 index = alloc_name_constant (CONSTANT_String, name);
3057 c = build_ref_from_constant_pool (index);
3058 c = convert (promote_type (string_type_node), c);
3060 else
3061 c = get_constant (jcf, index);
3062 push_value (c);
3066 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3067 long length ATTRIBUTE_UNUSED)
3069 const char *opname; /* Temporary ??? */
3070 int oldpc = PC; /* PC at instruction start. */
3072 /* If the instruction is at the beginning of a exception handler,
3073 replace the top of the stack with the thrown object reference */
3074 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3076 /* Note that the new verifier will not emit a type map at all
3077 for dead exception handlers. In this case we just ignore
3078 the situation. */
3079 if (! flag_new_verifier || (instruction_bits[PC] & BCODE_VERIFIED) != 0)
3081 tree type = pop_type (promote_type (throwable_type_node));
3082 push_value (build_exception_object_ref (type));
3086 switch (byte_ops[PC++])
3088 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3089 case OPCODE: \
3090 opname = #OPNAME; \
3091 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3092 break;
3094 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3096 int saw_index = 0; \
3097 int index = OPERAND_VALUE; \
3098 build_java_ret \
3099 (find_local_variable (index, return_address_type_node, oldpc)); \
3102 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3104 /* OPERAND_VALUE may have side-effects on PC */ \
3105 int opvalue = OPERAND_VALUE; \
3106 build_java_jsr (oldpc + opvalue, PC); \
3109 /* Push a constant onto the stack. */
3110 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3111 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3112 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3113 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3115 /* internal macro added for use by the WIDE case */
3116 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3117 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3119 /* Push local variable onto the opcode stack. */
3120 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3122 /* have to do this since OPERAND_VALUE may have side-effects */ \
3123 int opvalue = OPERAND_VALUE; \
3124 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3127 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3128 expand_java_return (OPERAND_TYPE##_type_node)
3130 #define REM_EXPR TRUNC_MOD_EXPR
3131 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3132 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3134 #define FIELD(IS_STATIC, IS_PUT) \
3135 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3137 #define TEST(OPERAND_TYPE, CONDITION) \
3138 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3140 #define COND(OPERAND_TYPE, CONDITION) \
3141 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3143 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3144 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3146 #define BRANCH_GOTO(OPERAND_VALUE) \
3147 expand_java_goto (oldpc + OPERAND_VALUE)
3149 #define BRANCH_CALL(OPERAND_VALUE) \
3150 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3152 #if 0
3153 #define BRANCH_RETURN(OPERAND_VALUE) \
3155 tree type = OPERAND_TYPE##_type_node; \
3156 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3157 expand_java_ret (value); \
3159 #endif
3161 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3162 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3163 fprintf (stderr, "(not implemented)\n")
3164 #define NOT_IMPL1(OPERAND_VALUE) \
3165 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3166 fprintf (stderr, "(not implemented)\n")
3168 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3170 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3172 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3174 #define STACK_SWAP(COUNT) java_stack_swap()
3176 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3177 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3178 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3180 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3181 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3183 #define LOOKUP_SWITCH \
3184 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3185 tree selector = pop_value (INT_type_node); \
3186 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3187 while (--npairs >= 0) \
3189 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3190 expand_java_add_case (switch_expr, match, oldpc + offset); \
3194 #define TABLE_SWITCH \
3195 { jint default_offset = IMMEDIATE_s4; \
3196 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3197 tree selector = pop_value (INT_type_node); \
3198 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3199 for (; low <= high; low++) \
3201 jint offset = IMMEDIATE_s4; \
3202 expand_java_add_case (switch_expr, low, oldpc + offset); \
3206 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3207 { int opcode = byte_ops[PC-1]; \
3208 int method_ref_index = IMMEDIATE_u2; \
3209 int nargs; \
3210 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3211 else nargs = -1; \
3212 expand_invoke (opcode, method_ref_index, nargs); \
3215 /* Handle new, checkcast, instanceof */
3216 #define OBJECT(TYPE, OP) \
3217 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3219 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3221 #define ARRAY_LOAD(OPERAND_TYPE) \
3223 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3226 #define ARRAY_STORE(OPERAND_TYPE) \
3228 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3231 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3232 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3233 #define ARRAY_NEW_PTR() \
3234 push_value (build_anewarray (get_class_constant (current_jcf, \
3235 IMMEDIATE_u2), \
3236 pop_value (int_type_node)));
3237 #define ARRAY_NEW_NUM() \
3239 int atype = IMMEDIATE_u1; \
3240 push_value (build_newarray (atype, pop_value (int_type_node)));\
3242 #define ARRAY_NEW_MULTI() \
3244 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3245 int ndims = IMMEDIATE_u1; \
3246 expand_java_multianewarray( class, ndims ); \
3249 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3250 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3251 pop_value (OPERAND_TYPE##_type_node))));
3253 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3255 push_value (build1 (NOP_EXPR, int_type_node, \
3256 (convert (TO_TYPE##_type_node, \
3257 pop_value (FROM_TYPE##_type_node))))); \
3260 #define CONVERT(FROM_TYPE, TO_TYPE) \
3262 push_value (convert (TO_TYPE##_type_node, \
3263 pop_value (FROM_TYPE##_type_node))); \
3266 /* internal macro added for use by the WIDE case
3267 Added TREE_TYPE (decl) assignment, apbianco */
3268 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3270 tree decl, value; \
3271 int index = OPVALUE; \
3272 tree type = OPTYPE; \
3273 value = pop_value (type); \
3274 type = TREE_TYPE (value); \
3275 decl = find_local_variable (index, type, oldpc); \
3276 set_local_type (index, type); \
3277 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3278 update_aliases (decl, index, PC); \
3281 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3283 /* have to do this since OPERAND_VALUE may have side-effects */ \
3284 int opvalue = OPERAND_VALUE; \
3285 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3288 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3289 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3291 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3292 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3294 #define MONITOR_OPERATION(call) \
3296 tree o = pop_value (ptr_type_node); \
3297 tree c; \
3298 flush_quick_stack (); \
3299 c = build_java_monitor (call, o); \
3300 TREE_SIDE_EFFECTS (c) = 1; \
3301 java_add_stmt (c); \
3304 #define SPECIAL_IINC(IGNORED) \
3306 unsigned int local_var_index = IMMEDIATE_u1; \
3307 int ival = IMMEDIATE_s1; \
3308 expand_iinc(local_var_index, ival, oldpc); \
3311 #define SPECIAL_WIDE(IGNORED) \
3313 int modified_opcode = IMMEDIATE_u1; \
3314 unsigned int local_var_index = IMMEDIATE_u2; \
3315 switch (modified_opcode) \
3317 case OPCODE_iinc: \
3319 int ival = IMMEDIATE_s2; \
3320 expand_iinc (local_var_index, ival, oldpc); \
3321 break; \
3323 case OPCODE_iload: \
3324 case OPCODE_lload: \
3325 case OPCODE_fload: \
3326 case OPCODE_dload: \
3327 case OPCODE_aload: \
3329 /* duplicate code from LOAD macro */ \
3330 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3331 break; \
3333 case OPCODE_istore: \
3334 case OPCODE_lstore: \
3335 case OPCODE_fstore: \
3336 case OPCODE_dstore: \
3337 case OPCODE_astore: \
3339 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3340 break; \
3342 default: \
3343 error ("unrecogized wide sub-instruction"); \
3347 #define SPECIAL_THROW(IGNORED) \
3348 build_java_athrow (pop_value (throwable_type_node))
3350 #define SPECIAL_BREAK NOT_IMPL1
3351 #define IMPL NOT_IMPL
3353 #include "javaop.def"
3354 #undef JAVAOP
3355 default:
3356 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3358 return PC;
3361 /* Return the opcode at PC in the code section pointed to by
3362 CODE_OFFSET. */
3364 static unsigned char
3365 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3367 unsigned char opcode;
3368 long absolute_offset = (long)JCF_TELL (jcf);
3370 JCF_SEEK (jcf, code_offset);
3371 opcode = jcf->read_ptr [pc];
3372 JCF_SEEK (jcf, absolute_offset);
3373 return opcode;
3376 /* Some bytecode compilers are emitting accurate LocalVariableTable
3377 attributes. Here's an example:
3379 PC <t>store_<n>
3380 PC+1 ...
3382 Attribute "LocalVariableTable"
3383 slot #<n>: ... (PC: PC+1 length: L)
3385 This is accurate because the local in slot <n> really exists after
3386 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3388 This procedure recognizes this situation and extends the live range
3389 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3390 length of the store instruction.)
3392 This function is used by `give_name_to_locals' so that a local's
3393 DECL features a DECL_LOCAL_START_PC such that the first related
3394 store operation will use DECL as a destination, not a unrelated
3395 temporary created for the occasion.
3397 This function uses a global (instruction_bits) `note_instructions' should
3398 have allocated and filled properly. */
3401 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3402 int start_pc, int slot)
3404 int first, index, opcode;
3405 int pc, insn_pc;
3406 int wide_found = 0;
3408 if (!start_pc)
3409 return start_pc;
3411 first = index = -1;
3413 /* Find last previous instruction and remember it */
3414 for (pc = start_pc-1; pc; pc--)
3415 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3416 break;
3417 insn_pc = pc;
3419 /* Retrieve the instruction, handle `wide'. */
3420 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3421 if (opcode == OPCODE_wide)
3423 wide_found = 1;
3424 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3427 switch (opcode)
3429 case OPCODE_astore_0:
3430 case OPCODE_astore_1:
3431 case OPCODE_astore_2:
3432 case OPCODE_astore_3:
3433 first = OPCODE_astore_0;
3434 break;
3436 case OPCODE_istore_0:
3437 case OPCODE_istore_1:
3438 case OPCODE_istore_2:
3439 case OPCODE_istore_3:
3440 first = OPCODE_istore_0;
3441 break;
3443 case OPCODE_lstore_0:
3444 case OPCODE_lstore_1:
3445 case OPCODE_lstore_2:
3446 case OPCODE_lstore_3:
3447 first = OPCODE_lstore_0;
3448 break;
3450 case OPCODE_fstore_0:
3451 case OPCODE_fstore_1:
3452 case OPCODE_fstore_2:
3453 case OPCODE_fstore_3:
3454 first = OPCODE_fstore_0;
3455 break;
3457 case OPCODE_dstore_0:
3458 case OPCODE_dstore_1:
3459 case OPCODE_dstore_2:
3460 case OPCODE_dstore_3:
3461 first = OPCODE_dstore_0;
3462 break;
3464 case OPCODE_astore:
3465 case OPCODE_istore:
3466 case OPCODE_lstore:
3467 case OPCODE_fstore:
3468 case OPCODE_dstore:
3469 index = peek_opcode_at_pc (jcf, code_offset, pc);
3470 if (wide_found)
3472 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3473 index = (other << 8) + index;
3475 break;
3478 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3479 means we have a <t>store. */
3480 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3481 start_pc = insn_pc;
3483 return start_pc;
3486 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3487 order, as specified by Java Language Specification.
3489 The problem is that while expand_expr will evaluate its sub-operands in
3490 left-to-right order, for variables it will just return an rtx (i.e.
3491 an lvalue) for the variable (rather than an rvalue). So it is possible
3492 that a later sub-operand will change the register, and when the
3493 actual operation is done, it will use the new value, when it should
3494 have used the original value.
3496 We fix this by using save_expr. This forces the sub-operand to be
3497 copied into a fresh virtual register,
3499 For method invocation, we modify the arguments so that a
3500 left-to-right order evaluation is performed. Saved expressions
3501 will, in CALL_EXPR order, be reused when the call will be expanded.
3504 tree
3505 force_evaluation_order (tree node)
3507 if (flag_syntax_only)
3508 return node;
3509 if (TREE_CODE (node) == CALL_EXPR
3510 || TREE_CODE (node) == NEW_CLASS_EXPR
3511 || (TREE_CODE (node) == COMPOUND_EXPR
3512 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3513 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3515 tree arg, cmp;
3517 arg = node;
3519 /* Position arg properly, account for wrapped around ctors. */
3520 if (TREE_CODE (node) == COMPOUND_EXPR)
3521 arg = TREE_OPERAND (node, 0);
3523 arg = TREE_OPERAND (arg, 1);
3525 /* An empty argument list is ok, just ignore it. */
3526 if (!arg)
3527 return node;
3529 /* Not having a list of arguments here is an error. */
3530 if (TREE_CODE (arg) != TREE_LIST)
3531 abort ();
3533 /* This reverses the evaluation order. This is a desired effect. */
3534 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3536 tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3537 cmp = (cmp == NULL_TREE ? saved :
3538 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3539 TREE_VALUE (arg) = saved;
3542 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3543 TREE_SIDE_EFFECTS (cmp) = 1;
3545 if (cmp)
3547 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3548 if (TREE_TYPE (cmp) != void_type_node)
3549 cmp = save_expr (cmp);
3550 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3551 TREE_SIDE_EFFECTS (cmp) = 1;
3552 node = cmp;
3555 return node;
3558 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3559 location where an expression or an identifier were encountered. It
3560 is necessary for languages where the frontend parser will handle
3561 recursively more than one file (Java is one of them). */
3563 tree
3564 build_expr_wfl (tree node,
3565 #ifdef USE_MAPPED_LOCATION
3566 source_location location
3567 #else
3568 const char *file, int line, int col
3569 #endif
3572 tree wfl;
3574 #ifdef USE_MAPPED_LOCATION
3575 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3576 SET_EXPR_LOCATION (wfl, location);
3577 #else
3578 static const char *last_file = 0;
3579 static tree last_filenode = NULL_TREE;
3581 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3583 EXPR_WFL_SET_LINECOL (wfl, line, col);
3584 if (file != last_file)
3586 last_file = file;
3587 last_filenode = file ? get_identifier (file) : NULL_TREE;
3589 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3590 #endif
3591 EXPR_WFL_NODE (wfl) = node;
3592 if (node)
3594 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3595 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3596 TREE_TYPE (wfl) = TREE_TYPE (node);
3599 return wfl;
3602 #ifdef USE_MAPPED_LOCATION
3603 tree
3604 expr_add_location (tree node, source_location location, bool statement)
3606 tree wfl;
3607 #if 0
3608 /* FIXME. This optimization causes failures in code that expects an
3609 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3610 if (node && ! (statement && flag_emit_class_files))
3612 source_location node_loc = EXPR_LOCATION (node);
3613 if (node_loc == location || location == UNKNOWN_LOCATION)
3614 return node;
3615 if (node_loc == UNKNOWN_LOCATION
3616 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3618 SET_EXPR_LOCATION (node, location);
3619 return node;
3622 #endif
3623 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3624 SET_EXPR_LOCATION (wfl, location);
3625 EXPR_WFL_NODE (wfl) = node;
3626 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3627 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3628 if (node)
3630 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3631 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3632 TREE_TYPE (wfl) = TREE_TYPE (node);
3635 return wfl;
3637 #endif
3639 /* Build a node to represent empty statements and blocks. */
3641 tree
3642 build_java_empty_stmt (void)
3644 tree t = build_empty_stmt ();
3645 CAN_COMPLETE_NORMALLY (t) = 1;
3646 return t;
3649 /* Promote all args of integral type before generating any code. */
3651 static void
3652 promote_arguments (void)
3654 int i;
3655 tree arg;
3656 for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
3657 arg != NULL_TREE; arg = TREE_CHAIN (arg), i++)
3659 tree arg_type = TREE_TYPE (arg);
3660 if (INTEGRAL_TYPE_P (arg_type)
3661 && TYPE_PRECISION (arg_type) < 32)
3663 tree copy = find_local_variable (i, integer_type_node, -1);
3664 java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
3665 copy,
3666 fold_convert (integer_type_node, arg)));
3668 if (TYPE_IS_WIDE (arg_type))
3669 i++;
3673 #include "gt-java-expr.h"