ncgflw.pas 51 KB

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