cpupi.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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,
  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;
  37. {*****************************************************************************
  38. twasmexceptionstatehandler_noexceptions
  39. *****************************************************************************}
  40. type
  41. twasmexceptionstatehandler_noexceptions = class(tcgexceptionstatehandler)
  42. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); override;
  43. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); override;
  44. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  45. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  46. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  47. end;
  48. class procedure twasmexceptionstatehandler_noexceptions.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  49. begin
  50. if not assigned(exceptionreasontype) then
  51. exceptionreasontype:=search_system_proc('fpc_setjmp').returndef;
  52. reference_reset(t.envbuf,0,[]);
  53. reference_reset(t.jmpbuf,0,[]);
  54. tg.gethltemp(list,exceptionreasontype,exceptionreasontype.size,tt_persistent,t.reasonbuf);
  55. end;
  56. class procedure twasmexceptionstatehandler_noexceptions.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  57. begin
  58. tg.ungettemp(list,t.reasonbuf);
  59. end;
  60. class procedure twasmexceptionstatehandler_noexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  61. begin
  62. exceptstate.exceptionlabel:=nil;
  63. exceptstate.oldflowcontrol:=flowcontrol;
  64. exceptstate.finallycodelabel:=nil;
  65. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  66. end;
  67. class procedure twasmexceptionstatehandler_noexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  68. begin
  69. end;
  70. class procedure twasmexceptionstatehandler_noexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  71. begin
  72. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  73. end;
  74. {*****************************************************************************
  75. twasmexceptionstatehandler_jsexceptions
  76. *****************************************************************************}
  77. type
  78. twasmexceptionstatehandler_jsexceptions = class(tcgexceptionstatehandler)
  79. class procedure get_exception_temps(list:TAsmList;var t:texceptiontemps); override;
  80. class procedure unget_exception_temps(list:TAsmList;const t:texceptiontemps); override;
  81. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  82. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  83. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  84. end;
  85. class procedure twasmexceptionstatehandler_jsexceptions.get_exception_temps(list:TAsmList;var t:texceptiontemps);
  86. begin
  87. if not assigned(exceptionreasontype) then
  88. exceptionreasontype:=search_system_proc('fpc_setjmp').returndef;
  89. reference_reset(t.envbuf,0,[]);
  90. reference_reset(t.jmpbuf,0,[]);
  91. tg.gethltemp(list,exceptionreasontype,exceptionreasontype.size,tt_persistent,t.reasonbuf);
  92. end;
  93. class procedure twasmexceptionstatehandler_jsexceptions.unget_exception_temps(list:TAsmList;const t:texceptiontemps);
  94. begin
  95. tg.ungettemp(list,t.reasonbuf);
  96. end;
  97. class procedure twasmexceptionstatehandler_jsexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  98. begin
  99. exceptstate.exceptionlabel:=nil;
  100. exceptstate.oldflowcontrol:=flowcontrol;
  101. exceptstate.finallycodelabel:=nil;
  102. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  103. end;
  104. class procedure twasmexceptionstatehandler_jsexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  105. begin
  106. end;
  107. class procedure twasmexceptionstatehandler_jsexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  108. begin
  109. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  110. end;
  111. {*****************************************************************************
  112. twasmexceptionstatehandler_nativeexceptions
  113. *****************************************************************************}
  114. type
  115. twasmexceptionstatehandler_nativeexceptions = class(tcgexceptionstatehandler)
  116. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  117. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  118. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  119. end;
  120. class procedure twasmexceptionstatehandler_nativeexceptions.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  121. begin
  122. list.Concat(tai_comment.Create(strpnew('TODO: new_exception')));
  123. end;
  124. class procedure twasmexceptionstatehandler_nativeexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  125. begin
  126. list.Concat(tai_comment.Create(strpnew('TODO: free_exception')));
  127. end;
  128. class procedure twasmexceptionstatehandler_nativeexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  129. begin
  130. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  131. end;
  132. {*****************************************************************************
  133. tcpuprocinfo
  134. *****************************************************************************}
  135. function tcpuprocinfo.calc_stackframe_size: longint;
  136. begin
  137. { the stack frame in WebAssembly should always have a 16-byte alignment }
  138. Result:=Align(inherited calc_stackframe_size,16);
  139. end;
  140. procedure tcpuprocinfo.setup_eh;
  141. begin
  142. if ts_wasm_native_exceptions in current_settings.targetswitches then
  143. cexceptionstatehandler:=twasmexceptionstatehandler_nativeexceptions
  144. else if ts_wasm_js_exceptions in current_settings.targetswitches then
  145. cexceptionstatehandler:=twasmexceptionstatehandler_jsexceptions
  146. else if ts_wasm_no_exceptions in current_settings.targetswitches then
  147. cexceptionstatehandler:=twasmexceptionstatehandler_noexceptions
  148. else
  149. internalerror(2021091701);
  150. end;
  151. procedure tcpuprocinfo.postprocess_code;
  152. function findfirst_tai_functype(asmlist: TAsmList): tai_functype;
  153. var
  154. hp: tai;
  155. begin
  156. result:=nil;
  157. if not assigned(asmlist) then
  158. exit;
  159. hp:=tai(asmlist.first);
  160. while assigned(hp) do
  161. begin
  162. if hp.typ=ait_functype then
  163. begin
  164. result:=tai_functype(hp);
  165. exit;
  166. end;
  167. hp:=tai(hp.Next);
  168. end;
  169. end;
  170. procedure replace_local_frame_pointer(asmlist: TAsmList);
  171. var
  172. hp: tai;
  173. instr: taicpu;
  174. l: Integer;
  175. begin
  176. if not assigned(asmlist) then
  177. exit;
  178. hp:=tai(asmlist.first);
  179. while assigned(hp) do
  180. begin
  181. if hp.typ=ait_instruction then
  182. begin
  183. instr:=taicpu(hp);
  184. for l:=0 to instr.ops-1 do
  185. if (instr.oper[l]^.typ=top_reg) and (instr.oper[l]^.reg=NR_LOCAL_FRAME_POINTER_REG) then
  186. instr.loadref(l,tcpuprocdef(current_procinfo.procdef).frame_pointer_ref);
  187. end;
  188. hp:=tai(hp.Next);
  189. end;
  190. end;
  191. var
  192. templist : TAsmList;
  193. l : TWasmLocal;
  194. first: Boolean;
  195. local: tai_local;
  196. begin
  197. templist:=TAsmList.create;
  198. local:=nil;
  199. first:=true;
  200. l:=ttgwasm(tg).localvars.first;
  201. while Assigned(l) do
  202. begin
  203. local:=tai_local.create(l.typ);
  204. local.first:=first;
  205. first:=false;
  206. templist.Concat(local);
  207. l:=l.nextseq;
  208. end;
  209. if assigned(local) then
  210. local.last:=true;
  211. aktproccode.insertListAfter(findfirst_tai_functype(aktproccode),templist);
  212. templist.Free;
  213. replace_local_frame_pointer(aktproccode);
  214. inherited postprocess_code;
  215. end;
  216. procedure tcpuprocinfo.set_first_temp_offset;
  217. var
  218. sz : integer;
  219. i : integer;
  220. sym: tsym;
  221. begin
  222. {
  223. Stackframe layout:
  224. sp:
  225. <incoming parameters>
  226. sp+first_temp_offset:
  227. <locals>
  228. <temp>
  229. }
  230. procdef.init_paraloc_info(calleeside);
  231. sz := procdef.calleeargareasize;
  232. tg.setfirsttemp(sz);
  233. end;
  234. initialization
  235. cprocinfo:=tcpuprocinfo;
  236. end.