]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/except.c
gcc/ChangeLog:
[thirdparty/gcc.git] / gcc / java / except.c
1 /* Handle exceptions for GNU compiler for the Java(TM) language.
2 Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "java-tree.h"
31 #include "javaop.h"
32 #include "java-opcodes.h"
33 #include "jcf.h"
34 #include "java-except.h"
35 #include "toplev.h"
36 #include "tree-iterator.h"
37
38
39 static void expand_start_java_handler (struct eh_range *);
40 static struct eh_range *find_handler_in_range (int, struct eh_range *,
41 struct eh_range *);
42 static void check_start_handlers (struct eh_range *, int);
43 static void free_eh_ranges (struct eh_range *range);
44
45 struct eh_range *current_method_handlers;
46
47 struct eh_range *current_try_block = NULL;
48
49 /* These variables are used to speed up find_handler. */
50
51 static int cache_range_start, cache_range_end;
52 static struct eh_range *cache_range;
53 static struct eh_range *cache_next_child;
54
55 /* A dummy range that represents the entire method. */
56
57 struct eh_range whole_range;
58
59 /* Check the invariants of the structure we're using to contain
60 exception regions. Either returns true or fails an assertion
61 check. */
62
63 bool
64 sanity_check_exception_range (struct eh_range *range)
65 {
66 struct eh_range *ptr = range->first_child;
67 for (; ptr; ptr = ptr->next_sibling)
68 {
69 gcc_assert (ptr->outer == range
70 && ptr->end_pc > ptr->start_pc);
71 if (ptr->next_sibling)
72 gcc_assert (ptr->next_sibling->start_pc >= ptr->end_pc);
73 gcc_assert (ptr->start_pc >= ptr->outer->start_pc
74 && ptr->end_pc <= ptr->outer->end_pc);
75 (void) sanity_check_exception_range (ptr);
76 }
77 return true;
78 }
79
80 #if defined(DEBUG_JAVA_BINDING_LEVELS)
81 extern int is_class_level;
82 extern int current_pc;
83 extern int binding_depth;
84 extern void indent (void);
85 static void
86 print_ranges (struct eh_range *range)
87 {
88 if (! range)
89 return;
90
91 struct eh_range *child = range->first_child;
92
93 indent ();
94 fprintf (stderr, "handler pc %d --> %d ", range->start_pc, range->end_pc);
95
96 tree handler = range->handlers;
97 for ( ; handler != NULL_TREE; handler = TREE_CHAIN (handler))
98 {
99 tree type = TREE_PURPOSE (handler);
100 if (type == NULL)
101 type = throwable_type_node;
102 fprintf (stderr, " type=%s ", IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
103 }
104 fprintf (stderr, "\n");
105
106 int saved = binding_depth;
107 binding_depth++;
108 print_ranges (child);
109 binding_depth = saved;
110
111 print_ranges (range->next_sibling);
112 }
113 #endif
114
115 /* Search for the most specific eh_range containing PC.
116 Assume PC is within RANGE.
117 CHILD is a list of children of RANGE such that any
118 previous children have end_pc values that are too low. */
119
120 static struct eh_range *
121 find_handler_in_range (int pc, struct eh_range *range, struct eh_range *child)
122 {
123 for (; child != NULL; child = child->next_sibling)
124 {
125 if (pc < child->start_pc)
126 break;
127 if (pc < child->end_pc)
128 return find_handler_in_range (pc, child, child->first_child);
129 }
130 cache_range = range;
131 cache_range_start = pc;
132 cache_next_child = child;
133 cache_range_end = child == NULL ? range->end_pc : child->start_pc;
134 return range;
135 }
136
137 /* Find the inner-most handler that contains PC. */
138
139 struct eh_range *
140 find_handler (int pc)
141 {
142 struct eh_range *h;
143 if (pc >= cache_range_start)
144 {
145 h = cache_range;
146 if (pc < cache_range_end)
147 return h;
148 while (pc >= h->end_pc)
149 {
150 cache_next_child = h->next_sibling;
151 h = h->outer;
152 }
153 }
154 else
155 {
156 h = &whole_range;
157 cache_next_child = h->first_child;
158 }
159 return find_handler_in_range (pc, h, cache_next_child);
160 }
161
162 static void
163 free_eh_ranges (struct eh_range *range)
164 {
165 while (range)
166 {
167 struct eh_range *next = range->next_sibling;
168 free_eh_ranges (range->first_child);
169 if (range != &whole_range)
170 free (range);
171 range = next;
172 }
173 }
174
175 /* Called to re-initialize the exception machinery for a new method. */
176
177 void
178 method_init_exceptions (void)
179 {
180 free_eh_ranges (&whole_range);
181 whole_range.start_pc = 0;
182 whole_range.end_pc = DECL_CODE_LENGTH (current_function_decl) + 1;
183 whole_range.outer = NULL;
184 whole_range.first_child = NULL;
185 whole_range.next_sibling = NULL;
186 cache_range_start = 0xFFFFFF;
187 }
188
189 /* Split an exception range into two at PC. The sub-ranges that
190 belong to the range are split and distributed between the two new
191 ranges. */
192
193 static void
194 split_range (struct eh_range *range, int pc)
195 {
196 struct eh_range *ptr;
197 struct eh_range **first_child, **second_child;
198 struct eh_range *h;
199
200 /* First, split all the sub-ranges. */
201 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
202 {
203 if (pc > ptr->start_pc
204 && pc < ptr->end_pc)
205 {
206 split_range (ptr, pc);
207 }
208 }
209
210 /* Create a new range. */
211 h = XNEW (struct eh_range);
212
213 h->start_pc = pc;
214 h->end_pc = range->end_pc;
215 h->next_sibling = range->next_sibling;
216 range->next_sibling = h;
217 range->end_pc = pc;
218 h->handlers = build_tree_list (TREE_PURPOSE (range->handlers),
219 TREE_VALUE (range->handlers));
220 h->next_sibling = NULL;
221 h->expanded = 0;
222 h->stmt = NULL;
223 h->outer = range->outer;
224 h->first_child = NULL;
225
226 ptr = range->first_child;
227 first_child = &range->first_child;
228 second_child = &h->first_child;
229
230 /* Distribute the sub-ranges between the two new ranges. */
231 for (ptr = range->first_child; ptr; ptr = ptr->next_sibling)
232 {
233 if (ptr->start_pc < pc)
234 {
235 *first_child = ptr;
236 ptr->outer = range;
237 first_child = &ptr->next_sibling;
238 }
239 else
240 {
241 *second_child = ptr;
242 ptr->outer = h;
243 second_child = &ptr->next_sibling;
244 }
245 }
246 *first_child = NULL;
247 *second_child = NULL;
248 }
249
250
251 /* Add an exception range.
252
253 There are some missed optimization opportunities here. For
254 example, some bytecode obfuscators generate seemingly
255 nonoverlapping exception ranges which, when coalesced, do in fact
256 nest correctly. We could merge these, but we'd have to fix up all
257 the enclosed regions first and perhaps create a new range anyway if
258 it overlapped existing ranges.
259
260 Also, we don't attempt to detect the case where two previously
261 added disjoint ranges could be coalesced by a new range. */
262
263 void
264 add_handler (int start_pc, int end_pc, tree handler, tree type)
265 {
266 struct eh_range *ptr, *h;
267 struct eh_range **first_child, **prev;
268
269 /* First, split all the existing ranges that we need to enclose. */
270 for (ptr = whole_range.first_child; ptr; ptr = ptr->next_sibling)
271 {
272 if (start_pc > ptr->start_pc
273 && start_pc < ptr->end_pc)
274 {
275 split_range (ptr, start_pc);
276 }
277
278 if (end_pc > ptr->start_pc
279 && end_pc < ptr->end_pc)
280 {
281 split_range (ptr, end_pc);
282 }
283
284 if (ptr->start_pc >= end_pc)
285 break;
286 }
287
288 /* Create the new range. */
289 h = XNEW (struct eh_range);
290 first_child = &h->first_child;
291
292 h->start_pc = start_pc;
293 h->end_pc = end_pc;
294 h->first_child = NULL;
295 h->outer = NULL_EH_RANGE;
296 h->handlers = build_tree_list (type, handler);
297 h->next_sibling = NULL;
298 h->expanded = 0;
299 h->stmt = NULL;
300
301 /* Find every range at the top level that will be a sub-range of the
302 range we're inserting and make it so. */
303 {
304 struct eh_range **prev = &whole_range.first_child;
305 for (ptr = *prev; ptr;)
306 {
307 struct eh_range *next = ptr->next_sibling;
308
309 if (ptr->start_pc >= end_pc)
310 break;
311
312 if (ptr->start_pc < start_pc)
313 {
314 prev = &ptr->next_sibling;
315 }
316 else if (ptr->start_pc >= start_pc
317 && ptr->start_pc < end_pc)
318 {
319 *prev = next;
320 *first_child = ptr;
321 first_child = &ptr->next_sibling;
322 ptr->outer = h;
323 ptr->next_sibling = NULL;
324 }
325
326 ptr = next;
327 }
328 }
329
330 /* Find the right place to insert the new range. */
331 prev = &whole_range.first_child;
332 for (ptr = *prev; ptr; prev = &ptr->next_sibling, ptr = ptr->next_sibling)
333 {
334 gcc_assert (ptr->outer == NULL_EH_RANGE);
335 if (ptr->start_pc >= start_pc)
336 break;
337 }
338
339 /* And insert it there. */
340 *prev = h;
341 if (ptr)
342 {
343 h->next_sibling = ptr;
344 h->outer = ptr->outer;
345 }
346 }
347
348
349 /* if there are any handlers for this range, issue start of region */
350 static void
351 expand_start_java_handler (struct eh_range *range)
352 {
353 #if defined(DEBUG_JAVA_BINDING_LEVELS)
354 indent ();
355 fprintf (stderr, "expand start handler pc %d --> %d\n",
356 current_pc, range->end_pc);
357 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
358 pushlevel (0);
359 register_exception_range (range, range->start_pc, range->end_pc);
360 range->expanded = 1;
361 }
362
363 tree
364 prepare_eh_table_type (tree type)
365 {
366 tree exp;
367 tree *slot;
368 const char *name;
369 char *buf;
370 tree decl;
371 tree utf8_ref;
372
373 /* The "type" (match_info) in a (Java) exception table is a pointer to:
374 * a) NULL - meaning match any type in a try-finally.
375 * b) a pointer to a pointer to a class.
376 * c) a pointer to a pointer to a utf8_ref. The pointer is
377 * rewritten to point to the appropriate class. */
378
379 if (type == NULL_TREE)
380 return NULL_TREE;
381
382 if (TYPE_TO_RUNTIME_MAP (output_class) == NULL)
383 TYPE_TO_RUNTIME_MAP (output_class) = java_treetreehash_create (10);
384
385 slot = java_treetreehash_new (TYPE_TO_RUNTIME_MAP (output_class), type);
386 if (*slot != NULL)
387 return TREE_VALUE (*slot);
388
389 if (is_compiled_class (type) && !flag_indirect_dispatch)
390 {
391 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
392 buf = (char *) alloca (strlen (name) + 5);
393 sprintf (buf, "%s_ref", name);
394 decl = build_decl (input_location,
395 VAR_DECL, get_identifier (buf), ptr_type_node);
396 TREE_STATIC (decl) = 1;
397 DECL_ARTIFICIAL (decl) = 1;
398 DECL_IGNORED_P (decl) = 1;
399 TREE_READONLY (decl) = 1;
400 TREE_THIS_VOLATILE (decl) = 0;
401 DECL_INITIAL (decl) = build_class_ref (type);
402 layout_decl (decl, 0);
403 pushdecl (decl);
404 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (decl)), decl);
405 }
406 else
407 {
408 utf8_ref = build_utf8_ref (DECL_NAME (TYPE_NAME (type)));
409 name = IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (utf8_ref, 0)));
410 buf = (char *) alloca (strlen (name) + 5);
411 sprintf (buf, "%s_ref", name);
412 decl = build_decl (input_location,
413 VAR_DECL, get_identifier (buf), utf8const_ptr_type);
414 TREE_STATIC (decl) = 1;
415 DECL_ARTIFICIAL (decl) = 1;
416 DECL_IGNORED_P (decl) = 1;
417 TREE_READONLY (decl) = 1;
418 TREE_THIS_VOLATILE (decl) = 0;
419 layout_decl (decl, 0);
420 pushdecl (decl);
421 exp = build1 (ADDR_EXPR, build_pointer_type (utf8const_ptr_type), decl);
422 TYPE_CATCH_CLASSES (output_class) =
423 tree_cons (NULL, make_catch_class_record (exp, utf8_ref),
424 TYPE_CATCH_CLASSES (output_class));
425 }
426
427 exp = convert (ptr_type_node, exp);
428
429 *slot = tree_cons (type, exp, NULL_TREE);
430
431 return exp;
432 }
433
434 static int
435 expand_catch_class (void **entry, void *x ATTRIBUTE_UNUSED)
436 {
437 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
438 tree addr = TREE_VALUE ((tree)ite->value);
439 tree decl;
440 STRIP_NOPS (addr);
441 decl = TREE_OPERAND (addr, 0);
442 rest_of_decl_compilation (decl, global_bindings_p (), 0);
443 return true;
444 }
445
446 /* For every class in the TYPE_TO_RUNTIME_MAP, expand the
447 corresponding object that is used by the runtime type matcher. */
448
449 void
450 java_expand_catch_classes (tree this_class)
451 {
452 if (TYPE_TO_RUNTIME_MAP (this_class))
453 htab_traverse
454 (TYPE_TO_RUNTIME_MAP (this_class),
455 expand_catch_class, NULL);
456 }
457
458 /* Build and push the variable that will hold the exception object
459 within this function. */
460
461 static tree
462 build_exception_object_var (void)
463 {
464 tree decl = DECL_FUNCTION_EXC_OBJ (current_function_decl);
465 if (decl == NULL)
466 {
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;
471
472 DECL_FUNCTION_EXC_OBJ (current_function_decl) = decl;
473 pushdecl_function_level (decl);
474 }
475 return decl;
476 }
477
478 /* Build a reference to the jthrowable object being carried in the
479 exception header. */
480
481 tree
482 build_exception_object_ref (tree type)
483 {
484 tree obj;
485
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 = build2 (POINTER_PLUS_EXPR, TREE_TYPE (obj), obj,
491 fold_build1 (NEGATE_EXPR, sizetype,
492 TYPE_SIZE_UNIT (TREE_TYPE (obj))));
493 obj = build1 (INDIRECT_REF, type, obj);
494
495 return obj;
496 }
497
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)
502 {
503 tree handler = range->handlers;
504 if (handler)
505 {
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;
510
511 for (; handler; handler = TREE_CHAIN (handler))
512 {
513 tree type, eh_type, x;
514 tree stmts = make_node (STATEMENT_LIST);
515 tree_stmt_iterator stmts_i = tsi_last (stmts);
516
517 type = TREE_PURPOSE (handler);
518 if (type == NULL)
519 type = throwable_type_node;
520 eh_type = prepare_eh_table_type (type);
521
522 x = build_call_expr (built_in_decls[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);
526
527 x = build1 (GOTO_EXPR, void_type_node, TREE_VALUE (handler));
528 tsi_link_after (&stmts_i, x, TSI_CONTINUE_LINKING);
529
530 x = build2 (CATCH_EXPR, void_type_node, eh_type, stmts);
531 tsi_link_after (&catches_i, x, TSI_CONTINUE_LINKING);
532
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;
540 }
541
542 body = get_stmts ();
543 *body = build2 (TRY_CATCH_EXPR, void_type_node, *body, catches);
544 }
545
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) */
551 }
552
553 /* Recursive helper routine for maybe_start_handlers. */
554
555 static void
556 check_start_handlers (struct eh_range *range, int pc)
557 {
558 if (range != NULL_EH_RANGE && range->start_pc == pc)
559 {
560 check_start_handlers (range->outer, pc);
561 if (!range->expanded)
562 expand_start_java_handler (range);
563 }
564 }
565
566
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.
570
571 This is used to ensure that -fexceptions has been specified if the
572 compiler tries to use any exception-specific functions. */
573
574 static inline int
575 doing_eh (void)
576 {
577 if (! flag_exceptions)
578 {
579 static int warned = 0;
580 if (! warned)
581 {
582 error ("exception handling disabled, use -fexceptions to enable");
583 warned = 1;
584 }
585 return 0;
586 }
587 return 1;
588 }
589
590 static struct eh_range *current_range;
591
592 /* Emit any start-of-try-range starting at start_pc and ending after
593 end_pc. */
594
595 void
596 maybe_start_try (int start_pc, int end_pc)
597 {
598 struct eh_range *range;
599 if (! doing_eh ())
600 return;
601
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;
606
607 current_range = range;
608 check_start_handlers (range, start_pc);
609 }
610