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