OSDN Git Service

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