OSDN Git Service

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