Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / java / expr.c
blobe7b299245a1476486eebe642ec74adcff035c67e
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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "rtl.h"
35 #include "flags.h"
36 #include "expr.h"
37 #include "java-tree.h"
38 #include "javaop.h"
39 #include "java-opcodes.h"
40 #include "jcf.h"
41 #include "java-except.h"
42 #include "parse.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "ggc.h"
46 #include "tree-gimple.h"
47 #include "target.h"
49 static void flush_quick_stack (void);
50 static void push_value (tree);
51 static tree pop_value (tree);
52 static void java_stack_swap (void);
53 static void java_stack_dup (int, int);
54 static void build_java_athrow (tree);
55 static void build_java_jsr (int, int);
56 static void build_java_ret (tree);
57 static void expand_java_multianewarray (tree, int);
58 static void expand_java_arraystore (tree);
59 static void expand_java_arrayload (tree);
60 static void expand_java_array_length (void);
61 static tree build_java_monitor (tree, tree);
62 static void expand_java_pushc (int, tree);
63 static void expand_java_return (tree);
64 static void expand_load_internal (int, tree, int);
65 static void expand_java_NEW (tree);
66 static void expand_java_INSTANCEOF (tree);
67 static void expand_java_CHECKCAST (tree);
68 static void expand_iinc (unsigned int, int, int);
69 static void expand_java_binop (tree, enum tree_code);
70 static void note_label (int, int);
71 static void expand_compare (enum tree_code, tree, tree, int);
72 static void expand_test (enum tree_code, tree, int);
73 static void expand_cond (enum tree_code, tree, int);
74 static void expand_java_goto (int);
75 static tree expand_java_switch (tree, int);
76 static void expand_java_add_case (tree, int, int);
77 #if 0
78 static void expand_java_call (int, int);
79 static void expand_java_ret (tree);
80 #endif
81 static tree pop_arguments (tree);
82 static void expand_invoke (int, int, int);
83 static void expand_java_field_op (int, int, int);
84 static void java_push_constant_from_pool (struct JCF *, int);
85 static void java_stack_pop (int);
86 static tree build_java_throw_out_of_bounds_exception (tree);
87 static tree build_java_check_indexed_type (tree, tree);
88 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
89 static void promote_arguments (void);
91 static GTY(()) tree operand_type[59];
93 static GTY(()) tree methods_ident;
94 static GTY(()) tree ncode_ident;
95 tree dtable_ident = NULL_TREE;
97 /* Set to nonzero value in order to emit class initialization code
98 before static field references. */
99 int always_initialize_class_p = 0;
101 /* We store the stack state in two places:
102 Within a basic block, we use the quick_stack, which is a
103 pushdown list (TREE_LISTs) of expression nodes.
104 This is the top part of the stack; below that we use find_stack_slot.
105 At the end of a basic block, the quick_stack must be flushed
106 to the stack slot array (as handled by find_stack_slot).
107 Using quick_stack generates better code (especially when
108 compiled without optimization), because we do not have to
109 explicitly store and load trees to temporary variables.
111 If a variable is on the quick stack, it means the value of variable
112 when the quick stack was last flushed. Conceptually, flush_quick_stack
113 saves all the quick_stack elements in parallel. However, that is
114 complicated, so it actually saves them (i.e. copies each stack value
115 to is home virtual register) from low indexes. This allows a quick_stack
116 element at index i (counting from the bottom of stack the) to references
117 slot virtuals for register that are >= i, but not those that are deeper.
118 This convention makes most operations easier. For example iadd works
119 even when the stack contains (reg[0], reg[1]): It results in the
120 stack containing (reg[0]+reg[1]), which is OK. However, some stack
121 operations are more complicated. For example dup given a stack
122 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
123 the convention, since stack value 1 would refer to a register with
124 lower index (reg[0]), which flush_quick_stack does not safely handle.
125 So dup cannot just add an extra element to the quick_stack, but iadd can.
128 static GTY(()) tree quick_stack;
130 /* A free-list of unused permanent TREE_LIST nodes. */
131 static GTY((deletable)) tree tree_list_free_list;
133 /* The stack pointer of the Java virtual machine.
134 This does include the size of the quick_stack. */
136 int stack_pointer;
138 const unsigned char *linenumber_table;
139 int linenumber_count;
141 /* Largest pc so far in this method that has been passed to lookup_label. */
142 int highest_label_pc_this_method = -1;
144 /* Base value for this method to add to pc to get generated label. */
145 int start_label_pc_this_method = 0;
147 void
148 init_expr_processing (void)
150 operand_type[21] = operand_type[54] = int_type_node;
151 operand_type[22] = operand_type[55] = long_type_node;
152 operand_type[23] = operand_type[56] = float_type_node;
153 operand_type[24] = operand_type[57] = double_type_node;
154 operand_type[25] = operand_type[58] = ptr_type_node;
157 tree
158 java_truthvalue_conversion (tree expr)
160 /* It is simpler and generates better code to have only TRUTH_*_EXPR
161 or comparison expressions as truth values at this level.
163 This function should normally be identity for Java. */
165 switch (TREE_CODE (expr))
167 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
168 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
169 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
170 case ORDERED_EXPR: case UNORDERED_EXPR:
171 case TRUTH_ANDIF_EXPR:
172 case TRUTH_ORIF_EXPR:
173 case TRUTH_AND_EXPR:
174 case TRUTH_OR_EXPR:
175 case TRUTH_XOR_EXPR:
176 case TRUTH_NOT_EXPR:
177 case ERROR_MARK:
178 return expr;
180 case INTEGER_CST:
181 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
183 case REAL_CST:
184 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
186 /* are these legal? XXX JH */
187 case NEGATE_EXPR:
188 case ABS_EXPR:
189 case FLOAT_EXPR:
190 /* These don't change whether an object is nonzero or zero. */
191 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
193 case COND_EXPR:
194 /* Distribute the conversion into the arms of a COND_EXPR. */
195 return fold_build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
196 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
197 java_truthvalue_conversion (TREE_OPERAND (expr, 2)));
199 case NOP_EXPR:
200 /* If this is widening the argument, we can ignore it. */
201 if (TYPE_PRECISION (TREE_TYPE (expr))
202 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
203 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
204 /* fall through to default */
206 default:
207 return fold_build2 (NE_EXPR, boolean_type_node,
208 expr, boolean_false_node);
212 /* Save any stack slots that happen to be in the quick_stack into their
213 home virtual register slots.
215 The copy order is from low stack index to high, to support the invariant
216 that the expression for a slot may contain decls for stack slots with
217 higher (or the same) index, but not lower. */
219 static void
220 flush_quick_stack (void)
222 int stack_index = stack_pointer;
223 tree prev, cur, next;
225 /* First reverse the quick_stack, and count the number of slots it has. */
226 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
228 next = TREE_CHAIN (cur);
229 TREE_CHAIN (cur) = prev;
230 prev = cur;
231 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
233 quick_stack = prev;
235 while (quick_stack != NULL_TREE)
237 tree decl;
238 tree node = quick_stack, type;
239 quick_stack = TREE_CHAIN (node);
240 TREE_CHAIN (node) = tree_list_free_list;
241 tree_list_free_list = node;
242 node = TREE_VALUE (node);
243 type = TREE_TYPE (node);
245 decl = find_stack_slot (stack_index, type);
246 if (decl != node)
247 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
248 stack_index += 1 + TYPE_IS_WIDE (type);
252 /* Push TYPE on the type stack.
253 Return true on success, 0 on overflow. */
256 push_type_0 (tree type)
258 int n_words;
259 type = promote_type (type);
260 n_words = 1 + TYPE_IS_WIDE (type);
261 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
262 return 0;
263 /* Allocate decl for this variable now, so we get a temporary that
264 survives the whole method. */
265 find_stack_slot (stack_pointer, type);
266 stack_type_map[stack_pointer++] = type;
267 n_words--;
268 while (--n_words >= 0)
269 stack_type_map[stack_pointer++] = TYPE_SECOND;
270 return 1;
273 void
274 push_type (tree type)
276 if (! push_type_0 (type))
277 abort ();
280 static void
281 push_value (tree value)
283 tree type = TREE_TYPE (value);
284 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
286 type = promote_type (type);
287 value = convert (type, value);
289 push_type (type);
290 if (tree_list_free_list == NULL_TREE)
291 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
292 else
294 tree node = tree_list_free_list;
295 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
296 TREE_VALUE (node) = value;
297 TREE_CHAIN (node) = quick_stack;
298 quick_stack = node;
302 /* Pop a type from the type stack.
303 TYPE is the expected type. Return the actual type, which must be
304 convertible to TYPE.
305 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
307 tree
308 pop_type_0 (tree type, char **messagep)
310 int n_words;
311 tree t;
312 *messagep = NULL;
313 if (TREE_CODE (type) == RECORD_TYPE)
314 type = promote_type (type);
315 n_words = 1 + TYPE_IS_WIDE (type);
316 if (stack_pointer < n_words)
318 *messagep = xstrdup ("stack underflow");
319 return type;
321 while (--n_words > 0)
323 if (stack_type_map[--stack_pointer] != void_type_node)
325 *messagep = xstrdup ("Invalid multi-word value on type stack");
326 return type;
329 t = stack_type_map[--stack_pointer];
330 if (type == NULL_TREE || t == type)
331 return t;
332 if (TREE_CODE (t) == TREE_LIST)
336 tree tt = TREE_PURPOSE (t);
337 if (! can_widen_reference_to (tt, type))
339 t = tt;
340 goto fail;
342 t = TREE_CHAIN (t);
344 while (t);
345 return t;
347 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
348 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
349 return t;
350 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
352 /* If the expected type we've been passed is object or ptr
353 (i.e. void*), the caller needs to know the real type. */
354 if (type == ptr_type_node || type == object_ptr_type_node)
355 return t;
357 /* Since the verifier has already run, we know that any
358 types we see will be compatible. In BC mode, this fact
359 may be checked at runtime, but if that is so then we can
360 assume its truth here as well. So, we always succeed
361 here, with the expected type. */
362 return type;
365 if (! flag_verify_invocations && flag_indirect_dispatch
366 && t == object_ptr_type_node)
368 if (type != ptr_type_node)
369 warning (0, "need to insert runtime check for %s",
370 xstrdup (lang_printable_name (type, 0)));
371 return type;
374 /* lang_printable_name uses a static buffer, so we must save the result
375 from calling it the first time. */
376 fail:
378 char *temp = xstrdup (lang_printable_name (type, 0));
379 /* If the stack contains a multi-word type, keep popping the stack until
380 the real type is found. */
381 while (t == void_type_node)
382 t = stack_type_map[--stack_pointer];
383 *messagep = concat ("expected type '", temp,
384 "' but stack contains '", lang_printable_name (t, 0),
385 "'", NULL);
386 free (temp);
388 return type;
391 /* Pop a type from the type stack.
392 TYPE is the expected type. Return the actual type, which must be
393 convertible to TYPE, otherwise call error. */
395 tree
396 pop_type (tree type)
398 char *message = NULL;
399 type = pop_type_0 (type, &message);
400 if (message != NULL)
402 error ("%s", message);
403 free (message);
405 return type;
409 /* Return true if two type assertions are equal. */
411 static int
412 type_assertion_eq (const void * k1_p, const void * k2_p)
414 type_assertion k1 = *(type_assertion *)k1_p;
415 type_assertion k2 = *(type_assertion *)k2_p;
416 return (k1.assertion_code == k2.assertion_code
417 && k1.op1 == k2.op1
418 && k1.op2 == k2.op2);
421 /* Hash a type assertion. */
423 static hashval_t
424 type_assertion_hash (const void *p)
426 const type_assertion *k_p = p;
427 hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
428 k_p->assertion_code, 0);
429 hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
430 return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
433 /* Add an entry to the type assertion table for the given class.
434 CLASS is the class for which this assertion will be evaluated by the
435 runtime during loading/initialization.
436 ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
437 OP1 and OP2 are the operands. The tree type of these arguments may be
438 specific to each assertion_code. */
440 void
441 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
443 htab_t assertions_htab;
444 type_assertion as;
445 void **as_pp;
447 assertions_htab = TYPE_ASSERTIONS (class);
448 if (assertions_htab == NULL)
450 assertions_htab = htab_create_ggc (7, type_assertion_hash,
451 type_assertion_eq, NULL);
452 TYPE_ASSERTIONS (current_class) = assertions_htab;
455 as.assertion_code = assertion_code;
456 as.op1 = op1;
457 as.op2 = op2;
459 as_pp = htab_find_slot (assertions_htab, &as, INSERT);
461 /* Don't add the same assertion twice. */
462 if (*as_pp)
463 return;
465 *as_pp = ggc_alloc (sizeof (type_assertion));
466 **(type_assertion **)as_pp = as;
470 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
471 Handles array types and interfaces. */
474 can_widen_reference_to (tree source_type, tree target_type)
476 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
477 return 1;
479 /* Get rid of pointers */
480 if (TREE_CODE (source_type) == POINTER_TYPE)
481 source_type = TREE_TYPE (source_type);
482 if (TREE_CODE (target_type) == POINTER_TYPE)
483 target_type = TREE_TYPE (target_type);
485 if (source_type == target_type)
486 return 1;
488 /* FIXME: This is very pessimistic, in that it checks everything,
489 even if we already know that the types are compatible. If we're
490 to support full Java class loader semantics, we need this.
491 However, we could do something more optimal. */
492 if (! flag_verify_invocations)
494 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
495 source_type, target_type);
497 if (!quiet_flag)
498 warning (0, "assert: %s is assign compatible with %s",
499 xstrdup (lang_printable_name (target_type, 0)),
500 xstrdup (lang_printable_name (source_type, 0)));
501 /* Punt everything to runtime. */
502 return 1;
505 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
507 return 1;
509 else
511 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
513 HOST_WIDE_INT source_length, target_length;
514 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
516 /* An array implements Cloneable and Serializable. */
517 tree name = DECL_NAME (TYPE_NAME (target_type));
518 return (name == java_lang_cloneable_identifier_node
519 || name == java_io_serializable_identifier_node);
521 target_length = java_array_type_length (target_type);
522 if (target_length >= 0)
524 source_length = java_array_type_length (source_type);
525 if (source_length != target_length)
526 return 0;
528 source_type = TYPE_ARRAY_ELEMENT (source_type);
529 target_type = TYPE_ARRAY_ELEMENT (target_type);
530 if (source_type == target_type)
531 return 1;
532 if (TREE_CODE (source_type) != POINTER_TYPE
533 || TREE_CODE (target_type) != POINTER_TYPE)
534 return 0;
535 return can_widen_reference_to (source_type, target_type);
537 else
539 int source_depth = class_depth (source_type);
540 int target_depth = class_depth (target_type);
542 if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
544 if (! quiet_flag)
545 warning (0, "assert: %s is assign compatible with %s",
546 xstrdup (lang_printable_name (target_type, 0)),
547 xstrdup (lang_printable_name (source_type, 0)));
548 return 1;
551 /* class_depth can return a negative depth if an error occurred */
552 if (source_depth < 0 || target_depth < 0)
553 return 0;
555 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
557 /* target_type is OK if source_type or source_type ancestors
558 implement target_type. We handle multiple sub-interfaces */
559 tree binfo, base_binfo;
560 int i;
562 for (binfo = TYPE_BINFO (source_type), i = 0;
563 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
564 if (can_widen_reference_to
565 (BINFO_TYPE (base_binfo), target_type))
566 return 1;
568 if (!i)
569 return 0;
572 for ( ; source_depth > target_depth; source_depth--)
574 source_type
575 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
577 return source_type == target_type;
582 static tree
583 pop_value (tree type)
585 type = pop_type (type);
586 if (quick_stack)
588 tree node = quick_stack;
589 quick_stack = TREE_CHAIN (quick_stack);
590 TREE_CHAIN (node) = tree_list_free_list;
591 tree_list_free_list = node;
592 node = TREE_VALUE (node);
593 return node;
595 else
596 return find_stack_slot (stack_pointer, promote_type (type));
600 /* Pop and discard the top COUNT stack slots. */
602 static void
603 java_stack_pop (int count)
605 while (count > 0)
607 tree type, val;
609 if (stack_pointer == 0)
610 abort ();
612 type = stack_type_map[stack_pointer - 1];
613 if (type == TYPE_SECOND)
615 count--;
616 if (stack_pointer == 1 || count <= 0)
617 abort ();
619 type = stack_type_map[stack_pointer - 2];
621 val = pop_value (type);
622 count--;
626 /* Implement the 'swap' operator (to swap two top stack slots). */
628 static void
629 java_stack_swap (void)
631 tree type1, type2;
632 tree temp;
633 tree decl1, decl2;
635 if (stack_pointer < 2
636 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
637 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
638 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
639 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
640 /* Bad stack swap. */
641 abort ();
643 flush_quick_stack ();
644 decl1 = find_stack_slot (stack_pointer - 1, type1);
645 decl2 = find_stack_slot (stack_pointer - 2, type2);
646 temp = build_decl (VAR_DECL, NULL_TREE, type1);
647 java_add_local_var (temp);
648 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
649 java_add_stmt (build2 (MODIFY_EXPR, type2,
650 find_stack_slot (stack_pointer - 1, type2),
651 decl2));
652 java_add_stmt (build2 (MODIFY_EXPR, type1,
653 find_stack_slot (stack_pointer - 2, type1),
654 temp));
655 stack_type_map[stack_pointer - 1] = type2;
656 stack_type_map[stack_pointer - 2] = type1;
659 static void
660 java_stack_dup (int size, int offset)
662 int low_index = stack_pointer - size - offset;
663 int dst_index;
664 if (low_index < 0)
665 error ("stack underflow - dup* operation");
667 flush_quick_stack ();
669 stack_pointer += size;
670 dst_index = stack_pointer;
672 for (dst_index = stack_pointer; --dst_index >= low_index; )
674 tree type;
675 int src_index = dst_index - size;
676 if (src_index < low_index)
677 src_index = dst_index + size + offset;
678 type = stack_type_map [src_index];
679 if (type == TYPE_SECOND)
681 if (src_index <= low_index)
682 /* Dup operation splits 64-bit number. */
683 abort ();
685 stack_type_map[dst_index] = type;
686 src_index--; dst_index--;
687 type = stack_type_map[src_index];
688 if (! TYPE_IS_WIDE (type))
689 abort ();
691 else if (TYPE_IS_WIDE (type))
692 abort ();
694 if (src_index != dst_index)
696 tree src_decl = find_stack_slot (src_index, type);
697 tree dst_decl = find_stack_slot (dst_index, type);
699 java_add_stmt
700 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
701 stack_type_map[dst_index] = type;
706 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
707 value stack. */
709 static void
710 build_java_athrow (tree node)
712 tree call;
714 call = build3 (CALL_EXPR,
715 void_type_node,
716 build_address_of (throw_node),
717 build_tree_list (NULL_TREE, node),
718 NULL_TREE);
719 TREE_SIDE_EFFECTS (call) = 1;
720 java_add_stmt (call);
721 java_stack_pop (stack_pointer);
724 /* Implementation for jsr/ret */
726 static void
727 build_java_jsr (int target_pc, int return_pc)
729 tree where = lookup_label (target_pc);
730 tree ret = lookup_label (return_pc);
731 tree ret_label = fold_build1 (ADDR_EXPR, return_address_type_node, ret);
732 push_value (ret_label);
733 flush_quick_stack ();
734 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
736 /* Do not need to emit the label here. We noted the existence of the
737 label as a jump target in note_instructions; we'll emit the label
738 for real at the beginning of the expand_byte_code loop. */
741 static void
742 build_java_ret (tree location)
744 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
747 /* Implementation of operations on array: new, load, store, length */
749 tree
750 decode_newarray_type (int atype)
752 switch (atype)
754 case 4: return boolean_type_node;
755 case 5: return char_type_node;
756 case 6: return float_type_node;
757 case 7: return double_type_node;
758 case 8: return byte_type_node;
759 case 9: return short_type_node;
760 case 10: return int_type_node;
761 case 11: return long_type_node;
762 default: return NULL_TREE;
766 /* Map primitive type to the code used by OPCODE_newarray. */
769 encode_newarray_type (tree type)
771 if (type == boolean_type_node)
772 return 4;
773 else if (type == char_type_node)
774 return 5;
775 else if (type == float_type_node)
776 return 6;
777 else if (type == double_type_node)
778 return 7;
779 else if (type == byte_type_node)
780 return 8;
781 else if (type == short_type_node)
782 return 9;
783 else if (type == int_type_node)
784 return 10;
785 else if (type == long_type_node)
786 return 11;
787 else
788 abort ();
791 /* Build a call to _Jv_ThrowBadArrayIndex(), the
792 ArrayIndexOfBoundsException exception handler. */
794 static tree
795 build_java_throw_out_of_bounds_exception (tree index)
797 tree node = build3 (CALL_EXPR, int_type_node,
798 build_address_of (soft_badarrayindex_node),
799 build_tree_list (NULL_TREE, index), NULL_TREE);
800 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
801 return (node);
804 /* Return the length of an array. Doesn't perform any checking on the nature
805 or value of the array NODE. May be used to implement some bytecodes. */
807 tree
808 build_java_array_length_access (tree node)
810 tree type = TREE_TYPE (node);
811 tree array_type = TREE_TYPE (type);
812 HOST_WIDE_INT length;
814 if (!is_array_type_p (type))
816 /* With the new verifier, we will see an ordinary pointer type
817 here. In this case, we just use an arbitrary array type. */
818 array_type = build_java_array_type (object_ptr_type_node, -1);
819 type = promote_type (array_type);
822 length = java_array_type_length (type);
823 if (length >= 0)
824 return build_int_cst (NULL_TREE, length);
826 node = build3 (COMPONENT_REF, int_type_node,
827 build_java_indirect_ref (array_type, node,
828 flag_check_references),
829 lookup_field (&array_type, get_identifier ("length")),
830 NULL_TREE);
831 IS_ARRAY_LENGTH_ACCESS (node) = 1;
832 return node;
835 /* Optionally checks a reference against the NULL pointer. ARG1: the
836 expr, ARG2: we should check the reference. Don't generate extra
837 checks if we're not generating code. */
839 tree
840 java_check_reference (tree expr, int check)
842 if (!flag_syntax_only && check)
844 expr = save_expr (expr);
845 expr = build3 (COND_EXPR, TREE_TYPE (expr),
846 build2 (EQ_EXPR, boolean_type_node,
847 expr, null_pointer_node),
848 build3 (CALL_EXPR, void_type_node,
849 build_address_of (soft_nullpointer_node),
850 NULL_TREE, NULL_TREE),
851 expr);
854 return expr;
857 /* Reference an object: just like an INDIRECT_REF, but with checking. */
859 tree
860 build_java_indirect_ref (tree type, tree expr, int check)
862 tree t;
863 t = java_check_reference (expr, check);
864 t = convert (build_pointer_type (type), t);
865 return build1 (INDIRECT_REF, type, t);
868 /* Implement array indexing (either as l-value or r-value).
869 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
870 Optionally performs bounds checking and/or test to NULL.
871 At this point, ARRAY should have been verified as an array. */
873 tree
874 build_java_arrayaccess (tree array, tree type, tree index)
876 tree node, throw = NULL_TREE;
877 tree data_field;
878 tree ref;
879 tree array_type = TREE_TYPE (TREE_TYPE (array));
881 if (!is_array_type_p (TREE_TYPE (array)))
883 /* With the new verifier, we will see an ordinary pointer type
884 here. In this case, we just use the correct array type. */
885 array_type = build_java_array_type (type, -1);
888 if (flag_bounds_check)
890 /* Generate:
891 * (unsigned jint) INDEX >= (unsigned jint) LEN
892 * && throw ArrayIndexOutOfBoundsException.
893 * Note this is equivalent to and more efficient than:
894 * INDEX < 0 || INDEX >= LEN && throw ... */
895 tree test;
896 tree len = convert (unsigned_int_type_node,
897 build_java_array_length_access (array));
898 test = fold_build2 (GE_EXPR, boolean_type_node,
899 convert (unsigned_int_type_node, index),
900 len);
901 if (! integer_zerop (test))
903 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
904 build_java_throw_out_of_bounds_exception (index));
905 /* allows expansion within COMPOUND */
906 TREE_SIDE_EFFECTS( throw ) = 1;
910 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
911 to have the bounds check evaluated first. */
912 if (throw != NULL_TREE)
913 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
915 data_field = lookup_field (&array_type, get_identifier ("data"));
917 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
918 build_java_indirect_ref (array_type, array,
919 flag_check_references),
920 data_field, NULL_TREE);
922 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
923 return node;
926 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
927 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
928 determine that no check is required. */
930 tree
931 build_java_arraystore_check (tree array, tree object)
933 tree check, element_type, source;
934 tree array_type_p = TREE_TYPE (array);
935 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
937 if (! flag_verify_invocations)
939 /* With the new verifier, we don't track precise types. FIXME:
940 performance regression here. */
941 element_type = TYPE_NAME (object_type_node);
943 else
945 if (! is_array_type_p (array_type_p))
946 abort ();
948 /* Get the TYPE_DECL for ARRAY's element type. */
949 element_type
950 = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
953 if (TREE_CODE (element_type) != TYPE_DECL
954 || TREE_CODE (object_type) != TYPE_DECL)
955 abort ();
957 if (!flag_store_check)
958 return build1 (NOP_EXPR, array_type_p, array);
960 /* No check is needed if the element type is final. Also check that
961 element_type matches object_type, since in the bytecode
962 compilation case element_type may be the actual element type of
963 the array rather than its declared type. However, if we're doing
964 indirect dispatch, we can't do the `final' optimization. */
965 if (element_type == object_type
966 && ! flag_indirect_dispatch
967 && CLASS_FINAL (element_type))
968 return build1 (NOP_EXPR, array_type_p, array);
970 /* OBJECT might be wrapped by a SAVE_EXPR. */
971 if (TREE_CODE (object) == SAVE_EXPR)
972 source = TREE_OPERAND (object, 0);
973 else
974 source = object;
976 /* Avoid the check if OBJECT was just loaded from the same array. */
977 if (TREE_CODE (source) == ARRAY_REF)
979 tree target;
980 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
981 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
982 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
983 if (TREE_CODE (source) == SAVE_EXPR)
984 source = TREE_OPERAND (source, 0);
986 target = array;
987 if (TREE_CODE (target) == SAVE_EXPR)
988 target = TREE_OPERAND (target, 0);
990 if (source == target)
991 return build1 (NOP_EXPR, array_type_p, array);
994 /* Build an invocation of _Jv_CheckArrayStore */
995 check = build3 (CALL_EXPR, void_type_node,
996 build_address_of (soft_checkarraystore_node),
997 tree_cons (NULL_TREE, array,
998 build_tree_list (NULL_TREE, object)),
999 NULL_TREE);
1000 TREE_SIDE_EFFECTS (check) = 1;
1002 return check;
1005 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
1006 ARRAY_NODE. This function is used to retrieve something less vague than
1007 a pointer type when indexing the first dimension of something like [[<t>.
1008 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1009 return unchanged. */
1011 static tree
1012 build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED,
1013 tree indexed_type)
1015 /* We used to check to see if ARRAY_NODE really had array type.
1016 However, with the new verifier, this is not necessary, as we know
1017 that the object will be an array of the appropriate type. */
1019 return indexed_type;
1022 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
1023 called with an integer code (the type of array to create), and the length
1024 of the array to create. */
1026 tree
1027 build_newarray (int atype_value, tree length)
1029 tree type_arg;
1031 tree prim_type = decode_newarray_type (atype_value);
1032 tree type
1033 = build_java_array_type (prim_type,
1034 host_integerp (length, 0) == INTEGER_CST
1035 ? tree_low_cst (length, 0) : -1);
1037 /* If compiling to native, pass a reference to the primitive type class
1038 and save the runtime some work. However, the bytecode generator
1039 expects to find the type_code int here. */
1040 if (flag_emit_class_files)
1041 type_arg = build_int_cst (NULL_TREE, atype_value);
1042 else
1043 type_arg = build_class_ref (prim_type);
1045 return build3 (CALL_EXPR, promote_type (type),
1046 build_address_of (soft_newarray_node),
1047 tree_cons (NULL_TREE,
1048 type_arg,
1049 build_tree_list (NULL_TREE, length)),
1050 NULL_TREE);
1053 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1054 of the dimension. */
1056 tree
1057 build_anewarray (tree class_type, tree length)
1059 tree type
1060 = build_java_array_type (class_type,
1061 host_integerp (length, 0)
1062 ? tree_low_cst (length, 0) : -1);
1064 return build3 (CALL_EXPR, promote_type (type),
1065 build_address_of (soft_anewarray_node),
1066 tree_cons (NULL_TREE, length,
1067 tree_cons (NULL_TREE, build_class_ref (class_type),
1068 build_tree_list (NULL_TREE,
1069 null_pointer_node))),
1070 NULL_TREE);
1073 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1075 tree
1076 build_new_array (tree type, tree length)
1078 if (JPRIMITIVE_TYPE_P (type))
1079 return build_newarray (encode_newarray_type (type), length);
1080 else
1081 return build_anewarray (TREE_TYPE (type), length);
1084 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1085 class pointer, a number of dimensions and the matching number of
1086 dimensions. The argument list is NULL terminated. */
1088 static void
1089 expand_java_multianewarray (tree class_type, int ndim)
1091 int i;
1092 tree args = build_tree_list( NULL_TREE, null_pointer_node );
1094 for( i = 0; i < ndim; i++ )
1095 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1097 push_value (build3 (CALL_EXPR,
1098 promote_type (class_type),
1099 build_address_of (soft_multianewarray_node),
1100 tree_cons (NULL_TREE, build_class_ref (class_type),
1101 tree_cons (NULL_TREE,
1102 build_int_cst (NULL_TREE, ndim),
1103 args)),
1104 NULL_TREE));
1107 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1108 ARRAY is an array type. May expand some bound checking and NULL
1109 pointer checking. RHS_TYPE_NODE we are going to store. In the case
1110 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1111 INT. In those cases, we make the conversion.
1113 if ARRAy is a reference type, the assignment is checked at run-time
1114 to make sure that the RHS can be assigned to the array element
1115 type. It is not necessary to generate this code if ARRAY is final. */
1117 static void
1118 expand_java_arraystore (tree rhs_type_node)
1120 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
1121 && TYPE_PRECISION (rhs_type_node) <= 32) ?
1122 int_type_node : rhs_type_node);
1123 tree index = pop_value (int_type_node);
1124 tree array_type, array;
1126 /* If we're processing an `aaload' we might as well just pick
1127 `Object'. */
1128 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1130 array_type = build_java_array_type (object_ptr_type_node, -1);
1131 rhs_type_node = object_ptr_type_node;
1133 else
1134 array_type = build_java_array_type (rhs_type_node, -1);
1136 array = pop_value (array_type);
1137 array = build1 (NOP_EXPR, promote_type (array_type), array);
1139 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
1141 flush_quick_stack ();
1143 index = save_expr (index);
1144 array = save_expr (array);
1146 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1148 tree check = build_java_arraystore_check (array, rhs_node);
1149 java_add_stmt (check);
1152 array = build_java_arrayaccess (array, rhs_type_node, index);
1153 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1156 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1157 sure that LHS is an array type. May expand some bound checking and NULL
1158 pointer checking.
1159 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1160 BOOLEAN/SHORT, we push a promoted type back to the stack.
1163 static void
1164 expand_java_arrayload (tree lhs_type_node)
1166 tree load_node;
1167 tree index_node = pop_value (int_type_node);
1168 tree array_type;
1169 tree array_node;
1171 /* If we're processing an `aaload' we might as well just pick
1172 `Object'. */
1173 if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1175 array_type = build_java_array_type (object_ptr_type_node, -1);
1176 lhs_type_node = object_ptr_type_node;
1178 else
1179 array_type = build_java_array_type (lhs_type_node, -1);
1180 array_node = pop_value (array_type);
1181 array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1183 index_node = save_expr (index_node);
1184 array_node = save_expr (array_node);
1186 lhs_type_node = build_java_check_indexed_type (array_node,
1187 lhs_type_node);
1188 load_node = build_java_arrayaccess (array_node,
1189 lhs_type_node,
1190 index_node);
1191 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1192 load_node = fold_build1 (NOP_EXPR, int_type_node, load_node);
1193 push_value (load_node);
1196 /* Expands .length. Makes sure that we deal with and array and may expand
1197 a NULL check on the array object. */
1199 static void
1200 expand_java_array_length (void)
1202 tree array = pop_value (ptr_type_node);
1203 tree length = build_java_array_length_access (array);
1205 push_value (length);
1208 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1209 either soft_monitorenter_node or soft_monitorexit_node. */
1211 static tree
1212 build_java_monitor (tree call, tree object)
1214 return build3 (CALL_EXPR,
1215 void_type_node,
1216 build_address_of (call),
1217 build_tree_list (NULL_TREE, object),
1218 NULL_TREE);
1221 /* Emit code for one of the PUSHC instructions. */
1223 static void
1224 expand_java_pushc (int ival, tree type)
1226 tree value;
1227 if (type == ptr_type_node && ival == 0)
1228 value = null_pointer_node;
1229 else if (type == int_type_node || type == long_type_node)
1230 value = build_int_cst (type, ival);
1231 else if (type == float_type_node || type == double_type_node)
1233 REAL_VALUE_TYPE x;
1234 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1235 value = build_real (type, x);
1237 else
1238 abort ();
1240 push_value (value);
1243 static void
1244 expand_java_return (tree type)
1246 if (type == void_type_node)
1247 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1248 else
1250 tree retval = pop_value (type);
1251 tree res = DECL_RESULT (current_function_decl);
1252 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1254 /* Handle the situation where the native integer type is smaller
1255 than the JVM integer. It can happen for many cross compilers.
1256 The whole if expression just goes away if INT_TYPE_SIZE < 32
1257 is false. */
1258 if (INT_TYPE_SIZE < 32
1259 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1260 < GET_MODE_SIZE (TYPE_MODE (type))))
1261 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1263 TREE_SIDE_EFFECTS (retval) = 1;
1264 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1268 static void
1269 expand_load_internal (int index, tree type, int pc)
1271 tree copy;
1272 tree var = find_local_variable (index, type, pc);
1274 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1275 on the stack. If there is an assignment to this VAR_DECL between
1276 the stack push and the use, then the wrong code could be
1277 generated. To avoid this we create a new local and copy our
1278 value into it. Then we push this new local on the stack.
1279 Hopefully this all gets optimized out. */
1280 copy = build_decl (VAR_DECL, NULL_TREE, type);
1281 if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1282 && TREE_TYPE (copy) != TREE_TYPE (var))
1283 var = convert (type, var);
1284 java_add_local_var (copy);
1285 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1287 push_value (copy);
1290 tree
1291 build_address_of (tree value)
1293 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1296 bool
1297 class_has_finalize_method (tree type)
1299 tree super = CLASSTYPE_SUPER (type);
1301 if (super == NULL_TREE)
1302 return false; /* Every class with a real finalizer inherits */
1303 /* from java.lang.Object. */
1304 else
1305 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1308 tree
1309 java_create_object (tree type)
1311 tree alloc_node = (class_has_finalize_method (type)
1312 ? alloc_object_node
1313 : alloc_no_finalizer_node);
1315 return build3 (CALL_EXPR, promote_type (type),
1316 build_address_of (alloc_node),
1317 build_tree_list (NULL_TREE, build_class_ref (type)),
1318 NULL_TREE);
1321 static void
1322 expand_java_NEW (tree type)
1324 tree alloc_node;
1326 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1327 : alloc_no_finalizer_node);
1328 if (! CLASS_LOADED_P (type))
1329 load_class (type, 1);
1330 safe_layout_class (type);
1331 push_value (build3 (CALL_EXPR, promote_type (type),
1332 build_address_of (alloc_node),
1333 build_tree_list (NULL_TREE, build_class_ref (type)),
1334 NULL_TREE));
1337 /* This returns an expression which will extract the class of an
1338 object. */
1340 tree
1341 build_get_class (tree value)
1343 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1344 tree vtable_field = lookup_field (&object_type_node,
1345 get_identifier ("vtable"));
1346 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1347 build_java_indirect_ref (object_type_node, value,
1348 flag_check_references),
1349 vtable_field, NULL_TREE);
1350 return build3 (COMPONENT_REF, class_ptr_type,
1351 build1 (INDIRECT_REF, dtable_type, tmp),
1352 class_field, NULL_TREE);
1355 /* This builds the tree representation of the `instanceof' operator.
1356 It tries various tricks to optimize this in cases where types are
1357 known. */
1359 tree
1360 build_instanceof (tree value, tree type)
1362 tree expr;
1363 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1364 tree valtype = TREE_TYPE (TREE_TYPE (value));
1365 tree valclass = TYPE_NAME (valtype);
1366 tree klass;
1368 /* When compiling from bytecode, we need to ensure that TYPE has
1369 been loaded. */
1370 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1372 load_class (type, 1);
1373 safe_layout_class (type);
1374 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1375 return error_mark_node;
1377 klass = TYPE_NAME (type);
1379 if (type == object_type_node || inherits_from_p (valtype, type))
1381 /* Anything except `null' is an instance of Object. Likewise,
1382 if the object is known to be an instance of the class, then
1383 we only need to check for `null'. */
1384 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1386 else if (flag_verify_invocations
1387 && ! TYPE_ARRAY_P (type)
1388 && ! TYPE_ARRAY_P (valtype)
1389 && DECL_P (klass) && DECL_P (valclass)
1390 && ! CLASS_INTERFACE (valclass)
1391 && ! CLASS_INTERFACE (klass)
1392 && ! inherits_from_p (type, valtype)
1393 && (CLASS_FINAL (klass)
1394 || ! inherits_from_p (valtype, type)))
1396 /* The classes are from different branches of the derivation
1397 tree, so we immediately know the answer. */
1398 expr = boolean_false_node;
1400 else if (DECL_P (klass) && CLASS_FINAL (klass))
1402 tree save = save_expr (value);
1403 expr = build3 (COND_EXPR, itype,
1404 build2 (NE_EXPR, boolean_type_node,
1405 save, null_pointer_node),
1406 build2 (EQ_EXPR, itype,
1407 build_get_class (save),
1408 build_class_ref (type)),
1409 boolean_false_node);
1411 else
1413 expr = build3 (CALL_EXPR, itype,
1414 build_address_of (soft_instanceof_node),
1415 tree_cons (NULL_TREE, value,
1416 build_tree_list (NULL_TREE,
1417 build_class_ref (type))),
1418 NULL_TREE);
1420 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1421 return expr;
1424 static void
1425 expand_java_INSTANCEOF (tree type)
1427 tree value = pop_value (object_ptr_type_node);
1428 value = build_instanceof (value, type);
1429 push_value (value);
1432 static void
1433 expand_java_CHECKCAST (tree type)
1435 tree value = pop_value (ptr_type_node);
1436 value = build3 (CALL_EXPR, promote_type (type),
1437 build_address_of (soft_checkcast_node),
1438 tree_cons (NULL_TREE, build_class_ref (type),
1439 build_tree_list (NULL_TREE, value)),
1440 NULL_TREE);
1441 push_value (value);
1444 static void
1445 expand_iinc (unsigned int local_var_index, int ival, int pc)
1447 tree local_var, res;
1448 tree constant_value;
1450 flush_quick_stack ();
1451 local_var = find_local_variable (local_var_index, int_type_node, pc);
1452 constant_value = build_int_cst (NULL_TREE, ival);
1453 res = fold_build2 (PLUS_EXPR, int_type_node, local_var, constant_value);
1454 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1455 update_aliases (local_var, local_var_index, pc);
1459 tree
1460 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1462 tree call = NULL;
1463 tree arg1 = convert (type, op1);
1464 tree arg2 = convert (type, op2);
1466 if (type == int_type_node)
1468 switch (op)
1470 case TRUNC_DIV_EXPR:
1471 call = soft_idiv_node;
1472 break;
1473 case TRUNC_MOD_EXPR:
1474 call = soft_irem_node;
1475 break;
1476 default:
1477 break;
1480 else if (type == long_type_node)
1482 switch (op)
1484 case TRUNC_DIV_EXPR:
1485 call = soft_ldiv_node;
1486 break;
1487 case TRUNC_MOD_EXPR:
1488 call = soft_lrem_node;
1489 break;
1490 default:
1491 break;
1495 if (! call)
1496 abort ();
1498 call = build3 (CALL_EXPR, type,
1499 build_address_of (call),
1500 tree_cons (NULL_TREE, arg1,
1501 build_tree_list (NULL_TREE, arg2)),
1502 NULL_TREE);
1504 return call;
1507 tree
1508 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1510 tree mask;
1511 switch (op)
1513 case URSHIFT_EXPR:
1515 tree u_type = java_unsigned_type (type);
1516 arg1 = convert (u_type, arg1);
1517 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1518 return convert (type, arg1);
1520 case LSHIFT_EXPR:
1521 case RSHIFT_EXPR:
1522 mask = build_int_cst (NULL_TREE,
1523 TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1524 arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
1525 break;
1527 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1528 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1529 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1531 tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1532 boolean_type_node, arg1, arg2);
1533 tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
1534 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1535 ifexp2, integer_zero_node,
1536 op == COMPARE_L_EXPR
1537 ? integer_minus_one_node
1538 : integer_one_node);
1539 return fold_build3 (COND_EXPR, int_type_node, ifexp1,
1540 op == COMPARE_L_EXPR ? integer_one_node
1541 : integer_minus_one_node,
1542 second_compare);
1544 case COMPARE_EXPR:
1545 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1547 tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
1548 tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
1549 tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1550 ifexp2, integer_one_node,
1551 integer_zero_node);
1552 return fold_build3 (COND_EXPR, int_type_node,
1553 ifexp1, integer_minus_one_node, second_compare);
1555 case TRUNC_DIV_EXPR:
1556 case TRUNC_MOD_EXPR:
1557 if (TREE_CODE (type) == REAL_TYPE
1558 && op == TRUNC_MOD_EXPR)
1560 tree call;
1561 if (type != double_type_node)
1563 arg1 = convert (double_type_node, arg1);
1564 arg2 = convert (double_type_node, arg2);
1566 call = build3 (CALL_EXPR, double_type_node,
1567 build_address_of (soft_fmod_node),
1568 tree_cons (NULL_TREE, arg1,
1569 build_tree_list (NULL_TREE, arg2)),
1570 NULL_TREE);
1571 if (type != double_type_node)
1572 call = convert (type, call);
1573 return call;
1576 if (TREE_CODE (type) == INTEGER_TYPE
1577 && flag_use_divide_subroutine
1578 && ! flag_syntax_only)
1579 return build_java_soft_divmod (op, type, arg1, arg2);
1581 break;
1582 default: ;
1584 return fold_build2 (op, type, arg1, arg2);
1587 static void
1588 expand_java_binop (tree type, enum tree_code op)
1590 tree larg, rarg;
1591 tree ltype = type;
1592 tree rtype = type;
1593 switch (op)
1595 case LSHIFT_EXPR:
1596 case RSHIFT_EXPR:
1597 case URSHIFT_EXPR:
1598 rtype = int_type_node;
1599 rarg = pop_value (rtype);
1600 break;
1601 default:
1602 rarg = pop_value (rtype);
1604 larg = pop_value (ltype);
1605 push_value (build_java_binop (op, type, larg, rarg));
1608 /* Lookup the field named NAME in *TYPEP or its super classes.
1609 If not found, return NULL_TREE.
1610 (If the *TYPEP is not found, or if the field reference is
1611 ambiguous, return error_mark_node.)
1612 If found, return the FIELD_DECL, and set *TYPEP to the
1613 class containing the field. */
1615 tree
1616 lookup_field (tree *typep, tree name)
1618 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1620 load_class (*typep, 1);
1621 safe_layout_class (*typep);
1622 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1623 return error_mark_node;
1627 tree field, binfo, base_binfo;
1628 tree save_field;
1629 int i;
1631 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1632 if (DECL_NAME (field) == name)
1633 return field;
1635 /* Process implemented interfaces. */
1636 save_field = NULL_TREE;
1637 for (binfo = TYPE_BINFO (*typep), i = 0;
1638 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1640 tree t = BINFO_TYPE (base_binfo);
1641 if ((field = lookup_field (&t, name)))
1643 if (save_field == field)
1644 continue;
1645 if (save_field == NULL_TREE)
1646 save_field = field;
1647 else
1649 tree i1 = DECL_CONTEXT (save_field);
1650 tree i2 = DECL_CONTEXT (field);
1651 error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1652 IDENTIFIER_POINTER (name),
1653 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1654 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1655 return error_mark_node;
1660 if (save_field != NULL_TREE)
1661 return save_field;
1663 *typep = CLASSTYPE_SUPER (*typep);
1664 } while (*typep);
1665 return NULL_TREE;
1668 /* Look up the field named NAME in object SELF_VALUE,
1669 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1670 SELF_VALUE is NULL_TREE if looking for a static field. */
1672 tree
1673 build_field_ref (tree self_value, tree self_class, tree name)
1675 tree base_class = self_class;
1676 tree field_decl = lookup_field (&base_class, name);
1677 if (field_decl == NULL_TREE)
1679 error ("field %qs not found", IDENTIFIER_POINTER (name));
1680 return error_mark_node;
1682 if (self_value == NULL_TREE)
1684 return build_static_field_ref (field_decl);
1686 else
1688 int check = (flag_check_references
1689 && ! (DECL_P (self_value)
1690 && DECL_NAME (self_value) == this_identifier_node));
1692 tree base_type = promote_type (base_class);
1693 if (base_type != TREE_TYPE (self_value))
1694 self_value = fold_build1 (NOP_EXPR, base_type, self_value);
1695 if (! flag_syntax_only && flag_indirect_dispatch)
1697 tree otable_index
1698 = build_int_cst (NULL_TREE, get_symbol_table_index
1699 (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1700 tree field_offset
1701 = build4 (ARRAY_REF, integer_type_node,
1702 TYPE_OTABLE_DECL (output_class), otable_index,
1703 NULL_TREE, NULL_TREE);
1704 tree address;
1706 if (DECL_CONTEXT (field_decl) != output_class)
1707 field_offset
1708 = build3 (COND_EXPR, TREE_TYPE (field_offset),
1709 build2 (EQ_EXPR, boolean_type_node,
1710 field_offset, integer_zero_node),
1711 build3 (CALL_EXPR, void_type_node,
1712 build_address_of (soft_nosuchfield_node),
1713 build_tree_list (NULL_TREE, otable_index),
1714 NULL_TREE),
1715 field_offset);
1717 field_offset = fold (convert (sizetype, field_offset));
1718 address
1719 = fold_build2 (PLUS_EXPR,
1720 build_pointer_type (TREE_TYPE (field_decl)),
1721 self_value, field_offset);
1722 return fold_build1 (INDIRECT_REF, TREE_TYPE (field_decl), address);
1725 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1726 self_value, check);
1727 return fold_build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1728 self_value, field_decl, NULL_TREE);
1732 tree
1733 lookup_label (int pc)
1735 tree name;
1736 char buf[32];
1737 if (pc > highest_label_pc_this_method)
1738 highest_label_pc_this_method = pc;
1739 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", start_label_pc_this_method + pc);
1740 name = get_identifier (buf);
1741 if (IDENTIFIER_LOCAL_VALUE (name))
1742 return IDENTIFIER_LOCAL_VALUE (name);
1743 else
1745 /* The type of the address of a label is return_address_type_node. */
1746 tree decl = create_label_decl (name);
1747 LABEL_PC (decl) = pc;
1748 return pushdecl (decl);
1752 /* Generate a unique name for the purpose of loops and switches
1753 labels, and try-catch-finally blocks label or temporary variables. */
1755 tree
1756 generate_name (void)
1758 static int l_number = 0;
1759 char buff [32];
1760 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1761 l_number++;
1762 return get_identifier (buff);
1765 tree
1766 create_label_decl (tree name)
1768 tree decl;
1769 decl = build_decl (LABEL_DECL, name,
1770 TREE_TYPE (return_address_type_node));
1771 DECL_CONTEXT (decl) = current_function_decl;
1772 DECL_IGNORED_P (decl) = 1;
1773 return decl;
1776 /* This maps a bytecode offset (PC) to various flags. */
1777 char *instruction_bits;
1779 static void
1780 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1782 lookup_label (target_pc);
1783 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1786 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1787 where CONDITION is one of one the compare operators. */
1789 static void
1790 expand_compare (enum tree_code condition, tree value1, tree value2,
1791 int target_pc)
1793 tree target = lookup_label (target_pc);
1794 tree cond = fold_build2 (condition, boolean_type_node, value1, value2);
1795 java_add_stmt
1796 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1797 build1 (GOTO_EXPR, void_type_node, target),
1798 build_java_empty_stmt ()));
1801 /* Emit code for a TEST-type opcode. */
1803 static void
1804 expand_test (enum tree_code condition, tree type, int target_pc)
1806 tree value1, value2;
1807 flush_quick_stack ();
1808 value1 = pop_value (type);
1809 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1810 expand_compare (condition, value1, value2, target_pc);
1813 /* Emit code for a COND-type opcode. */
1815 static void
1816 expand_cond (enum tree_code condition, tree type, int target_pc)
1818 tree value1, value2;
1819 flush_quick_stack ();
1820 /* note: pop values in opposite order */
1821 value2 = pop_value (type);
1822 value1 = pop_value (type);
1823 /* Maybe should check value1 and value2 for type compatibility ??? */
1824 expand_compare (condition, value1, value2, target_pc);
1827 static void
1828 expand_java_goto (int target_pc)
1830 tree target_label = lookup_label (target_pc);
1831 flush_quick_stack ();
1832 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1835 static tree
1836 expand_java_switch (tree selector, int default_pc)
1838 tree switch_expr, x;
1840 flush_quick_stack ();
1841 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1842 NULL_TREE, NULL_TREE);
1843 java_add_stmt (switch_expr);
1845 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1846 create_artificial_label ());
1847 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1849 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1850 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1852 return switch_expr;
1855 static void
1856 expand_java_add_case (tree switch_expr, int match, int target_pc)
1858 tree value, x;
1860 value = build_int_cst (TREE_TYPE (switch_expr), match);
1862 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1863 create_artificial_label ());
1864 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1866 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1867 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1870 static tree
1871 pop_arguments (tree arg_types)
1873 if (arg_types == end_params_node)
1874 return NULL_TREE;
1875 if (TREE_CODE (arg_types) == TREE_LIST)
1877 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1878 tree type = TREE_VALUE (arg_types);
1879 tree arg = pop_value (type);
1881 /* We simply cast each argument to its proper type. This is
1882 needed since we lose type information coming out of the
1883 verifier. We also have to do this when we pop an integer
1884 type that must be promoted for the function call. */
1885 if (TREE_CODE (type) == POINTER_TYPE)
1886 arg = build1 (NOP_EXPR, type, arg);
1887 else if (targetm.calls.promote_prototypes (type)
1888 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1889 && INTEGRAL_TYPE_P (type))
1890 arg = convert (integer_type_node, arg);
1891 return tree_cons (NULL_TREE, arg, tail);
1893 abort ();
1896 /* Attach to PTR (a block) the declaration found in ENTRY. */
1899 attach_init_test_initialization_flags (void **entry, void *ptr)
1901 tree block = (tree)ptr;
1902 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1904 if (block != error_mark_node)
1906 if (TREE_CODE (block) == BIND_EXPR)
1908 tree body = BIND_EXPR_BODY (block);
1909 TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1910 BIND_EXPR_VARS (block) = ite->value;
1911 body = build2 (COMPOUND_EXPR, void_type_node,
1912 build1 (DECL_EXPR, void_type_node, ite->value), body);
1913 BIND_EXPR_BODY (block) = body;
1915 else
1917 tree body = BLOCK_SUBBLOCKS (block);
1918 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1919 BLOCK_EXPR_DECLS (block) = ite->value;
1920 body = build2 (COMPOUND_EXPR, void_type_node,
1921 build1 (DECL_EXPR, void_type_node, ite->value), body);
1922 BLOCK_SUBBLOCKS (block) = body;
1926 return true;
1929 /* Build an expression to initialize the class CLAS.
1930 if EXPR is non-NULL, returns an expression to first call the initializer
1931 (if it is needed) and then calls EXPR. */
1933 tree
1934 build_class_init (tree clas, tree expr)
1936 tree init;
1938 /* An optimization: if CLAS is a superclass of the class we're
1939 compiling, we don't need to initialize it. However, if CLAS is
1940 an interface, it won't necessarily be initialized, even if we
1941 implement it. */
1942 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1943 && inherits_from_p (current_class, clas))
1944 || current_class == clas)
1945 return expr;
1947 if (always_initialize_class_p)
1949 init = build3 (CALL_EXPR, void_type_node,
1950 build_address_of (soft_initclass_node),
1951 build_tree_list (NULL_TREE, build_class_ref (clas)),
1952 NULL_TREE);
1953 TREE_SIDE_EFFECTS (init) = 1;
1955 else
1957 tree *init_test_decl;
1958 tree decl;
1959 init_test_decl = java_treetreehash_new
1960 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1962 if (*init_test_decl == NULL)
1964 /* Build a declaration and mark it as a flag used to track
1965 static class initializations. */
1966 decl = build_decl (VAR_DECL, NULL_TREE,
1967 boolean_type_node);
1968 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1969 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
1970 DECL_CONTEXT (decl) = current_function_decl;
1971 DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
1972 /* Tell the check-init code to ignore this decl when not
1973 optimizing class initialization. */
1974 if (!STATIC_CLASS_INIT_OPT_P ())
1975 DECL_BIT_INDEX (decl) = -1;
1976 DECL_INITIAL (decl) = boolean_false_node;
1977 /* Don't emit any symbolic debugging info for this decl. */
1978 DECL_IGNORED_P (decl) = 1;
1979 *init_test_decl = decl;
1982 init = build3 (CALL_EXPR, void_type_node,
1983 build_address_of (soft_initclass_node),
1984 build_tree_list (NULL_TREE, build_class_ref (clas)),
1985 NULL_TREE);
1986 TREE_SIDE_EFFECTS (init) = 1;
1987 init = build3 (COND_EXPR, void_type_node,
1988 build2 (EQ_EXPR, boolean_type_node,
1989 *init_test_decl, boolean_false_node),
1990 init, integer_zero_node);
1991 TREE_SIDE_EFFECTS (init) = 1;
1992 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
1993 build2 (MODIFY_EXPR, boolean_type_node,
1994 *init_test_decl, boolean_true_node));
1995 TREE_SIDE_EFFECTS (init) = 1;
1998 if (expr != NULL_TREE)
2000 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2001 TREE_SIDE_EFFECTS (expr) = 1;
2002 return expr;
2004 return init;
2007 tree
2008 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2009 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2010 tree arg_list ATTRIBUTE_UNUSED)
2012 tree func;
2013 if (is_compiled_class (self_type))
2015 /* With indirect dispatch we have to use indirect calls for all
2016 publicly visible methods or gcc will use PLT indirections
2017 to reach them. We also have to use indirect dispatch for all
2018 external methods. */
2019 if (! flag_indirect_dispatch
2020 || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2022 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2023 method);
2025 else
2027 tree table_index
2028 = build_int_cst (NULL_TREE, get_symbol_table_index
2029 (method, &TYPE_ATABLE_METHODS (output_class)));
2030 func
2031 = build4 (ARRAY_REF,
2032 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2033 TYPE_ATABLE_DECL (output_class), table_index,
2034 NULL_TREE, NULL_TREE);
2036 func = convert (method_ptr_type_node, func);
2038 else
2040 /* We don't know whether the method has been (statically) compiled.
2041 Compile this code to get a reference to the method's code:
2043 SELF_TYPE->methods[METHOD_INDEX].ncode
2047 int method_index = 0;
2048 tree meth, ref;
2050 /* The method might actually be declared in some superclass, so
2051 we have to use its class context, not the caller's notion of
2052 where the method is. */
2053 self_type = DECL_CONTEXT (method);
2054 ref = build_class_ref (self_type);
2055 ref = build1 (INDIRECT_REF, class_type_node, ref);
2056 if (ncode_ident == NULL_TREE)
2057 ncode_ident = get_identifier ("ncode");
2058 if (methods_ident == NULL_TREE)
2059 methods_ident = get_identifier ("methods");
2060 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2061 lookup_field (&class_type_node, methods_ident),
2062 NULL_TREE);
2063 for (meth = TYPE_METHODS (self_type);
2064 ; meth = TREE_CHAIN (meth))
2066 if (method == meth)
2067 break;
2068 if (meth == NULL_TREE)
2069 fatal_error ("method '%s' not found in class",
2070 IDENTIFIER_POINTER (DECL_NAME (method)));
2071 method_index++;
2073 method_index *= int_size_in_bytes (method_type_node);
2074 ref = fold_build2 (PLUS_EXPR, method_ptr_type_node,
2075 ref, build_int_cst (NULL_TREE, method_index));
2076 ref = build1 (INDIRECT_REF, method_type_node, ref);
2077 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2078 ref, lookup_field (&method_type_node, ncode_ident),
2079 NULL_TREE);
2081 return func;
2084 tree
2085 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2087 tree dtable, objectref;
2089 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2091 /* If we're dealing with interfaces and if the objectref
2092 argument is an array then get the dispatch table of the class
2093 Object rather than the one from the objectref. */
2094 objectref = (is_invoke_interface
2095 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2096 ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2098 if (dtable_ident == NULL_TREE)
2099 dtable_ident = get_identifier ("vtable");
2100 dtable = build_java_indirect_ref (object_type_node, objectref,
2101 flag_check_references);
2102 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2103 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2105 return dtable;
2108 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2109 T. If this decl has not been seen before, it will be added to the
2110 [oa]table_methods. If it has, the existing table slot will be
2111 reused. */
2114 get_symbol_table_index (tree t, tree *symbol_table)
2116 int i = 1;
2117 tree method_list;
2119 if (*symbol_table == NULL_TREE)
2121 *symbol_table = build_tree_list (t, t);
2122 return 1;
2125 method_list = *symbol_table;
2127 while (1)
2129 tree value = TREE_VALUE (method_list);
2130 if (value == t)
2131 return i;
2132 i++;
2133 if (TREE_CHAIN (method_list) == NULL_TREE)
2134 break;
2135 else
2136 method_list = TREE_CHAIN (method_list);
2139 TREE_CHAIN (method_list) = build_tree_list (t, t);
2140 return i;
2143 tree
2144 build_invokevirtual (tree dtable, tree method)
2146 tree func;
2147 tree nativecode_ptr_ptr_type_node
2148 = build_pointer_type (nativecode_ptr_type_node);
2149 tree method_index;
2150 tree otable_index;
2152 if (flag_indirect_dispatch)
2154 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2155 abort ();
2157 otable_index
2158 = build_int_cst (NULL_TREE, get_symbol_table_index
2159 (method, &TYPE_OTABLE_METHODS (output_class)));
2160 method_index = build4 (ARRAY_REF, integer_type_node,
2161 TYPE_OTABLE_DECL (output_class),
2162 otable_index, NULL_TREE, NULL_TREE);
2164 else
2166 /* We fetch the DECL_VINDEX field directly here, rather than
2167 using get_method_index(). DECL_VINDEX is the true offset
2168 from the vtable base to a method, regrdless of any extra
2169 words inserted at the start of the vtable. */
2170 method_index = DECL_VINDEX (method);
2171 method_index = size_binop (MULT_EXPR, method_index,
2172 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2173 if (TARGET_VTABLE_USES_DESCRIPTORS)
2174 method_index = size_binop (MULT_EXPR, method_index,
2175 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2178 func = fold_build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2179 convert (nativecode_ptr_ptr_type_node, method_index));
2181 if (TARGET_VTABLE_USES_DESCRIPTORS)
2182 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2183 else
2184 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2186 return func;
2189 static GTY(()) tree class_ident;
2190 tree
2191 build_invokeinterface (tree dtable, tree method)
2193 tree lookup_arg;
2194 tree interface;
2195 tree idx;
2197 /* We expand invokeinterface here. */
2199 if (class_ident == NULL_TREE)
2200 class_ident = get_identifier ("class");
2202 dtable = build_java_indirect_ref (dtable_type, dtable,
2203 flag_check_references);
2204 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2205 lookup_field (&dtable_type, class_ident), NULL_TREE);
2207 interface = DECL_CONTEXT (method);
2208 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2209 abort ();
2210 layout_class_methods (interface);
2212 if (flag_indirect_dispatch)
2214 int itable_index
2215 = 2 * (get_symbol_table_index
2216 (method, &TYPE_ITABLE_METHODS (output_class)));
2217 interface
2218 = build4 (ARRAY_REF,
2219 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2220 TYPE_ITABLE_DECL (output_class),
2221 build_int_cst (NULL_TREE, itable_index-1),
2222 NULL_TREE, NULL_TREE);
2223 idx
2224 = build4 (ARRAY_REF,
2225 TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2226 TYPE_ITABLE_DECL (output_class),
2227 build_int_cst (NULL_TREE, itable_index),
2228 NULL_TREE, NULL_TREE);
2229 interface = convert (class_ptr_type, interface);
2230 idx = convert (integer_type_node, idx);
2232 else
2234 idx = build_int_cst (NULL_TREE,
2235 get_interface_method_index (method, interface));
2236 interface = build_class_ref (interface);
2239 lookup_arg = tree_cons (NULL_TREE, dtable,
2240 tree_cons (NULL_TREE, interface,
2241 build_tree_list (NULL_TREE, idx)));
2243 return build3 (CALL_EXPR, ptr_type_node,
2244 build_address_of (soft_lookupinterfacemethod_node),
2245 lookup_arg, NULL_TREE);
2248 /* Expand one of the invoke_* opcodes.
2249 OPCODE is the specific opcode.
2250 METHOD_REF_INDEX is an index into the constant pool.
2251 NARGS is the number of arguments, or -1 if not specified. */
2253 static void
2254 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2256 tree method_signature
2257 = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2258 tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2259 method_ref_index);
2260 tree self_type
2261 = get_class_constant (current_jcf,
2262 COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2263 method_ref_index));
2264 const char *const self_name
2265 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2266 tree call, func, method, arg_list, method_type;
2267 tree check = NULL_TREE;
2269 if (! CLASS_LOADED_P (self_type))
2271 load_class (self_type, 1);
2272 safe_layout_class (self_type);
2273 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2274 fatal_error ("failed to find class '%s'", self_name);
2276 layout_class_methods (self_type);
2278 if (ID_INIT_P (method_name))
2279 method = lookup_java_constructor (self_type, method_signature);
2280 else
2281 method = lookup_java_method (self_type, method_name, method_signature);
2283 /* We've found a method in an interface, but this isn't an interface
2284 call. */
2285 if (opcode != OPCODE_invokeinterface
2286 && method
2287 && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2288 method = NULL_TREE;
2290 /* We've found a non-interface method but we are making an
2291 interface call. This can happen if the interface overrides a
2292 method in Object. */
2293 if (! flag_verify_invocations
2294 && opcode == OPCODE_invokeinterface
2295 && method
2296 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2297 method = NULL_TREE;
2299 if (method == NULL_TREE)
2301 if (flag_verify_invocations || ! flag_indirect_dispatch)
2303 error ("class '%s' has no method named '%s' matching signature '%s'",
2304 self_name,
2305 IDENTIFIER_POINTER (method_name),
2306 IDENTIFIER_POINTER (method_signature));
2308 else
2310 int flags = ACC_PUBLIC;
2311 if (opcode == OPCODE_invokestatic)
2312 flags |= ACC_STATIC;
2313 if (opcode == OPCODE_invokeinterface)
2315 flags |= ACC_INTERFACE | ACC_ABSTRACT;
2316 CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2318 method = add_method (self_type, flags, method_name,
2319 method_signature);
2320 DECL_ARTIFICIAL (method) = 1;
2321 METHOD_DUMMY (method) = 1;
2322 layout_class_method (self_type, NULL,
2323 method, NULL);
2327 /* Invoke static can't invoke static/abstract method */
2328 if (method != NULL_TREE)
2330 if (opcode == OPCODE_invokestatic)
2332 if (!METHOD_STATIC (method))
2334 error ("invokestatic on non static method");
2335 method = NULL_TREE;
2337 else if (METHOD_ABSTRACT (method))
2339 error ("invokestatic on abstract method");
2340 method = NULL_TREE;
2343 else
2345 if (METHOD_STATIC (method))
2347 error ("invoke[non-static] on static method");
2348 method = NULL_TREE;
2353 if (method == NULL_TREE)
2355 /* If we got here, we emitted an error message above. So we
2356 just pop the arguments, push a properly-typed zero, and
2357 continue. */
2358 method_type = get_type_from_signature (method_signature);
2359 pop_arguments (TYPE_ARG_TYPES (method_type));
2360 if (opcode != OPCODE_invokestatic)
2361 pop_type (self_type);
2362 method_type = promote_type (TREE_TYPE (method_type));
2363 push_value (convert (method_type, integer_zero_node));
2364 return;
2367 method_type = TREE_TYPE (method);
2368 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2369 flush_quick_stack ();
2371 func = NULL_TREE;
2372 if (opcode == OPCODE_invokestatic)
2373 func = build_known_method_ref (method, method_type, self_type,
2374 method_signature, arg_list);
2375 else if (opcode == OPCODE_invokespecial
2376 || (opcode == OPCODE_invokevirtual
2377 && (METHOD_PRIVATE (method)
2378 || METHOD_FINAL (method)
2379 || CLASS_FINAL (TYPE_NAME (self_type)))))
2381 /* If the object for the method call is null, we throw an
2382 exception. We don't do this if the object is the current
2383 method's `this'. In other cases we just rely on an
2384 optimization pass to eliminate redundant checks. FIXME:
2385 Unfortunately there doesn't seem to be a way to determine
2386 what the current method is right now.
2387 We do omit the check if we're calling <init>. */
2388 /* We use a SAVE_EXPR here to make sure we only evaluate
2389 the new `self' expression once. */
2390 tree save_arg = save_expr (TREE_VALUE (arg_list));
2391 TREE_VALUE (arg_list) = save_arg;
2392 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2393 func = build_known_method_ref (method, method_type, self_type,
2394 method_signature, arg_list);
2396 else
2398 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2399 arg_list);
2400 if (opcode == OPCODE_invokevirtual)
2401 func = build_invokevirtual (dtable, method);
2402 else
2403 func = build_invokeinterface (dtable, method);
2406 if (TREE_CODE (func) == ADDR_EXPR)
2407 TREE_TYPE (func) = build_pointer_type (method_type);
2408 else
2409 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2411 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2412 func, arg_list, NULL_TREE);
2413 TREE_SIDE_EFFECTS (call) = 1;
2414 call = check_for_builtin (method, call);
2416 if (check != NULL_TREE)
2418 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2419 TREE_SIDE_EFFECTS (call) = 1;
2422 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2423 java_add_stmt (call);
2424 else
2426 push_value (call);
2427 flush_quick_stack ();
2431 /* Create a stub which will be put into the vtable but which will call
2432 a JNI function. */
2434 tree
2435 build_jni_stub (tree method)
2437 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2438 tree jni_func_type, tem;
2439 tree env_var, res_var = NULL_TREE, block;
2440 tree method_args, res_type;
2441 tree meth_var;
2442 tree bind;
2444 int args_size = 0;
2446 tree klass = DECL_CONTEXT (method);
2447 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2448 klass = build_class_ref (klass);
2450 if (! METHOD_NATIVE (method) || ! flag_jni)
2451 abort ();
2453 DECL_ARTIFICIAL (method) = 1;
2454 DECL_EXTERNAL (method) = 0;
2456 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2457 DECL_CONTEXT (env_var) = method;
2459 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2461 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2462 TREE_TYPE (TREE_TYPE (method)));
2463 DECL_CONTEXT (res_var) = method;
2464 TREE_CHAIN (env_var) = res_var;
2467 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2468 TREE_STATIC (meth_var) = 1;
2469 TREE_PUBLIC (meth_var) = 0;
2470 DECL_EXTERNAL (meth_var) = 0;
2471 DECL_CONTEXT (meth_var) = method;
2472 DECL_ARTIFICIAL (meth_var) = 1;
2473 DECL_INITIAL (meth_var) = null_pointer_node;
2474 TREE_USED (meth_var) = 1;
2475 chainon (env_var, meth_var);
2476 build_result_decl (method);
2478 /* One strange way that the front ends are different is that they
2479 store arguments differently. */
2480 if (from_class)
2481 method_args = DECL_ARGUMENTS (method);
2482 else
2483 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2484 block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
2485 TREE_SIDE_EFFECTS (block) = 1;
2486 /* When compiling from source we don't set the type of the block,
2487 because that will prevent patch_return from ever being run. */
2488 if (from_class)
2489 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2491 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2492 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2493 build3 (CALL_EXPR, ptr_type_node,
2494 build_address_of (soft_getjnienvnewframe_node),
2495 build_tree_list (NULL_TREE, klass),
2496 NULL_TREE));
2497 CAN_COMPLETE_NORMALLY (body) = 1;
2499 /* All the arguments to this method become arguments to the
2500 underlying JNI function. If we had to wrap object arguments in a
2501 special way, we would do that here. */
2502 args = NULL_TREE;
2503 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2505 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2506 #ifdef PARM_BOUNDARY
2507 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2508 * PARM_BOUNDARY);
2509 #endif
2510 args_size += (arg_bits / BITS_PER_UNIT);
2512 args = tree_cons (NULL_TREE, tem, args);
2514 args = nreverse (args);
2515 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2517 /* For a static method the second argument is the class. For a
2518 non-static method the second argument is `this'; that is already
2519 available in the argument list. */
2520 if (METHOD_STATIC (method))
2522 args_size += int_size_in_bytes (TREE_TYPE (klass));
2523 args = tree_cons (NULL_TREE, klass, args);
2524 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2527 /* The JNIEnv structure is the first argument to the JNI function. */
2528 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2529 args = tree_cons (NULL_TREE, env_var, args);
2530 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2532 /* We call _Jv_LookupJNIMethod to find the actual underlying
2533 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2534 exception if this function is not found at runtime. */
2535 tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2536 method_sig = build_java_signature (TREE_TYPE (method));
2537 lookup_arg = tree_cons (NULL_TREE,
2538 build_utf8_ref (unmangle_classname
2539 (IDENTIFIER_POINTER (method_sig),
2540 IDENTIFIER_LENGTH (method_sig))),
2541 tem);
2542 tem = DECL_NAME (method);
2543 lookup_arg
2544 = tree_cons (NULL_TREE, klass,
2545 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2547 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2549 #ifdef MODIFY_JNI_METHOD_CALL
2550 tem = MODIFY_JNI_METHOD_CALL (tem);
2551 #endif
2553 jni_func_type = build_pointer_type (tem);
2555 jnifunc = build3 (COND_EXPR, ptr_type_node,
2556 meth_var, meth_var,
2557 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2558 build3 (CALL_EXPR, ptr_type_node,
2559 build_address_of
2560 (soft_lookupjnimethod_node),
2561 lookup_arg, NULL_TREE)));
2563 /* Now we make the actual JNI call via the resulting function
2564 pointer. */
2565 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2566 build1 (NOP_EXPR, jni_func_type, jnifunc),
2567 args, NULL_TREE);
2569 /* If the JNI call returned a result, capture it here. If we had to
2570 unwrap JNI object results, we would do that here. */
2571 if (res_var != NULL_TREE)
2573 /* If the call returns an object, it may return a JNI weak
2574 reference, in which case we must unwrap it. */
2575 if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
2576 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2577 build_address_of (soft_unwrapjni_node),
2578 build_tree_list (NULL_TREE, call),
2579 NULL_TREE);
2580 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2581 res_var, call);
2584 TREE_SIDE_EFFECTS (call) = 1;
2585 CAN_COMPLETE_NORMALLY (call) = 1;
2587 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2588 TREE_SIDE_EFFECTS (body) = 1;
2590 /* Now free the environment we allocated. */
2591 call = build3 (CALL_EXPR, ptr_type_node,
2592 build_address_of (soft_jnipopsystemframe_node),
2593 build_tree_list (NULL_TREE, env_var),
2594 NULL_TREE);
2595 TREE_SIDE_EFFECTS (call) = 1;
2596 CAN_COMPLETE_NORMALLY (call) = 1;
2597 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2598 TREE_SIDE_EFFECTS (body) = 1;
2600 /* Finally, do the return. */
2601 res_type = void_type_node;
2602 if (res_var != NULL_TREE)
2604 tree drt;
2605 if (! DECL_RESULT (method))
2606 abort ();
2607 /* Make sure we copy the result variable to the actual
2608 result. We use the type of the DECL_RESULT because it
2609 might be different from the return type of the function:
2610 it might be promoted. */
2611 drt = TREE_TYPE (DECL_RESULT (method));
2612 if (drt != TREE_TYPE (res_var))
2613 res_var = build1 (CONVERT_EXPR, drt, res_var);
2614 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2615 TREE_SIDE_EFFECTS (res_var) = 1;
2618 body = build2 (COMPOUND_EXPR, void_type_node, body,
2619 build1 (RETURN_EXPR, res_type, res_var));
2620 TREE_SIDE_EFFECTS (body) = 1;
2622 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2623 body, block);
2624 return bind;
2627 /* Expand an operation to extract from or store into a field.
2628 IS_STATIC is 1 iff the field is static.
2629 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2630 FIELD_REF_INDEX is an index into the constant pool. */
2632 static void
2633 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2635 tree self_type
2636 = get_class_constant (current_jcf,
2637 COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2638 field_ref_index));
2639 const char *self_name
2640 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2641 tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2642 tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
2643 field_ref_index);
2644 tree field_type = get_type_from_signature (field_signature);
2645 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2646 tree field_ref;
2647 int is_error = 0;
2648 tree original_self_type = self_type;
2649 tree field_decl;
2651 if (! CLASS_LOADED_P (self_type))
2652 load_class (self_type, 1);
2653 field_decl = lookup_field (&self_type, field_name);
2654 if (field_decl == error_mark_node)
2656 is_error = 1;
2658 else if (field_decl == NULL_TREE)
2660 if (! flag_verify_invocations)
2662 int flags = ACC_PUBLIC;
2663 if (is_static)
2664 flags |= ACC_STATIC;
2665 self_type = original_self_type;
2666 field_decl = add_field (original_self_type, field_name,
2667 field_type, flags);
2668 DECL_ARTIFICIAL (field_decl) = 1;
2669 DECL_IGNORED_P (field_decl) = 1;
2671 else
2673 error ("missing field '%s' in '%s'",
2674 IDENTIFIER_POINTER (field_name), self_name);
2675 is_error = 1;
2678 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2680 error ("mismatching signature for field '%s' in '%s'",
2681 IDENTIFIER_POINTER (field_name), self_name);
2682 is_error = 1;
2684 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2685 if (is_error)
2687 if (! is_putting)
2688 push_value (convert (field_type, integer_zero_node));
2689 flush_quick_stack ();
2690 return;
2693 field_ref = build_field_ref (field_ref, self_type, field_name);
2694 if (is_static
2695 && ! flag_indirect_dispatch)
2696 field_ref = build_class_init (self_type, field_ref);
2697 if (is_putting)
2699 flush_quick_stack ();
2700 if (FIELD_FINAL (field_decl))
2702 if (DECL_CONTEXT (field_decl) != current_class)
2703 error ("assignment to final field %q+D not in field's class",
2704 field_decl);
2705 else if (FIELD_STATIC (field_decl))
2707 if (!DECL_CLINIT_P (current_function_decl))
2708 warning (0, "assignment to final static field %q+D not in "
2709 "class initializer",
2710 field_decl);
2712 else
2714 tree cfndecl_name = DECL_NAME (current_function_decl);
2715 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2716 && !ID_FINIT_P (cfndecl_name))
2717 warning (0, "assignment to final field %q+D not in constructor",
2718 field_decl);
2721 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2722 field_ref, new_value));
2724 else
2725 push_value (field_ref);
2728 void
2729 load_type_state (tree label)
2731 int i;
2732 tree vec = LABEL_TYPE_STATE (label);
2733 int cur_length = TREE_VEC_LENGTH (vec);
2734 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2735 for (i = 0; i < cur_length; i++)
2736 type_map [i] = TREE_VEC_ELT (vec, i);
2739 /* Go over METHOD's bytecode and note instruction starts in
2740 instruction_bits[]. */
2742 void
2743 note_instructions (JCF *jcf, tree method)
2745 int PC;
2746 unsigned char* byte_ops;
2747 long length = DECL_CODE_LENGTH (method);
2749 int saw_index;
2750 jint INT_temp;
2752 #undef RET /* Defined by config/i386/i386.h */
2753 #undef PTR
2754 #define BCODE byte_ops
2755 #define BYTE_type_node byte_type_node
2756 #define SHORT_type_node short_type_node
2757 #define INT_type_node int_type_node
2758 #define LONG_type_node long_type_node
2759 #define CHAR_type_node char_type_node
2760 #define PTR_type_node ptr_type_node
2761 #define FLOAT_type_node float_type_node
2762 #define DOUBLE_type_node double_type_node
2763 #define VOID_type_node void_type_node
2764 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2765 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2766 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2767 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2769 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2771 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2772 byte_ops = jcf->read_ptr;
2773 instruction_bits = xrealloc (instruction_bits, length + 1);
2774 memset (instruction_bits, 0, length + 1);
2776 /* This pass figures out which PC can be the targets of jumps. */
2777 for (PC = 0; PC < length;)
2779 int oldpc = PC; /* PC at instruction start. */
2780 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2781 switch (byte_ops[PC++])
2783 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2784 case OPCODE: \
2785 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2786 break;
2788 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2790 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2791 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2792 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2793 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2794 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2795 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2796 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2797 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2799 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2800 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2801 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2802 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2803 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2804 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2805 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2806 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2808 /* two forms of wide instructions */
2809 #define PRE_SPECIAL_WIDE(IGNORE) \
2811 int modified_opcode = IMMEDIATE_u1; \
2812 if (modified_opcode == OPCODE_iinc) \
2814 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2815 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2817 else \
2819 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2823 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2825 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2827 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2828 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2829 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2830 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2831 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2832 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2833 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2834 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2835 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2836 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2838 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2839 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2840 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2841 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2842 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2843 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2844 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2845 NOTE_LABEL (PC); \
2846 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2848 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2850 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2851 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2853 #define PRE_LOOKUP_SWITCH \
2854 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2855 NOTE_LABEL (default_offset+oldpc); \
2856 if (npairs >= 0) \
2857 while (--npairs >= 0) { \
2858 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2859 jint offset = IMMEDIATE_s4; \
2860 NOTE_LABEL (offset+oldpc); } \
2863 #define PRE_TABLE_SWITCH \
2864 { jint default_offset = IMMEDIATE_s4; \
2865 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2866 NOTE_LABEL (default_offset+oldpc); \
2867 if (low <= high) \
2868 while (low++ <= high) { \
2869 jint offset = IMMEDIATE_s4; \
2870 NOTE_LABEL (offset+oldpc); } \
2873 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2874 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2875 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2876 (void)(IMMEDIATE_u2); \
2877 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2879 #include "javaop.def"
2880 #undef JAVAOP
2882 } /* for */
2885 void
2886 expand_byte_code (JCF *jcf, tree method)
2888 int PC;
2889 int i;
2890 const unsigned char *linenumber_pointer;
2891 int dead_code_index = -1;
2892 unsigned char* byte_ops;
2893 long length = DECL_CODE_LENGTH (method);
2895 stack_pointer = 0;
2896 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2897 byte_ops = jcf->read_ptr;
2899 /* We make an initial pass of the line number table, to note
2900 which instructions have associated line number entries. */
2901 linenumber_pointer = linenumber_table;
2902 for (i = 0; i < linenumber_count; i++)
2904 int pc = GET_u2 (linenumber_pointer);
2905 linenumber_pointer += 4;
2906 if (pc >= length)
2907 warning (0, "invalid PC in line number table");
2908 else
2910 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2911 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2912 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2916 if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2917 return;
2919 promote_arguments ();
2921 /* Translate bytecodes. */
2922 linenumber_pointer = linenumber_table;
2923 for (PC = 0; PC < length;)
2925 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2927 tree label = lookup_label (PC);
2928 flush_quick_stack ();
2929 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2930 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2931 if (LABEL_VERIFIED (label) || PC == 0)
2932 load_type_state (label);
2935 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2937 if (dead_code_index == -1)
2939 /* This is the start of a region of unreachable bytecodes.
2940 They still need to be processed in order for EH ranges
2941 to get handled correctly. However, we can simply
2942 replace these bytecodes with nops. */
2943 dead_code_index = PC;
2946 /* Turn this bytecode into a nop. */
2947 byte_ops[PC] = 0x0;
2949 else
2951 if (dead_code_index != -1)
2953 /* We've just reached the end of a region of dead code. */
2954 if (extra_warnings)
2955 warning (0, "unreachable bytecode from %d to before %d",
2956 dead_code_index, PC);
2957 dead_code_index = -1;
2961 /* Handle possible line number entry for this PC.
2963 This code handles out-of-order and multiple linenumbers per PC,
2964 but is optimized for the case of line numbers increasing
2965 monotonically with PC. */
2966 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2968 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2969 || GET_u2 (linenumber_pointer) != PC)
2970 linenumber_pointer = linenumber_table;
2971 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2973 int pc = GET_u2 (linenumber_pointer);
2974 linenumber_pointer += 4;
2975 if (pc == PC)
2977 int line = GET_u2 (linenumber_pointer - 2);
2978 #ifdef USE_MAPPED_LOCATION
2979 input_location = linemap_line_start (&line_table, line, 1);
2980 #else
2981 input_location.line = line;
2982 #endif
2983 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2984 break;
2988 maybe_pushlevels (PC);
2989 PC = process_jvm_instruction (PC, byte_ops, length);
2990 maybe_poplevels (PC);
2991 } /* for */
2993 if (dead_code_index != -1)
2995 /* We've just reached the end of a region of dead code. */
2996 if (extra_warnings)
2997 warning (0, "unreachable bytecode from %d to the end of the method",
2998 dead_code_index);
3002 static void
3003 java_push_constant_from_pool (JCF *jcf, int index)
3005 tree c;
3006 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3008 tree name;
3009 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3010 index = alloc_name_constant (CONSTANT_String, name);
3011 c = build_ref_from_constant_pool (index);
3012 c = convert (promote_type (string_type_node), c);
3014 else
3015 c = get_constant (jcf, index);
3016 push_value (c);
3020 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3021 long length ATTRIBUTE_UNUSED)
3023 const char *opname; /* Temporary ??? */
3024 int oldpc = PC; /* PC at instruction start. */
3026 /* If the instruction is at the beginning of an exception handler,
3027 replace the top of the stack with the thrown object reference. */
3028 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3030 /* Note that the verifier will not emit a type map at all for
3031 dead exception handlers. In this case we just ignore the
3032 situation. */
3033 if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
3035 tree type = pop_type (promote_type (throwable_type_node));
3036 push_value (build_exception_object_ref (type));
3040 switch (byte_ops[PC++])
3042 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3043 case OPCODE: \
3044 opname = #OPNAME; \
3045 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3046 break;
3048 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
3050 int saw_index = 0; \
3051 int index = OPERAND_VALUE; \
3052 build_java_ret \
3053 (find_local_variable (index, return_address_type_node, oldpc)); \
3056 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3058 /* OPERAND_VALUE may have side-effects on PC */ \
3059 int opvalue = OPERAND_VALUE; \
3060 build_java_jsr (oldpc + opvalue, PC); \
3063 /* Push a constant onto the stack. */
3064 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3065 { int saw_index = 0; int ival = (OPERAND_VALUE); \
3066 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3067 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3069 /* internal macro added for use by the WIDE case */
3070 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3071 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3073 /* Push local variable onto the opcode stack. */
3074 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3076 /* have to do this since OPERAND_VALUE may have side-effects */ \
3077 int opvalue = OPERAND_VALUE; \
3078 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3081 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3082 expand_java_return (OPERAND_TYPE##_type_node)
3084 #define REM_EXPR TRUNC_MOD_EXPR
3085 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3086 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3088 #define FIELD(IS_STATIC, IS_PUT) \
3089 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3091 #define TEST(OPERAND_TYPE, CONDITION) \
3092 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3094 #define COND(OPERAND_TYPE, CONDITION) \
3095 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3097 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3098 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3100 #define BRANCH_GOTO(OPERAND_VALUE) \
3101 expand_java_goto (oldpc + OPERAND_VALUE)
3103 #define BRANCH_CALL(OPERAND_VALUE) \
3104 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3106 #if 0
3107 #define BRANCH_RETURN(OPERAND_VALUE) \
3109 tree type = OPERAND_TYPE##_type_node; \
3110 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3111 expand_java_ret (value); \
3113 #endif
3115 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3116 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3117 fprintf (stderr, "(not implemented)\n")
3118 #define NOT_IMPL1(OPERAND_VALUE) \
3119 fprintf (stderr, "%3d: %s ", oldpc, opname); \
3120 fprintf (stderr, "(not implemented)\n")
3122 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3124 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3126 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3128 #define STACK_SWAP(COUNT) java_stack_swap()
3130 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3131 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3132 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3134 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3135 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3137 #define LOOKUP_SWITCH \
3138 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
3139 tree selector = pop_value (INT_type_node); \
3140 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3141 while (--npairs >= 0) \
3143 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3144 expand_java_add_case (switch_expr, match, oldpc + offset); \
3148 #define TABLE_SWITCH \
3149 { jint default_offset = IMMEDIATE_s4; \
3150 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3151 tree selector = pop_value (INT_type_node); \
3152 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3153 for (; low <= high; low++) \
3155 jint offset = IMMEDIATE_s4; \
3156 expand_java_add_case (switch_expr, low, oldpc + offset); \
3160 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3161 { int opcode = byte_ops[PC-1]; \
3162 int method_ref_index = IMMEDIATE_u2; \
3163 int nargs; \
3164 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
3165 else nargs = -1; \
3166 expand_invoke (opcode, method_ref_index, nargs); \
3169 /* Handle new, checkcast, instanceof */
3170 #define OBJECT(TYPE, OP) \
3171 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3173 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3175 #define ARRAY_LOAD(OPERAND_TYPE) \
3177 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
3180 #define ARRAY_STORE(OPERAND_TYPE) \
3182 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3185 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3186 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3187 #define ARRAY_NEW_PTR() \
3188 push_value (build_anewarray (get_class_constant (current_jcf, \
3189 IMMEDIATE_u2), \
3190 pop_value (int_type_node)));
3191 #define ARRAY_NEW_NUM() \
3193 int atype = IMMEDIATE_u1; \
3194 push_value (build_newarray (atype, pop_value (int_type_node)));\
3196 #define ARRAY_NEW_MULTI() \
3198 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
3199 int ndims = IMMEDIATE_u1; \
3200 expand_java_multianewarray( class, ndims ); \
3203 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3204 push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3205 pop_value (OPERAND_TYPE##_type_node)));
3207 #define CONVERT2(FROM_TYPE, TO_TYPE) \
3209 push_value (build1 (NOP_EXPR, int_type_node, \
3210 (convert (TO_TYPE##_type_node, \
3211 pop_value (FROM_TYPE##_type_node))))); \
3214 #define CONVERT(FROM_TYPE, TO_TYPE) \
3216 push_value (convert (TO_TYPE##_type_node, \
3217 pop_value (FROM_TYPE##_type_node))); \
3220 /* internal macro added for use by the WIDE case
3221 Added TREE_TYPE (decl) assignment, apbianco */
3222 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
3224 tree decl, value; \
3225 int index = OPVALUE; \
3226 tree type = OPTYPE; \
3227 value = pop_value (type); \
3228 type = TREE_TYPE (value); \
3229 decl = find_local_variable (index, type, oldpc); \
3230 set_local_type (index, type); \
3231 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
3232 update_aliases (decl, index, PC); \
3235 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3237 /* have to do this since OPERAND_VALUE may have side-effects */ \
3238 int opvalue = OPERAND_VALUE; \
3239 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3242 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3243 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3245 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3246 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
3248 #define MONITOR_OPERATION(call) \
3250 tree o = pop_value (ptr_type_node); \
3251 tree c; \
3252 flush_quick_stack (); \
3253 c = build_java_monitor (call, o); \
3254 TREE_SIDE_EFFECTS (c) = 1; \
3255 java_add_stmt (c); \
3258 #define SPECIAL_IINC(IGNORED) \
3260 unsigned int local_var_index = IMMEDIATE_u1; \
3261 int ival = IMMEDIATE_s1; \
3262 expand_iinc(local_var_index, ival, oldpc); \
3265 #define SPECIAL_WIDE(IGNORED) \
3267 int modified_opcode = IMMEDIATE_u1; \
3268 unsigned int local_var_index = IMMEDIATE_u2; \
3269 switch (modified_opcode) \
3271 case OPCODE_iinc: \
3273 int ival = IMMEDIATE_s2; \
3274 expand_iinc (local_var_index, ival, oldpc); \
3275 break; \
3277 case OPCODE_iload: \
3278 case OPCODE_lload: \
3279 case OPCODE_fload: \
3280 case OPCODE_dload: \
3281 case OPCODE_aload: \
3283 /* duplicate code from LOAD macro */ \
3284 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3285 break; \
3287 case OPCODE_istore: \
3288 case OPCODE_lstore: \
3289 case OPCODE_fstore: \
3290 case OPCODE_dstore: \
3291 case OPCODE_astore: \
3293 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3294 break; \
3296 default: \
3297 error ("unrecogized wide sub-instruction"); \
3301 #define SPECIAL_THROW(IGNORED) \
3302 build_java_athrow (pop_value (throwable_type_node))
3304 #define SPECIAL_BREAK NOT_IMPL1
3305 #define IMPL NOT_IMPL
3307 #include "javaop.def"
3308 #undef JAVAOP
3309 default:
3310 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3312 return PC;
3315 /* Return the opcode at PC in the code section pointed to by
3316 CODE_OFFSET. */
3318 static unsigned char
3319 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3321 unsigned char opcode;
3322 long absolute_offset = (long)JCF_TELL (jcf);
3324 JCF_SEEK (jcf, code_offset);
3325 opcode = jcf->read_ptr [pc];
3326 JCF_SEEK (jcf, absolute_offset);
3327 return opcode;
3330 /* Some bytecode compilers are emitting accurate LocalVariableTable
3331 attributes. Here's an example:
3333 PC <t>store_<n>
3334 PC+1 ...
3336 Attribute "LocalVariableTable"
3337 slot #<n>: ... (PC: PC+1 length: L)
3339 This is accurate because the local in slot <n> really exists after
3340 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3342 This procedure recognizes this situation and extends the live range
3343 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3344 length of the store instruction.)
3346 This function is used by `give_name_to_locals' so that a local's
3347 DECL features a DECL_LOCAL_START_PC such that the first related
3348 store operation will use DECL as a destination, not an unrelated
3349 temporary created for the occasion.
3351 This function uses a global (instruction_bits) `note_instructions' should
3352 have allocated and filled properly. */
3355 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3356 int start_pc, int slot)
3358 int first, index, opcode;
3359 int pc, insn_pc;
3360 int wide_found = 0;
3362 if (!start_pc)
3363 return start_pc;
3365 first = index = -1;
3367 /* Find last previous instruction and remember it */
3368 for (pc = start_pc-1; pc; pc--)
3369 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3370 break;
3371 insn_pc = pc;
3373 /* Retrieve the instruction, handle `wide'. */
3374 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3375 if (opcode == OPCODE_wide)
3377 wide_found = 1;
3378 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3381 switch (opcode)
3383 case OPCODE_astore_0:
3384 case OPCODE_astore_1:
3385 case OPCODE_astore_2:
3386 case OPCODE_astore_3:
3387 first = OPCODE_astore_0;
3388 break;
3390 case OPCODE_istore_0:
3391 case OPCODE_istore_1:
3392 case OPCODE_istore_2:
3393 case OPCODE_istore_3:
3394 first = OPCODE_istore_0;
3395 break;
3397 case OPCODE_lstore_0:
3398 case OPCODE_lstore_1:
3399 case OPCODE_lstore_2:
3400 case OPCODE_lstore_3:
3401 first = OPCODE_lstore_0;
3402 break;
3404 case OPCODE_fstore_0:
3405 case OPCODE_fstore_1:
3406 case OPCODE_fstore_2:
3407 case OPCODE_fstore_3:
3408 first = OPCODE_fstore_0;
3409 break;
3411 case OPCODE_dstore_0:
3412 case OPCODE_dstore_1:
3413 case OPCODE_dstore_2:
3414 case OPCODE_dstore_3:
3415 first = OPCODE_dstore_0;
3416 break;
3418 case OPCODE_astore:
3419 case OPCODE_istore:
3420 case OPCODE_lstore:
3421 case OPCODE_fstore:
3422 case OPCODE_dstore:
3423 index = peek_opcode_at_pc (jcf, code_offset, pc);
3424 if (wide_found)
3426 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3427 index = (other << 8) + index;
3429 break;
3432 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3433 means we have a <t>store. */
3434 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3435 start_pc = insn_pc;
3437 return start_pc;
3440 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3441 order, as specified by Java Language Specification.
3443 The problem is that while expand_expr will evaluate its sub-operands in
3444 left-to-right order, for variables it will just return an rtx (i.e.
3445 an lvalue) for the variable (rather than an rvalue). So it is possible
3446 that a later sub-operand will change the register, and when the
3447 actual operation is done, it will use the new value, when it should
3448 have used the original value.
3450 We fix this by using save_expr. This forces the sub-operand to be
3451 copied into a fresh virtual register,
3453 For method invocation, we modify the arguments so that a
3454 left-to-right order evaluation is performed. Saved expressions
3455 will, in CALL_EXPR order, be reused when the call will be expanded.
3457 We also promote outgoing args if needed. */
3459 tree
3460 force_evaluation_order (tree node)
3462 if (flag_syntax_only)
3463 return node;
3464 if (TREE_CODE (node) == CALL_EXPR
3465 || TREE_CODE (node) == NEW_CLASS_EXPR
3466 || (TREE_CODE (node) == COMPOUND_EXPR
3467 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3468 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3470 tree arg, cmp;
3472 arg = node;
3474 /* Position arg properly, account for wrapped around ctors. */
3475 if (TREE_CODE (node) == COMPOUND_EXPR)
3476 arg = TREE_OPERAND (node, 0);
3478 arg = TREE_OPERAND (arg, 1);
3480 /* An empty argument list is ok, just ignore it. */
3481 if (!arg)
3482 return node;
3484 /* Not having a list of arguments here is an error. */
3485 if (TREE_CODE (arg) != TREE_LIST)
3486 abort ();
3488 /* This reverses the evaluation order. This is a desired effect. */
3489 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3491 /* Promote types smaller than integer. This is required by
3492 some ABIs. */
3493 tree type = TREE_TYPE (TREE_VALUE (arg));
3494 tree saved;
3495 if (targetm.calls.promote_prototypes (type)
3496 && INTEGRAL_TYPE_P (type)
3497 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
3498 TYPE_SIZE (integer_type_node)))
3499 TREE_VALUE (arg) = fold_convert (integer_type_node, TREE_VALUE (arg));
3501 saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3502 cmp = (cmp == NULL_TREE ? saved :
3503 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3504 TREE_VALUE (arg) = saved;
3507 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3508 TREE_SIDE_EFFECTS (cmp) = 1;
3510 if (cmp)
3512 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3513 if (TREE_TYPE (cmp) != void_type_node)
3514 cmp = save_expr (cmp);
3515 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3516 TREE_SIDE_EFFECTS (cmp) = 1;
3517 node = cmp;
3520 return node;
3523 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3524 location where an expression or an identifier were encountered. It
3525 is necessary for languages where the frontend parser will handle
3526 recursively more than one file (Java is one of them). */
3528 tree
3529 build_expr_wfl (tree node,
3530 #ifdef USE_MAPPED_LOCATION
3531 source_location location
3532 #else
3533 const char *file, int line, int col
3534 #endif
3537 tree wfl;
3539 #ifdef USE_MAPPED_LOCATION
3540 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3541 SET_EXPR_LOCATION (wfl, location);
3542 #else
3543 static const char *last_file = 0;
3544 static tree last_filenode = NULL_TREE;
3546 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3548 EXPR_WFL_SET_LINECOL (wfl, line, col);
3549 if (file != last_file)
3551 last_file = file;
3552 last_filenode = file ? get_identifier (file) : NULL_TREE;
3554 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3555 #endif
3556 EXPR_WFL_NODE (wfl) = node;
3557 if (node)
3559 if (!TYPE_P (node))
3560 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3561 TREE_TYPE (wfl) = TREE_TYPE (node);
3564 return wfl;
3567 #ifdef USE_MAPPED_LOCATION
3568 tree
3569 expr_add_location (tree node, source_location location, bool statement)
3571 tree wfl;
3572 #if 0
3573 /* FIXME. This optimization causes failures in code that expects an
3574 EXPR_WITH_FILE_LOCATION. E.g. in resolve_qualified_expression_name. */
3575 if (node && ! (statement && flag_emit_class_files))
3577 source_location node_loc = EXPR_LOCATION (node);
3578 if (node_loc == location || location == UNKNOWN_LOCATION)
3579 return node;
3580 if (node_loc == UNKNOWN_LOCATION
3581 && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3583 SET_EXPR_LOCATION (node, location);
3584 return node;
3587 #endif
3588 wfl = make_node (EXPR_WITH_FILE_LOCATION);
3589 SET_EXPR_LOCATION (wfl, location);
3590 EXPR_WFL_NODE (wfl) = node;
3591 if (statement && debug_info_level != DINFO_LEVEL_NONE)
3592 EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3593 if (node)
3595 if (!TYPE_P (node))
3596 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3597 TREE_TYPE (wfl) = TREE_TYPE (node);
3600 return wfl;
3602 #endif
3604 /* Build a node to represent empty statements and blocks. */
3606 tree
3607 build_java_empty_stmt (void)
3609 tree t = build_empty_stmt ();
3610 CAN_COMPLETE_NORMALLY (t) = 1;
3611 return t;
3614 /* Promote all args of integral type before generating any code. */
3616 static void
3617 promote_arguments (void)
3619 int i;
3620 tree arg;
3621 for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
3622 arg != NULL_TREE; arg = TREE_CHAIN (arg), i++)
3624 tree arg_type = TREE_TYPE (arg);
3625 if (INTEGRAL_TYPE_P (arg_type)
3626 && TYPE_PRECISION (arg_type) < 32)
3628 tree copy = find_local_variable (i, integer_type_node, -1);
3629 java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
3630 copy,
3631 fold_convert (integer_type_node, arg)));
3633 if (TYPE_IS_WIDE (arg_type))
3634 i++;
3638 #include "gt-java-expr.h"