except.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448
  1. /* Implements exception handling.
  2. Copyright (C) 1989-2015 Free Software Foundation, Inc.
  3. Contributed by Mike Stump <mrs@cygnus.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* An exception is an event that can be "thrown" from within a
  17. function. This event can then be "caught" by the callers of
  18. the function.
  19. The representation of exceptions changes several times during
  20. the compilation process:
  21. In the beginning, in the front end, we have the GENERIC trees
  22. TRY_CATCH_EXPR, TRY_FINALLY_EXPR, WITH_CLEANUP_EXPR,
  23. CLEANUP_POINT_EXPR, CATCH_EXPR, and EH_FILTER_EXPR.
  24. During initial gimplification (gimplify.c) these are lowered
  25. to the GIMPLE_TRY, GIMPLE_CATCH, and GIMPLE_EH_FILTER nodes.
  26. The WITH_CLEANUP_EXPR and CLEANUP_POINT_EXPR nodes are converted
  27. into GIMPLE_TRY_FINALLY nodes; the others are a more direct 1-1
  28. conversion.
  29. During pass_lower_eh (tree-eh.c) we record the nested structure
  30. of the TRY nodes in EH_REGION nodes in CFUN->EH->REGION_TREE.
  31. We expand the eh_protect_cleanup_actions langhook into MUST_NOT_THROW
  32. regions at this time. We can then flatten the statements within
  33. the TRY nodes to straight-line code. Statements that had been within
  34. TRY nodes that can throw are recorded within CFUN->EH->THROW_STMT_TABLE,
  35. so that we may remember what action is supposed to be taken if
  36. a given statement does throw. During this lowering process,
  37. we create an EH_LANDING_PAD node for each EH_REGION that has
  38. some code within the function that needs to be executed if a
  39. throw does happen. We also create RESX statements that are
  40. used to transfer control from an inner EH_REGION to an outer
  41. EH_REGION. We also create EH_DISPATCH statements as placeholders
  42. for a runtime type comparison that should be made in order to
  43. select the action to perform among different CATCH and EH_FILTER
  44. regions.
  45. During pass_lower_eh_dispatch (tree-eh.c), which is run after
  46. all inlining is complete, we are able to run assign_filter_values,
  47. which allows us to map the set of types manipulated by all of the
  48. CATCH and EH_FILTER regions to a set of integers. This set of integers
  49. will be how the exception runtime communicates with the code generated
  50. within the function. We then expand the GIMPLE_EH_DISPATCH statements
  51. to a switch or conditional branches that use the argument provided by
  52. the runtime (__builtin_eh_filter) and the set of integers we computed
  53. in assign_filter_values.
  54. During pass_lower_resx (tree-eh.c), which is run near the end
  55. of optimization, we expand RESX statements. If the eh region
  56. that is outer to the RESX statement is a MUST_NOT_THROW, then
  57. the RESX expands to some form of abort statement. If the eh
  58. region that is outer to the RESX statement is within the current
  59. function, then the RESX expands to a bookkeeping call
  60. (__builtin_eh_copy_values) and a goto. Otherwise, the next
  61. handler for the exception must be within a function somewhere
  62. up the call chain, so we call back into the exception runtime
  63. (__builtin_unwind_resume).
  64. During pass_expand (cfgexpand.c), we generate REG_EH_REGION notes
  65. that create an rtl to eh_region mapping that corresponds to the
  66. gimple to eh_region mapping that had been recorded in the
  67. THROW_STMT_TABLE.
  68. Then, via finish_eh_generation, we generate the real landing pads
  69. to which the runtime will actually transfer control. These new
  70. landing pads perform whatever bookkeeping is needed by the target
  71. backend in order to resume execution within the current function.
  72. Each of these new landing pads falls through into the post_landing_pad
  73. label which had been used within the CFG up to this point. All
  74. exception edges within the CFG are redirected to the new landing pads.
  75. If the target uses setjmp to implement exceptions, the various extra
  76. calls into the runtime to register and unregister the current stack
  77. frame are emitted at this time.
  78. During pass_convert_to_eh_region_ranges (except.c), we transform
  79. the REG_EH_REGION notes attached to individual insns into
  80. non-overlapping ranges of insns bounded by NOTE_INSN_EH_REGION_BEG
  81. and NOTE_INSN_EH_REGION_END. Each insn within such ranges has the
  82. same associated action within the exception region tree, meaning
  83. that (1) the exception is caught by the same landing pad within the
  84. current function, (2) the exception is blocked by the runtime with
  85. a MUST_NOT_THROW region, or (3) the exception is not handled at all
  86. within the current function.
  87. Finally, during assembly generation, we call
  88. output_function_exception_table (except.c) to emit the tables with
  89. which the exception runtime can determine if a given stack frame
  90. handles a given exception, and if so what filter value to provide
  91. to the function when the non-local control transfer is effected.
  92. If the target uses dwarf2 unwinding to implement exceptions, then
  93. output_call_frame_info (dwarf2out.c) emits the required unwind data. */
  94. #include "config.h"
  95. #include "system.h"
  96. #include "coretypes.h"
  97. #include "tm.h"
  98. #include "rtl.h"
  99. #include "hash-set.h"
  100. #include "machmode.h"
  101. #include "vec.h"
  102. #include "double-int.h"
  103. #include "input.h"
  104. #include "alias.h"
  105. #include "symtab.h"
  106. #include "wide-int.h"
  107. #include "inchash.h"
  108. #include "real.h"
  109. #include "tree.h"
  110. #include "fold-const.h"
  111. #include "stringpool.h"
  112. #include "stor-layout.h"
  113. #include "flags.h"
  114. #include "hard-reg-set.h"
  115. #include "function.h"
  116. #include "insn-codes.h"
  117. #include "optabs.h"
  118. #include "hashtab.h"
  119. #include "statistics.h"
  120. #include "fixed-value.h"
  121. #include "insn-config.h"
  122. #include "expmed.h"
  123. #include "dojump.h"
  124. #include "explow.h"
  125. #include "calls.h"
  126. #include "emit-rtl.h"
  127. #include "varasm.h"
  128. #include "stmt.h"
  129. #include "expr.h"
  130. #include "libfuncs.h"
  131. #include "except.h"
  132. #include "output.h"
  133. #include "dwarf2asm.h"
  134. #include "dwarf2out.h"
  135. #include "dwarf2.h"
  136. #include "toplev.h"
  137. #include "hash-table.h"
  138. #include "intl.h"
  139. #include "tm_p.h"
  140. #include "target.h"
  141. #include "common/common-target.h"
  142. #include "langhooks.h"
  143. #include "predict.h"
  144. #include "dominance.h"
  145. #include "cfg.h"
  146. #include "cfgrtl.h"
  147. #include "basic-block.h"
  148. #include "hash-map.h"
  149. #include "is-a.h"
  150. #include "plugin-api.h"
  151. #include "ipa-ref.h"
  152. #include "cgraph.h"
  153. #include "diagnostic.h"
  154. #include "tree-pretty-print.h"
  155. #include "tree-pass.h"
  156. #include "cfgloop.h"
  157. #include "builtins.h"
  158. /* Provide defaults for stuff that may not be defined when using
  159. sjlj exceptions. */
  160. #ifndef EH_RETURN_DATA_REGNO
  161. #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
  162. #endif
  163. static GTY(()) int call_site_base;
  164. struct tree_hash_traits : default_hashmap_traits
  165. {
  166. static hashval_t hash (tree t) { return TREE_HASH (t); }
  167. };
  168. static GTY (()) hash_map<tree, tree, tree_hash_traits> *type_to_runtime_map;
  169. /* Describe the SjLj_Function_Context structure. */
  170. static GTY(()) tree sjlj_fc_type_node;
  171. static int sjlj_fc_call_site_ofs;
  172. static int sjlj_fc_data_ofs;
  173. static int sjlj_fc_personality_ofs;
  174. static int sjlj_fc_lsda_ofs;
  175. static int sjlj_fc_jbuf_ofs;
  176. struct GTY(()) call_site_record_d
  177. {
  178. rtx landing_pad;
  179. int action;
  180. };
  181. /* In the following structure and associated functions,
  182. we represent entries in the action table as 1-based indices.
  183. Special cases are:
  184. 0: null action record, non-null landing pad; implies cleanups
  185. -1: null action record, null landing pad; implies no action
  186. -2: no call-site entry; implies must_not_throw
  187. -3: we have yet to process outer regions
  188. Further, no special cases apply to the "next" field of the record.
  189. For next, 0 means end of list. */
  190. struct action_record
  191. {
  192. int offset;
  193. int filter;
  194. int next;
  195. };
  196. /* Hashtable helpers. */
  197. struct action_record_hasher : typed_free_remove <action_record>
  198. {
  199. typedef action_record value_type;
  200. typedef action_record compare_type;
  201. static inline hashval_t hash (const value_type *);
  202. static inline bool equal (const value_type *, const compare_type *);
  203. };
  204. inline hashval_t
  205. action_record_hasher::hash (const value_type *entry)
  206. {
  207. return entry->next * 1009 + entry->filter;
  208. }
  209. inline bool
  210. action_record_hasher::equal (const value_type *entry, const compare_type *data)
  211. {
  212. return entry->filter == data->filter && entry->next == data->next;
  213. }
  214. typedef hash_table<action_record_hasher> action_hash_type;
  215. static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
  216. eh_landing_pad *);
  217. static void dw2_build_landing_pads (void);
  218. static int collect_one_action_chain (action_hash_type *, eh_region);
  219. static int add_call_site (rtx, int, int);
  220. static void push_uleb128 (vec<uchar, va_gc> **, unsigned int);
  221. static void push_sleb128 (vec<uchar, va_gc> **, int);
  222. #ifndef HAVE_AS_LEB128
  223. static int dw2_size_of_call_site_table (int);
  224. static int sjlj_size_of_call_site_table (void);
  225. #endif
  226. static void dw2_output_call_site_table (int, int);
  227. static void sjlj_output_call_site_table (void);
  228. void
  229. init_eh (void)
  230. {
  231. if (! flag_exceptions)
  232. return;
  233. type_to_runtime_map
  234. = hash_map<tree, tree, tree_hash_traits>::create_ggc (31);
  235. /* Create the SjLj_Function_Context structure. This should match
  236. the definition in unwind-sjlj.c. */
  237. if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
  238. {
  239. tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
  240. sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
  241. f_prev = build_decl (BUILTINS_LOCATION,
  242. FIELD_DECL, get_identifier ("__prev"),
  243. build_pointer_type (sjlj_fc_type_node));
  244. DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
  245. f_cs = build_decl (BUILTINS_LOCATION,
  246. FIELD_DECL, get_identifier ("__call_site"),
  247. integer_type_node);
  248. DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
  249. tmp = build_index_type (size_int (4 - 1));
  250. tmp = build_array_type (lang_hooks.types.type_for_mode
  251. (targetm.unwind_word_mode (), 1),
  252. tmp);
  253. f_data = build_decl (BUILTINS_LOCATION,
  254. FIELD_DECL, get_identifier ("__data"), tmp);
  255. DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
  256. f_per = build_decl (BUILTINS_LOCATION,
  257. FIELD_DECL, get_identifier ("__personality"),
  258. ptr_type_node);
  259. DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
  260. f_lsda = build_decl (BUILTINS_LOCATION,
  261. FIELD_DECL, get_identifier ("__lsda"),
  262. ptr_type_node);
  263. DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
  264. #ifdef DONT_USE_BUILTIN_SETJMP
  265. #ifdef JMP_BUF_SIZE
  266. tmp = size_int (JMP_BUF_SIZE - 1);
  267. #else
  268. /* Should be large enough for most systems, if it is not,
  269. JMP_BUF_SIZE should be defined with the proper value. It will
  270. also tend to be larger than necessary for most systems, a more
  271. optimal port will define JMP_BUF_SIZE. */
  272. tmp = size_int (FIRST_PSEUDO_REGISTER + 2 - 1);
  273. #endif
  274. #else
  275. /* Compute a minimally sized jump buffer. We need room to store at
  276. least 3 pointers - stack pointer, frame pointer and return address.
  277. Plus for some targets we need room for an extra pointer - in the
  278. case of MIPS this is the global pointer. This makes a total of four
  279. pointers, but to be safe we actually allocate room for 5.
  280. If pointers are smaller than words then we allocate enough room for
  281. 5 words, just in case the backend needs this much room. For more
  282. discussion on this issue see:
  283. http://gcc.gnu.org/ml/gcc-patches/2014-05/msg00313.html. */
  284. if (POINTER_SIZE > BITS_PER_WORD)
  285. tmp = size_int (5 - 1);
  286. else
  287. tmp = size_int ((5 * BITS_PER_WORD / POINTER_SIZE) - 1);
  288. #endif
  289. tmp = build_index_type (tmp);
  290. tmp = build_array_type (ptr_type_node, tmp);
  291. f_jbuf = build_decl (BUILTINS_LOCATION,
  292. FIELD_DECL, get_identifier ("__jbuf"), tmp);
  293. #ifdef DONT_USE_BUILTIN_SETJMP
  294. /* We don't know what the alignment requirements of the
  295. runtime's jmp_buf has. Overestimate. */
  296. DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
  297. DECL_USER_ALIGN (f_jbuf) = 1;
  298. #endif
  299. DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
  300. TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
  301. TREE_CHAIN (f_prev) = f_cs;
  302. TREE_CHAIN (f_cs) = f_data;
  303. TREE_CHAIN (f_data) = f_per;
  304. TREE_CHAIN (f_per) = f_lsda;
  305. TREE_CHAIN (f_lsda) = f_jbuf;
  306. layout_type (sjlj_fc_type_node);
  307. /* Cache the interesting field offsets so that we have
  308. easy access from rtl. */
  309. sjlj_fc_call_site_ofs
  310. = (tree_to_uhwi (DECL_FIELD_OFFSET (f_cs))
  311. + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_cs)) / BITS_PER_UNIT);
  312. sjlj_fc_data_ofs
  313. = (tree_to_uhwi (DECL_FIELD_OFFSET (f_data))
  314. + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_data)) / BITS_PER_UNIT);
  315. sjlj_fc_personality_ofs
  316. = (tree_to_uhwi (DECL_FIELD_OFFSET (f_per))
  317. + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_per)) / BITS_PER_UNIT);
  318. sjlj_fc_lsda_ofs
  319. = (tree_to_uhwi (DECL_FIELD_OFFSET (f_lsda))
  320. + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_lsda)) / BITS_PER_UNIT);
  321. sjlj_fc_jbuf_ofs
  322. = (tree_to_uhwi (DECL_FIELD_OFFSET (f_jbuf))
  323. + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_jbuf)) / BITS_PER_UNIT);
  324. }
  325. }
  326. void
  327. init_eh_for_function (void)
  328. {
  329. cfun->eh = ggc_cleared_alloc<eh_status> ();
  330. /* Make sure zero'th entries are used. */
  331. vec_safe_push (cfun->eh->region_array, (eh_region)0);
  332. vec_safe_push (cfun->eh->lp_array, (eh_landing_pad)0);
  333. }
  334. /* Routines to generate the exception tree somewhat directly.
  335. These are used from tree-eh.c when processing exception related
  336. nodes during tree optimization. */
  337. static eh_region
  338. gen_eh_region (enum eh_region_type type, eh_region outer)
  339. {
  340. eh_region new_eh;
  341. /* Insert a new blank region as a leaf in the tree. */
  342. new_eh = ggc_cleared_alloc<eh_region_d> ();
  343. new_eh->type = type;
  344. new_eh->outer = outer;
  345. if (outer)
  346. {
  347. new_eh->next_peer = outer->inner;
  348. outer->inner = new_eh;
  349. }
  350. else
  351. {
  352. new_eh->next_peer = cfun->eh->region_tree;
  353. cfun->eh->region_tree = new_eh;
  354. }
  355. new_eh->index = vec_safe_length (cfun->eh->region_array);
  356. vec_safe_push (cfun->eh->region_array, new_eh);
  357. /* Copy the language's notion of whether to use __cxa_end_cleanup. */
  358. if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
  359. new_eh->use_cxa_end_cleanup = true;
  360. return new_eh;
  361. }
  362. eh_region
  363. gen_eh_region_cleanup (eh_region outer)
  364. {
  365. return gen_eh_region (ERT_CLEANUP, outer);
  366. }
  367. eh_region
  368. gen_eh_region_try (eh_region outer)
  369. {
  370. return gen_eh_region (ERT_TRY, outer);
  371. }
  372. eh_catch
  373. gen_eh_region_catch (eh_region t, tree type_or_list)
  374. {
  375. eh_catch c, l;
  376. tree type_list, type_node;
  377. gcc_assert (t->type == ERT_TRY);
  378. /* Ensure to always end up with a type list to normalize further
  379. processing, then register each type against the runtime types map. */
  380. type_list = type_or_list;
  381. if (type_or_list)
  382. {
  383. if (TREE_CODE (type_or_list) != TREE_LIST)
  384. type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
  385. type_node = type_list;
  386. for (; type_node; type_node = TREE_CHAIN (type_node))
  387. add_type_for_runtime (TREE_VALUE (type_node));
  388. }
  389. c = ggc_cleared_alloc<eh_catch_d> ();
  390. c->type_list = type_list;
  391. l = t->u.eh_try.last_catch;
  392. c->prev_catch = l;
  393. if (l)
  394. l->next_catch = c;
  395. else
  396. t->u.eh_try.first_catch = c;
  397. t->u.eh_try.last_catch = c;
  398. return c;
  399. }
  400. eh_region
  401. gen_eh_region_allowed (eh_region outer, tree allowed)
  402. {
  403. eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
  404. region->u.allowed.type_list = allowed;
  405. for (; allowed ; allowed = TREE_CHAIN (allowed))
  406. add_type_for_runtime (TREE_VALUE (allowed));
  407. return region;
  408. }
  409. eh_region
  410. gen_eh_region_must_not_throw (eh_region outer)
  411. {
  412. return gen_eh_region (ERT_MUST_NOT_THROW, outer);
  413. }
  414. eh_landing_pad
  415. gen_eh_landing_pad (eh_region region)
  416. {
  417. eh_landing_pad lp = ggc_cleared_alloc<eh_landing_pad_d> ();
  418. lp->next_lp = region->landing_pads;
  419. lp->region = region;
  420. lp->index = vec_safe_length (cfun->eh->lp_array);
  421. region->landing_pads = lp;
  422. vec_safe_push (cfun->eh->lp_array, lp);
  423. return lp;
  424. }
  425. eh_region
  426. get_eh_region_from_number_fn (struct function *ifun, int i)
  427. {
  428. return (*ifun->eh->region_array)[i];
  429. }
  430. eh_region
  431. get_eh_region_from_number (int i)
  432. {
  433. return get_eh_region_from_number_fn (cfun, i);
  434. }
  435. eh_landing_pad
  436. get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
  437. {
  438. return (*ifun->eh->lp_array)[i];
  439. }
  440. eh_landing_pad
  441. get_eh_landing_pad_from_number (int i)
  442. {
  443. return get_eh_landing_pad_from_number_fn (cfun, i);
  444. }
  445. eh_region
  446. get_eh_region_from_lp_number_fn (struct function *ifun, int i)
  447. {
  448. if (i < 0)
  449. return (*ifun->eh->region_array)[-i];
  450. else if (i == 0)
  451. return NULL;
  452. else
  453. {
  454. eh_landing_pad lp;
  455. lp = (*ifun->eh->lp_array)[i];
  456. return lp->region;
  457. }
  458. }
  459. eh_region
  460. get_eh_region_from_lp_number (int i)
  461. {
  462. return get_eh_region_from_lp_number_fn (cfun, i);
  463. }
  464. /* Returns true if the current function has exception handling regions. */
  465. bool
  466. current_function_has_exception_handlers (void)
  467. {
  468. return cfun->eh->region_tree != NULL;
  469. }
  470. /* A subroutine of duplicate_eh_regions. Copy the eh_region tree at OLD.
  471. Root it at OUTER, and apply LP_OFFSET to the lp numbers. */
  472. struct duplicate_eh_regions_data
  473. {
  474. duplicate_eh_regions_map label_map;
  475. void *label_map_data;
  476. hash_map<void *, void *> *eh_map;
  477. };
  478. static void
  479. duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
  480. eh_region old_r, eh_region outer)
  481. {
  482. eh_landing_pad old_lp, new_lp;
  483. eh_region new_r;
  484. new_r = gen_eh_region (old_r->type, outer);
  485. gcc_assert (!data->eh_map->put (old_r, new_r));
  486. switch (old_r->type)
  487. {
  488. case ERT_CLEANUP:
  489. break;
  490. case ERT_TRY:
  491. {
  492. eh_catch oc, nc;
  493. for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
  494. {
  495. /* We should be doing all our region duplication before and
  496. during inlining, which is before filter lists are created. */
  497. gcc_assert (oc->filter_list == NULL);
  498. nc = gen_eh_region_catch (new_r, oc->type_list);
  499. nc->label = data->label_map (oc->label, data->label_map_data);
  500. }
  501. }
  502. break;
  503. case ERT_ALLOWED_EXCEPTIONS:
  504. new_r->u.allowed.type_list = old_r->u.allowed.type_list;
  505. if (old_r->u.allowed.label)
  506. new_r->u.allowed.label
  507. = data->label_map (old_r->u.allowed.label, data->label_map_data);
  508. else
  509. new_r->u.allowed.label = NULL_TREE;
  510. break;
  511. case ERT_MUST_NOT_THROW:
  512. new_r->u.must_not_throw.failure_loc =
  513. LOCATION_LOCUS (old_r->u.must_not_throw.failure_loc);
  514. new_r->u.must_not_throw.failure_decl =
  515. old_r->u.must_not_throw.failure_decl;
  516. break;
  517. }
  518. for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
  519. {
  520. /* Don't bother copying unused landing pads. */
  521. if (old_lp->post_landing_pad == NULL)
  522. continue;
  523. new_lp = gen_eh_landing_pad (new_r);
  524. gcc_assert (!data->eh_map->put (old_lp, new_lp));
  525. new_lp->post_landing_pad
  526. = data->label_map (old_lp->post_landing_pad, data->label_map_data);
  527. EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
  528. }
  529. /* Make sure to preserve the original use of __cxa_end_cleanup. */
  530. new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
  531. for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
  532. duplicate_eh_regions_1 (data, old_r, new_r);
  533. }
  534. /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
  535. the current function and root the tree below OUTER_REGION.
  536. The special case of COPY_REGION of NULL means all regions.
  537. Remap labels using MAP/MAP_DATA callback. Return a pointer map
  538. that allows the caller to remap uses of both EH regions and
  539. EH landing pads. */
  540. hash_map<void *, void *> *
  541. duplicate_eh_regions (struct function *ifun,
  542. eh_region copy_region, int outer_lp,
  543. duplicate_eh_regions_map map, void *map_data)
  544. {
  545. struct duplicate_eh_regions_data data;
  546. eh_region outer_region;
  547. #ifdef ENABLE_CHECKING
  548. verify_eh_tree (ifun);
  549. #endif
  550. data.label_map = map;
  551. data.label_map_data = map_data;
  552. data.eh_map = new hash_map<void *, void *>;
  553. outer_region = get_eh_region_from_lp_number (outer_lp);
  554. /* Copy all the regions in the subtree. */
  555. if (copy_region)
  556. duplicate_eh_regions_1 (&data, copy_region, outer_region);
  557. else
  558. {
  559. eh_region r;
  560. for (r = ifun->eh->region_tree; r ; r = r->next_peer)
  561. duplicate_eh_regions_1 (&data, r, outer_region);
  562. }
  563. #ifdef ENABLE_CHECKING
  564. verify_eh_tree (cfun);
  565. #endif
  566. return data.eh_map;
  567. }
  568. /* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
  569. eh_region
  570. eh_region_outermost (struct function *ifun, eh_region region_a,
  571. eh_region region_b)
  572. {
  573. sbitmap b_outer;
  574. gcc_assert (ifun->eh->region_array);
  575. gcc_assert (ifun->eh->region_tree);
  576. b_outer = sbitmap_alloc (ifun->eh->region_array->length ());
  577. bitmap_clear (b_outer);
  578. do
  579. {
  580. bitmap_set_bit (b_outer, region_b->index);
  581. region_b = region_b->outer;
  582. }
  583. while (region_b);
  584. do
  585. {
  586. if (bitmap_bit_p (b_outer, region_a->index))
  587. break;
  588. region_a = region_a->outer;
  589. }
  590. while (region_a);
  591. sbitmap_free (b_outer);
  592. return region_a;
  593. }
  594. void
  595. add_type_for_runtime (tree type)
  596. {
  597. /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
  598. if (TREE_CODE (type) == NOP_EXPR)
  599. return;
  600. bool existed = false;
  601. tree *slot = &type_to_runtime_map->get_or_insert (type, &existed);
  602. if (!existed)
  603. *slot = lang_hooks.eh_runtime_type (type);
  604. }
  605. tree
  606. lookup_type_for_runtime (tree type)
  607. {
  608. /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
  609. if (TREE_CODE (type) == NOP_EXPR)
  610. return type;
  611. /* We should have always inserted the data earlier. */
  612. return *type_to_runtime_map->get (type);
  613. }
  614. /* Represent an entry in @TTypes for either catch actions
  615. or exception filter actions. */
  616. struct ttypes_filter {
  617. tree t;
  618. int filter;
  619. };
  620. /* Helper for ttypes_filter hashing. */
  621. struct ttypes_filter_hasher : typed_free_remove <ttypes_filter>
  622. {
  623. typedef ttypes_filter value_type;
  624. typedef tree_node compare_type;
  625. static inline hashval_t hash (const value_type *);
  626. static inline bool equal (const value_type *, const compare_type *);
  627. };
  628. /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
  629. (a tree) for a @TTypes type node we are thinking about adding. */
  630. inline bool
  631. ttypes_filter_hasher::equal (const value_type *entry, const compare_type *data)
  632. {
  633. return entry->t == data;
  634. }
  635. inline hashval_t
  636. ttypes_filter_hasher::hash (const value_type *entry)
  637. {
  638. return TREE_HASH (entry->t);
  639. }
  640. typedef hash_table<ttypes_filter_hasher> ttypes_hash_type;
  641. /* Helper for ehspec hashing. */
  642. struct ehspec_hasher : typed_free_remove <ttypes_filter>
  643. {
  644. typedef ttypes_filter value_type;
  645. typedef ttypes_filter compare_type;
  646. static inline hashval_t hash (const value_type *);
  647. static inline bool equal (const value_type *, const compare_type *);
  648. };
  649. /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
  650. exception specification list we are thinking about adding. */
  651. /* ??? Currently we use the type lists in the order given. Someone
  652. should put these in some canonical order. */
  653. inline bool
  654. ehspec_hasher::equal (const value_type *entry, const compare_type *data)
  655. {
  656. return type_list_equal (entry->t, data->t);
  657. }
  658. /* Hash function for exception specification lists. */
  659. inline hashval_t
  660. ehspec_hasher::hash (const value_type *entry)
  661. {
  662. hashval_t h = 0;
  663. tree list;
  664. for (list = entry->t; list ; list = TREE_CHAIN (list))
  665. h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
  666. return h;
  667. }
  668. typedef hash_table<ehspec_hasher> ehspec_hash_type;
  669. /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
  670. to speed up the search. Return the filter value to be used. */
  671. static int
  672. add_ttypes_entry (ttypes_hash_type *ttypes_hash, tree type)
  673. {
  674. struct ttypes_filter **slot, *n;
  675. slot = ttypes_hash->find_slot_with_hash (type, (hashval_t) TREE_HASH (type),
  676. INSERT);
  677. if ((n = *slot) == NULL)
  678. {
  679. /* Filter value is a 1 based table index. */
  680. n = XNEW (struct ttypes_filter);
  681. n->t = type;
  682. n->filter = vec_safe_length (cfun->eh->ttype_data) + 1;
  683. *slot = n;
  684. vec_safe_push (cfun->eh->ttype_data, type);
  685. }
  686. return n->filter;
  687. }
  688. /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
  689. to speed up the search. Return the filter value to be used. */
  690. static int
  691. add_ehspec_entry (ehspec_hash_type *ehspec_hash, ttypes_hash_type *ttypes_hash,
  692. tree list)
  693. {
  694. struct ttypes_filter **slot, *n;
  695. struct ttypes_filter dummy;
  696. dummy.t = list;
  697. slot = ehspec_hash->find_slot (&dummy, INSERT);
  698. if ((n = *slot) == NULL)
  699. {
  700. int len;
  701. if (targetm.arm_eabi_unwinder)
  702. len = vec_safe_length (cfun->eh->ehspec_data.arm_eabi);
  703. else
  704. len = vec_safe_length (cfun->eh->ehspec_data.other);
  705. /* Filter value is a -1 based byte index into a uleb128 buffer. */
  706. n = XNEW (struct ttypes_filter);
  707. n->t = list;
  708. n->filter = -(len + 1);
  709. *slot = n;
  710. /* Generate a 0 terminated list of filter values. */
  711. for (; list ; list = TREE_CHAIN (list))
  712. {
  713. if (targetm.arm_eabi_unwinder)
  714. vec_safe_push (cfun->eh->ehspec_data.arm_eabi, TREE_VALUE (list));
  715. else
  716. {
  717. /* Look up each type in the list and encode its filter
  718. value as a uleb128. */
  719. push_uleb128 (&cfun->eh->ehspec_data.other,
  720. add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
  721. }
  722. }
  723. if (targetm.arm_eabi_unwinder)
  724. vec_safe_push (cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
  725. else
  726. vec_safe_push (cfun->eh->ehspec_data.other, (uchar)0);
  727. }
  728. return n->filter;
  729. }
  730. /* Generate the action filter values to be used for CATCH and
  731. ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
  732. we use lots of landing pads, and so every type or list can share
  733. the same filter value, which saves table space. */
  734. void
  735. assign_filter_values (void)
  736. {
  737. int i;
  738. eh_region r;
  739. eh_catch c;
  740. vec_alloc (cfun->eh->ttype_data, 16);
  741. if (targetm.arm_eabi_unwinder)
  742. vec_alloc (cfun->eh->ehspec_data.arm_eabi, 64);
  743. else
  744. vec_alloc (cfun->eh->ehspec_data.other, 64);
  745. ehspec_hash_type ehspec (31);
  746. ttypes_hash_type ttypes (31);
  747. for (i = 1; vec_safe_iterate (cfun->eh->region_array, i, &r); ++i)
  748. {
  749. if (r == NULL)
  750. continue;
  751. switch (r->type)
  752. {
  753. case ERT_TRY:
  754. for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
  755. {
  756. /* Whatever type_list is (NULL or true list), we build a list
  757. of filters for the region. */
  758. c->filter_list = NULL_TREE;
  759. if (c->type_list != NULL)
  760. {
  761. /* Get a filter value for each of the types caught and store
  762. them in the region's dedicated list. */
  763. tree tp_node = c->type_list;
  764. for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
  765. {
  766. int flt
  767. = add_ttypes_entry (&ttypes, TREE_VALUE (tp_node));
  768. tree flt_node = build_int_cst (integer_type_node, flt);
  769. c->filter_list
  770. = tree_cons (NULL_TREE, flt_node, c->filter_list);
  771. }
  772. }
  773. else
  774. {
  775. /* Get a filter value for the NULL list also since it
  776. will need an action record anyway. */
  777. int flt = add_ttypes_entry (&ttypes, NULL);
  778. tree flt_node = build_int_cst (integer_type_node, flt);
  779. c->filter_list
  780. = tree_cons (NULL_TREE, flt_node, NULL);
  781. }
  782. }
  783. break;
  784. case ERT_ALLOWED_EXCEPTIONS:
  785. r->u.allowed.filter
  786. = add_ehspec_entry (&ehspec, &ttypes, r->u.allowed.type_list);
  787. break;
  788. default:
  789. break;
  790. }
  791. }
  792. }
  793. /* Emit SEQ into basic block just before INSN (that is assumed to be
  794. first instruction of some existing BB and return the newly
  795. produced block. */
  796. static basic_block
  797. emit_to_new_bb_before (rtx_insn *seq, rtx insn)
  798. {
  799. rtx_insn *last;
  800. basic_block bb;
  801. edge e;
  802. edge_iterator ei;
  803. /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
  804. call), we don't want it to go into newly created landing pad or other EH
  805. construct. */
  806. for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
  807. if (e->flags & EDGE_FALLTHRU)
  808. force_nonfallthru (e);
  809. else
  810. ei_next (&ei);
  811. last = emit_insn_before (seq, insn);
  812. if (BARRIER_P (last))
  813. last = PREV_INSN (last);
  814. bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
  815. update_bb_for_insn (bb);
  816. bb->flags |= BB_SUPERBLOCK;
  817. return bb;
  818. }
  819. /* A subroutine of dw2_build_landing_pads, also used for edge splitting
  820. at the rtl level. Emit the code required by the target at a landing
  821. pad for the given region. */
  822. void
  823. expand_dw2_landing_pad_for_region (eh_region region)
  824. {
  825. #ifdef HAVE_exception_receiver
  826. if (HAVE_exception_receiver)
  827. emit_insn (gen_exception_receiver ());
  828. else
  829. #endif
  830. #ifdef HAVE_nonlocal_goto_receiver
  831. if (HAVE_nonlocal_goto_receiver)
  832. emit_insn (gen_nonlocal_goto_receiver ());
  833. else
  834. #endif
  835. { /* Nothing */ }
  836. if (region->exc_ptr_reg)
  837. emit_move_insn (region->exc_ptr_reg,
  838. gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
  839. if (region->filter_reg)
  840. emit_move_insn (region->filter_reg,
  841. gen_rtx_REG (targetm.eh_return_filter_mode (),
  842. EH_RETURN_DATA_REGNO (1)));
  843. }
  844. /* Expand the extra code needed at landing pads for dwarf2 unwinding. */
  845. static void
  846. dw2_build_landing_pads (void)
  847. {
  848. int i;
  849. eh_landing_pad lp;
  850. int e_flags = EDGE_FALLTHRU;
  851. /* If we're going to partition blocks, we need to be able to add
  852. new landing pads later, which means that we need to hold on to
  853. the post-landing-pad block. Prevent it from being merged away.
  854. We'll remove this bit after partitioning. */
  855. if (flag_reorder_blocks_and_partition)
  856. e_flags |= EDGE_PRESERVE;
  857. for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
  858. {
  859. basic_block bb;
  860. rtx_insn *seq;
  861. edge e;
  862. if (lp == NULL || lp->post_landing_pad == NULL)
  863. continue;
  864. start_sequence ();
  865. lp->landing_pad = gen_label_rtx ();
  866. emit_label (lp->landing_pad);
  867. LABEL_PRESERVE_P (lp->landing_pad) = 1;
  868. expand_dw2_landing_pad_for_region (lp->region);
  869. seq = get_insns ();
  870. end_sequence ();
  871. bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
  872. e = make_edge (bb, bb->next_bb, e_flags);
  873. e->count = bb->count;
  874. e->probability = REG_BR_PROB_BASE;
  875. if (current_loops)
  876. {
  877. struct loop *loop = bb->next_bb->loop_father;
  878. /* If we created a pre-header block, add the new block to the
  879. outer loop, otherwise to the loop itself. */
  880. if (bb->next_bb == loop->header)
  881. add_bb_to_loop (bb, loop_outer (loop));
  882. else
  883. add_bb_to_loop (bb, loop);
  884. }
  885. }
  886. }
  887. static vec<int> sjlj_lp_call_site_index;
  888. /* Process all active landing pads. Assign each one a compact dispatch
  889. index, and a call-site index. */
  890. static int
  891. sjlj_assign_call_site_values (void)
  892. {
  893. action_hash_type ar_hash (31);
  894. int i, disp_index;
  895. eh_landing_pad lp;
  896. vec_alloc (crtl->eh.action_record_data, 64);
  897. disp_index = 0;
  898. call_site_base = 1;
  899. for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
  900. if (lp && lp->post_landing_pad)
  901. {
  902. int action, call_site;
  903. /* First: build the action table. */
  904. action = collect_one_action_chain (&ar_hash, lp->region);
  905. /* Next: assign call-site values. If dwarf2 terms, this would be
  906. the region number assigned by convert_to_eh_region_ranges, but
  907. handles no-action and must-not-throw differently. */
  908. /* Map must-not-throw to otherwise unused call-site index 0. */
  909. if (action == -2)
  910. call_site = 0;
  911. /* Map no-action to otherwise unused call-site index -1. */
  912. else if (action == -1)
  913. call_site = -1;
  914. /* Otherwise, look it up in the table. */
  915. else
  916. call_site = add_call_site (GEN_INT (disp_index), action, 0);
  917. sjlj_lp_call_site_index[i] = call_site;
  918. disp_index++;
  919. }
  920. return disp_index;
  921. }
  922. /* Emit code to record the current call-site index before every
  923. insn that can throw. */
  924. static void
  925. sjlj_mark_call_sites (void)
  926. {
  927. int last_call_site = -2;
  928. rtx_insn *insn;
  929. rtx mem;
  930. for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
  931. {
  932. eh_landing_pad lp;
  933. eh_region r;
  934. bool nothrow;
  935. int this_call_site;
  936. rtx_insn *before, *p;
  937. /* Reset value tracking at extended basic block boundaries. */
  938. if (LABEL_P (insn))
  939. last_call_site = -2;
  940. if (! INSN_P (insn))
  941. continue;
  942. nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
  943. if (nothrow)
  944. continue;
  945. if (lp)
  946. this_call_site = sjlj_lp_call_site_index[lp->index];
  947. else if (r == NULL)
  948. {
  949. /* Calls (and trapping insns) without notes are outside any
  950. exception handling region in this function. Mark them as
  951. no action. */
  952. this_call_site = -1;
  953. }
  954. else
  955. {
  956. gcc_assert (r->type == ERT_MUST_NOT_THROW);
  957. this_call_site = 0;
  958. }
  959. if (this_call_site != -1)
  960. crtl->uses_eh_lsda = 1;
  961. if (this_call_site == last_call_site)
  962. continue;
  963. /* Don't separate a call from it's argument loads. */
  964. before = insn;
  965. if (CALL_P (insn))
  966. before = find_first_parameter_load (insn, NULL);
  967. start_sequence ();
  968. mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
  969. sjlj_fc_call_site_ofs);
  970. emit_move_insn (mem, gen_int_mode (this_call_site, GET_MODE (mem)));
  971. p = get_insns ();
  972. end_sequence ();
  973. emit_insn_before (p, before);
  974. last_call_site = this_call_site;
  975. }
  976. }
  977. /* Construct the SjLj_Function_Context. */
  978. static void
  979. sjlj_emit_function_enter (rtx_code_label *dispatch_label)
  980. {
  981. rtx_insn *fn_begin, *seq;
  982. rtx fc, mem;
  983. bool fn_begin_outside_block;
  984. rtx personality = get_personality_function (current_function_decl);
  985. fc = crtl->eh.sjlj_fc;
  986. start_sequence ();
  987. /* We're storing this libcall's address into memory instead of
  988. calling it directly. Thus, we must call assemble_external_libcall
  989. here, as we can not depend on emit_library_call to do it for us. */
  990. assemble_external_libcall (personality);
  991. mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
  992. emit_move_insn (mem, personality);
  993. mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
  994. if (crtl->uses_eh_lsda)
  995. {
  996. char buf[20];
  997. rtx sym;
  998. ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
  999. sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
  1000. SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
  1001. emit_move_insn (mem, sym);
  1002. }
  1003. else
  1004. emit_move_insn (mem, const0_rtx);
  1005. if (dispatch_label)
  1006. {
  1007. #ifdef DONT_USE_BUILTIN_SETJMP
  1008. rtx x;
  1009. x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
  1010. TYPE_MODE (integer_type_node), 1,
  1011. plus_constant (Pmode, XEXP (fc, 0),
  1012. sjlj_fc_jbuf_ofs), Pmode);
  1013. emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
  1014. TYPE_MODE (integer_type_node), 0,
  1015. dispatch_label, REG_BR_PROB_BASE / 100);
  1016. #else
  1017. expand_builtin_setjmp_setup (plus_constant (Pmode, XEXP (fc, 0),
  1018. sjlj_fc_jbuf_ofs),
  1019. dispatch_label);
  1020. #endif
  1021. }
  1022. emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
  1023. 1, XEXP (fc, 0), Pmode);
  1024. seq = get_insns ();
  1025. end_sequence ();
  1026. /* ??? Instead of doing this at the beginning of the function,
  1027. do this in a block that is at loop level 0 and dominates all
  1028. can_throw_internal instructions. */
  1029. fn_begin_outside_block = true;
  1030. for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
  1031. if (NOTE_P (fn_begin))
  1032. {
  1033. if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
  1034. break;
  1035. else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
  1036. fn_begin_outside_block = false;
  1037. }
  1038. if (fn_begin_outside_block)
  1039. insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
  1040. else
  1041. emit_insn_after (seq, fn_begin);
  1042. }
  1043. /* Call back from expand_function_end to know where we should put
  1044. the call to unwind_sjlj_unregister_libfunc if needed. */
  1045. void
  1046. sjlj_emit_function_exit_after (rtx_insn *after)
  1047. {
  1048. crtl->eh.sjlj_exit_after = after;
  1049. }
  1050. static void
  1051. sjlj_emit_function_exit (void)
  1052. {
  1053. rtx_insn *seq, *insn;
  1054. start_sequence ();
  1055. emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
  1056. 1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
  1057. seq = get_insns ();
  1058. end_sequence ();
  1059. /* ??? Really this can be done in any block at loop level 0 that
  1060. post-dominates all can_throw_internal instructions. This is
  1061. the last possible moment. */
  1062. insn = crtl->eh.sjlj_exit_after;
  1063. if (LABEL_P (insn))
  1064. insn = NEXT_INSN (insn);
  1065. emit_insn_after (seq, insn);
  1066. }
  1067. static void
  1068. sjlj_emit_dispatch_table (rtx_code_label *dispatch_label, int num_dispatch)
  1069. {
  1070. machine_mode unwind_word_mode = targetm.unwind_word_mode ();
  1071. machine_mode filter_mode = targetm.eh_return_filter_mode ();
  1072. eh_landing_pad lp;
  1073. rtx mem, fc, before, exc_ptr_reg, filter_reg;
  1074. rtx_insn *seq;
  1075. rtx first_reachable_label;
  1076. basic_block bb;
  1077. eh_region r;
  1078. edge e;
  1079. int i, disp_index;
  1080. vec<tree> dispatch_labels = vNULL;
  1081. fc = crtl->eh.sjlj_fc;
  1082. start_sequence ();
  1083. emit_label (dispatch_label);
  1084. #ifndef DONT_USE_BUILTIN_SETJMP
  1085. expand_builtin_setjmp_receiver (dispatch_label);
  1086. /* The caller of expand_builtin_setjmp_receiver is responsible for
  1087. making sure that the label doesn't vanish. The only other caller
  1088. is the expander for __builtin_setjmp_receiver, which places this
  1089. label on the nonlocal_goto_label list. Since we're modeling these
  1090. CFG edges more exactly, we can use the forced_labels list instead. */
  1091. LABEL_PRESERVE_P (dispatch_label) = 1;
  1092. forced_labels
  1093. = gen_rtx_INSN_LIST (VOIDmode, dispatch_label, forced_labels);
  1094. #endif
  1095. /* Load up exc_ptr and filter values from the function context. */
  1096. mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
  1097. if (unwind_word_mode != ptr_mode)
  1098. {
  1099. #ifdef POINTERS_EXTEND_UNSIGNED
  1100. mem = convert_memory_address (ptr_mode, mem);
  1101. #else
  1102. mem = convert_to_mode (ptr_mode, mem, 0);
  1103. #endif
  1104. }
  1105. exc_ptr_reg = force_reg (ptr_mode, mem);
  1106. mem = adjust_address (fc, unwind_word_mode,
  1107. sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
  1108. if (unwind_word_mode != filter_mode)
  1109. mem = convert_to_mode (filter_mode, mem, 0);
  1110. filter_reg = force_reg (filter_mode, mem);
  1111. /* Jump to one of the directly reachable regions. */
  1112. disp_index = 0;
  1113. first_reachable_label = NULL;
  1114. /* If there's exactly one call site in the function, don't bother
  1115. generating a switch statement. */
  1116. if (num_dispatch > 1)
  1117. dispatch_labels.create (num_dispatch);
  1118. for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
  1119. if (lp && lp->post_landing_pad)
  1120. {
  1121. rtx_insn *seq2;
  1122. rtx label;
  1123. start_sequence ();
  1124. lp->landing_pad = dispatch_label;
  1125. if (num_dispatch > 1)
  1126. {
  1127. tree t_label, case_elt, t;
  1128. t_label = create_artificial_label (UNKNOWN_LOCATION);
  1129. t = build_int_cst (integer_type_node, disp_index);
  1130. case_elt = build_case_label (t, NULL, t_label);
  1131. dispatch_labels.quick_push (case_elt);
  1132. label = label_rtx (t_label);
  1133. }
  1134. else
  1135. label = gen_label_rtx ();
  1136. if (disp_index == 0)
  1137. first_reachable_label = label;
  1138. emit_label (label);
  1139. r = lp->region;
  1140. if (r->exc_ptr_reg)
  1141. emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
  1142. if (r->filter_reg)
  1143. emit_move_insn (r->filter_reg, filter_reg);
  1144. seq2 = get_insns ();
  1145. end_sequence ();
  1146. before = label_rtx (lp->post_landing_pad);
  1147. bb = emit_to_new_bb_before (seq2, before);
  1148. e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
  1149. e->count = bb->count;
  1150. e->probability = REG_BR_PROB_BASE;
  1151. if (current_loops)
  1152. {
  1153. struct loop *loop = bb->next_bb->loop_father;
  1154. /* If we created a pre-header block, add the new block to the
  1155. outer loop, otherwise to the loop itself. */
  1156. if (bb->next_bb == loop->header)
  1157. add_bb_to_loop (bb, loop_outer (loop));
  1158. else
  1159. add_bb_to_loop (bb, loop);
  1160. /* ??? For multiple dispatches we will end up with edges
  1161. from the loop tree root into this loop, making it a
  1162. multiple-entry loop. Discard all affected loops. */
  1163. if (num_dispatch > 1)
  1164. {
  1165. for (loop = bb->loop_father;
  1166. loop_outer (loop); loop = loop_outer (loop))
  1167. mark_loop_for_removal (loop);
  1168. }
  1169. }
  1170. disp_index++;
  1171. }
  1172. gcc_assert (disp_index == num_dispatch);
  1173. if (num_dispatch > 1)
  1174. {
  1175. rtx disp = adjust_address (fc, TYPE_MODE (integer_type_node),
  1176. sjlj_fc_call_site_ofs);
  1177. expand_sjlj_dispatch_table (disp, dispatch_labels);
  1178. }
  1179. seq = get_insns ();
  1180. end_sequence ();
  1181. bb = emit_to_new_bb_before (seq, first_reachable_label);
  1182. if (num_dispatch == 1)
  1183. {
  1184. e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
  1185. e->count = bb->count;
  1186. e->probability = REG_BR_PROB_BASE;
  1187. if (current_loops)
  1188. {
  1189. struct loop *loop = bb->next_bb->loop_father;
  1190. /* If we created a pre-header block, add the new block to the
  1191. outer loop, otherwise to the loop itself. */
  1192. if (bb->next_bb == loop->header)
  1193. add_bb_to_loop (bb, loop_outer (loop));
  1194. else
  1195. add_bb_to_loop (bb, loop);
  1196. }
  1197. }
  1198. else
  1199. {
  1200. /* We are not wiring up edges here, but as the dispatcher call
  1201. is at function begin simply associate the block with the
  1202. outermost (non-)loop. */
  1203. if (current_loops)
  1204. add_bb_to_loop (bb, current_loops->tree_root);
  1205. }
  1206. }
  1207. static void
  1208. sjlj_build_landing_pads (void)
  1209. {
  1210. int num_dispatch;
  1211. num_dispatch = vec_safe_length (cfun->eh->lp_array);
  1212. if (num_dispatch == 0)
  1213. return;
  1214. sjlj_lp_call_site_index.safe_grow_cleared (num_dispatch);
  1215. num_dispatch = sjlj_assign_call_site_values ();
  1216. if (num_dispatch > 0)
  1217. {
  1218. rtx_code_label *dispatch_label = gen_label_rtx ();
  1219. int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
  1220. TYPE_MODE (sjlj_fc_type_node),
  1221. TYPE_ALIGN (sjlj_fc_type_node));
  1222. crtl->eh.sjlj_fc
  1223. = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
  1224. int_size_in_bytes (sjlj_fc_type_node),
  1225. align);
  1226. sjlj_mark_call_sites ();
  1227. sjlj_emit_function_enter (dispatch_label);
  1228. sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
  1229. sjlj_emit_function_exit ();
  1230. }
  1231. /* If we do not have any landing pads, we may still need to register a
  1232. personality routine and (empty) LSDA to handle must-not-throw regions. */
  1233. else if (function_needs_eh_personality (cfun) != eh_personality_none)
  1234. {
  1235. int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
  1236. TYPE_MODE (sjlj_fc_type_node),
  1237. TYPE_ALIGN (sjlj_fc_type_node));
  1238. crtl->eh.sjlj_fc
  1239. = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
  1240. int_size_in_bytes (sjlj_fc_type_node),
  1241. align);
  1242. sjlj_mark_call_sites ();
  1243. sjlj_emit_function_enter (NULL);
  1244. sjlj_emit_function_exit ();
  1245. }
  1246. sjlj_lp_call_site_index.release ();
  1247. }
  1248. /* After initial rtl generation, call back to finish generating
  1249. exception support code. */
  1250. void
  1251. finish_eh_generation (void)
  1252. {
  1253. basic_block bb;
  1254. /* Construct the landing pads. */
  1255. if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
  1256. sjlj_build_landing_pads ();
  1257. else
  1258. dw2_build_landing_pads ();
  1259. break_superblocks ();
  1260. if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
  1261. /* Kludge for Alpha (see alpha_gp_save_rtx). */
  1262. || single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->insns.r)
  1263. commit_edge_insertions ();
  1264. /* Redirect all EH edges from the post_landing_pad to the landing pad. */
  1265. FOR_EACH_BB_FN (bb, cfun)
  1266. {
  1267. eh_landing_pad lp;
  1268. edge_iterator ei;
  1269. edge e;
  1270. lp = get_eh_landing_pad_from_rtx (BB_END (bb));
  1271. FOR_EACH_EDGE (e, ei, bb->succs)
  1272. if (e->flags & EDGE_EH)
  1273. break;
  1274. /* We should not have generated any new throwing insns during this
  1275. pass, and we should not have lost any EH edges, so we only need
  1276. to handle two cases here:
  1277. (1) reachable handler and an existing edge to post-landing-pad,
  1278. (2) no reachable handler and no edge. */
  1279. gcc_assert ((lp != NULL) == (e != NULL));
  1280. if (lp != NULL)
  1281. {
  1282. gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
  1283. redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
  1284. e->flags |= (CALL_P (BB_END (bb))
  1285. ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
  1286. : EDGE_ABNORMAL);
  1287. }
  1288. }
  1289. }
  1290. /* This section handles removing dead code for flow. */
  1291. void
  1292. remove_eh_landing_pad (eh_landing_pad lp)
  1293. {
  1294. eh_landing_pad *pp;
  1295. for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
  1296. continue;
  1297. *pp = lp->next_lp;
  1298. if (lp->post_landing_pad)
  1299. EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
  1300. (*cfun->eh->lp_array)[lp->index] = NULL;
  1301. }
  1302. /* Splice the EH region at PP from the region tree. */
  1303. static void
  1304. remove_eh_handler_splicer (eh_region *pp)
  1305. {
  1306. eh_region region = *pp;
  1307. eh_landing_pad lp;
  1308. for (lp = region->landing_pads; lp ; lp = lp->next_lp)
  1309. {
  1310. if (lp->post_landing_pad)
  1311. EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
  1312. (*cfun->eh->lp_array)[lp->index] = NULL;
  1313. }
  1314. if (region->inner)
  1315. {
  1316. eh_region p, outer;
  1317. outer = region->outer;
  1318. *pp = p = region->inner;
  1319. do
  1320. {
  1321. p->outer = outer;
  1322. pp = &p->next_peer;
  1323. p = *pp;
  1324. }
  1325. while (p);
  1326. }
  1327. *pp = region->next_peer;
  1328. (*cfun->eh->region_array)[region->index] = NULL;
  1329. }
  1330. /* Splice a single EH region REGION from the region tree.
  1331. To unlink REGION, we need to find the pointer to it with a relatively
  1332. expensive search in REGION's outer region. If you are going to
  1333. remove a number of handlers, using remove_unreachable_eh_regions may
  1334. be a better option. */
  1335. void
  1336. remove_eh_handler (eh_region region)
  1337. {
  1338. eh_region *pp, *pp_start, p, outer;
  1339. outer = region->outer;
  1340. if (outer)
  1341. pp_start = &outer->inner;
  1342. else
  1343. pp_start = &cfun->eh->region_tree;
  1344. for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
  1345. continue;
  1346. remove_eh_handler_splicer (pp);
  1347. }
  1348. /* Worker for remove_unreachable_eh_regions.
  1349. PP is a pointer to the region to start a region tree depth-first
  1350. search from. R_REACHABLE is the set of regions that have to be
  1351. preserved. */
  1352. static void
  1353. remove_unreachable_eh_regions_worker (eh_region *pp, sbitmap r_reachable)
  1354. {
  1355. while (*pp)
  1356. {
  1357. eh_region region = *pp;
  1358. remove_unreachable_eh_regions_worker (&region->inner, r_reachable);
  1359. if (!bitmap_bit_p (r_reachable, region->index))
  1360. remove_eh_handler_splicer (pp);
  1361. else
  1362. pp = &region->next_peer;
  1363. }
  1364. }
  1365. /* Splice all EH regions *not* marked in R_REACHABLE from the region tree.
  1366. Do this by traversing the EH tree top-down and splice out regions that
  1367. are not marked. By removing regions from the leaves, we avoid costly
  1368. searches in the region tree. */
  1369. void
  1370. remove_unreachable_eh_regions (sbitmap r_reachable)
  1371. {
  1372. remove_unreachable_eh_regions_worker (&cfun->eh->region_tree, r_reachable);
  1373. }
  1374. /* Invokes CALLBACK for every exception handler landing pad label.
  1375. Only used by reload hackery; should not be used by new code. */
  1376. void
  1377. for_each_eh_label (void (*callback) (rtx))
  1378. {
  1379. eh_landing_pad lp;
  1380. int i;
  1381. for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
  1382. {
  1383. if (lp)
  1384. {
  1385. rtx lab = lp->landing_pad;
  1386. if (lab && LABEL_P (lab))
  1387. (*callback) (lab);
  1388. }
  1389. }
  1390. }
  1391. /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
  1392. call insn.
  1393. At the gimple level, we use LP_NR
  1394. > 0 : The statement transfers to landing pad LP_NR
  1395. = 0 : The statement is outside any EH region
  1396. < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
  1397. At the rtl level, we use LP_NR
  1398. > 0 : The insn transfers to landing pad LP_NR
  1399. = 0 : The insn cannot throw
  1400. < 0 : The insn is within MUST_NOT_THROW region -LP_NR
  1401. = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
  1402. missing note: The insn is outside any EH region.
  1403. ??? This difference probably ought to be avoided. We could stand
  1404. to record nothrow for arbitrary gimple statements, and so avoid
  1405. some moderately complex lookups in stmt_could_throw_p. Perhaps
  1406. NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
  1407. no-nonlocal-goto property should be recorded elsewhere as a bit
  1408. on the call_insn directly. Perhaps we should make more use of
  1409. attaching the trees to call_insns (reachable via symbol_ref in
  1410. direct call cases) and just pull the data out of the trees. */
  1411. void
  1412. make_reg_eh_region_note (rtx insn, int ecf_flags, int lp_nr)
  1413. {
  1414. rtx value;
  1415. if (ecf_flags & ECF_NOTHROW)
  1416. value = const0_rtx;
  1417. else if (lp_nr != 0)
  1418. value = GEN_INT (lp_nr);
  1419. else
  1420. return;
  1421. add_reg_note (insn, REG_EH_REGION, value);
  1422. }
  1423. /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
  1424. nor perform a non-local goto. Replace the region note if it
  1425. already exists. */
  1426. void
  1427. make_reg_eh_region_note_nothrow_nononlocal (rtx insn)
  1428. {
  1429. rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
  1430. rtx intmin = GEN_INT (INT_MIN);
  1431. if (note != 0)
  1432. XEXP (note, 0) = intmin;
  1433. else
  1434. add_reg_note (insn, REG_EH_REGION, intmin);
  1435. }
  1436. /* Return true if INSN could throw, assuming no REG_EH_REGION note
  1437. to the contrary. */
  1438. bool
  1439. insn_could_throw_p (const_rtx insn)
  1440. {
  1441. if (!flag_exceptions)
  1442. return false;
  1443. if (CALL_P (insn))
  1444. return true;
  1445. if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
  1446. return may_trap_p (PATTERN (insn));
  1447. return false;
  1448. }
  1449. /* Copy an REG_EH_REGION note to each insn that might throw beginning
  1450. at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
  1451. to look for a note, or the note itself. */
  1452. void
  1453. copy_reg_eh_region_note_forward (rtx note_or_insn, rtx_insn *first, rtx last)
  1454. {
  1455. rtx_insn *insn;
  1456. rtx note = note_or_insn;
  1457. if (INSN_P (note_or_insn))
  1458. {
  1459. note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
  1460. if (note == NULL)
  1461. return;
  1462. }
  1463. note = XEXP (note, 0);
  1464. for (insn = first; insn != last ; insn = NEXT_INSN (insn))
  1465. if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
  1466. && insn_could_throw_p (insn))
  1467. add_reg_note (insn, REG_EH_REGION, note);
  1468. }
  1469. /* Likewise, but iterate backward. */
  1470. void
  1471. copy_reg_eh_region_note_backward (rtx note_or_insn, rtx_insn *last, rtx first)
  1472. {
  1473. rtx_insn *insn;
  1474. rtx note = note_or_insn;
  1475. if (INSN_P (note_or_insn))
  1476. {
  1477. note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
  1478. if (note == NULL)
  1479. return;
  1480. }
  1481. note = XEXP (note, 0);
  1482. for (insn = last; insn != first; insn = PREV_INSN (insn))
  1483. if (insn_could_throw_p (insn))
  1484. add_reg_note (insn, REG_EH_REGION, note);
  1485. }
  1486. /* Extract all EH information from INSN. Return true if the insn
  1487. was marked NOTHROW. */
  1488. static bool
  1489. get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
  1490. eh_landing_pad *plp)
  1491. {
  1492. eh_landing_pad lp = NULL;
  1493. eh_region r = NULL;
  1494. bool ret = false;
  1495. rtx note;
  1496. int lp_nr;
  1497. if (! INSN_P (insn))
  1498. goto egress;
  1499. if (NONJUMP_INSN_P (insn)
  1500. && GET_CODE (PATTERN (insn)) == SEQUENCE)
  1501. insn = XVECEXP (PATTERN (insn), 0, 0);
  1502. note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
  1503. if (!note)
  1504. {
  1505. ret = !insn_could_throw_p (insn);
  1506. goto egress;
  1507. }
  1508. lp_nr = INTVAL (XEXP (note, 0));
  1509. if (lp_nr == 0 || lp_nr == INT_MIN)
  1510. {
  1511. ret = true;
  1512. goto egress;
  1513. }
  1514. if (lp_nr < 0)
  1515. r = (*cfun->eh->region_array)[-lp_nr];
  1516. else
  1517. {
  1518. lp = (*cfun->eh->lp_array)[lp_nr];
  1519. r = lp->region;
  1520. }
  1521. egress:
  1522. *plp = lp;
  1523. *pr = r;
  1524. return ret;
  1525. }
  1526. /* Return the landing pad to which INSN may go, or NULL if it does not
  1527. have a reachable landing pad within this function. */
  1528. eh_landing_pad
  1529. get_eh_landing_pad_from_rtx (const_rtx insn)
  1530. {
  1531. eh_landing_pad lp;
  1532. eh_region r;
  1533. get_eh_region_and_lp_from_rtx (insn, &r, &lp);
  1534. return lp;
  1535. }
  1536. /* Return the region to which INSN may go, or NULL if it does not
  1537. have a reachable region within this function. */
  1538. eh_region
  1539. get_eh_region_from_rtx (const_rtx insn)
  1540. {
  1541. eh_landing_pad lp;
  1542. eh_region r;
  1543. get_eh_region_and_lp_from_rtx (insn, &r, &lp);
  1544. return r;
  1545. }
  1546. /* Return true if INSN throws and is caught by something in this function. */
  1547. bool
  1548. can_throw_internal (const_rtx insn)
  1549. {
  1550. return get_eh_landing_pad_from_rtx (insn) != NULL;
  1551. }
  1552. /* Return true if INSN throws and escapes from the current function. */
  1553. bool
  1554. can_throw_external (const_rtx insn)
  1555. {
  1556. eh_landing_pad lp;
  1557. eh_region r;
  1558. bool nothrow;
  1559. if (! INSN_P (insn))
  1560. return false;
  1561. if (NONJUMP_INSN_P (insn)
  1562. && GET_CODE (PATTERN (insn)) == SEQUENCE)
  1563. {
  1564. rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
  1565. int i, n = seq->len ();
  1566. for (i = 0; i < n; i++)
  1567. if (can_throw_external (seq->element (i)))
  1568. return true;
  1569. return false;
  1570. }
  1571. nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
  1572. /* If we can't throw, we obviously can't throw external. */
  1573. if (nothrow)
  1574. return false;
  1575. /* If we have an internal landing pad, then we're not external. */
  1576. if (lp != NULL)
  1577. return false;
  1578. /* If we're not within an EH region, then we are external. */
  1579. if (r == NULL)
  1580. return true;
  1581. /* The only thing that ought to be left is MUST_NOT_THROW regions,
  1582. which don't always have landing pads. */
  1583. gcc_assert (r->type == ERT_MUST_NOT_THROW);
  1584. return false;
  1585. }
  1586. /* Return true if INSN cannot throw at all. */
  1587. bool
  1588. insn_nothrow_p (const_rtx insn)
  1589. {
  1590. eh_landing_pad lp;
  1591. eh_region r;
  1592. if (! INSN_P (insn))
  1593. return true;
  1594. if (NONJUMP_INSN_P (insn)
  1595. && GET_CODE (PATTERN (insn)) == SEQUENCE)
  1596. {
  1597. rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
  1598. int i, n = seq->len ();
  1599. for (i = 0; i < n; i++)
  1600. if (!insn_nothrow_p (seq->element (i)))
  1601. return false;
  1602. return true;
  1603. }
  1604. return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
  1605. }
  1606. /* Return true if INSN can perform a non-local goto. */
  1607. /* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
  1608. bool
  1609. can_nonlocal_goto (const_rtx insn)
  1610. {
  1611. if (nonlocal_goto_handler_labels && CALL_P (insn))
  1612. {
  1613. rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
  1614. if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
  1615. return true;
  1616. }
  1617. return false;
  1618. }
  1619. /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
  1620. static unsigned int
  1621. set_nothrow_function_flags (void)
  1622. {
  1623. rtx_insn *insn;
  1624. crtl->nothrow = 1;
  1625. /* Assume crtl->all_throwers_are_sibcalls until we encounter
  1626. something that can throw an exception. We specifically exempt
  1627. CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
  1628. and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
  1629. is optimistic. */
  1630. crtl->all_throwers_are_sibcalls = 1;
  1631. /* If we don't know that this implementation of the function will
  1632. actually be used, then we must not set TREE_NOTHROW, since
  1633. callers must not assume that this function does not throw. */
  1634. if (TREE_NOTHROW (current_function_decl))
  1635. return 0;
  1636. if (! flag_exceptions)
  1637. return 0;
  1638. for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  1639. if (can_throw_external (insn))
  1640. {
  1641. crtl->nothrow = 0;
  1642. if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
  1643. {
  1644. crtl->all_throwers_are_sibcalls = 0;
  1645. return 0;
  1646. }
  1647. }
  1648. if (crtl->nothrow
  1649. && (cgraph_node::get (current_function_decl)->get_availability ()
  1650. >= AVAIL_AVAILABLE))
  1651. {
  1652. struct cgraph_node *node = cgraph_node::get (current_function_decl);
  1653. struct cgraph_edge *e;
  1654. for (e = node->callers; e; e = e->next_caller)
  1655. e->can_throw_external = false;
  1656. node->set_nothrow_flag (true);
  1657. if (dump_file)
  1658. fprintf (dump_file, "Marking function nothrow: %s\n\n",
  1659. current_function_name ());
  1660. }
  1661. return 0;
  1662. }
  1663. namespace {
  1664. const pass_data pass_data_set_nothrow_function_flags =
  1665. {
  1666. RTL_PASS, /* type */
  1667. "nothrow", /* name */
  1668. OPTGROUP_NONE, /* optinfo_flags */
  1669. TV_NONE, /* tv_id */
  1670. 0, /* properties_required */
  1671. 0, /* properties_provided */
  1672. 0, /* properties_destroyed */
  1673. 0, /* todo_flags_start */
  1674. 0, /* todo_flags_finish */
  1675. };
  1676. class pass_set_nothrow_function_flags : public rtl_opt_pass
  1677. {
  1678. public:
  1679. pass_set_nothrow_function_flags (gcc::context *ctxt)
  1680. : rtl_opt_pass (pass_data_set_nothrow_function_flags, ctxt)
  1681. {}
  1682. /* opt_pass methods: */
  1683. virtual unsigned int execute (function *)
  1684. {
  1685. return set_nothrow_function_flags ();
  1686. }
  1687. }; // class pass_set_nothrow_function_flags
  1688. } // anon namespace
  1689. rtl_opt_pass *
  1690. make_pass_set_nothrow_function_flags (gcc::context *ctxt)
  1691. {
  1692. return new pass_set_nothrow_function_flags (ctxt);
  1693. }
  1694. /* Various hooks for unwind library. */
  1695. /* Expand the EH support builtin functions:
  1696. __builtin_eh_pointer and __builtin_eh_filter. */
  1697. static eh_region
  1698. expand_builtin_eh_common (tree region_nr_t)
  1699. {
  1700. HOST_WIDE_INT region_nr;
  1701. eh_region region;
  1702. gcc_assert (tree_fits_shwi_p (region_nr_t));
  1703. region_nr = tree_to_shwi (region_nr_t);
  1704. region = (*cfun->eh->region_array)[region_nr];
  1705. /* ??? We shouldn't have been able to delete a eh region without
  1706. deleting all the code that depended on it. */
  1707. gcc_assert (region != NULL);
  1708. return region;
  1709. }
  1710. /* Expand to the exc_ptr value from the given eh region. */
  1711. rtx
  1712. expand_builtin_eh_pointer (tree exp)
  1713. {
  1714. eh_region region
  1715. = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
  1716. if (region->exc_ptr_reg == NULL)
  1717. region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
  1718. return region->exc_ptr_reg;
  1719. }
  1720. /* Expand to the filter value from the given eh region. */
  1721. rtx
  1722. expand_builtin_eh_filter (tree exp)
  1723. {
  1724. eh_region region
  1725. = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
  1726. if (region->filter_reg == NULL)
  1727. region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
  1728. return region->filter_reg;
  1729. }
  1730. /* Copy the exc_ptr and filter values from one landing pad's registers
  1731. to another. This is used to inline the resx statement. */
  1732. rtx
  1733. expand_builtin_eh_copy_values (tree exp)
  1734. {
  1735. eh_region dst
  1736. = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
  1737. eh_region src
  1738. = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
  1739. machine_mode fmode = targetm.eh_return_filter_mode ();
  1740. if (dst->exc_ptr_reg == NULL)
  1741. dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
  1742. if (src->exc_ptr_reg == NULL)
  1743. src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
  1744. if (dst->filter_reg == NULL)
  1745. dst->filter_reg = gen_reg_rtx (fmode);
  1746. if (src->filter_reg == NULL)
  1747. src->filter_reg = gen_reg_rtx (fmode);
  1748. emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
  1749. emit_move_insn (dst->filter_reg, src->filter_reg);
  1750. return const0_rtx;
  1751. }
  1752. /* Do any necessary initialization to access arbitrary stack frames.
  1753. On the SPARC, this means flushing the register windows. */
  1754. void
  1755. expand_builtin_unwind_init (void)
  1756. {
  1757. /* Set this so all the registers get saved in our frame; we need to be
  1758. able to copy the saved values for any registers from frames we unwind. */
  1759. crtl->saves_all_registers = 1;
  1760. #ifdef SETUP_FRAME_ADDRESSES
  1761. SETUP_FRAME_ADDRESSES ();
  1762. #endif
  1763. }
  1764. /* Map a non-negative number to an eh return data register number; expands
  1765. to -1 if no return data register is associated with the input number.
  1766. At least the inputs 0 and 1 must be mapped; the target may provide more. */
  1767. rtx
  1768. expand_builtin_eh_return_data_regno (tree exp)
  1769. {
  1770. tree which = CALL_EXPR_ARG (exp, 0);
  1771. unsigned HOST_WIDE_INT iwhich;
  1772. if (TREE_CODE (which) != INTEGER_CST)
  1773. {
  1774. error ("argument of %<__builtin_eh_return_regno%> must be constant");
  1775. return constm1_rtx;
  1776. }
  1777. iwhich = tree_to_uhwi (which);
  1778. iwhich = EH_RETURN_DATA_REGNO (iwhich);
  1779. if (iwhich == INVALID_REGNUM)
  1780. return constm1_rtx;
  1781. #ifdef DWARF_FRAME_REGNUM
  1782. iwhich = DWARF_FRAME_REGNUM (iwhich);
  1783. #else
  1784. iwhich = DBX_REGISTER_NUMBER (iwhich);
  1785. #endif
  1786. return GEN_INT (iwhich);
  1787. }
  1788. /* Given a value extracted from the return address register or stack slot,
  1789. return the actual address encoded in that value. */
  1790. rtx
  1791. expand_builtin_extract_return_addr (tree addr_tree)
  1792. {
  1793. rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
  1794. if (GET_MODE (addr) != Pmode
  1795. && GET_MODE (addr) != VOIDmode)
  1796. {
  1797. #ifdef POINTERS_EXTEND_UNSIGNED
  1798. addr = convert_memory_address (Pmode, addr);
  1799. #else
  1800. addr = convert_to_mode (Pmode, addr, 0);
  1801. #endif
  1802. }
  1803. /* First mask out any unwanted bits. */
  1804. #ifdef MASK_RETURN_ADDR
  1805. expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
  1806. #endif
  1807. /* Then adjust to find the real return address. */
  1808. #if defined (RETURN_ADDR_OFFSET)
  1809. addr = plus_constant (Pmode, addr, RETURN_ADDR_OFFSET);
  1810. #endif
  1811. return addr;
  1812. }
  1813. /* Given an actual address in addr_tree, do any necessary encoding
  1814. and return the value to be stored in the return address register or
  1815. stack slot so the epilogue will return to that address. */
  1816. rtx
  1817. expand_builtin_frob_return_addr (tree addr_tree)
  1818. {
  1819. rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
  1820. addr = convert_memory_address (Pmode, addr);
  1821. #ifdef RETURN_ADDR_OFFSET
  1822. addr = force_reg (Pmode, addr);
  1823. addr = plus_constant (Pmode, addr, -RETURN_ADDR_OFFSET);
  1824. #endif
  1825. return addr;
  1826. }
  1827. /* Set up the epilogue with the magic bits we'll need to return to the
  1828. exception handler. */
  1829. void
  1830. expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
  1831. tree handler_tree)
  1832. {
  1833. rtx tmp;
  1834. #ifdef EH_RETURN_STACKADJ_RTX
  1835. tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
  1836. VOIDmode, EXPAND_NORMAL);
  1837. tmp = convert_memory_address (Pmode, tmp);
  1838. if (!crtl->eh.ehr_stackadj)
  1839. crtl->eh.ehr_stackadj = copy_to_reg (tmp);
  1840. else if (tmp != crtl->eh.ehr_stackadj)
  1841. emit_move_insn (crtl->eh.ehr_stackadj, tmp);
  1842. #endif
  1843. tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
  1844. VOIDmode, EXPAND_NORMAL);
  1845. tmp = convert_memory_address (Pmode, tmp);
  1846. if (!crtl->eh.ehr_handler)
  1847. crtl->eh.ehr_handler = copy_to_reg (tmp);
  1848. else if (tmp != crtl->eh.ehr_handler)
  1849. emit_move_insn (crtl->eh.ehr_handler, tmp);
  1850. if (!crtl->eh.ehr_label)
  1851. crtl->eh.ehr_label = gen_label_rtx ();
  1852. emit_jump (crtl->eh.ehr_label);
  1853. }
  1854. /* Expand __builtin_eh_return. This exit path from the function loads up
  1855. the eh return data registers, adjusts the stack, and branches to a
  1856. given PC other than the normal return address. */
  1857. void
  1858. expand_eh_return (void)
  1859. {
  1860. rtx_code_label *around_label;
  1861. if (! crtl->eh.ehr_label)
  1862. return;
  1863. crtl->calls_eh_return = 1;
  1864. #ifdef EH_RETURN_STACKADJ_RTX
  1865. emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
  1866. #endif
  1867. around_label = gen_label_rtx ();
  1868. emit_jump (around_label);
  1869. emit_label (crtl->eh.ehr_label);
  1870. clobber_return_register ();
  1871. #ifdef EH_RETURN_STACKADJ_RTX
  1872. emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
  1873. #endif
  1874. #ifdef HAVE_eh_return
  1875. if (HAVE_eh_return)
  1876. emit_insn (gen_eh_return (crtl->eh.ehr_handler));
  1877. else
  1878. #endif
  1879. {
  1880. #ifdef EH_RETURN_HANDLER_RTX
  1881. emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
  1882. #else
  1883. error ("__builtin_eh_return not supported on this target");
  1884. #endif
  1885. }
  1886. emit_label (around_label);
  1887. }
  1888. /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
  1889. POINTERS_EXTEND_UNSIGNED and return it. */
  1890. rtx
  1891. expand_builtin_extend_pointer (tree addr_tree)
  1892. {
  1893. rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
  1894. int extend;
  1895. #ifdef POINTERS_EXTEND_UNSIGNED
  1896. extend = POINTERS_EXTEND_UNSIGNED;
  1897. #else
  1898. /* The previous EH code did an unsigned extend by default, so we do this also
  1899. for consistency. */
  1900. extend = 1;
  1901. #endif
  1902. return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
  1903. }
  1904. static int
  1905. add_action_record (action_hash_type *ar_hash, int filter, int next)
  1906. {
  1907. struct action_record **slot, *new_ar, tmp;
  1908. tmp.filter = filter;
  1909. tmp.next = next;
  1910. slot = ar_hash->find_slot (&tmp, INSERT);
  1911. if ((new_ar = *slot) == NULL)
  1912. {
  1913. new_ar = XNEW (struct action_record);
  1914. new_ar->offset = crtl->eh.action_record_data->length () + 1;
  1915. new_ar->filter = filter;
  1916. new_ar->next = next;
  1917. *slot = new_ar;
  1918. /* The filter value goes in untouched. The link to the next
  1919. record is a "self-relative" byte offset, or zero to indicate
  1920. that there is no next record. So convert the absolute 1 based
  1921. indices we've been carrying around into a displacement. */
  1922. push_sleb128 (&crtl->eh.action_record_data, filter);
  1923. if (next)
  1924. next -= crtl->eh.action_record_data->length () + 1;
  1925. push_sleb128 (&crtl->eh.action_record_data, next);
  1926. }
  1927. return new_ar->offset;
  1928. }
  1929. static int
  1930. collect_one_action_chain (action_hash_type *ar_hash, eh_region region)
  1931. {
  1932. int next;
  1933. /* If we've reached the top of the region chain, then we have
  1934. no actions, and require no landing pad. */
  1935. if (region == NULL)
  1936. return -1;
  1937. switch (region->type)
  1938. {
  1939. case ERT_CLEANUP:
  1940. {
  1941. eh_region r;
  1942. /* A cleanup adds a zero filter to the beginning of the chain, but
  1943. there are special cases to look out for. If there are *only*
  1944. cleanups along a path, then it compresses to a zero action.
  1945. Further, if there are multiple cleanups along a path, we only
  1946. need to represent one of them, as that is enough to trigger
  1947. entry to the landing pad at runtime. */
  1948. next = collect_one_action_chain (ar_hash, region->outer);
  1949. if (next <= 0)
  1950. return 0;
  1951. for (r = region->outer; r ; r = r->outer)
  1952. if (r->type == ERT_CLEANUP)
  1953. return next;
  1954. return add_action_record (ar_hash, 0, next);
  1955. }
  1956. case ERT_TRY:
  1957. {
  1958. eh_catch c;
  1959. /* Process the associated catch regions in reverse order.
  1960. If there's a catch-all handler, then we don't need to
  1961. search outer regions. Use a magic -3 value to record
  1962. that we haven't done the outer search. */
  1963. next = -3;
  1964. for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
  1965. {
  1966. if (c->type_list == NULL)
  1967. {
  1968. /* Retrieve the filter from the head of the filter list
  1969. where we have stored it (see assign_filter_values). */
  1970. int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
  1971. next = add_action_record (ar_hash, filter, 0);
  1972. }
  1973. else
  1974. {
  1975. /* Once the outer search is done, trigger an action record for
  1976. each filter we have. */
  1977. tree flt_node;
  1978. if (next == -3)
  1979. {
  1980. next = collect_one_action_chain (ar_hash, region->outer);
  1981. /* If there is no next action, terminate the chain. */
  1982. if (next == -1)
  1983. next = 0;
  1984. /* If all outer actions are cleanups or must_not_throw,
  1985. we'll have no action record for it, since we had wanted
  1986. to encode these states in the call-site record directly.
  1987. Add a cleanup action to the chain to catch these. */
  1988. else if (next <= 0)
  1989. next = add_action_record (ar_hash, 0, 0);
  1990. }
  1991. flt_node = c->filter_list;
  1992. for (; flt_node; flt_node = TREE_CHAIN (flt_node))
  1993. {
  1994. int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
  1995. next = add_action_record (ar_hash, filter, next);
  1996. }
  1997. }
  1998. }
  1999. return next;
  2000. }
  2001. case ERT_ALLOWED_EXCEPTIONS:
  2002. /* An exception specification adds its filter to the
  2003. beginning of the chain. */
  2004. next = collect_one_action_chain (ar_hash, region->outer);
  2005. /* If there is no next action, terminate the chain. */
  2006. if (next == -1)
  2007. next = 0;
  2008. /* If all outer actions are cleanups or must_not_throw,
  2009. we'll have no action record for it, since we had wanted
  2010. to encode these states in the call-site record directly.
  2011. Add a cleanup action to the chain to catch these. */
  2012. else if (next <= 0)
  2013. next = add_action_record (ar_hash, 0, 0);
  2014. return add_action_record (ar_hash, region->u.allowed.filter, next);
  2015. case ERT_MUST_NOT_THROW:
  2016. /* A must-not-throw region with no inner handlers or cleanups
  2017. requires no call-site entry. Note that this differs from
  2018. the no handler or cleanup case in that we do require an lsda
  2019. to be generated. Return a magic -2 value to record this. */
  2020. return -2;
  2021. }
  2022. gcc_unreachable ();
  2023. }
  2024. static int
  2025. add_call_site (rtx landing_pad, int action, int section)
  2026. {
  2027. call_site_record record;
  2028. record = ggc_alloc<call_site_record_d> ();
  2029. record->landing_pad = landing_pad;
  2030. record->action = action;
  2031. vec_safe_push (crtl->eh.call_site_record_v[section], record);
  2032. return call_site_base + crtl->eh.call_site_record_v[section]->length () - 1;
  2033. }
  2034. static rtx_note *
  2035. emit_note_eh_region_end (rtx_insn *insn)
  2036. {
  2037. rtx_insn *next = NEXT_INSN (insn);
  2038. /* Make sure we do not split a call and its corresponding
  2039. CALL_ARG_LOCATION note. */
  2040. if (next && NOTE_P (next)
  2041. && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
  2042. insn = next;
  2043. return emit_note_after (NOTE_INSN_EH_REGION_END, insn);
  2044. }
  2045. /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
  2046. The new note numbers will not refer to region numbers, but
  2047. instead to call site entries. */
  2048. static unsigned int
  2049. convert_to_eh_region_ranges (void)
  2050. {
  2051. rtx insn;
  2052. rtx_insn *iter;
  2053. rtx_note *note;
  2054. action_hash_type ar_hash (31);
  2055. int last_action = -3;
  2056. rtx_insn *last_action_insn = NULL;
  2057. rtx last_landing_pad = NULL_RTX;
  2058. rtx_insn *first_no_action_insn = NULL;
  2059. int call_site = 0;
  2060. int cur_sec = 0;
  2061. rtx section_switch_note = NULL_RTX;
  2062. rtx_insn *first_no_action_insn_before_switch = NULL;
  2063. rtx_insn *last_no_action_insn_before_switch = NULL;
  2064. int saved_call_site_base = call_site_base;
  2065. vec_alloc (crtl->eh.action_record_data, 64);
  2066. for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
  2067. if (INSN_P (iter))
  2068. {
  2069. eh_landing_pad lp;
  2070. eh_region region;
  2071. bool nothrow;
  2072. int this_action;
  2073. rtx this_landing_pad;
  2074. insn = iter;
  2075. if (NONJUMP_INSN_P (insn)
  2076. && GET_CODE (PATTERN (insn)) == SEQUENCE)
  2077. insn = XVECEXP (PATTERN (insn), 0, 0);
  2078. nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
  2079. if (nothrow)
  2080. continue;
  2081. if (region)
  2082. this_action = collect_one_action_chain (&ar_hash, region);
  2083. else
  2084. this_action = -1;
  2085. /* Existence of catch handlers, or must-not-throw regions
  2086. implies that an lsda is needed (even if empty). */
  2087. if (this_action != -1)
  2088. crtl->uses_eh_lsda = 1;
  2089. /* Delay creation of region notes for no-action regions
  2090. until we're sure that an lsda will be required. */
  2091. else if (last_action == -3)
  2092. {
  2093. first_no_action_insn = iter;
  2094. last_action = -1;
  2095. }
  2096. if (this_action >= 0)
  2097. this_landing_pad = lp->landing_pad;
  2098. else
  2099. this_landing_pad = NULL_RTX;
  2100. /* Differing actions or landing pads implies a change in call-site
  2101. info, which implies some EH_REGION note should be emitted. */
  2102. if (last_action != this_action
  2103. || last_landing_pad != this_landing_pad)
  2104. {
  2105. /* If there is a queued no-action region in the other section
  2106. with hot/cold partitioning, emit it now. */
  2107. if (first_no_action_insn_before_switch)
  2108. {
  2109. gcc_assert (this_action != -1
  2110. && last_action == (first_no_action_insn
  2111. ? -1 : -3));
  2112. call_site = add_call_site (NULL_RTX, 0, 0);
  2113. note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
  2114. first_no_action_insn_before_switch);
  2115. NOTE_EH_HANDLER (note) = call_site;
  2116. note
  2117. = emit_note_eh_region_end (last_no_action_insn_before_switch);
  2118. NOTE_EH_HANDLER (note) = call_site;
  2119. gcc_assert (last_action != -3
  2120. || (last_action_insn
  2121. == last_no_action_insn_before_switch));
  2122. first_no_action_insn_before_switch = NULL;
  2123. last_no_action_insn_before_switch = NULL;
  2124. call_site_base++;
  2125. }
  2126. /* If we'd not seen a previous action (-3) or the previous
  2127. action was must-not-throw (-2), then we do not need an
  2128. end note. */
  2129. if (last_action >= -1)
  2130. {
  2131. /* If we delayed the creation of the begin, do it now. */
  2132. if (first_no_action_insn)
  2133. {
  2134. call_site = add_call_site (NULL_RTX, 0, cur_sec);
  2135. note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
  2136. first_no_action_insn);
  2137. NOTE_EH_HANDLER (note) = call_site;
  2138. first_no_action_insn = NULL;
  2139. }
  2140. note = emit_note_eh_region_end (last_action_insn);
  2141. NOTE_EH_HANDLER (note) = call_site;
  2142. }
  2143. /* If the new action is must-not-throw, then no region notes
  2144. are created. */
  2145. if (this_action >= -1)
  2146. {
  2147. call_site = add_call_site (this_landing_pad,
  2148. this_action < 0 ? 0 : this_action,
  2149. cur_sec);
  2150. note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
  2151. NOTE_EH_HANDLER (note) = call_site;
  2152. }
  2153. last_action = this_action;
  2154. last_landing_pad = this_landing_pad;
  2155. }
  2156. last_action_insn = iter;
  2157. }
  2158. else if (NOTE_P (iter)
  2159. && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
  2160. {
  2161. gcc_assert (section_switch_note == NULL_RTX);
  2162. gcc_assert (flag_reorder_blocks_and_partition);
  2163. section_switch_note = iter;
  2164. if (first_no_action_insn)
  2165. {
  2166. first_no_action_insn_before_switch = first_no_action_insn;
  2167. last_no_action_insn_before_switch = last_action_insn;
  2168. first_no_action_insn = NULL;
  2169. gcc_assert (last_action == -1);
  2170. last_action = -3;
  2171. }
  2172. /* Force closing of current EH region before section switch and
  2173. opening a new one afterwards. */
  2174. else if (last_action != -3)
  2175. last_landing_pad = pc_rtx;
  2176. if (crtl->eh.call_site_record_v[cur_sec])
  2177. call_site_base += crtl->eh.call_site_record_v[cur_sec]->length ();
  2178. cur_sec++;
  2179. gcc_assert (crtl->eh.call_site_record_v[cur_sec] == NULL);
  2180. vec_alloc (crtl->eh.call_site_record_v[cur_sec], 10);
  2181. }
  2182. if (last_action >= -1 && ! first_no_action_insn)
  2183. {
  2184. note = emit_note_eh_region_end (last_action_insn);
  2185. NOTE_EH_HANDLER (note) = call_site;
  2186. }
  2187. call_site_base = saved_call_site_base;
  2188. return 0;
  2189. }
  2190. namespace {
  2191. const pass_data pass_data_convert_to_eh_region_ranges =
  2192. {
  2193. RTL_PASS, /* type */
  2194. "eh_ranges", /* name */
  2195. OPTGROUP_NONE, /* optinfo_flags */
  2196. TV_NONE, /* tv_id */
  2197. 0, /* properties_required */
  2198. 0, /* properties_provided */
  2199. 0, /* properties_destroyed */
  2200. 0, /* todo_flags_start */
  2201. 0, /* todo_flags_finish */
  2202. };
  2203. class pass_convert_to_eh_region_ranges : public rtl_opt_pass
  2204. {
  2205. public:
  2206. pass_convert_to_eh_region_ranges (gcc::context *ctxt)
  2207. : rtl_opt_pass (pass_data_convert_to_eh_region_ranges, ctxt)
  2208. {}
  2209. /* opt_pass methods: */
  2210. virtual bool gate (function *);
  2211. virtual unsigned int execute (function *)
  2212. {
  2213. return convert_to_eh_region_ranges ();
  2214. }
  2215. }; // class pass_convert_to_eh_region_ranges
  2216. bool
  2217. pass_convert_to_eh_region_ranges::gate (function *)
  2218. {
  2219. /* Nothing to do for SJLJ exceptions or if no regions created. */
  2220. if (cfun->eh->region_tree == NULL)
  2221. return false;
  2222. if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
  2223. return false;
  2224. return true;
  2225. }
  2226. } // anon namespace
  2227. rtl_opt_pass *
  2228. make_pass_convert_to_eh_region_ranges (gcc::context *ctxt)
  2229. {
  2230. return new pass_convert_to_eh_region_ranges (ctxt);
  2231. }
  2232. static void
  2233. push_uleb128 (vec<uchar, va_gc> **data_area, unsigned int value)
  2234. {
  2235. do
  2236. {
  2237. unsigned char byte = value & 0x7f;
  2238. value >>= 7;
  2239. if (value)
  2240. byte |= 0x80;
  2241. vec_safe_push (*data_area, byte);
  2242. }
  2243. while (value);
  2244. }
  2245. static void
  2246. push_sleb128 (vec<uchar, va_gc> **data_area, int value)
  2247. {
  2248. unsigned char byte;
  2249. int more;
  2250. do
  2251. {
  2252. byte = value & 0x7f;
  2253. value >>= 7;
  2254. more = ! ((value == 0 && (byte & 0x40) == 0)
  2255. || (value == -1 && (byte & 0x40) != 0));
  2256. if (more)
  2257. byte |= 0x80;
  2258. vec_safe_push (*data_area, byte);
  2259. }
  2260. while (more);
  2261. }
  2262. #ifndef HAVE_AS_LEB128
  2263. static int
  2264. dw2_size_of_call_site_table (int section)
  2265. {
  2266. int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
  2267. int size = n * (4 + 4 + 4);
  2268. int i;
  2269. for (i = 0; i < n; ++i)
  2270. {
  2271. struct call_site_record_d *cs =
  2272. (*crtl->eh.call_site_record_v[section])[i];
  2273. size += size_of_uleb128 (cs->action);
  2274. }
  2275. return size;
  2276. }
  2277. static int
  2278. sjlj_size_of_call_site_table (void)
  2279. {
  2280. int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
  2281. int size = 0;
  2282. int i;
  2283. for (i = 0; i < n; ++i)
  2284. {
  2285. struct call_site_record_d *cs =
  2286. (*crtl->eh.call_site_record_v[0])[i];
  2287. size += size_of_uleb128 (INTVAL (cs->landing_pad));
  2288. size += size_of_uleb128 (cs->action);
  2289. }
  2290. return size;
  2291. }
  2292. #endif
  2293. static void
  2294. dw2_output_call_site_table (int cs_format, int section)
  2295. {
  2296. int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
  2297. int i;
  2298. const char *begin;
  2299. if (section == 0)
  2300. begin = current_function_func_begin_label;
  2301. else if (first_function_block_is_cold)
  2302. begin = crtl->subsections.hot_section_label;
  2303. else
  2304. begin = crtl->subsections.cold_section_label;
  2305. for (i = 0; i < n; ++i)
  2306. {
  2307. struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[section])[i];
  2308. char reg_start_lab[32];
  2309. char reg_end_lab[32];
  2310. char landing_pad_lab[32];
  2311. ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
  2312. ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
  2313. if (cs->landing_pad)
  2314. ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
  2315. CODE_LABEL_NUMBER (cs->landing_pad));
  2316. /* ??? Perhaps use insn length scaling if the assembler supports
  2317. generic arithmetic. */
  2318. /* ??? Perhaps use attr_length to choose data1 or data2 instead of
  2319. data4 if the function is small enough. */
  2320. if (cs_format == DW_EH_PE_uleb128)
  2321. {
  2322. dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
  2323. "region %d start", i);
  2324. dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
  2325. "length");
  2326. if (cs->landing_pad)
  2327. dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
  2328. "landing pad");
  2329. else
  2330. dw2_asm_output_data_uleb128 (0, "landing pad");
  2331. }
  2332. else
  2333. {
  2334. dw2_asm_output_delta (4, reg_start_lab, begin,
  2335. "region %d start", i);
  2336. dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
  2337. if (cs->landing_pad)
  2338. dw2_asm_output_delta (4, landing_pad_lab, begin,
  2339. "landing pad");
  2340. else
  2341. dw2_asm_output_data (4, 0, "landing pad");
  2342. }
  2343. dw2_asm_output_data_uleb128 (cs->action, "action");
  2344. }
  2345. call_site_base += n;
  2346. }
  2347. static void
  2348. sjlj_output_call_site_table (void)
  2349. {
  2350. int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
  2351. int i;
  2352. for (i = 0; i < n; ++i)
  2353. {
  2354. struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[0])[i];
  2355. dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
  2356. "region %d landing pad", i);
  2357. dw2_asm_output_data_uleb128 (cs->action, "action");
  2358. }
  2359. call_site_base += n;
  2360. }
  2361. /* Switch to the section that should be used for exception tables. */
  2362. static void
  2363. switch_to_exception_section (const char * ARG_UNUSED (fnname))
  2364. {
  2365. section *s;
  2366. if (exception_section)
  2367. s = exception_section;
  2368. else
  2369. {
  2370. /* Compute the section and cache it into exception_section,
  2371. unless it depends on the function name. */
  2372. if (targetm_common.have_named_sections)
  2373. {
  2374. int flags;
  2375. if (EH_TABLES_CAN_BE_READ_ONLY)
  2376. {
  2377. int tt_format =
  2378. ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
  2379. flags = ((! flag_pic
  2380. || ((tt_format & 0x70) != DW_EH_PE_absptr
  2381. && (tt_format & 0x70) != DW_EH_PE_aligned))
  2382. ? 0 : SECTION_WRITE);
  2383. }
  2384. else
  2385. flags = SECTION_WRITE;
  2386. #ifdef HAVE_LD_EH_GC_SECTIONS
  2387. if (flag_function_sections
  2388. || (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP))
  2389. {
  2390. char *section_name = XNEWVEC (char, strlen (fnname) + 32);
  2391. /* The EH table must match the code section, so only mark
  2392. it linkonce if we have COMDAT groups to tie them together. */
  2393. if (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP)
  2394. flags |= SECTION_LINKONCE;
  2395. sprintf (section_name, ".gcc_except_table.%s", fnname);
  2396. s = get_section (section_name, flags, current_function_decl);
  2397. free (section_name);
  2398. }
  2399. else
  2400. #endif
  2401. exception_section
  2402. = s = get_section (".gcc_except_table", flags, NULL);
  2403. }
  2404. else
  2405. exception_section
  2406. = s = flag_pic ? data_section : readonly_data_section;
  2407. }
  2408. switch_to_section (s);
  2409. }
  2410. /* Output a reference from an exception table to the type_info object TYPE.
  2411. TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
  2412. the value. */
  2413. static void
  2414. output_ttype (tree type, int tt_format, int tt_format_size)
  2415. {
  2416. rtx value;
  2417. bool is_public = true;
  2418. if (type == NULL_TREE)
  2419. value = const0_rtx;
  2420. else
  2421. {
  2422. /* FIXME lto. pass_ipa_free_lang_data changes all types to
  2423. runtime types so TYPE should already be a runtime type
  2424. reference. When pass_ipa_free_lang data is made a default
  2425. pass, we can then remove the call to lookup_type_for_runtime
  2426. below. */
  2427. if (TYPE_P (type))
  2428. type = lookup_type_for_runtime (type);
  2429. value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
  2430. /* Let cgraph know that the rtti decl is used. Not all of the
  2431. paths below go through assemble_integer, which would take
  2432. care of this for us. */
  2433. STRIP_NOPS (type);
  2434. if (TREE_CODE (type) == ADDR_EXPR)
  2435. {
  2436. type = TREE_OPERAND (type, 0);
  2437. if (TREE_CODE (type) == VAR_DECL)
  2438. is_public = TREE_PUBLIC (type);
  2439. }
  2440. else
  2441. gcc_assert (TREE_CODE (type) == INTEGER_CST);
  2442. }
  2443. /* Allow the target to override the type table entry format. */
  2444. if (targetm.asm_out.ttype (value))
  2445. return;
  2446. if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
  2447. assemble_integer (value, tt_format_size,
  2448. tt_format_size * BITS_PER_UNIT, 1);
  2449. else
  2450. dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
  2451. }
  2452. static void
  2453. output_one_function_exception_table (int section)
  2454. {
  2455. int tt_format, cs_format, lp_format, i;
  2456. #ifdef HAVE_AS_LEB128
  2457. char ttype_label[32];
  2458. char cs_after_size_label[32];
  2459. char cs_end_label[32];
  2460. #else
  2461. int call_site_len;
  2462. #endif
  2463. int have_tt_data;
  2464. int tt_format_size = 0;
  2465. have_tt_data = (vec_safe_length (cfun->eh->ttype_data)
  2466. || (targetm.arm_eabi_unwinder
  2467. ? vec_safe_length (cfun->eh->ehspec_data.arm_eabi)
  2468. : vec_safe_length (cfun->eh->ehspec_data.other)));
  2469. /* Indicate the format of the @TType entries. */
  2470. if (! have_tt_data)
  2471. tt_format = DW_EH_PE_omit;
  2472. else
  2473. {
  2474. tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
  2475. #ifdef HAVE_AS_LEB128
  2476. ASM_GENERATE_INTERNAL_LABEL (ttype_label,
  2477. section ? "LLSDATTC" : "LLSDATT",
  2478. current_function_funcdef_no);
  2479. #endif
  2480. tt_format_size = size_of_encoded_value (tt_format);
  2481. assemble_align (tt_format_size * BITS_PER_UNIT);
  2482. }
  2483. targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
  2484. current_function_funcdef_no);
  2485. /* The LSDA header. */
  2486. /* Indicate the format of the landing pad start pointer. An omitted
  2487. field implies @LPStart == @Start. */
  2488. /* Currently we always put @LPStart == @Start. This field would
  2489. be most useful in moving the landing pads completely out of
  2490. line to another section, but it could also be used to minimize
  2491. the size of uleb128 landing pad offsets. */
  2492. lp_format = DW_EH_PE_omit;
  2493. dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
  2494. eh_data_format_name (lp_format));
  2495. /* @LPStart pointer would go here. */
  2496. dw2_asm_output_data (1, tt_format, "@TType format (%s)",
  2497. eh_data_format_name (tt_format));
  2498. #ifndef HAVE_AS_LEB128
  2499. if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
  2500. call_site_len = sjlj_size_of_call_site_table ();
  2501. else
  2502. call_site_len = dw2_size_of_call_site_table (section);
  2503. #endif
  2504. /* A pc-relative 4-byte displacement to the @TType data. */
  2505. if (have_tt_data)
  2506. {
  2507. #ifdef HAVE_AS_LEB128
  2508. char ttype_after_disp_label[32];
  2509. ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
  2510. section ? "LLSDATTDC" : "LLSDATTD",
  2511. current_function_funcdef_no);
  2512. dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
  2513. "@TType base offset");
  2514. ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
  2515. #else
  2516. /* Ug. Alignment queers things. */
  2517. unsigned int before_disp, after_disp, last_disp, disp;
  2518. before_disp = 1 + 1;
  2519. after_disp = (1 + size_of_uleb128 (call_site_len)
  2520. + call_site_len
  2521. + vec_safe_length (crtl->eh.action_record_data)
  2522. + (vec_safe_length (cfun->eh->ttype_data)
  2523. * tt_format_size));
  2524. disp = after_disp;
  2525. do
  2526. {
  2527. unsigned int disp_size, pad;
  2528. last_disp = disp;
  2529. disp_size = size_of_uleb128 (disp);
  2530. pad = before_disp + disp_size + after_disp;
  2531. if (pad % tt_format_size)
  2532. pad = tt_format_size - (pad % tt_format_size);
  2533. else
  2534. pad = 0;
  2535. disp = after_disp + pad;
  2536. }
  2537. while (disp != last_disp);
  2538. dw2_asm_output_data_uleb128 (disp, "@TType base offset");
  2539. #endif
  2540. }
  2541. /* Indicate the format of the call-site offsets. */
  2542. #ifdef HAVE_AS_LEB128
  2543. cs_format = DW_EH_PE_uleb128;
  2544. #else
  2545. cs_format = DW_EH_PE_udata4;
  2546. #endif
  2547. dw2_asm_output_data (1, cs_format, "call-site format (%s)",
  2548. eh_data_format_name (cs_format));
  2549. #ifdef HAVE_AS_LEB128
  2550. ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
  2551. section ? "LLSDACSBC" : "LLSDACSB",
  2552. current_function_funcdef_no);
  2553. ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
  2554. section ? "LLSDACSEC" : "LLSDACSE",
  2555. current_function_funcdef_no);
  2556. dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
  2557. "Call-site table length");
  2558. ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
  2559. if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
  2560. sjlj_output_call_site_table ();
  2561. else
  2562. dw2_output_call_site_table (cs_format, section);
  2563. ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
  2564. #else
  2565. dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
  2566. if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
  2567. sjlj_output_call_site_table ();
  2568. else
  2569. dw2_output_call_site_table (cs_format, section);
  2570. #endif
  2571. /* ??? Decode and interpret the data for flag_debug_asm. */
  2572. {
  2573. uchar uc;
  2574. FOR_EACH_VEC_ELT (*crtl->eh.action_record_data, i, uc)
  2575. dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
  2576. }
  2577. if (have_tt_data)
  2578. assemble_align (tt_format_size * BITS_PER_UNIT);
  2579. i = vec_safe_length (cfun->eh->ttype_data);
  2580. while (i-- > 0)
  2581. {
  2582. tree type = (*cfun->eh->ttype_data)[i];
  2583. output_ttype (type, tt_format, tt_format_size);
  2584. }
  2585. #ifdef HAVE_AS_LEB128
  2586. if (have_tt_data)
  2587. ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
  2588. #endif
  2589. /* ??? Decode and interpret the data for flag_debug_asm. */
  2590. if (targetm.arm_eabi_unwinder)
  2591. {
  2592. tree type;
  2593. for (i = 0;
  2594. vec_safe_iterate (cfun->eh->ehspec_data.arm_eabi, i, &type); ++i)
  2595. output_ttype (type, tt_format, tt_format_size);
  2596. }
  2597. else
  2598. {
  2599. uchar uc;
  2600. for (i = 0;
  2601. vec_safe_iterate (cfun->eh->ehspec_data.other, i, &uc); ++i)
  2602. dw2_asm_output_data (1, uc,
  2603. i ? NULL : "Exception specification table");
  2604. }
  2605. }
  2606. void
  2607. output_function_exception_table (const char *fnname)
  2608. {
  2609. rtx personality = get_personality_function (current_function_decl);
  2610. /* Not all functions need anything. */
  2611. if (! crtl->uses_eh_lsda)
  2612. return;
  2613. if (personality)
  2614. {
  2615. assemble_external_libcall (personality);
  2616. if (targetm.asm_out.emit_except_personality)
  2617. targetm.asm_out.emit_except_personality (personality);
  2618. }
  2619. switch_to_exception_section (fnname);
  2620. /* If the target wants a label to begin the table, emit it here. */
  2621. targetm.asm_out.emit_except_table_label (asm_out_file);
  2622. output_one_function_exception_table (0);
  2623. if (crtl->eh.call_site_record_v[1])
  2624. output_one_function_exception_table (1);
  2625. switch_to_section (current_function_section ());
  2626. }
  2627. void
  2628. set_eh_throw_stmt_table (function *fun, hash_map<gimple, int> *table)
  2629. {
  2630. fun->eh->throw_stmt_table = table;
  2631. }
  2632. hash_map<gimple, int> *
  2633. get_eh_throw_stmt_table (struct function *fun)
  2634. {
  2635. return fun->eh->throw_stmt_table;
  2636. }
  2637. /* Determine if the function needs an EH personality function. */
  2638. enum eh_personality_kind
  2639. function_needs_eh_personality (struct function *fn)
  2640. {
  2641. enum eh_personality_kind kind = eh_personality_none;
  2642. eh_region i;
  2643. FOR_ALL_EH_REGION_FN (i, fn)
  2644. {
  2645. switch (i->type)
  2646. {
  2647. case ERT_CLEANUP:
  2648. /* Can do with any personality including the generic C one. */
  2649. kind = eh_personality_any;
  2650. break;
  2651. case ERT_TRY:
  2652. case ERT_ALLOWED_EXCEPTIONS:
  2653. /* Always needs a EH personality function. The generic C
  2654. personality doesn't handle these even for empty type lists. */
  2655. return eh_personality_lang;
  2656. case ERT_MUST_NOT_THROW:
  2657. /* Always needs a EH personality function. The language may specify
  2658. what abort routine that must be used, e.g. std::terminate. */
  2659. return eh_personality_lang;
  2660. }
  2661. }
  2662. return kind;
  2663. }
  2664. /* Dump EH information to OUT. */
  2665. void
  2666. dump_eh_tree (FILE * out, struct function *fun)
  2667. {
  2668. eh_region i;
  2669. int depth = 0;
  2670. static const char *const type_name[] = {
  2671. "cleanup", "try", "allowed_exceptions", "must_not_throw"
  2672. };
  2673. i = fun->eh->region_tree;
  2674. if (!i)
  2675. return;
  2676. fprintf (out, "Eh tree:\n");
  2677. while (1)
  2678. {
  2679. fprintf (out, " %*s %i %s", depth * 2, "",
  2680. i->index, type_name[(int) i->type]);
  2681. if (i->landing_pads)
  2682. {
  2683. eh_landing_pad lp;
  2684. fprintf (out, " land:");
  2685. if (current_ir_type () == IR_GIMPLE)
  2686. {
  2687. for (lp = i->landing_pads; lp ; lp = lp->next_lp)
  2688. {
  2689. fprintf (out, "{%i,", lp->index);
  2690. print_generic_expr (out, lp->post_landing_pad, 0);
  2691. fputc ('}', out);
  2692. if (lp->next_lp)
  2693. fputc (',', out);
  2694. }
  2695. }
  2696. else
  2697. {
  2698. for (lp = i->landing_pads; lp ; lp = lp->next_lp)
  2699. {
  2700. fprintf (out, "{%i,", lp->index);
  2701. if (lp->landing_pad)
  2702. fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
  2703. NOTE_P (lp->landing_pad) ? "(del)" : "");
  2704. else
  2705. fprintf (out, "(nil),");
  2706. if (lp->post_landing_pad)
  2707. {
  2708. rtx lab = label_rtx (lp->post_landing_pad);
  2709. fprintf (out, "%i%s}", INSN_UID (lab),
  2710. NOTE_P (lab) ? "(del)" : "");
  2711. }
  2712. else
  2713. fprintf (out, "(nil)}");
  2714. if (lp->next_lp)
  2715. fputc (',', out);
  2716. }
  2717. }
  2718. }
  2719. switch (i->type)
  2720. {
  2721. case ERT_CLEANUP:
  2722. case ERT_MUST_NOT_THROW:
  2723. break;
  2724. case ERT_TRY:
  2725. {
  2726. eh_catch c;
  2727. fprintf (out, " catch:");
  2728. for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
  2729. {
  2730. fputc ('{', out);
  2731. if (c->label)
  2732. {
  2733. fprintf (out, "lab:");
  2734. print_generic_expr (out, c->label, 0);
  2735. fputc (';', out);
  2736. }
  2737. print_generic_expr (out, c->type_list, 0);
  2738. fputc ('}', out);
  2739. if (c->next_catch)
  2740. fputc (',', out);
  2741. }
  2742. }
  2743. break;
  2744. case ERT_ALLOWED_EXCEPTIONS:
  2745. fprintf (out, " filter :%i types:", i->u.allowed.filter);
  2746. print_generic_expr (out, i->u.allowed.type_list, 0);
  2747. break;
  2748. }
  2749. fputc ('\n', out);
  2750. /* If there are sub-regions, process them. */
  2751. if (i->inner)
  2752. i = i->inner, depth++;
  2753. /* If there are peers, process them. */
  2754. else if (i->next_peer)
  2755. i = i->next_peer;
  2756. /* Otherwise, step back up the tree to the next peer. */
  2757. else
  2758. {
  2759. do
  2760. {
  2761. i = i->outer;
  2762. depth--;
  2763. if (i == NULL)
  2764. return;
  2765. }
  2766. while (i->next_peer == NULL);
  2767. i = i->next_peer;
  2768. }
  2769. }
  2770. }
  2771. /* Dump the EH tree for FN on stderr. */
  2772. DEBUG_FUNCTION void
  2773. debug_eh_tree (struct function *fn)
  2774. {
  2775. dump_eh_tree (stderr, fn);
  2776. }
  2777. /* Verify invariants on EH datastructures. */
  2778. DEBUG_FUNCTION void
  2779. verify_eh_tree (struct function *fun)
  2780. {
  2781. eh_region r, outer;
  2782. int nvisited_lp, nvisited_r;
  2783. int count_lp, count_r, depth, i;
  2784. eh_landing_pad lp;
  2785. bool err = false;
  2786. if (!fun->eh->region_tree)
  2787. return;
  2788. count_r = 0;
  2789. for (i = 1; vec_safe_iterate (fun->eh->region_array, i, &r); ++i)
  2790. if (r)
  2791. {
  2792. if (r->index == i)
  2793. count_r++;
  2794. else
  2795. {
  2796. error ("region_array is corrupted for region %i", r->index);
  2797. err = true;
  2798. }
  2799. }
  2800. count_lp = 0;
  2801. for (i = 1; vec_safe_iterate (fun->eh->lp_array, i, &lp); ++i)
  2802. if (lp)
  2803. {
  2804. if (lp->index == i)
  2805. count_lp++;
  2806. else
  2807. {
  2808. error ("lp_array is corrupted for lp %i", lp->index);
  2809. err = true;
  2810. }
  2811. }
  2812. depth = nvisited_lp = nvisited_r = 0;
  2813. outer = NULL;
  2814. r = fun->eh->region_tree;
  2815. while (1)
  2816. {
  2817. if ((*fun->eh->region_array)[r->index] != r)
  2818. {
  2819. error ("region_array is corrupted for region %i", r->index);
  2820. err = true;
  2821. }
  2822. if (r->outer != outer)
  2823. {
  2824. error ("outer block of region %i is wrong", r->index);
  2825. err = true;
  2826. }
  2827. if (depth < 0)
  2828. {
  2829. error ("negative nesting depth of region %i", r->index);
  2830. err = true;
  2831. }
  2832. nvisited_r++;
  2833. for (lp = r->landing_pads; lp ; lp = lp->next_lp)
  2834. {
  2835. if ((*fun->eh->lp_array)[lp->index] != lp)
  2836. {
  2837. error ("lp_array is corrupted for lp %i", lp->index);
  2838. err = true;
  2839. }
  2840. if (lp->region != r)
  2841. {
  2842. error ("region of lp %i is wrong", lp->index);
  2843. err = true;
  2844. }
  2845. nvisited_lp++;
  2846. }
  2847. if (r->inner)
  2848. outer = r, r = r->inner, depth++;
  2849. else if (r->next_peer)
  2850. r = r->next_peer;
  2851. else
  2852. {
  2853. do
  2854. {
  2855. r = r->outer;
  2856. if (r == NULL)
  2857. goto region_done;
  2858. depth--;
  2859. outer = r->outer;
  2860. }
  2861. while (r->next_peer == NULL);
  2862. r = r->next_peer;
  2863. }
  2864. }
  2865. region_done:
  2866. if (depth != 0)
  2867. {
  2868. error ("tree list ends on depth %i", depth);
  2869. err = true;
  2870. }
  2871. if (count_r != nvisited_r)
  2872. {
  2873. error ("region_array does not match region_tree");
  2874. err = true;
  2875. }
  2876. if (count_lp != nvisited_lp)
  2877. {
  2878. error ("lp_array does not match region_tree");
  2879. err = true;
  2880. }
  2881. if (err)
  2882. {
  2883. dump_eh_tree (stderr, fun);
  2884. internal_error ("verify_eh_tree failed");
  2885. }
  2886. }
  2887. #include "gt-except.h"