ncgflw.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. if not(cs_opt_size in current_settings.optimizerswitches) then
  358. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  359. end;
  360. {*****************************************************************************
  361. SecondBreakN
  362. *****************************************************************************}
  363. procedure tcgbreaknode.pass_generate_code;
  364. begin
  365. location_reset(location,LOC_VOID,OS_NO);
  366. include(flowcontrol,fc_break);
  367. if current_procinfo.CurrBreakLabel<>nil then
  368. begin
  369. {$ifdef OLDREGVARS}
  370. load_all_regvars(current_asmdata.CurrAsmList);
  371. {$endif OLDREGVARS}
  372. if (fc_unwind_loop in flowcontrol) then
  373. hlcg.g_local_unwind(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel)
  374. else
  375. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrBreakLabel);
  376. if not(cs_opt_size in current_settings.optimizerswitches) then
  377. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  378. end
  379. else
  380. CGMessage(cg_e_break_not_allowed);
  381. end;
  382. {*****************************************************************************
  383. SecondContinueN
  384. *****************************************************************************}
  385. procedure tcgcontinuenode.pass_generate_code;
  386. begin
  387. location_reset(location,LOC_VOID,OS_NO);
  388. include(flowcontrol,fc_continue);
  389. if current_procinfo.CurrContinueLabel<>nil then
  390. begin
  391. {$ifdef OLDREGVARS}
  392. load_all_regvars(current_asmdata.CurrAsmList);
  393. {$endif OLDREGVARS}
  394. if (fc_unwind_loop in flowcontrol) then
  395. hlcg.g_local_unwind(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel)
  396. else
  397. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel);
  398. if not(cs_opt_size in current_settings.optimizerswitches) then
  399. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  400. end
  401. else
  402. CGMessage(cg_e_continue_not_allowed);
  403. end;
  404. {*****************************************************************************
  405. SecondGoto
  406. *****************************************************************************}
  407. procedure tcggotonode.pass_generate_code;
  408. begin
  409. location_reset(location,LOC_VOID,OS_NO);
  410. include(flowcontrol,fc_gotolabel);
  411. {$ifdef OLDREGVARS}
  412. load_all_regvars(current_asmdata.CurrAsmList);
  413. {$endif OLDREGVARS}
  414. hlcg.a_jmp_always(current_asmdata.CurrAsmList,tcglabelnode(labelnode).getasmlabel);
  415. if not(cs_opt_size in current_settings.optimizerswitches) then
  416. current_asmdata.CurrAsmList.concat(cai_align.create_max(current_settings.alignment.jumpalign,current_settings.alignment.jumpalignskipmax));
  417. end;
  418. {*****************************************************************************
  419. SecondLabel
  420. *****************************************************************************}
  421. function tcglabelnode.getasmlabel : tasmlabel;
  422. begin
  423. if not(assigned(asmlabel)) then
  424. { labsym is not set in inlined procedures, but since assembler }
  425. { routines can't be inlined, that shouldn't matter }
  426. if assigned(labsym) and
  427. labsym.nonlocal then
  428. current_asmdata.getglobaljumplabel(asmlabel)
  429. else
  430. current_asmdata.getjumplabel(asmlabel);
  431. result:=asmlabel
  432. end;
  433. procedure tcglabelnode.pass_generate_code;
  434. begin
  435. location_reset(location,LOC_VOID,OS_NO);
  436. include(flowcontrol,fc_gotolabel);
  437. {$ifdef OLDREGVARS}
  438. load_all_regvars(current_asmdata.CurrAsmList);
  439. {$endif OLDREGVARS}
  440. hlcg.a_label(current_asmdata.CurrAsmList,getasmlabel);
  441. { Write also extra label if this label was referenced from
  442. assembler block }
  443. if assigned(labsym) and
  444. assigned(labsym.asmblocklabel) then
  445. hlcg.a_label(current_asmdata.CurrAsmList,labsym.asmblocklabel);
  446. secondpass(left);
  447. end;
  448. {*****************************************************************************
  449. SecondTryExcept
  450. *****************************************************************************}
  451. var
  452. endexceptlabel : tasmlabel;
  453. { jump out of an try/except block }
  454. procedure tcgtryexceptnode.emit_jump_out_of_try_except_frame(list: TasmList; frametype: tframetype; const exceptiontate: tcgexceptionstatehandler.texceptionstate; var excepttemps: tcgexceptionstatehandler.texceptiontemps; framelabel, outerlabel: tasmlabel);
  455. begin
  456. hlcg.a_label(list,framelabel);
  457. { we must also destroy the address frame which guards
  458. the exception object }
  459. cexceptionstatehandler.popaddrstack(list);
  460. hlcg.g_exception_reason_discard(list,osuinttype,excepttemps.reasonbuf);
  461. if frametype=ft_except then
  462. begin
  463. cexceptionstatehandler.cleanupobjectstack(list);
  464. cexceptionstatehandler.end_catch(list);
  465. end;
  466. hlcg.a_jmp_always(list,outerlabel);
  467. end;
  468. procedure tcgtryexceptnode.pass_generate_code;
  469. var
  470. oldendexceptlabel,
  471. lastonlabel,
  472. exitexceptlabel,
  473. continueexceptlabel,
  474. breakexceptlabel,
  475. exittrylabel,
  476. continuetrylabel,
  477. breaktrylabel,
  478. oldCurrExitLabel,
  479. oldContinueLabel,
  480. oldBreakLabel : tasmlabel;
  481. destroytemps,
  482. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  483. trystate,doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  484. afteronflowcontrol: tflowcontrol;
  485. label
  486. errorexit;
  487. begin
  488. location_reset(location,LOC_VOID,OS_NO);
  489. continuetrylabel:=nil;
  490. breaktrylabel:=nil;
  491. continueexceptlabel:=nil;
  492. breakexceptlabel:=nil;
  493. doobjectdestroyandreraisestate:=Default(tcgexceptionstatehandler.texceptionstate);
  494. { this can be called recursivly }
  495. oldBreakLabel:=nil;
  496. oldContinueLabel:=nil;
  497. oldendexceptlabel:=endexceptlabel;
  498. { save the old labels for control flow statements }
  499. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  500. if assigned(current_procinfo.CurrBreakLabel) then
  501. begin
  502. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  503. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  504. end;
  505. { get new labels for the control flow statements }
  506. current_asmdata.getjumplabel(exittrylabel);
  507. current_asmdata.getjumplabel(exitexceptlabel);
  508. if assigned(current_procinfo.CurrBreakLabel) then
  509. begin
  510. current_asmdata.getjumplabel(breaktrylabel);
  511. current_asmdata.getjumplabel(continuetrylabel);
  512. current_asmdata.getjumplabel(breakexceptlabel);
  513. current_asmdata.getjumplabel(continueexceptlabel);
  514. end;
  515. current_asmdata.getjumplabel(endexceptlabel);
  516. current_asmdata.getjumplabel(lastonlabel);
  517. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  518. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,tek_except,trystate);
  519. { try block }
  520. { set control flow labels for the try block }
  521. current_procinfo.CurrExitLabel:=exittrylabel;
  522. if assigned(oldBreakLabel) then
  523. begin
  524. current_procinfo.CurrContinueLabel:=continuetrylabel;
  525. current_procinfo.CurrBreakLabel:=breaktrylabel;
  526. end;
  527. secondpass(left);
  528. if codegenerror then
  529. goto errorexit;
  530. { don't generate line info for internal cleanup }
  531. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  532. cexceptionstatehandler.end_try_block(current_asmdata.CurrAsmList,tek_except,excepttemps,trystate,endexceptlabel);
  533. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,tek_except,trystate,excepttemps);
  534. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList, excepttemps, trystate, 0, endexceptlabel, false);
  535. { end cleanup }
  536. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  537. { set control flow labels for the except block }
  538. { and the on statements }
  539. current_procinfo.CurrExitLabel:=exitexceptlabel;
  540. if assigned(oldBreakLabel) then
  541. begin
  542. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  543. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  544. end;
  545. flowcontrol:=[fc_inflowcontrol]+trystate.oldflowcontrol*[fc_catching_exceptions];
  546. { on statements }
  547. if assigned(right) then
  548. secondpass(right);
  549. afteronflowcontrol:=flowcontrol;
  550. { don't generate line info for internal cleanup }
  551. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  552. hlcg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  553. { default handling except handling }
  554. if assigned(t1) then
  555. begin
  556. { FPC_CATCHES with 'default handler' flag (=-1) need no longer be called,
  557. it doesn't change any state and its return value is ignored (Sergei)
  558. }
  559. { the destruction of the exception object must be also }
  560. { guarded by an exception frame, but it can be omitted }
  561. { if there's no user code in 'except' block }
  562. cexceptionstatehandler.catch_all_start(current_asmdata.CurrAsmList);
  563. if not (has_no_code(t1)) then
  564. begin
  565. { if there is an outer frame that catches exceptions, remember this for the "except"
  566. part of this try/except }
  567. flowcontrol:=trystate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  568. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  569. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,destroytemps,tek_except,doobjectdestroyandreraisestate);
  570. cexceptionstatehandler.catch_all_add(current_asmdata.CurrAsmList);
  571. { the flowcontrol from the default except-block must be merged
  572. with the flowcontrol flags potentially set by the
  573. on-statements handled above (secondpass(right)), as they are
  574. at the same program level }
  575. flowcontrol:=
  576. flowcontrol+
  577. afteronflowcontrol;
  578. { except block needs line info }
  579. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  580. secondpass(t1);
  581. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraisestate);
  582. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  583. cexceptionstatehandler.catch_all_end(current_asmdata.CurrAsmList);
  584. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  585. end
  586. else
  587. begin
  588. doobjectdestroyandreraisestate.newflowcontrol:=afteronflowcontrol;
  589. cexceptionstatehandler.cleanupobjectstack(current_asmdata.CurrAsmList);
  590. cexceptionstatehandler.catch_all_end(current_asmdata.CurrAsmList);
  591. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  592. end;
  593. end
  594. else
  595. begin
  596. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,trystate,tek_except);
  597. doobjectdestroyandreraisestate.newflowcontrol:=afteronflowcontrol;
  598. end;
  599. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  600. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_except,doobjectdestroyandreraisestate,excepttemps,exitexceptlabel,oldCurrExitLabel);
  601. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  602. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_except,doobjectdestroyandreraisestate,excepttemps,breakexceptlabel,oldBreakLabel);
  603. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  604. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_except,doobjectdestroyandreraisestate,excepttemps,continueexceptlabel,oldContinueLabel);
  605. if fc_exit in trystate.newflowcontrol then
  606. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_try,trystate,excepttemps,exittrylabel,oldCurrExitLabel);
  607. if fc_break in trystate.newflowcontrol then
  608. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_try,trystate,excepttemps,breaktrylabel,oldBreakLabel);
  609. if fc_continue in trystate.newflowcontrol then
  610. emit_jump_out_of_try_except_frame(current_asmdata.CurrAsmList,ft_try,trystate,excepttemps,continuetrylabel,oldContinueLabel);
  611. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  612. hlcg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  613. { end cleanup }
  614. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  615. errorexit:
  616. { restore all saved labels }
  617. endexceptlabel:=oldendexceptlabel;
  618. { restore the control flow labels }
  619. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  620. if assigned(oldBreakLabel) then
  621. begin
  622. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  623. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  624. end;
  625. { return all used control flow statements }
  626. flowcontrol:=trystate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol +
  627. trystate.newflowcontrol - [fc_inflowcontrol,fc_catching_exceptions]);
  628. end;
  629. procedure tcgonnode.pass_generate_code;
  630. var
  631. nextonlabel,
  632. exitonlabel,
  633. continueonlabel,
  634. breakonlabel,
  635. oldCurrExitLabel,
  636. oldContinueLabel,
  637. oldBreakLabel : tasmlabel;
  638. doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  639. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  640. exceptvarsym : tlocalvarsym;
  641. exceptlocdef: tdef;
  642. exceptlocreg: tregister;
  643. begin
  644. location_reset(location,LOC_VOID,OS_NO);
  645. oldCurrExitLabel:=nil;
  646. continueonlabel:=nil;
  647. breakonlabel:=nil;
  648. exitonlabel:=nil;
  649. current_asmdata.getjumplabel(nextonlabel);
  650. cexceptionstatehandler.begin_catch(current_asmdata.CurrAsmList,excepttype,nextonlabel,exceptlocdef,exceptlocreg);
  651. { Retrieve exception variable }
  652. if assigned(excepTSymtable) then
  653. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  654. else
  655. internalerror(2011020401);
  656. if assigned(exceptvarsym) then
  657. begin
  658. location_reset_ref(exceptvarsym.localloc, LOC_REFERENCE, def_cgsize(voidpointertype), voidpointertype.alignment, []);
  659. tg.GetLocal(current_asmdata.CurrAsmList, exceptvarsym.vardef.size, exceptvarsym.vardef, exceptvarsym.localloc.reference);
  660. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList, exceptlocdef, exceptvarsym.vardef, exceptlocreg, exceptvarsym.localloc.reference);
  661. end;
  662. { in the case that another exception is risen
  663. we've to destroy the old one, so create a new
  664. exception frame for the catch-handler }
  665. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  666. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,tek_except,doobjectdestroyandreraisestate);
  667. oldBreakLabel:=nil;
  668. oldContinueLabel:=nil;
  669. if assigned(right) then
  670. begin
  671. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  672. current_asmdata.getjumplabel(exitonlabel);
  673. current_procinfo.CurrExitLabel:=exitonlabel;
  674. if assigned(current_procinfo.CurrBreakLabel) then
  675. begin
  676. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  677. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  678. current_asmdata.getjumplabel(breakonlabel);
  679. current_asmdata.getjumplabel(continueonlabel);
  680. current_procinfo.CurrContinueLabel:=continueonlabel;
  681. current_procinfo.CurrBreakLabel:=breakonlabel;
  682. end;
  683. secondpass(right);
  684. end;
  685. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraisestate);
  686. { clear some stuff }
  687. if assigned(exceptvarsym) then
  688. begin
  689. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  690. exceptvarsym.localloc.loc:=LOC_INVALID;
  691. end;
  692. cexceptionstatehandler.end_catch(current_asmdata.CurrAsmList);
  693. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  694. if assigned(right) then
  695. begin
  696. { special handling for control flow instructions }
  697. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  698. begin
  699. { the address and object pop does secondtryexcept }
  700. hlcg.a_label(current_asmdata.CurrAsmList,exitonlabel);
  701. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  702. end;
  703. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  704. begin
  705. { the address and object pop does secondtryexcept }
  706. hlcg.a_label(current_asmdata.CurrAsmList,breakonlabel);
  707. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  708. end;
  709. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  710. begin
  711. { the address and object pop does secondtryexcept }
  712. hlcg.a_label(current_asmdata.CurrAsmList,continueonlabel);
  713. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  714. end;
  715. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  716. if assigned(oldBreakLabel) then
  717. begin
  718. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  719. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  720. end;
  721. end;
  722. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  723. hlcg.a_label(current_asmdata.CurrAsmList,nextonlabel);
  724. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  725. { propagate exit/break/continue }
  726. flowcontrol:=doobjectdestroyandreraisestate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  727. { next on node }
  728. if assigned(left) then
  729. secondpass(left);
  730. end;
  731. {*****************************************************************************
  732. SecondTryFinally
  733. *****************************************************************************}
  734. { jump out of a finally block }
  735. procedure tcgtryfinallynode.emit_jump_out_of_try_finally_frame(list: TasmList; const reason: byte; const finallycodelabel: tasmlabel; var excepttemps: tcgexceptionstatehandler.texceptiontemps; framelabel: tasmlabel);
  736. begin
  737. hlcg.a_label(list,framelabel);
  738. hlcg.g_exception_reason_discard(list,osuinttype,excepttemps.reasonbuf);
  739. hlcg.g_exception_reason_save_const(list,osuinttype,reason,excepttemps.reasonbuf);
  740. hlcg.a_jmp_always(list,finallycodelabel);
  741. end;
  742. function tcgtryfinallynode.get_jump_out_of_try_finally_frame_label(const finallyexceptionstate: tcgexceptionstatehandler.texceptionstate): tasmlabel;
  743. begin
  744. current_asmdata.getjumplabel(result);
  745. end;
  746. procedure tcgtryfinallynode.handle_safecall_exception;
  747. var
  748. cgpara, resultpara: tcgpara;
  749. selfsym: tparavarsym;
  750. pd: tprocdef;
  751. safecallresult: tlocalvarsym;
  752. begin
  753. { call fpc_safecallhandler, passing self for methods of classes,
  754. nil otherwise. }
  755. pd:=search_system_proc('fpc_safecallhandler');
  756. cgpara.init;
  757. paramanager.getcgtempparaloc(current_asmdata.CurrAsmList,pd,1,cgpara);
  758. if is_class(current_procinfo.procdef.struct) then
  759. begin
  760. selfsym:=tparavarsym(current_procinfo.procdef.parast.Find('self'));
  761. if (selfsym=nil) or (selfsym.typ<>paravarsym) then
  762. InternalError(2011123101);
  763. hlcg.a_load_loc_cgpara(current_asmdata.CurrAsmList,selfsym.vardef,selfsym.localloc,cgpara);
  764. end
  765. else
  766. hlcg.a_load_const_cgpara(current_asmdata.CurrAsmList,voidpointertype,0,cgpara);
  767. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  768. resultpara:=hlcg.g_call_system_proc(current_asmdata.CurrAsmList,pd,[@cgpara],nil);
  769. cgpara.done;
  770. safecallresult:=tlocalvarsym(current_procinfo.procdef.localst.Find('safecallresult'));
  771. hlcg.gen_load_cgpara_loc(current_asmdata.CurrAsmList,resultpara.def,resultpara,safecallresult.localloc,false);
  772. resultpara.resetiftemp;
  773. end;
  774. procedure tcgtryfinallynode.pass_generate_code;
  775. var
  776. endfinallylabel,
  777. exitfinallylabel,
  778. continuefinallylabel,
  779. breakfinallylabel,
  780. oldCurrExitLabel,
  781. oldContinueLabel,
  782. oldBreakLabel,
  783. finallyNoExceptionLabel: tasmlabel;
  784. finallyexceptionstate: tcgexceptionstatehandler.texceptionstate;
  785. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  786. reasonreg : tregister;
  787. exceptframekind: tcgexceptionstatehandler.texceptframekind;
  788. tmplist: TAsmList;
  789. procedure handle_breakcontinueexit(const finallycode: tasmlabel; doreraise: boolean);
  790. begin
  791. { no exception happened, but maybe break/continue/exit }
  792. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  793. if fc_exit in finallyexceptionstate.newflowcontrol then
  794. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,2,reasonreg,oldCurrExitLabel);
  795. if fc_break in finallyexceptionstate.newflowcontrol then
  796. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,3,reasonreg,oldBreakLabel);
  797. if fc_continue in finallyexceptionstate.newflowcontrol then
  798. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,4,reasonreg,oldContinueLabel);
  799. if doreraise then
  800. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,tek_normalfinally)
  801. else
  802. hlcg.g_unreachable(current_asmdata.CurrAsmList);
  803. { redirect break/continue/exit to the label above, with the reasonbuf set appropriately }
  804. if fc_exit in finallyexceptionstate.newflowcontrol then
  805. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,2,finallycode,excepttemps,exitfinallylabel);
  806. if fc_break in finallyexceptionstate.newflowcontrol then
  807. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,3,finallycode,excepttemps,breakfinallylabel);
  808. if fc_continue in finallyexceptionstate.newflowcontrol then
  809. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,4,finallycode,excepttemps,continuefinallylabel);
  810. end;
  811. begin
  812. location_reset(location,LOC_VOID,OS_NO);
  813. oldBreakLabel:=nil;
  814. oldContinueLabel:=nil;
  815. continuefinallylabel:=nil;
  816. breakfinallylabel:=nil;
  817. if not implicitframe then
  818. exceptframekind:=tek_normalfinally
  819. else
  820. exceptframekind:=tek_implicitfinally;
  821. current_asmdata.getjumplabel(endfinallylabel);
  822. { call setjmp, and jump to finally label on non-zero result }
  823. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  824. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,exceptframekind,finallyexceptionstate);
  825. { the finally block must catch break, continue and exit }
  826. { statements }
  827. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  828. exitfinallylabel:=get_jump_out_of_try_finally_frame_label(finallyexceptionstate);
  829. current_procinfo.CurrExitLabel:=exitfinallylabel;
  830. if assigned(current_procinfo.CurrBreakLabel) then
  831. begin
  832. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  833. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  834. breakfinallylabel:=get_jump_out_of_try_finally_frame_label(finallyexceptionstate);
  835. continuefinallylabel:=get_jump_out_of_try_finally_frame_label(finallyexceptionstate);
  836. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  837. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  838. end;
  839. { try code }
  840. if assigned(left) then
  841. begin
  842. secondpass(left);
  843. if codegenerror then
  844. exit;
  845. end;
  846. { don't generate line info for internal cleanup }
  847. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  848. cexceptionstatehandler.end_try_block(current_asmdata.CurrAsmList,exceptframekind,excepttemps,finallyexceptionstate,finallyexceptionstate.finallycodelabel);
  849. if assigned(third) then
  850. begin
  851. tmplist:=TAsmList.create;
  852. { emit the except label already (to a temporary list) to ensure that any calls in the
  853. finally block refer to the outer exception frame rather than to the exception frame
  854. that emits this same finally code in case an exception does happen }
  855. cexceptionstatehandler.emit_except_label(tmplist,exceptframekind,finallyexceptionstate,excepttemps);
  856. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  857. current_asmdata.getjumplabel(finallyNoExceptionLabel);
  858. hlcg.a_label(current_asmdata.CurrAsmList,finallyNoExceptionLabel);
  859. if not implicitframe then
  860. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  861. secondpass(third);
  862. if codegenerror then
  863. exit;
  864. if not implicitframe then
  865. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  866. reasonreg:=hlcg.getintregister(current_asmdata.CurrAsmList,osuinttype);
  867. hlcg.g_exception_reason_load(current_asmdata.CurrAsmList,osuinttype,osuinttype,excepttemps.reasonbuf,reasonreg);
  868. handle_breakcontinueexit(finallyNoExceptionLabel,false);
  869. current_asmdata.CurrAsmList.concatList(tmplist);
  870. tmplist.free;
  871. end
  872. else
  873. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,exceptframekind,finallyexceptionstate,excepttemps);
  874. { just free the frame information }
  875. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,1,finallyexceptionstate.exceptionlabel,true);
  876. { end cleanup }
  877. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  878. { finally code (don't unconditionally set fc_inflowcontrol, since the
  879. finally code is unconditionally executed; we do have to filter out
  880. flags regarding break/contrinue/etc. because we have to give an
  881. error in case one of those is used in the finally-code }
  882. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions];
  883. secondpass(right);
  884. { goto is allowed if it stays inside the finally block,
  885. this is checked using the exception block number }
  886. if (flowcontrol-[fc_gotolabel])<>(finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol,fc_catching_exceptions]) then
  887. CGMessage(cg_e_control_flow_outside_finally);
  888. if codegenerror then
  889. exit;
  890. { don't generate line info for internal cleanup }
  891. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  892. { same level as before try, but this part is only executed if an exception occcurred
  893. -> always fc_in_flowcontrol }
  894. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_catching_exceptions];
  895. include(flowcontrol,fc_inflowcontrol);
  896. if not assigned(third) then
  897. begin
  898. { the value should now be in the exception handler }
  899. reasonreg:=hlcg.getintregister(current_asmdata.CurrAsmList,osuinttype);
  900. hlcg.g_exception_reason_load(current_asmdata.CurrAsmList,osuinttype,osuinttype,excepttemps.reasonbuf,reasonreg);
  901. if implicitframe then
  902. begin
  903. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  904. { finally code only needed to be executed on exception (-> in
  905. if-branch -> fc_inflowcontrol) }
  906. if current_procinfo.procdef.generate_safecall_wrapper then
  907. begin
  908. handle_safecall_exception;
  909. { we have to jump immediatly as we have to return the value of FPC_SAFECALL }
  910. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  911. end
  912. else
  913. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,exceptframekind);
  914. { we have to load 0 into the execepttemp, else the program thinks an exception happended }
  915. emit_jump_out_of_try_finally_frame(current_asmdata.CurrAsmList,0,finallyexceptionstate.exceptionlabel,excepttemps,exitfinallylabel);
  916. end
  917. else
  918. begin
  919. handle_breakcontinueexit(finallyexceptionstate.exceptionlabel,true);
  920. end;
  921. end
  922. else
  923. begin
  924. if implicitframe then
  925. begin
  926. if current_procinfo.procdef.generate_safecall_wrapper then
  927. handle_safecall_exception
  928. else
  929. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,exceptframekind);
  930. end
  931. else
  932. begin
  933. cexceptionstatehandler.handle_reraise(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate,exceptframekind);
  934. end;
  935. end;
  936. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  937. hlcg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  938. { end cleanup }
  939. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  940. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  941. if assigned(current_procinfo.CurrBreakLabel) then
  942. begin
  943. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  944. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  945. end;
  946. flowcontrol:=finallyexceptionstate.oldflowcontrol+(finallyexceptionstate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  947. end;
  948. function tcgraisenode.pass_1: tnode;
  949. begin
  950. if not(tf_use_psabieh in target_info.flags) or assigned(left) then
  951. result:=inherited
  952. else
  953. begin
  954. expectloc:=LOC_VOID;
  955. result:=nil;
  956. end;
  957. end;
  958. {$ifndef SkipABIEH}
  959. { has to be factored out as well }
  960. procedure tcgraisenode.pass_generate_code;
  961. var
  962. CurrentLandingPad, CurrentAction, ReRaiseLandingPad: TPSABIEHAction;
  963. psabiehprocinfo: tpsabiehprocinfo;
  964. begin
  965. if not(tf_use_psabieh in target_info.flags) then
  966. Internalerror(2019021701);
  967. location_reset(location,LOC_VOID,OS_NO);
  968. CurrentLandingPad:=nil;
  969. CurrentAction:=nil;
  970. ReRaiseLandingPad:=nil;
  971. psabiehprocinfo:=current_procinfo as tpsabiehprocinfo;
  972. { a reraise must raise the exception to the parent exception frame }
  973. if fc_catching_exceptions in flowcontrol then
  974. begin
  975. psabiehprocinfo.CreateNewPSABIEHCallsite(current_asmdata.CurrAsmList);
  976. CurrentLandingPad:=psabiehprocinfo.CurrentLandingPad;
  977. if psabiehprocinfo.PopLandingPad(CurrentLandingPad) then
  978. exclude(flowcontrol,fc_catching_exceptions);
  979. CurrentAction:=psabiehprocinfo.CurrentAction;
  980. psabiehprocinfo.FinalizeAndPopAction(CurrentAction);
  981. if not(fc_catching_exceptions in flowcontrol) then
  982. begin
  983. ReRaiseLandingPad:=psabiehprocinfo.NoAction;
  984. psabiehprocinfo.PushAction(ReRaiseLandingPad);
  985. psabiehprocinfo.PushLandingPad(ReRaiseLandingPad);
  986. end;
  987. end;
  988. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil).resetiftemp;
  989. if assigned(CurrentLandingPad) then
  990. begin
  991. psabiehprocinfo.CreateNewPSABIEHCallsite(current_asmdata.CurrAsmList);
  992. if not(fc_catching_exceptions in flowcontrol) then
  993. begin
  994. psabiehprocinfo.PopLandingPad(psabiehprocinfo.CurrentLandingPad);
  995. psabiehprocinfo.PopAction(ReRaiseLandingPad);
  996. end;
  997. psabiehprocinfo.PushAction(CurrentAction);
  998. psabiehprocinfo.PushLandingPad(CurrentLandingPad);
  999. include(flowcontrol,fc_catching_exceptions);
  1000. end;
  1001. end;
  1002. {$endif SkipABIEH}
  1003. begin
  1004. cwhilerepeatnode:=tcgwhilerepeatnode;
  1005. cifnode:=tcgifnode;
  1006. cfornode:=tcgfornode;
  1007. cexitnode:=tcgexitnode;
  1008. cbreaknode:=tcgbreaknode;
  1009. ccontinuenode:=tcgcontinuenode;
  1010. cgotonode:=tcggotonode;
  1011. clabelnode:=tcglabelnode;
  1012. craisenode:=tcgraisenode;
  1013. ctryexceptnode:=tcgtryexceptnode;
  1014. ctryfinallynode:=tcgtryfinallynode;
  1015. connode:=tcgonnode;
  1016. cexceptionstatehandler:=tcgexceptionstatehandler;
  1017. end.