PR c++/16115
[official-gcc.git] / gcc / java / expr.c
bloba6bc5759a63243f4abce824f18a15fd96518410c
1 /* Process expressions for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "rtl.h"
35 #include "flags.h"
36 #include "expr.h"
37 #include "java-tree.h"
38 #include "javaop.h"
39 #include "java-opcodes.h"
40 #include "jcf.h"
41 #include "java-except.h"
42 #include "parse.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "ggc.h"
46 #include "tree-gimple.h"
47 #include "target.h"
49 static void flush_quick_stack (void);
50 static void push_value (tree);
51 static tree pop_value (tree);
52 static void java_stack_swap (void);
53 static void java_stack_dup (int, int);
54 static void build_java_athrow (tree);
55 static void build_java_jsr (int, int);
56 static void build_java_ret (tree);
57 static void expand_java_multianewarray (tree, int);
58 static void expand_java_arraystore (tree);
59 static void expand_java_arrayload (tree);
60 static void expand_java_array_length (void);
61 static tree build_java_monitor (tree, tree);
62 static void expand_java_pushc (int, tree);
63 static void expand_java_return (tree);
64 static void expand_load_internal (int, tree, int);
65 static void expand_java_NEW (tree);
66 static void expand_java_INSTANCEOF (tree);
67 static void expand_java_CHECKCAST (tree);
68 static void expand_iinc (unsigned int, int, int);
69 static void expand_java_binop (tree, enum tree_code);
70 static void note_label (int, int);
71 static void expand_compare (enum tree_code, tree, tree, int);
72 static void expand_test (enum tree_code, tree, int);
73 static void expand_cond (enum tree_code, tree, int);
74 static void expand_java_goto (int);
75 static tree expand_java_switch (tree, int);
76 static void expand_java_add_case (tree, int, int);
77 #if 0
78 static void expand_java_call (int, int);
79 static void expand_java_ret (tree);
80 #endif
81 static tree pop_arguments (tree);
82 static void expand_invoke (int, int, int);
83 static void expand_java_field_op (int, int, int);
84 static void java_push_constant_from_pool (struct JCF *, int);
85 static void java_stack_pop (int);
86 static tree build_java_throw_out_of_bounds_exception (tree);
87 static tree build_java_check_indexed_type (tree, tree);
88 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
90 static GTY(()) tree operand_type[59];
92 static GTY(()) tree methods_ident;
93 static GTY(()) tree ncode_ident;
94 tree dtable_ident = NULL_TREE;
96 /* Set to nonzero value in order to emit class initialization code
97 before static field references. */
98 /* FIXME: Make this work with gimplify. */
99 int always_initialize_class_p = 1;
101 /* We store the stack state in two places:
102 Within a basic block, we use the quick_stack, which is a
103 pushdown list (TREE_LISTs) of expression nodes.
104 This is the top part of the stack; below that we use find_stack_slot.
105 At the end of a basic block, the quick_stack must be flushed
106 to the stack slot array (as handled by find_stack_slot).
107 Using quick_stack generates better code (especially when
108 compiled without optimization), because we do not have to
109 explicitly store and load trees to temporary variables.
111 If a variable is on the quick stack, it means the value of variable
112 when the quick stack was last flushed. Conceptually, flush_quick_stack
113 saves all the quick_stack elements in parallel. However, that is
114 complicated, so it actually saves them (i.e. copies each stack value
115 to is home virtual register) from low indexes. This allows a quick_stack
116 element at index i (counting from the bottom of stack the) to references
117 slot virtuals for register that are >= i, but not those that are deeper.
118 This convention makes most operations easier. For example iadd works
119 even when the stack contains (reg[0], reg[1]): It results in the
120 stack containing (reg[0]+reg[1]), which is OK. However, some stack
121 operations are more complicated. For example dup given a stack
122 containing (reg[0]) would yield (reg[0], reg[0]), which would violate
123 the convention, since stack value 1 would refer to a register with
124 lower index (reg[0]), which flush_quick_stack does not safely handle.
125 So dup cannot just add an extra element to the quick_stack, but iadd can.
128 static GTY(()) tree quick_stack;
130 /* A free-list of unused permanent TREE_LIST nodes. */
131 static GTY((deletable)) tree tree_list_free_list;
133 /* The stack pointer of the Java virtual machine.
134 This does include the size of the quick_stack. */
136 int stack_pointer;
138 const unsigned char *linenumber_table;
139 int linenumber_count;
141 void
142 init_expr_processing (void)
144 operand_type[21] = operand_type[54] = int_type_node;
145 operand_type[22] = operand_type[55] = long_type_node;
146 operand_type[23] = operand_type[56] = float_type_node;
147 operand_type[24] = operand_type[57] = double_type_node;
148 operand_type[25] = operand_type[58] = ptr_type_node;
151 tree
152 java_truthvalue_conversion (tree expr)
154 /* It is simpler and generates better code to have only TRUTH_*_EXPR
155 or comparison expressions as truth values at this level.
157 This function should normally be identity for Java. */
159 switch (TREE_CODE (expr))
161 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
162 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
163 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
164 case ORDERED_EXPR: case UNORDERED_EXPR:
165 case TRUTH_ANDIF_EXPR:
166 case TRUTH_ORIF_EXPR:
167 case TRUTH_AND_EXPR:
168 case TRUTH_OR_EXPR:
169 case TRUTH_XOR_EXPR:
170 case TRUTH_NOT_EXPR:
171 case ERROR_MARK:
172 return expr;
174 case INTEGER_CST:
175 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
177 case REAL_CST:
178 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
180 /* are these legal? XXX JH */
181 case NEGATE_EXPR:
182 case ABS_EXPR:
183 case FLOAT_EXPR:
184 /* These don't change whether an object is nonzero or zero. */
185 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
187 case COND_EXPR:
188 /* Distribute the conversion into the arms of a COND_EXPR. */
189 return fold
190 (build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
191 java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
192 java_truthvalue_conversion (TREE_OPERAND (expr, 2))));
194 case NOP_EXPR:
195 /* If this is widening the argument, we can ignore it. */
196 if (TYPE_PRECISION (TREE_TYPE (expr))
197 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
198 return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
199 /* fall through to default */
201 default:
202 return fold (build2 (NE_EXPR, boolean_type_node,
203 expr, boolean_false_node));
207 /* Save any stack slots that happen to be in the quick_stack into their
208 home virtual register slots.
210 The copy order is from low stack index to high, to support the invariant
211 that the expression for a slot may contain decls for stack slots with
212 higher (or the same) index, but not lower. */
214 static void
215 flush_quick_stack (void)
217 int stack_index = stack_pointer;
218 tree prev, cur, next;
220 /* First reverse the quick_stack, and count the number of slots it has. */
221 for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
223 next = TREE_CHAIN (cur);
224 TREE_CHAIN (cur) = prev;
225 prev = cur;
226 stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
228 quick_stack = prev;
230 while (quick_stack != NULL_TREE)
232 tree decl;
233 tree node = quick_stack, type;
234 quick_stack = TREE_CHAIN (node);
235 TREE_CHAIN (node) = tree_list_free_list;
236 tree_list_free_list = node;
237 node = TREE_VALUE (node);
238 type = TREE_TYPE (node);
240 decl = find_stack_slot (stack_index, type);
241 if (decl != node)
242 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
243 stack_index += 1 + TYPE_IS_WIDE (type);
247 /* Push TYPE on the type stack.
248 Return true on success, 0 on overflow. */
251 push_type_0 (tree type)
253 int n_words;
254 type = promote_type (type);
255 n_words = 1 + TYPE_IS_WIDE (type);
256 if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
257 return 0;
258 stack_type_map[stack_pointer++] = type;
259 n_words--;
260 while (--n_words >= 0)
261 stack_type_map[stack_pointer++] = TYPE_SECOND;
262 return 1;
265 void
266 push_type (tree type)
268 if (! push_type_0 (type))
269 abort ();
272 static void
273 push_value (tree value)
275 tree type = TREE_TYPE (value);
276 if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
278 type = promote_type (type);
279 value = convert (type, value);
281 push_type (type);
282 if (tree_list_free_list == NULL_TREE)
283 quick_stack = tree_cons (NULL_TREE, value, quick_stack);
284 else
286 tree node = tree_list_free_list;
287 tree_list_free_list = TREE_CHAIN (tree_list_free_list);
288 TREE_VALUE (node) = value;
289 TREE_CHAIN (node) = quick_stack;
290 quick_stack = node;
294 /* Pop a type from the type stack.
295 TYPE is the expected type. Return the actual type, which must be
296 convertible to TYPE.
297 On an error, *MESSAGEP is set to a freshly malloc'd error message. */
299 tree
300 pop_type_0 (tree type, char **messagep)
302 int n_words;
303 tree t;
304 *messagep = NULL;
305 if (TREE_CODE (type) == RECORD_TYPE)
306 type = promote_type (type);
307 n_words = 1 + TYPE_IS_WIDE (type);
308 if (stack_pointer < n_words)
310 *messagep = xstrdup ("stack underflow");
311 return type;
313 while (--n_words > 0)
315 if (stack_type_map[--stack_pointer] != void_type_node)
317 *messagep = xstrdup ("Invalid multi-word value on type stack");
318 return type;
321 t = stack_type_map[--stack_pointer];
322 if (type == NULL_TREE || t == type)
323 return t;
324 if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
325 && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
326 return t;
327 if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
329 if (type == ptr_type_node || type == object_ptr_type_node)
330 return t;
331 else if (t == ptr_type_node) /* Special case for null reference. */
332 return type;
333 else if (can_widen_reference_to (t, type))
334 return t;
335 /* This is a kludge, but matches what Sun's verifier does.
336 It can be tricked, but is safe as long as type errors
337 (i.e. interface method calls) are caught at run-time. */
338 else if (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (type))))
339 return object_ptr_type_node;
342 /* lang_printable_name uses a static buffer, so we must save the result
343 from calling it the first time. */
345 char *temp = xstrdup (lang_printable_name (type, 0));
346 *messagep = concat ("expected type '", temp,
347 "' but stack contains '", lang_printable_name (t, 0),
348 "'", NULL);
349 free (temp);
351 return type;
354 /* Pop a type from the type stack.
355 TYPE is the expected type. Return the actual type, which must be
356 convertible to TYPE, otherwise call error. */
358 tree
359 pop_type (tree type)
361 char *message = NULL;
362 type = pop_type_0 (type, &message);
363 if (message != NULL)
365 error ("%s", message);
366 free (message);
368 return type;
371 /* Return 1f if SOURCE_TYPE can be safely widened to TARGET_TYPE.
372 Handles array types and interfaces. */
375 can_widen_reference_to (tree source_type, tree target_type)
377 if (source_type == ptr_type_node || target_type == object_ptr_type_node)
378 return 1;
380 /* Get rid of pointers */
381 if (TREE_CODE (source_type) == POINTER_TYPE)
382 source_type = TREE_TYPE (source_type);
383 if (TREE_CODE (target_type) == POINTER_TYPE)
384 target_type = TREE_TYPE (target_type);
386 if (source_type == target_type)
387 return 1;
388 else
390 if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
392 HOST_WIDE_INT source_length, target_length;
393 if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
395 /* An array implements Cloneable and Serializable. */
396 tree name = DECL_NAME (TYPE_NAME (target_type));
397 return (name == java_lang_cloneable_identifier_node
398 || name == java_io_serializable_identifier_node);
400 target_length = java_array_type_length (target_type);
401 if (target_length >= 0)
403 source_length = java_array_type_length (source_type);
404 if (source_length != target_length)
405 return 0;
407 source_type = TYPE_ARRAY_ELEMENT (source_type);
408 target_type = TYPE_ARRAY_ELEMENT (target_type);
409 if (source_type == target_type)
410 return 1;
411 if (TREE_CODE (source_type) != POINTER_TYPE
412 || TREE_CODE (target_type) != POINTER_TYPE)
413 return 0;
414 return can_widen_reference_to (source_type, target_type);
416 else
418 int source_depth = class_depth (source_type);
419 int target_depth = class_depth (target_type);
421 /* class_depth can return a negative depth if an error occurred */
422 if (source_depth < 0 || target_depth < 0)
423 return 0;
425 if (CLASS_INTERFACE (TYPE_NAME (target_type)))
427 /* target_type is OK if source_type or source_type ancestors
428 implement target_type. We handle multiple sub-interfaces */
430 tree basetype_vec = BINFO_BASE_BINFOS (TYPE_BINFO (source_type));
431 int n = TREE_VEC_LENGTH (basetype_vec), i;
432 for (i=0 ; i < n; i++)
433 if (can_widen_reference_to
434 (TREE_TYPE (TREE_VEC_ELT (basetype_vec, i)),
435 target_type))
436 return 1;
437 if (n == 0)
438 return 0;
441 for ( ; source_depth > target_depth; source_depth--)
443 source_type
444 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
446 return source_type == target_type;
451 static tree
452 pop_value (tree type)
454 type = pop_type (type);
455 if (quick_stack)
457 tree node = quick_stack;
458 quick_stack = TREE_CHAIN (quick_stack);
459 TREE_CHAIN (node) = tree_list_free_list;
460 tree_list_free_list = node;
461 node = TREE_VALUE (node);
462 return node;
464 else
465 return find_stack_slot (stack_pointer, promote_type (type));
469 /* Pop and discard the top COUNT stack slots. */
471 static void
472 java_stack_pop (int count)
474 while (count > 0)
476 tree type, val;
478 if (stack_pointer == 0)
479 abort ();
481 type = stack_type_map[stack_pointer - 1];
482 if (type == TYPE_SECOND)
484 count--;
485 if (stack_pointer == 1 || count <= 0)
486 abort ();
488 type = stack_type_map[stack_pointer - 2];
490 val = pop_value (type);
491 count--;
495 /* Implement the 'swap' operator (to swap two top stack slots). */
497 static void
498 java_stack_swap (void)
500 tree type1, type2;
501 tree temp;
502 tree decl1, decl2;
504 if (stack_pointer < 2
505 || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_UNKNOWN
506 || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_UNKNOWN
507 || type1 == TYPE_SECOND || type2 == TYPE_SECOND
508 || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
509 /* Bad stack swap. */
510 abort ();
512 flush_quick_stack ();
513 decl1 = find_stack_slot (stack_pointer - 1, type1);
514 decl2 = find_stack_slot (stack_pointer - 2, type2);
515 temp = build_decl (VAR_DECL, NULL_TREE, type1);
516 java_add_local_var (temp);
517 java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
518 java_add_stmt (build2 (MODIFY_EXPR, type2,
519 find_stack_slot (stack_pointer - 1, type2),
520 decl2));
521 java_add_stmt (build2 (MODIFY_EXPR, type1,
522 find_stack_slot (stack_pointer - 2, type1),
523 temp));
524 stack_type_map[stack_pointer - 1] = type2;
525 stack_type_map[stack_pointer - 2] = type1;
528 static void
529 java_stack_dup (int size, int offset)
531 int low_index = stack_pointer - size - offset;
532 int dst_index;
533 if (low_index < 0)
534 error ("stack underflow - dup* operation");
536 flush_quick_stack ();
538 stack_pointer += size;
539 dst_index = stack_pointer;
541 for (dst_index = stack_pointer; --dst_index >= low_index; )
543 tree type;
544 int src_index = dst_index - size;
545 if (src_index < low_index)
546 src_index = dst_index + size + offset;
547 type = stack_type_map [src_index];
548 if (type == TYPE_SECOND)
550 if (src_index <= low_index)
551 /* Dup operation splits 64-bit number. */
552 abort ();
554 stack_type_map[dst_index] = type;
555 src_index--; dst_index--;
556 type = stack_type_map[src_index];
557 if (! TYPE_IS_WIDE (type))
558 abort ();
560 else if (TYPE_IS_WIDE (type))
561 abort ();
563 if (src_index != dst_index)
565 tree src_decl = find_stack_slot (src_index, type);
566 tree dst_decl = find_stack_slot (dst_index, type);
568 java_add_stmt
569 (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
570 stack_type_map[dst_index] = type;
575 /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
576 value stack. */
578 static void
579 build_java_athrow (tree node)
581 tree call;
583 call = build3 (CALL_EXPR,
584 void_type_node,
585 build_address_of (throw_node),
586 build_tree_list (NULL_TREE, node),
587 NULL_TREE);
588 TREE_SIDE_EFFECTS (call) = 1;
589 java_add_stmt (call);
590 java_stack_pop (stack_pointer);
593 /* Implementation for jsr/ret */
595 static void
596 build_java_jsr (int target_pc, int return_pc)
598 tree where = lookup_label (target_pc);
599 tree ret = lookup_label (return_pc);
600 tree ret_label = fold (build1 (ADDR_EXPR, return_address_type_node, ret));
601 push_value (ret_label);
602 flush_quick_stack ();
603 java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
605 /* Do not need to emit the label here. We noted the existance of the
606 label as a jump target in note_instructions; we'll emit the label
607 for real at the beginning of the expand_byte_code loop. */
610 static void
611 build_java_ret (tree location)
613 java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
616 /* Implementation of operations on array: new, load, store, length */
618 tree
619 decode_newarray_type (int atype)
621 switch (atype)
623 case 4: return boolean_type_node;
624 case 5: return char_type_node;
625 case 6: return float_type_node;
626 case 7: return double_type_node;
627 case 8: return byte_type_node;
628 case 9: return short_type_node;
629 case 10: return int_type_node;
630 case 11: return long_type_node;
631 default: return NULL_TREE;
635 /* Map primitive type to the code used by OPCODE_newarray. */
638 encode_newarray_type (tree type)
640 if (type == boolean_type_node)
641 return 4;
642 else if (type == char_type_node)
643 return 5;
644 else if (type == float_type_node)
645 return 6;
646 else if (type == double_type_node)
647 return 7;
648 else if (type == byte_type_node)
649 return 8;
650 else if (type == short_type_node)
651 return 9;
652 else if (type == int_type_node)
653 return 10;
654 else if (type == long_type_node)
655 return 11;
656 else
657 abort ();
660 /* Build a call to _Jv_ThrowBadArrayIndex(), the
661 ArrayIndexOfBoundsException exception handler. */
663 static tree
664 build_java_throw_out_of_bounds_exception (tree index)
666 tree node = build3 (CALL_EXPR, int_type_node,
667 build_address_of (soft_badarrayindex_node),
668 build_tree_list (NULL_TREE, index), NULL_TREE);
669 TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
670 return (node);
673 /* Return the length of an array. Doesn't perform any checking on the nature
674 or value of the array NODE. May be used to implement some bytecodes. */
676 tree
677 build_java_array_length_access (tree node)
679 tree type = TREE_TYPE (node);
680 tree array_type = TREE_TYPE (type);
681 HOST_WIDE_INT length;
683 /* JVM spec: If the arrayref is null, the arraylength instruction
684 throws a NullPointerException. The only way we could get a node
685 of type ptr_type_node at this point is `aconst_null; arraylength'
686 or something equivalent. */
687 if (type == ptr_type_node)
688 return build3 (CALL_EXPR, int_type_node,
689 build_address_of (soft_nullpointer_node),
690 NULL_TREE, NULL_TREE);
692 if (!is_array_type_p (type))
693 abort ();
695 length = java_array_type_length (type);
696 if (length >= 0)
697 return build_int_2 (length, 0);
699 node = build3 (COMPONENT_REF, int_type_node,
700 build_java_indirect_ref (array_type, node,
701 flag_check_references),
702 lookup_field (&array_type, get_identifier ("length")),
703 NULL_TREE);
704 IS_ARRAY_LENGTH_ACCESS (node) = 1;
705 return node;
708 /* Optionally checks a reference against the NULL pointer. ARG1: the
709 expr, ARG2: we should check the reference. Don't generate extra
710 checks if we're not generating code. */
712 tree
713 java_check_reference (tree expr, int check)
715 if (!flag_syntax_only && check)
717 expr = save_expr (expr);
718 expr = build3 (COND_EXPR, TREE_TYPE (expr),
719 build2 (EQ_EXPR, boolean_type_node,
720 expr, null_pointer_node),
721 build3 (CALL_EXPR, void_type_node,
722 build_address_of (soft_nullpointer_node),
723 NULL_TREE, NULL_TREE),
724 expr);
727 return expr;
730 /* Reference an object: just like an INDIRECT_REF, but with checking. */
732 tree
733 build_java_indirect_ref (tree type, tree expr, int check)
735 tree t;
736 t = java_check_reference (expr, check);
737 t = convert (build_pointer_type (type), t);
738 return build1 (INDIRECT_REF, type, t);
741 /* Implement array indexing (either as l-value or r-value).
742 Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
743 Optionally performs bounds checking and/or test to NULL.
744 At this point, ARRAY should have been verified as an array. */
746 tree
747 build_java_arrayaccess (tree array, tree type, tree index)
749 tree node, throw = NULL_TREE;
750 tree data_field;
751 tree ref;
752 tree array_type = TREE_TYPE (TREE_TYPE (array));
754 if (flag_bounds_check)
756 /* Generate:
757 * (unsigned jint) INDEX >= (unsigned jint) LEN
758 * && throw ArrayIndexOutOfBoundsException.
759 * Note this is equivalent to and more efficient than:
760 * INDEX < 0 || INDEX >= LEN && throw ... */
761 tree test;
762 tree len = build_java_array_length_access (array);
763 TREE_TYPE (len) = unsigned_int_type_node;
764 test = fold (build2 (GE_EXPR, boolean_type_node,
765 convert (unsigned_int_type_node, index),
766 len));
767 if (! integer_zerop (test))
769 throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
770 build_java_throw_out_of_bounds_exception (index));
771 /* allows expansion within COMPOUND */
772 TREE_SIDE_EFFECTS( throw ) = 1;
776 /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
777 to have the bounds check evaluated first. */
778 if (throw != NULL_TREE)
779 index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
781 data_field = lookup_field (&array_type, get_identifier ("data"));
783 ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
784 build_java_indirect_ref (array_type, array,
785 flag_check_references),
786 data_field, NULL_TREE);
788 node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
789 return node;
792 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
793 (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
794 determine that no check is required. */
796 tree
797 build_java_arraystore_check (tree array, tree object)
799 tree check, element_type, source;
800 tree array_type_p = TREE_TYPE (array);
801 tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
803 if (! is_array_type_p (array_type_p))
804 abort ();
806 /* Get the TYPE_DECL for ARRAY's element type. */
807 element_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
809 if (TREE_CODE (element_type) != TYPE_DECL
810 || TREE_CODE (object_type) != TYPE_DECL)
811 abort ();
813 if (!flag_store_check)
814 return build1 (NOP_EXPR, array_type_p, array);
816 /* No check is needed if the element type is final or is itself an array.
817 Also check that element_type matches object_type, since in the bytecode
818 compilation case element_type may be the actual element type of the array
819 rather than its declared type. */
820 if (element_type == object_type
821 && (TYPE_ARRAY_P (TREE_TYPE (element_type))
822 || CLASS_FINAL (element_type)))
823 return build1 (NOP_EXPR, array_type_p, array);
825 /* OBJECT might be wrapped by a SAVE_EXPR. */
826 if (TREE_CODE (object) == SAVE_EXPR)
827 source = TREE_OPERAND (object, 0);
828 else
829 source = object;
831 /* Avoid the check if OBJECT was just loaded from the same array. */
832 if (TREE_CODE (source) == ARRAY_REF)
834 tree target;
835 source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
836 source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
837 source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
838 if (TREE_CODE (source) == SAVE_EXPR)
839 source = TREE_OPERAND (source, 0);
841 target = array;
842 if (TREE_CODE (target) == SAVE_EXPR)
843 target = TREE_OPERAND (target, 0);
845 if (source == target)
846 return build1 (NOP_EXPR, array_type_p, array);
849 /* Build an invocation of _Jv_CheckArrayStore */
850 check = build3 (CALL_EXPR, void_type_node,
851 build_address_of (soft_checkarraystore_node),
852 tree_cons (NULL_TREE, array,
853 build_tree_list (NULL_TREE, object)),
854 NULL_TREE);
855 TREE_SIDE_EFFECTS (check) = 1;
857 return check;
860 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
861 ARRAY_NODE. This function is used to retrieve something less vague than
862 a pointer type when indexing the first dimension of something like [[<t>.
863 May return a corrected type, if necessary, otherwise INDEXED_TYPE is
864 return unchanged.
865 As a side effect, it also makes sure that ARRAY_NODE is an array. */
867 static tree
868 build_java_check_indexed_type (tree array_node, tree indexed_type)
870 tree elt_type;
872 if (!is_array_type_p (TREE_TYPE (array_node)))
873 abort ();
875 elt_type = (TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (array_node))));
877 if (indexed_type == ptr_type_node )
878 return promote_type (elt_type);
880 /* BYTE/BOOLEAN store and load are used for both type */
881 if (indexed_type == byte_type_node && elt_type == boolean_type_node )
882 return boolean_type_node;
884 if (indexed_type != elt_type )
885 abort ();
886 else
887 return indexed_type;
890 /* newarray triggers a call to _Jv_NewPrimArray. This function should be
891 called with an integer code (the type of array to create), and the length
892 of the array to create. */
894 tree
895 build_newarray (int atype_value, tree length)
897 tree type_arg;
899 tree prim_type = decode_newarray_type (atype_value);
900 tree type
901 = build_java_array_type (prim_type,
902 host_integerp (length, 0) == INTEGER_CST
903 ? tree_low_cst (length, 0) : -1);
905 /* If compiling to native, pass a reference to the primitive type class
906 and save the runtime some work. However, the bytecode generator
907 expects to find the type_code int here. */
908 if (flag_emit_class_files)
909 type_arg = build_int_2 (atype_value, 0);
910 else
911 type_arg = build_class_ref (prim_type);
913 return build3 (CALL_EXPR, promote_type (type),
914 build_address_of (soft_newarray_node),
915 tree_cons (NULL_TREE,
916 type_arg,
917 build_tree_list (NULL_TREE, length)),
918 NULL_TREE);
921 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
922 of the dimension. */
924 tree
925 build_anewarray (tree class_type, tree length)
927 tree type
928 = build_java_array_type (class_type,
929 host_integerp (length, 0)
930 ? tree_low_cst (length, 0) : -1);
932 return build3 (CALL_EXPR, promote_type (type),
933 build_address_of (soft_anewarray_node),
934 tree_cons (NULL_TREE, length,
935 tree_cons (NULL_TREE, build_class_ref (class_type),
936 build_tree_list (NULL_TREE,
937 null_pointer_node))),
938 NULL_TREE);
941 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
943 tree
944 build_new_array (tree type, tree length)
946 if (JPRIMITIVE_TYPE_P (type))
947 return build_newarray (encode_newarray_type (type), length);
948 else
949 return build_anewarray (TREE_TYPE (type), length);
952 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
953 class pointer, a number of dimensions and the matching number of
954 dimensions. The argument list is NULL terminated. */
956 static void
957 expand_java_multianewarray (tree class_type, int ndim)
959 int i;
960 tree args = build_tree_list( NULL_TREE, null_pointer_node );
962 for( i = 0; i < ndim; i++ )
963 args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
965 push_value (build3 (CALL_EXPR,
966 promote_type (class_type),
967 build_address_of (soft_multianewarray_node),
968 tree_cons (NULL_TREE, build_class_ref (class_type),
969 tree_cons (NULL_TREE,
970 build_int_2 (ndim, 0), args)),
971 NULL_TREE));
974 /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
975 ARRAY is an array type. May expand some bound checking and NULL
976 pointer checking. RHS_TYPE_NODE we are going to store. In the case
977 of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
978 INT. In those cases, we make the conversion.
980 if ARRAy is a reference type, the assignment is checked at run-time
981 to make sure that the RHS can be assigned to the array element
982 type. It is not necessary to generate this code if ARRAY is final. */
984 static void
985 expand_java_arraystore (tree rhs_type_node)
987 tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
988 && TYPE_PRECISION (rhs_type_node) <= 32) ?
989 int_type_node : rhs_type_node);
990 tree index = pop_value (int_type_node);
991 tree array = pop_value (ptr_type_node);
993 rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
995 flush_quick_stack ();
997 index = save_expr (index);
998 array = save_expr (array);
1000 if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1002 tree check = build_java_arraystore_check (array, rhs_node);
1003 java_add_stmt (check);
1006 array = build_java_arrayaccess (array, rhs_type_node, index);
1007 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));
1010 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
1011 sure that LHS is an array type. May expand some bound checking and NULL
1012 pointer checking.
1013 LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1014 BOOLEAN/SHORT, we push a promoted type back to the stack.
1017 static void
1018 expand_java_arrayload (tree lhs_type_node )
1020 tree load_node;
1021 tree index_node = pop_value (int_type_node);
1022 tree array_node = pop_value (ptr_type_node);
1024 index_node = save_expr (index_node);
1025 array_node = save_expr (array_node);
1027 if (TREE_TYPE (array_node) == ptr_type_node)
1028 /* The only way we could get a node of type ptr_type_node at this
1029 point is `aconst_null; arraylength' or something equivalent, so
1030 unconditionally throw NullPointerException. */
1031 load_node = build3 (CALL_EXPR, lhs_type_node,
1032 build_address_of (soft_nullpointer_node),
1033 NULL_TREE, NULL_TREE);
1034 else
1036 lhs_type_node = build_java_check_indexed_type (array_node, lhs_type_node);
1037 load_node = build_java_arrayaccess (array_node,
1038 lhs_type_node,
1039 index_node);
1041 if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1042 load_node = fold (build1 (NOP_EXPR, int_type_node, load_node));
1043 push_value (load_node);
1046 /* Expands .length. Makes sure that we deal with and array and may expand
1047 a NULL check on the array object. */
1049 static void
1050 expand_java_array_length (void)
1052 tree array = pop_value (ptr_type_node);
1053 tree length = build_java_array_length_access (array);
1055 push_value (length);
1058 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1059 either soft_monitorenter_node or soft_monitorexit_node. */
1061 static tree
1062 build_java_monitor (tree call, tree object)
1064 return build3 (CALL_EXPR,
1065 void_type_node,
1066 build_address_of (call),
1067 build_tree_list (NULL_TREE, object),
1068 NULL_TREE);
1071 /* Emit code for one of the PUSHC instructions. */
1073 static void
1074 expand_java_pushc (int ival, tree type)
1076 tree value;
1077 if (type == ptr_type_node && ival == 0)
1078 value = null_pointer_node;
1079 else if (type == int_type_node || type == long_type_node)
1081 value = build_int_2 (ival, ival < 0 ? -1 : 0);
1082 TREE_TYPE (value) = type;
1084 else if (type == float_type_node || type == double_type_node)
1086 REAL_VALUE_TYPE x;
1087 REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1088 value = build_real (type, x);
1090 else
1091 abort ();
1093 push_value (value);
1096 static void
1097 expand_java_return (tree type)
1099 if (type == void_type_node)
1100 java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
1101 else
1103 tree retval = pop_value (type);
1104 tree res = DECL_RESULT (current_function_decl);
1105 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1107 /* Handle the situation where the native integer type is smaller
1108 than the JVM integer. It can happen for many cross compilers.
1109 The whole if expression just goes away if INT_TYPE_SIZE < 32
1110 is false. */
1111 if (INT_TYPE_SIZE < 32
1112 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1113 < GET_MODE_SIZE (TYPE_MODE (type))))
1114 retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1116 TREE_SIDE_EFFECTS (retval) = 1;
1117 java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1121 static void
1122 expand_load_internal (int index, tree type, int pc)
1124 tree copy;
1125 tree var = find_local_variable (index, type, pc);
1127 /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1128 on the stack. If there is an assignment to this VAR_DECL between
1129 the stack push and the use, then the wrong code could be
1130 generated. To avoid this we create a new local and copy our
1131 value into it. Then we push this new local on the stack.
1132 Hopefully this all gets optimized out. */
1133 copy = build_decl (VAR_DECL, NULL_TREE, type);
1134 java_add_local_var (copy);
1135 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1137 push_value (copy);
1140 tree
1141 build_address_of (tree value)
1143 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1146 bool
1147 class_has_finalize_method (tree type)
1149 tree super = CLASSTYPE_SUPER (type);
1151 if (super == NULL_TREE)
1152 return false; /* Every class with a real finalizer inherits */
1153 /* from java.lang.Object. */
1154 else
1155 return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1158 static void
1159 expand_java_NEW (tree type)
1161 tree alloc_node;
1163 alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1164 : alloc_no_finalizer_node);
1165 if (! CLASS_LOADED_P (type))
1166 load_class (type, 1);
1167 safe_layout_class (type);
1168 push_value (build3 (CALL_EXPR, promote_type (type),
1169 build_address_of (alloc_node),
1170 build_tree_list (NULL_TREE, build_class_ref (type)),
1171 NULL_TREE));
1174 /* This returns an expression which will extract the class of an
1175 object. */
1177 tree
1178 build_get_class (tree value)
1180 tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1181 tree vtable_field = lookup_field (&object_type_node,
1182 get_identifier ("vtable"));
1183 tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1184 build_java_indirect_ref (object_type_node, value,
1185 flag_check_references),
1186 vtable_field, NULL_TREE);
1187 return build3 (COMPONENT_REF, class_ptr_type,
1188 build1 (INDIRECT_REF, dtable_type, tmp),
1189 class_field, NULL_TREE);
1192 /* This builds the tree representation of the `instanceof' operator.
1193 It tries various tricks to optimize this in cases where types are
1194 known. */
1196 tree
1197 build_instanceof (tree value, tree type)
1199 tree expr;
1200 tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1201 tree valtype = TREE_TYPE (TREE_TYPE (value));
1202 tree valclass = TYPE_NAME (valtype);
1203 tree klass;
1205 /* When compiling from bytecode, we need to ensure that TYPE has
1206 been loaded. */
1207 if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1209 load_class (type, 1);
1210 safe_layout_class (type);
1211 if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1212 return error_mark_node;
1214 klass = TYPE_NAME (type);
1216 if (type == object_type_node || inherits_from_p (valtype, type))
1218 /* Anything except `null' is an instance of Object. Likewise,
1219 if the object is known to be an instance of the class, then
1220 we only need to check for `null'. */
1221 expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1223 else if (! TYPE_ARRAY_P (type)
1224 && ! TYPE_ARRAY_P (valtype)
1225 && DECL_P (klass) && DECL_P (valclass)
1226 && ! CLASS_INTERFACE (valclass)
1227 && ! CLASS_INTERFACE (klass)
1228 && ! inherits_from_p (type, valtype)
1229 && (CLASS_FINAL (klass)
1230 || ! inherits_from_p (valtype, type)))
1232 /* The classes are from different branches of the derivation
1233 tree, so we immediately know the answer. */
1234 expr = boolean_false_node;
1236 else if (DECL_P (klass) && CLASS_FINAL (klass))
1238 tree save = save_expr (value);
1239 expr = build3 (COND_EXPR, itype,
1240 build2 (NE_EXPR, boolean_type_node,
1241 save, null_pointer_node),
1242 build2 (EQ_EXPR, itype,
1243 build_get_class (save),
1244 build_class_ref (type)),
1245 boolean_false_node);
1247 else
1249 expr = build3 (CALL_EXPR, itype,
1250 build_address_of (soft_instanceof_node),
1251 tree_cons (NULL_TREE, value,
1252 build_tree_list (NULL_TREE,
1253 build_class_ref (type))),
1254 NULL_TREE);
1256 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1257 return expr;
1260 static void
1261 expand_java_INSTANCEOF (tree type)
1263 tree value = pop_value (object_ptr_type_node);
1264 value = build_instanceof (value, type);
1265 push_value (value);
1268 static void
1269 expand_java_CHECKCAST (tree type)
1271 tree value = pop_value (ptr_type_node);
1272 value = build3 (CALL_EXPR, promote_type (type),
1273 build_address_of (soft_checkcast_node),
1274 tree_cons (NULL_TREE, build_class_ref (type),
1275 build_tree_list (NULL_TREE, value)),
1276 NULL_TREE);
1277 push_value (value);
1280 static void
1281 expand_iinc (unsigned int local_var_index, int ival, int pc)
1283 tree local_var, res;
1284 tree constant_value;
1286 flush_quick_stack ();
1287 local_var = find_local_variable (local_var_index, int_type_node, pc);
1288 constant_value = build_int_2 (ival, ival < 0 ? -1 : 0);
1289 res = fold (build2 (PLUS_EXPR, int_type_node, local_var, constant_value));
1290 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1291 update_aliases (local_var, local_var_index);
1295 tree
1296 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1298 tree call = NULL;
1299 tree arg1 = convert (type, op1);
1300 tree arg2 = convert (type, op2);
1302 if (type == int_type_node)
1304 switch (op)
1306 case TRUNC_DIV_EXPR:
1307 call = soft_idiv_node;
1308 break;
1309 case TRUNC_MOD_EXPR:
1310 call = soft_irem_node;
1311 break;
1312 default:
1313 break;
1316 else if (type == long_type_node)
1318 switch (op)
1320 case TRUNC_DIV_EXPR:
1321 call = soft_ldiv_node;
1322 break;
1323 case TRUNC_MOD_EXPR:
1324 call = soft_lrem_node;
1325 break;
1326 default:
1327 break;
1331 if (! call)
1332 abort ();
1334 call = build3 (CALL_EXPR, type,
1335 build_address_of (call),
1336 tree_cons (NULL_TREE, arg1,
1337 build_tree_list (NULL_TREE, arg2)),
1338 NULL_TREE);
1340 return call;
1343 tree
1344 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1346 tree mask;
1347 switch (op)
1349 case URSHIFT_EXPR:
1351 tree u_type = java_unsigned_type (type);
1352 arg1 = convert (u_type, arg1);
1353 arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1354 return convert (type, arg1);
1356 case LSHIFT_EXPR:
1357 case RSHIFT_EXPR:
1358 mask = build_int_2 (TYPE_PRECISION (TREE_TYPE (arg1)) - 1, 0);
1359 arg2 = fold (build2 (BIT_AND_EXPR, int_type_node, arg2, mask));
1360 break;
1362 case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
1363 case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
1364 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1366 tree ifexp1 = fold (build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1367 boolean_type_node, arg1, arg2));
1368 tree ifexp2 = fold (build2 (EQ_EXPR, boolean_type_node, arg1, arg2));
1369 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1370 ifexp2, integer_zero_node,
1371 op == COMPARE_L_EXPR
1372 ? integer_minus_one_node
1373 : integer_one_node));
1374 return fold (build3 (COND_EXPR, int_type_node, ifexp1,
1375 op == COMPARE_L_EXPR ? integer_one_node
1376 : integer_minus_one_node,
1377 second_compare));
1379 case COMPARE_EXPR:
1380 arg1 = save_expr (arg1); arg2 = save_expr (arg2);
1382 tree ifexp1 = fold (build2 (LT_EXPR, boolean_type_node, arg1, arg2));
1383 tree ifexp2 = fold (build2 (GT_EXPR, boolean_type_node, arg1, arg2));
1384 tree second_compare = fold (build3 (COND_EXPR, int_type_node,
1385 ifexp2, integer_one_node,
1386 integer_zero_node));
1387 return fold (build3 (COND_EXPR, int_type_node,
1388 ifexp1, integer_minus_one_node, second_compare));
1390 case TRUNC_DIV_EXPR:
1391 case TRUNC_MOD_EXPR:
1392 if (TREE_CODE (type) == REAL_TYPE
1393 && op == TRUNC_MOD_EXPR)
1395 tree call;
1396 if (type != double_type_node)
1398 arg1 = convert (double_type_node, arg1);
1399 arg2 = convert (double_type_node, arg2);
1401 call = build3 (CALL_EXPR, double_type_node,
1402 build_address_of (soft_fmod_node),
1403 tree_cons (NULL_TREE, arg1,
1404 build_tree_list (NULL_TREE, arg2)),
1405 NULL_TREE);
1406 if (type != double_type_node)
1407 call = convert (type, call);
1408 return call;
1411 if (TREE_CODE (type) == INTEGER_TYPE
1412 && flag_use_divide_subroutine
1413 && ! flag_syntax_only)
1414 return build_java_soft_divmod (op, type, arg1, arg2);
1416 break;
1417 default: ;
1419 return fold (build2 (op, type, arg1, arg2));
1422 static void
1423 expand_java_binop (tree type, enum tree_code op)
1425 tree larg, rarg;
1426 tree ltype = type;
1427 tree rtype = type;
1428 switch (op)
1430 case LSHIFT_EXPR:
1431 case RSHIFT_EXPR:
1432 case URSHIFT_EXPR:
1433 rtype = int_type_node;
1434 rarg = pop_value (rtype);
1435 break;
1436 default:
1437 rarg = pop_value (rtype);
1439 larg = pop_value (ltype);
1440 push_value (build_java_binop (op, type, larg, rarg));
1443 /* Lookup the field named NAME in *TYPEP or its super classes.
1444 If not found, return NULL_TREE.
1445 (If the *TYPEP is not found, or if the field reference is
1446 ambiguous, return error_mark_node.)
1447 If found, return the FIELD_DECL, and set *TYPEP to the
1448 class containing the field. */
1450 tree
1451 lookup_field (tree *typep, tree name)
1453 if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1455 load_class (*typep, 1);
1456 safe_layout_class (*typep);
1457 if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1458 return error_mark_node;
1462 tree field, basetype_vec;
1463 tree save_field;
1464 int n, i;
1466 for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1467 if (DECL_NAME (field) == name)
1468 return field;
1470 /* Process implemented interfaces. */
1471 basetype_vec = BINFO_BASE_BINFOS (TYPE_BINFO (*typep));
1472 n = TREE_VEC_LENGTH (basetype_vec);
1473 save_field = NULL_TREE;
1474 for (i = 0; i < n; i++)
1476 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
1477 if ((field = lookup_field (&t, name)))
1479 if (save_field == field)
1480 continue;
1481 if (save_field == NULL_TREE)
1482 save_field = field;
1483 else
1485 tree i1 = DECL_CONTEXT (save_field);
1486 tree i2 = DECL_CONTEXT (field);
1487 error ("reference `%s' is ambiguous: appears in interface `%s' and interface `%s'",
1488 IDENTIFIER_POINTER (name),
1489 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1490 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1491 return error_mark_node;
1496 if (save_field != NULL_TREE)
1497 return save_field;
1499 *typep = CLASSTYPE_SUPER (*typep);
1500 } while (*typep);
1501 return NULL_TREE;
1504 /* Look up the field named NAME in object SELF_VALUE,
1505 which has class SELF_CLASS (a non-handle RECORD_TYPE).
1506 SELF_VALUE is NULL_TREE if looking for a static field. */
1508 tree
1509 build_field_ref (tree self_value, tree self_class, tree name)
1511 tree base_class = self_class;
1512 tree field_decl = lookup_field (&base_class, name);
1513 if (field_decl == NULL_TREE)
1515 error ("field `%s' not found", IDENTIFIER_POINTER (name));
1516 return error_mark_node;
1518 if (self_value == NULL_TREE)
1520 return build_static_field_ref (field_decl);
1522 else
1524 int check = (flag_check_references
1525 && ! (DECL_P (self_value)
1526 && DECL_NAME (self_value) == this_identifier_node));
1528 tree base_type = promote_type (base_class);
1529 if (base_type != TREE_TYPE (self_value))
1530 self_value = fold (build1 (NOP_EXPR, base_type, self_value));
1531 if (flag_indirect_dispatch
1532 && output_class != self_class)
1533 /* FIXME: output_class != self_class is not exactly the right
1534 test. What we really want to know is whether self_class is
1535 in the same translation unit as output_class. If it is,
1536 we can make a direct reference. */
1538 tree otable_index
1539 = build_int_2 (get_symbol_table_index
1540 (field_decl, &TYPE_OTABLE_METHODS (output_class)),
1542 tree field_offset
1543 = build4 (ARRAY_REF, integer_type_node,
1544 TYPE_OTABLE_DECL (output_class), otable_index,
1545 NULL_TREE, NULL_TREE);
1546 tree address;
1548 field_offset = fold (convert (sizetype, field_offset));
1549 address
1550 = fold (build2 (PLUS_EXPR,
1551 build_pointer_type (TREE_TYPE (field_decl)),
1552 self_value, field_offset));
1553 return fold (build1 (INDIRECT_REF, TREE_TYPE (field_decl), address));
1556 self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1557 self_value, check);
1558 return fold (build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1559 self_value, field_decl, NULL_TREE));
1563 tree
1564 lookup_label (int pc)
1566 tree name;
1567 char buf[32];
1568 ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", pc);
1569 name = get_identifier (buf);
1570 if (IDENTIFIER_LOCAL_VALUE (name))
1571 return IDENTIFIER_LOCAL_VALUE (name);
1572 else
1574 /* The type of the address of a label is return_address_type_node. */
1575 tree decl = create_label_decl (name);
1576 LABEL_PC (decl) = pc;
1577 return pushdecl (decl);
1581 /* Generate a unique name for the purpose of loops and switches
1582 labels, and try-catch-finally blocks label or temporary variables. */
1584 tree
1585 generate_name (void)
1587 static int l_number = 0;
1588 char buff [32];
1589 ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1590 l_number++;
1591 return get_identifier (buff);
1594 tree
1595 create_label_decl (tree name)
1597 tree decl;
1598 decl = build_decl (LABEL_DECL, name,
1599 TREE_TYPE (return_address_type_node));
1600 DECL_CONTEXT (decl) = current_function_decl;
1601 DECL_IGNORED_P (decl) = 1;
1602 return decl;
1605 /* This maps a bytecode offset (PC) to various flags. */
1606 char *instruction_bits;
1608 static void
1609 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1611 lookup_label (target_pc);
1612 instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1615 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1616 where CONDITION is one of one the compare operators. */
1618 static void
1619 expand_compare (enum tree_code condition, tree value1, tree value2,
1620 int target_pc)
1622 tree target = lookup_label (target_pc);
1623 tree cond = fold (build2 (condition, boolean_type_node, value1, value2));
1624 java_add_stmt
1625 (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1626 build1 (GOTO_EXPR, void_type_node, target),
1627 build_java_empty_stmt ()));
1630 /* Emit code for a TEST-type opcode. */
1632 static void
1633 expand_test (enum tree_code condition, tree type, int target_pc)
1635 tree value1, value2;
1636 flush_quick_stack ();
1637 value1 = pop_value (type);
1638 value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1639 expand_compare (condition, value1, value2, target_pc);
1642 /* Emit code for a COND-type opcode. */
1644 static void
1645 expand_cond (enum tree_code condition, tree type, int target_pc)
1647 tree value1, value2;
1648 flush_quick_stack ();
1649 /* note: pop values in opposite order */
1650 value2 = pop_value (type);
1651 value1 = pop_value (type);
1652 /* Maybe should check value1 and value2 for type compatibility ??? */
1653 expand_compare (condition, value1, value2, target_pc);
1656 static void
1657 expand_java_goto (int target_pc)
1659 tree target_label = lookup_label (target_pc);
1660 flush_quick_stack ();
1661 java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1664 static tree
1665 expand_java_switch (tree selector, int default_pc)
1667 tree switch_expr, x;
1669 flush_quick_stack ();
1670 switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1671 NULL_TREE, NULL_TREE);
1672 java_add_stmt (switch_expr);
1674 x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1675 create_artificial_label ());
1676 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1678 x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1679 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1681 return switch_expr;
1684 static void
1685 expand_java_add_case (tree switch_expr, int match, int target_pc)
1687 tree value, x;
1689 value = build_int_2 (match, match < 0 ? -1 : 0);
1690 TREE_TYPE (value) = TREE_TYPE (switch_expr);
1692 x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1693 create_artificial_label ());
1694 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1696 x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1697 append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1700 #if 0
1701 static void
1702 expand_java_call (int target_pc, int return_address)
1704 tree target_label = lookup_label (target_pc);
1705 tree value = build_int_2 (return_address, return_address < 0 ? -1 : 0);
1706 push_value (value);
1707 flush_quick_stack ();
1708 expand_goto (target_label);
1711 static void
1712 expand_java_ret (tree return_address ATTRIBUTE_UNUSED)
1714 warning ("ret instruction not implemented");
1715 #if 0
1716 tree target_label = lookup_label (target_pc);
1717 flush_quick_stack ();
1718 expand_goto (target_label);
1719 #endif
1721 #endif
1723 static tree
1724 pop_arguments (tree arg_types)
1726 if (arg_types == end_params_node)
1727 return NULL_TREE;
1728 if (TREE_CODE (arg_types) == TREE_LIST)
1730 tree tail = pop_arguments (TREE_CHAIN (arg_types));
1731 tree type = TREE_VALUE (arg_types);
1732 tree arg = pop_value (type);
1733 if (targetm.calls.promote_prototypes (type)
1734 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1735 && INTEGRAL_TYPE_P (type))
1736 arg = convert (integer_type_node, arg);
1737 return tree_cons (NULL_TREE, arg, tail);
1739 abort ();
1742 /* Build an expression to initialize the class CLAS.
1743 if EXPR is non-NULL, returns an expression to first call the initializer
1744 (if it is needed) and then calls EXPR. */
1746 tree
1747 build_class_init (tree clas, tree expr)
1749 tree init;
1751 /* An optimization: if CLAS is a superclass of the class we're
1752 compiling, we don't need to initialize it. However, if CLAS is
1753 an interface, it won't necessarily be initialized, even if we
1754 implement it. */
1755 if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1756 && inherits_from_p (current_class, clas))
1757 || current_class == clas)
1758 return expr;
1760 if (always_initialize_class_p)
1762 init = build3 (CALL_EXPR, void_type_node,
1763 build_address_of (soft_initclass_node),
1764 build_tree_list (NULL_TREE, build_class_ref (clas)),
1765 NULL_TREE);
1766 TREE_SIDE_EFFECTS (init) = 1;
1768 else
1770 tree *init_test_decl;
1771 init_test_decl = java_treetreehash_new
1772 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1774 if (*init_test_decl == NULL)
1776 /* Build a declaration and mark it as a flag used to track
1777 static class initializations. */
1778 *init_test_decl = build_decl (VAR_DECL, NULL_TREE,
1779 boolean_type_node);
1780 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (*init_test_decl);
1781 LOCAL_CLASS_INITIALIZATION_FLAG (*init_test_decl) = 1;
1782 DECL_CONTEXT (*init_test_decl) = current_function_decl;
1783 DECL_FUNCTION_INIT_TEST_CLASS (*init_test_decl) = clas;
1784 /* Tell the check-init code to ignore this decl when not
1785 optimizing class initialization. */
1786 if (!STATIC_CLASS_INIT_OPT_P ())
1787 DECL_BIT_INDEX(*init_test_decl) = -1;
1788 DECL_INITIAL (*init_test_decl) = integer_zero_node;
1789 /* Don't emit any symbolic debugging info for this decl. */
1790 DECL_IGNORED_P (*init_test_decl) = 1;
1793 init = build3 (CALL_EXPR, void_type_node,
1794 build_address_of (soft_initclass_node),
1795 build_tree_list (NULL_TREE, build_class_ref (clas)),
1796 NULL_TREE);
1797 TREE_SIDE_EFFECTS (init) = 1;
1798 init = build3 (COND_EXPR, void_type_node,
1799 build2 (EQ_EXPR, boolean_type_node,
1800 *init_test_decl, boolean_false_node),
1801 init, integer_zero_node);
1802 TREE_SIDE_EFFECTS (init) = 1;
1803 init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
1804 build2 (MODIFY_EXPR, boolean_type_node,
1805 *init_test_decl, boolean_true_node));
1806 TREE_SIDE_EFFECTS (init) = 1;
1809 if (expr != NULL_TREE)
1811 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1812 TREE_SIDE_EFFECTS (expr) = 1;
1813 return expr;
1815 return init;
1818 tree
1819 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
1820 tree self_type, tree method_signature ATTRIBUTE_UNUSED,
1821 tree arg_list ATTRIBUTE_UNUSED)
1823 tree func;
1824 if (is_compiled_class (self_type))
1826 if (!flag_indirect_dispatch
1827 || (!TREE_PUBLIC (method) && DECL_CONTEXT (method)))
1829 make_decl_rtl (method, NULL);
1830 func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
1831 method);
1833 else
1835 tree table_index
1836 = build_int_2 (get_symbol_table_index
1837 (method, &TYPE_ATABLE_METHODS (output_class)), 0);
1838 func = build4 (ARRAY_REF, method_ptr_type_node,
1839 TYPE_ATABLE_DECL (output_class), table_index,
1840 NULL_TREE, NULL_TREE);
1842 func = convert (method_ptr_type_node, func);
1844 else
1846 /* We don't know whether the method has been (statically) compiled.
1847 Compile this code to get a reference to the method's code:
1849 SELF_TYPE->methods[METHOD_INDEX].ncode
1853 int method_index = 0;
1854 tree meth, ref;
1856 /* The method might actually be declared in some superclass, so
1857 we have to use its class context, not the caller's notion of
1858 where the method is. */
1859 self_type = DECL_CONTEXT (method);
1860 ref = build_class_ref (self_type);
1861 ref = build1 (INDIRECT_REF, class_type_node, ref);
1862 if (ncode_ident == NULL_TREE)
1863 ncode_ident = get_identifier ("ncode");
1864 if (methods_ident == NULL_TREE)
1865 methods_ident = get_identifier ("methods");
1866 ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
1867 lookup_field (&class_type_node, methods_ident),
1868 NULL_TREE);
1869 for (meth = TYPE_METHODS (self_type);
1870 ; meth = TREE_CHAIN (meth))
1872 if (method == meth)
1873 break;
1874 if (meth == NULL_TREE)
1875 fatal_error ("method '%s' not found in class",
1876 IDENTIFIER_POINTER (DECL_NAME (method)));
1877 method_index++;
1879 method_index *= int_size_in_bytes (method_type_node);
1880 ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
1881 ref, build_int_2 (method_index, 0)));
1882 ref = build1 (INDIRECT_REF, method_type_node, ref);
1883 func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
1884 ref, lookup_field (&method_type_node, ncode_ident),
1885 NULL_TREE);
1887 return func;
1890 tree
1891 invoke_build_dtable (int is_invoke_interface, tree arg_list)
1893 tree dtable, objectref;
1895 TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
1897 /* If we're dealing with interfaces and if the objectref
1898 argument is an array then get the dispatch table of the class
1899 Object rather than the one from the objectref. */
1900 objectref = (is_invoke_interface
1901 && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list))) ?
1902 object_type_node : TREE_VALUE (arg_list));
1904 if (dtable_ident == NULL_TREE)
1905 dtable_ident = get_identifier ("vtable");
1906 dtable = build_java_indirect_ref (object_type_node, objectref,
1907 flag_check_references);
1908 dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
1909 lookup_field (&object_type_node, dtable_ident), NULL_TREE);
1911 return dtable;
1914 /* Determine the index in SYMBOL_TABLE for a reference to the decl
1915 T. If this decl has not been seen before, it will be added to the
1916 otable_methods. If it has, the existing table slot will be
1917 reused. */
1920 get_symbol_table_index (tree t, tree *symbol_table)
1922 int i = 1;
1923 tree method_list;
1925 if (*symbol_table == NULL_TREE)
1927 *symbol_table = build_tree_list (t, t);
1928 return 1;
1931 method_list = *symbol_table;
1933 while (1)
1935 tree value = TREE_VALUE (method_list);
1936 if (value == t)
1937 return i;
1938 i++;
1939 if (TREE_CHAIN (method_list) == NULL_TREE)
1940 break;
1941 else
1942 method_list = TREE_CHAIN (method_list);
1945 TREE_CHAIN (method_list) = build_tree_list (t, t);
1946 return i;
1949 tree
1950 build_invokevirtual (tree dtable, tree method)
1952 tree func;
1953 tree nativecode_ptr_ptr_type_node
1954 = build_pointer_type (nativecode_ptr_type_node);
1955 tree method_index;
1956 tree otable_index;
1958 if (flag_indirect_dispatch)
1960 otable_index
1961 = build_int_2 (get_symbol_table_index
1962 (method, &TYPE_OTABLE_METHODS (output_class)), 0);
1963 method_index = build4 (ARRAY_REF, integer_type_node,
1964 TYPE_OTABLE_DECL (output_class),
1965 otable_index, NULL_TREE, NULL_TREE);
1967 else
1969 /* We fetch the DECL_VINDEX field directly here, rather than
1970 using get_method_index(). DECL_VINDEX is the true offset
1971 from the vtable base to a method, regrdless of any extra
1972 words inserted at the start of the vtable. */
1973 method_index = DECL_VINDEX (method);
1974 method_index = size_binop (MULT_EXPR, method_index,
1975 TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
1976 if (TARGET_VTABLE_USES_DESCRIPTORS)
1977 method_index = size_binop (MULT_EXPR, method_index,
1978 size_int (TARGET_VTABLE_USES_DESCRIPTORS));
1981 func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
1982 convert (nativecode_ptr_ptr_type_node, method_index)));
1984 if (TARGET_VTABLE_USES_DESCRIPTORS)
1985 func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
1986 else
1987 func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
1989 return func;
1992 static GTY(()) tree class_ident;
1993 tree
1994 build_invokeinterface (tree dtable, tree method)
1996 tree lookup_arg;
1997 tree interface;
1998 tree idx;
1999 tree otable_index;
2001 /* We expand invokeinterface here. _Jv_LookupInterfaceMethod() will
2002 ensure that the selected method exists, is public and not
2003 abstract nor static. */
2005 if (class_ident == NULL_TREE)
2006 class_ident = get_identifier ("class");
2008 dtable = build_java_indirect_ref (dtable_type, dtable,
2009 flag_check_references);
2010 dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2011 lookup_field (&dtable_type, class_ident), NULL_TREE);
2013 interface = DECL_CONTEXT (method);
2014 if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2015 abort ();
2016 layout_class_methods (interface);
2018 if (flag_indirect_dispatch)
2020 otable_index
2021 = build_int_2 (get_symbol_table_index
2022 (method, &TYPE_OTABLE_METHODS (output_class)), 0);
2023 idx = build4 (ARRAY_REF, integer_type_node,
2024 TYPE_OTABLE_DECL (output_class), otable_index,
2025 NULL_TREE, NULL_TREE);
2027 else
2028 idx = build_int_2 (get_interface_method_index (method, interface), 0);
2030 lookup_arg = tree_cons (NULL_TREE, dtable,
2031 tree_cons (NULL_TREE, build_class_ref (interface),
2032 build_tree_list (NULL_TREE, idx)));
2034 return build3 (CALL_EXPR, ptr_type_node,
2035 build_address_of (soft_lookupinterfacemethod_node),
2036 lookup_arg, NULL_TREE);
2039 /* Expand one of the invoke_* opcodes.
2040 OCPODE is the specific opcode.
2041 METHOD_REF_INDEX is an index into the constant pool.
2042 NARGS is the number of arguments, or -1 if not specified. */
2044 static void
2045 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2047 tree method_signature
2048 = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2049 tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool, method_ref_index);
2050 tree self_type
2051 = get_class_constant (current_jcf,
2052 COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2053 method_ref_index));
2054 const char *const self_name
2055 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2056 tree call, func, method, arg_list, method_type;
2057 tree check = NULL_TREE;
2059 if (! CLASS_LOADED_P (self_type))
2061 load_class (self_type, 1);
2062 safe_layout_class (self_type);
2063 if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2064 fatal_error ("failed to find class '%s'", self_name);
2066 layout_class_methods (self_type);
2068 if (ID_INIT_P (method_name))
2069 method = lookup_java_constructor (self_type, method_signature);
2070 else
2071 method = lookup_java_method (self_type, method_name, method_signature);
2072 if (method == NULL_TREE)
2074 error ("class '%s' has no method named '%s' matching signature '%s'",
2075 self_name,
2076 IDENTIFIER_POINTER (method_name),
2077 IDENTIFIER_POINTER (method_signature));
2079 /* Invoke static can't invoke static/abstract method */
2080 else if (opcode == OPCODE_invokestatic)
2082 if (!METHOD_STATIC (method))
2084 error ("invokestatic on non static method");
2085 method = NULL_TREE;
2087 else if (METHOD_ABSTRACT (method))
2089 error ("invokestatic on abstract method");
2090 method = NULL_TREE;
2093 else
2095 if (METHOD_STATIC (method))
2097 error ("invoke[non-static] on static method");
2098 method = NULL_TREE;
2102 if (method == NULL_TREE)
2104 method_type = get_type_from_signature (method_signature);
2105 pop_arguments (TYPE_ARG_TYPES (method_type));
2106 if (opcode != OPCODE_invokestatic)
2107 pop_type (self_type);
2108 method_type = promote_type (TREE_TYPE (method_type));
2109 push_value (convert (method_type, integer_zero_node));
2110 return;
2113 method_type = TREE_TYPE (method);
2114 arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2115 flush_quick_stack ();
2117 func = NULL_TREE;
2118 if (opcode == OPCODE_invokestatic)
2119 func = build_known_method_ref (method, method_type, self_type,
2120 method_signature, arg_list);
2121 else if (opcode == OPCODE_invokespecial
2122 || (opcode == OPCODE_invokevirtual
2123 && (METHOD_PRIVATE (method)
2124 || METHOD_FINAL (method)
2125 || CLASS_FINAL (TYPE_NAME (self_type)))))
2127 /* If the object for the method call is null, we throw an
2128 exception. We don't do this if the object is the current
2129 method's `this'. In other cases we just rely on an
2130 optimization pass to eliminate redundant checks. FIXME:
2131 Unfortunately there doesn't seem to be a way to determine
2132 what the current method is right now.
2133 We do omit the check if we're calling <init>. */
2134 /* We use a SAVE_EXPR here to make sure we only evaluate
2135 the new `self' expression once. */
2136 tree save_arg = save_expr (TREE_VALUE (arg_list));
2137 TREE_VALUE (arg_list) = save_arg;
2138 check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2139 func = build_known_method_ref (method, method_type, self_type,
2140 method_signature, arg_list);
2142 else
2144 tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
2145 arg_list);
2146 if (opcode == OPCODE_invokevirtual)
2147 func = build_invokevirtual (dtable, method);
2148 else
2149 func = build_invokeinterface (dtable, method);
2152 if (TREE_CODE (func) == ADDR_EXPR)
2153 TREE_TYPE (func) = build_pointer_type (method_type);
2154 else
2155 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2157 call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2158 func, arg_list, NULL_TREE);
2159 TREE_SIDE_EFFECTS (call) = 1;
2160 call = check_for_builtin (method, call);
2162 if (check != NULL_TREE)
2164 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2165 TREE_SIDE_EFFECTS (call) = 1;
2168 if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2169 java_add_stmt (call);
2170 else
2172 push_value (call);
2173 flush_quick_stack ();
2177 /* Create a stub which will be put into the vtable but which will call
2178 a JNI function. */
2180 tree
2181 build_jni_stub (tree method)
2183 tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2184 tree jni_func_type, tem;
2185 tree env_var, res_var = NULL_TREE, block;
2186 tree method_args, res_type;
2187 tree meth_var;
2188 tree bind;
2190 int args_size = 0;
2192 tree klass = DECL_CONTEXT (method);
2193 int from_class = ! CLASS_FROM_SOURCE_P (klass);
2194 klass = build_class_ref (klass);
2196 if (! METHOD_NATIVE (method) || ! flag_jni)
2197 abort ();
2199 DECL_ARTIFICIAL (method) = 1;
2200 DECL_EXTERNAL (method) = 0;
2202 env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2203 DECL_CONTEXT (env_var) = method;
2205 if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2207 res_var = build_decl (VAR_DECL, get_identifier ("res"),
2208 TREE_TYPE (TREE_TYPE (method)));
2209 DECL_CONTEXT (res_var) = method;
2210 TREE_CHAIN (env_var) = res_var;
2213 meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2214 TREE_STATIC (meth_var) = 1;
2215 TREE_PUBLIC (meth_var) = 0;
2216 DECL_EXTERNAL (meth_var) = 0;
2217 DECL_CONTEXT (meth_var) = method;
2218 DECL_ARTIFICIAL (meth_var) = 1;
2219 DECL_INITIAL (meth_var) = null_pointer_node;
2220 TREE_USED (meth_var) = 1;
2221 chainon (env_var, meth_var);
2222 build_result_decl (method);
2224 /* One strange way that the front ends are different is that they
2225 store arguments differently. */
2226 if (from_class)
2227 method_args = DECL_ARGUMENTS (method);
2228 else
2229 method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2230 block = build_block (env_var, NULL_TREE, NULL_TREE,
2231 method_args, NULL_TREE);
2232 TREE_SIDE_EFFECTS (block) = 1;
2233 /* When compiling from source we don't set the type of the block,
2234 because that will prevent patch_return from ever being run. */
2235 if (from_class)
2236 TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2238 /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
2239 body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2240 build3 (CALL_EXPR, ptr_type_node,
2241 build_address_of (soft_getjnienvnewframe_node),
2242 build_tree_list (NULL_TREE, klass),
2243 NULL_TREE));
2244 CAN_COMPLETE_NORMALLY (body) = 1;
2246 /* All the arguments to this method become arguments to the
2247 underlying JNI function. If we had to wrap object arguments in a
2248 special way, we would do that here. */
2249 args = NULL_TREE;
2250 for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2252 int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (tem)));
2253 #ifdef PARM_BOUNDARY
2254 arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2255 * PARM_BOUNDARY);
2256 #endif
2257 args_size += (arg_bits / BITS_PER_UNIT);
2259 args = tree_cons (NULL_TREE, tem, args);
2261 args = nreverse (args);
2262 arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2264 /* For a static method the second argument is the class. For a
2265 non-static method the second argument is `this'; that is already
2266 available in the argument list. */
2267 if (METHOD_STATIC (method))
2269 args_size += int_size_in_bytes (TREE_TYPE (klass));
2270 args = tree_cons (NULL_TREE, klass, args);
2271 arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2274 /* The JNIEnv structure is the first argument to the JNI function. */
2275 args_size += int_size_in_bytes (TREE_TYPE (env_var));
2276 args = tree_cons (NULL_TREE, env_var, args);
2277 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2279 /* We call _Jv_LookupJNIMethod to find the actual underlying
2280 function pointer. _Jv_LookupJNIMethod will throw the appropriate
2281 exception if this function is not found at runtime. */
2282 tem = build_tree_list (NULL_TREE, build_int_2 (args_size, 0));
2283 method_sig = build_java_signature (TREE_TYPE (method));
2284 lookup_arg = tree_cons (NULL_TREE,
2285 build_utf8_ref (unmangle_classname
2286 (IDENTIFIER_POINTER (method_sig),
2287 IDENTIFIER_LENGTH (method_sig))),
2288 tem);
2289 tem = DECL_NAME (method);
2290 lookup_arg
2291 = tree_cons (NULL_TREE, klass,
2292 tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2294 tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2296 #ifdef MODIFY_JNI_METHOD_CALL
2297 tem = MODIFY_JNI_METHOD_CALL (tem);
2298 #endif
2300 jni_func_type = build_pointer_type (tem);
2302 jnifunc = build3 (COND_EXPR, ptr_type_node,
2303 meth_var, meth_var,
2304 build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2305 build3 (CALL_EXPR, ptr_type_node,
2306 build_address_of
2307 (soft_lookupjnimethod_node),
2308 lookup_arg, NULL_TREE)));
2310 /* Now we make the actual JNI call via the resulting function
2311 pointer. */
2312 call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2313 build1 (NOP_EXPR, jni_func_type, jnifunc),
2314 args, NULL_TREE);
2316 /* If the JNI call returned a result, capture it here. If we had to
2317 unwrap JNI object results, we would do that here. */
2318 if (res_var != NULL_TREE)
2319 call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2320 res_var, call);
2322 TREE_SIDE_EFFECTS (call) = 1;
2323 CAN_COMPLETE_NORMALLY (call) = 1;
2325 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2326 TREE_SIDE_EFFECTS (body) = 1;
2328 /* Now free the environment we allocated. */
2329 call = build3 (CALL_EXPR, ptr_type_node,
2330 build_address_of (soft_jnipopsystemframe_node),
2331 build_tree_list (NULL_TREE, env_var),
2332 NULL_TREE);
2333 TREE_SIDE_EFFECTS (call) = 1;
2334 CAN_COMPLETE_NORMALLY (call) = 1;
2335 body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2336 TREE_SIDE_EFFECTS (body) = 1;
2338 /* Finally, do the return. */
2339 res_type = void_type_node;
2340 if (res_var != NULL_TREE)
2342 tree drt;
2343 if (! DECL_RESULT (method))
2344 abort ();
2345 /* Make sure we copy the result variable to the actual
2346 result. We use the type of the DECL_RESULT because it
2347 might be different from the return type of the function:
2348 it might be promoted. */
2349 drt = TREE_TYPE (DECL_RESULT (method));
2350 if (drt != TREE_TYPE (res_var))
2351 res_var = build1 (CONVERT_EXPR, drt, res_var);
2352 res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2353 TREE_SIDE_EFFECTS (res_var) = 1;
2356 body = build2 (COMPOUND_EXPR, void_type_node, body,
2357 build1 (RETURN_EXPR, res_type, res_var));
2358 TREE_SIDE_EFFECTS (body) = 1;
2360 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
2361 body, block);
2362 return bind;
2365 /* Expand an operation to extract from or store into a field.
2366 IS_STATIC is 1 iff the field is static.
2367 IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
2368 FIELD_REF_INDEX is an index into the constant pool. */
2370 static void
2371 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2373 tree self_type
2374 = get_class_constant (current_jcf,
2375 COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2376 field_ref_index));
2377 const char *self_name
2378 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2379 tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2380 tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
2381 field_ref_index);
2382 tree field_type = get_type_from_signature (field_signature);
2383 tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2384 tree field_ref;
2385 int is_error = 0;
2386 tree field_decl;
2388 if (! CLASS_LOADED_P (self_type))
2389 load_class (self_type, 1);
2390 field_decl = lookup_field (&self_type, field_name);
2391 if (field_decl == error_mark_node)
2393 is_error = 1;
2395 else if (field_decl == NULL_TREE)
2397 error ("missing field '%s' in '%s'",
2398 IDENTIFIER_POINTER (field_name), self_name);
2399 is_error = 1;
2401 else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2403 error ("mismatching signature for field '%s' in '%s'",
2404 IDENTIFIER_POINTER (field_name), self_name);
2405 is_error = 1;
2407 field_ref = is_static ? NULL_TREE : pop_value (self_type);
2408 if (is_error)
2410 if (! is_putting)
2411 push_value (convert (field_type, integer_zero_node));
2412 flush_quick_stack ();
2413 return;
2416 field_ref = build_field_ref (field_ref, self_type, field_name);
2417 if (is_static)
2418 field_ref = build_class_init (self_type, field_ref);
2419 if (is_putting)
2421 flush_quick_stack ();
2422 if (FIELD_FINAL (field_decl))
2424 if (DECL_CONTEXT (field_decl) != current_class)
2425 error ("%Jassignment to final field '%D' not in field's class",
2426 field_decl, field_decl);
2427 else if (FIELD_STATIC (field_decl))
2429 if (!DECL_CLINIT_P (current_function_decl))
2430 warning ("%Jassignment to final static field `%D' not in "
2431 "class initializer",
2432 field_decl, field_decl);
2434 else
2436 tree cfndecl_name = DECL_NAME (current_function_decl);
2437 if (! DECL_CONSTRUCTOR_P (current_function_decl)
2438 && !ID_FINIT_P (cfndecl_name))
2439 warning ("%Jassignment to final field '%D' not in constructor",
2440 field_decl, field_decl);
2443 java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2444 field_ref, new_value));
2446 else
2447 push_value (field_ref);
2450 void
2451 load_type_state (tree label)
2453 int i;
2454 tree vec = LABEL_TYPE_STATE (label);
2455 int cur_length = TREE_VEC_LENGTH (vec);
2456 stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2457 for (i = 0; i < cur_length; i++)
2458 type_map [i] = TREE_VEC_ELT (vec, i);
2461 /* Go over METHOD's bytecode and note instruction starts in
2462 instruction_bits[]. */
2464 void
2465 note_instructions (JCF *jcf, tree method)
2467 int PC;
2468 unsigned char* byte_ops;
2469 long length = DECL_CODE_LENGTH (method);
2471 int saw_index;
2472 jint INT_temp;
2474 #undef RET /* Defined by config/i386/i386.h */
2475 #undef PTR
2476 #define BCODE byte_ops
2477 #define BYTE_type_node byte_type_node
2478 #define SHORT_type_node short_type_node
2479 #define INT_type_node int_type_node
2480 #define LONG_type_node long_type_node
2481 #define CHAR_type_node char_type_node
2482 #define PTR_type_node ptr_type_node
2483 #define FLOAT_type_node float_type_node
2484 #define DOUBLE_type_node double_type_node
2485 #define VOID_type_node void_type_node
2486 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2487 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2488 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2489 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2491 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2493 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2494 byte_ops = jcf->read_ptr;
2495 instruction_bits = xrealloc (instruction_bits, length + 1);
2496 memset (instruction_bits, 0, length + 1);
2498 /* This pass figures out which PC can be the targets of jumps. */
2499 for (PC = 0; PC < length;)
2501 int oldpc = PC; /* PC at instruction start. */
2502 instruction_bits [PC] |= BCODE_INSTRUCTION_START;
2503 switch (byte_ops[PC++])
2505 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2506 case OPCODE: \
2507 PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2508 break;
2510 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2512 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2513 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2514 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2515 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2516 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2517 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2518 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2519 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2521 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2522 PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2523 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2524 ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2525 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2526 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2527 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2528 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2530 /* two forms of wide instructions */
2531 #define PRE_SPECIAL_WIDE(IGNORE) \
2533 int modified_opcode = IMMEDIATE_u1; \
2534 if (modified_opcode == OPCODE_iinc) \
2536 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2537 (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
2539 else \
2541 (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
2545 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2547 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2549 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2550 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2551 PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2552 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2553 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2554 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2555 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2556 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2557 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2558 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2560 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2561 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2562 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2563 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2564 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2565 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2566 saw_index = 0; INT_temp = (OPERAND_VALUE); \
2567 NOTE_LABEL (PC); \
2568 if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
2570 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
2572 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2573 PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2575 #define PRE_LOOKUP_SWITCH \
2576 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2577 NOTE_LABEL (default_offset+oldpc); \
2578 if (npairs >= 0) \
2579 while (--npairs >= 0) { \
2580 jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
2581 jint offset = IMMEDIATE_s4; \
2582 NOTE_LABEL (offset+oldpc); } \
2585 #define PRE_TABLE_SWITCH \
2586 { jint default_offset = IMMEDIATE_s4; \
2587 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2588 NOTE_LABEL (default_offset+oldpc); \
2589 if (low <= high) \
2590 while (low++ <= high) { \
2591 jint offset = IMMEDIATE_s4; \
2592 NOTE_LABEL (offset+oldpc); } \
2595 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2596 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2597 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2598 (void)(IMMEDIATE_u2); \
2599 PC += 2 * IS_INTERFACE /* for invokeinterface */;
2601 #include "javaop.def"
2602 #undef JAVAOP
2604 } /* for */
2607 void
2608 expand_byte_code (JCF *jcf, tree method)
2610 int PC;
2611 int i;
2612 const unsigned char *linenumber_pointer;
2613 int dead_code_index = -1;
2614 unsigned char* byte_ops;
2615 long length = DECL_CODE_LENGTH (method);
2617 stack_pointer = 0;
2618 JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2619 byte_ops = jcf->read_ptr;
2621 /* We make an initial pass of the line number table, to note
2622 which instructions have associated line number entries. */
2623 linenumber_pointer = linenumber_table;
2624 for (i = 0; i < linenumber_count; i++)
2626 int pc = GET_u2 (linenumber_pointer);
2627 linenumber_pointer += 4;
2628 if (pc >= length)
2629 warning ("invalid PC in line number table");
2630 else
2632 if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2633 instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2634 instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2638 if (! verify_jvm_instructions (jcf, byte_ops, length))
2639 return;
2641 /* Translate bytecodes. */
2642 linenumber_pointer = linenumber_table;
2643 for (PC = 0; PC < length;)
2645 if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2647 tree label = lookup_label (PC);
2648 flush_quick_stack ();
2649 if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2650 java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2651 if (LABEL_VERIFIED (label) || PC == 0)
2652 load_type_state (label);
2655 if (! (instruction_bits [PC] & BCODE_VERIFIED))
2657 if (dead_code_index == -1)
2659 /* This is the start of a region of unreachable bytecodes.
2660 They still need to be processed in order for EH ranges
2661 to get handled correctly. However, we can simply
2662 replace these bytecodes with nops. */
2663 dead_code_index = PC;
2666 /* Turn this bytecode into a nop. */
2667 byte_ops[PC] = 0x0;
2669 else
2671 if (dead_code_index != -1)
2673 /* We've just reached the end of a region of dead code. */
2674 if (extra_warnings)
2675 warning ("unreachable bytecode from %d to before %d",
2676 dead_code_index, PC);
2677 dead_code_index = -1;
2681 /* Handle possible line number entry for this PC.
2683 This code handles out-of-order and multiple linenumbers per PC,
2684 but is optimized for the case of line numbers increasing
2685 monotonically with PC. */
2686 if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2688 if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2689 || GET_u2 (linenumber_pointer) != PC)
2690 linenumber_pointer = linenumber_table;
2691 while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2693 int pc = GET_u2 (linenumber_pointer);
2694 linenumber_pointer += 4;
2695 if (pc == PC)
2697 input_location.line = GET_u2 (linenumber_pointer - 2);
2698 if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2699 break;
2703 maybe_pushlevels (PC);
2704 PC = process_jvm_instruction (PC, byte_ops, length);
2705 maybe_poplevels (PC);
2706 } /* for */
2708 if (dead_code_index != -1)
2710 /* We've just reached the end of a region of dead code. */
2711 if (extra_warnings)
2712 warning ("unreachable bytecode from %d to the end of the method",
2713 dead_code_index);
2717 static void
2718 java_push_constant_from_pool (JCF *jcf, int index)
2720 tree c;
2721 if (JPOOL_TAG (jcf, index) == CONSTANT_String)
2723 tree name;
2724 name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
2725 index = alloc_name_constant (CONSTANT_String, name);
2726 c = build_ref_from_constant_pool (index);
2727 c = convert (promote_type (string_type_node), c);
2729 else
2730 c = get_constant (jcf, index);
2731 push_value (c);
2735 process_jvm_instruction (int PC, const unsigned char* byte_ops,
2736 long length ATTRIBUTE_UNUSED)
2738 const char *opname; /* Temporary ??? */
2739 int oldpc = PC; /* PC at instruction start. */
2741 /* If the instruction is at the beginning of a exception handler,
2742 replace the top of the stack with the thrown object reference */
2743 if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
2745 tree type = pop_type (ptr_type_node);
2746 push_value (build_exception_object_ref (type));
2749 switch (byte_ops[PC++])
2751 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2752 case OPCODE: \
2753 opname = #OPNAME; \
2754 OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2755 break;
2757 #define RET(OPERAND_TYPE, OPERAND_VALUE) \
2759 int saw_index = 0; \
2760 int index = OPERAND_VALUE; \
2761 build_java_ret (find_local_variable (index, ptr_type_node, oldpc)); \
2764 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
2766 /* OPERAND_VALUE may have side-effects on PC */ \
2767 int opvalue = OPERAND_VALUE; \
2768 build_java_jsr (oldpc + opvalue, PC); \
2771 /* Push a constant onto the stack. */
2772 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
2773 { int saw_index = 0; int ival = (OPERAND_VALUE); \
2774 if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
2775 else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
2777 /* internal macro added for use by the WIDE case */
2778 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
2779 expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
2781 /* Push local variable onto the opcode stack. */
2782 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
2784 /* have to do this since OPERAND_VALUE may have side-effects */ \
2785 int opvalue = OPERAND_VALUE; \
2786 LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2789 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
2790 expand_java_return (OPERAND_TYPE##_type_node)
2792 #define REM_EXPR TRUNC_MOD_EXPR
2793 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
2794 expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
2796 #define FIELD(IS_STATIC, IS_PUT) \
2797 expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
2799 #define TEST(OPERAND_TYPE, CONDITION) \
2800 expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2802 #define COND(OPERAND_TYPE, CONDITION) \
2803 expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
2805 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2806 BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
2808 #define BRANCH_GOTO(OPERAND_VALUE) \
2809 expand_java_goto (oldpc + OPERAND_VALUE)
2811 #define BRANCH_CALL(OPERAND_VALUE) \
2812 expand_java_call (oldpc + OPERAND_VALUE, oldpc)
2814 #if 0
2815 #define BRANCH_RETURN(OPERAND_VALUE) \
2817 tree type = OPERAND_TYPE##_type_node; \
2818 tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
2819 expand_java_ret (value); \
2821 #endif
2823 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
2824 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2825 fprintf (stderr, "(not implemented)\n")
2826 #define NOT_IMPL1(OPERAND_VALUE) \
2827 fprintf (stderr, "%3d: %s ", oldpc, opname); \
2828 fprintf (stderr, "(not implemented)\n")
2830 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
2832 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
2834 #define STACK_POP(COUNT) java_stack_pop (COUNT)
2836 #define STACK_SWAP(COUNT) java_stack_swap()
2838 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
2839 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
2840 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
2842 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2843 PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
2845 #define LOOKUP_SWITCH \
2846 { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
2847 tree selector = pop_value (INT_type_node); \
2848 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
2849 while (--npairs >= 0) \
2851 jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
2852 expand_java_add_case (switch_expr, match, oldpc + offset); \
2856 #define TABLE_SWITCH \
2857 { jint default_offset = IMMEDIATE_s4; \
2858 jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
2859 tree selector = pop_value (INT_type_node); \
2860 tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
2861 for (; low <= high; low++) \
2863 jint offset = IMMEDIATE_s4; \
2864 expand_java_add_case (switch_expr, low, oldpc + offset); \
2868 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2869 { int opcode = byte_ops[PC-1]; \
2870 int method_ref_index = IMMEDIATE_u2; \
2871 int nargs; \
2872 if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
2873 else nargs = -1; \
2874 expand_invoke (opcode, method_ref_index, nargs); \
2877 /* Handle new, checkcast, instanceof */
2878 #define OBJECT(TYPE, OP) \
2879 expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
2881 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
2883 #define ARRAY_LOAD(OPERAND_TYPE) \
2885 expand_java_arrayload( OPERAND_TYPE##_type_node ); \
2888 #define ARRAY_STORE(OPERAND_TYPE) \
2890 expand_java_arraystore( OPERAND_TYPE##_type_node ); \
2893 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
2894 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
2895 #define ARRAY_NEW_PTR() \
2896 push_value (build_anewarray (get_class_constant (current_jcf, \
2897 IMMEDIATE_u2), \
2898 pop_value (int_type_node)));
2899 #define ARRAY_NEW_NUM() \
2901 int atype = IMMEDIATE_u1; \
2902 push_value (build_newarray (atype, pop_value (int_type_node)));\
2904 #define ARRAY_NEW_MULTI() \
2906 tree class = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
2907 int ndims = IMMEDIATE_u1; \
2908 expand_java_multianewarray( class, ndims ); \
2911 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
2912 push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
2913 pop_value (OPERAND_TYPE##_type_node))));
2915 #define CONVERT2(FROM_TYPE, TO_TYPE) \
2917 push_value (build1 (NOP_EXPR, int_type_node, \
2918 (convert (TO_TYPE##_type_node, \
2919 pop_value (FROM_TYPE##_type_node))))); \
2922 #define CONVERT(FROM_TYPE, TO_TYPE) \
2924 push_value (convert (TO_TYPE##_type_node, \
2925 pop_value (FROM_TYPE##_type_node))); \
2928 /* internal macro added for use by the WIDE case
2929 Added TREE_TYPE (decl) assignment, apbianco */
2930 #define STORE_INTERNAL(OPTYPE, OPVALUE) \
2932 tree decl, value; \
2933 int index = OPVALUE; \
2934 tree type = OPTYPE; \
2935 value = pop_value (type); \
2936 type = TREE_TYPE (value); \
2937 decl = find_local_variable (index, type, oldpc); \
2938 set_local_type (index, type); \
2939 java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
2940 update_aliases (decl, index); \
2943 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
2945 /* have to do this since OPERAND_VALUE may have side-effects */ \
2946 int opvalue = OPERAND_VALUE; \
2947 STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
2950 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2951 SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2953 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
2954 #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
2956 #define MONITOR_OPERATION(call) \
2958 tree o = pop_value (ptr_type_node); \
2959 tree c; \
2960 flush_quick_stack (); \
2961 c = build_java_monitor (call, o); \
2962 TREE_SIDE_EFFECTS (c) = 1; \
2963 java_add_stmt (c); \
2966 #define SPECIAL_IINC(IGNORED) \
2968 unsigned int local_var_index = IMMEDIATE_u1; \
2969 int ival = IMMEDIATE_s1; \
2970 expand_iinc(local_var_index, ival, oldpc); \
2973 #define SPECIAL_WIDE(IGNORED) \
2975 int modified_opcode = IMMEDIATE_u1; \
2976 unsigned int local_var_index = IMMEDIATE_u2; \
2977 switch (modified_opcode) \
2979 case OPCODE_iinc: \
2981 int ival = IMMEDIATE_s2; \
2982 expand_iinc (local_var_index, ival, oldpc); \
2983 break; \
2985 case OPCODE_iload: \
2986 case OPCODE_lload: \
2987 case OPCODE_fload: \
2988 case OPCODE_dload: \
2989 case OPCODE_aload: \
2991 /* duplicate code from LOAD macro */ \
2992 LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
2993 break; \
2995 case OPCODE_istore: \
2996 case OPCODE_lstore: \
2997 case OPCODE_fstore: \
2998 case OPCODE_dstore: \
2999 case OPCODE_astore: \
3001 STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3002 break; \
3004 default: \
3005 error ("unrecogized wide sub-instruction"); \
3009 #define SPECIAL_THROW(IGNORED) \
3010 build_java_athrow (pop_value (throwable_type_node))
3012 #define SPECIAL_BREAK NOT_IMPL1
3013 #define IMPL NOT_IMPL
3015 #include "javaop.def"
3016 #undef JAVAOP
3017 default:
3018 fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3020 return PC;
3023 /* Return the opcode at PC in the code section pointed to by
3024 CODE_OFFSET. */
3026 static unsigned char
3027 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3029 unsigned char opcode;
3030 long absolute_offset = (long)JCF_TELL (jcf);
3032 JCF_SEEK (jcf, code_offset);
3033 opcode = jcf->read_ptr [pc];
3034 JCF_SEEK (jcf, absolute_offset);
3035 return opcode;
3038 /* Some bytecode compilers are emitting accurate LocalVariableTable
3039 attributes. Here's an example:
3041 PC <t>store_<n>
3042 PC+1 ...
3044 Attribute "LocalVariableTable"
3045 slot #<n>: ... (PC: PC+1 length: L)
3047 This is accurate because the local in slot <n> really exists after
3048 the opcode at PC is executed, hence from PC+1 to PC+1+L.
3050 This procedure recognizes this situation and extends the live range
3051 of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3052 length of the store instruction.)
3054 This function is used by `give_name_to_locals' so that a local's
3055 DECL features a DECL_LOCAL_START_PC such that the first related
3056 store operation will use DECL as a destination, not a unrelated
3057 temporary created for the occasion.
3059 This function uses a global (instruction_bits) `note_instructions' should
3060 have allocated and filled properly. */
3063 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3064 int start_pc, int slot)
3066 int first, index, opcode;
3067 int pc, insn_pc;
3068 int wide_found = 0;
3070 if (!start_pc)
3071 return start_pc;
3073 first = index = -1;
3075 /* Find last previous instruction and remember it */
3076 for (pc = start_pc-1; pc; pc--)
3077 if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3078 break;
3079 insn_pc = pc;
3081 /* Retrieve the instruction, handle `wide'. */
3082 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3083 if (opcode == OPCODE_wide)
3085 wide_found = 1;
3086 opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3089 switch (opcode)
3091 case OPCODE_astore_0:
3092 case OPCODE_astore_1:
3093 case OPCODE_astore_2:
3094 case OPCODE_astore_3:
3095 first = OPCODE_astore_0;
3096 break;
3098 case OPCODE_istore_0:
3099 case OPCODE_istore_1:
3100 case OPCODE_istore_2:
3101 case OPCODE_istore_3:
3102 first = OPCODE_istore_0;
3103 break;
3105 case OPCODE_lstore_0:
3106 case OPCODE_lstore_1:
3107 case OPCODE_lstore_2:
3108 case OPCODE_lstore_3:
3109 first = OPCODE_lstore_0;
3110 break;
3112 case OPCODE_fstore_0:
3113 case OPCODE_fstore_1:
3114 case OPCODE_fstore_2:
3115 case OPCODE_fstore_3:
3116 first = OPCODE_fstore_0;
3117 break;
3119 case OPCODE_dstore_0:
3120 case OPCODE_dstore_1:
3121 case OPCODE_dstore_2:
3122 case OPCODE_dstore_3:
3123 first = OPCODE_dstore_0;
3124 break;
3126 case OPCODE_astore:
3127 case OPCODE_istore:
3128 case OPCODE_lstore:
3129 case OPCODE_fstore:
3130 case OPCODE_dstore:
3131 index = peek_opcode_at_pc (jcf, code_offset, pc);
3132 if (wide_found)
3134 int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3135 index = (other << 8) + index;
3137 break;
3140 /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3141 means we have a <t>store. */
3142 if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3143 start_pc = insn_pc;
3145 return start_pc;
3148 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3149 order, as specified by Java Language Specification.
3151 The problem is that while expand_expr will evaluate its sub-operands in
3152 left-to-right order, for variables it will just return an rtx (i.e.
3153 an lvalue) for the variable (rather than an rvalue). So it is possible
3154 that a later sub-operand will change the register, and when the
3155 actual operation is done, it will use the new value, when it should
3156 have used the original value.
3158 We fix this by using save_expr. This forces the sub-operand to be
3159 copied into a fresh virtual register,
3161 For method invocation, we modify the arguments so that a
3162 left-to-right order evaluation is performed. Saved expressions
3163 will, in CALL_EXPR order, be reused when the call will be expanded.
3166 tree
3167 force_evaluation_order (tree node)
3169 if (flag_syntax_only)
3170 return node;
3171 if (TREE_CODE (node) == CALL_EXPR
3172 || TREE_CODE (node) == NEW_CLASS_EXPR
3173 || (TREE_CODE (node) == COMPOUND_EXPR
3174 && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3175 && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR))
3177 tree arg, cmp;
3179 if (!TREE_OPERAND (node, 1))
3180 return node;
3182 arg = node;
3184 /* Position arg properly, account for wrapped around ctors. */
3185 if (TREE_CODE (node) == COMPOUND_EXPR)
3186 arg = TREE_OPERAND (node, 0);
3188 arg = TREE_OPERAND (arg, 1);
3190 /* Not having a list of argument here is an error. */
3191 if (TREE_CODE (arg) != TREE_LIST)
3192 abort ();
3194 /* This reverses the evaluation order. This is a desired effect. */
3195 for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3197 tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3198 cmp = (cmp == NULL_TREE ? saved :
3199 build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3200 TREE_VALUE (arg) = saved;
3203 if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3204 TREE_SIDE_EFFECTS (cmp) = 1;
3206 if (cmp)
3208 cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3209 if (TREE_TYPE (cmp) != void_type_node)
3210 cmp = save_expr (cmp);
3211 CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3212 TREE_SIDE_EFFECTS (cmp) = 1;
3213 node = cmp;
3216 return node;
3219 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3220 location where an expression or an identifier were encountered. It
3221 is necessary for languages where the frontend parser will handle
3222 recursively more than one file (Java is one of them). */
3224 tree
3225 build_expr_wfl (tree node, const char *file, int line, int col)
3227 static const char *last_file = 0;
3228 static tree last_filenode = NULL_TREE;
3229 tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
3231 EXPR_WFL_NODE (wfl) = node;
3232 EXPR_WFL_SET_LINECOL (wfl, line, col);
3233 if (file != last_file)
3235 last_file = file;
3236 last_filenode = file ? get_identifier (file) : NULL_TREE;
3239 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3240 if (node)
3242 if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3243 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3244 TREE_TYPE (wfl) = TREE_TYPE (node);
3247 return wfl;
3251 /* Build a node to represent empty statements and blocks. */
3253 tree
3254 build_java_empty_stmt (void)
3256 tree t = build_empty_stmt ();
3257 CAN_COMPLETE_NORMALLY (t) = 1;
3258 return t;
3261 #include "gt-java-expr.h"