n386flw.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. end;
  201. end;
  202. result:=n;
  203. end;
  204. function ti386tryfinallynode.simplify(forinline: boolean): tnode;
  205. begin
  206. result:=inherited simplify(forinline);
  207. if (target_info.system<>system_i386_win32) then
  208. exit;
  209. { actually, this is not really the right place to do a node transformation like this }
  210. if (result=nil) and assigned(finalizepi) and (not(assigned(finalizepi.code))) then
  211. begin
  212. finalizepi.code:=right;
  213. foreachnodestatic(right,@copy_parasize,finalizepi);
  214. right:=ccallnode.create(nil,tprocsym(finalizepi.procdef.procsym),nil,nil,[],nil);
  215. firstpass(right);
  216. { For implicit frames, no actual code is available at this time,
  217. it is added later in assembler form. So store the nested procinfo
  218. for later use. }
  219. if implicitframe then
  220. begin
  221. current_procinfo.finalize_procinfo:=finalizepi;
  222. end;
  223. end;
  224. end;
  225. procedure emit_scope_start(handler,data: TAsmSymbol);
  226. var
  227. href: treference;
  228. hreg: tregister;
  229. begin
  230. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  231. reference_reset_base(href,hreg,0,ctempposinvalid,sizeof(pint),[]);
  232. href.segment:=NR_FS;
  233. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  234. emit_sym(A_PUSH,S_L,data);
  235. emit_reg(A_PUSH,S_L,NR_FRAME_POINTER_REG);
  236. emit_sym(A_PUSH,S_L,handler);
  237. emit_ref(A_PUSH,S_L,href);
  238. emit_reg_ref(A_MOV,S_L,NR_ESP,href);
  239. end;
  240. procedure emit_scope_end;
  241. var
  242. href: treference;
  243. hreg,hreg2: tregister;
  244. begin
  245. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  246. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  247. reference_reset_base(href,hreg,0,ctempposinvalid,sizeof(pint),[]);
  248. href.segment:=NR_FS;
  249. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  250. emit_reg(A_POP,S_L,hreg2);
  251. emit_const_reg(A_ADD,S_L,3*sizeof(pint),NR_ESP);
  252. emit_reg_ref(A_MOV,S_L,hreg2,href);
  253. end;
  254. procedure ti386tryfinallynode.pass_generate_code;
  255. var
  256. finallylabel,
  257. exceptlabel,
  258. safecalllabel,
  259. endfinallylabel,
  260. exitfinallylabel,
  261. continuefinallylabel,
  262. breakfinallylabel,
  263. oldCurrExitLabel,
  264. oldContinueLabel,
  265. oldBreakLabel : tasmlabel;
  266. oldflowcontrol,tryflowcontrol : tflowcontrol;
  267. is_safecall: boolean;
  268. sym : tasmsymbol;
  269. begin
  270. if (target_info.system<>system_i386_win32) then
  271. begin
  272. inherited pass_generate_code;
  273. exit;
  274. end;
  275. location_reset(location,LOC_VOID,OS_NO);
  276. tryflowcontrol:=[];
  277. oldBreakLabel:=nil;
  278. oldContinueLabel:=nil;
  279. continuefinallylabel:=nil;
  280. breakfinallylabel:=nil;
  281. exceptlabel:=nil;
  282. safecalllabel:=nil;
  283. is_safecall:=implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall);
  284. { check if child nodes do a break/continue/exit }
  285. oldflowcontrol:=flowcontrol;
  286. flowcontrol:=[fc_inflowcontrol];
  287. current_asmdata.getjumplabel(finallylabel);
  288. current_asmdata.getjumplabel(endfinallylabel);
  289. { the finally block must catch break, continue and exit }
  290. { statements }
  291. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  292. if implicitframe then
  293. exitfinallylabel:=finallylabel
  294. else
  295. current_asmdata.getjumplabel(exitfinallylabel);
  296. current_procinfo.CurrExitLabel:=exitfinallylabel;
  297. if assigned(current_procinfo.CurrBreakLabel) then
  298. begin
  299. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  300. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  301. if implicitframe then
  302. begin
  303. breakfinallylabel:=finallylabel;
  304. continuefinallylabel:=finallylabel;
  305. end
  306. else
  307. begin
  308. current_asmdata.getjumplabel(breakfinallylabel);
  309. current_asmdata.getjumplabel(continuefinallylabel);
  310. end;
  311. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  312. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  313. end;
  314. { Start of scope }
  315. if is_safecall then
  316. begin
  317. with cg.rg[R_INTREGISTER] do
  318. used_in_proc:=used_in_proc+[RS_EBX,RS_ESI,RS_EDI];
  319. current_asmdata.getjumplabel(exceptlabel);
  320. sym:=current_asmdata.RefAsmSymbol('__FPC_except_safecall',AT_FUNCTION);
  321. emit_scope_start(
  322. sym,
  323. exceptlabel
  324. );
  325. current_module.add_extern_asmsym(sym);
  326. end
  327. else
  328. begin
  329. sym:=current_asmdata.RefAsmSymbol('__FPC_finally_handler',AT_FUNCTION);
  330. emit_scope_start(
  331. sym,
  332. current_asmdata.RefAsmSymbol(finalizepi.procdef.mangledname,AT_FUNCTION)
  333. );
  334. current_module.add_extern_asmsym(sym);
  335. end;
  336. { try code }
  337. if assigned(left) then
  338. begin
  339. secondpass(left);
  340. tryflowcontrol:=flowcontrol;
  341. if codegenerror then
  342. exit;
  343. end;
  344. { don't generate line info for internal cleanup }
  345. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  346. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  347. emit_scope_end;
  348. if is_safecall then
  349. begin
  350. current_asmdata.getjumplabel(safecalllabel);
  351. cg.a_jmp_always(current_asmdata.CurrAsmList,safecalllabel);
  352. { RTL handler will jump here on exception }
  353. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  354. handle_safecall_exception;
  355. cg.a_label(current_asmdata.CurrAsmList,safecalllabel);
  356. end;
  357. { end cleanup }
  358. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  359. { generate finally code as a separate procedure }
  360. { !!! this resets flowcontrol, how to check flow away? }
  361. if not implicitframe then
  362. tcgprocinfo(current_procinfo).generate_exceptfilter(finalizepi);
  363. flowcontrol:=[fc_inflowcontrol];
  364. { right is a call to finalizer procedure }
  365. secondpass(right);
  366. { goto is allowed if it stays inside the finally block,
  367. this is checked using the exception block number }
  368. if (flowcontrol-[fc_gotolabel])<>[fc_inflowcontrol] then
  369. CGMessage(cg_e_control_flow_outside_finally);
  370. if codegenerror then
  371. exit;
  372. { don't generate line info for internal cleanup }
  373. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  374. if not implicitframe then
  375. begin
  376. if tryflowcontrol*[fc_exit,fc_break,fc_continue]<>[] then
  377. cg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  378. { do some magic for exit,break,continue in the try block }
  379. if fc_exit in tryflowcontrol then
  380. begin
  381. cg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  382. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  383. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  384. end;
  385. if fc_break in tryflowcontrol then
  386. begin
  387. cg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  388. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  389. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  390. end;
  391. if fc_continue in tryflowcontrol then
  392. begin
  393. cg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  394. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  395. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  396. end;
  397. end;
  398. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  399. { end cleanup }
  400. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  401. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  402. if assigned(current_procinfo.CurrBreakLabel) then
  403. begin
  404. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  405. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  406. end;
  407. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  408. end;
  409. { ti386tryexceptnode }
  410. procedure ti386tryexceptnode.pass_generate_code;
  411. var
  412. exceptlabel,oldendexceptlabel,
  413. lastonlabel,
  414. exitexceptlabel,
  415. continueexceptlabel,
  416. breakexceptlabel,
  417. exittrylabel,
  418. continuetrylabel,
  419. breaktrylabel,
  420. oldCurrExitLabel,
  421. oldContinueLabel,
  422. oldBreakLabel : tasmlabel;
  423. onlabel,
  424. filterlabel: tasmlabel;
  425. oldflowcontrol,tryflowcontrol,
  426. exceptflowcontrol : tflowcontrol;
  427. hnode : tnode;
  428. hlist : tasmlist;
  429. onnodecount : tai_const;
  430. sym : tasmsymbol;
  431. label
  432. errorexit;
  433. begin
  434. if (target_info.system<>system_i386_win32) then
  435. begin
  436. inherited pass_generate_code;
  437. exit;
  438. end;
  439. location_reset(location,LOC_VOID,OS_NO);
  440. exceptflowcontrol:=[];
  441. breakexceptlabel:=nil;
  442. continueexceptlabel:=nil;
  443. breaktrylabel:=nil;
  444. continuetrylabel:=nil;
  445. oldflowcontrol:=flowcontrol;
  446. flowcontrol:=[fc_inflowcontrol];
  447. { this can be called recursivly }
  448. oldBreakLabel:=nil;
  449. oldContinueLabel:=nil;
  450. oldendexceptlabel:=endexceptlabel;
  451. { Win32 SEH unwinding does not preserve registers. Indicate that they are
  452. going to be destroyed. }
  453. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,[RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]);
  454. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,[RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]);
  455. { save the old labels for control flow statements }
  456. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  457. if assigned(current_procinfo.CurrBreakLabel) then
  458. begin
  459. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  460. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  461. end;
  462. { get new labels for the control flow statements }
  463. current_asmdata.getjumplabel(exittrylabel);
  464. current_asmdata.getjumplabel(exitexceptlabel);
  465. if assigned(current_procinfo.CurrBreakLabel) then
  466. begin
  467. current_asmdata.getjumplabel(breaktrylabel);
  468. current_asmdata.getjumplabel(continuetrylabel);
  469. current_asmdata.getjumplabel(breakexceptlabel);
  470. current_asmdata.getjumplabel(continueexceptlabel);
  471. end;
  472. current_asmdata.getjumplabel(exceptlabel);
  473. current_asmdata.getjumplabel(endexceptlabel);
  474. current_asmdata.getjumplabel(lastonlabel);
  475. filterlabel:=nil;
  476. { start of scope }
  477. if assigned(right) then
  478. begin
  479. current_asmdata.getaddrlabel(filterlabel);
  480. sym:=current_asmdata.RefAsmSymbol('__FPC_on_handler',AT_FUNCTION);
  481. emit_scope_start(
  482. sym,
  483. filterlabel);
  484. current_module.add_extern_asmsym(sym);
  485. end
  486. else
  487. begin
  488. sym:=current_asmdata.RefAsmSymbol('__FPC_except_handler',AT_FUNCTION);
  489. emit_scope_start(
  490. sym,
  491. exceptlabel);
  492. current_module.add_extern_asmsym(sym);
  493. end;
  494. { set control flow labels for the try block }
  495. current_procinfo.CurrExitLabel:=exittrylabel;
  496. if assigned(oldBreakLabel) then
  497. begin
  498. current_procinfo.CurrContinueLabel:=continuetrylabel;
  499. current_procinfo.CurrBreakLabel:=breaktrylabel;
  500. end;
  501. secondpass(left);
  502. tryflowcontrol:=flowcontrol;
  503. if codegenerror then
  504. goto errorexit;
  505. emit_scope_end;
  506. { jump over except handlers }
  507. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  508. if fc_exit in tryflowcontrol then
  509. begin
  510. cg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  511. emit_scope_end;
  512. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  513. end;
  514. if fc_break in tryflowcontrol then
  515. begin
  516. cg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  517. emit_scope_end;
  518. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  519. end;
  520. if fc_continue in tryflowcontrol then
  521. begin
  522. cg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  523. emit_scope_end;
  524. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  525. end;
  526. { target for catch-all handler }
  527. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  528. { set control flow labels for the except block }
  529. { and the on statements }
  530. current_procinfo.CurrExitLabel:=exitexceptlabel;
  531. if assigned(oldBreakLabel) then
  532. begin
  533. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  534. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  535. end;
  536. flowcontrol:=[fc_inflowcontrol];
  537. { on statements }
  538. if assigned(right) then
  539. begin
  540. { emit filter table to a temporary asmlist }
  541. hlist:=TAsmList.Create;
  542. new_section(hlist,sec_rodata,filterlabel.name,4);
  543. cg.a_label(hlist,filterlabel);
  544. onnodecount:=tai_const.create_32bit(0);
  545. hlist.concat(onnodecount);
  546. hnode:=right;
  547. while assigned(hnode) do
  548. begin
  549. if hnode.nodetype<>onn then
  550. InternalError(2011103101);
  551. current_asmdata.getjumplabel(onlabel);
  552. sym:=current_asmdata.RefAsmSymbol(tonnode(hnode).excepttype.vmt_mangledname,AT_DATA,true);
  553. hlist.concat(tai_const.create_sym(sym));
  554. hlist.concat(tai_const.create_sym(onlabel));
  555. current_module.add_extern_asmsym(sym);
  556. cg.a_label(current_asmdata.CurrAsmList,onlabel);
  557. secondpass(hnode);
  558. inc(onnodecount.value);
  559. hnode:=tonnode(hnode).left;
  560. end;
  561. { add 'else' node to the filter list, too }
  562. if assigned(t1) then
  563. begin
  564. hlist.concat(tai_const.create_32bit(-1));
  565. hlist.concat(tai_const.create_sym(lastonlabel));
  566. inc(onnodecount.value);
  567. end;
  568. { now move filter table to permanent list all at once }
  569. current_procinfo.aktlocaldata.concatlist(hlist);
  570. hlist.free;
  571. end;
  572. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  573. if assigned(t1) then
  574. begin
  575. { here we don't have to reset flowcontrol }
  576. { the default and on flowcontrols are handled equal }
  577. secondpass(t1);
  578. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  579. if (flowcontrol*[fc_exit,fc_break,fc_continue]<>[]) then
  580. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  581. end;
  582. exceptflowcontrol:=flowcontrol;
  583. if fc_exit in exceptflowcontrol then
  584. begin
  585. { do some magic for exit in the try block }
  586. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  587. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  588. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  589. end;
  590. if fc_break in exceptflowcontrol then
  591. begin
  592. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  593. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  594. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  595. end;
  596. if fc_continue in exceptflowcontrol then
  597. begin
  598. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  599. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  600. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  601. end;
  602. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  603. errorexit:
  604. { restore all saved labels }
  605. endexceptlabel:=oldendexceptlabel;
  606. { restore the control flow labels }
  607. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  608. if assigned(oldBreakLabel) then
  609. begin
  610. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  611. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  612. end;
  613. { return all used control flow statements }
  614. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  615. tryflowcontrol - [fc_inflowcontrol]);
  616. end;
  617. initialization
  618. craisenode:=ti386raisenode;
  619. connode:=ti386onnode;
  620. ctryexceptnode:=ti386tryexceptnode;
  621. ctryfinallynode:=ti386tryfinallynode;
  622. end.