cpupi.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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,globals,cpubase,tgcpu,aasmdata,aasmcpu,aasmtai,cgexcept,
  36. tgobj,paramgr,symconst,symcpu;
  37. {*****************************************************************************
  38. twasmexceptionstatehandler
  39. *****************************************************************************}
  40. type
  41. twasmexceptionstatehandler = class(tcgexceptionstatehandler)
  42. class procedure new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate); override;
  43. class procedure free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean); override;
  44. class procedure handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate); override;
  45. end;
  46. class procedure twasmexceptionstatehandler.new_exception(list:TAsmList;const t:texceptiontemps; const exceptframekind: texceptframekind; out exceptstate: texceptionstate);
  47. begin
  48. list.Concat(tai_comment.Create(strpnew('TODO: new_exception')));
  49. end;
  50. class procedure twasmexceptionstatehandler.free_exception(list: TAsmList; const t: texceptiontemps; const s: texceptionstate; a: aint; endexceptlabel: tasmlabel; onlyfree:boolean);
  51. begin
  52. list.Concat(tai_comment.Create(strpnew('TODO: free_exception')));
  53. end;
  54. class procedure twasmexceptionstatehandler.handle_nested_exception(list:TAsmList;var t:texceptiontemps;var entrystate: texceptionstate);
  55. begin
  56. list.Concat(tai_comment.Create(strpnew('TODO: handle_nested_exception')));
  57. end;
  58. {*****************************************************************************
  59. tcpuprocinfo
  60. *****************************************************************************}
  61. function tcpuprocinfo.calc_stackframe_size: longint;
  62. begin
  63. { the stack frame in WebAssembly should always have a 16-byte alignment }
  64. Result:=Align(inherited calc_stackframe_size,16);
  65. end;
  66. procedure tcpuprocinfo.setup_eh;
  67. begin
  68. cexceptionstatehandler:=twasmexceptionstatehandler;
  69. end;
  70. procedure tcpuprocinfo.postprocess_code;
  71. function findfirst_tai_functype(asmlist: TAsmList): tai_functype;
  72. var
  73. hp: tai;
  74. begin
  75. result:=nil;
  76. if not assigned(asmlist) then
  77. exit;
  78. hp:=tai(asmlist.first);
  79. while assigned(hp) do
  80. begin
  81. if hp.typ=ait_functype then
  82. begin
  83. result:=tai_functype(hp);
  84. exit;
  85. end;
  86. hp:=tai(hp.Next);
  87. end;
  88. end;
  89. procedure replace_local_frame_pointer(asmlist: TAsmList);
  90. var
  91. hp: tai;
  92. instr: taicpu;
  93. l: Integer;
  94. begin
  95. if not assigned(asmlist) then
  96. exit;
  97. hp:=tai(asmlist.first);
  98. while assigned(hp) do
  99. begin
  100. if hp.typ=ait_instruction then
  101. begin
  102. instr:=taicpu(hp);
  103. for l:=0 to instr.ops-1 do
  104. if (instr.oper[l]^.typ=top_reg) and (instr.oper[l]^.reg=NR_LOCAL_FRAME_POINTER_REG) then
  105. instr.loadref(l,tcpuprocdef(current_procinfo.procdef).frame_pointer_ref);
  106. end;
  107. hp:=tai(hp.Next);
  108. end;
  109. end;
  110. var
  111. templist : TAsmList;
  112. l : TWasmLocal;
  113. first: Boolean;
  114. local: tai_local;
  115. begin
  116. templist:=TAsmList.create;
  117. local:=nil;
  118. first:=true;
  119. l:=ttgwasm(tg).localvars.first;
  120. while Assigned(l) do
  121. begin
  122. local:=tai_local.create(l.typ);
  123. local.first:=first;
  124. first:=false;
  125. templist.Concat(local);
  126. l:=l.nextseq;
  127. end;
  128. if assigned(local) then
  129. local.last:=true;
  130. aktproccode.insertListAfter(findfirst_tai_functype(aktproccode),templist);
  131. templist.Free;
  132. replace_local_frame_pointer(aktproccode);
  133. inherited postprocess_code;
  134. end;
  135. procedure tcpuprocinfo.set_first_temp_offset;
  136. var
  137. sz : integer;
  138. i : integer;
  139. sym: tsym;
  140. begin
  141. {
  142. Stackframe layout:
  143. sp:
  144. <incoming parameters>
  145. sp+first_temp_offset:
  146. <locals>
  147. <temp>
  148. }
  149. procdef.init_paraloc_info(calleeside);
  150. sz := procdef.calleeargareasize;
  151. tg.setfirsttemp(sz);
  152. end;
  153. initialization
  154. cprocinfo:=tcpuprocinfo;
  155. end.