nx64flw.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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,_t1:TNode);override;
  36. function simplify(forinline: boolean): tnode;override;
  37. procedure pass_generate_code;override;
  38. end;
  39. implementation
  40. uses
  41. cutils,globtype,globals,verbose,systems,
  42. nbas,ncal,nmem,nutils,
  43. symconst,symbase,symtable,symsym,symdef,
  44. cgbase,cgobj,cgcpu,cgutils,tgobj,
  45. cpubase,htypechk,
  46. parabase,paramgr,pdecsub,pass_1,pass_2,ncgutil,cga,
  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. (
  128. { Don't create child procedures for generic methods, their nested-like
  129. behavior causes compilation errors because real nested procedures
  130. aren't allowed for generics. Not creating them doesn't harm because
  131. generic node tree is discarded without generating code. }
  132. not assigned(current_procinfo.procdef.struct) or
  133. not(df_generic in current_procinfo.procdef.struct.defoptions)
  134. ) then
  135. begin
  136. finalizepi:=tcgprocinfo(cprocinfo.create(current_procinfo));
  137. finalizepi.force_nested;
  138. finalizepi.procdef:=create_finalizer_procdef;
  139. finalizepi.entrypos:=r.fileinfo;
  140. finalizepi.entryswitches:=r.localswitches;
  141. finalizepi.exitpos:=current_filepos; // last_endtoken_pos?
  142. finalizepi.exitswitches:=current_settings.localswitches;
  143. { the init/final code is messing with asm nodes, so inform the compiler about this }
  144. include(finalizepi.flags,pi_has_assembler_block);
  145. { Regvar optimization for symbols is suppressed when using exceptions, but
  146. temps may be still placed into registers. This must be fixed. }
  147. foreachnodestatic(r,@reset_regvars,finalizepi);
  148. end;
  149. end;
  150. constructor tx64tryfinallynode.create_implicit(l, r, _t1: TNode);
  151. begin
  152. inherited create_implicit(l, r, _t1);
  153. if (target_info.system=system_x86_64_win64) then
  154. begin
  155. if assigned(current_procinfo.procdef.struct) and
  156. (df_generic in current_procinfo.procdef.struct.defoptions) then
  157. InternalError(2013012501);
  158. finalizepi:=tcgprocinfo(cprocinfo.create(current_procinfo));
  159. finalizepi.force_nested;
  160. finalizepi.procdef:=create_finalizer_procdef;
  161. finalizepi.entrypos:=current_filepos;
  162. finalizepi.exitpos:=current_filepos; // last_endtoken_pos?
  163. finalizepi.entryswitches:=r.localswitches;
  164. finalizepi.exitswitches:=current_settings.localswitches;
  165. include(finalizepi.flags,pi_do_call);
  166. { the init/final code is messing with asm nodes, so inform the compiler about this }
  167. include(finalizepi.flags,pi_has_assembler_block);
  168. finalizepi.allocate_push_parasize(32);
  169. end;
  170. end;
  171. function tx64tryfinallynode.simplify(forinline: boolean): tnode;
  172. begin
  173. result:=inherited simplify(forinline);
  174. if (target_info.system<>system_x86_64_win64) then
  175. exit;
  176. if (result=nil) then
  177. begin
  178. finalizepi.code:=right;
  179. foreachnodestatic(right,@copy_parasize,finalizepi);
  180. right:=ccallnode.create(nil,tprocsym(finalizepi.procdef.procsym),nil,nil,[],nil);
  181. firstpass(right);
  182. { For implicit frames, no actual code is available at this time,
  183. it is added later in assembler form. So store the nested procinfo
  184. for later use. }
  185. if implicitframe then
  186. begin
  187. current_procinfo.finalize_procinfo:=finalizepi;
  188. { don't leave dangling pointer }
  189. tcgprocinfo(current_procinfo).final_asmnode:=nil;
  190. end;
  191. end;
  192. end;
  193. procedure emit_nop;
  194. var
  195. dummy: TAsmLabel;
  196. begin
  197. { To avoid optimizing away the whole thing, prepend a jumplabel with increased refcount }
  198. current_asmdata.getjumplabel(dummy);
  199. dummy.increfs;
  200. cg.a_label(current_asmdata.CurrAsmList,dummy);
  201. current_asmdata.CurrAsmList.concat(Taicpu.op_none(A_NOP,S_NO));
  202. end;
  203. procedure tx64tryfinallynode.pass_generate_code;
  204. var
  205. trylabel,
  206. endtrylabel,
  207. finallylabel,
  208. endfinallylabel,
  209. oldexitlabel: tasmlabel;
  210. oldflowcontrol: tflowcontrol;
  211. catch_frame: boolean;
  212. begin
  213. if (target_info.system<>system_x86_64_win64) then
  214. begin
  215. inherited pass_generate_code;
  216. exit;
  217. end;
  218. location_reset(location,LOC_VOID,OS_NO);
  219. { Do not generate a frame that catches exceptions if the only action
  220. would be reraising it. Doing so is extremely inefficient with SEH
  221. (in contrast with setjmp/longjmp exception handling) }
  222. catch_frame:=implicitframe and ((not has_no_code(t1)) or
  223. (current_procinfo.procdef.proccalloption=pocall_safecall));
  224. oldflowcontrol:=flowcontrol;
  225. flowcontrol:=[fc_inflowcontrol];
  226. current_asmdata.getjumplabel(trylabel);
  227. current_asmdata.getjumplabel(endtrylabel);
  228. current_asmdata.getjumplabel(finallylabel);
  229. current_asmdata.getjumplabel(endfinallylabel);
  230. oldexitlabel:=current_procinfo.CurrExitLabel;
  231. if implicitframe then
  232. current_procinfo.CurrExitLabel:=finallylabel;
  233. { Start of scope }
  234. { Padding with NOP is necessary here because exceptions in called
  235. procedures are seen at the next instruction, while CPU/OS exceptions
  236. like AV are seen at the current instruction.
  237. So in the following code
  238. raise_some_exception; //(a)
  239. try
  240. pchar(nil)^:='0'; //(b)
  241. ...
  242. without NOP, exceptions (a) and (b) will be seen at the same address
  243. and fall into the same scope. However they should be seen in different scopes.
  244. }
  245. emit_nop;
  246. cg.a_label(current_asmdata.CurrAsmList,trylabel);
  247. { try code }
  248. if assigned(left) then
  249. begin
  250. { fc_unwind_xx tells exit/continue/break statements to emit special
  251. unwind code instead of just JMP }
  252. if not implicitframe then
  253. flowcontrol:=flowcontrol+[fc_unwind_exit,fc_unwind_loop];
  254. secondpass(left);
  255. flowcontrol:=flowcontrol-[fc_unwind_exit,fc_unwind_loop];
  256. if codegenerror then
  257. exit;
  258. end;
  259. { If the immediately preceding instruction is CALL,
  260. its return address must not end up outside the scope, so pad with NOP. }
  261. if catch_frame then
  262. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel)
  263. else
  264. emit_nop;
  265. cg.a_label(current_asmdata.CurrAsmList,endtrylabel);
  266. { Handle the except block first, so endtrylabel serves both
  267. as end of scope and as unwind target. This way it is possible to
  268. encode everything into a single scope record. }
  269. if catch_frame then
  270. begin
  271. if (current_procinfo.procdef.proccalloption=pocall_safecall) then
  272. begin
  273. handle_safecall_exception;
  274. cg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  275. end
  276. else
  277. InternalError(2014031601);
  278. end;
  279. flowcontrol:=[fc_inflowcontrol];
  280. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  281. { generate finally code as a separate procedure }
  282. if not implicitframe then
  283. tcgprocinfo(current_procinfo).generate_exceptfilter(finalizepi);
  284. { right is a call to finalizer procedure }
  285. secondpass(right);
  286. if codegenerror then
  287. exit;
  288. { normal exit from safecall proc must zero the result register }
  289. if implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall) then
  290. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,NR_FUNCTION_RESULT_REG);
  291. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  292. { generate the scope record in .xdata }
  293. tx86_64procinfo(current_procinfo).add_finally_scope(trylabel,endtrylabel,
  294. current_asmdata.RefAsmSymbol(finalizepi.procdef.mangledname,AT_FUNCTION),catch_frame);
  295. if implicitframe then
  296. current_procinfo.CurrExitLabel:=oldexitlabel;
  297. flowcontrol:=oldflowcontrol;
  298. end;
  299. { tx64tryexceptnode }
  300. procedure tx64tryexceptnode.pass_generate_code;
  301. var
  302. trylabel,
  303. exceptlabel,oldendexceptlabel,
  304. lastonlabel,
  305. exitexceptlabel,
  306. continueexceptlabel,
  307. breakexceptlabel,
  308. oldCurrExitLabel,
  309. oldContinueLabel,
  310. oldBreakLabel : tasmlabel;
  311. onlabel,
  312. filterlabel: tasmlabel;
  313. oldflowcontrol,tryflowcontrol,
  314. exceptflowcontrol : tflowcontrol;
  315. hnode : tnode;
  316. hlist : tasmlist;
  317. onnodecount : tai_const;
  318. label
  319. errorexit;
  320. begin
  321. if (target_info.system<>system_x86_64_win64) then
  322. begin
  323. inherited pass_generate_code;
  324. exit;
  325. end;
  326. location_reset(location,LOC_VOID,OS_NO);
  327. oldflowcontrol:=flowcontrol;
  328. exceptflowcontrol:=[];
  329. continueexceptlabel:=nil;
  330. breakexceptlabel:=nil;
  331. include(flowcontrol,fc_inflowcontrol);
  332. { this can be called recursivly }
  333. oldBreakLabel:=nil;
  334. oldContinueLabel:=nil;
  335. oldendexceptlabel:=endexceptlabel;
  336. { save the old labels for control flow statements }
  337. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  338. current_asmdata.getjumplabel(exitexceptlabel);
  339. if assigned(current_procinfo.CurrBreakLabel) then
  340. begin
  341. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  342. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  343. current_asmdata.getjumplabel(breakexceptlabel);
  344. current_asmdata.getjumplabel(continueexceptlabel);
  345. end;
  346. current_asmdata.getjumplabel(exceptlabel);
  347. current_asmdata.getjumplabel(endexceptlabel);
  348. current_asmdata.getjumplabel(lastonlabel);
  349. filterlabel:=nil;
  350. { start of scope }
  351. current_asmdata.getjumplabel(trylabel);
  352. emit_nop;
  353. cg.a_label(current_asmdata.CurrAsmList,trylabel);
  354. { control flow in try block needs no special handling,
  355. just make sure that target labels are outside the scope }
  356. secondpass(left);
  357. tryflowcontrol:=flowcontrol;
  358. if codegenerror then
  359. goto errorexit;
  360. { jump over except handlers }
  361. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  362. { end of scope }
  363. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  364. { set control flow labels for the except block }
  365. { and the on statements }
  366. current_procinfo.CurrExitLabel:=exitexceptlabel;
  367. if assigned(oldBreakLabel) then
  368. begin
  369. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  370. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  371. end;
  372. flowcontrol:=[fc_inflowcontrol];
  373. { on statements }
  374. if assigned(right) then
  375. begin
  376. { emit filter table to a temporary asmlist }
  377. hlist:=TAsmList.Create;
  378. current_asmdata.getaddrlabel(filterlabel);
  379. new_section(hlist,sec_rodata_norel,filterlabel.name,4);
  380. cg.a_label(hlist,filterlabel);
  381. onnodecount:=tai_const.create_32bit(0);
  382. hlist.concat(onnodecount);
  383. hnode:=right;
  384. while assigned(hnode) do
  385. begin
  386. if hnode.nodetype<>onn then
  387. InternalError(2011103101);
  388. current_asmdata.getjumplabel(onlabel);
  389. hlist.concat(tai_const.create_rva_sym(current_asmdata.RefAsmSymbol(tonnode(hnode).excepttype.vmt_mangledname,AT_DATA)));
  390. hlist.concat(tai_const.create_rva_sym(onlabel));
  391. cg.a_label(current_asmdata.CurrAsmList,onlabel);
  392. secondpass(hnode);
  393. inc(onnodecount.value);
  394. hnode:=tonnode(hnode).left;
  395. end;
  396. { add 'else' node to the filter list, too }
  397. if assigned(t1) then
  398. begin
  399. hlist.concat(tai_const.create_32bit(-1));
  400. hlist.concat(tai_const.create_rva_sym(lastonlabel));
  401. inc(onnodecount.value);
  402. end;
  403. { now move filter table to permanent list all at once }
  404. current_procinfo.aktlocaldata.concatlist(hlist);
  405. hlist.free;
  406. end;
  407. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  408. if assigned(t1) then
  409. begin
  410. { here we don't have to reset flowcontrol }
  411. { the default and on flowcontrols are handled equal }
  412. secondpass(t1);
  413. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  414. if (flowcontrol*[fc_exit,fc_break,fc_continue]<>[]) then
  415. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  416. end;
  417. exceptflowcontrol:=flowcontrol;
  418. if fc_exit in exceptflowcontrol then
  419. begin
  420. { do some magic for exit in the try block }
  421. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  422. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  423. if (fc_unwind_exit in oldflowcontrol) then
  424. cg.g_local_unwind(current_asmdata.CurrAsmList,oldCurrExitLabel)
  425. else
  426. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  427. end;
  428. if fc_break in exceptflowcontrol then
  429. begin
  430. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  431. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  432. if (fc_unwind_loop in oldflowcontrol) then
  433. cg.g_local_unwind(current_asmdata.CurrAsmList,oldBreakLabel)
  434. else
  435. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  436. end;
  437. if fc_continue in exceptflowcontrol then
  438. begin
  439. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  440. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  441. if (fc_unwind_loop in oldflowcontrol) then
  442. cg.g_local_unwind(current_asmdata.CurrAsmList,oldContinueLabel)
  443. else
  444. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  445. end;
  446. emit_nop;
  447. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  448. tx86_64procinfo(current_procinfo).add_except_scope(trylabel,exceptlabel,endexceptlabel,filterlabel);
  449. errorexit:
  450. { restore all saved labels }
  451. endexceptlabel:=oldendexceptlabel;
  452. { restore the control flow labels }
  453. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  454. if assigned(oldBreakLabel) then
  455. begin
  456. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  457. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  458. end;
  459. { return all used control flow statements }
  460. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  461. tryflowcontrol - [fc_inflowcontrol]);
  462. end;
  463. initialization
  464. craisenode:=tx64raisenode;
  465. connode:=tx64onnode;
  466. ctryexceptnode:=tx64tryexceptnode;
  467. ctryfinallynode:=tx64tryfinallynode;
  468. end.