ncpuflw.pas 19 KB

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