ncgflw.pas 50 KB

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