nllvmflw.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. {
  2. Copyright (c) 2016 by Jonas Maebe
  3. Generate assembler for nodes that influence the flow for llvm
  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 nllvmflw;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symtype,symdef,
  23. aasmbase,aasmdata,
  24. cgbase,
  25. node, nflw, ncgflw, ncgnstfl;
  26. type
  27. tllvmlabelnode = class(tcglabelnode)
  28. function getasmlabel: tasmlabel; override;
  29. end;
  30. tllvmexceptionstatehandler = class(tcgexceptionstatehandler)
  31. class procedure get_exception_temps(list: TAsmList; var t: texceptiontemps); override;
  32. class procedure unget_exception_temps(list: TAsmList; const t: texceptiontemps); override;
  33. class procedure new_exception(list: TAsmList; const t: texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  34. class procedure emit_except_label(list: TAsmList; exceptframekind: texceptframekind; var exceptionstate: texceptionstate;var exceptiontemps:texceptiontemps); override;
  35. class procedure end_try_block(list: TAsmList; exceptframekind: texceptframekind; const t: texceptiontemps; var exceptionstate: texceptionstate; endlabel: TAsmLabel); override;
  36. class procedure cleanupobjectstack(list: TAsmList); override;
  37. class procedure popaddrstack(list: TAsmList); override;
  38. class procedure handle_reraise(list: TAsmList; const t: texceptiontemps; const entrystate: texceptionstate; const exceptframekind: texceptframekind); override;
  39. class procedure begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister); override;
  40. class procedure end_catch(list: TAsmList); override;
  41. class procedure catch_all_start(list: TAsmList); override;
  42. class procedure catch_all_end(list: TAsmList); override;
  43. protected
  44. class procedure begin_catch_internal(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; add_catch: boolean; out exceptlocdef: tdef; out exceptlocreg: tregister);
  45. class procedure catch_all_start_internal(list: TAsmList; add_catch: boolean);
  46. end;
  47. tllvmtryexceptnode = class(tcgtryexceptnode)
  48. end;
  49. tllvmtryfinallynode = class(tcgtryfinallynode)
  50. function pass_1: tnode; override;
  51. end;
  52. tllvmraisenode = class(tcgraisenode)
  53. function pass_1: tnode; override;
  54. procedure pass_generate_code; override;
  55. end;
  56. implementation
  57. uses
  58. systems,globals,verbose,
  59. symconst,symtable,symsym,llvmdef,defutil,
  60. pass_2,cgutils,hlcgobj,parabase,paramgr,tgobj,
  61. llvmbase,aasmtai,aasmllvm,
  62. procinfo,llvmpi;
  63. {*****************************************************************************
  64. SecondLabel
  65. *****************************************************************************}
  66. function tllvmlabelnode.getasmlabel: tasmlabel;
  67. begin
  68. { don't allocate global labels even if the label is accessed from
  69. another routine: we always have to refer to such labels using the
  70. blockaddress() construct, which works with local labels too.
  71. Additionally, LLVM does not support defining global labels in the
  72. middle of a routine -> jumping to such a label from assembler code
  73. from another function will not work anyway (have to handle that by
  74. passing a blockaddress as argument to an assembler block, although
  75. "some targets may provide defined semantics when using the value as
  76. the operand to an inline assembly") }
  77. if not(assigned(asmlabel)) then
  78. current_asmdata.getjumplabel(asmlabel);
  79. result:=asmlabel
  80. end;
  81. {*****************************************************************************
  82. tllvmtryfinallynode
  83. *****************************************************************************}
  84. function tllvmtryfinallynode.pass_1: tnode;
  85. begin
  86. { make a copy of the "finally" code for the "no exception happened"
  87. case }
  88. if not assigned(third) then
  89. third:=right.getcopy;
  90. result:=inherited;
  91. end;
  92. {*****************************************************************************
  93. tllvmexceptionstatehandler
  94. *****************************************************************************}
  95. class procedure tllvmexceptionstatehandler.get_exception_temps(list: TAsmList; var t: texceptiontemps);
  96. begin
  97. tg.gethltemp(list,ossinttype,ossinttype.size,tt_persistent,t.reasonbuf);
  98. end;
  99. class procedure tllvmexceptionstatehandler.unget_exception_temps(list: TAsmList; const t: texceptiontemps);
  100. begin
  101. tg.ungettemp(list,t.reasonbuf);
  102. tllvmprocinfo(current_procinfo).poppad;
  103. end;
  104. class procedure tllvmexceptionstatehandler.new_exception(list: TAsmList; const t: texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  105. var
  106. reg: tregister;
  107. begin
  108. exceptstate.oldflowcontrol:=flowcontrol;
  109. if exceptframekind<>tek_except then
  110. current_asmdata.getjumplabel(exceptstate.finallycodelabel)
  111. else
  112. exceptstate.finallycodelabel:=nil;
  113. { all calls inside the exception block have to be invokes instead,
  114. which refer to the exception label:
  115. exceptionlabel:
  116. %reg = landingpad ..
  117. <exception handling code>
  118. }
  119. current_asmdata.getjumplabel(exceptstate.exceptionlabel);
  120. { for consistency checking when popping }
  121. tllvmprocinfo(current_procinfo).pushexceptlabel(exceptstate.exceptionlabel);
  122. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  123. { the reasonbuf is set to 1 by the generic code if we got in
  124. the exception block by catching an exception -> do the same here, so
  125. we can share that generic code; llvm will optimise it away. The
  126. reasonbuf is later also used for break/continue/... }
  127. reg:=hlcg.getintregister(list,ossinttype);
  128. hlcg.a_load_const_reg(list,ossinttype,1,reg);
  129. hlcg.g_exception_reason_save(list,ossinttype,ossinttype,reg,t.reasonbuf);
  130. { There can only be a landingpad if there were any invokes in the try-block,
  131. as otherwise we get an error; we can also generate exceptions from
  132. invalid memory accesses and the like, but LLVM cannot model that
  133. --
  134. We cheat for now by adding an invoke to a dummy routine at the start and at
  135. the end of the try-block. That will not magically fix the state
  136. of all variables when the exception gets caught though. }
  137. hlcg.g_call_system_proc(list,'FPC_DUMMYPOTENTIALRAISE',[],nil).resetiftemp;
  138. end;
  139. class procedure tllvmexceptionstatehandler.emit_except_label(list: TAsmList; exceptframekind: texceptframekind; var exceptionstate: texceptionstate;var exceptiontemps:texceptiontemps);
  140. var
  141. reg: tregister;
  142. landingpad: taillvm;
  143. landingpaddef: trecorddef;
  144. begin
  145. hlcg.g_unreachable(list);
  146. hlcg.a_label(list,exceptionstate.exceptionlabel);
  147. { use packrecords 1 because we don't want padding (LLVM 4.0+ requires
  148. exactly two fields in this struct) }
  149. landingpaddef:=llvmgettemprecorddef([voidpointertype,u32inttype],
  150. 1,
  151. targetinfos[target_info.system]^.alignment.recordalignmin,
  152. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  153. reg:=hlcg.getregisterfordef(list,landingpaddef);
  154. landingpad:=taillvm.landingpad(reg,landingpaddef,{clause}nil);
  155. list.concat(landingpad);
  156. if exceptframekind<>tek_except then
  157. begin
  158. if not assigned(exceptionstate.finallycodelabel) then
  159. internalerror(2018111102);
  160. if use_cleanup(exceptframekind) then
  161. landingpad.landingpad_add_clause(la_cleanup, nil, nil)
  162. else
  163. landingpad.landingpad_add_clause(la_catch, voidpointertype, nil);
  164. hlcg.a_label(list,exceptionstate.finallycodelabel);
  165. exceptionstate.finallycodelabel:=nil;
  166. end;
  167. { consistency check }
  168. tllvmprocinfo(current_procinfo).popexceptlabel(exceptionstate.exceptionlabel);
  169. tllvmprocinfo(current_procinfo).pushlandingpad(landingpad);
  170. end;
  171. class procedure tllvmexceptionstatehandler.end_try_block(list: TAsmList; exceptframekind: texceptframekind; const t: texceptiontemps; var exceptionstate: texceptionstate; endlabel: TAsmLabel);
  172. var
  173. reg: tregister;
  174. begin
  175. { llvm does not allow creating a landing pad if there are no invokes in
  176. the try block -> create a call to a dummy routine that cannot be
  177. analysed by llvm and that supposedly may raise an exception. Has to
  178. be combined with marking stores inside try blocks as volatile and the
  179. loads afterwards as well in order to guarantee correct optimizations
  180. in case an exception gets triggered inside a try-block though }
  181. hlcg.g_call_system_proc(list,'FPC_DUMMYPOTENTIALRAISE',[],nil).resetiftemp;
  182. { record that no exception happened in the reason buf }
  183. reg:=hlcg.getintregister(list,ossinttype);
  184. hlcg.a_load_const_reg(list,ossinttype,0,reg);
  185. hlcg.g_exception_reason_save(list,ossinttype,ossinttype,reg,t.reasonbuf);
  186. inherited;
  187. if exceptframekind=tek_except then
  188. hlcg.a_jmp_always(list,endlabel);
  189. end;
  190. class procedure tllvmexceptionstatehandler.cleanupobjectstack(list: TAsmList);
  191. var
  192. landingpad: taillvm;
  193. begin
  194. { if not a single catch block added -> catch all }
  195. landingpad:=tllvmprocinfo(current_procinfo).currlandingpad;
  196. if assigned(landingpad) and
  197. not assigned(landingpad.oper[2]^.ai) then
  198. begin
  199. landingpad.landingpad_add_clause(la_catch,voidpointertype,nil);
  200. end;
  201. end;
  202. class procedure tllvmexceptionstatehandler.popaddrstack(list: TAsmList);
  203. begin
  204. // nothing
  205. end;
  206. class procedure tllvmexceptionstatehandler.handle_reraise(list: TAsmList; const t: texceptiontemps; const entrystate: texceptionstate; const exceptframekind: texceptframekind);
  207. var
  208. landingpad: taillvm;
  209. landingpadres: tregister;
  210. landingpadresdef: tdef;
  211. begin
  212. { We use resume to propagate the exception to an outer function frame, and call
  213. reraise in case we are nested in another exception frame in the current function
  214. (because then we will emit an invoke which will tie this re-raise to that other
  215. exception frame; that is impossible to do with a resume instruction).
  216. Furthermore, the resume opcode only works for landingpads with a cleanup clause,
  217. which we only generate for outer implicitfinally frames }
  218. if not(fc_catching_exceptions in flowcontrol) and
  219. use_cleanup(exceptframekind) then
  220. begin
  221. { resume <result from catchpad> }
  222. landingpad:=tllvmprocinfo(current_procinfo).currlandingpad;
  223. landingpadres:=landingpad.oper[0]^.reg;
  224. landingpadresdef:=landingpad.oper[1]^.def;
  225. list.concat(taillvm.op_size_reg(la_resume,landingpadresdef,landingpadres));
  226. end
  227. else
  228. begin
  229. { Need a begin_catch so that the reraise will know what exception to throw.
  230. Don't need to add a "catch all" to the landing pad, as it contains one.
  231. We want to rethrow whatever exception was caught rather than guarantee
  232. that all possible kinds of exceptions get caught. }
  233. catch_all_start_internal(list,false);
  234. hlcg.g_call_system_proc(list,'fpc_reraise',[],nil).resetiftemp;
  235. end;
  236. end;
  237. class procedure tllvmexceptionstatehandler.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister);
  238. begin
  239. begin_catch_internal(list,excepttype,nextonlabel,true,exceptlocdef,exceptlocreg);
  240. end;
  241. class procedure tllvmexceptionstatehandler.end_catch(list: TAsmList);
  242. begin
  243. hlcg.g_call_system_proc(list,'fpc_psabi_end_catch',[],nil).resetiftemp;
  244. inherited;
  245. end;
  246. class procedure tllvmexceptionstatehandler.catch_all_start(list: TAsmList);
  247. begin
  248. catch_all_start_internal(list,true);
  249. end;
  250. class procedure tllvmexceptionstatehandler.catch_all_end(list: TAsmList);
  251. begin
  252. hlcg.g_call_system_proc(list,'fpc_psabi_end_catch',[],nil).resetiftemp;
  253. end;
  254. class procedure tllvmexceptionstatehandler.begin_catch_internal(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; add_catch: boolean; out exceptlocdef: tdef; out exceptlocreg: tregister);
  255. var
  256. catchstartlab: tasmlabel;
  257. landingpad: taillvm;
  258. begincatchres,
  259. typeidres,
  260. paraloc1: tcgpara;
  261. pd: tprocdef;
  262. landingpadstructdef,
  263. landingpadtypeiddef: tdef;
  264. rttisym: TAsmSymbol;
  265. rttidef: tdef;
  266. rttiref: treference;
  267. wrappedexception,
  268. exceptiontypeidreg,
  269. landingpadres: tregister;
  270. exceptloc: tlocation;
  271. indirect: boolean;
  272. otherunit: boolean;
  273. begin
  274. paraloc1.init;
  275. landingpad:=tllvmprocinfo(current_procinfo).currlandingpad;
  276. rttidef:=nil;
  277. rttisym:=nil;
  278. if add_catch then
  279. begin
  280. if assigned(excepttype) then
  281. begin
  282. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  283. indirect:=(tf_supports_packages in target_info.flags) and
  284. (target_info.system in systems_indirect_var_imports) and
  285. (cs_imported_data in current_settings.localswitches) and
  286. otherunit;
  287. { add "catch exceptiontype" clause to the landing pad }
  288. rttidef:=cpointerdef.getreusable(excepttype.vmt_def);
  289. rttisym:=current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect);
  290. landingpad.landingpad_add_clause(la_catch,rttidef,rttisym);
  291. end
  292. else
  293. begin
  294. landingpad.landingpad_add_clause(la_catch,voidpointertype,nil);
  295. end;
  296. end;
  297. { pascal_exception := FPC_psabi_begin_catch(wrappedExceptionObject) where
  298. wrappedExceptionObject is the exception returned by the landingpad }
  299. landingpadres:=landingpad.oper[0]^.reg;
  300. landingpadstructdef:=landingpad.oper[1]^.def;
  301. { check if the exception is handled by this node }
  302. if assigned(excepttype) then
  303. begin
  304. landingpadtypeiddef:=tfieldvarsym(trecorddef(landingpadstructdef).symtable.symlist[1]).vardef;
  305. exceptiontypeidreg:=hlcg.getaddressregister(list,landingpadtypeiddef);
  306. pd:=search_system_proc('llvm_eh_typeid_for');
  307. paramanager.getintparaloc(list,pd,1,paraloc1);
  308. reference_reset_symbol(rttiref,rttisym,0,rttidef.alignment,[]);
  309. rttiref.refaddr:=addr_full;
  310. hlcg.a_load_ref_cgpara(list,cpointerdef.getreusable(rttidef),rttiref,paraloc1);
  311. typeidres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  312. location_reset(exceptloc, LOC_REGISTER, def_cgsize(landingpadtypeiddef));
  313. exceptloc.register:=hlcg.getintregister(list,landingpadtypeiddef);
  314. hlcg.gen_load_cgpara_loc(list, landingpadtypeiddef, typeidres, exceptloc, true);
  315. list.concat(taillvm.extract(la_extractvalue,exceptiontypeidreg,landingpadstructdef,landingpadres,1));
  316. current_asmdata.getjumplabel(catchstartlab);
  317. hlcg.a_cmp_reg_loc_label(list,typeidres.Def,OC_EQ,exceptiontypeidreg,exceptloc,catchstartlab);
  318. hlcg.a_jmp_always(list,nextonlabel);
  319. hlcg.a_label(list,catchstartlab);
  320. typeidres.resetiftemp;
  321. end;
  322. wrappedexception:=hlcg.getaddressregister(list,voidpointertype);
  323. list.concat(taillvm.extract(la_extractvalue,wrappedexception,landingpadstructdef,landingpadres,0));
  324. pd:=search_system_proc('fpc_psabi_begin_catch');
  325. paramanager.getintparaloc(list, pd, 1, paraloc1);
  326. hlcg.a_load_reg_cgpara(list,voidpointertype,wrappedexception,paraloc1);
  327. begincatchres:=hlcg.g_call_system_proc(list,pd,[@paraloc1],nil);
  328. location_reset(exceptloc, LOC_REGISTER, def_cgsize(begincatchres.def));
  329. exceptloc.register:=hlcg.getaddressregister(list, begincatchres.def);
  330. hlcg.gen_load_cgpara_loc(list, begincatchres.def, begincatchres, exceptloc, true);
  331. begincatchres.resetiftemp;
  332. paraloc1.done;
  333. exceptlocdef:=begincatchres.def;
  334. exceptlocreg:=exceptloc.register;
  335. end;
  336. class procedure tllvmexceptionstatehandler.catch_all_start_internal(list: TAsmList; add_catch: boolean);
  337. var
  338. exceptlocdef: tdef;
  339. exceptlocreg: tregister;
  340. begin
  341. begin_catch_internal(list,nil,nil,add_catch,exceptlocdef,exceptlocreg);
  342. end;
  343. {*****************************************************************************
  344. tllvmexceptionstatehandler
  345. *****************************************************************************}
  346. function tllvmraisenode.pass_1: tnode;
  347. begin
  348. if assigned(left) then
  349. result:=inherited
  350. else
  351. begin
  352. expectloc:=LOC_VOID;
  353. result:=nil;
  354. end;
  355. end;
  356. procedure tllvmraisenode.pass_generate_code;
  357. var
  358. currexceptlabel: tasmlabel;
  359. begin
  360. location_reset(location,LOC_VOID,OS_NO);
  361. currexceptlabel:=nil;
  362. { a reraise must raise the exception to the parent exception frame }
  363. if fc_catching_exceptions in flowcontrol then
  364. begin
  365. currexceptlabel:=tllvmprocinfo(current_procinfo).CurrExceptLabel;
  366. if tllvmprocinfo(current_procinfo).popexceptlabel(currexceptlabel) then
  367. exclude(flowcontrol,fc_catching_exceptions);
  368. end;
  369. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_reraise',[],nil).resetiftemp;
  370. if assigned(currexceptlabel) then
  371. begin
  372. tllvmprocinfo(current_procinfo).pushexceptlabel(currexceptlabel);
  373. include(flowcontrol,fc_catching_exceptions);
  374. end;
  375. end;
  376. begin
  377. clabelnode:=tllvmlabelnode;
  378. ctryexceptnode:=tllvmtryexceptnode;
  379. ctryfinallynode:=tllvmtryfinallynode;
  380. cexceptionstatehandler:=tllvmexceptionstatehandler;
  381. craisenode:=tllvmraisenode;
  382. end.