OSDN Git Service

* java-gimplify.c: Use gcc_assert and gcc_unreachable throughout.
[pf3gnuchains/gcc-fork.git] / gcc / java / expr.c
1 /* Process expressions for the GNU compiler for the Java(TM) language.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  
21
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.  */
25
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
27
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"
48
49 static void flush_quick_stack (void);
50 static void push_value (tree);
51 static tree pop_value (tree);
52 static void java_stack_swap (void);
53 static void java_stack_dup (int, int);
54 static void build_java_athrow (tree);
55 static void build_java_jsr (int, int);
56 static void build_java_ret (tree);
57 static void expand_java_multianewarray (tree, int);
58 static void expand_java_arraystore (tree);
59 static void expand_java_arrayload (tree);
60 static void expand_java_array_length (void);
61 static tree build_java_monitor (tree, tree);
62 static void expand_java_pushc (int, tree);
63 static void expand_java_return (tree);
64 static void expand_load_internal (int, tree, int);
65 static void expand_java_NEW (tree);
66 static void expand_java_INSTANCEOF (tree);
67 static void expand_java_CHECKCAST (tree);
68 static void expand_iinc (unsigned int, int, int);
69 static void expand_java_binop (tree, enum tree_code);
70 static void note_label (int, int);
71 static void expand_compare (enum tree_code, tree, tree, int);
72 static void expand_test (enum tree_code, tree, int);
73 static void expand_cond (enum tree_code, tree, int);
74 static void expand_java_goto (int);
75 static tree expand_java_switch (tree, int);
76 static void expand_java_add_case (tree, int, int);
77 #if 0
78 static void expand_java_call (int, int);
79 static void expand_java_ret (tree); 
80 #endif
81 static tree pop_arguments (tree); 
82 static void expand_invoke (int, int, int); 
83 static void expand_java_field_op (int, int, int); 
84 static void java_push_constant_from_pool (struct JCF *, int); 
85 static void java_stack_pop (int); 
86 static tree build_java_throw_out_of_bounds_exception (tree); 
87 static tree build_java_check_indexed_type (tree, tree); 
88 static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
89 static void promote_arguments (void);
90
91 static GTY(()) tree operand_type[59];
92
93 static GTY(()) tree methods_ident;
94 static GTY(()) tree ncode_ident;
95 tree dtable_ident = NULL_TREE;
96
97 /* Set to nonzero value in order to emit class initialization code
98    before static field references.  */
99 int always_initialize_class_p = 0;
100
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.
110
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.
126 */
127
128 static GTY(()) tree quick_stack;
129
130 /* A free-list of unused permanent TREE_LIST nodes.  */
131 static GTY((deletable)) tree tree_list_free_list;
132
133 /* The stack pointer of the Java virtual machine.
134    This does include the size of the quick_stack. */
135
136 int stack_pointer;
137
138 const unsigned char *linenumber_table;
139 int linenumber_count;
140
141 /* Largest pc so far in this method that has been passed to lookup_label. */
142 int highest_label_pc_this_method = -1;
143
144 /* Base value for this method to add to pc to get generated label. */
145 int start_label_pc_this_method = 0;
146
147 void
148 init_expr_processing (void)
149 {
150   operand_type[21] = operand_type[54] = int_type_node;
151   operand_type[22] = operand_type[55] = long_type_node;
152   operand_type[23] = operand_type[56] = float_type_node;
153   operand_type[24] = operand_type[57] = double_type_node;
154   operand_type[25] = operand_type[58] = ptr_type_node;
155 }
156
157 tree
158 java_truthvalue_conversion (tree expr)
159 {
160   /* It is simpler and generates better code to have only TRUTH_*_EXPR
161      or comparison expressions as truth values at this level.
162
163      This function should normally be identity for Java.  */
164
165   switch (TREE_CODE (expr))
166     {
167     case EQ_EXPR:   case NE_EXPR:   case UNEQ_EXPR: case LTGT_EXPR:
168     case LE_EXPR:   case GE_EXPR:   case LT_EXPR:   case GT_EXPR:
169     case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
170     case ORDERED_EXPR: case UNORDERED_EXPR:
171     case TRUTH_ANDIF_EXPR:
172     case TRUTH_ORIF_EXPR:
173     case TRUTH_AND_EXPR:
174     case TRUTH_OR_EXPR:
175     case TRUTH_XOR_EXPR:
176     case TRUTH_NOT_EXPR:
177     case ERROR_MARK:
178       return expr;
179
180     case INTEGER_CST:
181       return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
182
183     case REAL_CST:
184       return real_zerop (expr) ? boolean_false_node : boolean_true_node;
185
186     /* are these legal? XXX JH */
187     case NEGATE_EXPR:
188     case ABS_EXPR:
189     case FLOAT_EXPR:
190       /* These don't change whether an object is nonzero or zero.  */
191       return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
192
193     case COND_EXPR:
194       /* Distribute the conversion into the arms of a COND_EXPR.  */
195       return fold_build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
196                           java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
197                           java_truthvalue_conversion (TREE_OPERAND (expr, 2)));
198
199     case NOP_EXPR:
200       /* If this is widening the argument, we can ignore it.  */
201       if (TYPE_PRECISION (TREE_TYPE (expr))
202           >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
203         return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
204       /* fall through to default */
205
206     default:
207       return fold_build2 (NE_EXPR, boolean_type_node,
208                           expr, boolean_false_node);
209     }
210 }
211
212 /* Save any stack slots that happen to be in the quick_stack into their
213    home virtual register slots.
214
215    The copy order is from low stack index to high, to support the invariant
216    that the expression for a slot may contain decls for stack slots with
217    higher (or the same) index, but not lower. */
218
219 static void
220 flush_quick_stack (void)
221 {
222   int stack_index = stack_pointer;
223   tree prev, cur, next;
224
225   /* First reverse the quick_stack, and count the number of slots it has. */
226   for (cur = quick_stack, prev = NULL_TREE; cur != NULL_TREE; cur = next)
227     {
228       next = TREE_CHAIN (cur);
229       TREE_CHAIN (cur) = prev;
230       prev = cur;
231       stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (TREE_VALUE (cur)));
232     }
233   quick_stack = prev;
234
235   while (quick_stack != NULL_TREE)
236     {
237       tree decl;
238       tree node = quick_stack, type;
239       quick_stack = TREE_CHAIN (node);
240       TREE_CHAIN (node) = tree_list_free_list;
241       tree_list_free_list = node;
242       node = TREE_VALUE (node);
243       type = TREE_TYPE (node);
244
245       decl = find_stack_slot (stack_index, type);
246       if (decl != node)
247         java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (node), decl, node));
248       stack_index += 1 + TYPE_IS_WIDE (type);
249     }
250 }
251
252 /* Push TYPE on the type stack.
253    Return true on success, 0 on overflow. */
254
255 int
256 push_type_0 (tree type)
257 {
258   int n_words;
259   type = promote_type (type);
260   n_words = 1 + TYPE_IS_WIDE (type);
261   if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
262     return 0;
263   /* Allocate decl for this variable now, so we get a temporary that
264      survives the whole method. */
265   find_stack_slot (stack_pointer, type);
266   stack_type_map[stack_pointer++] = type;
267   n_words--;
268   while (--n_words >= 0)
269     stack_type_map[stack_pointer++] = TYPE_SECOND;
270   return 1;
271 }
272
273 void
274 push_type (tree type)
275 {
276   gcc_assert (push_type_0 (type));
277 }
278
279 static void
280 push_value (tree value)
281 {
282   tree type = TREE_TYPE (value);
283   if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
284     {
285       type = promote_type (type);
286       value = convert (type, value);
287     }
288   push_type (type);
289   if (tree_list_free_list == NULL_TREE)
290     quick_stack = tree_cons (NULL_TREE, value, quick_stack);
291   else
292     {
293       tree node = tree_list_free_list;
294       tree_list_free_list = TREE_CHAIN (tree_list_free_list);
295       TREE_VALUE (node) = value;
296       TREE_CHAIN (node) = quick_stack;
297       quick_stack = node;
298     }
299 }
300
301 /* Pop a type from the type stack.
302    TYPE is the expected type.   Return the actual type, which must be
303    convertible to TYPE.
304    On an error, *MESSAGEP is set to a freshly malloc'd error message. */
305
306 tree
307 pop_type_0 (tree type, char **messagep)
308 {
309   int n_words;
310   tree t;
311   *messagep = NULL;
312   if (TREE_CODE (type) == RECORD_TYPE)
313     type = promote_type (type);
314   n_words = 1 + TYPE_IS_WIDE (type);
315   if (stack_pointer < n_words)
316     {
317       *messagep = xstrdup ("stack underflow");
318       return type;
319     }
320   while (--n_words > 0)
321     {
322       if (stack_type_map[--stack_pointer] != void_type_node)
323         {
324           *messagep = xstrdup ("Invalid multi-word value on type stack");
325           return type;
326         }
327     }
328   t = stack_type_map[--stack_pointer];
329   if (type == NULL_TREE || t == type)
330     return t;
331   if (TREE_CODE (t) == TREE_LIST)
332     {      
333       do
334         {
335           tree tt = TREE_PURPOSE (t);
336           if (! can_widen_reference_to (tt, type))
337             {
338               t = tt;
339               goto fail;
340             }
341           t = TREE_CHAIN (t);
342         }
343       while (t);
344       return t;
345     }
346   if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
347       && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
348     return t;
349   if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
350     {
351       /* If the expected type we've been passed is object or ptr
352          (i.e. void*), the caller needs to know the real type.  */
353       if (type == ptr_type_node || type == object_ptr_type_node)
354         return t;
355
356       /* Since the verifier has already run, we know that any
357          types we see will be compatible.  In BC mode, this fact
358          may be checked at runtime, but if that is so then we can
359          assume its truth here as well.  So, we always succeed
360          here, with the expected type.  */
361       return type;
362     }
363
364   if (! flag_verify_invocations && flag_indirect_dispatch
365       && t == object_ptr_type_node)
366     {
367       if (type != ptr_type_node)
368         warning (0, "need to insert runtime check for %s", 
369                  xstrdup (lang_printable_name (type, 0)));
370       return type;
371     }
372
373   /* lang_printable_name uses a static buffer, so we must save the result
374      from calling it the first time.  */
375  fail:
376   {
377     char *temp = xstrdup (lang_printable_name (type, 0));
378     /* If the stack contains a multi-word type, keep popping the stack until 
379        the real type is found.  */
380     while (t == void_type_node)
381       t = stack_type_map[--stack_pointer];
382     *messagep = concat ("expected type '", temp,
383                         "' but stack contains '", lang_printable_name (t, 0),
384                         "'", NULL);
385     free (temp);
386   }
387   return type;
388 }
389
390 /* Pop a type from the type stack.
391    TYPE is the expected type.  Return the actual type, which must be
392    convertible to TYPE, otherwise call error. */
393
394 tree
395 pop_type (tree type)
396 {
397   char *message = NULL;
398   type = pop_type_0 (type, &message);
399   if (message != NULL)
400     {
401       error ("%s", message);
402       free (message);
403     }
404   return type;
405 }
406
407 \f
408 /* Return true if two type assertions are equal.  */
409
410 static int
411 type_assertion_eq (const void * k1_p, const void * k2_p)
412 {
413   type_assertion k1 = *(type_assertion *)k1_p;
414   type_assertion k2 = *(type_assertion *)k2_p;
415   return (k1.assertion_code == k2.assertion_code
416           && k1.op1 == k2.op1
417           && k1.op2 == k2.op2);
418 }
419
420 /* Hash a type assertion.  */
421
422 static hashval_t
423 type_assertion_hash (const void *p)
424 {
425   const type_assertion *k_p = p;
426   hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
427                                    k_p->assertion_code, 0);
428   hash = iterative_hash (&k_p->op1, sizeof k_p->op1, hash);
429   return iterative_hash (&k_p->op2, sizeof k_p->op2, hash);
430 }
431
432 /* Add an entry to the type assertion table for the given class.  
433    CLASS is the class for which this assertion will be evaluated by the 
434    runtime during loading/initialization.
435    ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
436    OP1 and OP2 are the operands. The tree type of these arguments may be
437    specific to each assertion_code. */
438
439 void
440 add_type_assertion (tree class, int assertion_code, tree op1, tree op2)
441 {
442   htab_t assertions_htab;
443   type_assertion as;
444   void **as_pp;
445
446   assertions_htab = TYPE_ASSERTIONS (class);
447   if (assertions_htab == NULL)
448     {
449       assertions_htab = htab_create_ggc (7, type_assertion_hash, 
450                                          type_assertion_eq, NULL);
451       TYPE_ASSERTIONS (current_class) = assertions_htab;
452     }
453
454   as.assertion_code = assertion_code;
455   as.op1 = op1;
456   as.op2 = op2;
457
458   as_pp = htab_find_slot (assertions_htab, &as, INSERT);
459
460   /* Don't add the same assertion twice.  */
461   if (*as_pp)
462     return;
463
464   *as_pp = ggc_alloc (sizeof (type_assertion));
465   **(type_assertion **)as_pp = as;
466 }
467
468 \f
469 /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
470    Handles array types and interfaces.  */
471
472 int
473 can_widen_reference_to (tree source_type, tree target_type)
474 {
475   if (source_type == ptr_type_node || target_type == object_ptr_type_node)
476     return 1;
477
478   /* Get rid of pointers  */
479   if (TREE_CODE (source_type) == POINTER_TYPE)
480     source_type = TREE_TYPE (source_type);
481   if (TREE_CODE (target_type) == POINTER_TYPE)
482     target_type = TREE_TYPE (target_type);
483
484   if (source_type == target_type)
485     return 1;
486
487   /* FIXME: This is very pessimistic, in that it checks everything,
488      even if we already know that the types are compatible.  If we're
489      to support full Java class loader semantics, we need this.
490      However, we could do something more optimal.  */
491   if (! flag_verify_invocations)
492     {
493       add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE, 
494                           source_type, target_type);
495
496       if (!quiet_flag)
497        warning (0, "assert: %s is assign compatible with %s", 
498                 xstrdup (lang_printable_name (target_type, 0)),
499                 xstrdup (lang_printable_name (source_type, 0)));
500       /* Punt everything to runtime.  */
501       return 1;
502     }
503
504   if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
505     {
506       return 1;
507     }
508   else
509     {
510       if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
511         {
512           HOST_WIDE_INT source_length, target_length;
513           if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
514             {
515               /* An array implements Cloneable and Serializable.  */
516               tree name = DECL_NAME (TYPE_NAME (target_type));
517               return (name == java_lang_cloneable_identifier_node
518                       || name == java_io_serializable_identifier_node);
519             }
520           target_length = java_array_type_length (target_type);
521           if (target_length >= 0)
522             {
523               source_length = java_array_type_length (source_type);
524               if (source_length != target_length)
525                 return 0;
526             }
527           source_type = TYPE_ARRAY_ELEMENT (source_type);
528           target_type = TYPE_ARRAY_ELEMENT (target_type);
529           if (source_type == target_type)
530             return 1;
531           if (TREE_CODE (source_type) != POINTER_TYPE
532               || TREE_CODE (target_type) != POINTER_TYPE)
533             return 0;
534           return can_widen_reference_to (source_type, target_type);
535         }
536       else
537         {
538           int source_depth = class_depth (source_type);
539           int target_depth = class_depth (target_type);
540
541           if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
542             {
543               if (! quiet_flag)
544                 warning (0, "assert: %s is assign compatible with %s", 
545                          xstrdup (lang_printable_name (target_type, 0)),
546                          xstrdup (lang_printable_name (source_type, 0)));
547               return 1;
548             }
549
550           /* class_depth can return a negative depth if an error occurred */
551           if (source_depth < 0 || target_depth < 0)
552             return 0;
553
554           if (CLASS_INTERFACE (TYPE_NAME (target_type)))
555             {
556               /* target_type is OK if source_type or source_type ancestors
557                  implement target_type. We handle multiple sub-interfaces  */
558               tree binfo, base_binfo;
559               int i;
560
561               for (binfo = TYPE_BINFO (source_type), i = 0;
562                    BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
563                 if (can_widen_reference_to
564                     (BINFO_TYPE (base_binfo), target_type))
565                   return 1;
566               
567               if (!i)
568                 return 0;
569             }
570
571           for ( ; source_depth > target_depth;  source_depth--) 
572             {
573               source_type
574                 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
575             }
576           return source_type == target_type;
577         }
578     }
579 }
580
581 static tree
582 pop_value (tree type)
583 {
584   type = pop_type (type);
585   if (quick_stack)
586     {
587       tree node = quick_stack;
588       quick_stack = TREE_CHAIN (quick_stack);
589       TREE_CHAIN (node) = tree_list_free_list;
590       tree_list_free_list = node;
591       node = TREE_VALUE (node);
592       return node;
593     }
594   else
595     return find_stack_slot (stack_pointer, promote_type (type));
596 }
597
598
599 /* Pop and discard the top COUNT stack slots. */
600
601 static void
602 java_stack_pop (int count)
603 {
604   while (count > 0)
605     {
606       tree type, val;
607
608       gcc_assert (stack_pointer != 0);
609
610       type = stack_type_map[stack_pointer - 1];
611       if (type == TYPE_SECOND)
612         {
613           count--;
614           gcc_assert (stack_pointer != 1 && count > 0);
615
616           type = stack_type_map[stack_pointer - 2];
617         }
618       val = pop_value (type);
619       count--;
620     }
621 }
622
623 /* Implement the 'swap' operator (to swap two top stack slots). */
624
625 static void
626 java_stack_swap (void)
627 {
628   tree type1, type2;
629   tree temp;
630   tree decl1, decl2;
631
632   /* Bad stack swap.  */
633   gcc_assert (stack_pointer >= 2
634               && (type1 = stack_type_map[stack_pointer - 1]) != TYPE_UNKNOWN
635               && (type2 = stack_type_map[stack_pointer - 2]) != TYPE_UNKNOWN
636               && (type1 != TYPE_SECOND && type2 != TYPE_SECOND)
637               && (! TYPE_IS_WIDE (type1) && ! TYPE_IS_WIDE (type2)));
638
639   flush_quick_stack ();
640   decl1 = find_stack_slot (stack_pointer - 1, type1);
641   decl2 = find_stack_slot (stack_pointer - 2, type2);
642   temp = build_decl (VAR_DECL, NULL_TREE, type1);
643   java_add_local_var (temp);
644   java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
645   java_add_stmt (build2 (MODIFY_EXPR, type2, 
646                          find_stack_slot (stack_pointer - 1, type2),
647                          decl2));
648   java_add_stmt (build2 (MODIFY_EXPR, type1, 
649                          find_stack_slot (stack_pointer - 2, type1),
650                          temp));
651   stack_type_map[stack_pointer - 1] = type2;
652   stack_type_map[stack_pointer - 2] = type1;
653 }
654
655 static void
656 java_stack_dup (int size, int offset)
657 {
658   int low_index = stack_pointer - size - offset;
659   int dst_index;
660   if (low_index < 0)
661     error ("stack underflow - dup* operation");
662
663   flush_quick_stack ();
664
665   stack_pointer += size;
666   dst_index = stack_pointer;
667
668   for (dst_index = stack_pointer;  --dst_index >= low_index; )
669     {
670       tree type;
671       int src_index = dst_index - size;
672       if (src_index < low_index)
673         src_index = dst_index + size + offset;
674       type = stack_type_map [src_index];
675       if (type == TYPE_SECOND)
676         {
677           /* Dup operation splits 64-bit number.  */
678           gcc_assert (src_index > low_index);
679
680           stack_type_map[dst_index] = type;
681           src_index--;  dst_index--;
682           type = stack_type_map[src_index];
683           gcc_assert (TYPE_IS_WIDE (type));
684         }
685       else
686         gcc_assert (! TYPE_IS_WIDE (type));
687
688       if (src_index != dst_index)
689         {
690           tree src_decl = find_stack_slot (src_index, type);
691           tree dst_decl = find_stack_slot (dst_index, type);
692
693           java_add_stmt 
694             (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
695           stack_type_map[dst_index] = type;
696         }
697     }
698 }
699
700 /* Calls _Jv_Throw or _Jv_Sjlj_Throw.  Discard the contents of the
701    value stack. */
702
703 static void
704 build_java_athrow (tree node)
705 {
706   tree call;
707
708   call = build3 (CALL_EXPR,
709                  void_type_node,
710                  build_address_of (throw_node),
711                  build_tree_list (NULL_TREE, node),
712                  NULL_TREE);
713   TREE_SIDE_EFFECTS (call) = 1;
714   java_add_stmt (call);
715   java_stack_pop (stack_pointer);
716 }
717
718 /* Implementation for jsr/ret */
719
720 static void
721 build_java_jsr (int target_pc, int return_pc)
722 {
723   tree where =  lookup_label (target_pc);
724   tree ret = lookup_label (return_pc);
725   tree ret_label = fold_build1 (ADDR_EXPR, return_address_type_node, ret);
726   push_value (ret_label);
727   flush_quick_stack ();
728   java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
729
730   /* Do not need to emit the label here.  We noted the existence of the
731      label as a jump target in note_instructions; we'll emit the label
732      for real at the beginning of the expand_byte_code loop.  */
733 }
734
735 static void
736 build_java_ret (tree location)
737 {
738   java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
739 }
740  
741 /* Implementation of operations on array: new, load, store, length */
742
743 tree
744 decode_newarray_type (int atype)
745 {
746   switch (atype)
747     {
748     case 4:  return boolean_type_node;
749     case 5:  return char_type_node;
750     case 6:  return float_type_node;
751     case 7:  return double_type_node;
752     case 8:  return byte_type_node;
753     case 9:  return short_type_node;
754     case 10: return int_type_node;
755     case 11: return long_type_node;
756     default: return NULL_TREE;
757     }
758 }
759
760 /* Map primitive type to the code used by OPCODE_newarray. */
761
762 int
763 encode_newarray_type (tree type)
764 {
765   if (type == boolean_type_node)
766     return 4;
767   else if (type == char_type_node)
768     return 5;
769   else if (type == float_type_node)
770     return 6;
771   else if (type == double_type_node)
772     return 7;
773   else if (type == byte_type_node)
774     return 8;
775   else if (type == short_type_node)
776     return 9;
777   else if (type == int_type_node)
778     return 10;
779   else if (type == long_type_node)
780     return 11;
781   else
782     gcc_unreachable ();
783 }
784
785 /* Build a call to _Jv_ThrowBadArrayIndex(), the
786    ArrayIndexOfBoundsException exception handler.  */
787
788 static tree
789 build_java_throw_out_of_bounds_exception (tree index)
790 {
791   tree node = build3 (CALL_EXPR, int_type_node,
792                       build_address_of (soft_badarrayindex_node), 
793                       build_tree_list (NULL_TREE, index), NULL_TREE);
794   TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
795   return (node);
796 }
797
798 /* Return the length of an array. Doesn't perform any checking on the nature
799    or value of the array NODE. May be used to implement some bytecodes.  */
800
801 tree
802 build_java_array_length_access (tree node)
803 {
804   tree type = TREE_TYPE (node);
805   tree array_type = TREE_TYPE (type);
806   HOST_WIDE_INT length;
807
808   if (!is_array_type_p (type))
809     {
810       /* With the new verifier, we will see an ordinary pointer type
811          here.  In this case, we just use an arbitrary array type.  */
812       array_type = build_java_array_type (object_ptr_type_node, -1);
813       type = promote_type (array_type);
814     }
815
816   length = java_array_type_length (type);
817   if (length >= 0)
818     return build_int_cst (NULL_TREE, length);
819
820   node = build3 (COMPONENT_REF, int_type_node,
821                  build_java_indirect_ref (array_type, node,
822                                           flag_check_references),
823                  lookup_field (&array_type, get_identifier ("length")),
824                  NULL_TREE);
825   IS_ARRAY_LENGTH_ACCESS (node) = 1;
826   return node;
827 }
828
829 /* Optionally checks a reference against the NULL pointer.  ARG1: the
830    expr, ARG2: we should check the reference.  Don't generate extra
831    checks if we're not generating code.  */
832
833 tree 
834 java_check_reference (tree expr, int check)
835 {
836   if (!flag_syntax_only && check)
837     {
838       expr = save_expr (expr);
839       expr = build3 (COND_EXPR, TREE_TYPE (expr),
840                      build2 (EQ_EXPR, boolean_type_node,
841                              expr, null_pointer_node),
842                      build3 (CALL_EXPR, void_type_node, 
843                              build_address_of (soft_nullpointer_node),
844                              NULL_TREE, NULL_TREE),
845                      expr);
846     }
847
848   return expr;
849 }
850
851 /* Reference an object: just like an INDIRECT_REF, but with checking.  */
852
853 tree
854 build_java_indirect_ref (tree type, tree expr, int check)
855 {
856   tree t;
857   t = java_check_reference (expr, check);
858   t = convert (build_pointer_type (type), t);
859   return build1 (INDIRECT_REF, type, t);
860 }
861
862 /* Implement array indexing (either as l-value or r-value).
863    Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
864    Optionally performs bounds checking and/or test to NULL.
865    At this point, ARRAY should have been verified as an array.  */
866
867 tree
868 build_java_arrayaccess (tree array, tree type, tree index)
869 {
870   tree node, throw = NULL_TREE;
871   tree data_field;
872   tree ref;
873   tree array_type = TREE_TYPE (TREE_TYPE (array));
874
875   if (!is_array_type_p (TREE_TYPE (array)))
876     {
877       /* With the new verifier, we will see an ordinary pointer type
878          here.  In this case, we just use the correct array type.  */
879       array_type = build_java_array_type (type, -1);
880     }
881
882   if (flag_bounds_check)
883     {
884       /* Generate:
885        * (unsigned jint) INDEX >= (unsigned jint) LEN
886        *    && throw ArrayIndexOutOfBoundsException.
887        * Note this is equivalent to and more efficient than:
888        * INDEX < 0 || INDEX >= LEN && throw ... */
889       tree test;
890       tree len = convert (unsigned_int_type_node,
891                           build_java_array_length_access (array));
892       test = fold_build2 (GE_EXPR, boolean_type_node, 
893                           convert (unsigned_int_type_node, index),
894                           len);
895       if (! integer_zerop (test))
896         {
897           throw = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
898                           build_java_throw_out_of_bounds_exception (index));
899           /* allows expansion within COMPOUND */
900           TREE_SIDE_EFFECTS( throw ) = 1;
901         }
902     }
903
904   /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
905      to have the bounds check evaluated first. */
906   if (throw != NULL_TREE)
907     index = build2 (COMPOUND_EXPR, int_type_node, throw, index);
908  
909   data_field = lookup_field (&array_type, get_identifier ("data"));
910
911   ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),    
912                 build_java_indirect_ref (array_type, array, 
913                                          flag_check_references),
914                 data_field, NULL_TREE);
915   
916   node = build4 (ARRAY_REF, type, ref, index, NULL_TREE, NULL_TREE);
917   return node;
918 }
919
920 /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
921    (at runtime) to an element of ARRAY.  A NOP_EXPR is returned if it can
922    determine that no check is required. */
923
924 tree
925 build_java_arraystore_check (tree array, tree object)
926 {
927   tree check, element_type, source;
928   tree array_type_p = TREE_TYPE (array);
929   tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
930
931   if (! flag_verify_invocations)
932     {
933       /* With the new verifier, we don't track precise types.  FIXME:
934          performance regression here.  */
935       element_type = TYPE_NAME (object_type_node);
936     }
937   else
938     {
939       gcc_assert (is_array_type_p (array_type_p));
940
941       /* Get the TYPE_DECL for ARRAY's element type. */
942       element_type
943         = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
944     }
945
946   gcc_assert (TREE_CODE (element_type) == TYPE_DECL
947               && TREE_CODE (object_type) == TYPE_DECL);
948
949   if (!flag_store_check)
950     return build1 (NOP_EXPR, array_type_p, array);
951
952   /* No check is needed if the element type is final.  Also check that
953      element_type matches object_type, since in the bytecode
954      compilation case element_type may be the actual element type of
955      the array rather than its declared type.  However, if we're doing
956      indirect dispatch, we can't do the `final' optimization.  */
957   if (element_type == object_type
958       && ! flag_indirect_dispatch
959       && CLASS_FINAL (element_type))
960     return build1 (NOP_EXPR, array_type_p, array);
961   
962   /* OBJECT might be wrapped by a SAVE_EXPR. */
963   if (TREE_CODE (object) == SAVE_EXPR)
964     source = TREE_OPERAND (object, 0);
965   else
966     source = object;
967   
968   /* Avoid the check if OBJECT was just loaded from the same array. */
969   if (TREE_CODE (source) == ARRAY_REF)
970     {
971       tree target;
972       source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
973       source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
974       source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
975       if (TREE_CODE (source) == SAVE_EXPR)
976         source = TREE_OPERAND (source, 0);
977       
978       target = array;
979       if (TREE_CODE (target) == SAVE_EXPR)
980         target = TREE_OPERAND (target, 0);
981       
982       if (source == target)
983         return build1 (NOP_EXPR, array_type_p, array);
984     }
985
986   /* Build an invocation of _Jv_CheckArrayStore */
987   check = build3 (CALL_EXPR, void_type_node,
988                   build_address_of (soft_checkarraystore_node),
989                   tree_cons (NULL_TREE, array,
990                              build_tree_list (NULL_TREE, object)),
991                   NULL_TREE);
992   TREE_SIDE_EFFECTS (check) = 1;
993
994   return check;
995 }
996
997 /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
998    ARRAY_NODE. This function is used to retrieve something less vague than
999    a pointer type when indexing the first dimension of something like [[<t>.
1000    May return a corrected type, if necessary, otherwise INDEXED_TYPE is
1001    return unchanged.  */
1002
1003 static tree
1004 build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED,
1005                                tree indexed_type)
1006 {
1007   /* We used to check to see if ARRAY_NODE really had array type.
1008      However, with the new verifier, this is not necessary, as we know
1009      that the object will be an array of the appropriate type.  */
1010
1011   return indexed_type;
1012 }
1013
1014 /* newarray triggers a call to _Jv_NewPrimArray. This function should be 
1015    called with an integer code (the type of array to create), and the length
1016    of the array to create.  */
1017
1018 tree
1019 build_newarray (int atype_value, tree length)
1020 {
1021   tree type_arg;
1022
1023   tree prim_type = decode_newarray_type (atype_value);
1024   tree type
1025     = build_java_array_type (prim_type,
1026                              host_integerp (length, 0) == INTEGER_CST
1027                              ? tree_low_cst (length, 0) : -1);
1028
1029   /* If compiling to native, pass a reference to the primitive type class 
1030      and save the runtime some work. However, the bytecode generator
1031      expects to find the type_code int here. */
1032   if (flag_emit_class_files)
1033     type_arg = build_int_cst (NULL_TREE, atype_value);
1034   else
1035     type_arg = build_class_ref (prim_type);
1036
1037   return build3 (CALL_EXPR, promote_type (type),
1038                  build_address_of (soft_newarray_node),
1039                  tree_cons (NULL_TREE, 
1040                             type_arg,
1041                             build_tree_list (NULL_TREE, length)),
1042                  NULL_TREE);
1043 }
1044
1045 /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
1046    of the dimension. */
1047
1048 tree
1049 build_anewarray (tree class_type, tree length)
1050 {
1051   tree type
1052     = build_java_array_type (class_type,
1053                              host_integerp (length, 0)
1054                              ? tree_low_cst (length, 0) : -1);
1055
1056   return build3 (CALL_EXPR, promote_type (type),
1057                  build_address_of (soft_anewarray_node),
1058                  tree_cons (NULL_TREE, length,
1059                             tree_cons (NULL_TREE, build_class_ref (class_type),
1060                                        build_tree_list (NULL_TREE,
1061                                                         null_pointer_node))),
1062                  NULL_TREE);
1063 }
1064
1065 /* Return a node the evaluates 'new TYPE[LENGTH]'. */
1066
1067 tree
1068 build_new_array (tree type, tree length)
1069 {
1070   if (JPRIMITIVE_TYPE_P (type))
1071     return build_newarray (encode_newarray_type (type), length);
1072   else
1073     return build_anewarray (TREE_TYPE (type), length);
1074 }
1075
1076 /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
1077    class pointer, a number of dimensions and the matching number of
1078    dimensions. The argument list is NULL terminated.  */
1079
1080 static void
1081 expand_java_multianewarray (tree class_type, int ndim)
1082 {
1083   int i;
1084   tree args = build_tree_list( NULL_TREE, null_pointer_node );
1085
1086   for( i = 0; i < ndim; i++ )
1087     args = tree_cons (NULL_TREE, pop_value (int_type_node), args);
1088
1089   push_value (build3 (CALL_EXPR,
1090                       promote_type (class_type),
1091                       build_address_of (soft_multianewarray_node),
1092                       tree_cons (NULL_TREE, build_class_ref (class_type),
1093                                  tree_cons (NULL_TREE, 
1094                                             build_int_cst (NULL_TREE, ndim),
1095                                             args)),
1096                       NULL_TREE));
1097 }
1098
1099 /*  ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
1100     ARRAY is an array type. May expand some bound checking and NULL
1101     pointer checking. RHS_TYPE_NODE we are going to store. In the case
1102     of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
1103     INT. In those cases, we make the conversion.
1104
1105     if ARRAy is a reference type, the assignment is checked at run-time
1106     to make sure that the RHS can be assigned to the array element
1107     type. It is not necessary to generate this code if ARRAY is final.  */
1108
1109 static void
1110 expand_java_arraystore (tree rhs_type_node)
1111 {
1112   tree rhs_node    = pop_value ((INTEGRAL_TYPE_P (rhs_type_node) 
1113                                  && TYPE_PRECISION (rhs_type_node) <= 32) ? 
1114                                  int_type_node : rhs_type_node);
1115   tree index = pop_value (int_type_node);
1116   tree array_type, array;
1117
1118   /* If we're processing an `aaload' we might as well just pick
1119      `Object'.  */
1120   if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1121     {
1122       array_type = build_java_array_type (object_ptr_type_node, -1);
1123       rhs_type_node = object_ptr_type_node;
1124     }
1125   else
1126     array_type = build_java_array_type (rhs_type_node, -1);
1127
1128   array = pop_value (array_type);
1129   array = build1 (NOP_EXPR, promote_type (array_type), array);
1130
1131   rhs_type_node    = build_java_check_indexed_type (array, rhs_type_node);
1132
1133   flush_quick_stack ();
1134
1135   index = save_expr (index);
1136   array = save_expr (array);
1137
1138   if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
1139     {
1140       tree check = build_java_arraystore_check (array, rhs_node);
1141       java_add_stmt (check);
1142     }
1143   
1144   array = build_java_arrayaccess (array, rhs_type_node, index);
1145   java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (array), array, rhs_node));  
1146 }
1147
1148 /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes 
1149    sure that LHS is an array type. May expand some bound checking and NULL
1150    pointer checking.  
1151    LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
1152    BOOLEAN/SHORT, we push a promoted type back to the stack.
1153 */
1154
1155 static void
1156 expand_java_arrayload (tree lhs_type_node)
1157 {
1158   tree load_node;
1159   tree index_node = pop_value (int_type_node);
1160   tree array_type;
1161   tree array_node;
1162
1163   /* If we're processing an `aaload' we might as well just pick
1164      `Object'.  */
1165   if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
1166     {
1167       array_type = build_java_array_type (object_ptr_type_node, -1);
1168       lhs_type_node = object_ptr_type_node;
1169     }
1170   else
1171     array_type = build_java_array_type (lhs_type_node, -1);
1172   array_node = pop_value (array_type);
1173   array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
1174
1175   index_node = save_expr (index_node);
1176   array_node = save_expr (array_node);
1177
1178   lhs_type_node = build_java_check_indexed_type (array_node,
1179                                                  lhs_type_node);
1180   load_node = build_java_arrayaccess (array_node,
1181                                       lhs_type_node,
1182                                       index_node);
1183   if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
1184     load_node = fold_build1 (NOP_EXPR, int_type_node, load_node);
1185   push_value (load_node);
1186 }
1187
1188 /* Expands .length. Makes sure that we deal with and array and may expand
1189    a NULL check on the array object.  */
1190
1191 static void
1192 expand_java_array_length (void)
1193 {
1194   tree array  = pop_value (ptr_type_node);
1195   tree length = build_java_array_length_access (array);
1196
1197   push_value (length);
1198 }
1199
1200 /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
1201    either soft_monitorenter_node or soft_monitorexit_node.  */
1202
1203 static tree
1204 build_java_monitor (tree call, tree object)
1205 {
1206   return build3 (CALL_EXPR,
1207                  void_type_node,
1208                  build_address_of (call),
1209                  build_tree_list (NULL_TREE, object),
1210                  NULL_TREE);
1211 }
1212
1213 /* Emit code for one of the PUSHC instructions. */
1214
1215 static void
1216 expand_java_pushc (int ival, tree type)
1217 {
1218   tree value;
1219   if (type == ptr_type_node && ival == 0)
1220     value = null_pointer_node;
1221   else if (type == int_type_node || type == long_type_node)
1222     value = build_int_cst (type, ival);
1223   else if (type == float_type_node || type == double_type_node)
1224     {
1225       REAL_VALUE_TYPE x;
1226       REAL_VALUE_FROM_INT (x, ival, 0, TYPE_MODE (type));
1227       value = build_real (type, x);
1228     }
1229   else
1230     gcc_unreachable ();
1231
1232   push_value (value);
1233 }
1234
1235 static void
1236 expand_java_return (tree type)
1237 {
1238   if (type == void_type_node)
1239     java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));   
1240   else
1241     {
1242       tree retval = pop_value (type);
1243       tree res = DECL_RESULT (current_function_decl);
1244       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
1245
1246       /* Handle the situation where the native integer type is smaller
1247          than the JVM integer. It can happen for many cross compilers.
1248          The whole if expression just goes away if INT_TYPE_SIZE < 32
1249          is false. */
1250       if (INT_TYPE_SIZE < 32
1251           && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
1252               < GET_MODE_SIZE (TYPE_MODE (type))))
1253         retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
1254       
1255       TREE_SIDE_EFFECTS (retval) = 1;
1256       java_add_stmt (build1 (RETURN_EXPR, TREE_TYPE (retval), retval));
1257     }
1258 }
1259
1260 static void
1261 expand_load_internal (int index, tree type, int pc)
1262 {
1263   tree copy;
1264   tree var = find_local_variable (index, type, pc);
1265
1266   /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
1267      on the stack.  If there is an assignment to this VAR_DECL between
1268      the stack push and the use, then the wrong code could be
1269      generated.  To avoid this we create a new local and copy our
1270      value into it.  Then we push this new local on the stack.
1271      Hopefully this all gets optimized out.  */
1272   copy = build_decl (VAR_DECL, NULL_TREE, type);
1273   if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1274       && TREE_TYPE (copy) != TREE_TYPE (var))
1275     var = convert (type, var);
1276   java_add_local_var (copy);
1277   java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
1278   
1279   push_value (copy);
1280 }
1281
1282 tree
1283 build_address_of (tree value)
1284 {
1285   return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
1286 }
1287
1288 bool
1289 class_has_finalize_method (tree type)
1290 {
1291   tree super = CLASSTYPE_SUPER (type);
1292
1293   if (super == NULL_TREE)
1294     return false;       /* Every class with a real finalizer inherits   */
1295                         /* from java.lang.Object.                       */
1296   else
1297     return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
1298 }
1299
1300 tree
1301 java_create_object (tree type)
1302 {
1303   tree alloc_node = (class_has_finalize_method (type) 
1304                      ? alloc_object_node
1305                      : alloc_no_finalizer_node);
1306   
1307   return build3 (CALL_EXPR, promote_type (type),
1308                  build_address_of (alloc_node),
1309                  build_tree_list (NULL_TREE, build_class_ref (type)),
1310                  NULL_TREE);
1311 }
1312
1313 static void
1314 expand_java_NEW (tree type)
1315 {
1316   tree alloc_node;
1317
1318   alloc_node = (class_has_finalize_method (type) ? alloc_object_node
1319                                                  : alloc_no_finalizer_node);
1320   if (! CLASS_LOADED_P (type))
1321     load_class (type, 1);
1322   safe_layout_class (type);
1323   push_value (build3 (CALL_EXPR, promote_type (type),
1324                       build_address_of (alloc_node),
1325                       build_tree_list (NULL_TREE, build_class_ref (type)),
1326                       NULL_TREE));
1327 }
1328
1329 /* This returns an expression which will extract the class of an
1330    object.  */
1331
1332 tree
1333 build_get_class (tree value)
1334 {
1335   tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
1336   tree vtable_field = lookup_field (&object_type_node,
1337                                     get_identifier ("vtable"));
1338   tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
1339                      build_java_indirect_ref (object_type_node, value,
1340                                               flag_check_references),
1341                      vtable_field, NULL_TREE);
1342   return build3 (COMPONENT_REF, class_ptr_type,
1343                  build1 (INDIRECT_REF, dtable_type, tmp),
1344                  class_field, NULL_TREE);
1345 }
1346
1347 /* This builds the tree representation of the `instanceof' operator.
1348    It tries various tricks to optimize this in cases where types are
1349    known.  */
1350
1351 tree
1352 build_instanceof (tree value, tree type)
1353 {
1354   tree expr;
1355   tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
1356   tree valtype = TREE_TYPE (TREE_TYPE (value));
1357   tree valclass = TYPE_NAME (valtype);
1358   tree klass;
1359
1360   /* When compiling from bytecode, we need to ensure that TYPE has
1361      been loaded.  */
1362   if (CLASS_P (type) && ! CLASS_LOADED_P (type))
1363     {
1364       load_class (type, 1);
1365       safe_layout_class (type);
1366       if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
1367         return error_mark_node;
1368     }
1369   klass = TYPE_NAME (type);
1370
1371   if (type == object_type_node || inherits_from_p (valtype, type))
1372     {
1373       /* Anything except `null' is an instance of Object.  Likewise,
1374          if the object is known to be an instance of the class, then
1375          we only need to check for `null'.  */
1376       expr = build2 (NE_EXPR, itype, value, null_pointer_node);
1377     }
1378   else if (flag_verify_invocations
1379            && ! TYPE_ARRAY_P (type)
1380            && ! TYPE_ARRAY_P (valtype)
1381            && DECL_P (klass) && DECL_P (valclass)
1382            && ! CLASS_INTERFACE (valclass)
1383            && ! CLASS_INTERFACE (klass)
1384            && ! inherits_from_p (type, valtype)
1385            && (CLASS_FINAL (klass)
1386                || ! inherits_from_p (valtype, type)))
1387     {
1388       /* The classes are from different branches of the derivation
1389          tree, so we immediately know the answer.  */
1390       expr = boolean_false_node;
1391     }
1392   else if (DECL_P (klass) && CLASS_FINAL (klass))
1393     {
1394       tree save = save_expr (value);
1395       expr = build3 (COND_EXPR, itype,
1396                      build2 (NE_EXPR, boolean_type_node,
1397                              save, null_pointer_node),
1398                      build2 (EQ_EXPR, itype,
1399                              build_get_class (save),
1400                              build_class_ref (type)),
1401                      boolean_false_node);
1402     }
1403   else
1404     {
1405       expr = build3 (CALL_EXPR, itype,
1406                      build_address_of (soft_instanceof_node),
1407                      tree_cons (NULL_TREE, value,
1408                                 build_tree_list (NULL_TREE,
1409                                                  build_class_ref (type))),
1410                      NULL_TREE);
1411     }
1412   TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
1413   return expr;
1414 }
1415
1416 static void
1417 expand_java_INSTANCEOF (tree type)
1418 {
1419   tree value = pop_value (object_ptr_type_node);
1420   value = build_instanceof (value, type);
1421   push_value (value);
1422 }
1423
1424 static void
1425 expand_java_CHECKCAST (tree type)
1426 {
1427   tree value = pop_value (ptr_type_node);
1428   value = build3 (CALL_EXPR, promote_type (type),
1429                   build_address_of (soft_checkcast_node),
1430                   tree_cons (NULL_TREE, build_class_ref (type),
1431                              build_tree_list (NULL_TREE, value)),
1432                   NULL_TREE);
1433   push_value (value);
1434 }
1435
1436 static void
1437 expand_iinc (unsigned int local_var_index, int ival, int pc)
1438 {
1439   tree local_var, res;
1440   tree constant_value;
1441
1442   flush_quick_stack ();
1443   local_var = find_local_variable (local_var_index, int_type_node, pc);
1444   constant_value = build_int_cst (NULL_TREE, ival);
1445   res = fold_build2 (PLUS_EXPR, int_type_node, local_var, constant_value);
1446   java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
1447   update_aliases (local_var, local_var_index, pc);
1448 }
1449
1450
1451 tree
1452 build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
1453 {
1454   tree call = NULL;
1455   tree arg1 = convert (type, op1);
1456   tree arg2 = convert (type, op2);
1457
1458   if (type == int_type_node)
1459     {     
1460       switch (op)
1461         {
1462         case TRUNC_DIV_EXPR:
1463           call = soft_idiv_node;
1464           break;
1465         case TRUNC_MOD_EXPR:
1466           call = soft_irem_node;
1467           break;
1468         default:
1469           break;
1470         }
1471     }
1472   else if (type == long_type_node)
1473     {     
1474       switch (op)
1475         {
1476         case TRUNC_DIV_EXPR:
1477           call = soft_ldiv_node;
1478           break;
1479         case TRUNC_MOD_EXPR:
1480           call = soft_lrem_node;
1481           break;
1482         default:
1483           break;
1484         }
1485     }
1486
1487   gcc_assert (call);
1488   call = build3 (CALL_EXPR, type,
1489                  build_address_of (call),
1490                  tree_cons (NULL_TREE, arg1,
1491                             build_tree_list (NULL_TREE, arg2)),
1492                  NULL_TREE);
1493           
1494   return call;
1495 }
1496
1497 tree
1498 build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
1499 {
1500   tree mask;
1501   switch (op)
1502     {
1503     case URSHIFT_EXPR:
1504       {
1505         tree u_type = java_unsigned_type (type);
1506         arg1 = convert (u_type, arg1);
1507         arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
1508         return convert (type, arg1);
1509       }
1510     case LSHIFT_EXPR:
1511     case RSHIFT_EXPR:
1512       mask = build_int_cst (NULL_TREE,
1513                             TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
1514       arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
1515       break;
1516
1517     case COMPARE_L_EXPR:  /* arg1 > arg2 ?  1 : arg1 == arg2 ? 0 : -1 */
1518     case COMPARE_G_EXPR:  /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 :  1 */
1519       arg1 = save_expr (arg1);  arg2 = save_expr (arg2);
1520       {
1521         tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
1522                                    boolean_type_node, arg1, arg2);
1523         tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
1524         tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1525                                            ifexp2, integer_zero_node,
1526                                            op == COMPARE_L_EXPR
1527                                            ? integer_minus_one_node
1528                                            : integer_one_node);
1529         return fold_build3 (COND_EXPR, int_type_node, ifexp1,
1530                             op == COMPARE_L_EXPR ? integer_one_node
1531                             : integer_minus_one_node,
1532                             second_compare);
1533       }
1534     case COMPARE_EXPR:
1535       arg1 = save_expr (arg1);  arg2 = save_expr (arg2);
1536       {
1537         tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
1538         tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
1539         tree second_compare = fold_build3 (COND_EXPR, int_type_node,
1540                                            ifexp2, integer_one_node,
1541                                            integer_zero_node);
1542         return fold_build3 (COND_EXPR, int_type_node,
1543                             ifexp1, integer_minus_one_node, second_compare);
1544       }      
1545     case TRUNC_DIV_EXPR:
1546     case TRUNC_MOD_EXPR:
1547       if (TREE_CODE (type) == REAL_TYPE
1548           && op == TRUNC_MOD_EXPR)
1549         {
1550           tree call;
1551           if (type != double_type_node)
1552             {
1553               arg1 = convert (double_type_node, arg1);
1554               arg2 = convert (double_type_node, arg2);
1555             }
1556           call = build3 (CALL_EXPR, double_type_node,
1557                          build_address_of (soft_fmod_node),
1558                          tree_cons (NULL_TREE, arg1,
1559                                     build_tree_list (NULL_TREE, arg2)),
1560                          NULL_TREE);
1561           if (type != double_type_node)
1562             call = convert (type, call);
1563           return call;
1564         }
1565       
1566       if (TREE_CODE (type) == INTEGER_TYPE
1567           && flag_use_divide_subroutine
1568           && ! flag_syntax_only)
1569         return build_java_soft_divmod (op, type, arg1, arg2);
1570       
1571       break;
1572     default:  ;
1573     }
1574   return fold_build2 (op, type, arg1, arg2);
1575 }
1576
1577 static void
1578 expand_java_binop (tree type, enum tree_code op)
1579 {
1580   tree larg, rarg;
1581   tree ltype = type;
1582   tree rtype = type;
1583   switch (op)
1584     {
1585     case LSHIFT_EXPR:
1586     case RSHIFT_EXPR:
1587     case URSHIFT_EXPR:
1588       rtype = int_type_node;
1589       rarg = pop_value (rtype);
1590       break;
1591     default:
1592       rarg = pop_value (rtype);
1593     }
1594   larg = pop_value (ltype);
1595   push_value (build_java_binop (op, type, larg, rarg));
1596 }
1597
1598 /* Lookup the field named NAME in *TYPEP or its super classes.
1599    If not found, return NULL_TREE.
1600    (If the *TYPEP is not found, or if the field reference is
1601    ambiguous, return error_mark_node.)
1602    If found, return the FIELD_DECL, and set *TYPEP to the
1603    class containing the field. */
1604
1605 tree
1606 lookup_field (tree *typep, tree name)
1607 {
1608   if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
1609     {
1610       load_class (*typep, 1);
1611       safe_layout_class (*typep);
1612       if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
1613         return error_mark_node;
1614     }
1615   do
1616     {
1617       tree field, binfo, base_binfo;
1618       tree save_field;
1619       int i;
1620
1621       for (field = TYPE_FIELDS (*typep); field; field = TREE_CHAIN (field))
1622         if (DECL_NAME (field) == name)
1623           return field;
1624
1625       /* Process implemented interfaces. */
1626       save_field = NULL_TREE;
1627       for (binfo = TYPE_BINFO (*typep), i = 0;
1628            BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1629         {
1630           tree t = BINFO_TYPE (base_binfo);
1631           if ((field = lookup_field (&t, name)))
1632             {
1633               if (save_field == field)
1634                 continue;
1635               if (save_field == NULL_TREE)
1636                 save_field = field;
1637               else
1638                 {
1639                   tree i1 = DECL_CONTEXT (save_field);
1640                   tree i2 = DECL_CONTEXT (field);
1641                   error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
1642                          IDENTIFIER_POINTER (name),
1643                          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
1644                          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
1645                   return error_mark_node;
1646                 }
1647             }
1648         }
1649
1650       if (save_field != NULL_TREE)
1651         return save_field;
1652
1653       *typep = CLASSTYPE_SUPER (*typep);
1654     } while (*typep);
1655   return NULL_TREE;
1656 }
1657
1658 /* Look up the field named NAME in object SELF_VALUE,
1659    which has class SELF_CLASS (a non-handle RECORD_TYPE).
1660    SELF_VALUE is NULL_TREE if looking for a static field. */
1661
1662 tree
1663 build_field_ref (tree self_value, tree self_class, tree name)
1664 {
1665   tree base_class = self_class;
1666   tree field_decl = lookup_field (&base_class, name);
1667   if (field_decl == NULL_TREE)
1668     {
1669       error ("field %qs not found", IDENTIFIER_POINTER (name));
1670       return error_mark_node;
1671     }
1672   if (self_value == NULL_TREE)
1673     {
1674       return build_static_field_ref (field_decl);
1675     }
1676   else
1677     {
1678       int check = (flag_check_references
1679                    && ! (DECL_P (self_value)
1680                          && DECL_NAME (self_value) == this_identifier_node));
1681
1682       tree base_type = promote_type (base_class);
1683       if (base_type != TREE_TYPE (self_value))
1684         self_value = fold_build1 (NOP_EXPR, base_type, self_value);
1685       if (! flag_syntax_only && flag_indirect_dispatch)
1686         {
1687           tree otable_index
1688             = build_int_cst (NULL_TREE, get_symbol_table_index 
1689                              (field_decl, &TYPE_OTABLE_METHODS (output_class)));
1690           tree field_offset
1691             = build4 (ARRAY_REF, integer_type_node,
1692                       TYPE_OTABLE_DECL (output_class), otable_index,
1693                       NULL_TREE, NULL_TREE);
1694           tree address;
1695
1696           if (DECL_CONTEXT (field_decl) != output_class)
1697             field_offset
1698               = build3 (COND_EXPR, TREE_TYPE (field_offset),
1699                         build2 (EQ_EXPR, boolean_type_node,
1700                                 field_offset, integer_zero_node),
1701                         build3 (CALL_EXPR, void_type_node, 
1702                                 build_address_of (soft_nosuchfield_node),
1703                                 build_tree_list (NULL_TREE, otable_index), 
1704                                 NULL_TREE),
1705                         field_offset);
1706           
1707           field_offset = fold (convert (sizetype, field_offset));
1708           address 
1709             = fold_build2 (PLUS_EXPR, 
1710                            build_pointer_type (TREE_TYPE (field_decl)),
1711                            self_value, field_offset);
1712           return fold_build1 (INDIRECT_REF, TREE_TYPE (field_decl), address);
1713         }
1714
1715       self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
1716                                             self_value, check);
1717       return fold_build3 (COMPONENT_REF, TREE_TYPE (field_decl),
1718                           self_value, field_decl, NULL_TREE);
1719     }
1720 }
1721
1722 tree
1723 lookup_label (int pc)
1724 {
1725   tree name;
1726   char buf[32];
1727   if (pc > highest_label_pc_this_method)
1728     highest_label_pc_this_method = pc;
1729   ASM_GENERATE_INTERNAL_LABEL(buf, "LJpc=", start_label_pc_this_method + pc);
1730   name = get_identifier (buf);
1731   if (IDENTIFIER_LOCAL_VALUE (name))
1732     return IDENTIFIER_LOCAL_VALUE (name);
1733   else
1734     {
1735       /* The type of the address of a label is return_address_type_node. */
1736       tree decl = create_label_decl (name);
1737       LABEL_PC (decl) = pc;
1738       return pushdecl (decl);
1739     }
1740 }
1741
1742 /* Generate a unique name for the purpose of loops and switches
1743    labels, and try-catch-finally blocks label or temporary variables.  */
1744
1745 tree
1746 generate_name (void)
1747 {
1748   static int l_number = 0;
1749   char buff [32];
1750   ASM_GENERATE_INTERNAL_LABEL(buff, "LJv", l_number);
1751   l_number++;
1752   return get_identifier (buff);
1753 }
1754
1755 tree
1756 create_label_decl (tree name)
1757 {
1758   tree decl;
1759   decl = build_decl (LABEL_DECL, name, 
1760                      TREE_TYPE (return_address_type_node));
1761   DECL_CONTEXT (decl) = current_function_decl;
1762   DECL_IGNORED_P (decl) = 1;
1763   return decl;
1764 }
1765
1766 /* This maps a bytecode offset (PC) to various flags. */
1767 char *instruction_bits;
1768
1769 static void
1770 note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
1771 {
1772   lookup_label (target_pc);
1773   instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
1774 }
1775
1776 /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
1777    where CONDITION is one of one the compare operators. */
1778
1779 static void
1780 expand_compare (enum tree_code condition, tree value1, tree value2,
1781                 int target_pc)
1782 {
1783   tree target = lookup_label (target_pc);
1784   tree cond = fold_build2 (condition, boolean_type_node, value1, value2);
1785   java_add_stmt 
1786     (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
1787              build1 (GOTO_EXPR, void_type_node, target), 
1788              build_java_empty_stmt ()));
1789 }
1790
1791 /* Emit code for a TEST-type opcode. */
1792
1793 static void
1794 expand_test (enum tree_code condition, tree type, int target_pc)
1795 {
1796   tree value1, value2;
1797   flush_quick_stack ();
1798   value1 = pop_value (type);
1799   value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
1800   expand_compare (condition, value1, value2, target_pc);
1801 }
1802
1803 /* Emit code for a COND-type opcode. */
1804
1805 static void
1806 expand_cond (enum tree_code condition, tree type, int target_pc)
1807 {
1808   tree value1, value2;
1809   flush_quick_stack ();
1810   /* note: pop values in opposite order */
1811   value2 = pop_value (type);
1812   value1 = pop_value (type);
1813   /* Maybe should check value1 and value2 for type compatibility ??? */
1814   expand_compare (condition, value1, value2, target_pc);
1815 }
1816
1817 static void
1818 expand_java_goto (int target_pc)
1819 {
1820   tree target_label = lookup_label (target_pc);
1821   flush_quick_stack ();
1822   java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
1823 }
1824
1825 static tree
1826 expand_java_switch (tree selector, int default_pc)
1827 {
1828   tree switch_expr, x;
1829
1830   flush_quick_stack ();
1831   switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
1832                         NULL_TREE, NULL_TREE);
1833   java_add_stmt (switch_expr);
1834
1835   x = build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE,
1836               create_artificial_label ());
1837   append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1838
1839   x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
1840   append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1841
1842   return switch_expr;
1843 }
1844
1845 static void
1846 expand_java_add_case (tree switch_expr, int match, int target_pc)
1847 {
1848   tree value, x;
1849
1850   value = build_int_cst (TREE_TYPE (switch_expr), match);
1851   
1852   x = build3 (CASE_LABEL_EXPR, void_type_node, value, NULL_TREE,
1853               create_artificial_label ());
1854   append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1855
1856   x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
1857   append_to_statement_list (x, &SWITCH_BODY (switch_expr));
1858 }
1859
1860 static tree
1861 pop_arguments (tree arg_types)
1862 {
1863   if (arg_types == end_params_node)
1864     return NULL_TREE;
1865   if (TREE_CODE (arg_types) == TREE_LIST)
1866     {
1867       tree tail = pop_arguments (TREE_CHAIN (arg_types));
1868       tree type = TREE_VALUE (arg_types);
1869       tree arg = pop_value (type);
1870
1871       /* We simply cast each argument to its proper type.  This is
1872          needed since we lose type information coming out of the
1873          verifier.  We also have to do this when we pop an integer
1874          type that must be promoted for the function call.  */
1875       if (TREE_CODE (type) == POINTER_TYPE)
1876         arg = build1 (NOP_EXPR, type, arg);
1877       else if (targetm.calls.promote_prototypes (type)
1878                && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
1879                && INTEGRAL_TYPE_P (type))
1880         arg = convert (integer_type_node, arg);
1881       return tree_cons (NULL_TREE, arg, tail);
1882     }
1883   gcc_unreachable ();
1884 }
1885
1886 /* Attach to PTR (a block) the declaration found in ENTRY. */
1887
1888 int
1889 attach_init_test_initialization_flags (void **entry, void *ptr)
1890 {
1891   tree block = (tree)ptr;
1892   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1893
1894   if (block != error_mark_node)
1895     {
1896       if (TREE_CODE (block) == BIND_EXPR)
1897         {
1898           tree body = BIND_EXPR_BODY (block);
1899           TREE_CHAIN (ite->value) = BIND_EXPR_VARS (block);
1900           BIND_EXPR_VARS (block) = ite->value;
1901           body = build2 (COMPOUND_EXPR, void_type_node,
1902                          build1 (DECL_EXPR, void_type_node, ite->value), body);
1903           BIND_EXPR_BODY (block) = body;
1904         }
1905       else
1906         {
1907           tree body = BLOCK_SUBBLOCKS (block);
1908           TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
1909           BLOCK_EXPR_DECLS (block) = ite->value;
1910           body = build2 (COMPOUND_EXPR, void_type_node,
1911                          build1 (DECL_EXPR, void_type_node, ite->value), body);
1912           BLOCK_SUBBLOCKS (block) = body;
1913         }
1914       
1915     }
1916   return true;
1917 }
1918
1919 /* Build an expression to initialize the class CLAS.
1920    if EXPR is non-NULL, returns an expression to first call the initializer
1921    (if it is needed) and then calls EXPR. */
1922
1923 tree
1924 build_class_init (tree clas, tree expr)
1925 {
1926   tree init;
1927
1928   /* An optimization: if CLAS is a superclass of the class we're
1929      compiling, we don't need to initialize it.  However, if CLAS is
1930      an interface, it won't necessarily be initialized, even if we
1931      implement it.  */
1932   if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1933        && inherits_from_p (current_class, clas))
1934       || current_class == clas)
1935     return expr;
1936
1937   if (always_initialize_class_p)
1938     {
1939       init = build3 (CALL_EXPR, void_type_node,
1940                      build_address_of (soft_initclass_node),
1941                      build_tree_list (NULL_TREE, build_class_ref (clas)),
1942                      NULL_TREE);
1943       TREE_SIDE_EFFECTS (init) = 1;
1944     }
1945   else
1946     {
1947       tree *init_test_decl;
1948       tree decl;
1949       init_test_decl = java_treetreehash_new
1950         (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1951
1952       if (*init_test_decl == NULL)
1953         {
1954           /* Build a declaration and mark it as a flag used to track
1955              static class initializations. */
1956           decl = build_decl (VAR_DECL, NULL_TREE,
1957                              boolean_type_node);
1958           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1959           LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
1960           DECL_CONTEXT (decl) = current_function_decl;
1961           DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
1962           /* Tell the check-init code to ignore this decl when not
1963              optimizing class initialization. */
1964           if (!STATIC_CLASS_INIT_OPT_P ())
1965             DECL_BIT_INDEX (decl) = -1;
1966           DECL_INITIAL (decl) = boolean_false_node;
1967           /* Don't emit any symbolic debugging info for this decl.  */
1968           DECL_IGNORED_P (decl) = 1;      
1969           *init_test_decl = decl;
1970         }
1971
1972       init = build3 (CALL_EXPR, void_type_node,
1973                      build_address_of (soft_initclass_node),
1974                      build_tree_list (NULL_TREE, build_class_ref (clas)),
1975                      NULL_TREE);
1976       TREE_SIDE_EFFECTS (init) = 1;
1977       init = build3 (COND_EXPR, void_type_node,
1978                      build2 (EQ_EXPR, boolean_type_node, 
1979                              *init_test_decl, boolean_false_node),
1980                      init, integer_zero_node);
1981       TREE_SIDE_EFFECTS (init) = 1;
1982       init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, 
1983                      build2 (MODIFY_EXPR, boolean_type_node,
1984                              *init_test_decl, boolean_true_node));
1985       TREE_SIDE_EFFECTS (init) = 1;
1986     }
1987
1988   if (expr != NULL_TREE)
1989     {
1990       expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
1991       TREE_SIDE_EFFECTS (expr) = 1;
1992       return expr;
1993     }
1994   return init;
1995 }
1996
1997 tree
1998 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
1999                         tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2000                         tree arg_list ATTRIBUTE_UNUSED)
2001 {
2002   tree func;
2003   if (is_compiled_class (self_type))
2004     {
2005       /* With indirect dispatch we have to use indirect calls for all
2006          publicly visible methods or gcc will use PLT indirections
2007          to reach them.  We also have to use indirect dispatch for all
2008          external methods.  */
2009       if (! flag_indirect_dispatch 
2010           || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2011         {
2012           func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2013                          method);
2014         }
2015       else
2016         {
2017           tree table_index
2018             = build_int_cst (NULL_TREE, get_symbol_table_index 
2019                              (method, &TYPE_ATABLE_METHODS (output_class)));
2020           func 
2021             = build4 (ARRAY_REF,  
2022                       TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2023                       TYPE_ATABLE_DECL (output_class), table_index,
2024                       NULL_TREE, NULL_TREE);
2025         }
2026       func = convert (method_ptr_type_node, func);
2027     }
2028   else
2029     {
2030       /* We don't know whether the method has been (statically) compiled.
2031          Compile this code to get a reference to the method's code:
2032
2033          SELF_TYPE->methods[METHOD_INDEX].ncode
2034
2035       */
2036
2037       int method_index = 0;
2038       tree meth, ref;
2039
2040       /* The method might actually be declared in some superclass, so
2041          we have to use its class context, not the caller's notion of
2042          where the method is.  */
2043       self_type = DECL_CONTEXT (method);
2044       ref = build_class_ref (self_type);
2045       ref = build1 (INDIRECT_REF, class_type_node, ref);
2046       if (ncode_ident == NULL_TREE)
2047         ncode_ident = get_identifier ("ncode");
2048       if (methods_ident == NULL_TREE)
2049         methods_ident = get_identifier ("methods");
2050       ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2051                     lookup_field (&class_type_node, methods_ident),
2052                     NULL_TREE);
2053       for (meth = TYPE_METHODS (self_type);
2054            ; meth = TREE_CHAIN (meth))
2055         {
2056           if (method == meth)
2057             break;
2058           if (meth == NULL_TREE)
2059             fatal_error ("method '%s' not found in class",
2060                          IDENTIFIER_POINTER (DECL_NAME (method)));
2061           method_index++;
2062         }
2063       method_index *= int_size_in_bytes (method_type_node);
2064       ref = fold_build2 (PLUS_EXPR, method_ptr_type_node,
2065                          ref, build_int_cst (NULL_TREE, method_index));
2066       ref = build1 (INDIRECT_REF, method_type_node, ref);
2067       func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2068                      ref, lookup_field (&method_type_node, ncode_ident),
2069                      NULL_TREE);
2070     }
2071   return func;
2072 }
2073
2074 tree
2075 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2076 {
2077   tree dtable, objectref;
2078
2079   TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2080
2081   /* If we're dealing with interfaces and if the objectref
2082      argument is an array then get the dispatch table of the class
2083      Object rather than the one from the objectref.  */
2084   objectref = (is_invoke_interface 
2085                && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2086                ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2087
2088   if (dtable_ident == NULL_TREE)
2089     dtable_ident = get_identifier ("vtable");
2090   dtable = build_java_indirect_ref (object_type_node, objectref, 
2091                                     flag_check_references);
2092   dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2093                    lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2094
2095   return dtable;
2096 }
2097
2098 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2099    T. If this decl has not been seen before, it will be added to the
2100    [oa]table_methods. If it has, the existing table slot will be
2101    reused.  */
2102
2103 int
2104 get_symbol_table_index (tree t, tree *symbol_table)
2105 {
2106   int i = 1;
2107   tree method_list;
2108
2109   if (*symbol_table == NULL_TREE)
2110     {
2111       *symbol_table = build_tree_list (t, t);
2112       return 1;
2113     }
2114   
2115   method_list = *symbol_table;
2116   
2117   while (1)
2118     {
2119       tree value = TREE_VALUE (method_list);
2120       if (value == t)
2121         return i;
2122       i++;
2123       if (TREE_CHAIN (method_list) == NULL_TREE)
2124         break;
2125       else
2126         method_list = TREE_CHAIN (method_list);
2127     }
2128
2129   TREE_CHAIN (method_list) = build_tree_list (t, t);
2130   return i;
2131 }
2132
2133 tree 
2134 build_invokevirtual (tree dtable, tree method)
2135 {
2136   tree func;
2137   tree nativecode_ptr_ptr_type_node
2138     = build_pointer_type (nativecode_ptr_type_node);
2139   tree method_index;
2140   tree otable_index;
2141
2142   if (flag_indirect_dispatch)
2143     {
2144       gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))));
2145
2146       otable_index 
2147         = build_int_cst (NULL_TREE, get_symbol_table_index 
2148                          (method, &TYPE_OTABLE_METHODS (output_class)));
2149       method_index = build4 (ARRAY_REF, integer_type_node, 
2150                              TYPE_OTABLE_DECL (output_class), 
2151                              otable_index, NULL_TREE, NULL_TREE);
2152     }
2153   else
2154     {
2155       /* We fetch the DECL_VINDEX field directly here, rather than
2156          using get_method_index().  DECL_VINDEX is the true offset
2157          from the vtable base to a method, regrdless of any extra
2158          words inserted at the start of the vtable.  */
2159       method_index = DECL_VINDEX (method);
2160       method_index = size_binop (MULT_EXPR, method_index,
2161                                  TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2162       if (TARGET_VTABLE_USES_DESCRIPTORS)
2163         method_index = size_binop (MULT_EXPR, method_index,
2164                                    size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2165     }
2166
2167   func = fold_build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2168                       convert (nativecode_ptr_ptr_type_node, method_index));
2169
2170   if (TARGET_VTABLE_USES_DESCRIPTORS)
2171     func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2172   else
2173     func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2174
2175   return func;
2176 }
2177
2178 static GTY(()) tree class_ident;
2179 tree
2180 build_invokeinterface (tree dtable, tree method)
2181 {
2182   tree lookup_arg;
2183   tree interface;
2184   tree idx;
2185
2186   /* We expand invokeinterface here.  */
2187             
2188   if (class_ident == NULL_TREE)
2189     class_ident = get_identifier ("class");
2190
2191   dtable = build_java_indirect_ref (dtable_type, dtable,
2192                                     flag_check_references);
2193   dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2194                    lookup_field (&dtable_type, class_ident), NULL_TREE);
2195
2196   interface = DECL_CONTEXT (method);
2197   gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface)));
2198   layout_class_methods (interface);
2199   
2200   if (flag_indirect_dispatch)
2201     {
2202       int itable_index 
2203         = 2 * (get_symbol_table_index 
2204                (method, &TYPE_ITABLE_METHODS (output_class)));
2205       interface 
2206         = build4 (ARRAY_REF, 
2207                  TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2208                  TYPE_ITABLE_DECL (output_class), 
2209                   build_int_cst (NULL_TREE, itable_index-1),
2210                   NULL_TREE, NULL_TREE);
2211       idx 
2212         = build4 (ARRAY_REF, 
2213                  TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2214                  TYPE_ITABLE_DECL (output_class), 
2215                   build_int_cst (NULL_TREE, itable_index),
2216                   NULL_TREE, NULL_TREE);
2217       interface = convert (class_ptr_type, interface);
2218       idx = convert (integer_type_node, idx);
2219     }
2220   else
2221     {
2222       idx = build_int_cst (NULL_TREE, 
2223                            get_interface_method_index (method, interface));
2224       interface = build_class_ref (interface);
2225     }
2226                                                           
2227   lookup_arg = tree_cons (NULL_TREE, dtable,
2228                           tree_cons (NULL_TREE, interface,
2229                                      build_tree_list (NULL_TREE, idx)));
2230
2231   return build3 (CALL_EXPR, ptr_type_node, 
2232                  build_address_of (soft_lookupinterfacemethod_node),
2233                  lookup_arg, NULL_TREE);
2234 }
2235   
2236 /* Expand one of the invoke_* opcodes.
2237    OPCODE is the specific opcode.
2238    METHOD_REF_INDEX is an index into the constant pool.
2239    NARGS is the number of arguments, or -1 if not specified. */
2240
2241 static void
2242 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2243 {
2244   tree method_signature
2245     = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2246   tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2247                                          method_ref_index);
2248   tree self_type
2249     = get_class_constant (current_jcf,
2250                           COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2251                           method_ref_index));
2252   const char *const self_name
2253     = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2254   tree call, func, method, arg_list, method_type;
2255   tree check = NULL_TREE;
2256
2257   if (! CLASS_LOADED_P (self_type))
2258     {
2259       load_class (self_type, 1);
2260       safe_layout_class (self_type);
2261       if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2262         fatal_error ("failed to find class '%s'", self_name);
2263     }
2264   layout_class_methods (self_type);
2265
2266   if (ID_INIT_P (method_name))
2267     method = lookup_java_constructor (self_type, method_signature);
2268   else
2269     method = lookup_java_method (self_type, method_name, method_signature);
2270
2271   /* We've found a method in an interface, but this isn't an interface
2272      call.  */
2273   if (opcode != OPCODE_invokeinterface
2274       && method
2275       && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2276     method = NULL_TREE;
2277
2278   /* We've found a non-interface method but we are making an
2279      interface call.  This can happen if the interface overrides a
2280      method in Object.  */
2281   if (! flag_verify_invocations
2282       && opcode == OPCODE_invokeinterface
2283       && method
2284       && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2285     method = NULL_TREE;
2286
2287   if (method == NULL_TREE)
2288     {
2289       if (flag_verify_invocations || ! flag_indirect_dispatch)
2290         {
2291           error ("class '%s' has no method named '%s' matching signature '%s'",
2292                  self_name,
2293                  IDENTIFIER_POINTER (method_name),
2294                  IDENTIFIER_POINTER (method_signature));
2295         }
2296       else
2297         {
2298           int flags = ACC_PUBLIC;
2299           if (opcode == OPCODE_invokestatic)
2300             flags |= ACC_STATIC;
2301           if (opcode == OPCODE_invokeinterface)
2302             {
2303               flags |= ACC_INTERFACE | ACC_ABSTRACT;
2304               CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2305             }
2306           method = add_method (self_type, flags, method_name,
2307                                method_signature);
2308           DECL_ARTIFICIAL (method) = 1;
2309           METHOD_DUMMY (method) = 1;
2310           layout_class_method (self_type, NULL,
2311                                method, NULL);
2312         }
2313     }
2314
2315   /* Invoke static can't invoke static/abstract method */
2316   if (method != NULL_TREE)
2317     {
2318       if (opcode == OPCODE_invokestatic)
2319         {
2320           if (!METHOD_STATIC (method))
2321             {
2322               error ("invokestatic on non static method");
2323               method = NULL_TREE;
2324             }
2325           else if (METHOD_ABSTRACT (method))
2326             {
2327               error ("invokestatic on abstract method");
2328               method = NULL_TREE;
2329             }
2330         }
2331       else
2332         {
2333           if (METHOD_STATIC (method))
2334             {
2335               error ("invoke[non-static] on static method");
2336               method = NULL_TREE;
2337             }
2338         }
2339     }
2340
2341   if (method == NULL_TREE)
2342     {
2343       /* If we got here, we emitted an error message above.  So we
2344          just pop the arguments, push a properly-typed zero, and
2345          continue.  */
2346       method_type = get_type_from_signature (method_signature);
2347       pop_arguments (TYPE_ARG_TYPES (method_type));
2348       if (opcode != OPCODE_invokestatic) 
2349         pop_type (self_type);
2350       method_type = promote_type (TREE_TYPE (method_type));
2351       push_value (convert (method_type, integer_zero_node));
2352       return;
2353     }
2354
2355   method_type = TREE_TYPE (method);
2356   arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2357   flush_quick_stack ();
2358
2359   func = NULL_TREE;
2360   if (opcode == OPCODE_invokestatic)
2361     func = build_known_method_ref (method, method_type, self_type,
2362                                    method_signature, arg_list);
2363   else if (opcode == OPCODE_invokespecial
2364            || (opcode == OPCODE_invokevirtual
2365                && (METHOD_PRIVATE (method)
2366                    || METHOD_FINAL (method) 
2367                    || CLASS_FINAL (TYPE_NAME (self_type)))))
2368     {
2369       /* If the object for the method call is null, we throw an
2370          exception.  We don't do this if the object is the current
2371          method's `this'.  In other cases we just rely on an
2372          optimization pass to eliminate redundant checks.  FIXME:
2373          Unfortunately there doesn't seem to be a way to determine
2374          what the current method is right now.
2375          We do omit the check if we're calling <init>.  */
2376       /* We use a SAVE_EXPR here to make sure we only evaluate
2377          the new `self' expression once.  */
2378       tree save_arg = save_expr (TREE_VALUE (arg_list));
2379       TREE_VALUE (arg_list) = save_arg;
2380       check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2381       func = build_known_method_ref (method, method_type, self_type,
2382                                      method_signature, arg_list);
2383     }
2384   else
2385     {
2386       tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface, 
2387                                          arg_list);
2388       if (opcode == OPCODE_invokevirtual)
2389         func = build_invokevirtual (dtable, method);
2390       else
2391         func = build_invokeinterface (dtable, method);
2392     }
2393       
2394   if (TREE_CODE (func) == ADDR_EXPR)
2395     TREE_TYPE (func) = build_pointer_type (method_type);
2396   else
2397     func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2398
2399   call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2400                  func, arg_list, NULL_TREE);
2401   TREE_SIDE_EFFECTS (call) = 1;
2402   call = check_for_builtin (method, call);
2403
2404   if (check != NULL_TREE)
2405     {
2406       call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2407       TREE_SIDE_EFFECTS (call) = 1;
2408     }
2409
2410   if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2411     java_add_stmt (call);
2412   else
2413     {
2414       push_value (call);
2415       flush_quick_stack ();
2416     }
2417 }
2418
2419 /* Create a stub which will be put into the vtable but which will call
2420    a JNI function.  */
2421
2422 tree
2423 build_jni_stub (tree method)
2424 {
2425   tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2426   tree jni_func_type, tem;
2427   tree env_var, res_var = NULL_TREE, block;
2428   tree method_args, res_type;
2429   tree meth_var;
2430   tree bind;
2431
2432   int args_size = 0;
2433
2434   tree klass = DECL_CONTEXT (method);
2435   int from_class = ! CLASS_FROM_SOURCE_P (klass);
2436   klass = build_class_ref (klass);
2437
2438   gcc_assert (METHOD_NATIVE (method) && flag_jni);
2439
2440   DECL_ARTIFICIAL (method) = 1;
2441   DECL_EXTERNAL (method) = 0;
2442
2443   env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2444   DECL_CONTEXT (env_var) = method;
2445
2446   if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2447     {
2448       res_var = build_decl (VAR_DECL, get_identifier ("res"),
2449                             TREE_TYPE (TREE_TYPE (method)));
2450       DECL_CONTEXT (res_var) = method;
2451       TREE_CHAIN (env_var) = res_var;
2452     }
2453
2454   meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2455   TREE_STATIC (meth_var) = 1;
2456   TREE_PUBLIC (meth_var) = 0;
2457   DECL_EXTERNAL (meth_var) = 0;
2458   DECL_CONTEXT (meth_var) = method;
2459   DECL_ARTIFICIAL (meth_var) = 1;
2460   DECL_INITIAL (meth_var) = null_pointer_node;
2461   TREE_USED (meth_var) = 1;
2462   chainon (env_var, meth_var);
2463   build_result_decl (method);
2464
2465   /* One strange way that the front ends are different is that they
2466      store arguments differently.  */
2467   if (from_class)
2468     method_args = DECL_ARGUMENTS (method);
2469   else
2470     method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2471   block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
2472   TREE_SIDE_EFFECTS (block) = 1;
2473   /* When compiling from source we don't set the type of the block,
2474      because that will prevent patch_return from ever being run.  */
2475   if (from_class)
2476     TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2477
2478   /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame.  */
2479   body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2480                  build3 (CALL_EXPR, ptr_type_node,
2481                          build_address_of (soft_getjnienvnewframe_node),
2482                          build_tree_list (NULL_TREE, klass),
2483                          NULL_TREE));
2484   CAN_COMPLETE_NORMALLY (body) = 1;
2485
2486   /* All the arguments to this method become arguments to the
2487      underlying JNI function.  If we had to wrap object arguments in a
2488      special way, we would do that here.  */
2489   args = NULL_TREE;
2490   for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2491     {
2492       int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
2493 #ifdef PARM_BOUNDARY
2494       arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2495                   * PARM_BOUNDARY);
2496 #endif
2497       args_size += (arg_bits / BITS_PER_UNIT);
2498
2499       args = tree_cons (NULL_TREE, tem, args);
2500     }
2501   args = nreverse (args);
2502   arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2503
2504   /* For a static method the second argument is the class.  For a
2505      non-static method the second argument is `this'; that is already
2506      available in the argument list.  */
2507   if (METHOD_STATIC (method))
2508     {
2509       args_size += int_size_in_bytes (TREE_TYPE (klass));
2510       args = tree_cons (NULL_TREE, klass, args);
2511       arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2512     }
2513
2514   /* The JNIEnv structure is the first argument to the JNI function.  */
2515   args_size += int_size_in_bytes (TREE_TYPE (env_var));
2516   args = tree_cons (NULL_TREE, env_var, args);
2517   arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2518
2519   /* We call _Jv_LookupJNIMethod to find the actual underlying
2520      function pointer.  _Jv_LookupJNIMethod will throw the appropriate
2521      exception if this function is not found at runtime.  */
2522   tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2523   method_sig = build_java_signature (TREE_TYPE (method));
2524   lookup_arg = tree_cons (NULL_TREE,
2525                           build_utf8_ref (unmangle_classname
2526                                           (IDENTIFIER_POINTER (method_sig),
2527                                            IDENTIFIER_LENGTH (method_sig))), 
2528                           tem);
2529   tem = DECL_NAME (method);
2530   lookup_arg
2531     = tree_cons (NULL_TREE, klass,
2532                  tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2533   
2534   tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2535
2536 #ifdef MODIFY_JNI_METHOD_CALL
2537   tem = MODIFY_JNI_METHOD_CALL (tem);
2538 #endif
2539
2540   jni_func_type = build_pointer_type (tem);
2541
2542   jnifunc = build3 (COND_EXPR, ptr_type_node,
2543                     meth_var, meth_var,
2544                     build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2545                             build3 (CALL_EXPR, ptr_type_node,
2546                                     build_address_of
2547                                       (soft_lookupjnimethod_node),
2548                                     lookup_arg, NULL_TREE)));
2549
2550   /* Now we make the actual JNI call via the resulting function
2551      pointer.    */
2552   call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2553                  build1 (NOP_EXPR, jni_func_type, jnifunc),
2554                  args, NULL_TREE);
2555
2556   /* If the JNI call returned a result, capture it here.  If we had to
2557      unwrap JNI object results, we would do that here.  */
2558   if (res_var != NULL_TREE)
2559     {
2560       /* If the call returns an object, it may return a JNI weak
2561          reference, in which case we must unwrap it.  */
2562       if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
2563         call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2564                        build_address_of (soft_unwrapjni_node),
2565                        build_tree_list (NULL_TREE, call),
2566                        NULL_TREE);
2567       call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2568                      res_var, call);
2569     }
2570
2571   TREE_SIDE_EFFECTS (call) = 1;
2572   CAN_COMPLETE_NORMALLY (call) = 1;
2573
2574   body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2575   TREE_SIDE_EFFECTS (body) = 1;
2576
2577   /* Now free the environment we allocated.  */
2578   call = build3 (CALL_EXPR, ptr_type_node,
2579                  build_address_of (soft_jnipopsystemframe_node),
2580                  build_tree_list (NULL_TREE, env_var),
2581                  NULL_TREE);
2582   TREE_SIDE_EFFECTS (call) = 1;
2583   CAN_COMPLETE_NORMALLY (call) = 1;
2584   body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2585   TREE_SIDE_EFFECTS (body) = 1;
2586
2587   /* Finally, do the return.  */
2588   res_type = void_type_node;
2589   if (res_var != NULL_TREE)
2590     {
2591       tree drt;
2592       gcc_assert (DECL_RESULT (method));
2593       /* Make sure we copy the result variable to the actual
2594          result.  We use the type of the DECL_RESULT because it
2595          might be different from the return type of the function:
2596          it might be promoted.  */
2597       drt = TREE_TYPE (DECL_RESULT (method));
2598       if (drt != TREE_TYPE (res_var))
2599         res_var = build1 (CONVERT_EXPR, drt, res_var);
2600       res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2601       TREE_SIDE_EFFECTS (res_var) = 1;
2602     }
2603
2604   body = build2 (COMPOUND_EXPR, void_type_node, body,
2605                  build1 (RETURN_EXPR, res_type, res_var));
2606   TREE_SIDE_EFFECTS (body) = 1;
2607   
2608   bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block), 
2609                  body, block);
2610   return bind;
2611 }
2612
2613 /* Expand an operation to extract from or store into a field.
2614    IS_STATIC is 1 iff the field is static.
2615    IS_PUTTING is 1 for putting into a field;  0 for getting from the field.
2616    FIELD_REF_INDEX is an index into the constant pool.  */
2617
2618 static void
2619 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2620 {
2621   tree self_type
2622     = get_class_constant (current_jcf,
2623                           COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2624                           field_ref_index));
2625   const char *self_name
2626     = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2627   tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2628   tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool, 
2629                                                   field_ref_index);
2630   tree field_type = get_type_from_signature (field_signature);
2631   tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2632   tree field_ref;
2633   int is_error = 0;
2634   tree original_self_type = self_type;
2635   tree field_decl;
2636   
2637   if (! CLASS_LOADED_P (self_type))
2638     load_class (self_type, 1);  
2639   field_decl = lookup_field (&self_type, field_name);
2640   if (field_decl == error_mark_node)
2641     {
2642       is_error = 1;
2643     }
2644   else if (field_decl == NULL_TREE)
2645     {
2646       if (! flag_verify_invocations)
2647         {
2648           int flags = ACC_PUBLIC;
2649           if (is_static)
2650             flags |= ACC_STATIC;
2651           self_type = original_self_type;
2652           field_decl = add_field (original_self_type, field_name,
2653                                   field_type, flags); 
2654           DECL_ARTIFICIAL (field_decl) = 1;
2655           DECL_IGNORED_P (field_decl) = 1;
2656         }
2657       else
2658         {      
2659           error ("missing field '%s' in '%s'",
2660                  IDENTIFIER_POINTER (field_name), self_name);
2661           is_error = 1;
2662       }
2663     }
2664   else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2665     {
2666       error ("mismatching signature for field '%s' in '%s'",
2667              IDENTIFIER_POINTER (field_name), self_name);
2668       is_error = 1;
2669     }
2670   field_ref = is_static ? NULL_TREE : pop_value (self_type);
2671   if (is_error)
2672     {
2673       if (! is_putting)
2674         push_value (convert (field_type, integer_zero_node));
2675       flush_quick_stack ();
2676       return;
2677     }
2678
2679   field_ref = build_field_ref (field_ref, self_type, field_name);
2680   if (is_static
2681       && ! flag_indirect_dispatch)
2682     field_ref = build_class_init (self_type, field_ref);
2683   if (is_putting)
2684     {
2685       flush_quick_stack ();
2686       if (FIELD_FINAL (field_decl))
2687         {
2688           if (DECL_CONTEXT (field_decl) != current_class)
2689             error ("assignment to final field %q+D not in field's class",
2690                    field_decl);
2691           else if (FIELD_STATIC (field_decl))
2692             {
2693               if (!DECL_CLINIT_P (current_function_decl))
2694                 warning (0, "assignment to final static field %q+D not in "
2695                          "class initializer",
2696                          field_decl);
2697             }
2698           else
2699             {
2700               tree cfndecl_name = DECL_NAME (current_function_decl);
2701               if (! DECL_CONSTRUCTOR_P (current_function_decl)
2702                   && !ID_FINIT_P (cfndecl_name))
2703                 warning (0, "assignment to final field %q+D not in constructor",
2704                          field_decl);
2705             }
2706         }
2707       java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2708                              field_ref, new_value));
2709     }
2710   else
2711     push_value (field_ref);
2712 }
2713
2714 void
2715 load_type_state (tree label)
2716 {
2717   int i;
2718   tree vec = LABEL_TYPE_STATE (label);
2719   int cur_length = TREE_VEC_LENGTH (vec);
2720   stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2721   for (i = 0; i < cur_length; i++)
2722     type_map [i] = TREE_VEC_ELT (vec, i);
2723 }
2724
2725 /* Go over METHOD's bytecode and note instruction starts in
2726    instruction_bits[].  */
2727
2728 void
2729 note_instructions (JCF *jcf, tree method)
2730 {
2731   int PC; 
2732   unsigned char* byte_ops;
2733   long length = DECL_CODE_LENGTH (method);
2734
2735   int saw_index;
2736   jint INT_temp;
2737
2738 #undef RET /* Defined by config/i386/i386.h */
2739 #undef PTR
2740 #define BCODE byte_ops
2741 #define BYTE_type_node byte_type_node
2742 #define SHORT_type_node short_type_node
2743 #define INT_type_node int_type_node
2744 #define LONG_type_node long_type_node
2745 #define CHAR_type_node char_type_node
2746 #define PTR_type_node ptr_type_node
2747 #define FLOAT_type_node float_type_node
2748 #define DOUBLE_type_node double_type_node
2749 #define VOID_type_node void_type_node
2750 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2751 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2752 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2753 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2754
2755 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2756
2757   JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2758   byte_ops = jcf->read_ptr;
2759   instruction_bits = xrealloc (instruction_bits, length + 1);
2760   memset (instruction_bits, 0, length + 1);
2761
2762   /* This pass figures out which PC can be the targets of jumps. */
2763   for (PC = 0; PC < length;)
2764     {
2765       int oldpc = PC; /* PC at instruction start. */
2766       instruction_bits [PC] |=  BCODE_INSTRUCTION_START;
2767       switch (byte_ops[PC++])
2768         {
2769 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2770         case OPCODE: \
2771           PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2772           break;
2773
2774 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2775
2776 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2777 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2778 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2779 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2780 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2781 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2782 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2783 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2784
2785 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2786   PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2787 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2788   ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2789 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2790 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2791 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2792 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2793
2794 /* two forms of wide instructions */
2795 #define PRE_SPECIAL_WIDE(IGNORE) \
2796   { \
2797     int modified_opcode = IMMEDIATE_u1; \
2798     if (modified_opcode == OPCODE_iinc) \
2799       { \
2800         (void) IMMEDIATE_u2;    /* indexbyte1 and indexbyte2 */ \
2801         (void) IMMEDIATE_s2;    /* constbyte1 and constbyte2 */ \
2802       } \
2803     else \
2804       { \
2805         (void) IMMEDIATE_u2;    /* indexbyte1 and indexbyte2 */ \
2806       } \
2807   }
2808
2809 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2810
2811 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2812
2813 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2814 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2815           PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2816 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2817 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2818 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2819 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2820 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2821 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2822 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2823
2824 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2825 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2826 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2827   saw_index = 0;  INT_temp = (OPERAND_VALUE); \
2828   if (!saw_index)  NOTE_LABEL(oldpc + INT_temp);
2829 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2830   saw_index = 0;  INT_temp = (OPERAND_VALUE); \
2831   NOTE_LABEL (PC); \
2832   if (!saw_index)  NOTE_LABEL(oldpc + INT_temp);
2833
2834 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE)  (void)(OPERAND_VALUE)
2835
2836 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2837   PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2838
2839 #define PRE_LOOKUP_SWITCH                                               \
2840   { jint default_offset = IMMEDIATE_s4;  jint npairs = IMMEDIATE_s4;    \
2841     NOTE_LABEL (default_offset+oldpc);                                  \
2842     if (npairs >= 0)                                                    \
2843       while (--npairs >= 0) {                                           \
2844        jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4;                      \
2845        jint offset = IMMEDIATE_s4;                                      \
2846        NOTE_LABEL (offset+oldpc); }                                     \
2847   }
2848
2849 #define PRE_TABLE_SWITCH                                \
2850   { jint default_offset = IMMEDIATE_s4;                 \
2851     jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4;  \
2852     NOTE_LABEL (default_offset+oldpc);                  \
2853     if (low <= high)                                    \
2854      while (low++ <= high) {                            \
2855        jint offset = IMMEDIATE_s4;                      \
2856        NOTE_LABEL (offset+oldpc); }                     \
2857   }
2858
2859 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2860 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2861 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2862   (void)(IMMEDIATE_u2); \
2863   PC += 2 * IS_INTERFACE /* for invokeinterface */;
2864
2865 #include "javaop.def"
2866 #undef JAVAOP
2867         }
2868     } /* for */
2869 }
2870
2871 void
2872 expand_byte_code (JCF *jcf, tree method)
2873 {
2874   int PC;
2875   int i;
2876   const unsigned char *linenumber_pointer;
2877   int dead_code_index = -1;
2878   unsigned char* byte_ops;
2879   long length = DECL_CODE_LENGTH (method);
2880
2881   stack_pointer = 0;
2882   JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2883   byte_ops = jcf->read_ptr;
2884
2885   /* We make an initial pass of the line number table, to note
2886      which instructions have associated line number entries. */
2887   linenumber_pointer = linenumber_table;
2888   for (i = 0; i < linenumber_count; i++)
2889     {
2890       int pc = GET_u2 (linenumber_pointer);
2891       linenumber_pointer += 4;
2892       if (pc >= length)
2893         warning (0, "invalid PC in line number table");
2894       else
2895         {
2896           if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2897             instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2898           instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2899         }
2900     }  
2901
2902   if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2903     return;
2904
2905   promote_arguments ();
2906
2907   /* Translate bytecodes.  */
2908   linenumber_pointer = linenumber_table;
2909   for (PC = 0; PC < length;)
2910     {
2911       if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2912         {
2913           tree label = lookup_label (PC);
2914           flush_quick_stack ();
2915           if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2916             java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2917           if (LABEL_VERIFIED (label) || PC == 0)
2918             load_type_state (label);
2919         }
2920
2921       if (! (instruction_bits [PC] & BCODE_VERIFIED))
2922         {
2923           if (dead_code_index == -1)
2924             {
2925               /* This is the start of a region of unreachable bytecodes.
2926                  They still need to be processed in order for EH ranges
2927                  to get handled correctly.  However, we can simply
2928                  replace these bytecodes with nops.  */
2929               dead_code_index = PC;
2930             }
2931           
2932           /* Turn this bytecode into a nop.  */
2933           byte_ops[PC] = 0x0;
2934         }
2935        else
2936         {
2937           if (dead_code_index != -1)
2938             {
2939               /* We've just reached the end of a region of dead code.  */
2940               if (extra_warnings)
2941                 warning (0, "unreachable bytecode from %d to before %d",
2942                          dead_code_index, PC);
2943               dead_code_index = -1;
2944             }
2945         }
2946
2947       /* Handle possible line number entry for this PC.
2948
2949          This code handles out-of-order and multiple linenumbers per PC,
2950          but is optimized for the case of line numbers increasing
2951          monotonically with PC. */
2952       if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2953         {
2954           if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2955               || GET_u2 (linenumber_pointer) != PC)
2956             linenumber_pointer = linenumber_table;
2957           while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2958             {
2959               int pc = GET_u2 (linenumber_pointer);
2960               linenumber_pointer += 4;
2961               if (pc == PC)
2962                 {
2963                   int line = GET_u2 (linenumber_pointer - 2);
2964 #ifdef USE_MAPPED_LOCATION
2965                   input_location = linemap_line_start (&line_table, line, 1);
2966 #else
2967                   input_location.line = line;
2968 #endif
2969                   if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2970                     break;
2971                 }
2972             }
2973         }
2974       maybe_pushlevels (PC);
2975       PC = process_jvm_instruction (PC, byte_ops, length);
2976       maybe_poplevels (PC);
2977     } /* for */
2978   
2979   if (dead_code_index != -1)
2980     {
2981       /* We've just reached the end of a region of dead code.  */
2982       if (extra_warnings)
2983         warning (0, "unreachable bytecode from %d to the end of the method", 
2984                  dead_code_index);
2985     }
2986 }
2987
2988 static void
2989 java_push_constant_from_pool (JCF *jcf, int index)
2990 {
2991   tree c;
2992   if (JPOOL_TAG (jcf, index) == CONSTANT_String)
2993     {
2994       tree name;
2995       name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
2996       index = alloc_name_constant (CONSTANT_String, name);
2997       c = build_ref_from_constant_pool (index);
2998       c = convert (promote_type (string_type_node), c);
2999     }
3000   else
3001     c = get_constant (jcf, index);
3002   push_value (c);
3003
3004
3005 int
3006 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3007                          long length ATTRIBUTE_UNUSED)
3008
3009   const char *opname; /* Temporary ??? */
3010   int oldpc = PC; /* PC at instruction start. */
3011
3012   /* If the instruction is at the beginning of an exception handler,
3013      replace the top of the stack with the thrown object reference.  */
3014   if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3015     {
3016       /* Note that the verifier will not emit a type map at all for
3017          dead exception handlers.  In this case we just ignore the
3018          situation.  */
3019       if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
3020         {
3021           tree type = pop_type (promote_type (throwable_type_node));
3022           push_value (build_exception_object_ref (type));
3023         }
3024     }
3025
3026   switch (byte_ops[PC++])
3027     {
3028 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3029     case OPCODE: \
3030       opname = #OPNAME; \
3031       OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3032       break;
3033
3034 #define RET(OPERAND_TYPE, OPERAND_VALUE)                                \
3035   {                                                                     \
3036     int saw_index = 0;                                                  \
3037     int index     = OPERAND_VALUE;                                      \
3038     build_java_ret                                                      \
3039       (find_local_variable (index, return_address_type_node, oldpc));   \
3040   }
3041
3042 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3043   {                                                 \
3044     /* OPERAND_VALUE may have side-effects on PC */ \
3045     int opvalue = OPERAND_VALUE;                    \
3046     build_java_jsr (oldpc + opvalue, PC);           \
3047   }
3048
3049 /* Push a constant onto the stack. */
3050 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3051   { int saw_index = 0;  int ival = (OPERAND_VALUE); \
3052     if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3053     else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3054
3055 /* internal macro added for use by the WIDE case */
3056 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3057   expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3058
3059 /* Push local variable onto the opcode stack. */
3060 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3061   { \
3062     /* have to do this since OPERAND_VALUE may have side-effects */ \
3063     int opvalue = OPERAND_VALUE; \
3064     LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3065   }
3066
3067 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3068   expand_java_return (OPERAND_TYPE##_type_node)
3069
3070 #define REM_EXPR TRUNC_MOD_EXPR
3071 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3072   expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3073
3074 #define FIELD(IS_STATIC, IS_PUT) \
3075   expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3076
3077 #define TEST(OPERAND_TYPE, CONDITION) \
3078   expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3079
3080 #define COND(OPERAND_TYPE, CONDITION) \
3081   expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3082
3083 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3084   BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3085
3086 #define BRANCH_GOTO(OPERAND_VALUE) \
3087   expand_java_goto (oldpc + OPERAND_VALUE)
3088
3089 #define BRANCH_CALL(OPERAND_VALUE) \
3090   expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3091
3092 #if 0
3093 #define BRANCH_RETURN(OPERAND_VALUE) \
3094   { \
3095     tree type = OPERAND_TYPE##_type_node; \
3096     tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3097     expand_java_ret (value); \
3098   }
3099 #endif
3100
3101 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3102           fprintf (stderr, "%3d: %s ", oldpc, opname); \
3103           fprintf (stderr, "(not implemented)\n")
3104 #define NOT_IMPL1(OPERAND_VALUE) \
3105           fprintf (stderr, "%3d: %s ", oldpc, opname); \
3106           fprintf (stderr, "(not implemented)\n")
3107
3108 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3109
3110 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3111
3112 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3113
3114 #define STACK_SWAP(COUNT) java_stack_swap()
3115
3116 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3117 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3118 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3119
3120 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3121   PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3122
3123 #define LOOKUP_SWITCH \
3124   { jint default_offset = IMMEDIATE_s4;  jint npairs = IMMEDIATE_s4; \
3125     tree selector = pop_value (INT_type_node); \
3126     tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3127     while (--npairs >= 0) \
3128       { \
3129         jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3130         expand_java_add_case (switch_expr, match, oldpc + offset); \
3131       } \
3132   }
3133
3134 #define TABLE_SWITCH \
3135   { jint default_offset = IMMEDIATE_s4; \
3136     jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3137     tree selector = pop_value (INT_type_node); \
3138     tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3139     for (; low <= high; low++) \
3140       { \
3141         jint offset = IMMEDIATE_s4; \
3142         expand_java_add_case (switch_expr, low, oldpc + offset); \
3143       } \
3144   }
3145
3146 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3147   { int opcode = byte_ops[PC-1]; \
3148     int method_ref_index = IMMEDIATE_u2; \
3149     int nargs; \
3150     if (IS_INTERFACE) { nargs = IMMEDIATE_u1;  (void) IMMEDIATE_u1; } \
3151     else nargs = -1; \
3152     expand_invoke (opcode, method_ref_index, nargs); \
3153   }
3154
3155 /* Handle new, checkcast, instanceof */
3156 #define OBJECT(TYPE, OP) \
3157   expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3158
3159 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3160
3161 #define ARRAY_LOAD(OPERAND_TYPE)                        \
3162   {                                                     \
3163     expand_java_arrayload( OPERAND_TYPE##_type_node );  \
3164   }
3165
3166 #define ARRAY_STORE(OPERAND_TYPE)                       \
3167   {                                                     \
3168     expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3169   }
3170
3171 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3172 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3173 #define ARRAY_NEW_PTR()                                                 \
3174     push_value (build_anewarray (get_class_constant (current_jcf,       \
3175                                                      IMMEDIATE_u2),     \
3176                                  pop_value (int_type_node)));
3177 #define ARRAY_NEW_NUM()                         \
3178   {                                             \
3179     int atype = IMMEDIATE_u1;                   \
3180     push_value (build_newarray (atype, pop_value (int_type_node)));\
3181   }
3182 #define ARRAY_NEW_MULTI()                                       \
3183   {                                                             \
3184     tree class = get_class_constant (current_jcf, IMMEDIATE_u2 );       \
3185     int  ndims = IMMEDIATE_u1;                                  \
3186     expand_java_multianewarray( class, ndims );                 \
3187   }
3188
3189 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3190   push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3191                            pop_value (OPERAND_TYPE##_type_node)));
3192
3193 #define CONVERT2(FROM_TYPE, TO_TYPE)                                     \
3194   {                                                                      \
3195     push_value (build1 (NOP_EXPR, int_type_node,                         \
3196                         (convert (TO_TYPE##_type_node,                   \
3197                                   pop_value (FROM_TYPE##_type_node))))); \
3198   }
3199
3200 #define CONVERT(FROM_TYPE, TO_TYPE)                             \
3201   {                                                             \
3202     push_value (convert (TO_TYPE##_type_node,                   \
3203                          pop_value (FROM_TYPE##_type_node)));   \
3204   }
3205
3206 /* internal macro added for use by the WIDE case 
3207    Added TREE_TYPE (decl) assignment, apbianco  */
3208 #define STORE_INTERNAL(OPTYPE, OPVALUE)                         \
3209   {                                                             \
3210     tree decl, value;                                           \
3211     int index = OPVALUE;                                        \
3212     tree type = OPTYPE;                                         \
3213     value = pop_value (type);                                   \
3214     type = TREE_TYPE (value);                                   \
3215     decl = find_local_variable (index, type, oldpc);            \
3216     set_local_type (index, type);                               \
3217     java_add_stmt (build2 (MODIFY_EXPR, type, decl, value));    \
3218     update_aliases (decl, index, PC);                           \
3219   }
3220
3221 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3222   { \
3223     /* have to do this since OPERAND_VALUE may have side-effects */ \
3224     int opvalue = OPERAND_VALUE; \
3225     STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3226   }
3227
3228 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3229   SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3230
3231 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3232 #define SPECIAL_EXIT(IGNORED)  MONITOR_OPERATION (soft_monitorexit_node)
3233
3234 #define MONITOR_OPERATION(call)                 \
3235   {                                             \
3236     tree o = pop_value (ptr_type_node);         \
3237     tree c;                                     \
3238     flush_quick_stack ();                       \
3239     c = build_java_monitor (call, o);           \
3240     TREE_SIDE_EFFECTS (c) = 1;                  \
3241     java_add_stmt (c);                          \
3242   }
3243
3244 #define SPECIAL_IINC(IGNORED) \
3245   { \
3246     unsigned int local_var_index = IMMEDIATE_u1; \
3247     int ival = IMMEDIATE_s1; \
3248     expand_iinc(local_var_index, ival, oldpc); \
3249   }
3250
3251 #define SPECIAL_WIDE(IGNORED) \
3252   { \
3253     int modified_opcode = IMMEDIATE_u1; \
3254     unsigned int local_var_index = IMMEDIATE_u2; \
3255     switch (modified_opcode) \
3256       { \
3257       case OPCODE_iinc: \
3258         { \
3259           int ival = IMMEDIATE_s2; \
3260           expand_iinc (local_var_index, ival, oldpc); \
3261           break; \
3262         } \
3263       case OPCODE_iload: \
3264       case OPCODE_lload: \
3265       case OPCODE_fload: \
3266       case OPCODE_dload: \
3267       case OPCODE_aload: \
3268         { \
3269           /* duplicate code from LOAD macro */ \
3270           LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3271           break; \
3272         } \
3273       case OPCODE_istore: \
3274       case OPCODE_lstore: \
3275       case OPCODE_fstore: \
3276       case OPCODE_dstore: \
3277       case OPCODE_astore: \
3278         { \
3279           STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3280           break; \
3281         } \
3282       default: \
3283         error ("unrecogized wide sub-instruction"); \
3284       } \
3285   }
3286
3287 #define SPECIAL_THROW(IGNORED) \
3288   build_java_athrow (pop_value (throwable_type_node))
3289
3290 #define SPECIAL_BREAK NOT_IMPL1
3291 #define IMPL          NOT_IMPL
3292
3293 #include "javaop.def"
3294 #undef JAVAOP
3295    default:
3296     fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3297   }
3298   return PC;
3299 }
3300
3301 /* Return the opcode at PC in the code section pointed to by
3302    CODE_OFFSET.  */
3303
3304 static unsigned char
3305 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3306 {
3307   unsigned char opcode;
3308   long absolute_offset = (long)JCF_TELL (jcf);
3309
3310   JCF_SEEK (jcf, code_offset);
3311   opcode = jcf->read_ptr [pc];
3312   JCF_SEEK (jcf, absolute_offset);
3313   return opcode;
3314 }
3315
3316 /* Some bytecode compilers are emitting accurate LocalVariableTable
3317    attributes. Here's an example:
3318    
3319      PC   <t>store_<n>
3320      PC+1 ...
3321      
3322      Attribute "LocalVariableTable"
3323      slot #<n>: ... (PC: PC+1 length: L)
3324    
3325    This is accurate because the local in slot <n> really exists after
3326    the opcode at PC is executed, hence from PC+1 to PC+1+L.
3327
3328    This procedure recognizes this situation and extends the live range
3329    of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3330    length of the store instruction.)
3331
3332    This function is used by `give_name_to_locals' so that a local's
3333    DECL features a DECL_LOCAL_START_PC such that the first related
3334    store operation will use DECL as a destination, not an unrelated
3335    temporary created for the occasion.
3336
3337    This function uses a global (instruction_bits) `note_instructions' should
3338    have allocated and filled properly.  */
3339
3340 int
3341 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3342                        int start_pc, int slot)
3343 {
3344   int first, index, opcode;
3345   int pc, insn_pc;
3346   int wide_found = 0;
3347
3348   if (!start_pc)
3349     return start_pc;
3350
3351   first = index = -1;
3352
3353   /* Find last previous instruction and remember it */
3354   for (pc = start_pc-1; pc; pc--) 
3355     if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3356       break;
3357   insn_pc = pc;
3358
3359   /* Retrieve the instruction, handle `wide'. */  
3360   opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3361   if (opcode == OPCODE_wide)
3362     {
3363       wide_found = 1;
3364       opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3365     }
3366
3367   switch (opcode)
3368     {
3369     case OPCODE_astore_0:
3370     case OPCODE_astore_1:
3371     case OPCODE_astore_2:
3372     case OPCODE_astore_3:
3373       first = OPCODE_astore_0;
3374       break;
3375
3376     case OPCODE_istore_0:
3377     case OPCODE_istore_1:
3378     case OPCODE_istore_2:
3379     case OPCODE_istore_3:
3380       first = OPCODE_istore_0;
3381       break;
3382       
3383     case OPCODE_lstore_0:
3384     case OPCODE_lstore_1:
3385     case OPCODE_lstore_2:
3386     case OPCODE_lstore_3:
3387       first = OPCODE_lstore_0;
3388       break;
3389
3390     case OPCODE_fstore_0:
3391     case OPCODE_fstore_1:
3392     case OPCODE_fstore_2:
3393     case OPCODE_fstore_3:
3394       first = OPCODE_fstore_0;
3395       break;
3396
3397     case OPCODE_dstore_0:
3398     case OPCODE_dstore_1:
3399     case OPCODE_dstore_2:
3400     case OPCODE_dstore_3:
3401       first = OPCODE_dstore_0;
3402       break;
3403
3404     case OPCODE_astore:
3405     case OPCODE_istore:
3406     case OPCODE_lstore:
3407     case OPCODE_fstore:
3408     case OPCODE_dstore:
3409       index = peek_opcode_at_pc (jcf, code_offset, pc);
3410       if (wide_found)
3411         {
3412           int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3413           index = (other << 8) + index;
3414         }
3415       break;
3416     }
3417
3418   /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3419      means we have a <t>store. */
3420   if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3421     start_pc = insn_pc;
3422
3423   return start_pc;
3424 }
3425
3426 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3427    order, as specified by Java Language Specification.
3428
3429    The problem is that while expand_expr will evaluate its sub-operands in
3430    left-to-right order, for variables it will just return an rtx (i.e.
3431    an lvalue) for the variable (rather than an rvalue).  So it is possible
3432    that a later sub-operand will change the register, and when the
3433    actual operation is done, it will use the new value, when it should
3434    have used the original value.
3435
3436    We fix this by using save_expr.  This forces the sub-operand to be
3437    copied into a fresh virtual register,
3438
3439    For method invocation, we modify the arguments so that a
3440    left-to-right order evaluation is performed. Saved expressions
3441    will, in CALL_EXPR order, be reused when the call will be expanded.
3442
3443    We also promote outgoing args if needed.  */
3444
3445 tree
3446 force_evaluation_order (tree node)
3447 {
3448   if (flag_syntax_only)
3449     return node;
3450   if (TREE_CODE (node) == CALL_EXPR
3451       || TREE_CODE (node) == NEW_CLASS_EXPR
3452       || (TREE_CODE (node) == COMPOUND_EXPR
3453           && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3454           && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR)) 
3455     {
3456       tree arg, cmp;
3457
3458       arg = node;
3459       
3460       /* Position arg properly, account for wrapped around ctors. */
3461       if (TREE_CODE (node) == COMPOUND_EXPR)
3462         arg = TREE_OPERAND (node, 0);
3463       
3464       arg = TREE_OPERAND (arg, 1);
3465       
3466       /* An empty argument list is ok, just ignore it.  */
3467       if (!arg)
3468         return node;
3469
3470       /* Not having a list of arguments here is an error. */ 
3471       gcc_assert (TREE_CODE (arg) == TREE_LIST);
3472
3473       /* This reverses the evaluation order. This is a desired effect. */
3474       for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3475         {
3476           /* Promote types smaller than integer.  This is required by
3477              some ABIs.  */
3478           tree type = TREE_TYPE (TREE_VALUE (arg));
3479           tree saved;
3480           if (targetm.calls.promote_prototypes (type)
3481               && INTEGRAL_TYPE_P (type)
3482               && INT_CST_LT_UNSIGNED (TYPE_SIZE (type),
3483                                       TYPE_SIZE (integer_type_node)))
3484             TREE_VALUE (arg) = fold_convert (integer_type_node, TREE_VALUE (arg));
3485
3486           saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3487           cmp = (cmp == NULL_TREE ? saved :
3488                  build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3489           TREE_VALUE (arg) = saved;
3490         }
3491       
3492       if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3493         TREE_SIDE_EFFECTS (cmp) = 1;
3494
3495       if (cmp)
3496         {
3497           cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3498           if (TREE_TYPE (cmp) != void_type_node)
3499             cmp = save_expr (cmp);
3500           CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3501           TREE_SIDE_EFFECTS (cmp) = 1;
3502           node = cmp;
3503         }
3504     }
3505   return node;
3506 }
3507
3508 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3509    location where an expression or an identifier were encountered. It
3510    is necessary for languages where the frontend parser will handle
3511    recursively more than one file (Java is one of them).  */
3512
3513 tree
3514 build_expr_wfl (tree node,
3515 #ifdef USE_MAPPED_LOCATION
3516                 source_location location
3517 #else
3518                 const char *file, int line, int col
3519 #endif
3520 )
3521 {
3522   tree wfl;
3523
3524 #ifdef USE_MAPPED_LOCATION
3525   wfl = make_node (EXPR_WITH_FILE_LOCATION);
3526   SET_EXPR_LOCATION (wfl, location);
3527 #else
3528   static const char *last_file = 0;
3529   static tree last_filenode = NULL_TREE;
3530
3531   wfl = make_node (EXPR_WITH_FILE_LOCATION);
3532
3533   EXPR_WFL_SET_LINECOL (wfl, line, col);
3534   if (file != last_file)
3535     {
3536       last_file = file;
3537       last_filenode = file ? get_identifier (file) : NULL_TREE;
3538     }
3539   EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3540 #endif
3541   EXPR_WFL_NODE (wfl) = node;
3542   if (node)
3543     {
3544       if (!TYPE_P (node))
3545         TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3546       TREE_TYPE (wfl) = TREE_TYPE (node);
3547     }
3548
3549   return wfl;
3550 }
3551
3552 #ifdef USE_MAPPED_LOCATION
3553 tree
3554 expr_add_location (tree node, source_location location, bool statement)
3555 {
3556   tree wfl;
3557 #if 0
3558   /* FIXME. This optimization causes failures in code that expects an
3559      EXPR_WITH_FILE_LOCATION.  E.g. in resolve_qualified_expression_name. */
3560   if (node && ! (statement && flag_emit_class_files))
3561     {
3562       source_location node_loc = EXPR_LOCATION (node);
3563       if (node_loc == location || location == UNKNOWN_LOCATION)
3564         return node;
3565       if (node_loc == UNKNOWN_LOCATION
3566           && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3567         {
3568           SET_EXPR_LOCATION (node, location);
3569           return node;
3570         }
3571     }
3572 #endif
3573   wfl = make_node (EXPR_WITH_FILE_LOCATION);
3574   SET_EXPR_LOCATION (wfl, location);
3575   EXPR_WFL_NODE (wfl) = node;
3576   if (statement && debug_info_level != DINFO_LEVEL_NONE)
3577     EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3578   if (node)
3579     {
3580       if (!TYPE_P (node))
3581         TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3582       TREE_TYPE (wfl) = TREE_TYPE (node);
3583     }
3584
3585   return wfl;
3586 }
3587 #endif
3588
3589 /* Build a node to represent empty statements and blocks. */
3590
3591 tree
3592 build_java_empty_stmt (void)
3593 {
3594   tree t = build_empty_stmt ();
3595   CAN_COMPLETE_NORMALLY (t) = 1;
3596   return t;
3597 }
3598
3599 /* Promote all args of integral type before generating any code.  */
3600
3601 static void
3602 promote_arguments (void)
3603 {
3604   int i;
3605   tree arg;
3606   for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
3607        arg != NULL_TREE;  arg = TREE_CHAIN (arg), i++)
3608     {
3609       tree arg_type = TREE_TYPE (arg);
3610       if (INTEGRAL_TYPE_P (arg_type)
3611           && TYPE_PRECISION (arg_type) < 32)
3612         {
3613           tree copy = find_local_variable (i, integer_type_node, -1);
3614           java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
3615                                  copy,
3616                                  fold_convert (integer_type_node, arg)));
3617         }
3618       if (TYPE_IS_WIDE (arg_type))
3619         i++;
3620     }
3621 }
3622
3623 #include "gt-java-expr.h"