OSDN Git Service

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