cpupi.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. tcpuprocinfo
  185. *****************************************************************************}
  186. function tcpuprocinfo.calc_stackframe_size: longint;
  187. begin
  188. { the stack frame in WebAssembly should always have a 16-byte alignment }
  189. Result:=Align(inherited calc_stackframe_size,16);
  190. end;
  191. procedure tcpuprocinfo.setup_eh;
  192. begin
  193. if ts_wasm_native_exceptions in current_settings.targetswitches then
  194. cexceptionstatehandler:=twasmexceptionstatehandler_nativeexceptions
  195. else if ts_wasm_js_exceptions in current_settings.targetswitches then
  196. cexceptionstatehandler:=twasmexceptionstatehandler_jsexceptions
  197. else if ts_wasm_no_exceptions in current_settings.targetswitches then
  198. cexceptionstatehandler:=twasmexceptionstatehandler_noexceptions
  199. else
  200. internalerror(2021091701);
  201. end;
  202. procedure tcpuprocinfo.postprocess_code;
  203. function findfirst_tai_functype(asmlist: TAsmList): tai_functype;
  204. var
  205. hp: tai;
  206. begin
  207. result:=nil;
  208. if not assigned(asmlist) then
  209. exit;
  210. hp:=tai(asmlist.first);
  211. while assigned(hp) do
  212. begin
  213. if hp.typ=ait_functype then
  214. begin
  215. result:=tai_functype(hp);
  216. exit;
  217. end;
  218. hp:=tai(hp.Next);
  219. end;
  220. end;
  221. procedure replace_local_frame_pointer(asmlist: TAsmList);
  222. var
  223. hp: tai;
  224. instr: taicpu;
  225. l: Integer;
  226. begin
  227. if not assigned(asmlist) then
  228. exit;
  229. hp:=tai(asmlist.first);
  230. while assigned(hp) do
  231. begin
  232. if hp.typ=ait_instruction then
  233. begin
  234. instr:=taicpu(hp);
  235. for l:=0 to instr.ops-1 do
  236. if (instr.oper[l]^.typ=top_reg) and (instr.oper[l]^.reg=NR_LOCAL_FRAME_POINTER_REG) then
  237. instr.loadref(l,tcpuprocdef(current_procinfo.procdef).frame_pointer_ref);
  238. end;
  239. hp:=tai(hp.Next);
  240. end;
  241. end;
  242. var
  243. templist : TAsmList;
  244. l : TWasmLocal;
  245. first: Boolean;
  246. local: tai_local;
  247. begin
  248. templist:=TAsmList.create;
  249. local:=nil;
  250. first:=true;
  251. l:=ttgwasm(tg).localvars.first;
  252. while Assigned(l) do
  253. begin
  254. local:=tai_local.create(l.typ);
  255. local.first:=first;
  256. first:=false;
  257. templist.Concat(local);
  258. l:=l.nextseq;
  259. end;
  260. if assigned(local) then
  261. local.last:=true;
  262. aktproccode.insertListAfter(findfirst_tai_functype(aktproccode),templist);
  263. templist.Free;
  264. replace_local_frame_pointer(aktproccode);
  265. inherited postprocess_code;
  266. end;
  267. procedure tcpuprocinfo.set_first_temp_offset;
  268. var
  269. sz : integer;
  270. i : integer;
  271. sym: tsym;
  272. begin
  273. {
  274. Stackframe layout:
  275. sp:
  276. <incoming parameters>
  277. sp+first_temp_offset:
  278. <locals>
  279. <temp>
  280. }
  281. procdef.init_paraloc_info(calleeside);
  282. sz := procdef.calleeargareasize;
  283. tg.setfirsttemp(sz);
  284. end;
  285. initialization
  286. cprocinfo:=tcpuprocinfo;
  287. end.