OSDN Git Service

* Merged gcj-abi-2-dev-branch to trunk.
[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 /* Build an expression to initialize the class CLAS.
1939    if EXPR is non-NULL, returns an expression to first call the initializer
1940    (if it is needed) and then calls EXPR. */
1941
1942 tree
1943 build_class_init (tree clas, tree expr)
1944 {
1945   tree init;
1946
1947   /* An optimization: if CLAS is a superclass of the class we're
1948      compiling, we don't need to initialize it.  However, if CLAS is
1949      an interface, it won't necessarily be initialized, even if we
1950      implement it.  */
1951   if ((! CLASS_INTERFACE (TYPE_NAME (clas))
1952        && inherits_from_p (current_class, clas))
1953       || current_class == clas)
1954     return expr;
1955
1956   if (always_initialize_class_p)
1957     {
1958       init = build3 (CALL_EXPR, void_type_node,
1959                      build_address_of (soft_initclass_node),
1960                      build_tree_list (NULL_TREE, build_class_ref (clas)),
1961                      NULL_TREE);
1962       TREE_SIDE_EFFECTS (init) = 1;
1963     }
1964   else
1965     {
1966       tree *init_test_decl;
1967       tree decl;
1968       init_test_decl = java_treetreehash_new
1969         (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
1970
1971       if (*init_test_decl == NULL)
1972         {
1973           /* Build a declaration and mark it as a flag used to track
1974              static class initializations. */
1975           decl = build_decl (VAR_DECL, NULL_TREE,
1976                              boolean_type_node);
1977           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1978           LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
1979           DECL_CONTEXT (decl) = current_function_decl;
1980           DECL_FUNCTION_INIT_TEST_CLASS (decl) = clas;
1981           /* Tell the check-init code to ignore this decl when not
1982              optimizing class initialization. */
1983           if (!STATIC_CLASS_INIT_OPT_P ())
1984             DECL_BIT_INDEX (decl) = -1;
1985           DECL_INITIAL (decl) = boolean_false_node;
1986           /* Don't emit any symbolic debugging info for this decl.  */
1987           DECL_IGNORED_P (decl) = 1;      
1988           *init_test_decl = decl;
1989         }
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       init = build3 (COND_EXPR, void_type_node,
1997                      build2 (EQ_EXPR, boolean_type_node, 
1998                              *init_test_decl, boolean_false_node),
1999                      init, integer_zero_node);
2000       TREE_SIDE_EFFECTS (init) = 1;
2001       init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, 
2002                      build2 (MODIFY_EXPR, boolean_type_node,
2003                              *init_test_decl, boolean_true_node));
2004       TREE_SIDE_EFFECTS (init) = 1;
2005     }
2006
2007   if (expr != NULL_TREE)
2008     {
2009       expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
2010       TREE_SIDE_EFFECTS (expr) = 1;
2011       return expr;
2012     }
2013   return init;
2014 }
2015
2016 tree
2017 build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
2018                         tree self_type, tree method_signature ATTRIBUTE_UNUSED,
2019                         tree arg_list ATTRIBUTE_UNUSED)
2020 {
2021   tree func;
2022   if (is_compiled_class (self_type))
2023     {
2024       /* With indirect dispatch we have to use indirect calls for all
2025          publically visible methods or gcc will use PLT indirections
2026          to reach them.  We also have to use indirect dispatch for all
2027          external methods.  */
2028       if (! flag_indirect_dispatch 
2029           || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
2030         {
2031           make_decl_rtl (method);
2032           func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
2033                          method);
2034         }
2035       else
2036         {
2037           tree table_index
2038             = build_int_cst (NULL_TREE, get_symbol_table_index 
2039                              (method, &TYPE_ATABLE_METHODS (output_class)));
2040           func 
2041             = build4 (ARRAY_REF,  
2042                       TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
2043                       TYPE_ATABLE_DECL (output_class), table_index,
2044                       NULL_TREE, NULL_TREE);
2045         }
2046       func = convert (method_ptr_type_node, func);
2047     }
2048   else
2049     {
2050       /* We don't know whether the method has been (statically) compiled.
2051          Compile this code to get a reference to the method's code:
2052
2053          SELF_TYPE->methods[METHOD_INDEX].ncode
2054
2055       */
2056
2057       int method_index = 0;
2058       tree meth, ref;
2059
2060       /* The method might actually be declared in some superclass, so
2061          we have to use its class context, not the caller's notion of
2062          where the method is.  */
2063       self_type = DECL_CONTEXT (method);
2064       ref = build_class_ref (self_type);
2065       ref = build1 (INDIRECT_REF, class_type_node, ref);
2066       if (ncode_ident == NULL_TREE)
2067         ncode_ident = get_identifier ("ncode");
2068       if (methods_ident == NULL_TREE)
2069         methods_ident = get_identifier ("methods");
2070       ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
2071                     lookup_field (&class_type_node, methods_ident),
2072                     NULL_TREE);
2073       for (meth = TYPE_METHODS (self_type);
2074            ; meth = TREE_CHAIN (meth))
2075         {
2076           if (method == meth)
2077             break;
2078           if (meth == NULL_TREE)
2079             fatal_error ("method '%s' not found in class",
2080                          IDENTIFIER_POINTER (DECL_NAME (method)));
2081           method_index++;
2082         }
2083       method_index *= int_size_in_bytes (method_type_node);
2084       ref = fold (build2 (PLUS_EXPR, method_ptr_type_node,
2085                           ref, build_int_cst (NULL_TREE, method_index)));
2086       ref = build1 (INDIRECT_REF, method_type_node, ref);
2087       func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
2088                      ref, lookup_field (&method_type_node, ncode_ident),
2089                      NULL_TREE);
2090     }
2091   return func;
2092 }
2093
2094 tree
2095 invoke_build_dtable (int is_invoke_interface, tree arg_list)
2096 {
2097   tree dtable, objectref;
2098
2099   TREE_VALUE (arg_list) = save_expr (TREE_VALUE (arg_list));
2100
2101   /* If we're dealing with interfaces and if the objectref
2102      argument is an array then get the dispatch table of the class
2103      Object rather than the one from the objectref.  */
2104   objectref = (is_invoke_interface 
2105                && is_array_type_p (TREE_TYPE (TREE_VALUE (arg_list)))
2106                ? build_class_ref (object_type_node) : TREE_VALUE (arg_list));
2107
2108   if (dtable_ident == NULL_TREE)
2109     dtable_ident = get_identifier ("vtable");
2110   dtable = build_java_indirect_ref (object_type_node, objectref, 
2111                                     flag_check_references);
2112   dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
2113                    lookup_field (&object_type_node, dtable_ident), NULL_TREE);
2114
2115   return dtable;
2116 }
2117
2118 /* Determine the index in SYMBOL_TABLE for a reference to the decl
2119    T. If this decl has not been seen before, it will be added to the
2120    [oa]table_methods. If it has, the existing table slot will be
2121    reused.  */
2122
2123 int
2124 get_symbol_table_index (tree t, tree *symbol_table)
2125 {
2126   int i = 1;
2127   tree method_list;
2128
2129   if (*symbol_table == NULL_TREE)
2130     {
2131       *symbol_table = build_tree_list (t, t);
2132       return 1;
2133     }
2134   
2135   method_list = *symbol_table;
2136   
2137   while (1)
2138     {
2139       tree value = TREE_VALUE (method_list);
2140       if (value == t)
2141         return i;
2142       i++;
2143       if (TREE_CHAIN (method_list) == NULL_TREE)
2144         break;
2145       else
2146         method_list = TREE_CHAIN (method_list);
2147     }
2148
2149   TREE_CHAIN (method_list) = build_tree_list (t, t);
2150   return i;
2151 }
2152
2153 tree 
2154 build_invokevirtual (tree dtable, tree method)
2155 {
2156   tree func;
2157   tree nativecode_ptr_ptr_type_node
2158     = build_pointer_type (nativecode_ptr_type_node);
2159   tree method_index;
2160   tree otable_index;
2161
2162   if (flag_indirect_dispatch)
2163     {
2164       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2165         abort ();
2166
2167       otable_index 
2168         = build_int_cst (NULL_TREE, get_symbol_table_index 
2169                          (method, &TYPE_OTABLE_METHODS (output_class)));
2170       method_index = build4 (ARRAY_REF, integer_type_node, 
2171                              TYPE_OTABLE_DECL (output_class), 
2172                              otable_index, NULL_TREE, NULL_TREE);
2173     }
2174   else
2175     {
2176       /* We fetch the DECL_VINDEX field directly here, rather than
2177          using get_method_index().  DECL_VINDEX is the true offset
2178          from the vtable base to a method, regrdless of any extra
2179          words inserted at the start of the vtable.  */
2180       method_index = DECL_VINDEX (method);
2181       method_index = size_binop (MULT_EXPR, method_index,
2182                                  TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
2183       if (TARGET_VTABLE_USES_DESCRIPTORS)
2184         method_index = size_binop (MULT_EXPR, method_index,
2185                                    size_int (TARGET_VTABLE_USES_DESCRIPTORS));
2186     }
2187
2188   func = fold (build2 (PLUS_EXPR, nativecode_ptr_ptr_type_node, dtable,
2189                        convert (nativecode_ptr_ptr_type_node, method_index)));
2190
2191   if (TARGET_VTABLE_USES_DESCRIPTORS)
2192     func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
2193   else
2194     func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
2195
2196   return func;
2197 }
2198
2199 static GTY(()) tree class_ident;
2200 tree
2201 build_invokeinterface (tree dtable, tree method)
2202 {
2203   tree lookup_arg;
2204   tree interface;
2205   tree idx;
2206
2207   /* We expand invokeinterface here.  */
2208             
2209   if (class_ident == NULL_TREE)
2210     class_ident = get_identifier ("class");
2211
2212   dtable = build_java_indirect_ref (dtable_type, dtable,
2213                                     flag_check_references);
2214   dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
2215                    lookup_field (&dtable_type, class_ident), NULL_TREE);
2216
2217   interface = DECL_CONTEXT (method);
2218   if (! CLASS_INTERFACE (TYPE_NAME (interface)))
2219     abort ();
2220   layout_class_methods (interface);
2221   
2222   if (flag_indirect_dispatch)
2223     {
2224       int itable_index 
2225         = 2 * (get_symbol_table_index 
2226                (method, &TYPE_ITABLE_METHODS (output_class)));
2227       interface 
2228         = build4 (ARRAY_REF, 
2229                  TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2230                  TYPE_ITABLE_DECL (output_class), 
2231                   build_int_cst (NULL_TREE, itable_index-1),
2232                   NULL_TREE, NULL_TREE);
2233       idx 
2234         = build4 (ARRAY_REF, 
2235                  TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
2236                  TYPE_ITABLE_DECL (output_class), 
2237                   build_int_cst (NULL_TREE, itable_index),
2238                   NULL_TREE, NULL_TREE);
2239       interface = convert (class_ptr_type, interface);
2240       idx = convert (integer_type_node, idx);
2241     }
2242   else
2243     {
2244       idx = build_int_cst (NULL_TREE, 
2245                            get_interface_method_index (method, interface));
2246       interface = build_class_ref (interface);
2247     }
2248                                                           
2249   lookup_arg = tree_cons (NULL_TREE, dtable,
2250                           tree_cons (NULL_TREE, interface,
2251                                      build_tree_list (NULL_TREE, idx)));
2252
2253   return build3 (CALL_EXPR, ptr_type_node, 
2254                  build_address_of (soft_lookupinterfacemethod_node),
2255                  lookup_arg, NULL_TREE);
2256 }
2257   
2258 /* Expand one of the invoke_* opcodes.
2259    OPCODE is the specific opcode.
2260    METHOD_REF_INDEX is an index into the constant pool.
2261    NARGS is the number of arguments, or -1 if not specified. */
2262
2263 static void
2264 expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
2265 {
2266   tree method_signature
2267     = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
2268   tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
2269                                          method_ref_index);
2270   tree self_type
2271     = get_class_constant (current_jcf,
2272                           COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
2273                           method_ref_index));
2274   const char *const self_name
2275     = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2276   tree call, func, method, arg_list, method_type;
2277   tree check = NULL_TREE;
2278
2279   if (! CLASS_LOADED_P (self_type))
2280     {
2281       load_class (self_type, 1);
2282       safe_layout_class (self_type);
2283       if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
2284         fatal_error ("failed to find class '%s'", self_name);
2285     }
2286   layout_class_methods (self_type);
2287
2288   if (ID_INIT_P (method_name))
2289     method = lookup_java_constructor (self_type, method_signature);
2290   else
2291     method = lookup_java_method (self_type, method_name, method_signature);
2292
2293   /* We've found a method in an interface, but this isn't an interface
2294      call.  */
2295   if (opcode != OPCODE_invokeinterface
2296       && method
2297       && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
2298     method = NULL_TREE;
2299
2300   /* We've found a non-interface method but we are making an
2301      interface call.  This can happen if the interface overrides a
2302      method in Object.  */
2303   if (! flag_verify_invocations
2304       && opcode == OPCODE_invokeinterface
2305       && method
2306       && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
2307     method = NULL_TREE;
2308
2309   if (method == NULL_TREE)
2310     {
2311       if (flag_verify_invocations || ! flag_indirect_dispatch)
2312         {
2313           error ("class '%s' has no method named '%s' matching signature '%s'",
2314                  self_name,
2315                  IDENTIFIER_POINTER (method_name),
2316                  IDENTIFIER_POINTER (method_signature));
2317         }
2318       else
2319         {
2320           int flags = ACC_PUBLIC;
2321           if (opcode == OPCODE_invokestatic)
2322             flags |= ACC_STATIC;
2323           if (opcode == OPCODE_invokeinterface)
2324             {
2325               flags |= ACC_INTERFACE | ACC_ABSTRACT;
2326               CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
2327             }
2328           method = add_method (self_type, flags, method_name,
2329                                method_signature);
2330           DECL_ARTIFICIAL (method) = 1;
2331           METHOD_DUMMY (method) = 1;
2332           layout_class_method (self_type, NULL,
2333                                method, NULL);
2334         }
2335     }
2336
2337   /* Invoke static can't invoke static/abstract method */
2338   if (method != NULL_TREE)
2339     {
2340       if (opcode == OPCODE_invokestatic)
2341         {
2342           if (!METHOD_STATIC (method))
2343             {
2344               error ("invokestatic on non static method");
2345               method = NULL_TREE;
2346             }
2347           else if (METHOD_ABSTRACT (method))
2348             {
2349               error ("invokestatic on abstract method");
2350               method = NULL_TREE;
2351             }
2352         }
2353       else
2354         {
2355           if (METHOD_STATIC (method))
2356             {
2357               error ("invoke[non-static] on static method");
2358               method = NULL_TREE;
2359             }
2360         }
2361     }
2362
2363   if (method == NULL_TREE)
2364     {
2365       /* If we got here, we emitted an error message above.  So we
2366          just pop the arguments, push a properly-typed zero, and
2367          continue.  */
2368       method_type = get_type_from_signature (method_signature);
2369       pop_arguments (TYPE_ARG_TYPES (method_type));
2370       if (opcode != OPCODE_invokestatic) 
2371         pop_type (self_type);
2372       method_type = promote_type (TREE_TYPE (method_type));
2373       push_value (convert (method_type, integer_zero_node));
2374       return;
2375     }
2376
2377   method_type = TREE_TYPE (method);
2378   arg_list = pop_arguments (TYPE_ARG_TYPES (method_type));
2379   flush_quick_stack ();
2380
2381   func = NULL_TREE;
2382   if (opcode == OPCODE_invokestatic)
2383     func = build_known_method_ref (method, method_type, self_type,
2384                                    method_signature, arg_list);
2385   else if (opcode == OPCODE_invokespecial
2386            || (opcode == OPCODE_invokevirtual
2387                && (METHOD_PRIVATE (method)
2388                    || METHOD_FINAL (method) 
2389                    || CLASS_FINAL (TYPE_NAME (self_type)))))
2390     {
2391       /* If the object for the method call is null, we throw an
2392          exception.  We don't do this if the object is the current
2393          method's `this'.  In other cases we just rely on an
2394          optimization pass to eliminate redundant checks.  FIXME:
2395          Unfortunately there doesn't seem to be a way to determine
2396          what the current method is right now.
2397          We do omit the check if we're calling <init>.  */
2398       /* We use a SAVE_EXPR here to make sure we only evaluate
2399          the new `self' expression once.  */
2400       tree save_arg = save_expr (TREE_VALUE (arg_list));
2401       TREE_VALUE (arg_list) = save_arg;
2402       check = java_check_reference (save_arg, ! DECL_INIT_P (method));
2403       func = build_known_method_ref (method, method_type, self_type,
2404                                      method_signature, arg_list);
2405     }
2406   else
2407     {
2408       tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface, 
2409                                          arg_list);
2410       if (opcode == OPCODE_invokevirtual)
2411         func = build_invokevirtual (dtable, method);
2412       else
2413         func = build_invokeinterface (dtable, method);
2414     }
2415       
2416   if (TREE_CODE (func) == ADDR_EXPR)
2417     TREE_TYPE (func) = build_pointer_type (method_type);
2418   else
2419     func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
2420
2421   call = build3 (CALL_EXPR, TREE_TYPE (method_type),
2422                  func, arg_list, NULL_TREE);
2423   TREE_SIDE_EFFECTS (call) = 1;
2424   call = check_for_builtin (method, call);
2425
2426   if (check != NULL_TREE)
2427     {
2428       call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
2429       TREE_SIDE_EFFECTS (call) = 1;
2430     }
2431
2432   if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
2433     java_add_stmt (call);
2434   else
2435     {
2436       push_value (call);
2437       flush_quick_stack ();
2438     }
2439 }
2440
2441 /* Create a stub which will be put into the vtable but which will call
2442    a JNI function.  */
2443
2444 tree
2445 build_jni_stub (tree method)
2446 {
2447   tree jnifunc, call, args, body, lookup_arg, method_sig, arg_types;
2448   tree jni_func_type, tem;
2449   tree env_var, res_var = NULL_TREE, block;
2450   tree method_args, res_type;
2451   tree meth_var;
2452   tree bind;
2453
2454   int args_size = 0;
2455
2456   tree klass = DECL_CONTEXT (method);
2457   int from_class = ! CLASS_FROM_SOURCE_P (klass);
2458   klass = build_class_ref (klass);
2459
2460   if (! METHOD_NATIVE (method) || ! flag_jni)
2461     abort ();
2462
2463   DECL_ARTIFICIAL (method) = 1;
2464   DECL_EXTERNAL (method) = 0;
2465
2466   env_var = build_decl (VAR_DECL, get_identifier ("env"), ptr_type_node);
2467   DECL_CONTEXT (env_var) = method;
2468
2469   if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
2470     {
2471       res_var = build_decl (VAR_DECL, get_identifier ("res"),
2472                             TREE_TYPE (TREE_TYPE (method)));
2473       DECL_CONTEXT (res_var) = method;
2474       TREE_CHAIN (env_var) = res_var;
2475     }
2476
2477   meth_var = build_decl (VAR_DECL, get_identifier ("meth"), ptr_type_node);
2478   TREE_STATIC (meth_var) = 1;
2479   TREE_PUBLIC (meth_var) = 0;
2480   DECL_EXTERNAL (meth_var) = 0;
2481   DECL_CONTEXT (meth_var) = method;
2482   DECL_ARTIFICIAL (meth_var) = 1;
2483   DECL_INITIAL (meth_var) = null_pointer_node;
2484   TREE_USED (meth_var) = 1;
2485   chainon (env_var, meth_var);
2486   build_result_decl (method);
2487
2488   /* One strange way that the front ends are different is that they
2489      store arguments differently.  */
2490   if (from_class)
2491     method_args = DECL_ARGUMENTS (method);
2492   else
2493     method_args = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (method));
2494   block = build_block (env_var, NULL_TREE, NULL_TREE,
2495                        method_args, NULL_TREE);
2496   TREE_SIDE_EFFECTS (block) = 1;
2497   /* When compiling from source we don't set the type of the block,
2498      because that will prevent patch_return from ever being run.  */
2499   if (from_class)
2500     TREE_TYPE (block) = TREE_TYPE (TREE_TYPE (method));
2501
2502   /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame.  */
2503   body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
2504                  build3 (CALL_EXPR, ptr_type_node,
2505                          build_address_of (soft_getjnienvnewframe_node),
2506                          build_tree_list (NULL_TREE, klass),
2507                          NULL_TREE));
2508   CAN_COMPLETE_NORMALLY (body) = 1;
2509
2510   /* All the arguments to this method become arguments to the
2511      underlying JNI function.  If we had to wrap object arguments in a
2512      special way, we would do that here.  */
2513   args = NULL_TREE;
2514   for (tem = method_args; tem != NULL_TREE; tem = TREE_CHAIN (tem))
2515     {
2516       int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (tem)));
2517 #ifdef PARM_BOUNDARY
2518       arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
2519                   * PARM_BOUNDARY);
2520 #endif
2521       args_size += (arg_bits / BITS_PER_UNIT);
2522
2523       args = tree_cons (NULL_TREE, tem, args);
2524     }
2525   args = nreverse (args);
2526   arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
2527
2528   /* For a static method the second argument is the class.  For a
2529      non-static method the second argument is `this'; that is already
2530      available in the argument list.  */
2531   if (METHOD_STATIC (method))
2532     {
2533       args_size += int_size_in_bytes (TREE_TYPE (klass));
2534       args = tree_cons (NULL_TREE, klass, args);
2535       arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
2536     }
2537
2538   /* The JNIEnv structure is the first argument to the JNI function.  */
2539   args_size += int_size_in_bytes (TREE_TYPE (env_var));
2540   args = tree_cons (NULL_TREE, env_var, args);
2541   arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
2542
2543   /* We call _Jv_LookupJNIMethod to find the actual underlying
2544      function pointer.  _Jv_LookupJNIMethod will throw the appropriate
2545      exception if this function is not found at runtime.  */
2546   tem = build_tree_list (NULL_TREE, build_int_cst (NULL_TREE, args_size));
2547   method_sig = build_java_signature (TREE_TYPE (method));
2548   lookup_arg = tree_cons (NULL_TREE,
2549                           build_utf8_ref (unmangle_classname
2550                                           (IDENTIFIER_POINTER (method_sig),
2551                                            IDENTIFIER_LENGTH (method_sig))), 
2552                           tem);
2553   tem = DECL_NAME (method);
2554   lookup_arg
2555     = tree_cons (NULL_TREE, klass,
2556                  tree_cons (NULL_TREE, build_utf8_ref (tem), lookup_arg));
2557   
2558   tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
2559
2560 #ifdef MODIFY_JNI_METHOD_CALL
2561   tem = MODIFY_JNI_METHOD_CALL (tem);
2562 #endif
2563
2564   jni_func_type = build_pointer_type (tem);
2565
2566   jnifunc = build3 (COND_EXPR, ptr_type_node,
2567                     meth_var, meth_var,
2568                     build2 (MODIFY_EXPR, ptr_type_node, meth_var,
2569                             build3 (CALL_EXPR, ptr_type_node,
2570                                     build_address_of
2571                                       (soft_lookupjnimethod_node),
2572                                     lookup_arg, NULL_TREE)));
2573
2574   /* Now we make the actual JNI call via the resulting function
2575      pointer.    */
2576   call = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (method)),
2577                  build1 (NOP_EXPR, jni_func_type, jnifunc),
2578                  args, NULL_TREE);
2579
2580   /* If the JNI call returned a result, capture it here.  If we had to
2581      unwrap JNI object results, we would do that here.  */
2582   if (res_var != NULL_TREE)
2583     call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
2584                    res_var, call);
2585
2586   TREE_SIDE_EFFECTS (call) = 1;
2587   CAN_COMPLETE_NORMALLY (call) = 1;
2588
2589   body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2590   TREE_SIDE_EFFECTS (body) = 1;
2591
2592   /* Now free the environment we allocated.  */
2593   call = build3 (CALL_EXPR, ptr_type_node,
2594                  build_address_of (soft_jnipopsystemframe_node),
2595                  build_tree_list (NULL_TREE, env_var),
2596                  NULL_TREE);
2597   TREE_SIDE_EFFECTS (call) = 1;
2598   CAN_COMPLETE_NORMALLY (call) = 1;
2599   body = build2 (COMPOUND_EXPR, void_type_node, body, call);
2600   TREE_SIDE_EFFECTS (body) = 1;
2601
2602   /* Finally, do the return.  */
2603   res_type = void_type_node;
2604   if (res_var != NULL_TREE)
2605     {
2606       tree drt;
2607       if (! DECL_RESULT (method))
2608         abort ();
2609       /* Make sure we copy the result variable to the actual
2610          result.  We use the type of the DECL_RESULT because it
2611          might be different from the return type of the function:
2612          it might be promoted.  */
2613       drt = TREE_TYPE (DECL_RESULT (method));
2614       if (drt != TREE_TYPE (res_var))
2615         res_var = build1 (CONVERT_EXPR, drt, res_var);
2616       res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
2617       TREE_SIDE_EFFECTS (res_var) = 1;
2618     }
2619
2620   body = build2 (COMPOUND_EXPR, void_type_node, body,
2621                  build1 (RETURN_EXPR, res_type, res_var));
2622   TREE_SIDE_EFFECTS (body) = 1;
2623   
2624   bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block), 
2625                  body, block);
2626   return bind;
2627 }
2628
2629 /* Expand an operation to extract from or store into a field.
2630    IS_STATIC is 1 iff the field is static.
2631    IS_PUTTING is 1 for putting into a field;  0 for getting from the field.
2632    FIELD_REF_INDEX is an index into the constant pool.  */
2633
2634 static void
2635 expand_java_field_op (int is_static, int is_putting, int field_ref_index)
2636 {
2637   tree self_type
2638     = get_class_constant (current_jcf,
2639                           COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
2640                           field_ref_index));
2641   const char *self_name
2642     = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
2643   tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
2644   tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool, 
2645                                                   field_ref_index);
2646   tree field_type = get_type_from_signature (field_signature);
2647   tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
2648   tree field_ref;
2649   int is_error = 0;
2650   tree original_self_type = self_type;
2651   tree field_decl;
2652   
2653   if (! CLASS_LOADED_P (self_type))
2654     load_class (self_type, 1);  
2655   field_decl = lookup_field (&self_type, field_name);
2656   if (field_decl == error_mark_node)
2657     {
2658       is_error = 1;
2659     }
2660   else if (field_decl == NULL_TREE)
2661     {
2662       if (! flag_verify_invocations)
2663         {
2664           int flags = ACC_PUBLIC;
2665           if (is_static)
2666             flags |= ACC_STATIC;
2667           self_type = original_self_type;
2668           field_decl = add_field (original_self_type, field_name,
2669                                   field_type, flags); 
2670           DECL_ARTIFICIAL (field_decl) = 1;
2671           DECL_IGNORED_P (field_decl) = 1;
2672         }
2673       else
2674         {      
2675           error ("missing field '%s' in '%s'",
2676                  IDENTIFIER_POINTER (field_name), self_name);
2677           is_error = 1;
2678       }
2679     }
2680   else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
2681     {
2682       error ("mismatching signature for field '%s' in '%s'",
2683              IDENTIFIER_POINTER (field_name), self_name);
2684       is_error = 1;
2685     }
2686   field_ref = is_static ? NULL_TREE : pop_value (self_type);
2687   if (is_error)
2688     {
2689       if (! is_putting)
2690         push_value (convert (field_type, integer_zero_node));
2691       flush_quick_stack ();
2692       return;
2693     }
2694
2695   field_ref = build_field_ref (field_ref, self_type, field_name);
2696   if (is_static)
2697     field_ref = build_class_init (self_type, field_ref);
2698   if (is_putting)
2699     {
2700       flush_quick_stack ();
2701       if (FIELD_FINAL (field_decl))
2702         {
2703           if (DECL_CONTEXT (field_decl) != current_class)
2704             error ("%Jassignment to final field '%D' not in field's class",
2705                    field_decl, field_decl);
2706           else if (FIELD_STATIC (field_decl))
2707             {
2708               if (!DECL_CLINIT_P (current_function_decl))
2709                 warning ("%Jassignment to final static field %qD not in "
2710                          "class initializer",
2711                          field_decl, field_decl);
2712             }
2713           else
2714             {
2715               tree cfndecl_name = DECL_NAME (current_function_decl);
2716               if (! DECL_CONSTRUCTOR_P (current_function_decl)
2717                   && !ID_FINIT_P (cfndecl_name))
2718                 warning ("%Jassignment to final field '%D' not in constructor",
2719                          field_decl, field_decl);
2720             }
2721         }
2722       java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
2723                              field_ref, new_value));
2724     }
2725   else
2726     push_value (field_ref);
2727 }
2728
2729 void
2730 load_type_state (tree label)
2731 {
2732   int i;
2733   tree vec = LABEL_TYPE_STATE (label);
2734   int cur_length = TREE_VEC_LENGTH (vec);
2735   stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
2736   for (i = 0; i < cur_length; i++)
2737     type_map [i] = TREE_VEC_ELT (vec, i);
2738 }
2739
2740 /* Go over METHOD's bytecode and note instruction starts in
2741    instruction_bits[].  */
2742
2743 void
2744 note_instructions (JCF *jcf, tree method)
2745 {
2746   int PC; 
2747   unsigned char* byte_ops;
2748   long length = DECL_CODE_LENGTH (method);
2749
2750   int saw_index;
2751   jint INT_temp;
2752
2753 #undef RET /* Defined by config/i386/i386.h */
2754 #undef PTR
2755 #define BCODE byte_ops
2756 #define BYTE_type_node byte_type_node
2757 #define SHORT_type_node short_type_node
2758 #define INT_type_node int_type_node
2759 #define LONG_type_node long_type_node
2760 #define CHAR_type_node char_type_node
2761 #define PTR_type_node ptr_type_node
2762 #define FLOAT_type_node float_type_node
2763 #define DOUBLE_type_node double_type_node
2764 #define VOID_type_node void_type_node
2765 #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2766 #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2767 #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
2768 #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
2769
2770 #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
2771
2772   JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2773   byte_ops = jcf->read_ptr;
2774   instruction_bits = xrealloc (instruction_bits, length + 1);
2775   memset (instruction_bits, 0, length + 1);
2776
2777   /* This pass figures out which PC can be the targets of jumps. */
2778   for (PC = 0; PC < length;)
2779     {
2780       int oldpc = PC; /* PC at instruction start. */
2781       instruction_bits [PC] |=  BCODE_INSTRUCTION_START;
2782       switch (byte_ops[PC++])
2783         {
2784 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
2785         case OPCODE: \
2786           PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
2787           break;
2788
2789 #define NOTE_LABEL(PC) note_label(oldpc, PC)
2790
2791 #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2792 #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2793 #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
2794 #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2795 #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2796 #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2797 #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2798 #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2799
2800 #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
2801   PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
2802 #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
2803   ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
2804 #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
2805 #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
2806 #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
2807 #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
2808
2809 /* two forms of wide instructions */
2810 #define PRE_SPECIAL_WIDE(IGNORE) \
2811   { \
2812     int modified_opcode = IMMEDIATE_u1; \
2813     if (modified_opcode == OPCODE_iinc) \
2814       { \
2815         (void) IMMEDIATE_u2;    /* indexbyte1 and indexbyte2 */ \
2816         (void) IMMEDIATE_s2;    /* constbyte1 and constbyte2 */ \
2817       } \
2818     else \
2819       { \
2820         (void) IMMEDIATE_u2;    /* indexbyte1 and indexbyte2 */ \
2821       } \
2822   }
2823
2824 #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
2825
2826 #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2827
2828 #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
2829 #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
2830           PRE_ARRAY_##SUBOP(OPERAND_TYPE)
2831 #define PRE_ARRAY_LOAD(TYPE) /* nothing */
2832 #define PRE_ARRAY_STORE(TYPE) /* nothing */
2833 #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
2834 #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
2835 #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
2836 #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
2837 #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
2838
2839 #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2840 #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
2841 #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
2842   saw_index = 0;  INT_temp = (OPERAND_VALUE); \
2843   if (!saw_index)  NOTE_LABEL(oldpc + INT_temp);
2844 #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
2845   saw_index = 0;  INT_temp = (OPERAND_VALUE); \
2846   NOTE_LABEL (PC); \
2847   if (!saw_index)  NOTE_LABEL(oldpc + INT_temp);
2848
2849 #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE)  (void)(OPERAND_VALUE)
2850
2851 #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
2852   PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
2853
2854 #define PRE_LOOKUP_SWITCH                                               \
2855   { jint default_offset = IMMEDIATE_s4;  jint npairs = IMMEDIATE_s4;    \
2856     NOTE_LABEL (default_offset+oldpc);                                  \
2857     if (npairs >= 0)                                                    \
2858       while (--npairs >= 0) {                                           \
2859        jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4;                      \
2860        jint offset = IMMEDIATE_s4;                                      \
2861        NOTE_LABEL (offset+oldpc); }                                     \
2862   }
2863
2864 #define PRE_TABLE_SWITCH                                \
2865   { jint default_offset = IMMEDIATE_s4;                 \
2866     jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4;  \
2867     NOTE_LABEL (default_offset+oldpc);                  \
2868     if (low <= high)                                    \
2869      while (low++ <= high) {                            \
2870        jint offset = IMMEDIATE_s4;                      \
2871        NOTE_LABEL (offset+oldpc); }                     \
2872   }
2873
2874 #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2875 #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
2876 #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
2877   (void)(IMMEDIATE_u2); \
2878   PC += 2 * IS_INTERFACE /* for invokeinterface */;
2879
2880 #include "javaop.def"
2881 #undef JAVAOP
2882         }
2883     } /* for */
2884 }
2885
2886 void
2887 expand_byte_code (JCF *jcf, tree method)
2888 {
2889   int PC;
2890   int i;
2891   const unsigned char *linenumber_pointer;
2892   int dead_code_index = -1;
2893   unsigned char* byte_ops;
2894   long length = DECL_CODE_LENGTH (method);
2895
2896   stack_pointer = 0;
2897   JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
2898   byte_ops = jcf->read_ptr;
2899
2900   /* We make an initial pass of the line number table, to note
2901      which instructions have associated line number entries. */
2902   linenumber_pointer = linenumber_table;
2903   for (i = 0; i < linenumber_count; i++)
2904     {
2905       int pc = GET_u2 (linenumber_pointer);
2906       linenumber_pointer += 4;
2907       if (pc >= length)
2908         warning ("invalid PC in line number table");
2909       else
2910         {
2911           if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
2912             instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
2913           instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
2914         }
2915     }  
2916
2917   if (flag_new_verifier)
2918     {
2919       if (! verify_jvm_instructions_new (jcf, byte_ops, length))
2920         return;
2921     }
2922   else
2923     {
2924       if (! verify_jvm_instructions (jcf, byte_ops, length))
2925         return;
2926     }
2927
2928   /* Translate bytecodes.  */
2929   linenumber_pointer = linenumber_table;
2930   for (PC = 0; PC < length;)
2931     {
2932       if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
2933         {
2934           tree label = lookup_label (PC);
2935           flush_quick_stack ();
2936           if ((instruction_bits [PC] & BCODE_TARGET) != 0)
2937             java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
2938           if (LABEL_VERIFIED (label) || PC == 0)
2939             load_type_state (label);
2940         }
2941
2942       if (! (instruction_bits [PC] & BCODE_VERIFIED))
2943         {
2944           if (dead_code_index == -1)
2945             {
2946               /* This is the start of a region of unreachable bytecodes.
2947                  They still need to be processed in order for EH ranges
2948                  to get handled correctly.  However, we can simply
2949                  replace these bytecodes with nops.  */
2950               dead_code_index = PC;
2951             }
2952           
2953           /* Turn this bytecode into a nop.  */
2954           byte_ops[PC] = 0x0;
2955         }
2956        else
2957         {
2958           if (dead_code_index != -1)
2959             {
2960               /* We've just reached the end of a region of dead code.  */
2961               if (extra_warnings)
2962                 warning ("unreachable bytecode from %d to before %d",
2963                          dead_code_index, PC);
2964               dead_code_index = -1;
2965             }
2966         }
2967
2968       /* Handle possible line number entry for this PC.
2969
2970          This code handles out-of-order and multiple linenumbers per PC,
2971          but is optimized for the case of line numbers increasing
2972          monotonically with PC. */
2973       if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
2974         {
2975           if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
2976               || GET_u2 (linenumber_pointer) != PC)
2977             linenumber_pointer = linenumber_table;
2978           while (linenumber_pointer < linenumber_table + linenumber_count * 4)
2979             {
2980               int pc = GET_u2 (linenumber_pointer);
2981               linenumber_pointer += 4;
2982               if (pc == PC)
2983                 {
2984                   int line = GET_u2 (linenumber_pointer - 2);
2985 #ifdef USE_MAPPED_LOCATION
2986                   input_location = linemap_line_start (&line_table, line, 1);
2987 #else
2988                   input_location.line = line;
2989 #endif
2990                   if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
2991                     break;
2992                 }
2993             }
2994         }
2995       maybe_pushlevels (PC);
2996       PC = process_jvm_instruction (PC, byte_ops, length);
2997       maybe_poplevels (PC);
2998     } /* for */
2999   
3000   if (dead_code_index != -1)
3001     {
3002       /* We've just reached the end of a region of dead code.  */
3003       if (extra_warnings)
3004         warning ("unreachable bytecode from %d to the end of the method", 
3005                  dead_code_index);
3006     }
3007 }
3008
3009 static void
3010 java_push_constant_from_pool (JCF *jcf, int index)
3011 {
3012   tree c;
3013   if (JPOOL_TAG (jcf, index) == CONSTANT_String)
3014     {
3015       tree name;
3016       name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
3017       index = alloc_name_constant (CONSTANT_String, name);
3018       c = build_ref_from_constant_pool (index);
3019       c = convert (promote_type (string_type_node), c);
3020     }
3021   else
3022     c = get_constant (jcf, index);
3023   push_value (c);
3024
3025
3026 int
3027 process_jvm_instruction (int PC, const unsigned char* byte_ops,
3028                          long length ATTRIBUTE_UNUSED)
3029
3030   const char *opname; /* Temporary ??? */
3031   int oldpc = PC; /* PC at instruction start. */
3032
3033   /* If the instruction is at the beginning of a exception handler,
3034      replace the top of the stack with the thrown object reference */
3035   if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
3036     {
3037       /* Note that the new verifier will not emit a type map at all
3038          for dead exception handlers.  In this case we just ignore
3039          the situation.  */
3040       if (! flag_new_verifier || (instruction_bits[PC] & BCODE_VERIFIED) != 0)
3041         {
3042           tree type = pop_type (promote_type (throwable_type_node));
3043           push_value (build_exception_object_ref (type));
3044         }
3045     }
3046
3047   switch (byte_ops[PC++])
3048     {
3049 #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
3050     case OPCODE: \
3051       opname = #OPNAME; \
3052       OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
3053       break;
3054
3055 #define RET(OPERAND_TYPE, OPERAND_VALUE)                                \
3056   {                                                                     \
3057     int saw_index = 0;                                                  \
3058     int index     = OPERAND_VALUE;                                      \
3059     build_java_ret                                                      \
3060       (find_local_variable (index, return_address_type_node, oldpc));   \
3061   }
3062
3063 #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
3064   {                                                 \
3065     /* OPERAND_VALUE may have side-effects on PC */ \
3066     int opvalue = OPERAND_VALUE;                    \
3067     build_java_jsr (oldpc + opvalue, PC);           \
3068   }
3069
3070 /* Push a constant onto the stack. */
3071 #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
3072   { int saw_index = 0;  int ival = (OPERAND_VALUE); \
3073     if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
3074     else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
3075
3076 /* internal macro added for use by the WIDE case */
3077 #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
3078   expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
3079
3080 /* Push local variable onto the opcode stack. */
3081 #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
3082   { \
3083     /* have to do this since OPERAND_VALUE may have side-effects */ \
3084     int opvalue = OPERAND_VALUE; \
3085     LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3086   }
3087
3088 #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
3089   expand_java_return (OPERAND_TYPE##_type_node)
3090
3091 #define REM_EXPR TRUNC_MOD_EXPR
3092 #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
3093   expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
3094
3095 #define FIELD(IS_STATIC, IS_PUT) \
3096   expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
3097
3098 #define TEST(OPERAND_TYPE, CONDITION) \
3099   expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3100
3101 #define COND(OPERAND_TYPE, CONDITION) \
3102   expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
3103
3104 #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
3105   BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
3106
3107 #define BRANCH_GOTO(OPERAND_VALUE) \
3108   expand_java_goto (oldpc + OPERAND_VALUE)
3109
3110 #define BRANCH_CALL(OPERAND_VALUE) \
3111   expand_java_call (oldpc + OPERAND_VALUE, oldpc)
3112
3113 #if 0
3114 #define BRANCH_RETURN(OPERAND_VALUE) \
3115   { \
3116     tree type = OPERAND_TYPE##_type_node; \
3117     tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
3118     expand_java_ret (value); \
3119   }
3120 #endif
3121
3122 #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
3123           fprintf (stderr, "%3d: %s ", oldpc, opname); \
3124           fprintf (stderr, "(not implemented)\n")
3125 #define NOT_IMPL1(OPERAND_VALUE) \
3126           fprintf (stderr, "%3d: %s ", oldpc, opname); \
3127           fprintf (stderr, "(not implemented)\n")
3128
3129 #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
3130
3131 #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
3132
3133 #define STACK_POP(COUNT) java_stack_pop (COUNT)
3134
3135 #define STACK_SWAP(COUNT) java_stack_swap()
3136
3137 #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
3138 #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
3139 #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
3140
3141 #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
3142   PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
3143
3144 #define LOOKUP_SWITCH \
3145   { jint default_offset = IMMEDIATE_s4;  jint npairs = IMMEDIATE_s4; \
3146     tree selector = pop_value (INT_type_node); \
3147     tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3148     while (--npairs >= 0) \
3149       { \
3150         jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
3151         expand_java_add_case (switch_expr, match, oldpc + offset); \
3152       } \
3153   }
3154
3155 #define TABLE_SWITCH \
3156   { jint default_offset = IMMEDIATE_s4; \
3157     jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
3158     tree selector = pop_value (INT_type_node); \
3159     tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
3160     for (; low <= high; low++) \
3161       { \
3162         jint offset = IMMEDIATE_s4; \
3163         expand_java_add_case (switch_expr, low, oldpc + offset); \
3164       } \
3165   }
3166
3167 #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
3168   { int opcode = byte_ops[PC-1]; \
3169     int method_ref_index = IMMEDIATE_u2; \
3170     int nargs; \
3171     if (IS_INTERFACE) { nargs = IMMEDIATE_u1;  (void) IMMEDIATE_u1; } \
3172     else nargs = -1; \
3173     expand_invoke (opcode, method_ref_index, nargs); \
3174   }
3175
3176 /* Handle new, checkcast, instanceof */
3177 #define OBJECT(TYPE, OP) \
3178   expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
3179
3180 #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
3181
3182 #define ARRAY_LOAD(OPERAND_TYPE)                        \
3183   {                                                     \
3184     expand_java_arrayload( OPERAND_TYPE##_type_node );  \
3185   }
3186
3187 #define ARRAY_STORE(OPERAND_TYPE)                       \
3188   {                                                     \
3189     expand_java_arraystore( OPERAND_TYPE##_type_node ); \
3190   }
3191
3192 #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
3193 #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
3194 #define ARRAY_NEW_PTR()                                                 \
3195     push_value (build_anewarray (get_class_constant (current_jcf,       \
3196                                                      IMMEDIATE_u2),     \
3197                                  pop_value (int_type_node)));
3198 #define ARRAY_NEW_NUM()                         \
3199   {                                             \
3200     int atype = IMMEDIATE_u1;                   \
3201     push_value (build_newarray (atype, pop_value (int_type_node)));\
3202   }
3203 #define ARRAY_NEW_MULTI()                                       \
3204   {                                                             \
3205     tree class = get_class_constant (current_jcf, IMMEDIATE_u2 );       \
3206     int  ndims = IMMEDIATE_u1;                                  \
3207     expand_java_multianewarray( class, ndims );                 \
3208   }
3209
3210 #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
3211   push_value (fold (build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
3212                             pop_value (OPERAND_TYPE##_type_node))));
3213
3214 #define CONVERT2(FROM_TYPE, TO_TYPE)                                     \
3215   {                                                                      \
3216     push_value (build1 (NOP_EXPR, int_type_node,                         \
3217                         (convert (TO_TYPE##_type_node,                   \
3218                                   pop_value (FROM_TYPE##_type_node))))); \
3219   }
3220
3221 #define CONVERT(FROM_TYPE, TO_TYPE)                             \
3222   {                                                             \
3223     push_value (convert (TO_TYPE##_type_node,                   \
3224                          pop_value (FROM_TYPE##_type_node)));   \
3225   }
3226
3227 /* internal macro added for use by the WIDE case 
3228    Added TREE_TYPE (decl) assignment, apbianco  */
3229 #define STORE_INTERNAL(OPTYPE, OPVALUE)                         \
3230   {                                                             \
3231     tree decl, value;                                           \
3232     int index = OPVALUE;                                        \
3233     tree type = OPTYPE;                                         \
3234     value = pop_value (type);                                   \
3235     type = TREE_TYPE (value);                                   \
3236     decl = find_local_variable (index, type, oldpc);            \
3237     set_local_type (index, type);                               \
3238     java_add_stmt (build2 (MODIFY_EXPR, type, decl, value));    \
3239     update_aliases (decl, index, PC);                           \
3240   }
3241
3242 #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
3243   { \
3244     /* have to do this since OPERAND_VALUE may have side-effects */ \
3245     int opvalue = OPERAND_VALUE; \
3246     STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
3247   }
3248
3249 #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
3250   SPECIAL_##INSTRUCTION(OPERAND_TYPE)
3251
3252 #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
3253 #define SPECIAL_EXIT(IGNORED)  MONITOR_OPERATION (soft_monitorexit_node)
3254
3255 #define MONITOR_OPERATION(call)                 \
3256   {                                             \
3257     tree o = pop_value (ptr_type_node);         \
3258     tree c;                                     \
3259     flush_quick_stack ();                       \
3260     c = build_java_monitor (call, o);           \
3261     TREE_SIDE_EFFECTS (c) = 1;                  \
3262     java_add_stmt (c);                          \
3263   }
3264
3265 #define SPECIAL_IINC(IGNORED) \
3266   { \
3267     unsigned int local_var_index = IMMEDIATE_u1; \
3268     int ival = IMMEDIATE_s1; \
3269     expand_iinc(local_var_index, ival, oldpc); \
3270   }
3271
3272 #define SPECIAL_WIDE(IGNORED) \
3273   { \
3274     int modified_opcode = IMMEDIATE_u1; \
3275     unsigned int local_var_index = IMMEDIATE_u2; \
3276     switch (modified_opcode) \
3277       { \
3278       case OPCODE_iinc: \
3279         { \
3280           int ival = IMMEDIATE_s2; \
3281           expand_iinc (local_var_index, ival, oldpc); \
3282           break; \
3283         } \
3284       case OPCODE_iload: \
3285       case OPCODE_lload: \
3286       case OPCODE_fload: \
3287       case OPCODE_dload: \
3288       case OPCODE_aload: \
3289         { \
3290           /* duplicate code from LOAD macro */ \
3291           LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
3292           break; \
3293         } \
3294       case OPCODE_istore: \
3295       case OPCODE_lstore: \
3296       case OPCODE_fstore: \
3297       case OPCODE_dstore: \
3298       case OPCODE_astore: \
3299         { \
3300           STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
3301           break; \
3302         } \
3303       default: \
3304         error ("unrecogized wide sub-instruction"); \
3305       } \
3306   }
3307
3308 #define SPECIAL_THROW(IGNORED) \
3309   build_java_athrow (pop_value (throwable_type_node))
3310
3311 #define SPECIAL_BREAK NOT_IMPL1
3312 #define IMPL          NOT_IMPL
3313
3314 #include "javaop.def"
3315 #undef JAVAOP
3316    default:
3317     fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
3318   }
3319   return PC;
3320 }
3321
3322 /* Return the opcode at PC in the code section pointed to by
3323    CODE_OFFSET.  */
3324
3325 static unsigned char
3326 peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
3327 {
3328   unsigned char opcode;
3329   long absolute_offset = (long)JCF_TELL (jcf);
3330
3331   JCF_SEEK (jcf, code_offset);
3332   opcode = jcf->read_ptr [pc];
3333   JCF_SEEK (jcf, absolute_offset);
3334   return opcode;
3335 }
3336
3337 /* Some bytecode compilers are emitting accurate LocalVariableTable
3338    attributes. Here's an example:
3339    
3340      PC   <t>store_<n>
3341      PC+1 ...
3342      
3343      Attribute "LocalVariableTable"
3344      slot #<n>: ... (PC: PC+1 length: L)
3345    
3346    This is accurate because the local in slot <n> really exists after
3347    the opcode at PC is executed, hence from PC+1 to PC+1+L.
3348
3349    This procedure recognizes this situation and extends the live range
3350    of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
3351    length of the store instruction.)
3352
3353    This function is used by `give_name_to_locals' so that a local's
3354    DECL features a DECL_LOCAL_START_PC such that the first related
3355    store operation will use DECL as a destination, not a unrelated
3356    temporary created for the occasion.
3357
3358    This function uses a global (instruction_bits) `note_instructions' should
3359    have allocated and filled properly.  */
3360
3361 int
3362 maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
3363                        int start_pc, int slot)
3364 {
3365   int first, index, opcode;
3366   int pc, insn_pc;
3367   int wide_found = 0;
3368
3369   if (!start_pc)
3370     return start_pc;
3371
3372   first = index = -1;
3373
3374   /* Find last previous instruction and remember it */
3375   for (pc = start_pc-1; pc; pc--) 
3376     if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
3377       break;
3378   insn_pc = pc;
3379
3380   /* Retrieve the instruction, handle `wide'. */  
3381   opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3382   if (opcode == OPCODE_wide)
3383     {
3384       wide_found = 1;
3385       opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
3386     }
3387
3388   switch (opcode)
3389     {
3390     case OPCODE_astore_0:
3391     case OPCODE_astore_1:
3392     case OPCODE_astore_2:
3393     case OPCODE_astore_3:
3394       first = OPCODE_astore_0;
3395       break;
3396
3397     case OPCODE_istore_0:
3398     case OPCODE_istore_1:
3399     case OPCODE_istore_2:
3400     case OPCODE_istore_3:
3401       first = OPCODE_istore_0;
3402       break;
3403       
3404     case OPCODE_lstore_0:
3405     case OPCODE_lstore_1:
3406     case OPCODE_lstore_2:
3407     case OPCODE_lstore_3:
3408       first = OPCODE_lstore_0;
3409       break;
3410
3411     case OPCODE_fstore_0:
3412     case OPCODE_fstore_1:
3413     case OPCODE_fstore_2:
3414     case OPCODE_fstore_3:
3415       first = OPCODE_fstore_0;
3416       break;
3417
3418     case OPCODE_dstore_0:
3419     case OPCODE_dstore_1:
3420     case OPCODE_dstore_2:
3421     case OPCODE_dstore_3:
3422       first = OPCODE_dstore_0;
3423       break;
3424
3425     case OPCODE_astore:
3426     case OPCODE_istore:
3427     case OPCODE_lstore:
3428     case OPCODE_fstore:
3429     case OPCODE_dstore:
3430       index = peek_opcode_at_pc (jcf, code_offset, pc);
3431       if (wide_found)
3432         {
3433           int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
3434           index = (other << 8) + index;
3435         }
3436       break;
3437     }
3438
3439   /* Now we decide: first >0 means we have a <t>store_<n>, index >0
3440      means we have a <t>store. */
3441   if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
3442     start_pc = insn_pc;
3443
3444   return start_pc;
3445 }
3446
3447 /* Force the (direct) sub-operands of NODE to be evaluated in left-to-right
3448    order, as specified by Java Language Specification.
3449
3450    The problem is that while expand_expr will evaluate its sub-operands in
3451    left-to-right order, for variables it will just return an rtx (i.e.
3452    an lvalue) for the variable (rather than an rvalue).  So it is possible
3453    that a later sub-operand will change the register, and when the
3454    actual operation is done, it will use the new value, when it should
3455    have used the original value.
3456
3457    We fix this by using save_expr.  This forces the sub-operand to be
3458    copied into a fresh virtual register,
3459
3460    For method invocation, we modify the arguments so that a
3461    left-to-right order evaluation is performed. Saved expressions
3462    will, in CALL_EXPR order, be reused when the call will be expanded.
3463 */
3464
3465 tree
3466 force_evaluation_order (tree node)
3467 {
3468   if (flag_syntax_only)
3469     return node;
3470   if (TREE_CODE (node) == CALL_EXPR
3471       || TREE_CODE (node) == NEW_CLASS_EXPR
3472       || (TREE_CODE (node) == COMPOUND_EXPR
3473           && TREE_CODE (TREE_OPERAND (node, 0)) == CALL_EXPR
3474           && TREE_CODE (TREE_OPERAND (node, 1)) == SAVE_EXPR)) 
3475     {
3476       tree arg, cmp;
3477
3478       arg = node;
3479       
3480       /* Position arg properly, account for wrapped around ctors. */
3481       if (TREE_CODE (node) == COMPOUND_EXPR)
3482         arg = TREE_OPERAND (node, 0);
3483       
3484       arg = TREE_OPERAND (arg, 1);
3485       
3486       /* An empty argument list is ok, just ignore it.  */
3487       if (!arg)
3488         return node;
3489
3490       /* Not having a list of arguments here is an error. */ 
3491       if (TREE_CODE (arg) != TREE_LIST)
3492         abort ();
3493
3494       /* This reverses the evaluation order. This is a desired effect. */
3495       for (cmp = NULL_TREE; arg; arg = TREE_CHAIN (arg))
3496         {
3497           tree saved = save_expr (force_evaluation_order (TREE_VALUE (arg)));
3498           cmp = (cmp == NULL_TREE ? saved :
3499                  build2 (COMPOUND_EXPR, void_type_node, cmp, saved));
3500           TREE_VALUE (arg) = saved;
3501         }
3502       
3503       if (cmp && TREE_CODE (cmp) == COMPOUND_EXPR)
3504         TREE_SIDE_EFFECTS (cmp) = 1;
3505
3506       if (cmp)
3507         {
3508           cmp = build2 (COMPOUND_EXPR, TREE_TYPE (node), cmp, node);
3509           if (TREE_TYPE (cmp) != void_type_node)
3510             cmp = save_expr (cmp);
3511           CAN_COMPLETE_NORMALLY (cmp) = CAN_COMPLETE_NORMALLY (node);
3512           TREE_SIDE_EFFECTS (cmp) = 1;
3513           node = cmp;
3514         }
3515     }
3516   return node;
3517 }
3518
3519 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
3520    location where an expression or an identifier were encountered. It
3521    is necessary for languages where the frontend parser will handle
3522    recursively more than one file (Java is one of them).  */
3523
3524 tree
3525 build_expr_wfl (tree node,
3526 #ifdef USE_MAPPED_LOCATION
3527                 source_location location
3528 #else
3529                 const char *file, int line, int col
3530 #endif
3531 )
3532 {
3533   tree wfl;
3534   static const char *last_file = 0;
3535   static tree last_filenode = NULL_TREE;
3536
3537 #ifdef USE_MAPPED_LOCATION
3538   wfl = make_node (EXPR_WITH_FILE_LOCATION);
3539   SET_EXPR_LOCATION (wfl, location);
3540 #else
3541   wfl = make_node (EXPR_WITH_FILE_LOCATION);
3542
3543   EXPR_WFL_SET_LINECOL (wfl, line, col);
3544   if (file != last_file)
3545     {
3546       last_file = file;
3547       last_filenode = file ? get_identifier (file) : NULL_TREE;
3548     }
3549   EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
3550 #endif
3551   EXPR_WFL_NODE (wfl) = node;
3552   if (node)
3553     {
3554       if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3555         TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3556       TREE_TYPE (wfl) = TREE_TYPE (node);
3557     }
3558
3559   return wfl;
3560 }
3561
3562 #ifdef USE_MAPPED_LOCATION
3563 tree
3564 expr_add_location (tree node, source_location location, bool statement)
3565 {
3566   tree wfl;
3567 #if 0
3568   /* FIXME. This optimization causes failures in code that expects an
3569      EXPR_WITH_FILE_LOCATION.  E.g. in resolve_qualified_expression_name. */
3570   if (node && ! (statement && flag_emit_class_files))
3571     {
3572       source_location node_loc = EXPR_LOCATION (node);
3573       if (node_loc == location || location == UNKNOWN_LOCATION)
3574         return node;
3575       if (node_loc == UNKNOWN_LOCATION
3576           && IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3577         {
3578           SET_EXPR_LOCATION (node, location);
3579           return node;
3580         }
3581     }
3582 #endif
3583   wfl = make_node (EXPR_WITH_FILE_LOCATION);
3584   SET_EXPR_LOCATION (wfl, location);
3585   EXPR_WFL_NODE (wfl) = node;
3586   if (statement && debug_info_level != DINFO_LEVEL_NONE)
3587     EXPR_WFL_EMIT_LINE_NOTE (wfl) = 1;
3588   if (node)
3589     {
3590       if (IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (node))))
3591         TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
3592       TREE_TYPE (wfl) = TREE_TYPE (node);
3593     }
3594
3595   return wfl;
3596 }
3597 #endif
3598
3599 /* Build a node to represent empty statements and blocks. */
3600
3601 tree
3602 build_java_empty_stmt (void)
3603 {
3604   tree t = build_empty_stmt ();
3605   CAN_COMPLETE_NORMALLY (t) = 1;
3606   return t;
3607 }
3608
3609 #include "gt-java-expr.h"