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