tgcpu.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. {
  2. Copyright (C) 2019 Dmitry Boyarintsev
  3. This unit handles the temporary variables for the WebAssembly
  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 tgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmdata,
  23. cgutils, cpubase,
  24. symtype,tgobj;
  25. type
  26. { TWasmLocal }
  27. TWasmLocal = class
  28. inuse : Boolean;
  29. noreuse : Boolean;
  30. index : integer;
  31. typ : TWasmBasicType;
  32. next : TWasmLocal; // next in the same basic type
  33. nextseq : TWasmLocal; // from 0 to max
  34. constructor create(atype: TWasmBasicType; aindex: integer);
  35. end;
  36. { TWasmLocalVars }
  37. TWasmLocalVars = class
  38. private
  39. last: TWasmLocal; // need public?
  40. public
  41. locv: array[TWasmBasicType] of TWasmLocal;
  42. ordered: array of integer;
  43. first: TWasmLocal; // first in sequence
  44. varindex: integer;
  45. constructor Create(astartindex: Integer = 0);
  46. destructor Destroy; override;
  47. function alloc(bt: TWasmBasicType): integer;
  48. function allocnoreuse(bt: TWasmBasicType): integer;
  49. procedure dealloc(bt: TWasmBasicType; index: integer);
  50. procedure dealloc(index: integer);
  51. end;
  52. { ttgwasm }
  53. ttgwasm = class(ttgobj)
  54. private
  55. procedure updateFirstTemp;
  56. procedure allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  57. procedure allocLocalVarNoReuseToRef(wbt: TWasmBasicType; out ref: treference);
  58. procedure LocalVarToRef(idx: integer; size: Integer; out ref: treference);
  59. public
  60. localvars: TWasmLocalVars;
  61. constructor create; override;
  62. destructor destroy; override;
  63. procedure setfirsttemp(l : asizeint); override;
  64. procedure gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference); override;
  65. procedure gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference); override;
  66. procedure ungettemp(list: TAsmList; const ref : treference); override;
  67. procedure allocframepointer(list: TAsmList; out ref: treference);
  68. procedure allocbasepointer(list: TAsmList; out ref: treference);
  69. end;
  70. function defToWasmBasic(def: tdef; var wbt: TWasmBasicType): Boolean;
  71. implementation
  72. uses
  73. verbose,
  74. cgbase,
  75. symconst,symtable,symdef,symsym,symcpu,defutil,
  76. aasmbase,aasmcpu,
  77. hlcgobj,hlcgcpu, procinfo;
  78. function defToWasmBasic(def: tdef; var wbt: TWasmBasicType): Boolean;
  79. begin
  80. Result := assigned(def);
  81. if not Result then Exit;
  82. if is_pointer(def) then
  83. wbt := wbt_i32 // wasm32
  84. else if is_ordinal(def) then begin
  85. if is_64bit(def) then wbt := wbt_i64
  86. else wbt := wbt_i32;
  87. end else if is_real(def) then begin
  88. if is_single(def) then wbt := wbt_f32
  89. else wbt := wbt_f64; // real/double/extended
  90. end else
  91. Result := false;
  92. end;
  93. { TWasmLocal }
  94. constructor TWasmLocal.create(atype: TWasmBasicType;
  95. aindex: integer);
  96. begin
  97. typ:=atype;
  98. index:=aindex;
  99. end;
  100. { TWasmLocalVars }
  101. constructor TWasmLocalVars.Create(astartindex: Integer = 0);
  102. begin
  103. inherited Create;
  104. varindex := astartindex;
  105. end;
  106. destructor TWasmLocalVars.Destroy;
  107. var
  108. t : TWasmLocal;
  109. n : TWasmLocal;
  110. begin
  111. t := first;
  112. while Assigned(t) do
  113. begin
  114. n:=t;
  115. t:=t.nextseq;
  116. n.Free;
  117. end;
  118. inherited Destroy;
  119. end;
  120. { ttgwasm }
  121. procedure ttgwasm.updateFirstTemp;
  122. begin
  123. firsttemp := localvars.varindex;
  124. if lasttemp<firsttemp then lasttemp := firsttemp;
  125. end;
  126. constructor ttgwasm.create;
  127. begin
  128. inherited create;
  129. localvars:=TWasmLocalVars.Create;
  130. end;
  131. destructor ttgwasm.destroy;
  132. begin
  133. localvars.Free;
  134. inherited destroy;
  135. end;
  136. procedure ttgwasm.setfirsttemp(l: asizeint);
  137. begin
  138. firsttemp:=l;
  139. lasttemp:=l;
  140. localvars.varindex := l; //?
  141. end;
  142. procedure ttgwasm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  143. var
  144. wbt: TWasmBasicType;
  145. begin
  146. if temptype=tt_regallocator then
  147. begin
  148. if Assigned(def) and defToWasmBasic(def, wbt) then
  149. allocLocalVarToRef(wbt, ref)
  150. else
  151. internalerror(2020121801);
  152. end
  153. else
  154. inherited;
  155. end;
  156. procedure ttgwasm.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  157. begin
  158. inherited;
  159. end;
  160. procedure ttgwasm.ungettemp(list: TAsmList; const ref: treference);
  161. begin
  162. if ref.base=NR_LOCAL_STACK_POINTER_REG then
  163. localvars.dealloc(ref.offset)
  164. else
  165. inherited;
  166. end;
  167. procedure ttgwasm.allocframepointer(list: TAsmList; out ref: treference);
  168. begin
  169. allocLocalVarNoReuseToRef(wbt_i32,ref);
  170. end;
  171. procedure ttgwasm.allocbasepointer(list: TAsmList; out ref: treference);
  172. begin
  173. allocLocalVarNoReuseToRef(wbt_i32,ref);
  174. end;
  175. procedure ttgwasm.allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  176. var
  177. idx : integer;
  178. begin
  179. idx := localvars.alloc(wbt);
  180. localVarToRef(idx, 1, ref);
  181. end;
  182. procedure ttgwasm.allocLocalVarNoReuseToRef(wbt: TWasmBasicType; out ref: treference);
  183. var
  184. idx : integer;
  185. begin
  186. idx := localvars.allocnoreuse(wbt);
  187. localVarToRef(idx, 1, ref);
  188. end;
  189. procedure ttgwasm.localVarToRef(idx: integer; size: integer; out ref: treference);
  190. begin
  191. reference_reset_base(ref,NR_LOCAL_STACK_POINTER_REG,idx,ctempposinvalid,size,[]);
  192. updateFirstTemp;
  193. end;
  194. function TWasmLocalVars.alloc(bt: TWasmBasicType): integer;
  195. var
  196. i : integer;
  197. lc : TWasmLocal;
  198. t : TWasmLocal;
  199. begin
  200. lc := locv[bt];
  201. t := nil;
  202. while Assigned(lc) and ((lc.inuse) or (lc.noreuse)) do begin
  203. t := lc;
  204. lc := lc.next;
  205. end;
  206. if Assigned(lc) then begin
  207. lc.inuse := true;
  208. end else begin
  209. lc := TWasmLocal.Create(bt, varindex);
  210. if Assigned(t)
  211. then t.next := lc
  212. else locv[bt]:=lc;
  213. lc.inuse:=true;
  214. inc(varindex);
  215. if Assigned(last) then last.nextseq := lc;
  216. if not Assigned(first) then first := lc;
  217. last := lc;
  218. end;
  219. alloc := lc.index;
  220. end;
  221. function TWasmLocalVars.allocnoreuse(bt: TWasmBasicType): integer;
  222. var
  223. i : integer;
  224. lc : TWasmLocal;
  225. t : TWasmLocal;
  226. begin
  227. lc := locv[bt];
  228. t := nil;
  229. while Assigned(lc) do
  230. begin
  231. t := lc;
  232. lc := lc.next;
  233. end;
  234. lc := TWasmLocal.Create(bt, varindex);
  235. if Assigned(t) then
  236. t.next := lc
  237. else
  238. locv[bt]:=lc;
  239. lc.inuse:=true;
  240. lc.noreuse:=true;
  241. inc(varindex);
  242. if Assigned(last) then
  243. last.nextseq := lc;
  244. if not Assigned(first) then
  245. first := lc;
  246. last := lc;
  247. allocnoreuse := lc.index;
  248. end;
  249. procedure TWasmLocalVars.dealloc(bt: TWasmBasicType; index: integer);
  250. var
  251. lc : TWasmLocal;
  252. begin
  253. lc := locv[bt];
  254. while Assigned(lc) and (lc.index <> index) do
  255. lc := lc.next;
  256. if Assigned(lc) then lc.inuse := false;
  257. end;
  258. procedure TWasmLocalVars.dealloc(index: integer);
  259. var
  260. lc : TWasmLocal;
  261. begin
  262. lc := first;
  263. while Assigned(lc) and (lc.index <> index) do
  264. lc := lc.nextseq;
  265. if Assigned(lc) then lc.inuse := false;
  266. end;
  267. initialization
  268. tgobjclass:=ttgwasm;
  269. end.