cgexcept.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. {
  2. Copyright (c) 2017-2019 by Jonas Maebe, member of the
  3. Free Pascal Compiler development team
  4. Base class for exception handling support (setjump/longjump-based)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgexcept;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. aasmbase, aasmdata,
  24. symtype,symdef,
  25. cgbase,cgutils,pass_2;
  26. type
  27. { Utility class for exception handling state management that is used
  28. by tryexcept/tryfinally/on nodes (in a separate class so it can both
  29. be shared and overridden)
  30. Never instantiated. }
  31. tcgexceptionstatehandler = class
  32. type
  33. texceptiontemps=record
  34. jmpbuf,
  35. envbuf,
  36. reasonbuf : treference;
  37. { when using dwarf based eh handling, the landing pads get the unwind info passed, it is
  38. stored in the given register so it can be passed to unwind_resume }
  39. unwind_info : TRegister;
  40. end;
  41. texceptionstate = record
  42. exceptionlabel: TAsmLabel;
  43. oldflowcontrol,
  44. newflowcontrol: tflowcontrol;
  45. finallycodelabel : TAsmLabel;
  46. end;
  47. texceptframekind = (tek_except, tek_implicitfinally, tek_normalfinally);
  48. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); virtual;
  49. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); virtual;
  50. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); virtual;
  51. { start of "except/finally" block }
  52. class procedure emit_except_label(list: TAsmList; exceptframekind: texceptframekind; var exceptstate: texceptionstate;var exceptiontemps:texceptiontemps); virtual;
  53. { end of a try-block, label comes after the end of try/except or
  54. try/finally }
  55. class procedure end_try_block(list: TAsmList; exceptframekind: texceptframekind; const t: texceptiontemps; var exceptionstate: texceptionstate; endlabel: TAsmLabel); virtual;
  56. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); virtual;
  57. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); virtual;
  58. class procedure handle_reraise(list:TAsmList;const t:texceptiontemps;const entrystate: texceptionstate; const exceptframekind: texceptframekind); virtual;
  59. { start of an "on" (catch) block }
  60. class procedure begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister); virtual;
  61. { end of an "on" (catch) block }
  62. class procedure end_catch(list: TAsmList); virtual;
  63. { called for a catch all exception }
  64. class procedure catch_all_start(list: TAsmList); virtual;
  65. { called after the catch all exception has been started with new_exception }
  66. class procedure catch_all_add(list: TAsmList); virtual;
  67. class procedure catch_all_end(list: TAsmList); virtual;
  68. class procedure cleanupobjectstack(list: TAsmList); virtual;
  69. class procedure popaddrstack(list: TAsmList); virtual;
  70. class function use_cleanup(const exceptframekind: texceptframekind): boolean;
  71. end;
  72. tcgexceptionstatehandlerclass = class of tcgexceptionstatehandler;
  73. var
  74. cexceptionstatehandler: tcgexceptionstatehandlerclass = tcgexceptionstatehandler;
  75. implementation
  76. uses
  77. globals,
  78. systems,
  79. fmodule,
  80. aasmtai,
  81. symconst,symtable,defutil,
  82. parabase,paramgr,
  83. procinfo,
  84. tgobj,
  85. hlcgobj;
  86. {*****************************************************************************
  87. tcgexceptionstatehandler
  88. *****************************************************************************}
  89. class function tcgexceptionstatehandler.use_cleanup(const exceptframekind: texceptframekind): boolean;
  90. begin
  91. { in case of an exception caught by the implicit exception frame of
  92. a safecall routine, this is not a cleanup frame but one that
  93. catches the exception and returns a value from the function }
  94. result:=
  95. (exceptframekind=tek_implicitfinally) and
  96. not((tf_safecall_exceptions in target_info.flags) and
  97. (current_procinfo.procdef.proccalloption=pocall_safecall));
  98. end;
  99. { Allocate the buffers for exception management and setjmp environment.
  100. Return a pointer to these buffers, send them to the utility routine
  101. so they are registered, and then call setjmp.
  102. Then compare the result of setjmp with 0, and if not equal
  103. to zero, then jump to exceptlabel.
  104. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  105. It is to note that this routine may be called *after* the stackframe of a
  106. routine has been called, therefore on machines where the stack cannot
  107. be modified, all temps should be allocated on the heap instead of the
  108. stack. }
  109. class procedure tcgexceptionstatehandler.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  110. begin
  111. if not assigned(exceptionreasontype) then
  112. exceptionreasontype:=search_system_proc('fpc_setjmp').returndef;
  113. tg.gethltemp(list,rec_exceptaddr,rec_exceptaddr.size,tt_persistent,t.envbuf);
  114. tg.gethltemp(list,rec_jmp_buf,rec_jmp_buf.size,tt_persistent,t.jmpbuf);
  115. tg.gethltemp(list,exceptionreasontype,exceptionreasontype.size,tt_persistent,t.reasonbuf);
  116. end;
  117. class procedure tcgexceptionstatehandler.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  118. begin
  119. tg.Ungettemp(list,t.jmpbuf);
  120. tg.ungettemp(list,t.envbuf);
  121. tg.ungettemp(list,t.reasonbuf);
  122. end;
  123. class procedure tcgexceptionstatehandler.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  124. var
  125. paraloc1, paraloc2, paraloc3, pushexceptres, setjmpres: tcgpara;
  126. pd: tprocdef;
  127. tmpresloc: tlocation;
  128. begin
  129. current_asmdata.getjumplabel(exceptstate.exceptionlabel);
  130. exceptstate.oldflowcontrol:=flowcontrol;
  131. exceptstate.finallycodelabel:=nil;
  132. paraloc1.init;
  133. paraloc2.init;
  134. paraloc3.init;
  135. { fpc_pushexceptaddr(exceptionframetype, setjmp_buffer, exception_address_chain_entry) }
  136. pd:=search_system_proc('fpc_pushexceptaddr');
  137. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  138. paramanager.getcgtempparaloc(list,pd,2,paraloc2);
  139. paramanager.getcgtempparaloc(list,pd,3,paraloc3);
  140. if pd.is_pushleftright then
  141. begin
  142. { type of exceptionframe }
  143. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  144. { setjmp buffer }
  145. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  146. { exception address chain entry }
  147. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  148. end
  149. else
  150. begin
  151. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  152. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  153. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  154. end;
  155. paramanager.freecgpara(list,paraloc3);
  156. paramanager.freecgpara(list,paraloc2);
  157. paramanager.freecgpara(list,paraloc1);
  158. { perform the fpc_pushexceptaddr call }
  159. pushexceptres:=hlcg.g_call_system_proc(list,pd,[@paraloc1,@paraloc2,@paraloc3],nil);
  160. paraloc1.done;
  161. paraloc2.done;
  162. paraloc3.done;
  163. { get the result }
  164. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(pushexceptres.def));
  165. tmpresloc.register:=hlcg.getaddressregister(list,pushexceptres.def);
  166. hlcg.gen_load_cgpara_loc(list,pushexceptres.def,pushexceptres,tmpresloc,true);
  167. pushexceptres.resetiftemp;
  168. { fpc_setjmp(result_of_pushexceptaddr_call) }
  169. pd:=search_system_proc('fpc_setjmp');
  170. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  171. hlcg.a_load_reg_cgpara(list,pushexceptres.def,tmpresloc.register,paraloc1);
  172. paramanager.freecgpara(list,paraloc1);
  173. { perform the fpc_setjmp call }
  174. setjmpres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  175. paraloc1.done;
  176. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(setjmpres.def));
  177. tmpresloc.register:=hlcg.getintregister(list,setjmpres.def);
  178. hlcg.gen_load_cgpara_loc(list,setjmpres.def,setjmpres,tmpresloc,true);
  179. hlcg.g_exception_reason_save(list,setjmpres.def,exceptionreasontype,tmpresloc.register,t.reasonbuf);
  180. { if we get 1 here in the function result register, it means that we
  181. longjmp'd back here }
  182. hlcg.a_cmp_const_reg_label(list,setjmpres.def,OC_NE,0,tmpresloc.register,exceptstate.exceptionlabel);
  183. setjmpres.resetiftemp;
  184. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  185. end;
  186. class procedure tcgexceptionstatehandler.emit_except_label(list: TAsmList; exceptframekind: texceptframekind; var exceptstate: texceptionstate;var exceptiontemps:texceptiontemps);
  187. begin
  188. hlcg.a_label(list,exceptstate.exceptionlabel);
  189. end;
  190. class procedure tcgexceptionstatehandler.end_try_block(list: TAsmList; exceptframekind: texceptframekind; const t: texceptiontemps; var exceptionstate: texceptionstate; endlabel: TAsmLabel);
  191. begin
  192. exceptionstate.newflowcontrol:=flowcontrol;
  193. flowcontrol:=exceptionstate.oldflowcontrol;
  194. end;
  195. class procedure tcgexceptionstatehandler.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree: boolean);
  196. var
  197. reasonreg: tregister;
  198. begin
  199. popaddrstack(list);
  200. if not onlyfree then
  201. begin
  202. reasonreg:=hlcg.getintregister(list,exceptionreasontype);
  203. hlcg.g_exception_reason_load(list,exceptionreasontype,exceptionreasontype,t.reasonbuf,reasonreg);
  204. hlcg.a_cmp_const_reg_label(list,exceptionreasontype,OC_EQ,a,reasonreg,endexceptlabel);
  205. end;
  206. end;
  207. { does the necessary things to clean up the object stack }
  208. { in the except block }
  209. class procedure tcgexceptionstatehandler.cleanupobjectstack(list: TAsmList);
  210. begin
  211. hlcg.g_call_system_proc(list,'fpc_doneexception',[],nil).resetiftemp;
  212. end;
  213. { generates code to be executed when another exeception is raised while
  214. control is inside except block }
  215. class procedure tcgexceptionstatehandler.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  216. var
  217. exitlabel: tasmlabel;
  218. begin
  219. current_asmdata.getjumplabel(exitlabel);
  220. { add an catch all action clause, at least psabieh needs this }
  221. catch_all_add(list);
  222. end_try_block(list,tek_except,t,entrystate,exitlabel);
  223. emit_except_label(list,tek_except,entrystate,t);
  224. { don't generate line info for internal cleanup }
  225. list.concat(tai_marker.create(mark_NoLineInfoStart));
  226. free_exception(list,t,entrystate,0,exitlabel,false);
  227. { we don't need to save/restore registers here because reraise never }
  228. { returns }
  229. hlcg.g_call_system_proc(list,'fpc_raise_nested',[],nil).resetiftemp;
  230. hlcg.a_label(list,exitlabel);
  231. cleanupobjectstack(list);
  232. end;
  233. class procedure tcgexceptionstatehandler.handle_reraise(list: TAsmList; const t: texceptiontemps; const entrystate: texceptionstate; const exceptframekind: texceptframekind);
  234. begin
  235. hlcg.g_call_system_proc(list,'fpc_reraise',[],nil).resetiftemp;
  236. end;
  237. class procedure tcgexceptionstatehandler.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister);
  238. var
  239. pd: tprocdef;
  240. href2: treference;
  241. fpc_catches_res,
  242. paraloc1: tcgpara;
  243. exceptloc: tlocation;
  244. indirect: boolean;
  245. otherunit: boolean;
  246. begin
  247. paraloc1.init;
  248. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  249. indirect:=(tf_supports_packages in target_info.flags) and
  250. (target_info.system in systems_indirect_var_imports) and
  251. (cs_imported_data in current_settings.localswitches) and
  252. otherunit;
  253. { send the vmt parameter }
  254. pd:=search_system_proc('fpc_catches');
  255. reference_reset_symbol(href2, current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect), 0, sizeof(pint), []);
  256. if otherunit then
  257. current_module.add_extern_asmsym(excepttype.vmt_mangledname, AB_EXTERNAL, AT_DATA);
  258. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  259. hlcg.a_loadaddr_ref_cgpara(list, excepttype.vmt_def, href2, paraloc1);
  260. paramanager.freecgpara(list, paraloc1);
  261. fpc_catches_res:=hlcg.g_call_system_proc(list, pd, [@paraloc1], nil);
  262. location_reset(exceptloc, LOC_REGISTER, def_cgsize(fpc_catches_res.def));
  263. exceptloc.register:=hlcg.getaddressregister(list, fpc_catches_res.def);
  264. hlcg.gen_load_cgpara_loc(list, fpc_catches_res.def, fpc_catches_res, exceptloc, true);
  265. { is it this catch? No. go to next onlabel }
  266. hlcg.a_cmp_const_reg_label(list, fpc_catches_res.def, OC_EQ, 0, exceptloc.register, nextonlabel);
  267. paraloc1.done;
  268. exceptlocdef:=fpc_catches_res.def;
  269. exceptlocreg:=exceptloc.register;
  270. end;
  271. class procedure tcgexceptionstatehandler.end_catch(list: TAsmList);
  272. begin
  273. { nothing to do by default }
  274. end;
  275. class procedure tcgexceptionstatehandler.catch_all_start(list: TAsmList);
  276. begin
  277. { nothing to do by default }
  278. end;
  279. class procedure tcgexceptionstatehandler.catch_all_add(list: TAsmList);
  280. begin
  281. { nothing to do by default }
  282. end;
  283. class procedure tcgexceptionstatehandler.catch_all_end(list: TAsmList);
  284. begin
  285. { nothing to do by default }
  286. end;
  287. class procedure tcgexceptionstatehandler.popaddrstack(list: TAsmList);
  288. begin
  289. hlcg.g_call_system_proc(list,'fpc_popaddrstack',[],nil).resetiftemp;
  290. end;
  291. end.