OSDN Git Service

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