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,node
  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. if not (nf_internal in flags) then
  431. include(flowcontrol,fc_gotolabel);
  432. {$ifdef OLDREGVARS}
  433. load_all_regvars(current_asmdata.CurrAsmList);
  434. {$endif OLDREGVARS}
  435. hlcg.a_label(current_asmdata.CurrAsmList,getasmlabel);
  436. { Write also extra label if this label was referenced from
  437. assembler block }
  438. if assigned(labsym) and
  439. assigned(labsym.asmblocklabel) then
  440. hlcg.a_label(current_asmdata.CurrAsmList,labsym.asmblocklabel);
  441. secondpass(left);
  442. end;
  443. {*****************************************************************************
  444. tcgexceptionstatehandler
  445. *****************************************************************************}
  446. { Allocate the buffers for exception management and setjmp environment.
  447. Return a pointer to these buffers, send them to the utility routine
  448. so they are registered, and then call setjmp.
  449. Then compare the result of setjmp with 0, and if not equal
  450. to zero, then jump to exceptlabel.
  451. Also store the result of setjmp to a temporary space by calling g_save_exception_reason
  452. It is to note that this routine may be called *after* the stackframe of a
  453. routine has been called, therefore on machines where the stack cannot
  454. be modified, all temps should be allocated on the heap instead of the
  455. stack. }
  456. class procedure tcgexceptionstatehandler.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  457. begin
  458. tg.gethltemp(list,rec_exceptaddr,rec_exceptaddr.size,tt_persistent,t.envbuf);
  459. tg.gethltemp(list,rec_jmp_buf,rec_jmp_buf.size,tt_persistent,t.jmpbuf);
  460. tg.gethltemp(list,ossinttype,ossinttype.size,tt_persistent,t.reasonbuf);
  461. end;
  462. class procedure tcgexceptionstatehandler.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  463. begin
  464. tg.Ungettemp(list,t.jmpbuf);
  465. tg.ungettemp(list,t.envbuf);
  466. tg.ungettemp(list,t.reasonbuf);
  467. end;
  468. class procedure tcgexceptionstatehandler.new_exception(list:TAsmList;const t:texceptiontemps; out exceptstate: texceptionstate);
  469. var
  470. paraloc1, paraloc2, paraloc3, pushexceptres, setjmpres: tcgpara;
  471. pd: tprocdef;
  472. tmpresloc: tlocation;
  473. begin
  474. current_asmdata.getjumplabel(exceptstate.exceptionlabel);
  475. exceptstate.oldflowcontrol:=flowcontrol;
  476. paraloc1.init;
  477. paraloc2.init;
  478. paraloc3.init;
  479. { fpc_pushexceptaddr(exceptionframetype, setjmp_buffer, exception_address_chain_entry) }
  480. pd:=search_system_proc('fpc_pushexceptaddr');
  481. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  482. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,2,paraloc2);
  483. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,3,paraloc3);
  484. if pd.is_pushleftright then
  485. begin
  486. { type of exceptionframe }
  487. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  488. { setjmp buffer }
  489. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  490. { exception address chain entry }
  491. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  492. end
  493. else
  494. begin
  495. hlcg.a_loadaddr_ref_cgpara(list,rec_exceptaddr,t.envbuf,paraloc3);
  496. hlcg.a_loadaddr_ref_cgpara(list,rec_jmp_buf,t.jmpbuf,paraloc2);
  497. hlcg.a_load_const_cgpara(list,paraloc1.def,1,paraloc1);
  498. end;
  499. paramanager.freecgpara(list,paraloc3);
  500. paramanager.freecgpara(list,paraloc2);
  501. paramanager.freecgpara(list,paraloc1);
  502. { perform the fpc_pushexceptaddr call }
  503. pushexceptres:=hlcg.g_call_system_proc(list,pd,[@paraloc1,@paraloc2,@paraloc3],nil);
  504. paraloc1.done;
  505. paraloc2.done;
  506. paraloc3.done;
  507. { get the result }
  508. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(pushexceptres.def));
  509. tmpresloc.register:=hlcg.getaddressregister(list,pushexceptres.def);
  510. hlcg.gen_load_cgpara_loc(list,pushexceptres.def,pushexceptres,tmpresloc,true);
  511. pushexceptres.resetiftemp;
  512. { fpc_setjmp(result_of_pushexceptaddr_call) }
  513. pd:=search_system_proc('fpc_setjmp');
  514. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  515. hlcg.a_load_reg_cgpara(list,pushexceptres.def,tmpresloc.register,paraloc1);
  516. paramanager.freecgpara(list,paraloc1);
  517. { perform the fpc_setjmp call }
  518. setjmpres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  519. paraloc1.done;
  520. location_reset(tmpresloc,LOC_REGISTER,def_cgsize(setjmpres.def));
  521. tmpresloc.register:=hlcg.getintregister(list,setjmpres.def);
  522. hlcg.gen_load_cgpara_loc(list,setjmpres.def,setjmpres,tmpresloc,true);
  523. hlcg.g_exception_reason_save(list,setjmpres.def,ossinttype,tmpresloc.register,t.reasonbuf);
  524. { if we get 0 here in the function result register, it means that we
  525. longjmp'd back here }
  526. hlcg.a_cmp_const_reg_label(list,setjmpres.def,OC_NE,0,tmpresloc.register,exceptstate.exceptionlabel);
  527. setjmpres.resetiftemp;
  528. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  529. end;
  530. class procedure tcgexceptionstatehandler.emit_except_label(list: TAsmList; var exceptstate: texceptionstate);
  531. begin
  532. hlcg.a_label(list,exceptstate.exceptionlabel);
  533. exceptstate.newflowcontrol:=flowcontrol;
  534. flowcontrol:=exceptstate.oldflowcontrol;
  535. end;
  536. class procedure tcgexceptionstatehandler.free_exception(list:TAsmList;const t:texceptiontemps;a:aint;endexceptlabel:tasmlabel;onlyfree:boolean);
  537. var
  538. reasonreg: tregister;
  539. begin
  540. hlcg.g_call_system_proc(list,'fpc_popaddrstack',[],nil);
  541. if not onlyfree then
  542. begin
  543. reasonreg:=hlcg.getintregister(list,osuinttype);
  544. hlcg.g_exception_reason_load(list,osuinttype,osuinttype,t.reasonbuf,reasonreg);
  545. hlcg.a_cmp_const_reg_label(list,osuinttype,OC_EQ,a,reasonreg,endexceptlabel);
  546. end;
  547. end;
  548. { does the necessary things to clean up the object stack }
  549. { in the except block }
  550. class procedure tcgexceptionstatehandler.cleanupobjectstack;
  551. begin
  552. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_doneexception',[],nil);
  553. end;
  554. { generates code to be executed when another exeception is raised while
  555. control is inside except block }
  556. class procedure tcgexceptionstatehandler.handle_nested_exception(list:TAsmList;const t:texceptiontemps;var entrystate: texceptionstate);
  557. var
  558. exitlabel: tasmlabel;
  559. begin
  560. { don't generate line info for internal cleanup }
  561. list.concat(tai_marker.create(mark_NoLineInfoStart));
  562. current_asmdata.getjumplabel(exitlabel);
  563. emit_except_label(current_asmdata.CurrAsmList,entrystate);
  564. free_exception(list,t,0,exitlabel,false);
  565. { we don't need to save/restore registers here because reraise never }
  566. { returns }
  567. hlcg.g_call_system_proc(list,'fpc_raise_nested',[],nil);
  568. hlcg.a_label(list,exitlabel);
  569. cleanupobjectstack;
  570. end;
  571. {*****************************************************************************
  572. SecondTryExcept
  573. *****************************************************************************}
  574. var
  575. endexceptlabel : tasmlabel;
  576. procedure tcgtryexceptnode.pass_generate_code;
  577. var
  578. oldendexceptlabel,
  579. lastonlabel,
  580. exitexceptlabel,
  581. continueexceptlabel,
  582. breakexceptlabel,
  583. exittrylabel,
  584. continuetrylabel,
  585. breaktrylabel,
  586. oldCurrExitLabel,
  587. oldContinueLabel,
  588. oldBreakLabel : tasmlabel;
  589. destroytemps,
  590. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  591. trystate,doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  592. label
  593. errorexit;
  594. begin
  595. location_reset(location,LOC_VOID,OS_NO);
  596. continuetrylabel:=nil;
  597. breaktrylabel:=nil;
  598. continueexceptlabel:=nil;
  599. breakexceptlabel:=nil;
  600. doobjectdestroyandreraisestate:=Default(tcgexceptionstatehandler.texceptionstate);
  601. { this can be called recursivly }
  602. oldBreakLabel:=nil;
  603. oldContinueLabel:=nil;
  604. oldendexceptlabel:=endexceptlabel;
  605. { save the old labels for control flow statements }
  606. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  607. if assigned(current_procinfo.CurrBreakLabel) then
  608. begin
  609. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  610. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  611. end;
  612. { get new labels for the control flow statements }
  613. current_asmdata.getjumplabel(exittrylabel);
  614. current_asmdata.getjumplabel(exitexceptlabel);
  615. if assigned(current_procinfo.CurrBreakLabel) then
  616. begin
  617. current_asmdata.getjumplabel(breaktrylabel);
  618. current_asmdata.getjumplabel(continuetrylabel);
  619. current_asmdata.getjumplabel(breakexceptlabel);
  620. current_asmdata.getjumplabel(continueexceptlabel);
  621. end;
  622. current_asmdata.getjumplabel(endexceptlabel);
  623. current_asmdata.getjumplabel(lastonlabel);
  624. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  625. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,trystate);
  626. { try block }
  627. { set control flow labels for the try block }
  628. current_procinfo.CurrExitLabel:=exittrylabel;
  629. if assigned(oldBreakLabel) then
  630. begin
  631. current_procinfo.CurrContinueLabel:=continuetrylabel;
  632. current_procinfo.CurrBreakLabel:=breaktrylabel;
  633. end;
  634. secondpass(left);
  635. if codegenerror then
  636. goto errorexit;
  637. { don't generate line info for internal cleanup }
  638. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  639. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,trystate);
  640. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList, excepttemps, 0, endexceptlabel, false);
  641. { end cleanup }
  642. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  643. { set control flow labels for the except block }
  644. { and the on statements }
  645. current_procinfo.CurrExitLabel:=exitexceptlabel;
  646. if assigned(oldBreakLabel) then
  647. begin
  648. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  649. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  650. end;
  651. flowcontrol:=[fc_inflowcontrol];
  652. { on statements }
  653. if assigned(right) then
  654. secondpass(right);
  655. { don't generate line info for internal cleanup }
  656. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  657. hlcg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  658. { default handling except handling }
  659. if assigned(t1) then
  660. begin
  661. { FPC_CATCHES with 'default handler' flag (=-1) need no longer be called,
  662. it doesn't change any state and its return value is ignored (Sergei)
  663. }
  664. { the destruction of the exception object must be also }
  665. { guarded by an exception frame, but it can be omitted }
  666. { if there's no user code in 'except' block }
  667. if not (has_no_code(t1)) then
  668. begin
  669. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  670. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraisestate);
  671. { the flowcontrol from the default except-block must be merged
  672. with the flowcontrol flags potentially set by the
  673. on-statements handled above (secondpass(right)), as they are
  674. at the same program level }
  675. flowcontrol:=
  676. flowcontrol+
  677. doobjectdestroyandreraisestate.oldflowcontrol;
  678. { except block needs line info }
  679. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  680. secondpass(t1);
  681. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,destroytemps,doobjectdestroyandreraisestate);
  682. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,destroytemps);
  683. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  684. end
  685. else
  686. begin
  687. doobjectdestroyandreraisestate.newflowcontrol:=flowcontrol;
  688. cexceptionstatehandler.cleanupobjectstack;
  689. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  690. end;
  691. end
  692. else
  693. begin
  694. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil);
  695. doobjectdestroyandreraisestate.newflowcontrol:=flowcontrol;
  696. end;
  697. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  698. begin
  699. { do some magic for exit in the try block }
  700. hlcg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  701. { we must also destroy the address frame which guards }
  702. { exception object }
  703. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  704. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  705. cexceptionstatehandler.cleanupobjectstack;
  706. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  707. end;
  708. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  709. begin
  710. hlcg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  711. { we must also destroy the address frame which guards }
  712. { exception object }
  713. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  714. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  715. cexceptionstatehandler.cleanupobjectstack;
  716. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  717. end;
  718. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  719. begin
  720. hlcg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  721. { we must also destroy the address frame which guards }
  722. { exception object }
  723. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  724. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  725. cexceptionstatehandler.cleanupobjectstack;
  726. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  727. end;
  728. if fc_exit in trystate.newflowcontrol then
  729. begin
  730. { do some magic for exit in the try block }
  731. hlcg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  732. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  733. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  734. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  735. end;
  736. if fc_break in trystate.newflowcontrol then
  737. begin
  738. hlcg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  739. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  740. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  741. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  742. end;
  743. if fc_continue in trystate.newflowcontrol then
  744. begin
  745. hlcg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  746. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_popaddrstack',[],nil);
  747. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  748. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  749. end;
  750. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  751. hlcg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  752. { end cleanup }
  753. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  754. errorexit:
  755. { restore all saved labels }
  756. endexceptlabel:=oldendexceptlabel;
  757. { restore the control flow labels }
  758. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  759. if assigned(oldBreakLabel) then
  760. begin
  761. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  762. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  763. end;
  764. { return all used control flow statements }
  765. flowcontrol:=trystate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol +
  766. trystate.newflowcontrol - [fc_inflowcontrol,fc_catching_exceptions]);
  767. end;
  768. procedure tcgonnode.pass_generate_code;
  769. var
  770. nextonlabel,
  771. exitonlabel,
  772. continueonlabel,
  773. breakonlabel,
  774. oldCurrExitLabel,
  775. oldContinueLabel,
  776. oldBreakLabel : tasmlabel;
  777. doobjectdestroyandreraisestate: tcgexceptionstatehandler.texceptionstate;
  778. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  779. href2: treference;
  780. paraloc1 : tcgpara;
  781. exceptvarsym : tlocalvarsym;
  782. pd : tprocdef;
  783. fpc_catches_res: TCGPara;
  784. fpc_catches_resloc: tlocation;
  785. otherunit,
  786. indirect : boolean;
  787. begin
  788. paraloc1.init;
  789. location_reset(location,LOC_VOID,OS_NO);
  790. oldCurrExitLabel:=nil;
  791. continueonlabel:=nil;
  792. breakonlabel:=nil;
  793. exitonlabel:=nil;
  794. current_asmdata.getjumplabel(nextonlabel);
  795. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  796. indirect:=(tf_supports_packages in target_info.flags) and
  797. (target_info.system in systems_indirect_var_imports) and
  798. (cs_imported_data in current_settings.localswitches) and
  799. otherunit;
  800. { send the vmt parameter }
  801. pd:=search_system_proc('fpc_catches');
  802. reference_reset_symbol(href2,current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname,AT_DATA,indirect),0,sizeof(pint),[]);
  803. if otherunit then
  804. current_module.add_extern_asmsym(excepttype.vmt_mangledname,AB_EXTERNAL,AT_DATA);
  805. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,paraloc1);
  806. hlcg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,excepttype.vmt_def,href2,paraloc1);
  807. paramanager.freecgpara(current_asmdata.CurrAsmList,paraloc1);
  808. fpc_catches_res:=hlcg.g_call_system_proc(current_asmdata.CurrAsmList,pd,[@paraloc1],nil);
  809. location_reset(fpc_catches_resloc,LOC_REGISTER,def_cgsize(fpc_catches_res.def));
  810. fpc_catches_resloc.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,fpc_catches_res.def);
  811. hlcg.gen_load_cgpara_loc(current_asmdata.CurrAsmList,fpc_catches_res.def,fpc_catches_res,fpc_catches_resloc,true);
  812. { is it this catch? No. go to next onlabel }
  813. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,fpc_catches_res.def,OC_EQ,0,fpc_catches_resloc.register,nextonlabel);
  814. { Retrieve exception variable }
  815. if assigned(excepTSymtable) then
  816. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  817. else
  818. internalerror(2011020401);
  819. if assigned(exceptvarsym) then
  820. begin
  821. location_reset_ref(exceptvarsym.localloc,LOC_REFERENCE,def_cgsize(voidpointertype),voidpointertype.alignment,[]);
  822. tg.GetLocal(current_asmdata.CurrAsmList,exceptvarsym.vardef.size,exceptvarsym.vardef,exceptvarsym.localloc.reference);
  823. hlcg.a_load_reg_ref(current_asmdata.CurrAsmList,fpc_catches_res.def,exceptvarsym.vardef,fpc_catches_resloc.register,exceptvarsym.localloc.reference);
  824. end;
  825. { in the case that another exception is risen
  826. we've to destroy the old one:
  827. call setjmp, and jump to finally label on non-zero result }
  828. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  829. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraisestate);
  830. oldBreakLabel:=nil;
  831. oldContinueLabel:=nil;
  832. if assigned(right) then
  833. begin
  834. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  835. current_asmdata.getjumplabel(exitonlabel);
  836. current_procinfo.CurrExitLabel:=exitonlabel;
  837. if assigned(current_procinfo.CurrBreakLabel) then
  838. begin
  839. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  840. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  841. current_asmdata.getjumplabel(breakonlabel);
  842. current_asmdata.getjumplabel(continueonlabel);
  843. current_procinfo.CurrContinueLabel:=continueonlabel;
  844. current_procinfo.CurrBreakLabel:=breakonlabel;
  845. end;
  846. secondpass(right);
  847. end;
  848. cexceptionstatehandler.handle_nested_exception(current_asmdata.CurrAsmList,excepttemps,doobjectdestroyandreraisestate);
  849. { clear some stuff }
  850. if assigned(exceptvarsym) then
  851. begin
  852. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  853. exceptvarsym.localloc.loc:=LOC_INVALID;
  854. end;
  855. hlcg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  856. if assigned(right) then
  857. begin
  858. { special handling for control flow instructions }
  859. if fc_exit in doobjectdestroyandreraisestate.newflowcontrol then
  860. begin
  861. { the address and object pop does secondtryexcept }
  862. hlcg.a_label(current_asmdata.CurrAsmList,exitonlabel);
  863. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  864. end;
  865. if fc_break in doobjectdestroyandreraisestate.newflowcontrol then
  866. begin
  867. { the address and object pop does secondtryexcept }
  868. hlcg.a_label(current_asmdata.CurrAsmList,breakonlabel);
  869. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  870. end;
  871. if fc_continue in doobjectdestroyandreraisestate.newflowcontrol then
  872. begin
  873. { the address and object pop does secondtryexcept }
  874. hlcg.a_label(current_asmdata.CurrAsmList,continueonlabel);
  875. hlcg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  876. end;
  877. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  878. if assigned(oldBreakLabel) then
  879. begin
  880. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  881. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  882. end;
  883. end;
  884. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  885. hlcg.a_label(current_asmdata.CurrAsmList,nextonlabel);
  886. flowcontrol:=doobjectdestroyandreraisestate.oldflowcontrol+(doobjectdestroyandreraisestate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  887. paraloc1.done;
  888. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  889. { next on node }
  890. if assigned(left) then
  891. secondpass(left);
  892. end;
  893. {*****************************************************************************
  894. SecondTryFinally
  895. *****************************************************************************}
  896. procedure tcgtryfinallynode.handle_safecall_exception;
  897. var
  898. cgpara: tcgpara;
  899. selfsym: tparavarsym;
  900. pd: tprocdef;
  901. begin
  902. { call fpc_safecallhandler, passing self for methods of classes,
  903. nil otherwise. }
  904. pd:=search_system_proc('fpc_safecallhandler');
  905. cgpara.init;
  906. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,cgpara);
  907. if is_class(current_procinfo.procdef.struct) then
  908. begin
  909. selfsym:=tparavarsym(current_procinfo.procdef.parast.Find('self'));
  910. if (selfsym=nil) or (selfsym.typ<>paravarsym) then
  911. InternalError(2011123101);
  912. cg.a_load_loc_cgpara(current_asmdata.CurrAsmList,selfsym.localloc,cgpara);
  913. end
  914. else
  915. cg.a_load_const_cgpara(current_asmdata.CurrAsmList,OS_ADDR,0,cgpara);
  916. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  917. cgpara.done;
  918. cg.g_call(current_asmdata.CurrAsmList,'FPC_SAFECALLHANDLER');
  919. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_FUNCTION_RESULT_REG, NR_FUNCTION_RETURN_REG);
  920. end;
  921. procedure tcgtryfinallynode.pass_generate_code;
  922. var
  923. endfinallylabel,
  924. exitfinallylabel,
  925. continuefinallylabel,
  926. breakfinallylabel,
  927. oldCurrExitLabel,
  928. oldContinueLabel,
  929. oldBreakLabel : tasmlabel;
  930. finallyexceptionstate: tcgexceptionstatehandler.texceptionstate;
  931. excepttemps : tcgexceptionstatehandler.texceptiontemps;
  932. reasonreg : tregister;
  933. begin
  934. location_reset(location,LOC_VOID,OS_NO);
  935. oldBreakLabel:=nil;
  936. oldContinueLabel:=nil;
  937. continuefinallylabel:=nil;
  938. breakfinallylabel:=nil;
  939. current_asmdata.getjumplabel(endfinallylabel);
  940. { call setjmp, and jump to finally label on non-zero result }
  941. cexceptionstatehandler.get_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  942. cexceptionstatehandler.new_exception(current_asmdata.CurrAsmList,excepttemps,finallyexceptionstate);
  943. { the finally block must catch break, continue and exit }
  944. { statements }
  945. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  946. if implicitframe then
  947. exitfinallylabel:=finallyexceptionstate.exceptionlabel
  948. else
  949. current_asmdata.getjumplabel(exitfinallylabel);
  950. current_procinfo.CurrExitLabel:=exitfinallylabel;
  951. if assigned(current_procinfo.CurrBreakLabel) then
  952. begin
  953. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  954. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  955. if implicitframe then
  956. begin
  957. breakfinallylabel:=finallyexceptionstate.exceptionlabel;
  958. continuefinallylabel:=finallyexceptionstate.exceptionlabel;
  959. end
  960. else
  961. begin
  962. current_asmdata.getjumplabel(breakfinallylabel);
  963. current_asmdata.getjumplabel(continuefinallylabel);
  964. end;
  965. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  966. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  967. end;
  968. { try code }
  969. if assigned(left) then
  970. begin
  971. secondpass(left);
  972. if codegenerror then
  973. exit;
  974. end;
  975. { don't generate line info for internal cleanup }
  976. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  977. cexceptionstatehandler.emit_except_label(current_asmdata.CurrAsmList,finallyexceptionstate);
  978. { just free the frame information }
  979. cexceptionstatehandler.free_exception(current_asmdata.CurrAsmList,excepttemps,1,finallyexceptionstate.exceptionlabel,true);
  980. { end cleanup }
  981. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  982. { finally code (don't unconditionally set fc_inflowcontrol, since the
  983. finally code is unconditionally executed; we do have to filter out
  984. flags regarding break/contrinue/etc. because we have to give an
  985. error in case one of those is used in the finally-code }
  986. flowcontrol:=finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol];
  987. secondpass(right);
  988. { goto is allowed if it stays inside the finally block,
  989. this is checked using the exception block number }
  990. if (flowcontrol-[fc_gotolabel])<>(finallyexceptionstate.oldflowcontrol*[fc_inflowcontrol]) then
  991. CGMessage(cg_e_control_flow_outside_finally);
  992. if codegenerror then
  993. exit;
  994. { don't generate line info for internal cleanup }
  995. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  996. { the value should now be in the exception handler }
  997. reasonreg:=hlcg.getintregister(current_asmdata.CurrAsmList,osuinttype);
  998. hlcg.g_exception_reason_load(current_asmdata.CurrAsmList,osuinttype,osuinttype,excepttemps.reasonbuf,reasonreg);
  999. if implicitframe then
  1000. begin
  1001. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  1002. { finally code only needed to be executed on exception (-> in
  1003. if-branch -> fc_inflowcontrol) }
  1004. flowcontrol:=[fc_inflowcontrol];
  1005. if (tf_safecall_exceptions in target_info.flags) and
  1006. (current_procinfo.procdef.proccalloption=pocall_safecall) then
  1007. handle_safecall_exception
  1008. else
  1009. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil);
  1010. end
  1011. else
  1012. begin
  1013. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,0,reasonreg,endfinallylabel);
  1014. if fc_exit in finallyexceptionstate.newflowcontrol then
  1015. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,2,reasonreg,oldCurrExitLabel);
  1016. if fc_break in finallyexceptionstate.newflowcontrol then
  1017. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,3,reasonreg,oldBreakLabel);
  1018. if fc_continue in finallyexceptionstate.newflowcontrol then
  1019. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,osuinttype,OC_EQ,4,reasonreg,oldContinueLabel);
  1020. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil);
  1021. { do some magic for exit,break,continue in the try block }
  1022. if fc_exit in finallyexceptionstate.newflowcontrol then
  1023. begin
  1024. hlcg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  1025. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  1026. hlcg.g_exception_reason_save_const(current_asmdata.CurrAsmList,osuinttype,2,excepttemps.reasonbuf);
  1027. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallyexceptionstate.exceptionlabel);
  1028. end;
  1029. if fc_break in finallyexceptionstate.newflowcontrol then
  1030. begin
  1031. hlcg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  1032. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  1033. hlcg.g_exception_reason_save_const(current_asmdata.CurrAsmList,osuinttype,3,excepttemps.reasonbuf);
  1034. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallyexceptionstate.exceptionlabel);
  1035. end;
  1036. if fc_continue in finallyexceptionstate.newflowcontrol then
  1037. begin
  1038. hlcg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  1039. hlcg.g_exception_reason_discard(current_asmdata.CurrAsmList,osuinttype,excepttemps.reasonbuf);
  1040. hlcg.g_exception_reason_save_const(current_asmdata.CurrAsmList,osuinttype,4,excepttemps.reasonbuf);
  1041. hlcg.a_jmp_always(current_asmdata.CurrAsmList,finallyexceptionstate.exceptionlabel);
  1042. end;
  1043. end;
  1044. cexceptionstatehandler.unget_exception_temps(current_asmdata.CurrAsmList,excepttemps);
  1045. hlcg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  1046. { end cleanup }
  1047. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  1048. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  1049. if assigned(current_procinfo.CurrBreakLabel) then
  1050. begin
  1051. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  1052. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  1053. end;
  1054. flowcontrol:=finallyexceptionstate.oldflowcontrol+(finallyexceptionstate.newflowcontrol-[fc_inflowcontrol,fc_catching_exceptions]);
  1055. end;
  1056. begin
  1057. cwhilerepeatnode:=tcgwhilerepeatnode;
  1058. cifnode:=tcgifnode;
  1059. cfornode:=tcgfornode;
  1060. cexitnode:=tcgexitnode;
  1061. cbreaknode:=tcgbreaknode;
  1062. ccontinuenode:=tcgcontinuenode;
  1063. cgotonode:=tcggotonode;
  1064. clabelnode:=tcglabelnode;
  1065. craisenode:=tcgraisenode;
  1066. ctryexceptnode:=tcgtryexceptnode;
  1067. ctryfinallynode:=tcgtryfinallynode;
  1068. connode:=tcgonnode;
  1069. cexceptionstatehandler:=tcgexceptionstatehandler;
  1070. end.