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