OSDN Git Service

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