OSDN Git Service

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