ncgflw.pas 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for nodes that influence the flow which are
  4. the same for all (most?) processors
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgflw;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. aasmbase,aasmdata,nflw,
  24. pass_2,cgutils,ncgutil;
  25. type
  26. tcgwhilerepeatnode = class(twhilerepeatnode)
  27. usedregvars: tusedregvars;
  28. procedure pass_generate_code;override;
  29. procedure sync_regvars(checkusedregvars: boolean);
  30. end;
  31. tcgifnode = class(tifnode)
  32. procedure pass_generate_code;override;
  33. end;
  34. tcgfornode = class(tfornode)
  35. procedure pass_generate_code;override;
  36. end;
  37. tcgexitnode = class(texitnode)
  38. procedure pass_generate_code;override;
  39. end;
  40. tcgbreaknode = class(tbreaknode)
  41. procedure pass_generate_code;override;
  42. end;
  43. tcgcontinuenode = class(tcontinuenode)
  44. procedure pass_generate_code;override;
  45. end;
  46. tcggotonode = class(tgotonode)
  47. procedure pass_generate_code;override;
  48. end;
  49. tcglabelnode = class(tlabelnode)
  50. protected
  51. asmlabel : tasmlabel;
  52. public
  53. function getasmlabel : tasmlabel; virtual;
  54. procedure pass_generate_code;override;
  55. end;
  56. tcgraisenode = class(traisenode)
  57. end;
  58. { Utility class for exception handling state management that is used
  59. by tryexcept/tryfinally/on nodes (in a separate class so it can both
  60. be shared and overridden)
  61. Never instantiated. }
  62. tcgexceptionstatehandler = class
  63. type
  64. texceptiontemps=record
  65. jmpbuf,
  66. envbuf,
  67. reasonbuf : treference;
  68. end;
  69. texceptionstate = record
  70. exceptionlabel: TAsmLabel;
  71. oldflowcontrol,
  72. newflowcontrol: tflowcontrol;
  73. end;
  74. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); virtual;
  75. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); virtual;
  76. class procedure new_exception(list:TAsmList;const t:texceptiontemps; out exceptstate: texceptionstate); virtual;
  77. class procedure emit_except_label(list: TAsmList; var exceptstate: texceptionstate); virtual;
  78. class procedure free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean); virtual;
  79. class procedure cleanupobjectstack; virtual;
  80. class procedure handle_nested_exception(list:TAsmList;const t:texceptiontemps;var entrystate: texceptionstate); virtual;
  81. end;
  82. tcgexceptionstatehandlerclass = class of tcgexceptionstatehandler;
  83. tcgtryexceptnode = class(ttryexceptnode)
  84. procedure pass_generate_code;override;
  85. end;
  86. tcgtryfinallynode = class(ttryfinallynode)
  87. procedure handle_safecall_exception;
  88. procedure pass_generate_code;override;
  89. end;
  90. tcgonnode = class(tonnode)
  91. procedure pass_generate_code;override;
  92. end;
  93. var
  94. cexceptionstatehandler: tcgexceptionstatehandlerclass;
  95. implementation
  96. uses
  97. cutils,
  98. verbose,globals,systems,
  99. symconst,symdef,symsym,symtable,symtype,aasmtai,aasmcpu,defutil,
  100. procinfo,cgbase,parabase,
  101. fmodule,
  102. cpubase,
  103. tgobj,paramgr,
  104. cgobj,hlcgobj,nutils
  105. ;
  106. {*****************************************************************************
  107. Second_While_RepeatN
  108. *****************************************************************************}
  109. procedure tcgwhilerepeatnode.sync_regvars(checkusedregvars: boolean);
  110. begin
  111. if (cs_opt_regvar in current_settings.optimizerswitches) and
  112. not(pi_has_label in current_procinfo.flags) then
  113. begin
  114. if checkusedregvars then
  115. begin
  116. usedregvars.intregvars.init;
  117. usedregvars.addrregvars.init;
  118. usedregvars.fpuregvars.init;
  119. usedregvars.mmregvars.init;
  120. { we have to synchronise both the regvars used in the loop }
  121. { and the ones in the while/until condition }
  122. get_used_regvars(self,usedregvars);
  123. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  124. end
  125. else
  126. begin
  127. gen_sync_regvars(current_asmdata.CurrAsmList,usedregvars);
  128. usedregvars.intregvars.done;
  129. usedregvars.addrregvars.done;
  130. usedregvars.fpuregvars.done;
  131. usedregvars.mmregvars.done;
  132. end;
  133. end;
  134. end;
  135. procedure tcgwhilerepeatnode.pass_generate_code;
  136. var
  137. lcont,lbreak,lloop,
  138. oldclabel,oldblabel : tasmlabel;
  139. truelabel,falselabel : tasmlabel;
  140. oldflowcontrol : tflowcontrol;
  141. begin
  142. location_reset(location,LOC_VOID,OS_NO);
  143. current_asmdata.getjumplabel(lloop);
  144. current_asmdata.getjumplabel(lcont);
  145. current_asmdata.getjumplabel(lbreak);
  146. { arrange continue and breaklabels: }
  147. oldflowcontrol:=flowcontrol;
  148. oldclabel:=current_procinfo.CurrContinueLabel;
  149. oldblabel:=current_procinfo.CurrBreakLabel;
  150. include(flowcontrol,fc_inflowcontrol);
  151. exclude(flowcontrol,fc_unwind_loop);
  152. sync_regvars(true);
  153. {$ifdef OLDREGVARS}
  154. load_all_regvars(current_asmdata.CurrAsmList);
  155. {$endif OLDREGVARS}
  156. { handling code at the end as it is much more efficient, and makes
  157. while equal to repeat loop, only the end true/false is swapped (PFV) }
  158. if lnf_testatbegin in loopflags then
  159. hlcg.a_jmp_always(current_asmdata.CurrAsmList,lcont);
  160. if not(cs_opt_size in current_settings.optimizerswitches) then
  161. { align loop target }
  162. current_asmdata.CurrAsmList.concat(Tai_align.Create(current_settings.alignment.loopalign));
  163. hlcg.a_label(current_asmdata.CurrAsmList,lloop);
  164. current_procinfo.CurrContinueLabel:=lcont;
  165. current_procinfo.CurrBreakLabel:=lbreak;
  166. if assigned(right) then
  167. secondpass(right);
  168. {$ifdef OLDREGVARS}
  169. load_all_regvars(current_asmdata.CurrAsmList);
  170. {$endif OLDREGVARS}
  171. hlcg.a_label(current_asmdata.CurrAsmList,lcont);
  172. if lnf_checknegate in loopflags then
  173. begin
  174. truelabel:=lbreak;
  175. falselabel:=lloop;
  176. end
  177. else
  178. begin
  179. truelabel:=lloop;
  180. falselabel:=lbreak;
  181. end;
  182. secondpass(left);
  183. hlcg.maketojumpboollabels(current_asmdata.CurrAsmList,left,truelabel,falselabel);
  184. hlcg.a_label(current_asmdata.CurrAsmList,lbreak);
  185. sync_regvars(false);
  186. current_procinfo.CurrContinueLabel:=oldclabel;
  187. current_procinfo.CurrBreakLabel:=oldblabel;
  188. { a break/continue in a while/repeat block can't be seen outside }
  189. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
  190. end;
  191. {*****************************************************************************
  192. tcgIFNODE
  193. *****************************************************************************}
  194. procedure tcgifnode.pass_generate_code;
  195. var
  196. hl : tasmlabel;
  197. oldflowcontrol: tflowcontrol;
  198. (*
  199. org_regvar_loaded_other,
  200. then_regvar_loaded_other,
  201. else_regvar_loaded_other : regvarother_booleanarray;
  202. org_regvar_loaded_int,
  203. then_regvar_loaded_int,
  204. else_regvar_loaded_int : Tsuperregisterset;
  205. org_list,
  206. then_list,
  207. else_list : TAsmList;
  208. *)
  209. begin
  210. location_reset(location,LOC_VOID,OS_NO);
  211. hl:=nil;
  212. oldflowcontrol := flowcontrol;
  213. include(flowcontrol,fc_inflowcontrol);
  214. secondpass(left);
  215. (*
  216. { save regvars loaded in the beginning so that we can restore them }
  217. { when processing the else-block }
  218. if cs_opt_regvar in current_settings.optimizerswitches then
  219. begin
  220. org_list := current_asmdata.CurrAsmList;
  221. current_asmdata.CurrAsmList := TAsmList.create;
  222. end;
  223. *)
  224. hlcg.maketojumpbool(current_asmdata.CurrAsmList,left);
  225. (*
  226. if cs_opt_regvar in current_settings.optimizerswitches then
  227. begin
  228. org_regvar_loaded_int := rg.regvar_loaded_int;
  229. org_regvar_loaded_other := rg.regvar_loaded_other;
  230. end;
  231. *)
  232. if assigned(right) then
  233. begin
  234. hlcg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  235. secondpass(right);
  236. end;
  237. { save current asmlist (previous instructions + then-block) and }
  238. { loaded regvar state and create new clean ones }
  239. {
  240. if cs_opt_regvar in current_settings.optimizerswitches then
  241. begin
  242. then_regvar_loaded_int := rg.regvar_loaded_int;
  243. then_regvar_loaded_other := rg.regvar_loaded_other;
  244. rg.regvar_loaded_int := org_regvar_loaded_int;
  245. rg.regvar_loaded_other := org_regvar_loaded_other;
  246. then_list := current_asmdata.CurrAsmList;
  247. current_asmdata.CurrAsmList := TAsmList.create;
  248. end;
  249. }
  250. if assigned(t1) then
  251. begin
  252. if assigned(right) then
  253. begin
  254. current_asmdata.getjumplabel(hl);
  255. { do go back to if line !! }
  256. (*
  257. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  258. *)
  259. current_filepos:=current_asmdata.CurrAsmList.getlasttaifilepos^
  260. (*
  261. else
  262. current_filepos:=then_list.getlasttaifilepos^
  263. *)
  264. ;
  265. hlcg.a_jmp_always(current_asmdata.CurrAsmList,hl);
  266. end;
  267. hlcg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  268. secondpass(t1);
  269. (*
  270. { save current asmlist (previous instructions + else-block) }
  271. { and loaded regvar state and create a new clean list }
  272. if cs_opt_regvar in current_settings.optimizerswitches then
  273. begin
  274. { else_regvar_loaded_int := rg.regvar_loaded_int;
  275. else_regvar_loaded_other := rg.regvar_loaded_other;}
  276. else_list := current_asmdata.CurrAsmList;
  277. current_asmdata.CurrAsmList := TAsmList.create;
  278. end;
  279. *)
  280. if assigned(right) then
  281. hlcg.a_label(current_asmdata.CurrAsmList,hl);
  282. end
  283. else
  284. begin
  285. (*
  286. if cs_opt_regvar in current_settings.optimizerswitches then
  287. begin
  288. { else_regvar_loaded_int := rg.regvar_loaded_int;
  289. else_regvar_loaded_other := rg.regvar_loaded_other;}
  290. else_list := current_asmdata.CurrAsmList;
  291. current_asmdata.CurrAsmList := TAsmList.create;
  292. end;
  293. *)
  294. current_asmdata.CurrAsmList.concat(cai_align.create(current_settings.alignment.jumpalign));
  295. hlcg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  296. end;
  297. if not(assigned(right)) then
  298. begin
  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. if fc_no_direct_exit in flowcontrol then
  351. include(flowcontrol,fc_gotolabel);
  352. include(flowcontrol,fc_exit);
  353. if assigned(left) then
  354. secondpass(left);
  355. if (fc_unwind_exit in flowcontrol) then
  356. hlcg.g_local_unwind(current_asmdata.CurrAsmList,current_procinfo.CurrExitLabel)
  357. else
  358. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrExitLabel);
  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. end
  377. else
  378. CGMessage(cg_e_break_not_allowed);
  379. end;
  380. {*****************************************************************************
  381. SecondContinueN
  382. *****************************************************************************}
  383. procedure tcgcontinuenode.pass_generate_code;
  384. begin
  385. location_reset(location,LOC_VOID,OS_NO);
  386. include(flowcontrol,fc_continue);
  387. if current_procinfo.CurrContinueLabel<>nil then
  388. begin
  389. {$ifdef OLDREGVARS}
  390. load_all_regvars(current_asmdata.CurrAsmList);
  391. {$endif OLDREGVARS}
  392. if (fc_unwind_loop in flowcontrol) then
  393. hlcg.g_local_unwind(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel)
  394. else
  395. hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrContinueLabel)
  396. end
  397. else
  398. CGMessage(cg_e_continue_not_allowed);
  399. end;
  400. {*****************************************************************************
  401. SecondGoto
  402. *****************************************************************************}
  403. procedure tcggotonode.pass_generate_code;
  404. begin
  405. location_reset(location,LOC_VOID,OS_NO);
  406. include(flowcontrol,fc_gotolabel);
  407. {$ifdef OLDREGVARS}
  408. load_all_regvars(current_asmdata.CurrAsmList);
  409. {$endif OLDREGVARS}
  410. hlcg.a_jmp_always(current_asmdata.CurrAsmList,tcglabelnode(labelnode).getasmlabel)
  411. end;
  412. {*****************************************************************************
  413. SecondLabel
  414. *****************************************************************************}
  415. function tcglabelnode.getasmlabel : tasmlabel;
  416. begin
  417. if not(assigned(asmlabel)) then
  418. { labsym is not set in inlined procedures, but since assembler }
  419. { routines can't be inlined, that shouldn't matter }
  420. if assigned(labsym) and
  421. labsym.nonlocal then
  422. current_asmdata.getglobaljumplabel(asmlabel)
  423. else
  424. current_asmdata.getjumplabel(asmlabel);
  425. result:=asmlabel
  426. end;
  427. procedure tcglabelnode.pass_generate_code;
  428. begin
  429. location_reset(location,LOC_VOID,OS_NO);
  430. include(flowcontrol,fc_gotolabel);
  431. {$ifdef OLDREGVARS}
  432. load_all_regvars(current_asmdata.CurrAsmList);
  433. {$endif OLDREGVARS}
  434. hlcg.a_label(current_asmdata.CurrAsmList,getasmlabel);
  435. { Write also extra label if this label was referenced from
  436. assembler block }
  437. if assigned(labsym) and
  438. assigned(labsym.asmblocklabel) then
  439. hlcg.a_label(current_asmdata.CurrAsmList,labsym.asmblocklabel);
  440. secondpass(left);
  441. end;
  442. {*****************************************************************************
  443. tcgexceptionstatehandler
  444. *****************************************************************************}
  445. { Allocate the buffers for exception management and setjmp environment.
  446. Return a pointer to these buffers, send them to the utility routine
  447. so they are registered, and then call setjmp.
  448. Then compare the result of setjmp with 0, and if not equal
  449. to zero, then jump to exceptlabel.
  450. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  451. It is to note that this routine may be called *after* the stackframe of a
  452. routine has been called, therefore on machines where the stack cannot
  453. be modified, all temps should be allocated on the heap instead of the
  454. stack. }
  455. class procedure tcgexceptionstatehandler.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  456. begin
  457. tg.gethltemp(list,rec_exceptaddr,rec_exceptaddr.size,tt_persistent,t.envbuf);
  458. tg.gethltemp(list,rec_jmp_buf,rec_jmp_buf.size,tt_persistent,t.jmpbuf);
  459. tg.gethltemp(list,ossinttype,ossinttype.size,tt_persistent,t.reasonbuf);
  460. end;
  461. class procedure tcgexceptionstatehandler.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  462. begin
  463. tg.Ungettemp(list,t.jmpbuf);
  464. tg.ungettemp(list,t.envbuf);
  465. tg.ungettemp(list,t.reasonbuf);
  466. end;
  467. class procedure tcgexceptionstatehandler.new_exception(list:TAsmList;const t:texceptiontemps; out exceptstate: texceptionstate);
  468. var
  469. paraloc1, paraloc2, paraloc3, pushexceptres, setjmpres: tcgpara;
  470. pd: tprocdef;
  471. tmpresloc: tlocation;
  472. begin
  473. current_asmdata.getjumplabel(exceptstate.exceptionlabel);
  474. exceptstate.oldflowcontrol:=flowcontrol;
  475. paraloc1.init;
  476. paraloc2.init;
  477. paraloc3.init;
  478. { fpc_pushexceptaddr(exceptionframetype, setjmp_buffer, exception_address_chain_entry) }
  479. pd:=search_system_proc('fpc_pushexceptaddr');
  480. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  481. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,2,paraloc2);
  482. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,3,paraloc3);
  483. if pd.is_pushleftright then
  484. begin
  485. { type of exceptionframe }
  486. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  487. { setjmp buffer }
  488. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  489. { exception address chain entry }
  490. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  491. end
  492. else
  493. begin
  494. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  495. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  496. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  497. end;
  498. paramanager.freecgpara(list,paraloc3);
  499. paramanager.freecgpara(list,paraloc2);
  500. paramanager.freecgpara(list,paraloc1);
  501. { perform the fpc_pushexceptaddr call }
  502. pushexceptres:=hlcg.g_call_system_proc(list,pd,[@paraloc1,@paraloc2,@paraloc3],nil);
  503. paraloc1.done;
  504. paraloc2.done;
  505. paraloc3.done;
  506. { get the result }
  507. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(pushexceptres.def));
  508. tmpresloc.register:=hlcg.getaddressregister(list,pushexceptres.def);
  509. hlcg.gen_load_cgpara_loc(list,pushexceptres.def,pushexceptres,tmpresloc,true);
  510. pushexceptres.resetiftemp;
  511. { fpc_setjmp(result_of_pushexceptaddr_call) }
  512. pd:=search_system_proc('fpc_setjmp');
  513. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  514. hlcg.a_load_reg_cgpara(list,pushexceptres.def,tmpresloc.register,paraloc1);
  515. paramanager.freecgpara(list,paraloc1);
  516. { perform the fpc_setjmp call }
  517. setjmpres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  518. paraloc1.done;
  519. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(setjmpres.def));
  520. tmpresloc.register:=hlcg.getintregister(list,setjmpres.def);
  521. hlcg.gen_load_cgpara_loc(list,setjmpres.def,setjmpres,tmpresloc,true);
  522. hlcg.g_exception_reason_save(list,setjmpres.def,ossinttype,tmpresloc.register,t.reasonbuf);
  523. { if we get 0 here in the function result register, it means that we
  524. longjmp'd back here }
  525. hlcg.a_cmp_const_reg_label(list,setjmpres.def,OC_NE,0,tmpresloc.register,exceptstate.exceptionlabel);
  526. setjmpres.resetiftemp;
  527. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  528. end;
  529. class procedure tcgexceptionstatehandler.emit_except_label(list: TAsmList; var exceptstate: texceptionstate);
  530. begin
  531. hlcg.a_label(list,exceptstate.exceptionlabel);
  532. exceptstate.newflowcontrol:=flowcontrol;
  533. flowcontrol:=exceptstate.oldflowcontrol;
  534. end;
  535. class procedure tcgexceptionstatehandler.free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  536. var
  537. reasonreg: tregister;
  538. begin
  539. hlcg.g_call_system_proc(list,'fpc_popaddrstack',[],nil);
  540. if not onlyfree then
  541. begin
  542. reasonreg:=hlcg.getintregister(list,osuinttype);
  543. hlcg.g_exception_reason_load(list,osuinttype,osuinttype,t.reasonbuf,reasonreg);
  544. hlcg.a_cmp_const_reg_label(list,osuinttype,OC_EQ,a,reasonreg,endexceptlabel);
  545. end;
  546. end;
  547. { does the necessary things to clean up the object stack }
  548. { in the except block }
  549. class procedure tcgexceptionstatehandler.cleanupobjectstack;
  550. begin
  551. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_doneexception',[],nil);
  552. end;
  553. { generates code to be executed when another exeception is raised while
  554. control is inside except block }
  555. class procedure tcgexceptionstatehandler.handle_nested_exception(list:TAsmList;const t:texceptiontemps;var entrystate: texceptionstate);
  556. var
  557. exitlabel: tasmlabel;
  558. begin
  559. { don't generate line info for internal cleanup }
  560. list.concat(tai_marker.create(mark_NoLineInfoStart));
  561. current_asmdata.getjumplabel(exitlabel);
  562. emit_except_label(current_asmdata.CurrAsmList,entrystate);
  563. free_exception(list,t,0,exitlabel,false);
  564. { we don't need to save/restore registers here because reraise never }
  565. { returns }
  566. hlcg.g_call_system_proc(list,'fpc_raise_nested',[],nil);
  567. hlcg.a_label(list,exitlabel);
  568. cleanupobjectstack;
  569. end;
  570. {*****************************************************************************
  571. SecondTryExcept
  572. *****************************************************************************}
  573. var
  574. endexceptlabel : tasmlabel;
  575. procedure tcgtryexceptnode.pass_generate_code;
  576. var
  577. oldendexceptlabel,
  578. lastonlabel,
  579. exitexceptlabel,
  580. continueexceptlabel,
  581. breakexceptlabel,
  582. exittrylabel,
  583. continuetrylabel,
  584. breaktrylabel,
  585. oldCurrExitLabel,
  586. oldContinueLabel,
  587. oldBreakLabel : tasmlabel;
  588. destroytemps,
  589. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  590. trystate,doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  591. label
  592. errorexit;
  593. begin
  594. location_reset(location,LOC_VOID,OS_NO);
  595. continuetrylabel:=nil;
  596. breaktrylabel:=nil;
  597. continueexceptlabel:=nil;
  598. breakexceptlabel:=nil;
  599. doobjectdestroyandreraisestate:=Default(tcgexceptionstatehandler.texceptionstate);
  600. { this can be called recursivly }
  601. oldBreakLabel:=nil;
  602. oldContinueLabel:=nil;
  603. oldendexceptlabel:=endexceptlabel;
  604. { save the old labels for control flow statements }
  605. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  606. if assigned(current_procinfo.CurrBreakLabel) then
  607. begin
  608. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  609. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  610. end;
  611. { get new labels for the control flow statements }
  612. current_asmdata.getjumplabel(exittrylabel);
  613. current_asmdata.getjumplabel(exitexceptlabel);
  614. if assigned(current_procinfo.CurrBreakLabel) then
  615. begin
  616. current_asmdata.getjumplabel(breaktrylabel);
  617. current_asmdata.getjumplabel(continuetrylabel);
  618. current_asmdata.getjumplabel(breakexceptlabel);
  619. current_asmdata.getjumplabel(continueexceptlabel);
  620. end;
  621. current_asmdata.getjumplabel(endexceptlabel);
  622. current_asmdata.getjumplabel(lastonlabel);
  623. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  624. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,trystate);
  625. { try block }
  626. { set control flow labels for the try block }
  627. current_procinfo.CurrExitLabel:=exittrylabel;
  628. if assigned(oldBreakLabel) then
  629. begin
  630. current_procinfo.CurrContinueLabel:=continuetrylabel;
  631. current_procinfo.CurrBreakLabel:=breaktrylabel;
  632. end;
  633. secondpass(left);
  634. if codegenerror then
  635. goto errorexit;
  636. { don't generate line info for internal cleanup }
  637. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  638. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,trystate);
  639. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList, excepttemps, 0, endexceptlabel, false);
  640. { end cleanup }
  641. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  642. { set control flow labels for the except block }
  643. { and the on statements }
  644. current_procinfo.CurrExitLabel:=exitexceptlabel;
  645. if assigned(oldBreakLabel) then
  646. begin
  647. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  648. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  649. end;
  650. flowcontrol:=[fc_inflowcontrol];
  651. { on statements }
  652. if assigned(right) then
  653. secondpass(right);
  654. { don't generate line info for internal cleanup }
  655. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  656. hlcg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  657. { default handling except handling }
  658. if assigned(t1) then
  659. begin
  660. { FPC_CATCHES with 'default handler' flag (=-1) need no longer be called,
  661. it doesn't change any state and its return value is ignored (Sergei)
  662. }
  663. { the destruction of the exception object must be also }
  664. { guarded by an exception frame, but it can be omitted }
  665. { if there's no user code in 'except' block }
  666. if not (has_no_code(t1)) then
  667. begin
  668. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  669. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraisestate);
  670. { the flowcontrol from the default except-block must be merged
  671. with the flowcontrol flags potentially set by the
  672. on-statements handled above (secondpass(right)), as they are
  673. at the same program level }
  674. flowcontrol:=
  675. flowcontrol+
  676. doobjectdestroyandreraisestate.oldflowcontrol;
  677. { except block needs line info }
  678. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  679. secondpass(t1);
  680. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraisestate);
  681. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  682. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  683. end
  684. else
  685. begin
  686. doobjectdestroyandreraisestate.newflowcontrol:=flowcontrol;
  687. cexceptionstatehandler.cleanupobjectstack;
  688. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  689. end;
  690. end
  691. else
  692. begin
  693. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil);
  694. doobjectdestroyandreraisestate.newflowcontrol:=flowcontrol;
  695. end;
  696. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  697. begin
  698. { do some magic for exit in the try block }
  699. hlcg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  700. { we must also destroy the address frame which guards }
  701. { exception object }
  702. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  703. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  704. cexceptionstatehandler.cleanupobjectstack;
  705. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  706. end;
  707. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  708. begin
  709. hlcg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  710. { we must also destroy the address frame which guards }
  711. { exception object }
  712. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  713. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  714. cexceptionstatehandler.cleanupobjectstack;
  715. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  716. end;
  717. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  718. begin
  719. hlcg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  720. { we must also destroy the address frame which guards }
  721. { exception object }
  722. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  723. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  724. cexceptionstatehandler.cleanupobjectstack;
  725. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  726. end;
  727. if fc_exit in trystate.newflowcontrol then
  728. begin
  729. { do some magic for exit in the try block }
  730. hlcg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  731. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  732. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  733. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  734. end;
  735. if fc_break in trystate.newflowcontrol then
  736. begin
  737. hlcg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  738. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  739. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  740. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  741. end;
  742. if fc_continue in trystate.newflowcontrol then
  743. begin
  744. hlcg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  745. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  746. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  747. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  748. end;
  749. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  750. hlcg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  751. { end cleanup }
  752. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  753. errorexit:
  754. { restore all saved labels }
  755. endexceptlabel:=oldendexceptlabel;
  756. { restore the control flow labels }
  757. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  758. if assigned(oldBreakLabel) then
  759. begin
  760. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  761. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  762. end;
  763. { return all used control flow statements }
  764. flowcontrol:=trystate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol +
  765. trystate.newflowcontrol - [fc_inflowcontrol,fc_catching_exceptions]);
  766. end;
  767. procedure tcgonnode.pass_generate_code;
  768. var
  769. nextonlabel,
  770. exitonlabel,
  771. continueonlabel,
  772. breakonlabel,
  773. oldCurrExitLabel,
  774. oldContinueLabel,
  775. oldBreakLabel : tasmlabel;
  776. doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  777. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  778. href2: treference;
  779. paraloc1 : tcgpara;
  780. exceptvarsym : tlocalvarsym;
  781. pd : tprocdef;
  782. fpc_catches_res: TCGPara;
  783. fpc_catches_resloc: tlocation;
  784. otherunit,
  785. indirect : boolean;
  786. begin
  787. paraloc1.init;
  788. location_reset(location,LOC_VOID,OS_NO);
  789. oldCurrExitLabel:=nil;
  790. continueonlabel:=nil;
  791. breakonlabel:=nil;
  792. exitonlabel:=nil;
  793. current_asmdata.getjumplabel(nextonlabel);
  794. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  795. indirect:=(tf_supports_packages in target_info.flags) and
  796. (target_info.system in systems_indirect_var_imports) and
  797. (cs_imported_data in current_settings.localswitches) and
  798. otherunit;
  799. { send the vmt parameter }
  800. pd:=search_system_proc('fpc_catches');
  801. reference_reset_symbol(href2,current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname,AT_DATA,indirect),0,sizeof(pint),[]);
  802. if otherunit then
  803. current_module.add_extern_asmsym(excepttype.vmt_mangledname,AB_EXTERNAL,AT_DATA);
  804. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  805. hlcg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,excepttype.vmt_def,href2,paraloc1);
  806. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  807. fpc_catches_res:=hlcg.g_call_system_proc(current_asmdata.CurrAsmList,pd,[@paraloc1],nil);
  808. location_reset(fpc_catches_resloc,LOC_REGISTER,def_cgsize(fpc_catches_res.def));
  809. fpc_catches_resloc.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,fpc_catches_res.def);
  810. hlcg.gen_load_cgpara_loc(current_asmdata.CurrAsmList,fpc_catches_res.def,fpc_catches_res,fpc_catches_resloc,true);
  811. { is it this catch? No. go to next onlabel }
  812. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,fpc_catches_res.def,OC_EQ,0,fpc_catches_resloc.register,nextonlabel);
  813. { Retrieve exception variable }
  814. if assigned(excepTSymtable) then
  815. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  816. else
  817. internalerror(2011020401);
  818. if assigned(exceptvarsym) then
  819. begin
  820. location_reset_ref(exceptvarsym.localloc,LOC_REFERENCE,def_cgsize(voidpointertype),voidpointertype.alignment,[]);
  821. tg.GetLocal(current_asmdata.CurrAsmList,exceptvarsym.vardef.size,exceptvarsym.vardef,exceptvarsym.localloc.reference);
  822. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,fpc_catches_res.def,exceptvarsym.vardef,fpc_catches_resloc.register,exceptvarsym.localloc.reference);
  823. end;
  824. { in the case that another exception is risen
  825. we've to destroy the old one:
  826. call setjmp, and jump to finally label on non-zero result }
  827. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  828. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraisestate);
  829. oldBreakLabel:=nil;
  830. oldContinueLabel:=nil;
  831. if assigned(right) then
  832. begin
  833. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  834. current_asmdata.getjumplabel(exitonlabel);
  835. current_procinfo.CurrExitLabel:=exitonlabel;
  836. if assigned(current_procinfo.CurrBreakLabel) then
  837. begin
  838. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  839. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  840. current_asmdata.getjumplabel(breakonlabel);
  841. current_asmdata.getjumplabel(continueonlabel);
  842. current_procinfo.CurrContinueLabel:=continueonlabel;
  843. current_procinfo.CurrBreakLabel:=breakonlabel;
  844. end;
  845. secondpass(right);
  846. end;
  847. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraisestate);
  848. { clear some stuff }
  849. if assigned(exceptvarsym) then
  850. begin
  851. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  852. exceptvarsym.localloc.loc:=LOC_INVALID;
  853. end;
  854. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  855. if assigned(right) then
  856. begin
  857. { special handling for control flow instructions }
  858. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  859. begin
  860. { the address and object pop does secondtryexcept }
  861. hlcg.a_label(current_asmdata.CurrAsmList,exitonlabel);
  862. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  863. end;
  864. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  865. begin
  866. { the address and object pop does secondtryexcept }
  867. hlcg.a_label(current_asmdata.CurrAsmList,breakonlabel);
  868. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  869. end;
  870. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  871. begin
  872. { the address and object pop does secondtryexcept }
  873. hlcg.a_label(current_asmdata.CurrAsmList,continueonlabel);
  874. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  875. end;
  876. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  877. if assigned(oldBreakLabel) then
  878. begin
  879. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  880. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  881. end;
  882. end;
  883. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  884. hlcg.a_label(current_asmdata.CurrAsmList,nextonlabel);
  885. flowcontrol:=doobjectdestroyandreraisestate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  886. paraloc1.done;
  887. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  888. { next on node }
  889. if assigned(left) then
  890. secondpass(left);
  891. end;
  892. {*****************************************************************************
  893. SecondTryFinally
  894. *****************************************************************************}
  895. procedure tcgtryfinallynode.handle_safecall_exception;
  896. var
  897. cgpara: tcgpara;
  898. selfsym: tparavarsym;
  899. pd: tprocdef;
  900. begin
  901. { call fpc_safecallhandler, passing self for methods of classes,
  902. nil otherwise. }
  903. pd:=search_system_proc('fpc_safecallhandler');
  904. cgpara.init;
  905. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,cgpara);
  906. if is_class(current_procinfo.procdef.struct) then
  907. begin
  908. selfsym:=tparavarsym(current_procinfo.procdef.parast.Find('self'));
  909. if (selfsym=nil) or (selfsym.typ<>paravarsym) then
  910. InternalError(2011123101);
  911. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,selfsym.localloc,cgpara);
  912. end
  913. else
  914. cg.a_load_const_cgpara(current_asmdata.CurrAsmList,OS_ADDR,0,cgpara);
  915. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  916. cgpara.done;
  917. cg.g_call(current_asmdata.CurrAsmList,'FPC_SAFECALLHANDLER');
  918. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_FUNCTION_RESULT_REG, NR_FUNCTION_RETURN_REG);
  919. end;
  920. procedure tcgtryfinallynode.pass_generate_code;
  921. var
  922. endfinallylabel,
  923. exitfinallylabel,
  924. continuefinallylabel,
  925. breakfinallylabel,
  926. oldCurrExitLabel,
  927. oldContinueLabel,
  928. oldBreakLabel : tasmlabel;
  929. finallyexceptionstate: tcgexceptionstatehandler.texceptionstate;
  930. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  931. reasonreg : tregister;
  932. begin
  933. location_reset(location,LOC_VOID,OS_NO);
  934. oldBreakLabel:=nil;
  935. oldContinueLabel:=nil;
  936. continuefinallylabel:=nil;
  937. breakfinallylabel:=nil;
  938. current_asmdata.getjumplabel(endfinallylabel);
  939. { call setjmp, and jump to finally label on non-zero result }
  940. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  941. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate);
  942. { the finally block must catch break, continue and exit }
  943. { statements }
  944. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  945. if implicitframe then
  946. exitfinallylabel:=finallyexceptionstate.exceptionlabel
  947. else
  948. current_asmdata.getjumplabel(exitfinallylabel);
  949. current_procinfo.CurrExitLabel:=exitfinallylabel;
  950. if assigned(current_procinfo.CurrBreakLabel) then
  951. begin
  952. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  953. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  954. if implicitframe then
  955. begin
  956. breakfinallylabel:=finallyexceptionstate.exceptionlabel;
  957. continuefinallylabel:=finallyexceptionstate.exceptionlabel;
  958. end
  959. else
  960. begin
  961. current_asmdata.getjumplabel(breakfinallylabel);
  962. current_asmdata.getjumplabel(continuefinallylabel);
  963. end;
  964. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  965. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  966. end;
  967. { try code }
  968. if assigned(left) then
  969. begin
  970. secondpass(left);
  971. if codegenerror then
  972. exit;
  973. end;
  974. { don't generate line info for internal cleanup }
  975. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  976. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,finallyexceptionstate);
  977. { just free the frame information }
  978. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList,excepttemps,1,finallyexceptionstate.exceptionlabel,true);
  979. { end cleanup }
  980. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  981. { finally code (don't unconditionally set fc_inflowcontrol, since the
  982. finally code is unconditionally executed; we do have to filter out
  983. flags regarding break/contrinue/etc. because we have to give an
  984. error in case one of those is used in the finally-code }
  985. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol];
  986. secondpass(right);
  987. { goto is allowed if it stays inside the finally block,
  988. this is checked using the exception block number }
  989. if (flowcontrol-[fc_gotolabel])<>(finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol]) then
  990. CGMessage(cg_e_control_flow_outside_finally);
  991. if codegenerror then
  992. exit;
  993. { don't generate line info for internal cleanup }
  994. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  995. { the value should now be in the exception handler }
  996. reasonreg:=hlcg.getintregister(current_asmdata.CurrAsmList,osuinttype);
  997. hlcg.g_exception_reason_load(current_asmdata.CurrAsmList,osuinttype,osuinttype,excepttemps.reasonbuf,reasonreg);
  998. if implicitframe then
  999. begin
  1000. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  1001. { finally code only needed to be executed on exception (-> in
  1002. if-branch -> fc_inflowcontrol) }
  1003. flowcontrol:=[fc_inflowcontrol];
  1004. if (tf_safecall_exceptions in target_info.flags) and
  1005. (current_procinfo.procdef.proccalloption=pocall_safecall) then
  1006. handle_safecall_exception
  1007. else
  1008. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil);
  1009. end
  1010. else
  1011. begin
  1012. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  1013. if fc_exit in finallyexceptionstate.newflowcontrol then
  1014. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,2,reasonreg,oldCurrExitLabel);
  1015. if fc_break in finallyexceptionstate.newflowcontrol then
  1016. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,3,reasonreg,oldBreakLabel);
  1017. if fc_continue in finallyexceptionstate.newflowcontrol then
  1018. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,4,reasonreg,oldContinueLabel);
  1019. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil);
  1020. { do some magic for exit,break,continue in the try block }
  1021. if fc_exit in finallyexceptionstate.newflowcontrol then
  1022. begin
  1023. hlcg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  1024. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  1025. hlcg.g_exception_reason_save_const(current_asmdata.CurrAsmList,osuinttype,2,excepttemps.reasonbuf);
  1026. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallyexceptionstate.exceptionlabel);
  1027. end;
  1028. if fc_break in finallyexceptionstate.newflowcontrol then
  1029. begin
  1030. hlcg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  1031. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  1032. hlcg.g_exception_reason_save_const(current_asmdata.CurrAsmList,osuinttype,3,excepttemps.reasonbuf);
  1033. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallyexceptionstate.exceptionlabel);
  1034. end;
  1035. if fc_continue in finallyexceptionstate.newflowcontrol then
  1036. begin
  1037. hlcg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  1038. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  1039. hlcg.g_exception_reason_save_const(current_asmdata.CurrAsmList,osuinttype,4,excepttemps.reasonbuf);
  1040. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallyexceptionstate.exceptionlabel);
  1041. end;
  1042. end;
  1043. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1044. hlcg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  1045. { end cleanup }
  1046. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  1047. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1048. if assigned(current_procinfo.CurrBreakLabel) then
  1049. begin
  1050. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1051. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1052. end;
  1053. flowcontrol:=finallyexceptionstate.oldflowcontrol+(finallyexceptionstate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  1054. end;
  1055. begin
  1056. cwhilerepeatnode:=tcgwhilerepeatnode;
  1057. cifnode:=tcgifnode;
  1058. cfornode:=tcgfornode;
  1059. cexitnode:=tcgexitnode;
  1060. cbreaknode:=tcgbreaknode;
  1061. ccontinuenode:=tcgcontinuenode;
  1062. cgotonode:=tcggotonode;
  1063. clabelnode:=tcglabelnode;
  1064. craisenode:=tcgraisenode;
  1065. ctryexceptnode:=tcgtryexceptnode;
  1066. ctryfinallynode:=tcgtryfinallynode;
  1067. connode:=tcgonnode;
  1068. cexceptionstatehandler:=tcgexceptionstatehandler;
  1069. end.