OSDN Git Service

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