n386flw.pas 22 KB

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