tgcpu.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_currency(def) then
  85. wbt := wbt_i64
  86. else if is_ordinal(def) then begin
  87. if is_64bit(def) then wbt := wbt_i64
  88. else wbt := wbt_i32;
  89. end else if is_real(def) then begin
  90. if is_single(def) then wbt := wbt_f32
  91. else wbt := wbt_f64; // real/double/extended
  92. end else
  93. Result := false;
  94. end;
  95. { TWasmLocal }
  96. constructor TWasmLocal.create(atype: TWasmBasicType;
  97. aindex: integer);
  98. begin
  99. typ:=atype;
  100. index:=aindex;
  101. end;
  102. { TWasmLocalVars }
  103. constructor TWasmLocalVars.Create(astartindex: Integer = 0);
  104. begin
  105. inherited Create;
  106. varindex := astartindex;
  107. end;
  108. destructor TWasmLocalVars.Destroy;
  109. var
  110. t : TWasmLocal;
  111. n : TWasmLocal;
  112. begin
  113. t := first;
  114. while Assigned(t) do
  115. begin
  116. n:=t;
  117. t:=t.nextseq;
  118. n.Free;
  119. end;
  120. inherited Destroy;
  121. end;
  122. { ttgwasm }
  123. procedure ttgwasm.updateFirstTemp;
  124. begin
  125. firsttemp := localvars.varindex;
  126. if lasttemp<firsttemp then lasttemp := firsttemp;
  127. end;
  128. constructor ttgwasm.create;
  129. begin
  130. inherited create;
  131. localvars:=TWasmLocalVars.Create;
  132. end;
  133. destructor ttgwasm.destroy;
  134. begin
  135. localvars.Free;
  136. inherited destroy;
  137. end;
  138. procedure ttgwasm.setfirsttemp(l: asizeint);
  139. begin
  140. firsttemp:=l;
  141. lasttemp:=l;
  142. localvars.varindex := l; //?
  143. end;
  144. procedure ttgwasm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  145. var
  146. wbt: TWasmBasicType;
  147. begin
  148. if temptype=tt_regallocator then
  149. begin
  150. if Assigned(def) and defToWasmBasic(def, wbt) then
  151. allocLocalVarToRef(wbt, ref)
  152. else
  153. internalerror(2020121801);
  154. end
  155. else
  156. inherited;
  157. end;
  158. procedure ttgwasm.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  159. begin
  160. inherited;
  161. end;
  162. procedure ttgwasm.ungettemp(list: TAsmList; const ref: treference);
  163. begin
  164. if ref.base=NR_LOCAL_STACK_POINTER_REG then
  165. localvars.dealloc(ref.offset)
  166. else
  167. inherited;
  168. end;
  169. procedure ttgwasm.allocframepointer(list: TAsmList; out ref: treference);
  170. begin
  171. allocLocalVarNoReuseToRef(wbt_i32,ref);
  172. end;
  173. procedure ttgwasm.allocbasepointer(list: TAsmList; out ref: treference);
  174. begin
  175. allocLocalVarNoReuseToRef(wbt_i32,ref);
  176. end;
  177. procedure ttgwasm.allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  178. var
  179. idx : integer;
  180. begin
  181. idx := localvars.alloc(wbt);
  182. localVarToRef(idx, 1, ref);
  183. end;
  184. procedure ttgwasm.allocLocalVarNoReuseToRef(wbt: TWasmBasicType; out ref: treference);
  185. var
  186. idx : integer;
  187. begin
  188. idx := localvars.allocnoreuse(wbt);
  189. localVarToRef(idx, 1, ref);
  190. end;
  191. procedure ttgwasm.localVarToRef(idx: integer; size: integer; out ref: treference);
  192. begin
  193. reference_reset_base(ref,NR_LOCAL_STACK_POINTER_REG,idx,ctempposinvalid,size,[]);
  194. updateFirstTemp;
  195. end;
  196. function TWasmLocalVars.alloc(bt: TWasmBasicType): integer;
  197. var
  198. i : integer;
  199. lc : TWasmLocal;
  200. t : TWasmLocal;
  201. begin
  202. lc := locv[bt];
  203. t := nil;
  204. while Assigned(lc) and ((lc.inuse) or (lc.noreuse)) do begin
  205. t := lc;
  206. lc := lc.next;
  207. end;
  208. if Assigned(lc) then begin
  209. lc.inuse := true;
  210. end else begin
  211. lc := TWasmLocal.Create(bt, varindex);
  212. if Assigned(t)
  213. then t.next := lc
  214. else locv[bt]:=lc;
  215. lc.inuse:=true;
  216. inc(varindex);
  217. if Assigned(last) then last.nextseq := lc;
  218. if not Assigned(first) then first := lc;
  219. last := lc;
  220. end;
  221. alloc := lc.index;
  222. end;
  223. function TWasmLocalVars.allocnoreuse(bt: TWasmBasicType): integer;
  224. var
  225. i : integer;
  226. lc : TWasmLocal;
  227. t : TWasmLocal;
  228. begin
  229. lc := locv[bt];
  230. t := nil;
  231. while Assigned(lc) do
  232. begin
  233. t := lc;
  234. lc := lc.next;
  235. end;
  236. lc := TWasmLocal.Create(bt, varindex);
  237. if Assigned(t) then
  238. t.next := lc
  239. else
  240. locv[bt]:=lc;
  241. lc.inuse:=true;
  242. lc.noreuse:=true;
  243. inc(varindex);
  244. if Assigned(last) then
  245. last.nextseq := lc;
  246. if not Assigned(first) then
  247. first := lc;
  248. last := lc;
  249. allocnoreuse := lc.index;
  250. end;
  251. procedure TWasmLocalVars.dealloc(bt: TWasmBasicType; index: integer);
  252. var
  253. lc : TWasmLocal;
  254. begin
  255. lc := locv[bt];
  256. while Assigned(lc) and (lc.index <> index) do
  257. lc := lc.next;
  258. if Assigned(lc) then lc.inuse := false;
  259. end;
  260. procedure TWasmLocalVars.dealloc(index: integer);
  261. var
  262. lc : TWasmLocal;
  263. begin
  264. lc := first;
  265. while Assigned(lc) and (lc.index <> index) do
  266. lc := lc.nextseq;
  267. if Assigned(lc) then lc.inuse := false;
  268. end;
  269. initialization
  270. tgobjclass:=ttgwasm;
  271. end.