OSDN Git Service

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