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