* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / java / except.c
blob557726c05e519414fae4ef16424588e40e78931e
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 "symtab.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "stringpool.h"
33 #include "stor-layout.h"
34 #include "java-tree.h"
35 #include "javaop.h"
36 #include "java-opcodes.h"
37 #include "jcf.h"
38 #include "java-except.h"
39 #include "diagnostic-core.h"
40 #include "toplev.h"
41 #include "tree-iterator.h"
44 static void expand_start_java_handler (struct eh_range *);
45 static struct eh_range *find_handler_in_range (int, struct eh_range *,
46 struct eh_range *);
47 static void check_start_handlers (struct eh_range *, int);
48 static void free_eh_ranges (struct eh_range *range);
50 struct eh_range *current_method_handlers;
52 struct eh_range *current_try_block = NULL;
54 /* These variables are used to speed up find_handler. */
56 static int cache_range_start, cache_range_end;
57 static struct eh_range *cache_range;
58 static struct eh_range *cache_next_child;
60 /* A dummy range that represents the entire method. */
62 struct eh_range whole_range;
64 /* Check the invariants of the structure we're using to contain
65 exception regions. Either returns true or fails an assertion
66 check. */
68 bool
69 sanity_check_exception_range (struct eh_range *range)
71 struct eh_range *ptr = range->first_child;
72 for (; ptr; ptr = ptr->next_sibling)
74 gcc_assert (ptr->outer == range
75 && ptr->end_pc > ptr->start_pc);
76 if (ptr->next_sibling)
77 gcc_assert (ptr->next_sibling->start_pc >= ptr->end_pc);
78 gcc_assert (ptr->start_pc >= ptr->outer->start_pc
79 && ptr->end_pc <= ptr->outer->end_pc);
80 (void) sanity_check_exception_range (ptr);
82 return true;
85 #if defined(DEBUG_JAVA_BINDING_LEVELS)
86 extern int is_class_level;
87 extern int current_pc;
88 extern int binding_depth;
89 extern void indent (void);
90 static void
91 print_ranges (struct eh_range *range)
93 if (! range)
94 return;
96 struct eh_range *child = range->first_child;
98 indent ();
99 fprintf (stderr, "handler pc %d --> %d ", range->start_pc, range->end_pc);
101 tree handler = range->handlers;
102 for ( ; handler != NULL_TREE; handler = TREE_CHAIN (handler))
104 tree type = TREE_PURPOSE (handler);
105 if (type == NULL)
106 type = throwable_type_node;
107 fprintf (stderr, " type=%s ", IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
109 fprintf (stderr, "\n");
111 int saved = binding_depth;
112 binding_depth++;
113 print_ranges (child);
114 binding_depth = saved;
116 print_ranges (range->next_sibling);
118 #endif
120 /* Search for the most specific eh_range containing PC.
121 Assume PC is within RANGE.
122 CHILD is a list of children of RANGE such that any
123 previous children have end_pc values that are too low. */
125 static struct eh_range *
126 find_handler_in_range (int pc, struct eh_range *range, struct eh_range *child)
128 for (; child != NULL; child = child->next_sibling)
130 if (pc < child->start_pc)
131 break;
132 if (pc < child->end_pc)
133 return find_handler_in_range (pc, child, child->first_child);
135 cache_range = range;
136 cache_range_start = pc;
137 cache_next_child = child;
138 cache_range_end = child == NULL ? range->end_pc : child->start_pc;
139 return range;
142 /* Find the inner-most handler that contains PC. */
144 struct eh_range *
145 find_handler (int pc)
147 struct eh_range *h;
148 if (pc >= cache_range_start)
150 h = cache_range;
151 if (pc < cache_range_end)
152 return h;
153 while (pc >= h->end_pc)
155 cache_next_child = h->next_sibling;
156 h = h->outer;
159 else
161 h = &whole_range;
162 cache_next_child = h->first_child;
164 return find_handler_in_range (pc, h, cache_next_child);
167 static void
168 free_eh_ranges (struct eh_range *range)
170 while (range)
172 struct eh_range *next = range->next_sibling;
173 free_eh_ranges (range->first_child);
174 if (range != &whole_range)
175 free (range);
176 range = next;
180 /* Called to re-initialize the exception machinery for a new method. */
182 void
183 method_init_exceptions (void)
185 free_eh_ranges (&whole_range);
186 whole_range.start_pc = 0;
187 whole_range.end_pc = DECL_CODE_LENGTH (current_function_decl) + 1;
188 whole_range.outer = NULL;
189 whole_range.first_child = NULL;
190 whole_range.next_sibling = NULL;
191 cache_range_start = 0xFFFFFF;
194 /* Split an exception range into two at PC. The sub-ranges that
195 belong to the range are split and distributed between the two new
196 ranges. */
198 static void
199 split_range (struct eh_range *range, int pc)
201 struct eh_range *ptr;
202 struct eh_range **first_child, **second_child;
203 struct eh_range *h;
205 /* First, split all the sub-ranges. */
206 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
208 if (pc > ptr->start_pc
209 && pc < ptr->end_pc)
211 split_range (ptr, pc);
215 /* Create a new range. */
216 h = XNEW (struct eh_range);
218 h->start_pc = pc;
219 h->end_pc = range->end_pc;
220 h->next_sibling = range->next_sibling;
221 range->next_sibling = h;
222 range->end_pc = pc;
223 h->handlers = build_tree_list (TREE_PURPOSE (range->handlers),
224 TREE_VALUE (range->handlers));
225 h->next_sibling = NULL;
226 h->expanded = 0;
227 h->stmt = NULL;
228 h->outer = range->outer;
229 h->first_child = NULL;
231 ptr = range->first_child;
232 first_child = &range->first_child;
233 second_child = &h->first_child;
235 /* Distribute the sub-ranges between the two new ranges. */
236 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
238 if (ptr->start_pc < pc)
240 *first_child = ptr;
241 ptr->outer = range;
242 first_child = &ptr->next_sibling;
244 else
246 *second_child = ptr;
247 ptr->outer = h;
248 second_child = &ptr->next_sibling;
251 *first_child = NULL;
252 *second_child = NULL;
256 /* Add an exception range.
258 There are some missed optimization opportunities here. For
259 example, some bytecode obfuscators generate seemingly
260 nonoverlapping exception ranges which, when coalesced, do in fact
261 nest correctly. We could merge these, but we'd have to fix up all
262 the enclosed regions first and perhaps create a new range anyway if
263 it overlapped existing ranges.
265 Also, we don't attempt to detect the case where two previously
266 added disjoint ranges could be coalesced by a new range. */
268 void
269 add_handler (int start_pc, int end_pc, tree handler, tree type)
271 struct eh_range *ptr, *h;
272 struct eh_range **first_child, **prev;
274 /* First, split all the existing ranges that we need to enclose. */
275 for (ptr = whole_range.first_child; ptr; ptr = ptr->next_sibling)
277 if (start_pc > ptr->start_pc
278 && start_pc < ptr->end_pc)
280 split_range (ptr, start_pc);
283 if (end_pc > ptr->start_pc
284 && end_pc < ptr->end_pc)
286 split_range (ptr, end_pc);
289 if (ptr->start_pc >= end_pc)
290 break;
293 /* Create the new range. */
294 h = XNEW (struct eh_range);
295 first_child = &h->first_child;
297 h->start_pc = start_pc;
298 h->end_pc = end_pc;
299 h->first_child = NULL;
300 h->outer = NULL_EH_RANGE;
301 h->handlers = build_tree_list (type, handler);
302 h->next_sibling = NULL;
303 h->expanded = 0;
304 h->stmt = NULL;
306 /* Find every range at the top level that will be a sub-range of the
307 range we're inserting and make it so. */
309 struct eh_range **prev = &whole_range.first_child;
310 for (ptr = *prev; ptr;)
312 struct eh_range *next = ptr->next_sibling;
314 if (ptr->start_pc >= end_pc)
315 break;
317 if (ptr->start_pc < start_pc)
319 prev = &ptr->next_sibling;
321 else if (ptr->start_pc >= start_pc
322 && ptr->start_pc < end_pc)
324 *prev = next;
325 *first_child = ptr;
326 first_child = &ptr->next_sibling;
327 ptr->outer = h;
328 ptr->next_sibling = NULL;
331 ptr = next;
335 /* Find the right place to insert the new range. */
336 prev = &whole_range.first_child;
337 for (ptr = *prev; ptr; prev = &ptr->next_sibling, ptr = ptr->next_sibling)
339 gcc_assert (ptr->outer == NULL_EH_RANGE);
340 if (ptr->start_pc >= start_pc)
341 break;
344 /* And insert it there. */
345 *prev = h;
346 if (ptr)
348 h->next_sibling = ptr;
349 h->outer = ptr->outer;
354 /* if there are any handlers for this range, issue start of region */
355 static void
356 expand_start_java_handler (struct eh_range *range)
358 #if defined(DEBUG_JAVA_BINDING_LEVELS)
359 indent ();
360 fprintf (stderr, "expand start handler pc %d --> %d\n",
361 current_pc, range->end_pc);
362 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
363 pushlevel (0);
364 register_exception_range (range, range->start_pc, range->end_pc);
365 range->expanded = 1;
368 tree
369 prepare_eh_table_type (tree type)
371 tree exp;
372 tree *slot;
373 const char *name;
374 char *buf;
375 tree decl;
376 tree utf8_ref;
378 /* The "type" (match_info) in a (Java) exception table is a pointer to:
379 * a) NULL - meaning match any type in a try-finally.
380 * b) a pointer to a pointer to a class.
381 * c) a pointer to a pointer to a utf8_ref. The pointer is
382 * rewritten to point to the appropriate class. */
384 if (type == NULL_TREE)
385 return NULL_TREE;
387 if (TYPE_TO_RUNTIME_MAP (output_class) == NULL)
388 TYPE_TO_RUNTIME_MAP (output_class) = java_treetreehash_create (10);
390 slot = java_treetreehash_new (TYPE_TO_RUNTIME_MAP (output_class), type);
391 if (*slot != NULL)
392 return TREE_VALUE (*slot);
394 if (is_compiled_class (type) && !flag_indirect_dispatch)
396 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
397 buf = (char *) alloca (strlen (name) + 5);
398 sprintf (buf, "%s_ref", name);
399 decl = build_decl (input_location,
400 VAR_DECL, get_identifier (buf), ptr_type_node);
401 TREE_STATIC (decl) = 1;
402 DECL_ARTIFICIAL (decl) = 1;
403 DECL_IGNORED_P (decl) = 1;
404 TREE_READONLY (decl) = 1;
405 TREE_THIS_VOLATILE (decl) = 0;
406 DECL_INITIAL (decl) = build_class_ref (type);
407 layout_decl (decl, 0);
408 pushdecl (decl);
409 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (decl)), decl);
411 else
413 utf8_ref = build_utf8_ref (DECL_NAME (TYPE_NAME (type)));
414 name = IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (utf8_ref, 0)));
415 buf = (char *) alloca (strlen (name) + 5);
416 sprintf (buf, "%s_ref", name);
417 decl = build_decl (input_location,
418 VAR_DECL, get_identifier (buf), utf8const_ptr_type);
419 TREE_STATIC (decl) = 1;
420 DECL_ARTIFICIAL (decl) = 1;
421 DECL_IGNORED_P (decl) = 1;
422 TREE_READONLY (decl) = 1;
423 TREE_THIS_VOLATILE (decl) = 0;
424 layout_decl (decl, 0);
425 pushdecl (decl);
426 exp = build1 (ADDR_EXPR, build_pointer_type (utf8const_ptr_type), decl);
427 CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (output_class),
428 NULL_TREE,
429 make_catch_class_record (exp, utf8_ref));
432 exp = convert (ptr_type_node, exp);
434 *slot = tree_cons (type, exp, NULL_TREE);
436 return exp;
440 expand_catch_class (treetreehash_entry **entry, int)
442 struct treetreehash_entry *ite = *entry;
443 tree addr = TREE_VALUE ((tree)ite->value);
444 tree decl;
445 STRIP_NOPS (addr);
446 decl = TREE_OPERAND (addr, 0);
447 rest_of_decl_compilation (decl, global_bindings_p (), 0);
448 return true;
451 /* For every class in the TYPE_TO_RUNTIME_MAP, expand the
452 corresponding object that is used by the runtime type matcher. */
454 void
455 java_expand_catch_classes (tree this_class)
457 if (TYPE_TO_RUNTIME_MAP (this_class))
458 TYPE_TO_RUNTIME_MAP (this_class)->traverse<int, expand_catch_class> (0);
461 /* Build and push the variable that will hold the exception object
462 within this function. */
464 static tree
465 build_exception_object_var (void)
467 tree decl = DECL_FUNCTION_EXC_OBJ (current_function_decl);
468 if (decl == NULL)
470 decl = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
471 VAR_DECL, get_identifier ("#exc_obj"), ptr_type_node);
472 DECL_IGNORED_P (decl) = 1;
473 DECL_ARTIFICIAL (decl) = 1;
475 DECL_FUNCTION_EXC_OBJ (current_function_decl) = decl;
476 pushdecl_function_level (decl);
478 return decl;
481 /* Build a reference to the jthrowable object being carried in the
482 exception header. */
484 tree
485 build_exception_object_ref (tree type)
487 tree obj;
489 /* Java only passes object via pointer and doesn't require adjusting.
490 The java object is immediately before the generic exception header. */
491 obj = build_exception_object_var ();
492 obj = fold_convert (build_pointer_type (type), obj);
493 obj = fold_build_pointer_plus (obj,
494 fold_build1 (NEGATE_EXPR, sizetype,
495 TYPE_SIZE_UNIT (TREE_TYPE (obj))));
496 obj = build1 (INDIRECT_REF, type, obj);
498 return obj;
501 /* If there are any handlers for this range, issue end of range,
502 and then all handler blocks */
503 void
504 expand_end_java_handler (struct eh_range *range)
506 tree handler = range->handlers;
507 if (handler)
509 tree exc_obj = build_exception_object_var ();
510 tree catches = make_node (STATEMENT_LIST);
511 tree_stmt_iterator catches_i = tsi_last (catches);
512 tree *body;
514 for (; handler; handler = TREE_CHAIN (handler))
516 tree type, eh_type, x;
517 tree stmts = make_node (STATEMENT_LIST);
518 tree_stmt_iterator stmts_i = tsi_last (stmts);
520 type = TREE_PURPOSE (handler);
521 if (type == NULL)
522 type = throwable_type_node;
523 eh_type = prepare_eh_table_type (type);
525 x = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
526 1, integer_zero_node);
527 x = build2 (MODIFY_EXPR, void_type_node, exc_obj, x);
528 tsi_link_after (&stmts_i, x, TSI_CONTINUE_LINKING);
530 x = build1 (GOTO_EXPR, void_type_node, TREE_VALUE (handler));
531 tsi_link_after (&stmts_i, x, TSI_CONTINUE_LINKING);
533 x = build2 (CATCH_EXPR, void_type_node, eh_type, stmts);
534 tsi_link_after (&catches_i, x, TSI_CONTINUE_LINKING);
536 /* Throwable can match anything in Java, and therefore
537 any subsequent handlers are unreachable. */
538 /* ??? If we're assured of no foreign language exceptions,
539 we'd be better off using NULL as the exception type
540 for the catch. */
541 if (type == throwable_type_node)
542 break;
545 body = get_stmts ();
546 *body = build2 (TRY_CATCH_EXPR, void_type_node, *body, catches);
549 #if defined(DEBUG_JAVA_BINDING_LEVELS)
550 indent ();
551 fprintf (stderr, "expand end handler pc %d <-- %d\n",
552 current_pc, range->start_pc);
553 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
556 /* Recursive helper routine for maybe_start_handlers. */
558 static void
559 check_start_handlers (struct eh_range *range, int pc)
561 if (range != NULL_EH_RANGE && range->start_pc == pc)
563 check_start_handlers (range->outer, pc);
564 if (!range->expanded)
565 expand_start_java_handler (range);
570 /* Routine to see if exception handling is turned on.
571 DO_WARN is nonzero if we want to inform the user that exception
572 handling is turned off.
574 This is used to ensure that -fexceptions has been specified if the
575 compiler tries to use any exception-specific functions. */
577 static inline int
578 doing_eh (void)
580 if (! flag_exceptions)
582 static int warned = 0;
583 if (! warned)
585 error ("exception handling disabled, use -fexceptions to enable");
586 warned = 1;
588 return 0;
590 return 1;
593 static struct eh_range *current_range;
595 /* Emit any start-of-try-range starting at start_pc and ending after
596 end_pc. */
598 void
599 maybe_start_try (int start_pc, int end_pc)
601 struct eh_range *range;
602 if (! doing_eh ())
603 return;
605 range = find_handler (start_pc);
606 while (range != NULL_EH_RANGE && range->start_pc == start_pc
607 && range->end_pc < end_pc)
608 range = range->outer;
610 current_range = range;
611 check_start_handlers (range, start_pc);