OSDN Git Service

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