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