cpupi.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. exceptstate.exceptionlabel:=nil;
  123. exceptstate.oldflowcontrol:=flowcontrol;
  124. exceptstate.finallycodelabel:=nil;
  125. flowcontrol:=[fc_inflowcontrol,fc_catching_exceptions];
  126. end;
  127. class procedure twasmexceptionstatehandler_nativeexceptions.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  128. begin
  129. end;
  130. class procedure twasmexceptionstatehandler_nativeexceptions.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  131. begin
  132. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  133. end;
  134. {*****************************************************************************
  135. tcpuprocinfo
  136. *****************************************************************************}
  137. function tcpuprocinfo.calc_stackframe_size: longint;
  138. begin
  139. { the stack frame in WebAssembly should always have a 16-byte alignment }
  140. Result:=Align(inherited calc_stackframe_size,16);
  141. end;
  142. procedure tcpuprocinfo.setup_eh;
  143. begin
  144. if ts_wasm_native_exceptions in current_settings.targetswitches then
  145. cexceptionstatehandler:=twasmexceptionstatehandler_nativeexceptions
  146. else if ts_wasm_js_exceptions in current_settings.targetswitches then
  147. cexceptionstatehandler:=twasmexceptionstatehandler_jsexceptions
  148. else if ts_wasm_no_exceptions in current_settings.targetswitches then
  149. cexceptionstatehandler:=twasmexceptionstatehandler_noexceptions
  150. else
  151. internalerror(2021091701);
  152. end;
  153. procedure tcpuprocinfo.postprocess_code;
  154. function findfirst_tai_functype(asmlist: TAsmList): tai_functype;
  155. var
  156. hp: tai;
  157. begin
  158. result:=nil;
  159. if not assigned(asmlist) then
  160. exit;
  161. hp:=tai(asmlist.first);
  162. while assigned(hp) do
  163. begin
  164. if hp.typ=ait_functype then
  165. begin
  166. result:=tai_functype(hp);
  167. exit;
  168. end;
  169. hp:=tai(hp.Next);
  170. end;
  171. end;
  172. procedure replace_local_frame_pointer(asmlist: TAsmList);
  173. var
  174. hp: tai;
  175. instr: taicpu;
  176. l: Integer;
  177. begin
  178. if not assigned(asmlist) then
  179. exit;
  180. hp:=tai(asmlist.first);
  181. while assigned(hp) do
  182. begin
  183. if hp.typ=ait_instruction then
  184. begin
  185. instr:=taicpu(hp);
  186. for l:=0 to instr.ops-1 do
  187. if (instr.oper[l]^.typ=top_reg) and (instr.oper[l]^.reg=NR_LOCAL_FRAME_POINTER_REG) then
  188. instr.loadref(l,tcpuprocdef(current_procinfo.procdef).frame_pointer_ref);
  189. end;
  190. hp:=tai(hp.Next);
  191. end;
  192. end;
  193. var
  194. templist : TAsmList;
  195. l : TWasmLocal;
  196. first: Boolean;
  197. local: tai_local;
  198. begin
  199. templist:=TAsmList.create;
  200. local:=nil;
  201. first:=true;
  202. l:=ttgwasm(tg).localvars.first;
  203. while Assigned(l) do
  204. begin
  205. local:=tai_local.create(l.typ);
  206. local.first:=first;
  207. first:=false;
  208. templist.Concat(local);
  209. l:=l.nextseq;
  210. end;
  211. if assigned(local) then
  212. local.last:=true;
  213. aktproccode.insertListAfter(findfirst_tai_functype(aktproccode),templist);
  214. templist.Free;
  215. replace_local_frame_pointer(aktproccode);
  216. inherited postprocess_code;
  217. end;
  218. procedure tcpuprocinfo.set_first_temp_offset;
  219. var
  220. sz : integer;
  221. i : integer;
  222. sym: tsym;
  223. begin
  224. {
  225. Stackframe layout:
  226. sp:
  227. <incoming parameters>
  228. sp+first_temp_offset:
  229. <locals>
  230. <temp>
  231. }
  232. procdef.init_paraloc_info(calleeside);
  233. sz := procdef.calleeargareasize;
  234. tg.setfirsttemp(sz);
  235. end;
  236. initialization
  237. cprocinfo:=tcpuprocinfo;
  238. end.