n386flw.pas 23 KB

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