OSDN Git Service

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