nx64flw.pas 18 KB

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