OSDN Git Service

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