OSDN Git Service

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