OSDN Git Service

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