PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc-3.2.3-20040701/gcc/java/except.c

#
C | 454 lines | 317 code | 57 blank | 80 comment | 62 complexity | 5b2af65686524eca449faf1afe8513ef MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0, CC-BY-SA-3.0
  1. /* Handle exceptions for GNU compiler for the Java(TM) language.
  2. Copyright (C) 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU CC; see the file COPYING. If not, write to
  14. the Free Software Foundation, 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. Java and all Java-based marks are trademarks or registered trademarks
  17. of Sun Microsystems, Inc. in the United States and other countries.
  18. The Free Software Foundation is independent of Sun Microsystems, Inc. */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "tree.h"
  22. #include "real.h"
  23. #include "rtl.h"
  24. #include "java-tree.h"
  25. #include "javaop.h"
  26. #include "java-opcodes.h"
  27. #include "jcf.h"
  28. #include "function.h"
  29. #include "except.h"
  30. #include "java-except.h"
  31. #include "toplev.h"
  32. static void expand_start_java_handler PARAMS ((struct eh_range *));
  33. static void expand_end_java_handler PARAMS ((struct eh_range *));
  34. static struct eh_range *find_handler_in_range PARAMS ((int, struct eh_range *,
  35. struct eh_range *));
  36. static void link_handler PARAMS ((struct eh_range *, struct eh_range *));
  37. static void check_start_handlers PARAMS ((struct eh_range *, int));
  38. static void free_eh_ranges PARAMS ((struct eh_range *range));
  39. extern struct obstack permanent_obstack;
  40. struct eh_range *current_method_handlers;
  41. struct eh_range *current_try_block = NULL;
  42. struct eh_range *eh_range_freelist = NULL;
  43. /* These variables are used to speed up find_handler. */
  44. static int cache_range_start, cache_range_end;
  45. static struct eh_range *cache_range;
  46. static struct eh_range *cache_next_child;
  47. /* A dummy range that represents the entire method. */
  48. struct eh_range whole_range;
  49. #if defined(DEBUG_JAVA_BINDING_LEVELS)
  50. extern int binding_depth;
  51. extern int is_class_level;
  52. extern int current_pc;
  53. extern void indent ();
  54. #endif
  55. /* Search for the most specific eh_range containing PC.
  56. Assume PC is within RANGE.
  57. CHILD is a list of children of RANGE such that any
  58. previous children have end_pc values that are too low. */
  59. static struct eh_range *
  60. find_handler_in_range (pc, range, child)
  61. int pc;
  62. struct eh_range *range;
  63. register struct eh_range *child;
  64. {
  65. for (; child != NULL; child = child->next_sibling)
  66. {
  67. if (pc < child->start_pc)
  68. break;
  69. if (pc < child->end_pc)
  70. return find_handler_in_range (pc, child, child->first_child);
  71. }
  72. cache_range = range;
  73. cache_range_start = pc;
  74. cache_next_child = child;
  75. cache_range_end = child == NULL ? range->end_pc : child->start_pc;
  76. return range;
  77. }
  78. /* Find the inner-most handler that contains PC. */
  79. struct eh_range *
  80. find_handler (pc)
  81. int pc;
  82. {
  83. struct eh_range *h;
  84. if (pc >= cache_range_start)
  85. {
  86. h = cache_range;
  87. if (pc < cache_range_end)
  88. return h;
  89. while (pc >= h->end_pc)
  90. {
  91. cache_next_child = h->next_sibling;
  92. h = h->outer;
  93. }
  94. }
  95. else
  96. {
  97. h = &whole_range;
  98. cache_next_child = h->first_child;
  99. }
  100. return find_handler_in_range (pc, h, cache_next_child);
  101. }
  102. /* Recursive helper routine for check_nested_ranges. */
  103. static void
  104. link_handler (range, outer)
  105. struct eh_range *range, *outer;
  106. {
  107. struct eh_range **ptr;
  108. if (range->start_pc == outer->start_pc && range->end_pc == outer->end_pc)
  109. {
  110. outer->handlers = chainon (outer->handlers, range->handlers);
  111. return;
  112. }
  113. /* If the new range completely encloses the `outer' range, then insert it
  114. between the outer range and its parent. */
  115. if (range->start_pc <= outer->start_pc && range->end_pc >= outer->end_pc)
  116. {
  117. range->outer = outer->outer;
  118. range->next_sibling = NULL;
  119. range->first_child = outer;
  120. {
  121. struct eh_range **pr = &(outer->outer->first_child);
  122. while (*pr != outer)
  123. pr = &(*pr)->next_sibling;
  124. *pr = range;
  125. }
  126. outer->outer = range;
  127. return;
  128. }
  129. /* Handle overlapping ranges by splitting the new range. */
  130. if (range->start_pc < outer->start_pc || range->end_pc > outer->end_pc)
  131. {
  132. struct eh_range *h
  133. = (struct eh_range *) xmalloc (sizeof (struct eh_range));
  134. if (range->start_pc < outer->start_pc)
  135. {
  136. h->start_pc = range->start_pc;
  137. h->end_pc = outer->start_pc;
  138. range->start_pc = outer->start_pc;
  139. }
  140. else
  141. {
  142. h->start_pc = outer->end_pc;
  143. h->end_pc = range->end_pc;
  144. range->end_pc = outer->end_pc;
  145. }
  146. h->first_child = NULL;
  147. h->outer = NULL;
  148. h->handlers = build_tree_list (TREE_PURPOSE (range->handlers),
  149. TREE_VALUE (range->handlers));
  150. h->next_sibling = NULL;
  151. h->expanded = 0;
  152. /* Restart both from the top to avoid having to make this
  153. function smart about reentrancy. */
  154. link_handler (h, &whole_range);
  155. link_handler (range, &whole_range);
  156. return;
  157. }
  158. ptr = &outer->first_child;
  159. for (;; ptr = &(*ptr)->next_sibling)
  160. {
  161. if (*ptr == NULL || range->end_pc <= (*ptr)->start_pc)
  162. {
  163. range->next_sibling = *ptr;
  164. range->first_child = NULL;
  165. range->outer = outer;
  166. *ptr = range;
  167. return;
  168. }
  169. else if (range->start_pc < (*ptr)->end_pc)
  170. {
  171. link_handler (range, *ptr);
  172. return;
  173. }
  174. /* end_pc > (*ptr)->start_pc && start_pc >= (*ptr)->end_pc. */
  175. }
  176. }
  177. /* The first pass of exception range processing (calling add_handler)
  178. constructs a linked list of exception ranges. We turn this into
  179. the data structure expected by the rest of the code, and also
  180. ensure that exception ranges are properly nested. */
  181. void
  182. handle_nested_ranges ()
  183. {
  184. struct eh_range *ptr, *next;
  185. ptr = whole_range.first_child;
  186. whole_range.first_child = NULL;
  187. for (; ptr; ptr = next)
  188. {
  189. next = ptr->next_sibling;
  190. ptr->next_sibling = NULL;
  191. link_handler (ptr, &whole_range);
  192. }
  193. }
  194. /* Free RANGE as well as its children and siblings. */
  195. static void
  196. free_eh_ranges (range)
  197. struct eh_range *range;
  198. {
  199. while (range)
  200. {
  201. struct eh_range *next = range->next_sibling;
  202. free_eh_ranges (range->first_child);
  203. if (range != &whole_range)
  204. free (range);
  205. range = next;
  206. }
  207. }
  208. /* Called to re-initialize the exception machinery for a new method. */
  209. void
  210. method_init_exceptions ()
  211. {
  212. free_eh_ranges (&whole_range);
  213. whole_range.start_pc = 0;
  214. whole_range.end_pc = DECL_CODE_LENGTH (current_function_decl) + 1;
  215. whole_range.outer = NULL;
  216. whole_range.first_child = NULL;
  217. whole_range.next_sibling = NULL;
  218. cache_range_start = 0xFFFFFF;
  219. }
  220. /* Add an exception range. If we already have an exception range
  221. which has the same handler and label, and the new range overlaps
  222. that one, then we simply extend the existing range. Some bytecode
  223. obfuscators generate seemingly nonoverlapping exception ranges
  224. which, when coalesced, do in fact nest correctly.
  225. This constructs an ordinary linked list which check_nested_ranges()
  226. later turns into the data structure we actually want.
  227. We expect the input to come in order of increasing START_PC. This
  228. function doesn't attempt to detect the case where two previously
  229. added disjoint ranges could be coalesced by a new range; that is
  230. what the sorting counteracts. */
  231. void
  232. add_handler (start_pc, end_pc, handler, type)
  233. int start_pc, end_pc;
  234. tree handler;
  235. tree type;
  236. {
  237. struct eh_range *ptr, *prev = NULL, *h;
  238. for (ptr = whole_range.first_child; ptr; ptr = ptr->next_sibling)
  239. {
  240. if (start_pc >= ptr->start_pc
  241. && start_pc <= ptr->end_pc
  242. && TREE_PURPOSE (ptr->handlers) == type
  243. && TREE_VALUE (ptr->handlers) == handler)
  244. {
  245. /* Already found an overlapping range, so coalesce. */
  246. ptr->end_pc = MAX (ptr->end_pc, end_pc);
  247. return;
  248. }
  249. prev = ptr;
  250. }
  251. h = (struct eh_range *) xmalloc (sizeof (struct eh_range));
  252. h->start_pc = start_pc;
  253. h->end_pc = end_pc;
  254. h->first_child = NULL;
  255. h->outer = NULL;
  256. h->handlers = build_tree_list (type, handler);
  257. h->next_sibling = NULL;
  258. h->expanded = 0;
  259. if (prev == NULL)
  260. whole_range.first_child = h;
  261. else
  262. prev->next_sibling = h;
  263. }
  264. /* if there are any handlers for this range, issue start of region */
  265. static void
  266. expand_start_java_handler (range)
  267. struct eh_range *range;
  268. {
  269. #if defined(DEBUG_JAVA_BINDING_LEVELS)
  270. indent ();
  271. fprintf (stderr, "expand start handler pc %d --> %d\n",
  272. current_pc, range->end_pc);
  273. #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
  274. range->expanded = 1;
  275. expand_eh_region_start ();
  276. }
  277. tree
  278. prepare_eh_table_type (type)
  279. tree type;
  280. {
  281. tree exp;
  282. /* The "type" (metch_info) in a (Java) exception table is one:
  283. * a) NULL - meaning match any type in a try-finally.
  284. * b) a pointer to a (ccmpiled) class (low-order bit 0).
  285. * c) a pointer to the Utf8Const name of the class, plus one
  286. * (which yields a value with low-order bit 1). */
  287. if (type == NULL_TREE)
  288. exp = NULL_TREE;
  289. else if (is_compiled_class (type))
  290. exp = build_class_ref (type);
  291. else
  292. exp = fold (build
  293. (PLUS_EXPR, ptr_type_node,
  294. build_utf8_ref (build_internal_class_name (type)),
  295. size_one_node));
  296. return exp;
  297. }
  298. /* Build a reference to the jthrowable object being carried in the
  299. exception header. */
  300. tree
  301. build_exception_object_ref (type)
  302. tree type;
  303. {
  304. tree obj;
  305. /* Java only passes object via pointer and doesn't require adjusting.
  306. The java object is immediately before the generic exception header. */
  307. obj = build (EXC_PTR_EXPR, build_pointer_type (type));
  308. obj = build (MINUS_EXPR, TREE_TYPE (obj), obj,
  309. TYPE_SIZE_UNIT (TREE_TYPE (obj)));
  310. obj = build1 (INDIRECT_REF, type, obj);
  311. return obj;
  312. }
  313. /* If there are any handlers for this range, isssue end of range,
  314. and then all handler blocks */
  315. static void
  316. expand_end_java_handler (range)
  317. struct eh_range *range;
  318. {
  319. tree handler = range->handlers;
  320. force_poplevels (range->start_pc);
  321. expand_start_all_catch ();
  322. for ( ; handler != NULL_TREE; handler = TREE_CHAIN (handler))
  323. {
  324. /* For bytecode we treat exceptions a little unusually. A
  325. `finally' clause looks like an ordinary exception handler for
  326. Throwable. The reason for this is that the bytecode has
  327. already expanded the finally logic, and we would have to do
  328. extra (and difficult) work to get this to look like a
  329. gcc-style finally clause. */
  330. tree type = TREE_PURPOSE (handler);
  331. if (type == NULL)
  332. type = throwable_type_node;
  333. expand_start_catch (type);
  334. expand_goto (TREE_VALUE (handler));
  335. expand_end_catch ();
  336. }
  337. expand_end_all_catch ();
  338. #if defined(DEBUG_JAVA_BINDING_LEVELS)
  339. indent ();
  340. fprintf (stderr, "expand end handler pc %d <-- %d\n",
  341. current_pc, range->start_pc);
  342. #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
  343. }
  344. /* Recursive helper routine for maybe_start_handlers. */
  345. static void
  346. check_start_handlers (range, pc)
  347. struct eh_range *range;
  348. int pc;
  349. {
  350. if (range != NULL_EH_RANGE && range->start_pc == pc)
  351. {
  352. check_start_handlers (range->outer, pc);
  353. if (!range->expanded)
  354. expand_start_java_handler (range);
  355. }
  356. }
  357. static struct eh_range *current_range;
  358. /* Emit any start-of-try-range starting at start_pc and ending after
  359. end_pc. */
  360. void
  361. maybe_start_try (start_pc, end_pc)
  362. int start_pc;
  363. int end_pc;
  364. {
  365. struct eh_range *range;
  366. if (! doing_eh (1))
  367. return;
  368. range = find_handler (start_pc);
  369. while (range != NULL_EH_RANGE && range->start_pc == start_pc
  370. && range->end_pc < end_pc)
  371. range = range->outer;
  372. current_range = range;
  373. check_start_handlers (range, start_pc);
  374. }
  375. /* Emit any end-of-try-range ending at end_pc and starting before
  376. start_pc. */
  377. void
  378. maybe_end_try (start_pc, end_pc)
  379. int start_pc;
  380. int end_pc;
  381. {
  382. if (! doing_eh (1))
  383. return;
  384. while (current_range != NULL_EH_RANGE && current_range->end_pc <= end_pc
  385. && current_range->start_pc >= start_pc)
  386. {
  387. expand_end_java_handler (current_range);
  388. current_range = current_range->outer;
  389. }
  390. }