nx64flw.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. oldflowcontrol : tflowcontrol;
  71. exceptvarsym : tlocalvarsym;
  72. begin
  73. if (target_info.system<>system_x86_64_win64) then
  74. begin
  75. inherited pass_generate_code;
  76. exit;
  77. end;
  78. location_reset(location,LOC_VOID,OS_NO);
  79. oldflowcontrol:=flowcontrol;
  80. flowcontrol:=flowcontrol*[fc_unwind]+[fc_inflowcontrol];
  81. { RTL will put exceptobject into RAX when jumping here }
  82. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  83. { Retrieve exception variable }
  84. if assigned(excepTSymtable) then
  85. exceptvarsym:=tlocalvarsym(excepTSymtable.SymList[0])
  86. else
  87. exceptvarsym:=nil;
  88. if assigned(exceptvarsym) then
  89. begin
  90. exceptvarsym.localloc.loc:=LOC_REFERENCE;
  91. exceptvarsym.localloc.size:=OS_ADDR;
  92. tg.GetLocal(current_asmdata.CurrAsmList,sizeof(pint),voidpointertype,exceptvarsym.localloc.reference);
  93. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,NR_FUNCTION_RESULT_REG,exceptvarsym.localloc.reference);
  94. end;
  95. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_FUNCTION_RESULT_REG);
  96. if assigned(right) then
  97. secondpass(right);
  98. { deallocate exception symbol }
  99. if assigned(exceptvarsym) then
  100. begin
  101. tg.UngetLocal(current_asmdata.CurrAsmList,exceptvarsym.localloc.reference);
  102. exceptvarsym.localloc.loc:=LOC_INVALID;
  103. end;
  104. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  105. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  106. flowcontrol:=oldflowcontrol+(flowcontrol-[fc_inflowcontrol]);
  107. end;
  108. { tx64tryfinallynode }
  109. function reset_regvars(var n: tnode; arg: pointer): foreachnoderesult;
  110. begin
  111. case n.nodetype of
  112. temprefn:
  113. make_not_regable(n,[]);
  114. calln:
  115. include(tprocinfo(arg).flags,pi_do_call);
  116. end;
  117. result:=fen_true;
  118. end;
  119. function copy_parasize(var n: tnode; arg: pointer): foreachnoderesult;
  120. begin
  121. case n.nodetype of
  122. calln:
  123. tcgprocinfo(arg).allocate_push_parasize(tcallnode(n).pushed_parasize);
  124. end;
  125. result:=fen_true;
  126. end;
  127. constructor tx64tryfinallynode.create(l, r: TNode);
  128. begin
  129. inherited create(l,r);
  130. if (target_info.system=system_x86_64_win64) and
  131. (
  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. not assigned(current_procinfo.procdef.struct) or
  137. not(df_generic in current_procinfo.procdef.struct.defoptions)
  138. ) then
  139. begin
  140. finalizepi:=tcgprocinfo(cprocinfo.create(current_procinfo));
  141. finalizepi.force_nested;
  142. finalizepi.procdef:=create_finalizer_procdef;
  143. finalizepi.entrypos:=r.fileinfo;
  144. finalizepi.entryswitches:=r.localswitches;
  145. finalizepi.exitpos:=current_filepos; // last_endtoken_pos?
  146. finalizepi.exitswitches:=current_settings.localswitches;
  147. { the init/final code is messing with asm nodes, so inform the compiler about this }
  148. include(finalizepi.flags,pi_has_assembler_block);
  149. { Regvar optimization for symbols is suppressed when using exceptions, but
  150. temps may be still placed into registers. This must be fixed. }
  151. foreachnodestatic(r,@reset_regvars,finalizepi);
  152. end;
  153. end;
  154. constructor tx64tryfinallynode.create_implicit(l, r, _t1: TNode);
  155. begin
  156. inherited create_implicit(l, r, _t1);
  157. if (target_info.system=system_x86_64_win64) then
  158. begin
  159. if assigned(current_procinfo.procdef.struct) and
  160. (df_generic in current_procinfo.procdef.struct.defoptions) then
  161. InternalError(2013012501);
  162. finalizepi:=tcgprocinfo(cprocinfo.create(current_procinfo));
  163. finalizepi.force_nested;
  164. finalizepi.procdef:=create_finalizer_procdef;
  165. finalizepi.entrypos:=current_filepos;
  166. finalizepi.exitpos:=current_filepos; // last_endtoken_pos?
  167. finalizepi.entryswitches:=r.localswitches;
  168. finalizepi.exitswitches:=current_settings.localswitches;
  169. include(finalizepi.flags,pi_do_call);
  170. { the init/final code is messing with asm nodes, so inform the compiler about this }
  171. include(finalizepi.flags,pi_has_assembler_block);
  172. finalizepi.allocate_push_parasize(32);
  173. end;
  174. end;
  175. function tx64tryfinallynode.simplify(forinline: boolean): tnode;
  176. begin
  177. result:=inherited simplify(forinline);
  178. if (target_info.system<>system_x86_64_win64) then
  179. exit;
  180. if (result=nil) then
  181. begin
  182. finalizepi.code:=right;
  183. foreachnodestatic(right,@copy_parasize,finalizepi);
  184. right:=ccallnode.create(nil,tprocsym(finalizepi.procdef.procsym),nil,nil,[]);
  185. firstpass(right);
  186. { For implicit frames, no actual code is available at this time,
  187. it is added later in assembler form. So store the nested procinfo
  188. for later use. }
  189. if implicitframe then
  190. begin
  191. current_procinfo.finalize_procinfo:=finalizepi;
  192. { don't leave dangling pointer }
  193. tcgprocinfo(current_procinfo).final_asmnode:=nil;
  194. end;
  195. end;
  196. end;
  197. procedure emit_nop;
  198. var
  199. dummy: TAsmLabel;
  200. begin
  201. { To avoid optimizing away the whole thing, prepend a jumplabel with increased refcount }
  202. current_asmdata.getjumplabel(dummy);
  203. dummy.increfs;
  204. cg.a_label(current_asmdata.CurrAsmList,dummy);
  205. current_asmdata.CurrAsmList.concat(Taicpu.op_none(A_NOP,S_NO));
  206. end;
  207. procedure tx64tryfinallynode.pass_generate_code;
  208. var
  209. trylabel,
  210. endtrylabel,
  211. finallylabel,
  212. endfinallylabel,
  213. oldexitlabel: tasmlabel;
  214. oldflowcontrol: tflowcontrol;
  215. catch_frame: boolean;
  216. begin
  217. if (target_info.system<>system_x86_64_win64) then
  218. begin
  219. inherited pass_generate_code;
  220. exit;
  221. end;
  222. location_reset(location,LOC_VOID,OS_NO);
  223. { Do not generate a frame that catches exceptions if the only action
  224. would be reraising it. Doing so is extremely inefficient with SEH
  225. (in contrast with setjmp/longjmp exception handling) }
  226. catch_frame:=implicitframe and ((not has_no_code(t1)) or
  227. (current_procinfo.procdef.proccalloption=pocall_safecall));
  228. oldflowcontrol:=flowcontrol;
  229. flowcontrol:=[fc_inflowcontrol];
  230. current_asmdata.getjumplabel(trylabel);
  231. current_asmdata.getjumplabel(endtrylabel);
  232. current_asmdata.getjumplabel(finallylabel);
  233. current_asmdata.getjumplabel(endfinallylabel);
  234. oldexitlabel:=current_procinfo.CurrExitLabel;
  235. if implicitframe then
  236. current_procinfo.CurrExitLabel:=finallylabel;
  237. { Start of scope }
  238. { Padding with NOP is necessary here because exceptions in called
  239. procedures are seen at the next instruction, while CPU/OS exceptions
  240. like AV are seen at the current instruction.
  241. So in the following code
  242. raise_some_exception; //(a)
  243. try
  244. pchar(nil)^:='0'; //(b)
  245. ...
  246. without NOP, exceptions (a) and (b) will be seen at the same address
  247. and fall into the same scope. However they should be seen in different scopes.
  248. }
  249. emit_nop;
  250. cg.a_label(current_asmdata.CurrAsmList,trylabel);
  251. { try code }
  252. if assigned(left) then
  253. begin
  254. { fc_unwind tells exit/continue/break statements to emit special
  255. unwind code instead of just JMP }
  256. if not implicitframe then
  257. include(flowcontrol,fc_unwind);
  258. secondpass(left);
  259. exclude(flowcontrol,fc_unwind);
  260. if codegenerror then
  261. exit;
  262. end;
  263. { If the immediately preceding instruction is CALL,
  264. its return address must not end up outside the scope, so pad with NOP. }
  265. if catch_frame then
  266. cg.a_jmp_always(current_asmdata.CurrAsmList,finallylabel)
  267. else
  268. emit_nop;
  269. cg.a_label(current_asmdata.CurrAsmList,endtrylabel);
  270. { Handle the except block first, so endtrylabel serves both
  271. as end of scope and as unwind target. This way it is possible to
  272. encode everything into a single scope record. }
  273. if catch_frame then
  274. begin
  275. if (current_procinfo.procdef.proccalloption=pocall_safecall) then
  276. begin
  277. handle_safecall_exception;
  278. cg.a_jmp_always(current_asmdata.CurrAsmList,endfinallylabel);
  279. end
  280. else
  281. InternalError(2014031601);
  282. end;
  283. flowcontrol:=[fc_inflowcontrol];
  284. cg.a_label(current_asmdata.CurrAsmList,finallylabel);
  285. { generate finally code as a separate procedure }
  286. if not implicitframe then
  287. tcgprocinfo(current_procinfo).generate_exceptfilter(finalizepi);
  288. { right is a call to finalizer procedure }
  289. secondpass(right);
  290. if codegenerror then
  291. exit;
  292. { normal exit from safecall proc must zero the result register }
  293. if implicitframe and (current_procinfo.procdef.proccalloption=pocall_safecall) then
  294. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,NR_FUNCTION_RESULT_REG);
  295. cg.a_label(current_asmdata.CurrAsmList,endfinallylabel);
  296. { generate the scope record in .xdata }
  297. tx86_64procinfo(current_procinfo).add_finally_scope(trylabel,endtrylabel,
  298. current_asmdata.RefAsmSymbol(finalizepi.procdef.mangledname),catch_frame);
  299. if implicitframe then
  300. current_procinfo.CurrExitLabel:=oldexitlabel;
  301. flowcontrol:=oldflowcontrol;
  302. end;
  303. { tx64tryexceptnode }
  304. procedure tx64tryexceptnode.pass_generate_code;
  305. var
  306. trylabel,
  307. exceptlabel,oldendexceptlabel,
  308. lastonlabel,
  309. exitexceptlabel,
  310. continueexceptlabel,
  311. breakexceptlabel,
  312. oldCurrExitLabel,
  313. oldContinueLabel,
  314. oldBreakLabel : tasmlabel;
  315. onlabel,
  316. filterlabel: tasmlabel;
  317. oldflowcontrol,tryflowcontrol,
  318. exceptflowcontrol : tflowcontrol;
  319. hnode : tnode;
  320. hlist : tasmlist;
  321. onnodecount : tai_const;
  322. label
  323. errorexit;
  324. begin
  325. if (target_info.system<>system_x86_64_win64) then
  326. begin
  327. inherited pass_generate_code;
  328. exit;
  329. end;
  330. location_reset(location,LOC_VOID,OS_NO);
  331. oldflowcontrol:=flowcontrol;
  332. exceptflowcontrol:=[];
  333. continueexceptlabel:=nil;
  334. breakexceptlabel:=nil;
  335. flowcontrol:=flowcontrol*[fc_unwind]+[fc_inflowcontrol];
  336. { this can be called recursivly }
  337. oldBreakLabel:=nil;
  338. oldContinueLabel:=nil;
  339. oldendexceptlabel:=endexceptlabel;
  340. { save the old labels for control flow statements }
  341. oldCurrExitLabel:=current_procinfo.CurrExitLabel;
  342. current_asmdata.getjumplabel(exitexceptlabel);
  343. if assigned(current_procinfo.CurrBreakLabel) then
  344. begin
  345. oldContinueLabel:=current_procinfo.CurrContinueLabel;
  346. oldBreakLabel:=current_procinfo.CurrBreakLabel;
  347. current_asmdata.getjumplabel(breakexceptlabel);
  348. current_asmdata.getjumplabel(continueexceptlabel);
  349. end;
  350. current_asmdata.getjumplabel(exceptlabel);
  351. current_asmdata.getjumplabel(endexceptlabel);
  352. current_asmdata.getjumplabel(lastonlabel);
  353. filterlabel:=nil;
  354. { start of scope }
  355. current_asmdata.getjumplabel(trylabel);
  356. emit_nop;
  357. cg.a_label(current_asmdata.CurrAsmList,trylabel);
  358. { control flow in try block needs no special handling,
  359. just make sure that target labels are outside the scope }
  360. secondpass(left);
  361. tryflowcontrol:=flowcontrol;
  362. if codegenerror then
  363. goto errorexit;
  364. { jump over except handlers }
  365. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  366. { end of scope }
  367. cg.a_label(current_asmdata.CurrAsmList,exceptlabel);
  368. { set control flow labels for the except block }
  369. { and the on statements }
  370. current_procinfo.CurrExitLabel:=exitexceptlabel;
  371. if assigned(oldBreakLabel) then
  372. begin
  373. current_procinfo.CurrContinueLabel:=continueexceptlabel;
  374. current_procinfo.CurrBreakLabel:=breakexceptlabel;
  375. end;
  376. flowcontrol:=flowcontrol*[fc_unwind]+[fc_inflowcontrol];
  377. { on statements }
  378. if assigned(right) then
  379. begin
  380. { emit filter table to a temporary asmlist }
  381. hlist:=TAsmList.Create;
  382. current_asmdata.getdatalabel(filterlabel);
  383. new_section(hlist,sec_rodata_norel,filterlabel.name,4);
  384. cg.a_label(hlist,filterlabel);
  385. onnodecount:=tai_const.create_32bit(0);
  386. hlist.concat(onnodecount);
  387. hnode:=right;
  388. while assigned(hnode) do
  389. begin
  390. if hnode.nodetype<>onn then
  391. InternalError(2011103101);
  392. { TODO: make it done without using global label }
  393. current_asmdata.getglobaljumplabel(onlabel);
  394. hlist.concat(tai_const.create_rva_sym(current_asmdata.RefAsmSymbol(tonnode(hnode).excepttype.vmt_mangledname,AT_DATA)));
  395. hlist.concat(tai_const.create_rva_sym(onlabel));
  396. cg.a_label(current_asmdata.CurrAsmList,onlabel);
  397. secondpass(hnode);
  398. inc(onnodecount.value);
  399. hnode:=tonnode(hnode).left;
  400. end;
  401. { add 'else' node to the filter list, too }
  402. if assigned(t1) then
  403. begin
  404. hlist.concat(tai_const.create_32bit(-1));
  405. hlist.concat(tai_const.create_rva_sym(lastonlabel));
  406. inc(onnodecount.value);
  407. end;
  408. { now move filter table to permanent list all at once }
  409. maybe_new_object_file(current_asmdata.asmlists[al_typedconsts]);
  410. current_asmdata.asmlists[al_typedconsts].concatlist(hlist);
  411. hlist.free;
  412. end;
  413. cg.a_label(current_asmdata.CurrAsmList,lastonlabel);
  414. if assigned(t1) then
  415. begin
  416. { here we don't have to reset flowcontrol }
  417. { the default and on flowcontrols are handled equal }
  418. secondpass(t1);
  419. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  420. if (flowcontrol*[fc_exit,fc_break,fc_continue]<>[]) then
  421. cg.a_jmp_always(current_asmdata.CurrAsmList,endexceptlabel);
  422. end;
  423. exceptflowcontrol:=flowcontrol;
  424. if fc_exit in exceptflowcontrol then
  425. begin
  426. { do some magic for exit in the try block }
  427. cg.a_label(current_asmdata.CurrAsmList,exitexceptlabel);
  428. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  429. if (fc_unwind in flowcontrol) then
  430. cg.g_local_unwind(current_asmdata.CurrAsmList,oldCurrExitLabel)
  431. else
  432. cg.a_jmp_always(current_asmdata.CurrAsmList,oldCurrExitLabel);
  433. end;
  434. if fc_break in exceptflowcontrol then
  435. begin
  436. cg.a_label(current_asmdata.CurrAsmList,breakexceptlabel);
  437. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  438. if (fc_unwind in flowcontrol) then
  439. cg.g_local_unwind(current_asmdata.CurrAsmList,oldBreakLabel)
  440. else
  441. cg.a_jmp_always(current_asmdata.CurrAsmList,oldBreakLabel);
  442. end;
  443. if fc_continue in exceptflowcontrol then
  444. begin
  445. cg.a_label(current_asmdata.CurrAsmList,continueexceptlabel);
  446. cg.g_call(current_asmdata.CurrAsmList,'FPC_DONEEXCEPTION');
  447. if (fc_unwind in flowcontrol) then
  448. cg.g_local_unwind(current_asmdata.CurrAsmList,oldContinueLabel)
  449. else
  450. cg.a_jmp_always(current_asmdata.CurrAsmList,oldContinueLabel);
  451. end;
  452. emit_nop;
  453. cg.a_label(current_asmdata.CurrAsmList,endexceptlabel);
  454. tx86_64procinfo(current_procinfo).add_except_scope(trylabel,exceptlabel,endexceptlabel,filterlabel);
  455. errorexit:
  456. { restore all saved labels }
  457. endexceptlabel:=oldendexceptlabel;
  458. { restore the control flow labels }
  459. current_procinfo.CurrExitLabel:=oldCurrExitLabel;
  460. if assigned(oldBreakLabel) then
  461. begin
  462. current_procinfo.CurrContinueLabel:=oldContinueLabel;
  463. current_procinfo.CurrBreakLabel:=oldBreakLabel;
  464. end;
  465. { return all used control flow statements }
  466. flowcontrol:=oldflowcontrol+(exceptflowcontrol +
  467. tryflowcontrol - [fc_inflowcontrol]);
  468. end;
  469. initialization
  470. craisenode:=tx64raisenode;
  471. connode:=tx64onnode;
  472. ctryexceptnode:=tx64tryexceptnode;
  473. ctryfinallynode:=tx64tryfinallynode;
  474. end.