ncgflw.pas 49 KB

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