n386flw.pas 23 KB

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