Daily bump.
[official-gcc.git] / gcc / java / except.c
bloba2bd0daf4e85a9f099c8e9edf840545b7cd3e24a
1 /* Handle exceptions for GNU compiler for the Java(TM) language.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
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. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "alias.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "java-tree.h"
34 #include "javaop.h"
35 #include "java-opcodes.h"
36 #include "jcf.h"
37 #include "java-except.h"
38 #include "diagnostic-core.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
43 static void expand_start_java_handler (struct eh_range *);
44 static struct eh_range *find_handler_in_range (int, struct eh_range *,
45 struct eh_range *);
46 static void check_start_handlers (struct eh_range *, int);
47 static void free_eh_ranges (struct eh_range *range);
49 struct eh_range *current_method_handlers;
51 struct eh_range *current_try_block = NULL;
53 /* These variables are used to speed up find_handler. */
55 static int cache_range_start, cache_range_end;
56 static struct eh_range *cache_range;
57 static struct eh_range *cache_next_child;
59 /* A dummy range that represents the entire method. */
61 struct eh_range whole_range;
63 /* Check the invariants of the structure we're using to contain
64 exception regions. Either returns true or fails an assertion
65 check. */
67 bool
68 sanity_check_exception_range (struct eh_range *range)
70 struct eh_range *ptr = range->first_child;
71 for (; ptr; ptr = ptr->next_sibling)
73 gcc_assert (ptr->outer == range
74 && ptr->end_pc > ptr->start_pc);
75 if (ptr->next_sibling)
76 gcc_assert (ptr->next_sibling->start_pc >= ptr->end_pc);
77 gcc_assert (ptr->start_pc >= ptr->outer->start_pc
78 && ptr->end_pc <= ptr->outer->end_pc);
79 (void) sanity_check_exception_range (ptr);
81 return true;
84 #if defined(DEBUG_JAVA_BINDING_LEVELS)
85 extern int is_class_level;
86 extern int current_pc;
87 extern int binding_depth;
88 extern void indent (void);
89 static void
90 print_ranges (struct eh_range *range)
92 if (! range)
93 return;
95 struct eh_range *child = range->first_child;
97 indent ();
98 fprintf (stderr, "handler pc %d --> %d ", range->start_pc, range->end_pc);
100 tree handler = range->handlers;
101 for ( ; handler != NULL_TREE; handler = TREE_CHAIN (handler))
103 tree type = TREE_PURPOSE (handler);
104 if (type == NULL)
105 type = throwable_type_node;
106 fprintf (stderr, " type=%s ", IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
108 fprintf (stderr, "\n");
110 int saved = binding_depth;
111 binding_depth++;
112 print_ranges (child);
113 binding_depth = saved;
115 print_ranges (range->next_sibling);
117 #endif
119 /* Search for the most specific eh_range containing PC.
120 Assume PC is within RANGE.
121 CHILD is a list of children of RANGE such that any
122 previous children have end_pc values that are too low. */
124 static struct eh_range *
125 find_handler_in_range (int pc, struct eh_range *range, struct eh_range *child)
127 for (; child != NULL; child = child->next_sibling)
129 if (pc < child->start_pc)
130 break;
131 if (pc < child->end_pc)
132 return find_handler_in_range (pc, child, child->first_child);
134 cache_range = range;
135 cache_range_start = pc;
136 cache_next_child = child;
137 cache_range_end = child == NULL ? range->end_pc : child->start_pc;
138 return range;
141 /* Find the inner-most handler that contains PC. */
143 struct eh_range *
144 find_handler (int pc)
146 struct eh_range *h;
147 if (pc >= cache_range_start)
149 h = cache_range;
150 if (pc < cache_range_end)
151 return h;
152 while (pc >= h->end_pc)
154 cache_next_child = h->next_sibling;
155 h = h->outer;
158 else
160 h = &whole_range;
161 cache_next_child = h->first_child;
163 return find_handler_in_range (pc, h, cache_next_child);
166 static void
167 free_eh_ranges (struct eh_range *range)
169 while (range)
171 struct eh_range *next = range->next_sibling;
172 free_eh_ranges (range->first_child);
173 if (range != &whole_range)
174 free (range);
175 range = next;
179 /* Called to re-initialize the exception machinery for a new method. */
181 void
182 method_init_exceptions (void)
184 free_eh_ranges (&whole_range);
185 whole_range.start_pc = 0;
186 whole_range.end_pc = DECL_CODE_LENGTH (current_function_decl) + 1;
187 whole_range.outer = NULL;
188 whole_range.first_child = NULL;
189 whole_range.next_sibling = NULL;
190 cache_range_start = 0xFFFFFF;
193 /* Split an exception range into two at PC. The sub-ranges that
194 belong to the range are split and distributed between the two new
195 ranges. */
197 static void
198 split_range (struct eh_range *range, int pc)
200 struct eh_range *ptr;
201 struct eh_range **first_child, **second_child;
202 struct eh_range *h;
204 /* First, split all the sub-ranges. */
205 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
207 if (pc > ptr->start_pc
208 && pc < ptr->end_pc)
210 split_range (ptr, pc);
214 /* Create a new range. */
215 h = XNEW (struct eh_range);
217 h->start_pc = pc;
218 h->end_pc = range->end_pc;
219 h->next_sibling = range->next_sibling;
220 range->next_sibling = h;
221 range->end_pc = pc;
222 h->handlers = build_tree_list (TREE_PURPOSE (range->handlers),
223 TREE_VALUE (range->handlers));
224 h->next_sibling = NULL;
225 h->expanded = 0;
226 h->stmt = NULL;
227 h->outer = range->outer;
228 h->first_child = NULL;
230 ptr = range->first_child;
231 first_child = &range->first_child;
232 second_child = &h->first_child;
234 /* Distribute the sub-ranges between the two new ranges. */
235 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
237 if (ptr->start_pc < pc)
239 *first_child = ptr;
240 ptr->outer = range;
241 first_child = &ptr->next_sibling;
243 else
245 *second_child = ptr;
246 ptr->outer = h;
247 second_child = &ptr->next_sibling;
250 *first_child = NULL;
251 *second_child = NULL;
255 /* Add an exception range.
257 There are some missed optimization opportunities here. For
258 example, some bytecode obfuscators generate seemingly
259 nonoverlapping exception ranges which, when coalesced, do in fact
260 nest correctly. We could merge these, but we'd have to fix up all
261 the enclosed regions first and perhaps create a new range anyway if
262 it overlapped existing ranges.
264 Also, we don't attempt to detect the case where two previously
265 added disjoint ranges could be coalesced by a new range. */
267 void
268 add_handler (int start_pc, int end_pc, tree handler, tree type)
270 struct eh_range *ptr, *h;
271 struct eh_range **first_child, **prev;
273 /* First, split all the existing ranges that we need to enclose. */
274 for (ptr = whole_range.first_child; ptr; ptr = ptr->next_sibling)
276 if (start_pc > ptr->start_pc
277 && start_pc < ptr->end_pc)
279 split_range (ptr, start_pc);
282 if (end_pc > ptr->start_pc
283 && end_pc < ptr->end_pc)
285 split_range (ptr, end_pc);
288 if (ptr->start_pc >= end_pc)
289 break;
292 /* Create the new range. */
293 h = XNEW (struct eh_range);
294 first_child = &h->first_child;
296 h->start_pc = start_pc;
297 h->end_pc = end_pc;
298 h->first_child = NULL;
299 h->outer = NULL_EH_RANGE;
300 h->handlers = build_tree_list (type, handler);
301 h->next_sibling = NULL;
302 h->expanded = 0;
303 h->stmt = NULL;
305 /* Find every range at the top level that will be a sub-range of the
306 range we're inserting and make it so. */
308 struct eh_range **prev = &whole_range.first_child;
309 for (ptr = *prev; ptr;)
311 struct eh_range *next = ptr->next_sibling;
313 if (ptr->start_pc >= end_pc)
314 break;
316 if (ptr->start_pc < start_pc)
318 prev = &ptr->next_sibling;
320 else if (ptr->start_pc >= start_pc
321 && ptr->start_pc < end_pc)
323 *prev = next;
324 *first_child = ptr;
325 first_child = &ptr->next_sibling;
326 ptr->outer = h;
327 ptr->next_sibling = NULL;
330 ptr = next;
334 /* Find the right place to insert the new range. */
335 prev = &whole_range.first_child;
336 for (ptr = *prev; ptr; prev = &ptr->next_sibling, ptr = ptr->next_sibling)
338 gcc_assert (ptr->outer == NULL_EH_RANGE);
339 if (ptr->start_pc >= start_pc)
340 break;
343 /* And insert it there. */
344 *prev = h;
345 if (ptr)
347 h->next_sibling = ptr;
348 h->outer = ptr->outer;
353 /* if there are any handlers for this range, issue start of region */
354 static void
355 expand_start_java_handler (struct eh_range *range)
357 #if defined(DEBUG_JAVA_BINDING_LEVELS)
358 indent ();
359 fprintf (stderr, "expand start handler pc %d --> %d\n",
360 current_pc, range->end_pc);
361 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
362 pushlevel (0);
363 register_exception_range (range, range->start_pc, range->end_pc);
364 range->expanded = 1;
367 tree
368 prepare_eh_table_type (tree type)
370 tree exp;
371 tree *slot;
372 const char *name;
373 char *buf;
374 tree decl;
375 tree utf8_ref;
377 /* The "type" (match_info) in a (Java) exception table is a pointer to:
378 * a) NULL - meaning match any type in a try-finally.
379 * b) a pointer to a pointer to a class.
380 * c) a pointer to a pointer to a utf8_ref. The pointer is
381 * rewritten to point to the appropriate class. */
383 if (type == NULL_TREE)
384 return NULL_TREE;
386 if (TYPE_TO_RUNTIME_MAP (output_class) == NULL)
387 TYPE_TO_RUNTIME_MAP (output_class) = java_treetreehash_create (10);
389 slot = java_treetreehash_new (TYPE_TO_RUNTIME_MAP (output_class), type);
390 if (*slot != NULL)
391 return TREE_VALUE (*slot);
393 if (is_compiled_class (type) && !flag_indirect_dispatch)
395 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
396 buf = (char *) alloca (strlen (name) + 5);
397 sprintf (buf, "%s_ref", name);
398 decl = build_decl (input_location,
399 VAR_DECL, get_identifier (buf), ptr_type_node);
400 TREE_STATIC (decl) = 1;
401 DECL_ARTIFICIAL (decl) = 1;
402 DECL_IGNORED_P (decl) = 1;
403 TREE_READONLY (decl) = 1;
404 TREE_THIS_VOLATILE (decl) = 0;
405 DECL_INITIAL (decl) = build_class_ref (type);
406 layout_decl (decl, 0);
407 pushdecl (decl);
408 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (decl)), decl);
410 else
412 utf8_ref = build_utf8_ref (DECL_NAME (TYPE_NAME (type)));
413 name = IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (utf8_ref, 0)));
414 buf = (char *) alloca (strlen (name) + 5);
415 sprintf (buf, "%s_ref", name);
416 decl = build_decl (input_location,
417 VAR_DECL, get_identifier (buf), utf8const_ptr_type);
418 TREE_STATIC (decl) = 1;
419 DECL_ARTIFICIAL (decl) = 1;
420 DECL_IGNORED_P (decl) = 1;
421 TREE_READONLY (decl) = 1;
422 TREE_THIS_VOLATILE (decl) = 0;
423 layout_decl (decl, 0);
424 pushdecl (decl);
425 exp = build1 (ADDR_EXPR, build_pointer_type (utf8const_ptr_type), decl);
426 CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (output_class),
427 NULL_TREE,
428 make_catch_class_record (exp, utf8_ref));
431 exp = convert (ptr_type_node, exp);
433 *slot = tree_cons (type, exp, NULL_TREE);
435 return exp;
439 expand_catch_class (treetreehash_entry **entry, int)
441 struct treetreehash_entry *ite = *entry;
442 tree addr = TREE_VALUE ((tree)ite->value);
443 tree decl;
444 STRIP_NOPS (addr);
445 decl = TREE_OPERAND (addr, 0);
446 rest_of_decl_compilation (decl, global_bindings_p (), 0);
447 return true;
450 /* For every class in the TYPE_TO_RUNTIME_MAP, expand the
451 corresponding object that is used by the runtime type matcher. */
453 void
454 java_expand_catch_classes (tree this_class)
456 if (TYPE_TO_RUNTIME_MAP (this_class))
457 TYPE_TO_RUNTIME_MAP (this_class)->traverse<int, expand_catch_class> (0);
460 /* Build and push the variable that will hold the exception object
461 within this function. */
463 static tree
464 build_exception_object_var (void)
466 tree decl = DECL_FUNCTION_EXC_OBJ (current_function_decl);
467 if (decl == NULL)
469 decl = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
470 VAR_DECL, get_identifier ("#exc_obj"), ptr_type_node);
471 DECL_IGNORED_P (decl) = 1;
472 DECL_ARTIFICIAL (decl) = 1;
474 DECL_FUNCTION_EXC_OBJ (current_function_decl) = decl;
475 pushdecl_function_level (decl);
477 return decl;
480 /* Build a reference to the jthrowable object being carried in the
481 exception header. */
483 tree
484 build_exception_object_ref (tree type)
486 tree obj;
488 /* Java only passes object via pointer and doesn't require adjusting.
489 The java object is immediately before the generic exception header. */
490 obj = build_exception_object_var ();
491 obj = fold_convert (build_pointer_type (type), obj);
492 obj = fold_build_pointer_plus (obj,
493 fold_build1 (NEGATE_EXPR, sizetype,
494 TYPE_SIZE_UNIT (TREE_TYPE (obj))));
495 obj = build1 (INDIRECT_REF, type, obj);
497 return obj;
500 /* If there are any handlers for this range, issue end of range,
501 and then all handler blocks */
502 void
503 expand_end_java_handler (struct eh_range *range)
505 tree handler = range->handlers;
506 if (handler)
508 tree exc_obj = build_exception_object_var ();
509 tree catches = make_node (STATEMENT_LIST);
510 tree_stmt_iterator catches_i = tsi_last (catches);
511 tree *body;
513 for (; handler; handler = TREE_CHAIN (handler))
515 tree type, eh_type, x;
516 tree stmts = make_node (STATEMENT_LIST);
517 tree_stmt_iterator stmts_i = tsi_last (stmts);
519 type = TREE_PURPOSE (handler);
520 if (type == NULL)
521 type = throwable_type_node;
522 eh_type = prepare_eh_table_type (type);
524 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
525 1, integer_zero_node);
526 x = build2 (MODIFY_EXPR, void_type_node, exc_obj, x);
527 tsi_link_after (&stmts_i, x, TSI_CONTINUE_LINKING);
529 x = build1 (GOTO_EXPR, void_type_node, TREE_VALUE (handler));
530 tsi_link_after (&stmts_i, x, TSI_CONTINUE_LINKING);
532 x = build2 (CATCH_EXPR, void_type_node, eh_type, stmts);
533 tsi_link_after (&catches_i, x, TSI_CONTINUE_LINKING);
535 /* Throwable can match anything in Java, and therefore
536 any subsequent handlers are unreachable. */
537 /* ??? If we're assured of no foreign language exceptions,
538 we'd be better off using NULL as the exception type
539 for the catch. */
540 if (type == throwable_type_node)
541 break;
544 body = get_stmts ();
545 *body = build2 (TRY_CATCH_EXPR, void_type_node, *body, catches);
548 #if defined(DEBUG_JAVA_BINDING_LEVELS)
549 indent ();
550 fprintf (stderr, "expand end handler pc %d <-- %d\n",
551 current_pc, range->start_pc);
552 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
555 /* Recursive helper routine for maybe_start_handlers. */
557 static void
558 check_start_handlers (struct eh_range *range, int pc)
560 if (range != NULL_EH_RANGE && range->start_pc == pc)
562 check_start_handlers (range->outer, pc);
563 if (!range->expanded)
564 expand_start_java_handler (range);
569 /* Routine to see if exception handling is turned on.
570 DO_WARN is nonzero if we want to inform the user that exception
571 handling is turned off.
573 This is used to ensure that -fexceptions has been specified if the
574 compiler tries to use any exception-specific functions. */
576 static inline int
577 doing_eh (void)
579 if (! flag_exceptions)
581 static int warned = 0;
582 if (! warned)
584 error ("exception handling disabled, use -fexceptions to enable");
585 warned = 1;
587 return 0;
589 return 1;
592 static struct eh_range *current_range;
594 /* Emit any start-of-try-range starting at start_pc and ending after
595 end_pc. */
597 void
598 maybe_start_try (int start_pc, int end_pc)
600 struct eh_range *range;
601 if (! doing_eh ())
602 return;
604 range = find_handler (start_pc);
605 while (range != NULL_EH_RANGE && range->start_pc == start_pc
606 && range->end_pc < end_pc)
607 range = range->outer;
609 current_range = range;
610 check_start_handlers (range, start_pc);