OSDN Git Service

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