cpupi.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. {
  2. Copyright (c) 2002-2010 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the CPU specific part of tprocinfo
  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 cpupi;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,globtype,
  22. procinfo,cpuinfo, symtype,aasmbase,cgbase,
  23. psub, cclasses;
  24. type
  25. { tcpuprocinfo }
  26. tcpuprocinfo=class(tcgprocinfo)
  27. public
  28. function calc_stackframe_size : longint;override;
  29. procedure setup_eh; override;
  30. procedure postprocess_code; override;
  31. procedure set_first_temp_offset;override;
  32. end;
  33. implementation
  34. uses
  35. systems,verbose,globals,cpubase,tgcpu,aasmdata,aasmcpu,aasmtai,cgexcept,
  36. tgobj,paramgr,symconst,symdef,symtable,symcpu,cgutils,pass_2,parabase,
  37. fmodule,hlcgobj,hlcgcpu,defutil;
  38. {*****************************************************************************
  39. twasmexceptionstatehandler_noexceptions
  40. *****************************************************************************}
  41. type
  42. { twasmexceptionstatehandler_noexceptions }
  43. twasmexceptionstatehandler_noexceptions = class(tcgexceptionstatehandler)
  44. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); override;
  45. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); override;
  46. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  47. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  48. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  49. end;
  50. class procedure twasmexceptionstatehandler_noexceptions.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  51. begin
  52. if not assigned(exceptionreasontype) then
  53. exceptionreasontype:=search_system_proc('fpc_setjmp').returndef;
  54. reference_reset(t.envbuf,0,[]);
  55. reference_reset(t.jmpbuf,0,[]);
  56. tg.gethltemp(list,exceptionreasontype,exceptionreasontype.size,tt_persistent,t.reasonbuf);
  57. end;
  58. class procedure twasmexceptionstatehandler_noexceptions.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  59. begin
  60. tg.ungettemp(list,t.reasonbuf);
  61. end;
  62. class procedure twasmexceptionstatehandler_noexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  63. begin
  64. exceptstate.exceptionlabel:=nil;
  65. exceptstate.oldflowcontrol:=flowcontrol;
  66. exceptstate.finallycodelabel:=nil;
  67. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  68. end;
  69. class procedure twasmexceptionstatehandler_noexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  70. begin
  71. end;
  72. class procedure twasmexceptionstatehandler_noexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  73. begin
  74. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  75. end;
  76. {*****************************************************************************
  77. twasmexceptionstatehandler_jsexceptions
  78. *****************************************************************************}
  79. type
  80. twasmexceptionstatehandler_jsexceptions = class(tcgexceptionstatehandler)
  81. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); override;
  82. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); override;
  83. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  84. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  85. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  86. end;
  87. class procedure twasmexceptionstatehandler_jsexceptions.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  88. begin
  89. if not assigned(exceptionreasontype) then
  90. exceptionreasontype:=search_system_proc('fpc_setjmp').returndef;
  91. reference_reset(t.envbuf,0,[]);
  92. reference_reset(t.jmpbuf,0,[]);
  93. tg.gethltemp(list,exceptionreasontype,exceptionreasontype.size,tt_persistent,t.reasonbuf);
  94. end;
  95. class procedure twasmexceptionstatehandler_jsexceptions.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  96. begin
  97. tg.ungettemp(list,t.reasonbuf);
  98. end;
  99. class procedure twasmexceptionstatehandler_jsexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  100. begin
  101. exceptstate.exceptionlabel:=nil;
  102. exceptstate.oldflowcontrol:=flowcontrol;
  103. exceptstate.finallycodelabel:=nil;
  104. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  105. end;
  106. class procedure twasmexceptionstatehandler_jsexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  107. begin
  108. end;
  109. class procedure twasmexceptionstatehandler_jsexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  110. begin
  111. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  112. end;
  113. {*****************************************************************************
  114. twasmexceptionstatehandler_nativeexceptions
  115. *****************************************************************************}
  116. type
  117. { twasmexceptionstatehandler_nativeexceptions }
  118. twasmexceptionstatehandler_nativeexceptions = class(tcgexceptionstatehandler)
  119. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  120. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  121. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  122. { start of an "on" (catch) block }
  123. class procedure begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister); override;
  124. { end of an "on" (catch) block }
  125. class procedure end_catch(list: TAsmList); override;
  126. end;
  127. class procedure twasmexceptionstatehandler_nativeexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  128. begin
  129. exceptstate.exceptionlabel:=nil;
  130. exceptstate.oldflowcontrol:=flowcontrol;
  131. exceptstate.finallycodelabel:=nil;
  132. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  133. end;
  134. class procedure twasmexceptionstatehandler_nativeexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  135. begin
  136. end;
  137. class procedure twasmexceptionstatehandler_nativeexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  138. begin
  139. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  140. end;
  141. class procedure twasmexceptionstatehandler_nativeexceptions.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister);
  142. var
  143. pd: tprocdef;
  144. href2: treference;
  145. fpc_catches_res,
  146. paraloc1: tcgpara;
  147. exceptloc: tlocation;
  148. indirect: boolean;
  149. otherunit: boolean;
  150. begin
  151. paraloc1.init;
  152. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  153. indirect:=(tf_supports_packages in target_info.flags) and
  154. (target_info.system in systems_indirect_var_imports) and
  155. (cs_imported_data in current_settings.localswitches) and
  156. otherunit;
  157. { send the vmt parameter }
  158. pd:=search_system_proc('fpc_catches');
  159. reference_reset_symbol(href2, current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect), 0, sizeof(pint), []);
  160. if otherunit then
  161. current_module.add_extern_asmsym(excepttype.vmt_mangledname, AB_EXTERNAL, AT_DATA);
  162. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  163. hlcg.a_loadaddr_ref_cgpara(list, excepttype.vmt_def, href2, paraloc1);
  164. paramanager.freecgpara(list, paraloc1);
  165. fpc_catches_res:=hlcg.g_call_system_proc(list, pd, [@paraloc1], nil);
  166. location_reset(exceptloc, LOC_REGISTER, def_cgsize(fpc_catches_res.def));
  167. exceptloc.register:=hlcg.getaddressregister(list, fpc_catches_res.def);
  168. hlcg.gen_load_cgpara_loc(list, fpc_catches_res.def, fpc_catches_res, exceptloc, true);
  169. { is it this catch? }
  170. thlcgwasm(hlcg).a_cmp_const_reg_stack(list, fpc_catches_res.def, OC_NE, 0, exceptloc.register);
  171. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  172. thlcgwasm(hlcg).incblock;
  173. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  174. paraloc1.done;
  175. exceptlocdef:=fpc_catches_res.def;
  176. exceptlocreg:=exceptloc.register;
  177. end;
  178. class procedure twasmexceptionstatehandler_nativeexceptions.end_catch(list: TAsmList);
  179. begin
  180. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  181. thlcgwasm(hlcg).decblock;
  182. end;
  183. {*****************************************************************************
  184. twasmexceptionstatehandler_bfexceptions
  185. *****************************************************************************}
  186. type
  187. { twasmexceptionstatehandler_bfexceptions }
  188. twasmexceptionstatehandler_bfexceptions = class(tcgexceptionstatehandler)
  189. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  190. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  191. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  192. { start of an "on" (catch) block }
  193. class procedure begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister); override;
  194. { end of an "on" (catch) block }
  195. class procedure end_catch(list: TAsmList); override;
  196. end;
  197. class procedure twasmexceptionstatehandler_bfexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  198. begin
  199. exceptstate.exceptionlabel:=nil;
  200. exceptstate.oldflowcontrol:=flowcontrol;
  201. exceptstate.finallycodelabel:=nil;
  202. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  203. end;
  204. class procedure twasmexceptionstatehandler_bfexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  205. begin
  206. end;
  207. class procedure twasmexceptionstatehandler_bfexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  208. begin
  209. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  210. end;
  211. class procedure twasmexceptionstatehandler_bfexceptions.begin_catch(list: TAsmList; excepttype: tobjectdef; nextonlabel: tasmlabel; out exceptlocdef: tdef; out exceptlocreg: tregister);
  212. var
  213. pd: tprocdef;
  214. href2: treference;
  215. fpc_catches_res,
  216. paraloc1: tcgpara;
  217. exceptloc: tlocation;
  218. indirect: boolean;
  219. otherunit: boolean;
  220. begin
  221. paraloc1.init;
  222. otherunit:=findunitsymtable(excepttype.owner).moduleid<>findunitsymtable(current_procinfo.procdef.owner).moduleid;
  223. indirect:=(tf_supports_packages in target_info.flags) and
  224. (target_info.system in systems_indirect_var_imports) and
  225. (cs_imported_data in current_settings.localswitches) and
  226. otherunit;
  227. { send the vmt parameter }
  228. pd:=search_system_proc('fpc_catches');
  229. reference_reset_symbol(href2, current_asmdata.RefAsmSymbol(excepttype.vmt_mangledname, AT_DATA, indirect), 0, sizeof(pint), []);
  230. if otherunit then
  231. current_module.add_extern_asmsym(excepttype.vmt_mangledname, AB_EXTERNAL, AT_DATA);
  232. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  233. hlcg.a_loadaddr_ref_cgpara(list, excepttype.vmt_def, href2, paraloc1);
  234. paramanager.freecgpara(list, paraloc1);
  235. fpc_catches_res:=hlcg.g_call_system_proc(list, pd, [@paraloc1], nil);
  236. location_reset(exceptloc, LOC_REGISTER, def_cgsize(fpc_catches_res.def));
  237. exceptloc.register:=hlcg.getaddressregister(list, fpc_catches_res.def);
  238. hlcg.gen_load_cgpara_loc(list, fpc_catches_res.def, fpc_catches_res, exceptloc, true);
  239. { is it this catch? }
  240. thlcgwasm(hlcg).a_cmp_const_reg_stack(list, fpc_catches_res.def, OC_NE, 0, exceptloc.register);
  241. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_if));
  242. thlcgwasm(hlcg).incblock;
  243. thlcgwasm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  244. paraloc1.done;
  245. exceptlocdef:=fpc_catches_res.def;
  246. exceptlocreg:=exceptloc.register;
  247. end;
  248. class procedure twasmexceptionstatehandler_bfexceptions.end_catch(list: TAsmList);
  249. begin
  250. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_if));
  251. thlcgwasm(hlcg).decblock;
  252. end;
  253. {*****************************************************************************
  254. tcpuprocinfo
  255. *****************************************************************************}
  256. function tcpuprocinfo.calc_stackframe_size: longint;
  257. begin
  258. { the stack frame in WebAssembly should always have a 16-byte alignment }
  259. Result:=Align(inherited calc_stackframe_size,16);
  260. end;
  261. procedure tcpuprocinfo.setup_eh;
  262. begin
  263. if ts_wasm_native_exceptions in current_settings.targetswitches then
  264. cexceptionstatehandler:=twasmexceptionstatehandler_nativeexceptions
  265. else if ts_wasm_js_exceptions in current_settings.targetswitches then
  266. cexceptionstatehandler:=twasmexceptionstatehandler_jsexceptions
  267. else if ts_wasm_no_exceptions in current_settings.targetswitches then
  268. cexceptionstatehandler:=twasmexceptionstatehandler_noexceptions
  269. else if ts_wasm_bf_exceptions in current_settings.targetswitches then
  270. cexceptionstatehandler:=twasmexceptionstatehandler_bfexceptions
  271. else
  272. internalerror(2021091701);
  273. end;
  274. procedure tcpuprocinfo.postprocess_code;
  275. function findfirst_tai_functype(asmlist: TAsmList): tai_functype;
  276. var
  277. hp: tai;
  278. begin
  279. result:=nil;
  280. if not assigned(asmlist) then
  281. exit;
  282. hp:=tai(asmlist.first);
  283. while assigned(hp) do
  284. begin
  285. if hp.typ=ait_functype then
  286. begin
  287. result:=tai_functype(hp);
  288. exit;
  289. end;
  290. hp:=tai(hp.Next);
  291. end;
  292. end;
  293. procedure replace_local_frame_pointer(asmlist: TAsmList);
  294. var
  295. hp: tai;
  296. instr: taicpu;
  297. l: Integer;
  298. begin
  299. if not assigned(asmlist) then
  300. exit;
  301. hp:=tai(asmlist.first);
  302. while assigned(hp) do
  303. begin
  304. if hp.typ=ait_instruction then
  305. begin
  306. instr:=taicpu(hp);
  307. for l:=0 to instr.ops-1 do
  308. if (instr.oper[l]^.typ=top_reg) and (instr.oper[l]^.reg=NR_LOCAL_FRAME_POINTER_REG) then
  309. instr.loadref(l,tcpuprocdef(current_procinfo.procdef).frame_pointer_ref);
  310. end;
  311. hp:=tai(hp.Next);
  312. end;
  313. end;
  314. var
  315. templist : TAsmList;
  316. l : TWasmLocal;
  317. first: Boolean;
  318. local: tai_local;
  319. begin
  320. templist:=TAsmList.create;
  321. local:=nil;
  322. first:=true;
  323. l:=ttgwasm(tg).localvars.first;
  324. while Assigned(l) do
  325. begin
  326. local:=tai_local.create(l.typ);
  327. local.first:=first;
  328. first:=false;
  329. templist.Concat(local);
  330. l:=l.nextseq;
  331. end;
  332. if assigned(local) then
  333. local.last:=true;
  334. aktproccode.insertListAfter(findfirst_tai_functype(aktproccode),templist);
  335. templist.Free;
  336. replace_local_frame_pointer(aktproccode);
  337. inherited postprocess_code;
  338. end;
  339. procedure tcpuprocinfo.set_first_temp_offset;
  340. var
  341. sz : integer;
  342. i : integer;
  343. sym: tsym;
  344. begin
  345. {
  346. Stackframe layout:
  347. sp:
  348. <incoming parameters>
  349. sp+first_temp_offset:
  350. <locals>
  351. <temp>
  352. }
  353. procdef.init_paraloc_info(calleeside);
  354. sz := procdef.calleeargareasize;
  355. tg.setfirsttemp(sz);
  356. end;
  357. initialization
  358. cprocinfo:=tcpuprocinfo;
  359. end.