n386flw.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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,_t1: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,pdecsub,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:=flowcontrol*[fc_unwind]+[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. assigned(current_procinfo.procdef.struct) and
  137. (df_generic in current_procinfo.procdef.struct.defoptions)
  138. ) then
  139. exit;
  140. finalizepi:=tcgprocinfo(cprocinfo.create(current_procinfo));
  141. finalizepi.force_nested;
  142. finalizepi.procdef:=create_finalizer_procdef;
  143. finalizepi.entrypos:=r.fileinfo;
  144. finalizepi.entryswitches:=r.localswitches;
  145. finalizepi.exitpos:=current_filepos; // last_endtoken_pos?
  146. finalizepi.exitswitches:=current_settings.localswitches;
  147. { Regvar optimization for symbols is suppressed when using exceptions, but
  148. temps may be still placed into registers. This must be fixed. }
  149. foreachnodestatic(r,@reset_regvars,finalizepi);
  150. include(finalizepi.flags,pi_has_assembler_block);
  151. include(finalizepi.flags,pi_do_call);
  152. include(finalizepi.flags,pi_uses_exceptions);
  153. end;
  154. constructor ti386tryfinallynode.create_implicit(l, r, _t1: TNode);
  155. begin
  156. inherited create_implicit(l, r, _t1);
  157. if (target_info.system<>system_i386_win32) then
  158. exit;
  159. { safecall procedures can handle implicit finalization as part of "except" flow }
  160. if implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall) then
  161. exit;
  162. if assigned(current_procinfo.procdef.struct) and
  163. (df_generic in current_procinfo.procdef.struct.defoptions) then
  164. InternalError(2013012501);
  165. finalizepi:=tcgprocinfo(cprocinfo.create(current_procinfo));
  166. finalizepi.force_nested;
  167. finalizepi.procdef:=create_finalizer_procdef;
  168. finalizepi.entrypos:=current_filepos;
  169. finalizepi.exitpos:=current_filepos; // last_endtoken_pos?
  170. finalizepi.entryswitches:=r.localswitches;
  171. finalizepi.exitswitches:=current_settings.localswitches;
  172. include(finalizepi.flags,pi_has_assembler_block);
  173. include(finalizepi.flags,pi_do_call);
  174. include(finalizepi.flags,pi_uses_exceptions);
  175. end;
  176. function ti386tryfinallynode.pass_1: tnode;
  177. var
  178. selfsym: tparavarsym;
  179. begin
  180. result:=inherited pass_1;
  181. if (target_info.system=system_i386_win32) then
  182. begin
  183. { safecall method will access 'self' from except block -> make it non-regable }
  184. if implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall) and
  185. is_class(current_procinfo.procdef.struct) then
  186. begin
  187. selfsym:=tparavarsym(current_procinfo.procdef.parast.Find('self'));
  188. if (selfsym=nil) or (selfsym.typ<>paravarsym) then
  189. InternalError(2011123101);
  190. selfsym.varregable:=vr_none;
  191. end;
  192. end;
  193. end;
  194. function ti386tryfinallynode.simplify(forinline: boolean): tnode;
  195. begin
  196. result:=inherited simplify(forinline);
  197. if (target_info.system<>system_i386_win32) then
  198. exit;
  199. if (result=nil) and assigned(finalizepi) then
  200. begin
  201. finalizepi.code:=right;
  202. foreachnodestatic(right,@copy_parasize,finalizepi);
  203. right:=ccallnode.create(nil,tprocsym(finalizepi.procdef.procsym),nil,nil,[]);
  204. firstpass(right);
  205. { For implicit frames, no actual code is available at this time,
  206. it is added later in assembler form. So store the nested procinfo
  207. for later use. }
  208. if implicitframe then
  209. begin
  210. current_procinfo.finalize_procinfo:=finalizepi;
  211. { don't leave dangling pointer }
  212. tcgprocinfo(current_procinfo).final_asmnode:=nil;
  213. end;
  214. end;
  215. end;
  216. procedure emit_scope_start(handler,data: TAsmSymbol);
  217. var
  218. href: treference;
  219. hreg: tregister;
  220. begin
  221. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  222. reference_reset_base(href,hreg,0,sizeof(pint));
  223. href.segment:=NR_FS;
  224. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  225. emit_sym(A_PUSH,S_L,data);
  226. emit_reg(A_PUSH,S_L,NR_FRAME_POINTER_REG);
  227. emit_sym(A_PUSH,S_L,handler);
  228. emit_ref(A_PUSH,S_L,href);
  229. emit_reg_ref(A_MOV,S_L,NR_ESP,href);
  230. end;
  231. procedure emit_scope_end;
  232. var
  233. href: treference;
  234. hreg,hreg2: tregister;
  235. begin
  236. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  237. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  238. reference_reset_base(href,hreg,0,sizeof(pint));
  239. href.segment:=NR_FS;
  240. emit_reg_reg(A_XOR,S_L,hreg,hreg);
  241. emit_reg(A_POP,S_L,hreg2);
  242. emit_const_reg(A_ADD,S_L,3*sizeof(pint),NR_ESP);
  243. emit_reg_ref(A_MOV,S_L,hreg2,href);
  244. end;
  245. procedure ti386tryfinallynode.pass_generate_code;
  246. var
  247. finallylabel,
  248. exceptlabel,
  249. safecalllabel,
  250. endfinallylabel,
  251. exitfinallylabel,
  252. continuefinallylabel,
  253. breakfinallylabel,
  254. oldCurrExitLabel,
  255. oldContinueLabel,
  256. oldBreakLabel : tasmlabel;
  257. oldflowcontrol,tryflowcontrol : tflowcontrol;
  258. is_safecall: boolean;
  259. hreg: tregister;
  260. begin
  261. if (target_info.system<>system_i386_win32) then
  262. begin
  263. inherited pass_generate_code;
  264. exit;
  265. end;
  266. location_reset(location,LOC_VOID,OS_NO);
  267. tryflowcontrol:=[];
  268. oldBreakLabel:=nil;
  269. oldContinueLabel:=nil;
  270. continuefinallylabel:=nil;
  271. breakfinallylabel:=nil;
  272. exceptlabel:=nil;
  273. safecalllabel:=nil;
  274. is_safecall:=implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall);
  275. { check if child nodes do a break/continue/exit }
  276. oldflowcontrol:=flowcontrol;
  277. flowcontrol:=[fc_inflowcontrol];
  278. current_asmdata.getjumplabel(finallylabel);
  279. current_asmdata.getjumplabel(endfinallylabel);
  280. { the finally block must catch break, continue and exit }
  281. { statements }
  282. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  283. if implicitframe then
  284. exitfinallylabel:=finallylabel
  285. else
  286. current_asmdata.getjumplabel(exitfinallylabel);
  287. current_procinfo.CurrExitLabel:=exitfinallylabel;
  288. if assigned(current_procinfo.CurrBreakLabel) then
  289. begin
  290. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  291. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  292. if implicitframe then
  293. begin
  294. breakfinallylabel:=finallylabel;
  295. continuefinallylabel:=finallylabel;
  296. end
  297. else
  298. begin
  299. current_asmdata.getjumplabel(breakfinallylabel);
  300. current_asmdata.getjumplabel(continuefinallylabel);
  301. end;
  302. current_procinfo.CurrContinueLabel:=continuefinallylabel;
  303. current_procinfo.CurrBreakLabel:=breakfinallylabel;
  304. end;
  305. { Start of scope }
  306. if is_safecall then
  307. begin
  308. with cg.rg[R_INTREGISTER] do
  309. used_in_proc:=used_in_proc+[RS_EBX,RS_ESI,RS_EDI];
  310. current_asmdata.getjumplabel(exceptlabel);
  311. emit_scope_start(
  312. current_asmdata.RefAsmSymbol('__FPC_except_safecall'),
  313. exceptlabel
  314. );
  315. end
  316. else
  317. emit_scope_start(
  318. current_asmdata.RefAsmSymbol('__FPC_finally_handler'),
  319. current_asmdata.RefAsmSymbol(finalizepi.procdef.mangledname)
  320. );
  321. { try code }
  322. if assigned(left) then
  323. begin
  324. secondpass(left);
  325. tryflowcontrol:=flowcontrol;
  326. if codegenerror then
  327. exit;
  328. end;
  329. { don't generate line info for internal cleanup }
  330. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  331. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  332. emit_scope_end;
  333. if is_safecall then
  334. begin
  335. current_asmdata.getjumplabel(safecalllabel);
  336. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  337. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hreg);
  338. cg.a_jmp_always(current_asmdata.CurrAsmList,safecalllabel);
  339. { RTL handler will jump here on exception }
  340. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  341. handle_safecall_exception;
  342. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_FUNCTION_RESULT_REG,hreg);
  343. cg.a_label(current_asmdata.CurrAsmList,safecalllabel);
  344. end;
  345. { end cleanup }
  346. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  347. { generate finally code as a separate procedure }
  348. { !!! this resets flowcontrol, how to check flow away? }
  349. if not implicitframe then
  350. tcgprocinfo(current_procinfo).generate_exceptfilter(finalizepi);
  351. flowcontrol:=[fc_inflowcontrol];
  352. { right is a call to finalizer procedure }
  353. secondpass(right);
  354. { goto is allowed if it stays inside the finally block,
  355. this is checked using the exception block number }
  356. if (flowcontrol-[fc_gotolabel])<>[fc_inflowcontrol] then
  357. CGMessage(cg_e_control_flow_outside_finally);
  358. if codegenerror then
  359. exit;
  360. { don't generate line info for internal cleanup }
  361. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoStart));
  362. if not implicitframe then
  363. begin
  364. if tryflowcontrol*[fc_exit,fc_break,fc_continue]<>[] then
  365. cg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  366. { do some magic for exit,break,continue in the try block }
  367. if fc_exit in tryflowcontrol then
  368. begin
  369. cg.a_label(current_asmdata.CurrAsmList,exitfinallylabel);
  370. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  371. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  372. end;
  373. if fc_break in tryflowcontrol then
  374. begin
  375. cg.a_label(current_asmdata.CurrAsmList,breakfinallylabel);
  376. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  377. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  378. end;
  379. if fc_continue in tryflowcontrol then
  380. begin
  381. cg.a_label(current_asmdata.CurrAsmList,continuefinallylabel);
  382. cg.g_call(current_asmdata.CurrAsmList,'_FPC_leave');
  383. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  384. end;
  385. end;
  386. if is_safecall then
  387. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,hreg,NR_FUNCTION_RETURN_REG);
  388. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  389. { end cleanup }
  390. current_asmdata.CurrAsmList.concat(tai_marker.create(mark_NoLineInfoEnd));
  391. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  392. if assigned(current_procinfo.CurrBreakLabel) then
  393. begin
  394. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  395. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  396. end;
  397. flowcontrol:=oldflowcontrol+(tryflowcontrol-[fc_inflowcontrol]);
  398. end;
  399. { ti386tryexceptnode }
  400. procedure ti386tryexceptnode.pass_generate_code;
  401. var
  402. exceptlabel,oldendexceptlabel,
  403. lastonlabel,
  404. exitexceptlabel,
  405. continueexceptlabel,
  406. breakexceptlabel,
  407. exittrylabel,
  408. continuetrylabel,
  409. breaktrylabel,
  410. oldCurrExitLabel,
  411. oldContinueLabel,
  412. oldBreakLabel : tasmlabel;
  413. onlabel,
  414. filterlabel: tasmlabel;
  415. oldflowcontrol,tryflowcontrol,
  416. exceptflowcontrol : tflowcontrol;
  417. hnode : tnode;
  418. hlist : tasmlist;
  419. onnodecount : tai_const;
  420. label
  421. errorexit;
  422. begin
  423. if (target_info.system<>system_i386_win32) then
  424. begin
  425. inherited pass_generate_code;
  426. exit;
  427. end;
  428. location_reset(location,LOC_VOID,OS_NO);
  429. oldflowcontrol:=flowcontrol;
  430. flowcontrol:=[fc_inflowcontrol];
  431. { this can be called recursivly }
  432. oldBreakLabel:=nil;
  433. oldContinueLabel:=nil;
  434. oldendexceptlabel:=endexceptlabel;
  435. { Win32 SEH unwinding does not preserve registers. Indicate that they are
  436. going to be destroyed. }
  437. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,[RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]);
  438. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,[RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]);
  439. { save the old labels for control flow statements }
  440. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  441. if assigned(current_procinfo.CurrBreakLabel) then
  442. begin
  443. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  444. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  445. end;
  446. { get new labels for the control flow statements }
  447. current_asmdata.getjumplabel(exittrylabel);
  448. current_asmdata.getjumplabel(exitexceptlabel);
  449. if assigned(current_procinfo.CurrBreakLabel) then
  450. begin
  451. current_asmdata.getjumplabel(breaktrylabel);
  452. current_asmdata.getjumplabel(continuetrylabel);
  453. current_asmdata.getjumplabel(breakexceptlabel);
  454. current_asmdata.getjumplabel(continueexceptlabel);
  455. end;
  456. current_asmdata.getjumplabel(exceptlabel);
  457. current_asmdata.getjumplabel(endexceptlabel);
  458. current_asmdata.getjumplabel(lastonlabel);
  459. filterlabel:=nil;
  460. { start of scope }
  461. if assigned(right) then
  462. begin
  463. current_asmdata.getdatalabel(filterlabel);
  464. emit_scope_start(
  465. current_asmdata.RefAsmSymbol('__FPC_on_handler'),
  466. filterlabel);
  467. end
  468. else
  469. emit_scope_start(
  470. current_asmdata.RefAsmSymbol('__FPC_except_handler'),
  471. exceptlabel);
  472. { set control flow labels for the try block }
  473. current_procinfo.CurrExitLabel:=exittrylabel;
  474. if assigned(oldBreakLabel) then
  475. begin
  476. current_procinfo.CurrContinueLabel:=continuetrylabel;
  477. current_procinfo.CurrBreakLabel:=breaktrylabel;
  478. end;
  479. secondpass(left);
  480. tryflowcontrol:=flowcontrol;
  481. if codegenerror then
  482. goto errorexit;
  483. emit_scope_end;
  484. { jump over except handlers }
  485. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  486. if fc_exit in tryflowcontrol then
  487. begin
  488. cg.a_label(current_asmdata.CurrAsmList,exittrylabel);
  489. emit_scope_end;
  490. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  491. end;
  492. if fc_break in tryflowcontrol then
  493. begin
  494. cg.a_label(current_asmdata.CurrAsmList,breaktrylabel);
  495. emit_scope_end;
  496. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  497. end;
  498. if fc_continue in tryflowcontrol then
  499. begin
  500. cg.a_label(current_asmdata.CurrAsmList,continuetrylabel);
  501. emit_scope_end;
  502. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  503. end;
  504. { target for catch-all handler }
  505. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  506. { set control flow labels for the except block }
  507. { and the on statements }
  508. current_procinfo.CurrExitLabel:=exitexceptlabel;
  509. if assigned(oldBreakLabel) then
  510. begin
  511. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  512. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  513. end;
  514. flowcontrol:=[fc_inflowcontrol];
  515. { on statements }
  516. if assigned(right) then
  517. begin
  518. { emit filter table to a temporary asmlist }
  519. hlist:=TAsmList.Create;
  520. new_section(hlist,sec_rodata,filterlabel.name,4);
  521. cg.a_label(hlist,filterlabel);
  522. onnodecount:=tai_const.create_32bit(0);
  523. hlist.concat(onnodecount);
  524. hnode:=right;
  525. while assigned(hnode) do
  526. begin
  527. if hnode.nodetype<>onn then
  528. InternalError(2011103101);
  529. { TODO: make it done without using global label }
  530. current_asmdata.getglobaljumplabel(onlabel);
  531. hlist.concat(tai_const.create_sym(current_asmdata.RefAsmSymbol(tonnode(hnode).excepttype.vmt_mangledname,AT_DATA)));
  532. hlist.concat(tai_const.create_sym(onlabel));
  533. cg.a_label(current_asmdata.CurrAsmList,onlabel);
  534. secondpass(hnode);
  535. inc(onnodecount.value);
  536. hnode:=tonnode(hnode).left;
  537. end;
  538. { add 'else' node to the filter list, too }
  539. if assigned(t1) then
  540. begin
  541. hlist.concat(tai_const.create_32bit(-1));
  542. hlist.concat(tai_const.create_sym(lastonlabel));
  543. inc(onnodecount.value);
  544. end;
  545. { now move filter table to permanent list all at once }
  546. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  547. current_asmdata.asmlists[al_typedconsts].concatlist(hlist);
  548. hlist.free;
  549. end;
  550. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  551. if assigned(t1) then
  552. begin
  553. { here we don't have to reset flowcontrol }
  554. { the default and on flowcontrols are handled equal }
  555. secondpass(t1);
  556. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  557. if (flowcontrol*[fc_exit,fc_break,fc_continue]<>[]) then
  558. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  559. end;
  560. exceptflowcontrol:=flowcontrol;
  561. if fc_exit in exceptflowcontrol then
  562. begin
  563. { do some magic for exit in the try block }
  564. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  565. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  566. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  567. end;
  568. if fc_break in exceptflowcontrol then
  569. begin
  570. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  571. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  572. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  573. end;
  574. if fc_continue in exceptflowcontrol then
  575. begin
  576. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  577. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  578. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  579. end;
  580. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  581. errorexit:
  582. { restore all saved labels }
  583. endexceptlabel:=oldendexceptlabel;
  584. { restore the control flow labels }
  585. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  586. if assigned(oldBreakLabel) then
  587. begin
  588. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  589. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  590. end;
  591. { return all used control flow statements }
  592. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  593. tryflowcontrol - [fc_inflowcontrol]);
  594. end;
  595. initialization
  596. craisenode:=ti386raisenode;
  597. connode:=ti386onnode;
  598. ctryexceptnode:=ti386tryexceptnode;
  599. ctryfinallynode:=ti386tryfinallynode;
  600. end.