n386flw.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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,
  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. end;
  118. result:=fen_true;
  119. end;
  120. function copy_parasize(var n: tnode; arg: pointer): foreachnoderesult;
  121. begin
  122. case n.nodetype of
  123. calln:
  124. tcgprocinfo(arg).allocate_push_parasize(tcallnode(n).pushed_parasize);
  125. end;
  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(2011123101);
  176. selfsym.varregable:=vr_none;
  177. end;
  178. end;
  179. end;
  180. function ti386tryfinallynode.simplify(forinline: boolean): tnode;
  181. begin
  182. result:=inherited simplify(forinline);
  183. if (target_info.system<>system_i386_win32) then
  184. exit;
  185. if (result=nil) and assigned(finalizepi) then
  186. begin
  187. finalizepi.code:=right;
  188. foreachnodestatic(right,@copy_parasize,finalizepi);
  189. right:=ccallnode.create(nil,tprocsym(finalizepi.procdef.procsym),nil,nil,[],nil);
  190. firstpass(right);
  191. { For implicit frames, no actual code is available at this time,
  192. it is added later in assembler form. So store the nested procinfo
  193. for later use. }
  194. if implicitframe then
  195. begin
  196. current_procinfo.finalize_procinfo:=finalizepi;
  197. { don't leave dangling pointer }
  198. tcgprocinfo(current_procinfo).final_asmnode:=nil;
  199. end;
  200. end;
  201. end;
  202. procedure emit_scope_start(handler,data: TAsmSymbol);
  203. var
  204. href: treference;
  205. hreg: tregister;
  206. begin
  207. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  208. reference_reset_base(href,hreg,0,ctempposinvalid,sizeof(pint),[]);
  209. href.segment:=NR_FS;
  210. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  211. emit_sym(A_PUSH,S_L,data);
  212. emit_reg(A_PUSH,S_L,NR_FRAME_POINTER_REG);
  213. emit_sym(A_PUSH,S_L,handler);
  214. emit_ref(A_PUSH,S_L,href);
  215. emit_reg_ref(A_MOV,S_L,NR_ESP,href);
  216. end;
  217. procedure emit_scope_end;
  218. var
  219. href: treference;
  220. hreg,hreg2: tregister;
  221. begin
  222. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  223. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  224. reference_reset_base(href,hreg,0,ctempposinvalid,sizeof(pint),[]);
  225. href.segment:=NR_FS;
  226. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  227. emit_reg(A_POP,S_L,hreg2);
  228. emit_const_reg(A_ADD,S_L,3*sizeof(pint),NR_ESP);
  229. emit_reg_ref(A_MOV,S_L,hreg2,href);
  230. end;
  231. procedure ti386tryfinallynode.pass_generate_code;
  232. var
  233. finallylabel,
  234. exceptlabel,
  235. safecalllabel,
  236. endfinallylabel,
  237. exitfinallylabel,
  238. continuefinallylabel,
  239. breakfinallylabel,
  240. oldCurrExitLabel,
  241. oldContinueLabel,
  242. oldBreakLabel : tasmlabel;
  243. oldflowcontrol,tryflowcontrol : tflowcontrol;
  244. is_safecall: boolean;
  245. hreg: tregister;
  246. begin
  247. if (target_info.system<>system_i386_win32) then
  248. begin
  249. inherited pass_generate_code;
  250. exit;
  251. end;
  252. location_reset(location,LOC_VOID,OS_NO);
  253. tryflowcontrol:=[];
  254. oldBreakLabel:=nil;
  255. oldContinueLabel:=nil;
  256. continuefinallylabel:=nil;
  257. breakfinallylabel:=nil;
  258. exceptlabel:=nil;
  259. safecalllabel:=nil;
  260. hreg:=NR_NO;
  261. is_safecall:=implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall);
  262. { check if child nodes do a break/continue/exit }
  263. oldflowcontrol:=flowcontrol;
  264. flowcontrol:=[fc_inflowcontrol];
  265. current_asmdata.getjumplabel(finallylabel);
  266. current_asmdata.getjumplabel(endfinallylabel);
  267. { the finally block must catch break, continue and exit }
  268. { statements }
  269. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  270. if implicitframe then
  271. exitfinallylabel:=finallylabel
  272. else
  273. current_asmdata.getjumplabel(exitfinallylabel);
  274. current_procinfo.CurrExitLabel:=exitfinallylabel;
  275. if assigned(current_procinfo.CurrBreakLabel) then
  276. begin
  277. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  278. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  279. if implicitframe then
  280. begin
  281. breakfinallylabel:=finallylabel;
  282. continuefinallylabel:=finallylabel;
  283. end
  284. else
  285. begin
  286. current_asmdata.getjumplabel(breakfinallylabel);
  287. current_asmdata.getjumplabel(continuefinallylabel);
  288. end;
  289. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  290. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  291. end;
  292. { Start of scope }
  293. if is_safecall then
  294. begin
  295. with cg.rg[R_INTREGISTER] do
  296. used_in_proc:=used_in_proc+[RS_EBX,RS_ESI,RS_EDI];
  297. current_asmdata.getjumplabel(exceptlabel);
  298. emit_scope_start(
  299. current_asmdata.RefAsmSymbol('__FPC_except_safecall',AT_FUNCTION),
  300. exceptlabel
  301. );
  302. end
  303. else
  304. emit_scope_start(
  305. current_asmdata.RefAsmSymbol('__FPC_finally_handler',AT_FUNCTION),
  306. current_asmdata.RefAsmSymbol(finalizepi.procdef.mangledname,AT_FUNCTION)
  307. );
  308. { try code }
  309. if assigned(left) then
  310. begin
  311. secondpass(left);
  312. tryflowcontrol:=flowcontrol;
  313. if codegenerror then
  314. exit;
  315. end;
  316. { don't generate line info for internal cleanup }
  317. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  318. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  319. emit_scope_end;
  320. if is_safecall then
  321. begin
  322. current_asmdata.getjumplabel(safecalllabel);
  323. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  324. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hreg);
  325. cg.a_jmp_always(current_asmdata.CurrAsmList,safecalllabel);
  326. { RTL handler will jump here on exception }
  327. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  328. handle_safecall_exception;
  329. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_FUNCTION_RESULT_REG,hreg);
  330. cg.a_label(current_asmdata.CurrAsmList,safecalllabel);
  331. end;
  332. { end cleanup }
  333. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  334. { generate finally code as a separate procedure }
  335. { !!! this resets flowcontrol, how to check flow away? }
  336. if not implicitframe then
  337. tcgprocinfo(current_procinfo).generate_exceptfilter(finalizepi);
  338. flowcontrol:=[fc_inflowcontrol];
  339. { right is a call to finalizer procedure }
  340. secondpass(right);
  341. { goto is allowed if it stays inside the finally block,
  342. this is checked using the exception block number }
  343. if (flowcontrol-[fc_gotolabel])<>[fc_inflowcontrol] then
  344. CGMessage(cg_e_control_flow_outside_finally);
  345. if codegenerror then
  346. exit;
  347. { don't generate line info for internal cleanup }
  348. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  349. if not implicitframe then
  350. begin
  351. if tryflowcontrol*[fc_exit,fc_break,fc_continue]<>[] then
  352. cg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  353. { do some magic for exit,break,continue in the try block }
  354. if fc_exit in tryflowcontrol then
  355. begin
  356. cg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  357. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  358. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  359. end;
  360. if fc_break in tryflowcontrol then
  361. begin
  362. cg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  363. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  364. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  365. end;
  366. if fc_continue in tryflowcontrol then
  367. begin
  368. cg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  369. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  370. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  371. end;
  372. end;
  373. if is_safecall then
  374. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,hreg,NR_FUNCTION_RETURN_REG);
  375. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  376. { end cleanup }
  377. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  378. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  379. if assigned(current_procinfo.CurrBreakLabel) then
  380. begin
  381. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  382. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  383. end;
  384. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  385. end;
  386. { ti386tryexceptnode }
  387. procedure ti386tryexceptnode.pass_generate_code;
  388. var
  389. exceptlabel,oldendexceptlabel,
  390. lastonlabel,
  391. exitexceptlabel,
  392. continueexceptlabel,
  393. breakexceptlabel,
  394. exittrylabel,
  395. continuetrylabel,
  396. breaktrylabel,
  397. oldCurrExitLabel,
  398. oldContinueLabel,
  399. oldBreakLabel : tasmlabel;
  400. onlabel,
  401. filterlabel: tasmlabel;
  402. oldflowcontrol,tryflowcontrol,
  403. exceptflowcontrol : tflowcontrol;
  404. hnode : tnode;
  405. hlist : tasmlist;
  406. onnodecount : tai_const;
  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. emit_scope_start(
  457. current_asmdata.RefAsmSymbol('__FPC_on_handler',AT_FUNCTION),
  458. filterlabel);
  459. end
  460. else
  461. emit_scope_start(
  462. current_asmdata.RefAsmSymbol('__FPC_except_handler',AT_FUNCTION),
  463. exceptlabel);
  464. { set control flow labels for the try block }
  465. current_procinfo.CurrExitLabel:=exittrylabel;
  466. if assigned(oldBreakLabel) then
  467. begin
  468. current_procinfo.CurrContinueLabel:=continuetrylabel;
  469. current_procinfo.CurrBreakLabel:=breaktrylabel;
  470. end;
  471. secondpass(left);
  472. tryflowcontrol:=flowcontrol;
  473. if codegenerror then
  474. goto errorexit;
  475. emit_scope_end;
  476. { jump over except handlers }
  477. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  478. if fc_exit in tryflowcontrol then
  479. begin
  480. cg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  481. emit_scope_end;
  482. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  483. end;
  484. if fc_break in tryflowcontrol then
  485. begin
  486. cg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  487. emit_scope_end;
  488. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  489. end;
  490. if fc_continue in tryflowcontrol then
  491. begin
  492. cg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  493. emit_scope_end;
  494. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  495. end;
  496. { target for catch-all handler }
  497. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  498. { set control flow labels for the except block }
  499. { and the on statements }
  500. current_procinfo.CurrExitLabel:=exitexceptlabel;
  501. if assigned(oldBreakLabel) then
  502. begin
  503. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  504. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  505. end;
  506. flowcontrol:=[fc_inflowcontrol];
  507. { on statements }
  508. if assigned(right) then
  509. begin
  510. { emit filter table to a temporary asmlist }
  511. hlist:=TAsmList.Create;
  512. new_section(hlist,sec_rodata,filterlabel.name,4);
  513. cg.a_label(hlist,filterlabel);
  514. onnodecount:=tai_const.create_32bit(0);
  515. hlist.concat(onnodecount);
  516. hnode:=right;
  517. while assigned(hnode) do
  518. begin
  519. if hnode.nodetype<>onn then
  520. InternalError(2011103101);
  521. current_asmdata.getjumplabel(onlabel);
  522. hlist.concat(tai_const.create_sym(current_asmdata.RefAsmSymbol(tonnode(hnode).excepttype.vmt_mangledname,AT_DATA)));
  523. hlist.concat(tai_const.create_sym(onlabel));
  524. cg.a_label(current_asmdata.CurrAsmList,onlabel);
  525. secondpass(hnode);
  526. inc(onnodecount.value);
  527. hnode:=tonnode(hnode).left;
  528. end;
  529. { add 'else' node to the filter list, too }
  530. if assigned(t1) then
  531. begin
  532. hlist.concat(tai_const.create_32bit(-1));
  533. hlist.concat(tai_const.create_sym(lastonlabel));
  534. inc(onnodecount.value);
  535. end;
  536. { now move filter table to permanent list all at once }
  537. current_procinfo.aktlocaldata.concatlist(hlist);
  538. hlist.free;
  539. end;
  540. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  541. if assigned(t1) then
  542. begin
  543. { here we don't have to reset flowcontrol }
  544. { the default and on flowcontrols are handled equal }
  545. secondpass(t1);
  546. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  547. if (flowcontrol*[fc_exit,fc_break,fc_continue]<>[]) then
  548. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  549. end;
  550. exceptflowcontrol:=flowcontrol;
  551. if fc_exit in exceptflowcontrol then
  552. begin
  553. { do some magic for exit in the try block }
  554. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  555. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  556. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  557. end;
  558. if fc_break in exceptflowcontrol then
  559. begin
  560. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  561. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  562. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  563. end;
  564. if fc_continue in exceptflowcontrol then
  565. begin
  566. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  567. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  568. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  569. end;
  570. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  571. errorexit:
  572. { restore all saved labels }
  573. endexceptlabel:=oldendexceptlabel;
  574. { restore the control flow labels }
  575. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  576. if assigned(oldBreakLabel) then
  577. begin
  578. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  579. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  580. end;
  581. { return all used control flow statements }
  582. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  583. tryflowcontrol - [fc_inflowcontrol]);
  584. end;
  585. initialization
  586. craisenode:=ti386raisenode;
  587. connode:=ti386onnode;
  588. ctryexceptnode:=ti386tryexceptnode;
  589. ctryfinallynode:=ti386tryfinallynode;
  590. end.