ncgflw.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for nodes that influence the flow which are
  4. the same for all (most?) processors
  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 ncgflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. symtype,symdef,
  24. aasmbase,aasmdata,
  25. node,nflw,
  26. pass_2,cgbase,cgutils,ncgutil,cgexcept;
  27. type
  28. tcgwhilerepeatnode = class(twhilerepeatnode)
  29. usedregvars: tusedregvars;
  30. procedure pass_generate_code;override;
  31. procedure sync_regvars(checkusedregvars: boolean);
  32. end;
  33. tcgifnode = class(tifnode)
  34. procedure pass_generate_code;override;
  35. end;
  36. tcgfornode = class(tfornode)
  37. procedure pass_generate_code;override;
  38. end;
  39. tcgexitnode = class(texitnode)
  40. procedure pass_generate_code;override;
  41. end;
  42. tcgbreaknode = class(tbreaknode)
  43. procedure pass_generate_code;override;
  44. end;
  45. tcgcontinuenode = class(tcontinuenode)
  46. procedure pass_generate_code;override;
  47. end;
  48. tcggotonode = class(tgotonode)
  49. procedure pass_generate_code;override;
  50. end;
  51. tcglabelnode = class(tlabelnode)
  52. protected
  53. asmlabel : tasmlabel;
  54. public
  55. function getasmlabel : tasmlabel; virtual;
  56. procedure pass_generate_code;override;
  57. end;
  58. tcgraisenode = class(traisenode)
  59. function pass_1: tnode;override;
  60. {$ifndef jvm}
  61. procedure pass_generate_code;override;
  62. {$endif jvm}
  63. end;
  64. tcgtryexceptnode = class(ttryexceptnode)
  65. protected
  66. type
  67. tframetype = (ft_try,ft_except);
  68. procedure emit_jump_out_of_try_except_frame(list: TasmList; frametype: tframetype; const exceptiontate: tcgexceptionstatehandler.texceptionstate; var excepttemps: tcgexceptionstatehandler.texceptiontemps; framelabel, outerlabel: tasmlabel); virtual;
  69. public
  70. procedure pass_generate_code;override;
  71. end;
  72. tcgtryfinallynode = class(ttryfinallynode)
  73. protected
  74. procedure emit_jump_out_of_try_finally_frame(list: TasmList; const reason: byte; const finallycodelabel: tasmlabel; var excepttemps: tcgexceptionstatehandler.texceptiontemps; framelabel: tasmlabel);
  75. function get_jump_out_of_try_finally_frame_label(const finallyexceptionstate: tcgexceptionstatehandler.texceptionstate): tasmlabel;
  76. public
  77. procedure handle_safecall_exception;
  78. procedure pass_generate_code;override;
  79. end;
  80. tcgonnode = class(tonnode)
  81. procedure pass_generate_code;override;
  82. end;
  83. implementation
  84. uses
  85. cutils,
  86. verbose,globals,systems,
  87. symconst,symsym,symtable,aasmtai,aasmcpu,defutil,
  88. procinfo,parabase,
  89. fmodule,
  90. cpubase,
  91. tgobj,paramgr,
  92. cgobj,hlcgobj,nutils
  93. {$ifndef jvm}
  94. ,psabiehpi
  95. {$endif jvm}
  96. ;
  97. {*****************************************************************************
  98. Second_While_RepeatN
  99. *****************************************************************************}
  100. procedure tcgwhilerepeatnode.sync_regvars(checkusedregvars: boolean);
  101. begin
  102. if (cs_opt_regvar in current_settings.optimizerswitches) and
  103. not(pi_has_label in current_procinfo.flags) then
  104. begin
  105. if checkusedregvars then
  106. begin
  107. usedregvars.intregvars.init;
  108. usedregvars.addrregvars.init;
  109. usedregvars.fpuregvars.init;
  110. usedregvars.mmregvars.init;
  111. { we have to synchronise both the regvars used in the loop }
  112. { and the ones in the while/until condition }
  113. get_used_regvars(self,usedregvars);
  114. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  115. end
  116. else
  117. begin
  118. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  119. usedregvars.intregvars.done;
  120. usedregvars.addrregvars.done;
  121. usedregvars.fpuregvars.done;
  122. usedregvars.mmregvars.done;
  123. end;
  124. end;
  125. end;
  126. procedure tcgwhilerepeatnode.pass_generate_code;
  127. var
  128. lcont,lbreak,lloop,
  129. oldclabel,oldblabel : tasmlabel;
  130. truelabel,falselabel : tasmlabel;
  131. oldflowcontrol : tflowcontrol;
  132. begin
  133. location_reset(location,LOC_VOID,OS_NO);
  134. current_asmdata.getjumplabel(lloop);
  135. current_asmdata.getjumplabel(lcont);
  136. current_asmdata.getjumplabel(lbreak);
  137. { arrange continue and breaklabels: }
  138. oldflowcontrol:=flowcontrol;
  139. oldclabel:=current_procinfo.CurrContinueLabel;
  140. oldblabel:=current_procinfo.CurrBreakLabel;
  141. include(flowcontrol,fc_inflowcontrol);
  142. exclude(flowcontrol,fc_unwind_loop);
  143. sync_regvars(true);
  144. {$ifdef OLDREGVARS}
  145. load_all_regvars(current_asmdata.CurrAsmList);
  146. {$endif OLDREGVARS}
  147. { handling code at the end as it is much more efficient, and makes
  148. while equal to repeat loop, only the end true/false is swapped (PFV) }
  149. if lnf_testatbegin in loopflags then
  150. hlcg.a_jmp_always(current_asmdata.CurrAsmList,lcont);
  151. if not(cs_opt_size in current_settings.optimizerswitches) then
  152. { align loop target, as an unconditional jump is done before,
  153. use jump align which assume that the instructions inserted as alignment are never executed }
  154. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  155. hlcg.a_label(current_asmdata.CurrAsmList,lloop);
  156. current_procinfo.CurrContinueLabel:=lcont;
  157. current_procinfo.CurrBreakLabel:=lbreak;
  158. if assigned(right) then
  159. secondpass(right);
  160. {$ifdef OLDREGVARS}
  161. load_all_regvars(current_asmdata.CurrAsmList);
  162. {$endif OLDREGVARS}
  163. hlcg.a_label(current_asmdata.CurrAsmList,lcont);
  164. if lnf_checknegate in loopflags then
  165. begin
  166. truelabel:=lbreak;
  167. falselabel:=lloop;
  168. end
  169. else
  170. begin
  171. truelabel:=lloop;
  172. falselabel:=lbreak;
  173. end;
  174. secondpass(left);
  175. hlcg.maketojumpboollabels(current_asmdata.CurrAsmList,left,truelabel,falselabel);
  176. hlcg.a_label(current_asmdata.CurrAsmList,lbreak);
  177. sync_regvars(false);
  178. current_procinfo.CurrContinueLabel:=oldclabel;
  179. current_procinfo.CurrBreakLabel:=oldblabel;
  180. { a break/continue in a while/repeat block can't be seen outside }
  181. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  182. end;
  183. {*****************************************************************************
  184. tcgIFNODE
  185. *****************************************************************************}
  186. procedure tcgifnode.pass_generate_code;
  187. var
  188. hl : tasmlabel;
  189. oldflowcontrol: tflowcontrol;
  190. (*
  191. org_regvar_loaded_other,
  192. then_regvar_loaded_other,
  193. else_regvar_loaded_other : regvarother_booleanarray;
  194. org_regvar_loaded_int,
  195. then_regvar_loaded_int,
  196. else_regvar_loaded_int : Tsuperregisterset;
  197. org_list,
  198. then_list,
  199. else_list : TAsmList;
  200. *)
  201. begin
  202. location_reset(location,LOC_VOID,OS_NO);
  203. hl:=nil;
  204. oldflowcontrol := flowcontrol;
  205. include(flowcontrol,fc_inflowcontrol);
  206. secondpass(left);
  207. (*
  208. { save regvars loaded in the beginning so that we can restore them }
  209. { when processing the else-block }
  210. if cs_opt_regvar in current_settings.optimizerswitches then
  211. begin
  212. org_list := current_asmdata.CurrAsmList;
  213. current_asmdata.CurrAsmList := TAsmList.create;
  214. end;
  215. *)
  216. hlcg.maketojumpbool(current_asmdata.CurrAsmList,left);
  217. (*
  218. if cs_opt_regvar in current_settings.optimizerswitches then
  219. begin
  220. org_regvar_loaded_int := rg.regvar_loaded_int;
  221. org_regvar_loaded_other := rg.regvar_loaded_other;
  222. end;
  223. *)
  224. if assigned(right) then
  225. begin
  226. hlcg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  227. secondpass(right);
  228. end;
  229. { save current asmlist (previous instructions + then-block) and }
  230. { loaded regvar state and create new clean ones }
  231. {
  232. if cs_opt_regvar in current_settings.optimizerswitches then
  233. begin
  234. then_regvar_loaded_int := rg.regvar_loaded_int;
  235. then_regvar_loaded_other := rg.regvar_loaded_other;
  236. rg.regvar_loaded_int := org_regvar_loaded_int;
  237. rg.regvar_loaded_other := org_regvar_loaded_other;
  238. then_list := current_asmdata.CurrAsmList;
  239. current_asmdata.CurrAsmList := TAsmList.create;
  240. end;
  241. }
  242. if assigned(t1) then
  243. begin
  244. if assigned(right) then
  245. begin
  246. current_asmdata.getjumplabel(hl);
  247. { do go back to if line !! }
  248. (*
  249. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  250. *)
  251. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^
  252. (*
  253. else
  254. current_filepos:=then_list.getlasttaifilepos^
  255. *)
  256. ;
  257. hlcg.a_jmp_always(current_asmdata.CurrAsmList,hl);
  258. if not(cs_opt_size in current_settings.optimizerswitches) then
  259. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  260. end;
  261. hlcg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  262. secondpass(t1);
  263. (*
  264. { save current asmlist (previous instructions + else-block) }
  265. { and loaded regvar state and create a new clean list }
  266. if cs_opt_regvar in current_settings.optimizerswitches then
  267. begin
  268. { else_regvar_loaded_int := rg.regvar_loaded_int;
  269. else_regvar_loaded_other := rg.regvar_loaded_other;}
  270. else_list := current_asmdata.CurrAsmList;
  271. current_asmdata.CurrAsmList := TAsmList.create;
  272. end;
  273. *)
  274. if assigned(right) then
  275. hlcg.a_label(current_asmdata.CurrAsmList,hl);
  276. end
  277. else
  278. begin
  279. (*
  280. if cs_opt_regvar in current_settings.optimizerswitches then
  281. begin
  282. { else_regvar_loaded_int := rg.regvar_loaded_int;
  283. else_regvar_loaded_other := rg.regvar_loaded_other;}
  284. else_list := current_asmdata.CurrAsmList;
  285. current_asmdata.CurrAsmList := TAsmList.create;
  286. end;
  287. *)
  288. if not(cs_opt_size in current_settings.optimizerswitches) then
  289. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.coalescealign,current_settings.alignment.coalescealignskipmax));
  290. hlcg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  291. end;
  292. if not(assigned(right)) then
  293. begin
  294. if not(cs_opt_size in current_settings.optimizerswitches) then
  295. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.coalescealign,current_settings.alignment.coalescealignskipmax));
  296. hlcg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  297. end;
  298. (*
  299. if cs_opt_regvar in current_settings.optimizerswitches then
  300. begin
  301. { add loads of regvars at the end of the then- and else-blocks }
  302. { so that at the end of both blocks the same regvars are loaded }
  303. { no else block? }
  304. if not assigned(t1) then
  305. begin
  306. sync_regvars_int(org_list,then_list,org_regvar_loaded_int,then_regvar_loaded_int);
  307. sync_regvars_other(org_list,then_list,org_regvar_loaded_other,then_regvar_loaded_other);
  308. end
  309. { no then block? }
  310. else if not assigned(right) then
  311. begin
  312. sync_regvars_int(org_list,else_list,org_regvar_loaded_int,else_regvar_loaded_int);
  313. sync_regvars_other(org_list,else_list,org_regvar_loaded_other,else_regvar_loaded_other);
  314. end
  315. { both else and then blocks }
  316. else
  317. begin
  318. sync_regvars_int(then_list,else_list,then_regvar_loaded_int,else_regvar_loaded_int);
  319. sync_regvars_other(then_list,else_list,then_regvar_loaded_other,else_regvar_loaded_other);
  320. end;
  321. { add all lists together }
  322. org_list.concatlist(then_list);
  323. then_list.free;
  324. org_list.concatlist(else_list);
  325. else_list.free;
  326. org_list.concatlist(current_asmdata.CurrAsmList);
  327. current_asmdata.CurrAsmList.free;
  328. current_asmdata.CurrAsmList := org_list;
  329. end;
  330. *)
  331. flowcontrol := oldflowcontrol + (flowcontrol - [fc_inflowcontrol]);
  332. end;
  333. {*****************************************************************************
  334. SecondFor
  335. *****************************************************************************}
  336. procedure tcgfornode.pass_generate_code;
  337. begin
  338. { for nodes are converted in pass_1 in a while loop }
  339. internalerror(2015082501);
  340. end;
  341. {*****************************************************************************
  342. SecondExitN
  343. *****************************************************************************}
  344. procedure tcgexitnode.pass_generate_code;
  345. begin
  346. location_reset(location,LOC_VOID,OS_NO);
  347. include(flowcontrol,fc_exit);
  348. if assigned(left) then
  349. secondpass(left);
  350. if (fc_unwind_exit in flowcontrol) then
  351. hlcg.g_local_unwind(current_asmdata.CurrAsmList,current_procinfo.CurrExitLabel)
  352. else
  353. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrExitLabel);
  354. if not(cs_opt_size in current_settings.optimizerswitches) then
  355. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  356. end;
  357. {*****************************************************************************
  358. SecondBreakN
  359. *****************************************************************************}
  360. procedure tcgbreaknode.pass_generate_code;
  361. begin
  362. location_reset(location,LOC_VOID,OS_NO);
  363. include(flowcontrol,fc_break);
  364. if current_procinfo.CurrBreakLabel<>nil then
  365. begin
  366. {$ifdef OLDREGVARS}
  367. load_all_regvars(current_asmdata.CurrAsmList);
  368. {$endif OLDREGVARS}
  369. if (fc_unwind_loop in flowcontrol) then
  370. hlcg.g_local_unwind(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel)
  371. else
  372. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel);
  373. if not(cs_opt_size in current_settings.optimizerswitches) then
  374. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  375. end
  376. else
  377. CGMessage(cg_e_break_not_allowed);
  378. end;
  379. {*****************************************************************************
  380. SecondContinueN
  381. *****************************************************************************}
  382. procedure tcgcontinuenode.pass_generate_code;
  383. begin
  384. location_reset(location,LOC_VOID,OS_NO);
  385. include(flowcontrol,fc_continue);
  386. if current_procinfo.CurrContinueLabel<>nil then
  387. begin
  388. {$ifdef OLDREGVARS}
  389. load_all_regvars(current_asmdata.CurrAsmList);
  390. {$endif OLDREGVARS}
  391. if (fc_unwind_loop in flowcontrol) then
  392. hlcg.g_local_unwind(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel)
  393. else
  394. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel);
  395. if not(cs_opt_size in current_settings.optimizerswitches) then
  396. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  397. end
  398. else
  399. CGMessage(cg_e_continue_not_allowed);
  400. end;
  401. {*****************************************************************************
  402. SecondGoto
  403. *****************************************************************************}
  404. procedure tcggotonode.pass_generate_code;
  405. begin
  406. location_reset(location,LOC_VOID,OS_NO);
  407. include(flowcontrol,fc_gotolabel);
  408. {$ifdef OLDREGVARS}
  409. load_all_regvars(current_asmdata.CurrAsmList);
  410. {$endif OLDREGVARS}
  411. hlcg.a_jmp_always(current_asmdata.CurrAsmList,tcglabelnode(labelnode).getasmlabel);
  412. if not(cs_opt_size in current_settings.optimizerswitches) then
  413. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  414. end;
  415. {*****************************************************************************
  416. SecondLabel
  417. *****************************************************************************}
  418. function tcglabelnode.getasmlabel : tasmlabel;
  419. begin
  420. if not(assigned(asmlabel)) then
  421. { labsym is not set in inlined procedures, but since assembler }
  422. { routines can't be inlined, that shouldn't matter }
  423. if assigned(labsym) and
  424. labsym.nonlocal then
  425. current_asmdata.getglobaljumplabel(asmlabel)
  426. else
  427. current_asmdata.getjumplabel(asmlabel);
  428. result:=asmlabel
  429. end;
  430. procedure tcglabelnode.pass_generate_code;
  431. begin
  432. location_reset(location,LOC_VOID,OS_NO);
  433. include(flowcontrol,fc_gotolabel);
  434. {$ifdef OLDREGVARS}
  435. load_all_regvars(current_asmdata.CurrAsmList);
  436. {$endif OLDREGVARS}
  437. hlcg.a_label(current_asmdata.CurrAsmList,getasmlabel);
  438. { Write also extra label if this label was referenced from
  439. assembler block }
  440. if assigned(labsym) and
  441. assigned(labsym.asmblocklabel) then
  442. hlcg.a_label(current_asmdata.CurrAsmList,labsym.asmblocklabel);
  443. secondpass(left);
  444. end;
  445. {*****************************************************************************
  446. SecondTryExcept
  447. *****************************************************************************}
  448. var
  449. endexceptlabel : tasmlabel;
  450. { jump out of an try/except block }
  451. procedure tcgtryexceptnode.emit_jump_out_of_try_except_frame(list: TasmList; frametype: tframetype; const exceptiontate: tcgexceptionstatehandler.texceptionstate; var excepttemps: tcgexceptionstatehandler.texceptiontemps; framelabel, outerlabel: tasmlabel);
  452. begin
  453. hlcg.a_label(list,framelabel);
  454. { we must also destroy the address frame which guards
  455. the exception object }
  456. cexceptionstatehandler.popaddrstack(list);
  457. hlcg.g_exception_reason_discard(list,osuinttype,excepttemps.reasonbuf);
  458. if frametype=ft_except then
  459. begin
  460. cexceptionstatehandler.cleanupobjectstack(list);
  461. cexceptionstatehandler.end_catch(list);
  462. end;
  463. hlcg.a_jmp_always(list,outerlabel);
  464. end;
  465. procedure tcgtryexceptnode.pass_generate_code;
  466. var
  467. oldendexceptlabel,
  468. lastonlabel,
  469. exitexceptlabel,
  470. continueexceptlabel,
  471. breakexceptlabel,
  472. exittrylabel,
  473. continuetrylabel,
  474. breaktrylabel,
  475. oldCurrExitLabel,
  476. oldContinueLabel,
  477. oldBreakLabel : tasmlabel;
  478. destroytemps,
  479. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  480. trystate,doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  481. afteronflowcontrol: tflowcontrol;
  482. label
  483. errorexit;
  484. begin
  485. location_reset(location,LOC_VOID,OS_NO);
  486. continuetrylabel:=nil;
  487. breaktrylabel:=nil;
  488. continueexceptlabel:=nil;
  489. breakexceptlabel:=nil;
  490. doobjectdestroyandreraisestate:=Default(tcgexceptionstatehandler.texceptionstate);
  491. { this can be called recursivly }
  492. oldBreakLabel:=nil;
  493. oldContinueLabel:=nil;
  494. oldendexceptlabel:=endexceptlabel;
  495. { save the old labels for control flow statements }
  496. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  497. if assigned(current_procinfo.CurrBreakLabel) then
  498. begin
  499. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  500. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  501. end;
  502. { get new labels for the control flow statements }
  503. current_asmdata.getjumplabel(exittrylabel);
  504. current_asmdata.getjumplabel(exitexceptlabel);
  505. if assigned(current_procinfo.CurrBreakLabel) then
  506. begin
  507. current_asmdata.getjumplabel(breaktrylabel);
  508. current_asmdata.getjumplabel(continuetrylabel);
  509. current_asmdata.getjumplabel(breakexceptlabel);
  510. current_asmdata.getjumplabel(continueexceptlabel);
  511. end;
  512. current_asmdata.getjumplabel(endexceptlabel);
  513. current_asmdata.getjumplabel(lastonlabel);
  514. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  515. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,tek_except,trystate);
  516. { try block }
  517. { set control flow labels for the try block }
  518. current_procinfo.CurrExitLabel:=exittrylabel;
  519. if assigned(oldBreakLabel) then
  520. begin
  521. current_procinfo.CurrContinueLabel:=continuetrylabel;
  522. current_procinfo.CurrBreakLabel:=breaktrylabel;
  523. end;
  524. secondpass(left);
  525. if codegenerror then
  526. goto errorexit;
  527. { don't generate line info for internal cleanup }
  528. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  529. cexceptionstatehandler.end_try_block(current_asmdata.CurrAsmList,tek_except,excepttemps,trystate,endexceptlabel);
  530. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,tek_except,trystate,excepttemps);
  531. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList, excepttemps, trystate, 0, endexceptlabel, false);
  532. { end cleanup }
  533. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  534. { set control flow labels for the except block }
  535. { and the on statements }
  536. current_procinfo.CurrExitLabel:=exitexceptlabel;
  537. if assigned(oldBreakLabel) then
  538. begin
  539. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  540. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  541. end;
  542. flowcontrol:=[fc_inflowcontrol]+trystate.oldflowcontrol*[fc_catching_exceptions];
  543. { on statements }
  544. if assigned(right) then
  545. secondpass(right);
  546. afteronflowcontrol:=flowcontrol;
  547. { don't generate line info for internal cleanup }
  548. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  549. hlcg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  550. { default handling except handling }
  551. if assigned(t1) then
  552. begin
  553. { FPC_CATCHES with 'default handler' flag (=-1) need no longer be called,
  554. it doesn't change any state and its return value is ignored (Sergei)
  555. }
  556. { the destruction of the exception object must be also }
  557. { guarded by an exception frame, but it can be omitted }
  558. { if there's no user code in 'except' block }
  559. cexceptionstatehandler.catch_all_start(current_asmdata.CurrAsmList);
  560. if not (has_no_code(t1)) then
  561. begin
  562. { if there is an outer frame that catches exceptions, remember this for the "except"
  563. part of this try/except }
  564. flowcontrol:=trystate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  565. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  566. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,destroytemps,tek_except,doobjectdestroyandreraisestate);
  567. cexceptionstatehandler.catch_all_add(current_asmdata.CurrAsmList);
  568. { the flowcontrol from the default except-block must be merged
  569. with the flowcontrol flags potentially set by the
  570. on-statements handled above (secondpass(right)), as they are
  571. at the same program level }
  572. flowcontrol:=
  573. flowcontrol+
  574. afteronflowcontrol;
  575. { except block needs line info }
  576. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  577. secondpass(t1);
  578. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraisestate);
  579. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  580. cexceptionstatehandler.catch_all_end(current_asmdata.CurrAsmList);
  581. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  582. end
  583. else
  584. begin
  585. doobjectdestroyandreraisestate.newflowcontrol:=afteronflowcontrol;
  586. cexceptionstatehandler.cleanupobjectstack(current_asmdata.CurrAsmList);
  587. cexceptionstatehandler.catch_all_end(current_asmdata.CurrAsmList);
  588. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  589. end;
  590. end
  591. else
  592. begin
  593. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,trystate,tek_except);
  594. doobjectdestroyandreraisestate.newflowcontrol:=afteronflowcontrol;
  595. end;
  596. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  597. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_except,doobjectdestroyandreraisestate,excepttemps,exitexceptlabel,oldCurrExitLabel);
  598. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  599. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_except,doobjectdestroyandreraisestate,excepttemps,breakexceptlabel,oldBreakLabel);
  600. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  601. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_except,doobjectdestroyandreraisestate,excepttemps,continueexceptlabel,oldContinueLabel);
  602. if fc_exit in trystate.newflowcontrol then
  603. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_try,trystate,excepttemps,exittrylabel,oldCurrExitLabel);
  604. if fc_break in trystate.newflowcontrol then
  605. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_try,trystate,excepttemps,breaktrylabel,oldBreakLabel);
  606. if fc_continue in trystate.newflowcontrol then
  607. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_try,trystate,excepttemps,continuetrylabel,oldContinueLabel);
  608. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  609. hlcg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  610. { end cleanup }
  611. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  612. errorexit:
  613. { restore all saved labels }
  614. endexceptlabel:=oldendexceptlabel;
  615. { restore the control flow labels }
  616. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  617. if assigned(oldBreakLabel) then
  618. begin
  619. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  620. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  621. end;
  622. { return all used control flow statements }
  623. flowcontrol:=trystate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol +
  624. trystate.newflowcontrol - [fc_inflowcontrol,fc_catching_exceptions]);
  625. end;
  626. procedure tcgonnode.pass_generate_code;
  627. var
  628. nextonlabel,
  629. exitonlabel,
  630. continueonlabel,
  631. breakonlabel,
  632. oldCurrExitLabel,
  633. oldContinueLabel,
  634. oldBreakLabel : tasmlabel;
  635. doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  636. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  637. exceptvarsym : tlocalvarsym;
  638. exceptlocdef: tdef;
  639. exceptlocreg: tregister;
  640. begin
  641. location_reset(location,LOC_VOID,OS_NO);
  642. oldCurrExitLabel:=nil;
  643. continueonlabel:=nil;
  644. breakonlabel:=nil;
  645. exitonlabel:=nil;
  646. current_asmdata.getjumplabel(nextonlabel);
  647. cexceptionstatehandler.begin_catch(current_asmdata.CurrAsmList,excepttype,nextonlabel,exceptlocdef,exceptlocreg);
  648. { Retrieve exception variable }
  649. if assigned(excepTSymtable) then
  650. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  651. else
  652. internalerror(2011020401);
  653. if assigned(exceptvarsym) then
  654. begin
  655. location_reset_ref(exceptvarsym.localloc, LOC_REFERENCE, def_cgsize(voidpointertype), voidpointertype.alignment, []);
  656. tg.GetLocal(current_asmdata.CurrAsmList, exceptvarsym.vardef.size, exceptvarsym.vardef, exceptvarsym.localloc.reference);
  657. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList, exceptlocdef, exceptvarsym.vardef, exceptlocreg, exceptvarsym.localloc.reference);
  658. end;
  659. { in the case that another exception is risen
  660. we've to destroy the old one, so create a new
  661. exception frame for the catch-handler }
  662. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  663. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,tek_except,doobjectdestroyandreraisestate);
  664. oldBreakLabel:=nil;
  665. oldContinueLabel:=nil;
  666. if assigned(right) then
  667. begin
  668. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  669. current_asmdata.getjumplabel(exitonlabel);
  670. current_procinfo.CurrExitLabel:=exitonlabel;
  671. if assigned(current_procinfo.CurrBreakLabel) then
  672. begin
  673. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  674. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  675. current_asmdata.getjumplabel(breakonlabel);
  676. current_asmdata.getjumplabel(continueonlabel);
  677. current_procinfo.CurrContinueLabel:=continueonlabel;
  678. current_procinfo.CurrBreakLabel:=breakonlabel;
  679. end;
  680. secondpass(right);
  681. end;
  682. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraisestate);
  683. { clear some stuff }
  684. if assigned(exceptvarsym) then
  685. begin
  686. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  687. exceptvarsym.localloc.loc:=LOC_INVALID;
  688. end;
  689. cexceptionstatehandler.end_catch(current_asmdata.CurrAsmList);
  690. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  691. if assigned(right) then
  692. begin
  693. { special handling for control flow instructions }
  694. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  695. begin
  696. { the address and object pop does secondtryexcept }
  697. hlcg.a_label(current_asmdata.CurrAsmList,exitonlabel);
  698. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  699. end;
  700. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  701. begin
  702. { the address and object pop does secondtryexcept }
  703. hlcg.a_label(current_asmdata.CurrAsmList,breakonlabel);
  704. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  705. end;
  706. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  707. begin
  708. { the address and object pop does secondtryexcept }
  709. hlcg.a_label(current_asmdata.CurrAsmList,continueonlabel);
  710. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  711. end;
  712. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  713. if assigned(oldBreakLabel) then
  714. begin
  715. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  716. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  717. end;
  718. end;
  719. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  720. hlcg.a_label(current_asmdata.CurrAsmList,nextonlabel);
  721. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  722. { propagate exit/break/continue }
  723. flowcontrol:=doobjectdestroyandreraisestate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  724. { next on node }
  725. if assigned(left) then
  726. secondpass(left);
  727. end;
  728. {*****************************************************************************
  729. SecondTryFinally
  730. *****************************************************************************}
  731. { jump out of a finally block }
  732. procedure tcgtryfinallynode.emit_jump_out_of_try_finally_frame(list: TasmList; const reason: byte; const finallycodelabel: tasmlabel; var excepttemps: tcgexceptionstatehandler.texceptiontemps; framelabel: tasmlabel);
  733. begin
  734. hlcg.a_label(list,framelabel);
  735. hlcg.g_exception_reason_discard(list,osuinttype,excepttemps.reasonbuf);
  736. hlcg.g_exception_reason_save_const(list,osuinttype,reason,excepttemps.reasonbuf);
  737. hlcg.a_jmp_always(list,finallycodelabel);
  738. end;
  739. function tcgtryfinallynode.get_jump_out_of_try_finally_frame_label(const finallyexceptionstate: tcgexceptionstatehandler.texceptionstate): tasmlabel;
  740. begin
  741. current_asmdata.getjumplabel(result);
  742. end;
  743. procedure tcgtryfinallynode.handle_safecall_exception;
  744. var
  745. cgpara, resultpara: tcgpara;
  746. selfsym: tparavarsym;
  747. pd: tprocdef;
  748. safecallresult: tlocalvarsym;
  749. begin
  750. { call fpc_safecallhandler, passing self for methods of classes,
  751. nil otherwise. }
  752. pd:=search_system_proc('fpc_safecallhandler');
  753. cgpara.init;
  754. paramanager.getcgtempparaloc(current_asmdata.CurrAsmList,pd,1,cgpara);
  755. if is_class(current_procinfo.procdef.struct) then
  756. begin
  757. selfsym:=tparavarsym(current_procinfo.procdef.parast.Find('self'));
  758. if (selfsym=nil) or (selfsym.typ<>paravarsym) then
  759. InternalError(2011123101);
  760. hlcg.a_load_loc_cgpara(current_asmdata.CurrAsmList,selfsym.vardef,selfsym.localloc,cgpara);
  761. end
  762. else
  763. hlcg.a_load_const_cgpara(current_asmdata.CurrAsmList,voidpointertype,0,cgpara);
  764. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  765. resultpara:=hlcg.g_call_system_proc(current_asmdata.CurrAsmList,pd,[@cgpara],nil);
  766. cgpara.done;
  767. safecallresult:=tlocalvarsym(current_procinfo.procdef.localst.Find('safecallresult'));
  768. hlcg.gen_load_cgpara_loc(current_asmdata.CurrAsmList,resultpara.def,resultpara,safecallresult.localloc,false);
  769. resultpara.resetiftemp;
  770. end;
  771. procedure tcgtryfinallynode.pass_generate_code;
  772. var
  773. endfinallylabel,
  774. exitfinallylabel,
  775. continuefinallylabel,
  776. breakfinallylabel,
  777. oldCurrExitLabel,
  778. oldContinueLabel,
  779. oldBreakLabel,
  780. finallyNoExceptionLabel: tasmlabel;
  781. finallyexceptionstate: tcgexceptionstatehandler.texceptionstate;
  782. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  783. reasonreg : tregister;
  784. exceptframekind: tcgexceptionstatehandler.texceptframekind;
  785. tmplist: TAsmList;
  786. procedure handle_breakcontinueexit(const finallycode: tasmlabel; doreraise: boolean);
  787. begin
  788. { no exception happened, but maybe break/continue/exit }
  789. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  790. if fc_exit in finallyexceptionstate.newflowcontrol then
  791. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,2,reasonreg,oldCurrExitLabel);
  792. if fc_break in finallyexceptionstate.newflowcontrol then
  793. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,3,reasonreg,oldBreakLabel);
  794. if fc_continue in finallyexceptionstate.newflowcontrol then
  795. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,4,reasonreg,oldContinueLabel);
  796. if doreraise then
  797. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,tek_normalfinally)
  798. else
  799. hlcg.g_unreachable(current_asmdata.CurrAsmList);
  800. { redirect break/continue/exit to the label above, with the reasonbuf set appropriately }
  801. if fc_exit in finallyexceptionstate.newflowcontrol then
  802. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,2,finallycode,excepttemps,exitfinallylabel);
  803. if fc_break in finallyexceptionstate.newflowcontrol then
  804. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,3,finallycode,excepttemps,breakfinallylabel);
  805. if fc_continue in finallyexceptionstate.newflowcontrol then
  806. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,4,finallycode,excepttemps,continuefinallylabel);
  807. end;
  808. begin
  809. location_reset(location,LOC_VOID,OS_NO);
  810. oldBreakLabel:=nil;
  811. oldContinueLabel:=nil;
  812. continuefinallylabel:=nil;
  813. breakfinallylabel:=nil;
  814. if not implicitframe then
  815. exceptframekind:=tek_normalfinally
  816. else
  817. exceptframekind:=tek_implicitfinally;
  818. current_asmdata.getjumplabel(endfinallylabel);
  819. { call setjmp, and jump to finally label on non-zero result }
  820. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  821. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,exceptframekind,finallyexceptionstate);
  822. { the finally block must catch break, continue and exit }
  823. { statements }
  824. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  825. exitfinallylabel:=get_jump_out_of_try_finally_frame_label(finallyexceptionstate);
  826. current_procinfo.CurrExitLabel:=exitfinallylabel;
  827. if assigned(current_procinfo.CurrBreakLabel) then
  828. begin
  829. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  830. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  831. breakfinallylabel:=get_jump_out_of_try_finally_frame_label(finallyexceptionstate);
  832. continuefinallylabel:=get_jump_out_of_try_finally_frame_label(finallyexceptionstate);
  833. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  834. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  835. end;
  836. { try code }
  837. if assigned(left) then
  838. begin
  839. secondpass(left);
  840. if codegenerror then
  841. exit;
  842. end;
  843. { don't generate line info for internal cleanup }
  844. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  845. cexceptionstatehandler.end_try_block(current_asmdata.CurrAsmList,exceptframekind,excepttemps,finallyexceptionstate,finallyexceptionstate.finallycodelabel);
  846. if assigned(third) then
  847. begin
  848. tmplist:=TAsmList.create;
  849. { emit the except label already (to a temporary list) to ensure that any calls in the
  850. finally block refer to the outer exception frame rather than to the exception frame
  851. that emits this same finally code in case an exception does happen }
  852. cexceptionstatehandler.emit_except_label(tmplist,exceptframekind,finallyexceptionstate,excepttemps);
  853. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  854. current_asmdata.getjumplabel(finallyNoExceptionLabel);
  855. hlcg.a_label(current_asmdata.CurrAsmList,finallyNoExceptionLabel);
  856. if not implicitframe then
  857. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  858. secondpass(third);
  859. if codegenerror then
  860. exit;
  861. if not implicitframe then
  862. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  863. reasonreg:=hlcg.getintregister(current_asmdata.CurrAsmList,osuinttype);
  864. hlcg.g_exception_reason_load(current_asmdata.CurrAsmList,osuinttype,osuinttype,excepttemps.reasonbuf,reasonreg);
  865. handle_breakcontinueexit(finallyNoExceptionLabel,false);
  866. current_asmdata.CurrAsmList.concatList(tmplist);
  867. tmplist.free;
  868. end
  869. else
  870. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,exceptframekind,finallyexceptionstate,excepttemps);
  871. { just free the frame information }
  872. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,1,finallyexceptionstate.exceptionlabel,true);
  873. { end cleanup }
  874. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  875. { finally code (don't unconditionally set fc_inflowcontrol, since the
  876. finally code is unconditionally executed; we do have to filter out
  877. flags regarding break/contrinue/etc. because we have to give an
  878. error in case one of those is used in the finally-code }
  879. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  880. secondpass(right);
  881. { goto is allowed if it stays inside the finally block,
  882. this is checked using the exception block number }
  883. if (flowcontrol-[fc_gotolabel])<>(finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions]) then
  884. CGMessage(cg_e_control_flow_outside_finally);
  885. if codegenerror then
  886. exit;
  887. { don't generate line info for internal cleanup }
  888. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  889. { same level as before try, but this part is only executed if an exception occcurred
  890. -> always fc_in_flowcontrol }
  891. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_catching_exceptions];
  892. include(flowcontrol,fc_inflowcontrol);
  893. if not assigned(third) then
  894. begin
  895. { the value should now be in the exception handler }
  896. reasonreg:=hlcg.getintregister(current_asmdata.CurrAsmList,osuinttype);
  897. hlcg.g_exception_reason_load(current_asmdata.CurrAsmList,osuinttype,osuinttype,excepttemps.reasonbuf,reasonreg);
  898. if implicitframe then
  899. begin
  900. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  901. { finally code only needed to be executed on exception (-> in
  902. if-branch -> fc_inflowcontrol) }
  903. if current_procinfo.procdef.generate_safecall_wrapper then
  904. begin
  905. handle_safecall_exception;
  906. { we have to jump immediatly as we have to return the value of FPC_SAFECALL }
  907. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  908. end
  909. else
  910. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,exceptframekind);
  911. { we have to load 0 into the execepttemp, else the program thinks an exception happended }
  912. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,0,finallyexceptionstate.exceptionlabel,excepttemps,exitfinallylabel);
  913. end
  914. else
  915. begin
  916. handle_breakcontinueexit(finallyexceptionstate.exceptionlabel,true);
  917. end;
  918. end
  919. else
  920. begin
  921. if implicitframe then
  922. begin
  923. if current_procinfo.procdef.generate_safecall_wrapper then
  924. handle_safecall_exception
  925. else
  926. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,exceptframekind);
  927. end
  928. else
  929. begin
  930. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,exceptframekind);
  931. end;
  932. end;
  933. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  934. hlcg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  935. { end cleanup }
  936. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  937. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  938. if assigned(current_procinfo.CurrBreakLabel) then
  939. begin
  940. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  941. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  942. end;
  943. flowcontrol:=finallyexceptionstate.oldflowcontrol+(finallyexceptionstate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  944. end;
  945. function tcgraisenode.pass_1: tnode;
  946. begin
  947. if not(tf_use_psabieh in target_info.flags) or assigned(left) then
  948. result:=inherited
  949. else
  950. begin
  951. expectloc:=LOC_VOID;
  952. result:=nil;
  953. end;
  954. end;
  955. {$ifndef jvm}
  956. { has to be factored out as well }
  957. procedure tcgraisenode.pass_generate_code;
  958. var
  959. CurrentLandingPad, CurrentAction, ReRaiseLandingPad: TPSABIEHAction;
  960. psabiehprocinfo: tpsabiehprocinfo;
  961. begin
  962. if not(tf_use_psabieh in target_info.flags) then
  963. Internalerror(2019021701);
  964. location_reset(location,LOC_VOID,OS_NO);
  965. CurrentLandingPad:=nil;
  966. CurrentAction:=nil;
  967. ReRaiseLandingPad:=nil;
  968. psabiehprocinfo:=current_procinfo as tpsabiehprocinfo;
  969. { a reraise must raise the exception to the parent exception frame }
  970. if fc_catching_exceptions in flowcontrol then
  971. begin
  972. psabiehprocinfo.CreateNewPSABIEHCallsite(current_asmdata.CurrAsmList);
  973. CurrentLandingPad:=psabiehprocinfo.CurrentLandingPad;
  974. if psabiehprocinfo.PopLandingPad(CurrentLandingPad) then
  975. exclude(flowcontrol,fc_catching_exceptions);
  976. CurrentAction:=psabiehprocinfo.CurrentAction;
  977. psabiehprocinfo.FinalizeAndPopAction(CurrentAction);
  978. if not(fc_catching_exceptions in flowcontrol) then
  979. begin
  980. ReRaiseLandingPad:=psabiehprocinfo.NoAction;
  981. psabiehprocinfo.PushAction(ReRaiseLandingPad);
  982. psabiehprocinfo.PushLandingPad(ReRaiseLandingPad);
  983. end;
  984. end;
  985. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil).resetiftemp;
  986. if assigned(CurrentLandingPad) then
  987. begin
  988. psabiehprocinfo.CreateNewPSABIEHCallsite(current_asmdata.CurrAsmList);
  989. if not(fc_catching_exceptions in flowcontrol) then
  990. begin
  991. psabiehprocinfo.PopLandingPad(psabiehprocinfo.CurrentLandingPad);
  992. psabiehprocinfo.PopAction(ReRaiseLandingPad);
  993. end;
  994. psabiehprocinfo.PushAction(CurrentAction);
  995. psabiehprocinfo.PushLandingPad(CurrentLandingPad);
  996. include(flowcontrol,fc_catching_exceptions);
  997. end;
  998. end;
  999. {$endif jvm}
  1000. begin
  1001. cwhilerepeatnode:=tcgwhilerepeatnode;
  1002. cifnode:=tcgifnode;
  1003. cfornode:=tcgfornode;
  1004. cexitnode:=tcgexitnode;
  1005. cbreaknode:=tcgbreaknode;
  1006. ccontinuenode:=tcgcontinuenode;
  1007. cgotonode:=tcggotonode;
  1008. clabelnode:=tcglabelnode;
  1009. craisenode:=tcgraisenode;
  1010. ctryexceptnode:=tcgtryexceptnode;
  1011. ctryfinallynode:=tcgtryfinallynode;
  1012. connode:=tcgonnode;
  1013. cexceptionstatehandler:=tcgexceptionstatehandler;
  1014. end.