/compiler/x86_64/nx64flw.pas

https://github.com/slibre/freepascal · Pascal · 571 lines · 400 code · 76 blank · 95 comment · 42 complexity · 879c79ef2ccc5aea7ffdd956e08719b4 MD5 · raw file

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