OSDN Git Service

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