Fix bootstrap failure.
[official-gcc.git] / gcc / java / except.c
blob492e9775855b0fbf5bf7ca9d5f1221026ba2aa24
1 /* Handle exceptions for GNU compiler for the Java(TM) language.
2 Copyright (C) 1997-2014 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 "tree.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "java-tree.h"
32 #include "javaop.h"
33 #include "java-opcodes.h"
34 #include "jcf.h"
35 #include "java-except.h"
36 #include "diagnostic-core.h"
37 #include "toplev.h"
38 #include "tree-iterator.h"
41 static void expand_start_java_handler (struct eh_range *);
42 static struct eh_range *find_handler_in_range (int, struct eh_range *,
43 struct eh_range *);
44 static void check_start_handlers (struct eh_range *, int);
45 static void free_eh_ranges (struct eh_range *range);
47 struct eh_range *current_method_handlers;
49 struct eh_range *current_try_block = NULL;
51 /* These variables are used to speed up find_handler. */
53 static int cache_range_start, cache_range_end;
54 static struct eh_range *cache_range;
55 static struct eh_range *cache_next_child;
57 /* A dummy range that represents the entire method. */
59 struct eh_range whole_range;
61 /* Check the invariants of the structure we're using to contain
62 exception regions. Either returns true or fails an assertion
63 check. */
65 bool
66 sanity_check_exception_range (struct eh_range *range)
68 struct eh_range *ptr = range->first_child;
69 for (; ptr; ptr = ptr->next_sibling)
71 gcc_assert (ptr->outer == range
72 && ptr->end_pc > ptr->start_pc);
73 if (ptr->next_sibling)
74 gcc_assert (ptr->next_sibling->start_pc >= ptr->end_pc);
75 gcc_assert (ptr->start_pc >= ptr->outer->start_pc
76 && ptr->end_pc <= ptr->outer->end_pc);
77 (void) sanity_check_exception_range (ptr);
79 return true;
82 #if defined(DEBUG_JAVA_BINDING_LEVELS)
83 extern int is_class_level;
84 extern int current_pc;
85 extern int binding_depth;
86 extern void indent (void);
87 static void
88 print_ranges (struct eh_range *range)
90 if (! range)
91 return;
93 struct eh_range *child = range->first_child;
95 indent ();
96 fprintf (stderr, "handler pc %d --> %d ", range->start_pc, range->end_pc);
98 tree handler = range->handlers;
99 for ( ; handler != NULL_TREE; handler = TREE_CHAIN (handler))
101 tree type = TREE_PURPOSE (handler);
102 if (type == NULL)
103 type = throwable_type_node;
104 fprintf (stderr, " type=%s ", IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
106 fprintf (stderr, "\n");
108 int saved = binding_depth;
109 binding_depth++;
110 print_ranges (child);
111 binding_depth = saved;
113 print_ranges (range->next_sibling);
115 #endif
117 /* Search for the most specific eh_range containing PC.
118 Assume PC is within RANGE.
119 CHILD is a list of children of RANGE such that any
120 previous children have end_pc values that are too low. */
122 static struct eh_range *
123 find_handler_in_range (int pc, struct eh_range *range, struct eh_range *child)
125 for (; child != NULL; child = child->next_sibling)
127 if (pc < child->start_pc)
128 break;
129 if (pc < child->end_pc)
130 return find_handler_in_range (pc, child, child->first_child);
132 cache_range = range;
133 cache_range_start = pc;
134 cache_next_child = child;
135 cache_range_end = child == NULL ? range->end_pc : child->start_pc;
136 return range;
139 /* Find the inner-most handler that contains PC. */
141 struct eh_range *
142 find_handler (int pc)
144 struct eh_range *h;
145 if (pc >= cache_range_start)
147 h = cache_range;
148 if (pc < cache_range_end)
149 return h;
150 while (pc >= h->end_pc)
152 cache_next_child = h->next_sibling;
153 h = h->outer;
156 else
158 h = &whole_range;
159 cache_next_child = h->first_child;
161 return find_handler_in_range (pc, h, cache_next_child);
164 static void
165 free_eh_ranges (struct eh_range *range)
167 while (range)
169 struct eh_range *next = range->next_sibling;
170 free_eh_ranges (range->first_child);
171 if (range != &whole_range)
172 free (range);
173 range = next;
177 /* Called to re-initialize the exception machinery for a new method. */
179 void
180 method_init_exceptions (void)
182 free_eh_ranges (&whole_range);
183 whole_range.start_pc = 0;
184 whole_range.end_pc = DECL_CODE_LENGTH (current_function_decl) + 1;
185 whole_range.outer = NULL;
186 whole_range.first_child = NULL;
187 whole_range.next_sibling = NULL;
188 cache_range_start = 0xFFFFFF;
191 /* Split an exception range into two at PC. The sub-ranges that
192 belong to the range are split and distributed between the two new
193 ranges. */
195 static void
196 split_range (struct eh_range *range, int pc)
198 struct eh_range *ptr;
199 struct eh_range **first_child, **second_child;
200 struct eh_range *h;
202 /* First, split all the sub-ranges. */
203 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
205 if (pc > ptr->start_pc
206 && pc < ptr->end_pc)
208 split_range (ptr, pc);
212 /* Create a new range. */
213 h = XNEW (struct eh_range);
215 h->start_pc = pc;
216 h->end_pc = range->end_pc;
217 h->next_sibling = range->next_sibling;
218 range->next_sibling = h;
219 range->end_pc = pc;
220 h->handlers = build_tree_list (TREE_PURPOSE (range->handlers),
221 TREE_VALUE (range->handlers));
222 h->next_sibling = NULL;
223 h->expanded = 0;
224 h->stmt = NULL;
225 h->outer = range->outer;
226 h->first_child = NULL;
228 ptr = range->first_child;
229 first_child = &range->first_child;
230 second_child = &h->first_child;
232 /* Distribute the sub-ranges between the two new ranges. */
233 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
235 if (ptr->start_pc < pc)
237 *first_child = ptr;
238 ptr->outer = range;
239 first_child = &ptr->next_sibling;
241 else
243 *second_child = ptr;
244 ptr->outer = h;
245 second_child = &ptr->next_sibling;
248 *first_child = NULL;
249 *second_child = NULL;
253 /* Add an exception range.
255 There are some missed optimization opportunities here. For
256 example, some bytecode obfuscators generate seemingly
257 nonoverlapping exception ranges which, when coalesced, do in fact
258 nest correctly. We could merge these, but we'd have to fix up all
259 the enclosed regions first and perhaps create a new range anyway if
260 it overlapped existing ranges.
262 Also, we don't attempt to detect the case where two previously
263 added disjoint ranges could be coalesced by a new range. */
265 void
266 add_handler (int start_pc, int end_pc, tree handler, tree type)
268 struct eh_range *ptr, *h;
269 struct eh_range **first_child, **prev;
271 /* First, split all the existing ranges that we need to enclose. */
272 for (ptr = whole_range.first_child; ptr; ptr = ptr->next_sibling)
274 if (start_pc > ptr->start_pc
275 && start_pc < ptr->end_pc)
277 split_range (ptr, start_pc);
280 if (end_pc > ptr->start_pc
281 && end_pc < ptr->end_pc)
283 split_range (ptr, end_pc);
286 if (ptr->start_pc >= end_pc)
287 break;
290 /* Create the new range. */
291 h = XNEW (struct eh_range);
292 first_child = &h->first_child;
294 h->start_pc = start_pc;
295 h->end_pc = end_pc;
296 h->first_child = NULL;
297 h->outer = NULL_EH_RANGE;
298 h->handlers = build_tree_list (type, handler);
299 h->next_sibling = NULL;
300 h->expanded = 0;
301 h->stmt = NULL;
303 /* Find every range at the top level that will be a sub-range of the
304 range we're inserting and make it so. */
306 struct eh_range **prev = &whole_range.first_child;
307 for (ptr = *prev; ptr;)
309 struct eh_range *next = ptr->next_sibling;
311 if (ptr->start_pc >= end_pc)
312 break;
314 if (ptr->start_pc < start_pc)
316 prev = &ptr->next_sibling;
318 else if (ptr->start_pc >= start_pc
319 && ptr->start_pc < end_pc)
321 *prev = next;
322 *first_child = ptr;
323 first_child = &ptr->next_sibling;
324 ptr->outer = h;
325 ptr->next_sibling = NULL;
328 ptr = next;
332 /* Find the right place to insert the new range. */
333 prev = &whole_range.first_child;
334 for (ptr = *prev; ptr; prev = &ptr->next_sibling, ptr = ptr->next_sibling)
336 gcc_assert (ptr->outer == NULL_EH_RANGE);
337 if (ptr->start_pc >= start_pc)
338 break;
341 /* And insert it there. */
342 *prev = h;
343 if (ptr)
345 h->next_sibling = ptr;
346 h->outer = ptr->outer;
351 /* if there are any handlers for this range, issue start of region */
352 static void
353 expand_start_java_handler (struct eh_range *range)
355 #if defined(DEBUG_JAVA_BINDING_LEVELS)
356 indent ();
357 fprintf (stderr, "expand start handler pc %d --> %d\n",
358 current_pc, range->end_pc);
359 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
360 pushlevel (0);
361 register_exception_range (range, range->start_pc, range->end_pc);
362 range->expanded = 1;
365 tree
366 prepare_eh_table_type (tree type)
368 tree exp;
369 tree *slot;
370 const char *name;
371 char *buf;
372 tree decl;
373 tree utf8_ref;
375 /* The "type" (match_info) in a (Java) exception table is a pointer to:
376 * a) NULL - meaning match any type in a try-finally.
377 * b) a pointer to a pointer to a class.
378 * c) a pointer to a pointer to a utf8_ref. The pointer is
379 * rewritten to point to the appropriate class. */
381 if (type == NULL_TREE)
382 return NULL_TREE;
384 if (TYPE_TO_RUNTIME_MAP (output_class) == NULL)
385 TYPE_TO_RUNTIME_MAP (output_class) = java_treetreehash_create (10);
387 slot = java_treetreehash_new (TYPE_TO_RUNTIME_MAP (output_class), type);
388 if (*slot != NULL)
389 return TREE_VALUE (*slot);
391 if (is_compiled_class (type) && !flag_indirect_dispatch)
393 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
394 buf = (char *) alloca (strlen (name) + 5);
395 sprintf (buf, "%s_ref", name);
396 decl = build_decl (input_location,
397 VAR_DECL, get_identifier (buf), ptr_type_node);
398 TREE_STATIC (decl) = 1;
399 DECL_ARTIFICIAL (decl) = 1;
400 DECL_IGNORED_P (decl) = 1;
401 TREE_READONLY (decl) = 1;
402 TREE_THIS_VOLATILE (decl) = 0;
403 DECL_INITIAL (decl) = build_class_ref (type);
404 layout_decl (decl, 0);
405 pushdecl (decl);
406 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (decl)), decl);
408 else
410 utf8_ref = build_utf8_ref (DECL_NAME (TYPE_NAME (type)));
411 name = IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (utf8_ref, 0)));
412 buf = (char *) alloca (strlen (name) + 5);
413 sprintf (buf, "%s_ref", name);
414 decl = build_decl (input_location,
415 VAR_DECL, get_identifier (buf), utf8const_ptr_type);
416 TREE_STATIC (decl) = 1;
417 DECL_ARTIFICIAL (decl) = 1;
418 DECL_IGNORED_P (decl) = 1;
419 TREE_READONLY (decl) = 1;
420 TREE_THIS_VOLATILE (decl) = 0;
421 layout_decl (decl, 0);
422 pushdecl (decl);
423 exp = build1 (ADDR_EXPR, build_pointer_type (utf8const_ptr_type), decl);
424 CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (output_class),
425 NULL_TREE,
426 make_catch_class_record (exp, utf8_ref));
429 exp = convert (ptr_type_node, exp);
431 *slot = tree_cons (type, exp, NULL_TREE);
433 return exp;
437 expand_catch_class (treetreehash_entry **entry, int)
439 struct treetreehash_entry *ite = *entry;
440 tree addr = TREE_VALUE ((tree)ite->value);
441 tree decl;
442 STRIP_NOPS (addr);
443 decl = TREE_OPERAND (addr, 0);
444 rest_of_decl_compilation (decl, global_bindings_p (), 0);
445 return true;
448 /* For every class in the TYPE_TO_RUNTIME_MAP, expand the
449 corresponding object that is used by the runtime type matcher. */
451 void
452 java_expand_catch_classes (tree this_class)
454 if (TYPE_TO_RUNTIME_MAP (this_class))
455 TYPE_TO_RUNTIME_MAP (this_class)->traverse<int, expand_catch_class> (0);
458 /* Build and push the variable that will hold the exception object
459 within this function. */
461 static tree
462 build_exception_object_var (void)
464 tree decl = DECL_FUNCTION_EXC_OBJ (current_function_decl);
465 if (decl == NULL)
467 decl = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
468 VAR_DECL, get_identifier ("#exc_obj"), ptr_type_node);
469 DECL_IGNORED_P (decl) = 1;
470 DECL_ARTIFICIAL (decl) = 1;
472 DECL_FUNCTION_EXC_OBJ (current_function_decl) = decl;
473 pushdecl_function_level (decl);
475 return decl;
478 /* Build a reference to the jthrowable object being carried in the
479 exception header. */
481 tree
482 build_exception_object_ref (tree type)
484 tree obj;
486 /* Java only passes object via pointer and doesn't require adjusting.
487 The java object is immediately before the generic exception header. */
488 obj = build_exception_object_var ();
489 obj = fold_convert (build_pointer_type (type), obj);
490 obj = fold_build_pointer_plus (obj,
491 fold_build1 (NEGATE_EXPR, sizetype,
492 TYPE_SIZE_UNIT (TREE_TYPE (obj))));
493 obj = build1 (INDIRECT_REF, type, obj);
495 return obj;
498 /* If there are any handlers for this range, issue end of range,
499 and then all handler blocks */
500 void
501 expand_end_java_handler (struct eh_range *range)
503 tree handler = range->handlers;
504 if (handler)
506 tree exc_obj = build_exception_object_var ();
507 tree catches = make_node (STATEMENT_LIST);
508 tree_stmt_iterator catches_i = tsi_last (catches);
509 tree *body;
511 for (; handler; handler = TREE_CHAIN (handler))
513 tree type, eh_type, x;
514 tree stmts = make_node (STATEMENT_LIST);
515 tree_stmt_iterator stmts_i = tsi_last (stmts);
517 type = TREE_PURPOSE (handler);
518 if (type == NULL)
519 type = throwable_type_node;
520 eh_type = prepare_eh_table_type (type);
522 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
523 1, integer_zero_node);
524 x = build2 (MODIFY_EXPR, void_type_node, exc_obj, x);
525 tsi_link_after (&stmts_i, x, TSI_CONTINUE_LINKING);
527 x = build1 (GOTO_EXPR, void_type_node, TREE_VALUE (handler));
528 tsi_link_after (&stmts_i, x, TSI_CONTINUE_LINKING);
530 x = build2 (CATCH_EXPR, void_type_node, eh_type, stmts);
531 tsi_link_after (&catches_i, x, TSI_CONTINUE_LINKING);
533 /* Throwable can match anything in Java, and therefore
534 any subsequent handlers are unreachable. */
535 /* ??? If we're assured of no foreign language exceptions,
536 we'd be better off using NULL as the exception type
537 for the catch. */
538 if (type == throwable_type_node)
539 break;
542 body = get_stmts ();
543 *body = build2 (TRY_CATCH_EXPR, void_type_node, *body, catches);
546 #if defined(DEBUG_JAVA_BINDING_LEVELS)
547 indent ();
548 fprintf (stderr, "expand end handler pc %d <-- %d\n",
549 current_pc, range->start_pc);
550 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
553 /* Recursive helper routine for maybe_start_handlers. */
555 static void
556 check_start_handlers (struct eh_range *range, int pc)
558 if (range != NULL_EH_RANGE && range->start_pc == pc)
560 check_start_handlers (range->outer, pc);
561 if (!range->expanded)
562 expand_start_java_handler (range);
567 /* Routine to see if exception handling is turned on.
568 DO_WARN is nonzero if we want to inform the user that exception
569 handling is turned off.
571 This is used to ensure that -fexceptions has been specified if the
572 compiler tries to use any exception-specific functions. */
574 static inline int
575 doing_eh (void)
577 if (! flag_exceptions)
579 static int warned = 0;
580 if (! warned)
582 error ("exception handling disabled, use -fexceptions to enable");
583 warned = 1;
585 return 0;
587 return 1;
590 static struct eh_range *current_range;
592 /* Emit any start-of-try-range starting at start_pc and ending after
593 end_pc. */
595 void
596 maybe_start_try (int start_pc, int end_pc)
598 struct eh_range *range;
599 if (! doing_eh ())
600 return;
602 range = find_handler (start_pc);
603 while (range != NULL_EH_RANGE && range->start_pc == start_pc
604 && range->end_pc < end_pc)
605 range = range->outer;
607 current_range = range;
608 check_start_handlers (range, start_pc);