cgexcept.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. tg.gethltemp(list,rec_exceptaddr,rec_exceptaddr.size,tt_persistent,t.envbuf);
  112. tg.gethltemp(list,rec_jmp_buf,rec_jmp_buf.size,tt_persistent,t.jmpbuf);
  113. tg.gethltemp(list,ossinttype,ossinttype.size,tt_persistent,t.reasonbuf);
  114. end;
  115. class procedure tcgexceptionstatehandler.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  116. begin
  117. tg.Ungettemp(list,t.jmpbuf);
  118. tg.ungettemp(list,t.envbuf);
  119. tg.ungettemp(list,t.reasonbuf);
  120. end;
  121. class procedure tcgexceptionstatehandler.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  122. var
  123. paraloc1, paraloc2, paraloc3, pushexceptres, setjmpres: tcgpara;
  124. pd: tprocdef;
  125. tmpresloc: tlocation;
  126. begin
  127. current_asmdata.getjumplabel(exceptstate.exceptionlabel);
  128. exceptstate.oldflowcontrol:=flowcontrol;
  129. exceptstate.finallycodelabel:=nil;;
  130. paraloc1.init;
  131. paraloc2.init;
  132. paraloc3.init;
  133. { fpc_pushexceptaddr(exceptionframetype, setjmp_buffer, exception_address_chain_entry) }
  134. pd:=search_system_proc('fpc_pushexceptaddr');
  135. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  136. paramanager.getcgtempparaloc(list,pd,2,paraloc2);
  137. paramanager.getcgtempparaloc(list,pd,3,paraloc3);
  138. if pd.is_pushleftright then
  139. begin
  140. { type of exceptionframe }
  141. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  142. { setjmp buffer }
  143. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  144. { exception address chain entry }
  145. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  146. end
  147. else
  148. begin
  149. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  150. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  151. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  152. end;
  153. paramanager.freecgpara(list,paraloc3);
  154. paramanager.freecgpara(list,paraloc2);
  155. paramanager.freecgpara(list,paraloc1);
  156. { perform the fpc_pushexceptaddr call }
  157. pushexceptres:=hlcg.g_call_system_proc(list,pd,[@paraloc1,@paraloc2,@paraloc3],nil);
  158. paraloc1.done;
  159. paraloc2.done;
  160. paraloc3.done;
  161. { get the result }
  162. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(pushexceptres.def));
  163. tmpresloc.register:=hlcg.getaddressregister(list,pushexceptres.def);
  164. hlcg.gen_load_cgpara_loc(list,pushexceptres.def,pushexceptres,tmpresloc,true);
  165. pushexceptres.resetiftemp;
  166. { fpc_setjmp(result_of_pushexceptaddr_call) }
  167. pd:=search_system_proc('fpc_setjmp');
  168. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  169. hlcg.a_load_reg_cgpara(list,pushexceptres.def,tmpresloc.register,paraloc1);
  170. paramanager.freecgpara(list,paraloc1);
  171. { perform the fpc_setjmp call }
  172. setjmpres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  173. paraloc1.done;
  174. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(setjmpres.def));
  175. tmpresloc.register:=hlcg.getintregister(list,setjmpres.def);
  176. hlcg.gen_load_cgpara_loc(list,setjmpres.def,setjmpres,tmpresloc,true);
  177. hlcg.g_exception_reason_save(list,setjmpres.def,ossinttype,tmpresloc.register,t.reasonbuf);
  178. { if we get 1 here in the function result register, it means that we
  179. longjmp'd back here }
  180. hlcg.a_cmp_const_reg_label(list,setjmpres.def,OC_NE,0,tmpresloc.register,exceptstate.exceptionlabel);
  181. setjmpres.resetiftemp;
  182. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  183. end;
  184. class procedure tcgexceptionstatehandler.emit_except_label(list: TAsmList; exceptframekind: texceptframekind; var exceptstate: texceptionstate;var exceptiontemps:texceptiontemps);
  185. begin
  186. hlcg.a_label(list,exceptstate.exceptionlabel);
  187. end;
  188. class procedure tcgexceptionstatehandler.end_try_block(list: TAsmList; exceptframekind: texceptframekind; const t: texceptiontemps; var exceptionstate: texceptionstate; endlabel: TAsmLabel);
  189. begin
  190. exceptionstate.newflowcontrol:=flowcontrol;
  191. flowcontrol:=exceptionstate.oldflowcontrol;
  192. end;
  193. class procedure tcgexceptionstatehandler.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree: boolean);
  194. var
  195. reasonreg: tregister;
  196. begin
  197. popaddrstack(list);
  198. if not onlyfree then
  199. begin
  200. reasonreg:=hlcg.getintregister(list,osuinttype);
  201. hlcg.g_exception_reason_load(list,osuinttype,osuinttype,t.reasonbuf,reasonreg);
  202. hlcg.a_cmp_const_reg_label(list,osuinttype,OC_EQ,a,reasonreg,endexceptlabel);
  203. end;
  204. end;
  205. { does the necessary things to clean up the object stack }
  206. { in the except block }
  207. class procedure tcgexceptionstatehandler.cleanupobjectstack(list: TAsmList);
  208. begin
  209. hlcg.g_call_system_proc(list,'fpc_doneexception',[],nil).resetiftemp;
  210. end;
  211. { generates code to be executed when another exeception is raised while
  212. control is inside except block }
  213. class procedure tcgexceptionstatehandler.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  214. var
  215. exitlabel: tasmlabel;
  216. begin
  217. current_asmdata.getjumplabel(exitlabel);
  218. { add an catch all action clause, at least psabieh needs this }
  219. catch_all_add(list);
  220. end_try_block(list,tek_except,t,entrystate,exitlabel);
  221. emit_except_label(list,tek_except,entrystate,t);
  222. { don't generate line info for internal cleanup }
  223. list.concat(tai_marker.create(mark_NoLineInfoStart));
  224. free_exception(list,t,entrystate,0,exitlabel,false);
  225. { we don't need to save/restore registers here because reraise never }
  226. { returns }
  227. hlcg.g_call_system_proc(list,'fpc_raise_nested',[],nil).resetiftemp;
  228. hlcg.a_label(list,exitlabel);
  229. cleanupobjectstack(list);
  230. end;
  231. class procedure tcgexceptionstatehandler.handle_reraise(list: TAsmList; const t: texceptiontemps; const entrystate: texceptionstate; const exceptframekind: texceptframekind);
  232. begin
  233. hlcg.g_call_system_proc(list,'fpc_reraise',[],nil).resetiftemp;
  234. end;
  235. class procedure tcgexceptionstatehandler.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister);
  236. var
  237. pd: tprocdef;
  238. href2: treference;
  239. fpc_catches_res,
  240. paraloc1: tcgpara;
  241. exceptloc: tlocation;
  242. indirect: boolean;
  243. otherunit: boolean;
  244. begin
  245. paraloc1.init;
  246. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  247. indirect:=(tf_supports_packages in target_info.flags) and
  248. (target_info.system in systems_indirect_var_imports) and
  249. (cs_imported_data in current_settings.localswitches) and
  250. otherunit;
  251. { send the vmt parameter }
  252. pd:=search_system_proc('fpc_catches');
  253. reference_reset_symbol(href2, current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect), 0, sizeof(pint), []);
  254. if otherunit then
  255. current_module.add_extern_asmsym(excepttype.vmt_mangledname, AB_EXTERNAL, AT_DATA);
  256. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  257. hlcg.a_loadaddr_ref_cgpara(list, excepttype.vmt_def, href2, paraloc1);
  258. paramanager.freecgpara(list, paraloc1);
  259. fpc_catches_res:=hlcg.g_call_system_proc(list, pd, [@paraloc1], nil);
  260. location_reset(exceptloc, LOC_REGISTER, def_cgsize(fpc_catches_res.def));
  261. exceptloc.register:=hlcg.getaddressregister(list, fpc_catches_res.def);
  262. hlcg.gen_load_cgpara_loc(list, fpc_catches_res.def, fpc_catches_res, exceptloc, true);
  263. { is it this catch? No. go to next onlabel }
  264. hlcg.a_cmp_const_reg_label(list, fpc_catches_res.def, OC_EQ, 0, exceptloc.register, nextonlabel);
  265. paraloc1.done;
  266. exceptlocdef:=fpc_catches_res.def;
  267. exceptlocreg:=exceptloc.register;
  268. end;
  269. class procedure tcgexceptionstatehandler.end_catch(list: TAsmList);
  270. begin
  271. { nothing to do by default }
  272. end;
  273. class procedure tcgexceptionstatehandler.catch_all_start(list: TAsmList);
  274. begin
  275. { nothing to do by default }
  276. end;
  277. class procedure tcgexceptionstatehandler.catch_all_add(list: TAsmList);
  278. begin
  279. { nothing to do by default }
  280. end;
  281. class procedure tcgexceptionstatehandler.catch_all_end(list: TAsmList);
  282. begin
  283. { nothing to do by default }
  284. end;
  285. class procedure tcgexceptionstatehandler.popaddrstack(list: TAsmList);
  286. begin
  287. hlcg.g_call_system_proc(list,'fpc_popaddrstack',[],nil).resetiftemp;
  288. end;
  289. end.