n386flw.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. {
  2. Copyright (c) 2011 by Free Pascal development team
  3. Generate Win32-specific exception handling code
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit n386flw;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nflw,ncgflw,psub;
  22. type
  23. ti386raisenode=class(tcgraisenode)
  24. function pass_1 : tnode;override;
  25. end;
  26. ti386onnode=class(tcgonnode)
  27. procedure pass_generate_code;override;
  28. end;
  29. ti386tryexceptnode=class(tcgtryexceptnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. ti386tryfinallynode=class(tcgtryfinallynode)
  33. finalizepi: tcgprocinfo;
  34. constructor create(l,r:TNode);override;
  35. constructor create_implicit(l,r:TNode);override;
  36. function pass_1: tnode;override;
  37. function dogetcopy : tnode;override;
  38. function simplify(forinline: boolean): tnode;override;
  39. procedure pass_generate_code;override;
  40. end;
  41. implementation
  42. uses
  43. cutils,globtype,globals,verbose,systems,fmodule,
  44. nbas,ncal,nmem,nutils,
  45. symconst,symbase,symtable,symsym,symdef,
  46. cgbase,cgobj,cgcpu,cgutils,tgobj,
  47. cpubase,htypechk,
  48. parabase,paramgr,pass_1,pass_2,ncgutil,cga,
  49. aasmbase,aasmtai,aasmdata,aasmcpu,procinfo,cpupi,procdefutil;
  50. var
  51. endexceptlabel: tasmlabel;
  52. { ti386raisenode }
  53. function ti386raisenode.pass_1 : tnode;
  54. var
  55. statements : tstatementnode;
  56. raisenode : tcallnode;
  57. begin
  58. { difference from generic code is that address stack is not popped on reraise }
  59. if (target_info.system<>system_i386_win32) or assigned(left) then
  60. result:=inherited pass_1
  61. else
  62. begin
  63. result:=internalstatements(statements);
  64. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  65. include(raisenode.callnodeflags,cnf_call_never_returns);
  66. addstatement(statements,raisenode);
  67. end;
  68. end;
  69. { ti386onnode }
  70. procedure ti386onnode.pass_generate_code;
  71. var
  72. oldflowcontrol : tflowcontrol;
  73. exceptvarsym : tlocalvarsym;
  74. begin
  75. if (target_info.system<>system_i386_win32) then
  76. begin
  77. inherited pass_generate_code;
  78. exit;
  79. end;
  80. location_reset(location,LOC_VOID,OS_NO);
  81. oldflowcontrol:=flowcontrol;
  82. flowcontrol:=[fc_inflowcontrol];
  83. { RTL will put exceptobject into EAX when jumping here }
  84. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  85. { Retrieve exception variable }
  86. if assigned(excepTSymtable) then
  87. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  88. else
  89. exceptvarsym:=nil;
  90. if assigned(exceptvarsym) then
  91. begin
  92. exceptvarsym.localloc.loc:=LOC_REFERENCE;
  93. exceptvarsym.localloc.size:=OS_ADDR;
  94. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),voidpointertype,exceptvarsym.localloc.reference);
  95. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptvarsym.localloc.reference);
  96. end;
  97. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  98. if assigned(right) then
  99. secondpass(right);
  100. { deallocate exception symbol }
  101. if assigned(exceptvarsym) then
  102. begin
  103. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  104. exceptvarsym.localloc.loc:=LOC_INVALID;
  105. end;
  106. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  107. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  108. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  109. end;
  110. { ti386tryfinallynode }
  111. function reset_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  112. begin
  113. case n.nodetype of
  114. temprefn:
  115. make_not_regable(n,[]);
  116. calln:
  117. include(tprocinfo(arg).flags,pi_do_call);
  118. else ;
  119. end;
  120. result:=fen_true;
  121. end;
  122. function copy_parasize(var n: tnode; arg: pointer): foreachnoderesult;
  123. begin
  124. if n.nodetype=calln then
  125. tcgprocinfo(arg).allocate_push_parasize(tcallnode(n).pushed_parasize);
  126. result:=fen_true;
  127. end;
  128. constructor ti386tryfinallynode.create(l, r: TNode);
  129. begin
  130. inherited create(l,r);
  131. if (target_info.system<>system_i386_win32) or
  132. { Don't create child procedures for generic methods, their nested-like
  133. behavior causes compilation errors because real nested procedures
  134. aren't allowed for generics. Not creating them doesn't harm because
  135. generic node tree is discarded without generating code. }
  136. (df_generic in current_procinfo.procdef.defoptions)
  137. then
  138. exit;
  139. finalizepi:=tcgprocinfo(current_procinfo.create_for_outlining('$fin$',current_procinfo.procdef.struct,potype_exceptfilter,voidtype,r));
  140. { Regvar optimization for symbols is suppressed when using exceptions, but
  141. temps may be still placed into registers. This must be fixed. }
  142. foreachnodestatic(r,@reset_regvars,finalizepi);
  143. include(finalizepi.flags,pi_has_assembler_block);
  144. include(finalizepi.flags,pi_do_call);
  145. include(finalizepi.flags,pi_uses_exceptions);
  146. end;
  147. constructor ti386tryfinallynode.create_implicit(l, r: TNode);
  148. begin
  149. inherited create_implicit(l, r);
  150. if (target_info.system<>system_i386_win32) then
  151. exit;
  152. { safecall procedures can handle implicit finalization as part of "except" flow }
  153. if implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall) then
  154. exit;
  155. if df_generic in current_procinfo.procdef.defoptions then
  156. InternalError(2013012501);
  157. finalizepi:=tcgprocinfo(current_procinfo.create_for_outlining('$fin$',current_procinfo.procdef.struct,potype_exceptfilter,voidtype,r));
  158. include(finalizepi.flags,pi_has_assembler_block);
  159. include(finalizepi.flags,pi_do_call);
  160. include(finalizepi.flags,pi_uses_exceptions);
  161. end;
  162. function ti386tryfinallynode.pass_1: tnode;
  163. var
  164. selfsym: tparavarsym;
  165. begin
  166. result:=inherited pass_1;
  167. if (target_info.system=system_i386_win32) then
  168. begin
  169. { safecall method will access 'self' from except block -> make it non-regable }
  170. if implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall) and
  171. is_class(current_procinfo.procdef.struct) then
  172. begin
  173. selfsym:=tparavarsym(current_procinfo.procdef.parast.Find('self'));
  174. if (selfsym=nil) or (selfsym.typ<>paravarsym) then
  175. InternalError(2011123102);
  176. selfsym.varregable:=vr_none;
  177. end;
  178. end;
  179. end;
  180. function ti386tryfinallynode.dogetcopy: tnode;
  181. var
  182. n: ti386tryfinallynode;
  183. begin
  184. n:=ti386tryfinallynode(inherited dogetcopy);
  185. if target_info.system=system_i386_win32 then
  186. begin
  187. n.finalizepi:=tcgprocinfo(cprocinfo.create(finalizepi.parent));
  188. n.finalizepi.force_nested;
  189. n.finalizepi.procdef:=create_outline_procdef('$fin$',current_procinfo.procdef.struct,potype_exceptfilter,voidtype);
  190. n.finalizepi.entrypos:=finalizepi.entrypos;
  191. n.finalizepi.entryswitches:=finalizepi.entryswitches;
  192. n.finalizepi.exitpos:=finalizepi.exitpos;
  193. n.finalizepi.exitswitches:=finalizepi.exitswitches;
  194. n.finalizepi.flags:=finalizepi.flags;
  195. { node already transformed? }
  196. if assigned(finalizepi.code) then
  197. begin
  198. n.finalizepi.code:=finalizepi.code.getcopy;
  199. n.right:=ccallnode.create(nil,tprocsym(n.finalizepi.procdef.procsym),nil,nil,[],nil);
  200. firstpass(n.right);
  201. end;
  202. end;
  203. result:=n;
  204. end;
  205. function ti386tryfinallynode.simplify(forinline: boolean): tnode;
  206. begin
  207. result:=inherited simplify(forinline);
  208. if (target_info.system<>system_i386_win32) then
  209. exit;
  210. { actually, this is not really the right place to do a node transformation like this }
  211. if (result=nil) and assigned(finalizepi) and (not(assigned(finalizepi.code))) then
  212. begin
  213. finalizepi.code:=right;
  214. foreachnodestatic(right,@copy_parasize,finalizepi);
  215. right:=ccallnode.create(nil,tprocsym(finalizepi.procdef.procsym),nil,nil,[],nil);
  216. firstpass(right);
  217. { For implicit frames, no actual code is available at this time,
  218. it is added later in assembler form. So store the nested procinfo
  219. for later use. }
  220. if implicitframe then
  221. begin
  222. current_procinfo.finalize_procinfo:=finalizepi;
  223. end;
  224. end;
  225. end;
  226. procedure emit_scope_start(handler,data: TAsmSymbol);
  227. var
  228. href: treference;
  229. hreg: tregister;
  230. begin
  231. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  232. reference_reset_base(href,hreg,0,ctempposinvalid,sizeof(pint),[]);
  233. href.segment:=NR_FS;
  234. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  235. emit_sym(A_PUSH,S_L,data);
  236. emit_reg(A_PUSH,S_L,NR_FRAME_POINTER_REG);
  237. emit_sym(A_PUSH,S_L,handler);
  238. emit_ref(A_PUSH,S_L,href);
  239. emit_reg_ref(A_MOV,S_L,NR_ESP,href);
  240. end;
  241. procedure emit_scope_end;
  242. var
  243. href: treference;
  244. hreg,hreg2: tregister;
  245. begin
  246. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  247. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  248. reference_reset_base(href,hreg,0,ctempposinvalid,sizeof(pint),[]);
  249. href.segment:=NR_FS;
  250. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  251. emit_reg(A_POP,S_L,hreg2);
  252. emit_const_reg(A_ADD,S_L,3*sizeof(pint),NR_ESP);
  253. emit_reg_ref(A_MOV,S_L,hreg2,href);
  254. end;
  255. procedure ti386tryfinallynode.pass_generate_code;
  256. var
  257. finallylabel,
  258. exceptlabel,
  259. safecalllabel,
  260. endfinallylabel,
  261. exitfinallylabel,
  262. continuefinallylabel,
  263. breakfinallylabel,
  264. oldCurrExitLabel,
  265. oldContinueLabel,
  266. oldBreakLabel : tasmlabel;
  267. oldflowcontrol,tryflowcontrol : tflowcontrol;
  268. is_safecall: boolean;
  269. sym : tasmsymbol;
  270. begin
  271. if (target_info.system<>system_i386_win32) then
  272. begin
  273. inherited pass_generate_code;
  274. exit;
  275. end;
  276. location_reset(location,LOC_VOID,OS_NO);
  277. tryflowcontrol:=[];
  278. oldBreakLabel:=nil;
  279. oldContinueLabel:=nil;
  280. continuefinallylabel:=nil;
  281. breakfinallylabel:=nil;
  282. exceptlabel:=nil;
  283. safecalllabel:=nil;
  284. is_safecall:=implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall);
  285. { check if child nodes do a break/continue/exit }
  286. oldflowcontrol:=flowcontrol;
  287. flowcontrol:=[fc_inflowcontrol];
  288. current_asmdata.getjumplabel(finallylabel);
  289. current_asmdata.getjumplabel(endfinallylabel);
  290. { the finally block must catch break, continue and exit }
  291. { statements }
  292. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  293. if implicitframe then
  294. exitfinallylabel:=finallylabel
  295. else
  296. current_asmdata.getjumplabel(exitfinallylabel);
  297. current_procinfo.CurrExitLabel:=exitfinallylabel;
  298. if assigned(current_procinfo.CurrBreakLabel) then
  299. begin
  300. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  301. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  302. if implicitframe then
  303. begin
  304. breakfinallylabel:=finallylabel;
  305. continuefinallylabel:=finallylabel;
  306. end
  307. else
  308. begin
  309. current_asmdata.getjumplabel(breakfinallylabel);
  310. current_asmdata.getjumplabel(continuefinallylabel);
  311. end;
  312. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  313. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  314. end;
  315. { Start of scope }
  316. if is_safecall then
  317. begin
  318. with cg.rg[R_INTREGISTER] do
  319. used_in_proc:=used_in_proc+[RS_EBX,RS_ESI,RS_EDI];
  320. current_asmdata.getjumplabel(exceptlabel);
  321. sym:=current_asmdata.RefAsmSymbol('__FPC_except_safecall',AT_FUNCTION);
  322. emit_scope_start(
  323. sym,
  324. exceptlabel
  325. );
  326. current_module.add_extern_asmsym(sym);
  327. end
  328. else
  329. begin
  330. sym:=current_asmdata.RefAsmSymbol('__FPC_finally_handler',AT_FUNCTION);
  331. emit_scope_start(
  332. sym,
  333. current_asmdata.RefAsmSymbol(finalizepi.procdef.mangledname,AT_FUNCTION)
  334. );
  335. current_module.add_extern_asmsym(sym);
  336. end;
  337. { try code }
  338. if assigned(left) then
  339. begin
  340. secondpass(left);
  341. tryflowcontrol:=flowcontrol;
  342. if codegenerror then
  343. exit;
  344. end;
  345. { don't generate line info for internal cleanup }
  346. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  347. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  348. emit_scope_end;
  349. if is_safecall then
  350. begin
  351. current_asmdata.getjumplabel(safecalllabel);
  352. cg.a_jmp_always(current_asmdata.CurrAsmList,safecalllabel);
  353. { RTL handler will jump here on exception }
  354. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  355. handle_safecall_exception;
  356. cg.a_label(current_asmdata.CurrAsmList,safecalllabel);
  357. end;
  358. { end cleanup }
  359. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  360. { generate finally code as a separate procedure }
  361. { !!! this resets flowcontrol, how to check flow away? }
  362. if not implicitframe then
  363. tcgprocinfo(current_procinfo).generate_exceptfilter(finalizepi);
  364. flowcontrol:=[fc_inflowcontrol];
  365. { right is a call to finalizer procedure }
  366. secondpass(right);
  367. { goto is allowed if it stays inside the finally block,
  368. this is checked using the exception block number }
  369. if (flowcontrol-[fc_gotolabel])<>[fc_inflowcontrol] then
  370. CGMessage(cg_e_control_flow_outside_finally);
  371. if codegenerror then
  372. exit;
  373. { don't generate line info for internal cleanup }
  374. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  375. if not implicitframe then
  376. begin
  377. if tryflowcontrol*[fc_exit,fc_break,fc_continue]<>[] then
  378. cg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  379. { do some magic for exit,break,continue in the try block }
  380. if fc_exit in tryflowcontrol then
  381. begin
  382. cg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  383. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  384. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  385. end;
  386. if fc_break in tryflowcontrol then
  387. begin
  388. cg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  389. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  390. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  391. end;
  392. if fc_continue in tryflowcontrol then
  393. begin
  394. cg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  395. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  396. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  397. end;
  398. end;
  399. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  400. { end cleanup }
  401. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  402. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  403. if assigned(current_procinfo.CurrBreakLabel) then
  404. begin
  405. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  406. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  407. end;
  408. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  409. end;
  410. { ti386tryexceptnode }
  411. procedure ti386tryexceptnode.pass_generate_code;
  412. var
  413. exceptlabel,oldendexceptlabel,
  414. lastonlabel,
  415. exitexceptlabel,
  416. continueexceptlabel,
  417. breakexceptlabel,
  418. exittrylabel,
  419. continuetrylabel,
  420. breaktrylabel,
  421. oldCurrExitLabel,
  422. oldContinueLabel,
  423. oldBreakLabel : tasmlabel;
  424. onlabel,
  425. filterlabel: tasmlabel;
  426. oldflowcontrol,tryflowcontrol,
  427. exceptflowcontrol : tflowcontrol;
  428. hnode : tnode;
  429. hlist : tasmlist;
  430. onnodecount : tai_const;
  431. sym : tasmsymbol;
  432. label
  433. errorexit;
  434. begin
  435. if (target_info.system<>system_i386_win32) then
  436. begin
  437. inherited pass_generate_code;
  438. exit;
  439. end;
  440. location_reset(location,LOC_VOID,OS_NO);
  441. exceptflowcontrol:=[];
  442. breakexceptlabel:=nil;
  443. continueexceptlabel:=nil;
  444. breaktrylabel:=nil;
  445. continuetrylabel:=nil;
  446. oldflowcontrol:=flowcontrol;
  447. flowcontrol:=[fc_inflowcontrol];
  448. { this can be called recursivly }
  449. oldBreakLabel:=nil;
  450. oldContinueLabel:=nil;
  451. oldendexceptlabel:=endexceptlabel;
  452. { Win32 SEH unwinding does not preserve registers. Indicate that they are
  453. going to be destroyed. }
  454. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,[RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]);
  455. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,[RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]);
  456. { save the old labels for control flow statements }
  457. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  458. if assigned(current_procinfo.CurrBreakLabel) then
  459. begin
  460. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  461. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  462. end;
  463. { get new labels for the control flow statements }
  464. current_asmdata.getjumplabel(exittrylabel);
  465. current_asmdata.getjumplabel(exitexceptlabel);
  466. if assigned(current_procinfo.CurrBreakLabel) then
  467. begin
  468. current_asmdata.getjumplabel(breaktrylabel);
  469. current_asmdata.getjumplabel(continuetrylabel);
  470. current_asmdata.getjumplabel(breakexceptlabel);
  471. current_asmdata.getjumplabel(continueexceptlabel);
  472. end;
  473. current_asmdata.getjumplabel(exceptlabel);
  474. current_asmdata.getjumplabel(endexceptlabel);
  475. current_asmdata.getjumplabel(lastonlabel);
  476. filterlabel:=nil;
  477. { start of scope }
  478. if assigned(right) then
  479. begin
  480. current_asmdata.getaddrlabel(filterlabel);
  481. sym:=current_asmdata.RefAsmSymbol('__FPC_on_handler',AT_FUNCTION);
  482. emit_scope_start(
  483. sym,
  484. filterlabel);
  485. current_module.add_extern_asmsym(sym);
  486. end
  487. else
  488. begin
  489. sym:=current_asmdata.RefAsmSymbol('__FPC_except_handler',AT_FUNCTION);
  490. emit_scope_start(
  491. sym,
  492. exceptlabel);
  493. current_module.add_extern_asmsym(sym);
  494. end;
  495. { set control flow labels for the try block }
  496. current_procinfo.CurrExitLabel:=exittrylabel;
  497. if assigned(oldBreakLabel) then
  498. begin
  499. current_procinfo.CurrContinueLabel:=continuetrylabel;
  500. current_procinfo.CurrBreakLabel:=breaktrylabel;
  501. end;
  502. secondpass(left);
  503. tryflowcontrol:=flowcontrol;
  504. if codegenerror then
  505. goto errorexit;
  506. emit_scope_end;
  507. { jump over except handlers }
  508. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  509. if fc_exit in tryflowcontrol then
  510. begin
  511. cg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  512. emit_scope_end;
  513. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  514. end;
  515. if fc_break in tryflowcontrol then
  516. begin
  517. cg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  518. emit_scope_end;
  519. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  520. end;
  521. if fc_continue in tryflowcontrol then
  522. begin
  523. cg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  524. emit_scope_end;
  525. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  526. end;
  527. { target for catch-all handler }
  528. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  529. { set control flow labels for the except block }
  530. { and the on statements }
  531. current_procinfo.CurrExitLabel:=exitexceptlabel;
  532. if assigned(oldBreakLabel) then
  533. begin
  534. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  535. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  536. end;
  537. flowcontrol:=[fc_inflowcontrol];
  538. { on statements }
  539. if assigned(right) then
  540. begin
  541. { emit filter table to a temporary asmlist }
  542. hlist:=TAsmList.Create;
  543. new_section(hlist,sec_rodata,filterlabel.name,4);
  544. cg.a_label(hlist,filterlabel);
  545. onnodecount:=tai_const.create_32bit(0);
  546. hlist.concat(onnodecount);
  547. hnode:=right;
  548. while assigned(hnode) do
  549. begin
  550. if hnode.nodetype<>onn then
  551. InternalError(2011103101);
  552. current_asmdata.getjumplabel(onlabel);
  553. sym:=current_asmdata.RefAsmSymbol(tonnode(hnode).excepttype.vmt_mangledname,AT_DATA,true);
  554. hlist.concat(tai_const.create_sym(sym));
  555. hlist.concat(tai_const.create_sym(onlabel));
  556. current_module.add_extern_asmsym(sym);
  557. cg.a_label(current_asmdata.CurrAsmList,onlabel);
  558. secondpass(hnode);
  559. inc(onnodecount.value);
  560. hnode:=tonnode(hnode).left;
  561. end;
  562. { add 'else' node to the filter list, too }
  563. if assigned(t1) then
  564. begin
  565. hlist.concat(tai_const.create_32bit(-1));
  566. hlist.concat(tai_const.create_sym(lastonlabel));
  567. inc(onnodecount.value);
  568. end;
  569. { now move filter table to permanent list all at once }
  570. current_procinfo.aktlocaldata.concatlist(hlist);
  571. hlist.free;
  572. end;
  573. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  574. if assigned(t1) then
  575. begin
  576. { here we don't have to reset flowcontrol }
  577. { the default and on flowcontrols are handled equal }
  578. secondpass(t1);
  579. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  580. if (flowcontrol*[fc_exit,fc_break,fc_continue]<>[]) then
  581. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  582. end;
  583. exceptflowcontrol:=flowcontrol;
  584. if fc_exit in exceptflowcontrol then
  585. begin
  586. { do some magic for exit in the try block }
  587. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  588. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  589. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  590. end;
  591. if fc_break in exceptflowcontrol then
  592. begin
  593. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  594. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  595. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  596. end;
  597. if fc_continue in exceptflowcontrol then
  598. begin
  599. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  600. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  601. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  602. end;
  603. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  604. errorexit:
  605. { restore all saved labels }
  606. endexceptlabel:=oldendexceptlabel;
  607. { restore the control flow labels }
  608. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  609. if assigned(oldBreakLabel) then
  610. begin
  611. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  612. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  613. end;
  614. { return all used control flow statements }
  615. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  616. tryflowcontrol - [fc_inflowcontrol]);
  617. end;
  618. initialization
  619. craisenode:=ti386raisenode;
  620. connode:=ti386onnode;
  621. ctryexceptnode:=ti386tryexceptnode;
  622. ctryfinallynode:=ti386tryfinallynode;
  623. end.