n386flw.pas 23 KB

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