nx64flw.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. {
  2. Copyright (c) 2011 by Free Pascal development team
  3. Generate Win64-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 nx64flw;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nflw,ncgflw,psub;
  22. type
  23. tx64raisenode=class(tcgraisenode)
  24. function pass_1 : tnode;override;
  25. end;
  26. tx64onnode=class(tcgonnode)
  27. procedure pass_generate_code;override;
  28. end;
  29. tx64tryexceptnode=class(tcgtryexceptnode)
  30. procedure pass_generate_code;override;
  31. end;
  32. tx64tryfinallynode=class(tcgtryfinallynode)
  33. finalizepi: tcgprocinfo;
  34. constructor create(l,r:TNode);override;
  35. constructor create_implicit(l,r:TNode);override;
  36. function simplify(forinline: boolean): tnode;override;
  37. procedure pass_generate_code;override;
  38. end;
  39. implementation
  40. uses
  41. globtype,globals,verbose,systems,
  42. nbas,ncal,nutils,
  43. symconst,symsym,symdef,
  44. cgbase,cgobj,cgutils,tgobj,
  45. cpubase,htypechk,
  46. pass_1,pass_2,
  47. aasmbase,aasmtai,aasmdata,aasmcpu,procinfo,cpupi;
  48. var
  49. endexceptlabel: tasmlabel;
  50. { tx64raisenode }
  51. function tx64raisenode.pass_1 : tnode;
  52. var
  53. statements : tstatementnode;
  54. raisenode : tcallnode;
  55. begin
  56. { difference from generic code is that address stack is not popped on reraise }
  57. if (target_info.system<>system_x86_64_win64) or assigned(left) then
  58. result:=inherited pass_1
  59. else
  60. begin
  61. result:=internalstatements(statements);
  62. raisenode:=ccallnode.createintern('fpc_reraise',nil);
  63. include(raisenode.callnodeflags,cnf_call_never_returns);
  64. addstatement(statements,raisenode);
  65. end;
  66. end;
  67. { tx64onnode }
  68. procedure tx64onnode.pass_generate_code;
  69. var
  70. exceptvarsym : tlocalvarsym;
  71. begin
  72. if (target_info.system<>system_x86_64_win64) then
  73. begin
  74. inherited pass_generate_code;
  75. exit;
  76. end;
  77. location_reset(location,LOC_VOID,OS_NO);
  78. { RTL will put exceptobject into RAX when jumping here }
  79. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  80. { Retrieve exception variable }
  81. if assigned(excepTSymtable) then
  82. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  83. else
  84. exceptvarsym:=nil;
  85. if assigned(exceptvarsym) then
  86. begin
  87. exceptvarsym.localloc.loc:=LOC_REFERENCE;
  88. exceptvarsym.localloc.size:=OS_ADDR;
  89. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),voidpointertype,exceptvarsym.localloc.reference);
  90. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptvarsym.localloc.reference);
  91. end;
  92. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  93. if assigned(right) then
  94. secondpass(right);
  95. { deallocate exception symbol }
  96. if assigned(exceptvarsym) then
  97. begin
  98. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  99. exceptvarsym.localloc.loc:=LOC_INVALID;
  100. end;
  101. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  102. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  103. end;
  104. { tx64tryfinallynode }
  105. function reset_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  106. begin
  107. case n.nodetype of
  108. temprefn:
  109. make_not_regable(n,[]);
  110. calln:
  111. include(tprocinfo(arg).flags,pi_do_call);
  112. end;
  113. result:=fen_true;
  114. end;
  115. function copy_parasize(var n: tnode; arg: pointer): foreachnoderesult;
  116. begin
  117. case n.nodetype of
  118. calln:
  119. tcgprocinfo(arg).allocate_push_parasize(tcallnode(n).pushed_parasize);
  120. end;
  121. result:=fen_true;
  122. end;
  123. constructor tx64tryfinallynode.create(l, r: TNode);
  124. begin
  125. inherited create(l,r);
  126. if (target_info.system=system_x86_64_win64) and
  127. { Don't create child procedures for generic methods, their nested-like
  128. behavior causes compilation errors because real nested procedures
  129. aren't allowed for generics. Not creating them doesn't harm because
  130. generic node tree is discarded without generating code. }
  131. not (df_generic in current_procinfo.procdef.defoptions) then
  132. begin
  133. finalizepi:=tcgprocinfo(current_procinfo.create_for_outlining('$fin$',current_procinfo.procdef.struct,potype_exceptfilter,voidtype,r));
  134. { the init/final code is messing with asm nodes, so inform the compiler about this }
  135. include(finalizepi.flags,pi_has_assembler_block);
  136. { Regvar optimization for symbols is suppressed when using exceptions, but
  137. temps may be still placed into registers. This must be fixed. }
  138. foreachnodestatic(r,@reset_regvars,finalizepi);
  139. end;
  140. end;
  141. constructor tx64tryfinallynode.create_implicit(l, r: TNode);
  142. begin
  143. inherited create_implicit(l, r);
  144. if (target_info.system=system_x86_64_win64) then
  145. begin
  146. if df_generic in current_procinfo.procdef.defoptions then
  147. InternalError(2013012501);
  148. finalizepi:=tcgprocinfo(current_procinfo.create_for_outlining('$fin$',current_procinfo.procdef.struct,potype_exceptfilter,voidtype,r));
  149. include(finalizepi.flags,pi_do_call);
  150. { the init/final code is messing with asm nodes, so inform the compiler about this }
  151. include(finalizepi.flags,pi_has_assembler_block);
  152. finalizepi.allocate_push_parasize(32);
  153. end;
  154. end;
  155. function tx64tryfinallynode.simplify(forinline: boolean): tnode;
  156. begin
  157. result:=inherited simplify(forinline);
  158. if (target_info.system<>system_x86_64_win64) then
  159. exit;
  160. if (result=nil) then
  161. begin
  162. finalizepi.code:=right;
  163. foreachnodestatic(right,@copy_parasize,finalizepi);
  164. right:=ccallnode.create(nil,tprocsym(finalizepi.procdef.procsym),nil,nil,[],nil);
  165. firstpass(right);
  166. { For implicit frames, no actual code is available at this time,
  167. it is added later in assembler form. So store the nested procinfo
  168. for later use. }
  169. if implicitframe then
  170. begin
  171. current_procinfo.finalize_procinfo:=finalizepi;
  172. end;
  173. end;
  174. end;
  175. procedure emit_nop;
  176. var
  177. dummy: TAsmLabel;
  178. begin
  179. { To avoid optimizing away the whole thing, prepend a jumplabel with increased refcount }
  180. current_asmdata.getjumplabel(dummy);
  181. dummy.increfs;
  182. cg.a_label(current_asmdata.CurrAsmList,dummy);
  183. current_asmdata.CurrAsmList.concat(Taicpu.op_none(A_NOP,S_NO));
  184. end;
  185. procedure tx64tryfinallynode.pass_generate_code;
  186. var
  187. trylabel,
  188. endtrylabel,
  189. finallylabel,
  190. endfinallylabel,
  191. oldexitlabel: tasmlabel;
  192. oldflowcontrol: tflowcontrol;
  193. catch_frame: boolean;
  194. begin
  195. if (target_info.system<>system_x86_64_win64) then
  196. begin
  197. inherited pass_generate_code;
  198. exit;
  199. end;
  200. location_reset(location,LOC_VOID,OS_NO);
  201. { Do not generate a frame that catches exceptions if the only action
  202. would be reraising it. Doing so is extremely inefficient with SEH
  203. (in contrast with setjmp/longjmp exception handling) }
  204. catch_frame:=implicitframe and
  205. (current_procinfo.procdef.proccalloption=pocall_safecall);
  206. oldflowcontrol:=flowcontrol;
  207. flowcontrol:=[fc_inflowcontrol];
  208. current_asmdata.getjumplabel(trylabel);
  209. current_asmdata.getjumplabel(endtrylabel);
  210. current_asmdata.getjumplabel(finallylabel);
  211. current_asmdata.getjumplabel(endfinallylabel);
  212. oldexitlabel:=current_procinfo.CurrExitLabel;
  213. if implicitframe then
  214. current_procinfo.CurrExitLabel:=finallylabel;
  215. { Start of scope }
  216. { Padding with NOP is necessary here because exceptions in called
  217. procedures are seen at the next instruction, while CPU/OS exceptions
  218. like AV are seen at the current instruction.
  219. So in the following code
  220. raise_some_exception; //(a)
  221. try
  222. pchar(nil)^:='0'; //(b)
  223. ...
  224. without NOP, exceptions (a) and (b) will be seen at the same address
  225. and fall into the same scope. However they should be seen in different scopes.
  226. }
  227. emit_nop;
  228. cg.a_label(current_asmdata.CurrAsmList,trylabel);
  229. { try code }
  230. if assigned(left) then
  231. begin
  232. { fc_unwind_xx tells exit/continue/break statements to emit special
  233. unwind code instead of just JMP }
  234. if not implicitframe then
  235. flowcontrol:=flowcontrol+[fc_catching_exceptions,fc_unwind_exit,fc_unwind_loop];
  236. secondpass(left);
  237. flowcontrol:=flowcontrol-[fc_catching_exceptions,fc_unwind_exit,fc_unwind_loop];
  238. if codegenerror then
  239. exit;
  240. end;
  241. { If the immediately preceding instruction is CALL,
  242. its return address must not end up outside the scope, so pad with NOP. }
  243. if catch_frame then
  244. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel)
  245. else
  246. emit_nop;
  247. cg.a_label(current_asmdata.CurrAsmList,endtrylabel);
  248. { Handle the except block first, so endtrylabel serves both
  249. as end of scope and as unwind target. This way it is possible to
  250. encode everything into a single scope record. }
  251. if catch_frame then
  252. begin
  253. if (current_procinfo.procdef.proccalloption=pocall_safecall) then
  254. begin
  255. handle_safecall_exception;
  256. cg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  257. end
  258. else
  259. InternalError(2014031601);
  260. end;
  261. flowcontrol:=[fc_inflowcontrol];
  262. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  263. { generate finally code as a separate procedure }
  264. if not implicitframe then
  265. tcgprocinfo(current_procinfo).generate_exceptfilter(finalizepi);
  266. { right is a call to finalizer procedure }
  267. secondpass(right);
  268. if codegenerror then
  269. exit;
  270. { normal exit from safecall proc must zero the result register }
  271. if implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall) then
  272. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,NR_FUNCTION_RESULT_REG);
  273. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  274. { generate the scope record in .xdata }
  275. tcpuprocinfo(current_procinfo).add_finally_scope(trylabel,endtrylabel,
  276. current_asmdata.RefAsmSymbol(finalizepi.procdef.mangledname,AT_FUNCTION),catch_frame);
  277. if implicitframe then
  278. current_procinfo.CurrExitLabel:=oldexitlabel;
  279. flowcontrol:=oldflowcontrol;
  280. end;
  281. { tx64tryexceptnode }
  282. procedure tx64tryexceptnode.pass_generate_code;
  283. var
  284. trylabel,
  285. exceptlabel,oldendexceptlabel,
  286. lastonlabel,
  287. exitexceptlabel,
  288. continueexceptlabel,
  289. breakexceptlabel,
  290. oldCurrExitLabel,
  291. oldContinueLabel,
  292. oldBreakLabel : tasmlabel;
  293. onlabel,
  294. filterlabel: tasmlabel;
  295. oldflowcontrol,tryflowcontrol,
  296. exceptflowcontrol : tflowcontrol;
  297. hnode : tnode;
  298. hlist : tasmlist;
  299. onnodecount : tai_const;
  300. label
  301. errorexit;
  302. begin
  303. if (target_info.system<>system_x86_64_win64) then
  304. begin
  305. inherited pass_generate_code;
  306. exit;
  307. end;
  308. location_reset(location,LOC_VOID,OS_NO);
  309. oldflowcontrol:=flowcontrol;
  310. exceptflowcontrol:=[];
  311. continueexceptlabel:=nil;
  312. breakexceptlabel:=nil;
  313. include(flowcontrol,fc_inflowcontrol);
  314. { this can be called recursivly }
  315. oldBreakLabel:=nil;
  316. oldContinueLabel:=nil;
  317. oldendexceptlabel:=endexceptlabel;
  318. { save the old labels for control flow statements }
  319. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  320. current_asmdata.getjumplabel(exitexceptlabel);
  321. if assigned(current_procinfo.CurrBreakLabel) then
  322. begin
  323. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  324. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  325. current_asmdata.getjumplabel(breakexceptlabel);
  326. current_asmdata.getjumplabel(continueexceptlabel);
  327. end;
  328. current_asmdata.getjumplabel(exceptlabel);
  329. current_asmdata.getjumplabel(endexceptlabel);
  330. current_asmdata.getjumplabel(lastonlabel);
  331. filterlabel:=nil;
  332. { start of scope }
  333. current_asmdata.getjumplabel(trylabel);
  334. emit_nop;
  335. cg.a_label(current_asmdata.CurrAsmList,trylabel);
  336. { control flow in try block needs no special handling,
  337. just make sure that target labels are outside the scope }
  338. secondpass(left);
  339. tryflowcontrol:=flowcontrol;
  340. if codegenerror then
  341. goto errorexit;
  342. { jump over except handlers }
  343. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  344. { end of scope }
  345. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  346. { set control flow labels for the except block }
  347. { and the on statements }
  348. current_procinfo.CurrExitLabel:=exitexceptlabel;
  349. if assigned(oldBreakLabel) then
  350. begin
  351. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  352. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  353. end;
  354. flowcontrol:=[fc_inflowcontrol];
  355. { on statements }
  356. if assigned(right) then
  357. begin
  358. { emit filter table to a temporary asmlist }
  359. hlist:=TAsmList.Create;
  360. current_asmdata.getaddrlabel(filterlabel);
  361. new_section(hlist,sec_rodata_norel,filterlabel.name,4);
  362. cg.a_label(hlist,filterlabel);
  363. onnodecount:=tai_const.create_32bit(0);
  364. hlist.concat(onnodecount);
  365. hnode:=right;
  366. while assigned(hnode) do
  367. begin
  368. if hnode.nodetype<>onn then
  369. InternalError(2011103101);
  370. current_asmdata.getjumplabel(onlabel);
  371. hlist.concat(tai_const.create_rva_sym(current_asmdata.RefAsmSymbol(tonnode(hnode).excepttype.vmt_mangledname,AT_DATA)));
  372. hlist.concat(tai_const.create_rva_sym(onlabel));
  373. cg.a_label(current_asmdata.CurrAsmList,onlabel);
  374. secondpass(hnode);
  375. inc(onnodecount.value);
  376. hnode:=tonnode(hnode).left;
  377. end;
  378. { add 'else' node to the filter list, too }
  379. if assigned(t1) then
  380. begin
  381. hlist.concat(tai_const.create_32bit(-1));
  382. hlist.concat(tai_const.create_rva_sym(lastonlabel));
  383. inc(onnodecount.value);
  384. end;
  385. { now move filter table to permanent list all at once }
  386. current_procinfo.aktlocaldata.concatlist(hlist);
  387. hlist.free;
  388. end;
  389. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  390. if assigned(t1) then
  391. begin
  392. { here we don't have to reset flowcontrol }
  393. { the default and on flowcontrols are handled equal }
  394. secondpass(t1);
  395. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  396. if (flowcontrol*[fc_exit,fc_break,fc_continue]<>[]) then
  397. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  398. end;
  399. exceptflowcontrol:=flowcontrol;
  400. if fc_exit in exceptflowcontrol then
  401. begin
  402. { do some magic for exit in the try block }
  403. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  404. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  405. if (fc_unwind_exit in oldflowcontrol) then
  406. cg.g_local_unwind(current_asmdata.CurrAsmList,oldCurrExitLabel)
  407. else
  408. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  409. end;
  410. if fc_break in exceptflowcontrol then
  411. begin
  412. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  413. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  414. if (fc_unwind_loop in oldflowcontrol) then
  415. cg.g_local_unwind(current_asmdata.CurrAsmList,oldBreakLabel)
  416. else
  417. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  418. end;
  419. if fc_continue in exceptflowcontrol then
  420. begin
  421. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  422. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  423. if (fc_unwind_loop in oldflowcontrol) then
  424. cg.g_local_unwind(current_asmdata.CurrAsmList,oldContinueLabel)
  425. else
  426. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  427. end;
  428. emit_nop;
  429. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  430. tcpuprocinfo(current_procinfo).add_except_scope(trylabel,exceptlabel,endexceptlabel,filterlabel);
  431. errorexit:
  432. { restore all saved labels }
  433. endexceptlabel:=oldendexceptlabel;
  434. { restore the control flow labels }
  435. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  436. if assigned(oldBreakLabel) then
  437. begin
  438. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  439. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  440. end;
  441. { return all used control flow statements }
  442. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  443. tryflowcontrol - [fc_inflowcontrol]);
  444. end;
  445. initialization
  446. craisenode:=tx64raisenode;
  447. connode:=tx64onnode;
  448. ctryexceptnode:=tx64tryexceptnode;
  449. ctryfinallynode:=tx64tryfinallynode;
  450. end.