tgcpu.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. var
  80. fields, i: Integer;
  81. wbt_candidate: TWasmBasicType;
  82. begin
  83. Result := assigned(def);
  84. if not Result then
  85. Exit;
  86. if (def.typ=procvardef) and (po_wasm_funcref in tprocvardef(def).procoptions) then
  87. wbt := wbt_funcref
  88. else if is_pointer(def) then
  89. wbt := wbt_i32 // wasm32
  90. else if is_currency(def) then
  91. wbt := wbt_i64
  92. else if is_ordinal(def) then
  93. begin
  94. if is_64bit(def) then
  95. wbt := wbt_i64
  96. else
  97. wbt := wbt_i32;
  98. end
  99. else if is_real(def) then
  100. begin
  101. if is_single(def) then
  102. wbt := wbt_f32
  103. else
  104. wbt := wbt_f64; // real/double/extended
  105. end
  106. else if def.typ=recorddef then
  107. begin
  108. if not (def.size in [1,2,4,8]) then
  109. exit(false);
  110. fields:=0;
  111. wbt_candidate:=Default(TWasmBasicType);
  112. for i:=0 to trecorddef(def).symtable.symlist.count-1 do
  113. begin
  114. if (tsym(trecorddef(def).symtable.symlist[i]).typ<>fieldvarsym) or
  115. (sp_static in tsym(trecorddef(def).symtable.symlist[i]).symoptions) then
  116. continue;
  117. if assigned(tfieldvarsym(trecorddef(def).symtable.symlist[i]).vardef) then
  118. begin
  119. Inc(fields);
  120. if fields>1 then
  121. exit(false);
  122. { search recursively }
  123. if not defToWasmBasic(tfieldvarsym(trecorddef(def).symtable.symlist[i]).vardef,wbt_candidate) then
  124. exit(false);
  125. end;
  126. end;
  127. if fields=1 then
  128. begin
  129. wbt:=wbt_candidate;
  130. result:=true;
  131. end
  132. else
  133. result:=false;
  134. end
  135. else if def.typ=arraydef then
  136. begin
  137. if (def.size in [1,2,4,8]) and (tarraydef(def).elecount=1) then
  138. result:=defToWasmBasic(tarraydef(def).elementdef,wbt)
  139. else
  140. result:=false;
  141. end
  142. else
  143. Result := false;
  144. end;
  145. { TWasmLocal }
  146. constructor TWasmLocal.create(atype: TWasmBasicType;
  147. aindex: integer);
  148. begin
  149. typ:=atype;
  150. index:=aindex;
  151. end;
  152. { TWasmLocalVars }
  153. constructor TWasmLocalVars.Create(astartindex: Integer = 0);
  154. begin
  155. inherited Create;
  156. varindex := astartindex;
  157. end;
  158. destructor TWasmLocalVars.Destroy;
  159. var
  160. t : TWasmLocal;
  161. n : TWasmLocal;
  162. begin
  163. t := first;
  164. while Assigned(t) do
  165. begin
  166. n:=t;
  167. t:=t.nextseq;
  168. n.Free;
  169. end;
  170. inherited Destroy;
  171. end;
  172. { ttgwasm }
  173. procedure ttgwasm.updateFirstTemp;
  174. begin
  175. firsttemp := localvars.varindex;
  176. if lasttemp<firsttemp then lasttemp := firsttemp;
  177. end;
  178. constructor ttgwasm.create;
  179. begin
  180. inherited create;
  181. localvars:=TWasmLocalVars.Create;
  182. end;
  183. destructor ttgwasm.destroy;
  184. begin
  185. localvars.Free;
  186. inherited destroy;
  187. end;
  188. procedure ttgwasm.setfirsttemp(l: asizeint);
  189. begin
  190. firsttemp:=l;
  191. lasttemp:=l;
  192. localvars.varindex := l; //?
  193. end;
  194. procedure ttgwasm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  195. var
  196. wbt: TWasmBasicType;
  197. begin
  198. if temptype=tt_regallocator then
  199. begin
  200. if Assigned(def) and defToWasmBasic(def, wbt) then
  201. allocLocalVarToRef(wbt, ref)
  202. else
  203. internalerror(2020121801);
  204. end
  205. else
  206. inherited;
  207. end;
  208. procedure ttgwasm.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  209. begin
  210. inherited;
  211. end;
  212. procedure ttgwasm.ungettemp(list: TAsmList; const ref: treference);
  213. begin
  214. if ref.base=NR_LOCAL_STACK_POINTER_REG then
  215. localvars.dealloc(ref.offset)
  216. else
  217. inherited;
  218. end;
  219. procedure ttgwasm.allocframepointer(list: TAsmList; out ref: treference);
  220. begin
  221. allocLocalVarNoReuseToRef(wbt_i32,ref);
  222. end;
  223. procedure ttgwasm.allocbasepointer(list: TAsmList; out ref: treference);
  224. begin
  225. allocLocalVarNoReuseToRef(wbt_i32,ref);
  226. end;
  227. procedure ttgwasm.allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  228. var
  229. idx : integer;
  230. begin
  231. idx := localvars.alloc(wbt);
  232. localVarToRef(idx, 1, ref);
  233. end;
  234. procedure ttgwasm.allocLocalVarNoReuseToRef(wbt: TWasmBasicType; out ref: treference);
  235. var
  236. idx : integer;
  237. begin
  238. idx := localvars.allocnoreuse(wbt);
  239. localVarToRef(idx, 1, ref);
  240. end;
  241. procedure ttgwasm.localVarToRef(idx: integer; size: integer; out ref: treference);
  242. begin
  243. reference_reset_base(ref,NR_LOCAL_STACK_POINTER_REG,idx,ctempposinvalid,size,[]);
  244. updateFirstTemp;
  245. end;
  246. function TWasmLocalVars.alloc(bt: TWasmBasicType): integer;
  247. var
  248. i : integer;
  249. lc : TWasmLocal;
  250. t : TWasmLocal;
  251. begin
  252. lc := locv[bt];
  253. t := nil;
  254. while Assigned(lc) and ((lc.inuse) or (lc.noreuse)) do begin
  255. t := lc;
  256. lc := lc.next;
  257. end;
  258. if Assigned(lc) then begin
  259. lc.inuse := true;
  260. end else begin
  261. lc := TWasmLocal.Create(bt, varindex);
  262. if Assigned(t)
  263. then t.next := lc
  264. else locv[bt]:=lc;
  265. lc.inuse:=true;
  266. inc(varindex);
  267. if Assigned(last) then last.nextseq := lc;
  268. if not Assigned(first) then first := lc;
  269. last := lc;
  270. end;
  271. alloc := lc.index;
  272. end;
  273. function TWasmLocalVars.allocnoreuse(bt: TWasmBasicType): integer;
  274. var
  275. i : integer;
  276. lc : TWasmLocal;
  277. t : TWasmLocal;
  278. begin
  279. lc := locv[bt];
  280. t := nil;
  281. while Assigned(lc) do
  282. begin
  283. t := lc;
  284. lc := lc.next;
  285. end;
  286. lc := TWasmLocal.Create(bt, varindex);
  287. if Assigned(t) then
  288. t.next := lc
  289. else
  290. locv[bt]:=lc;
  291. lc.inuse:=true;
  292. lc.noreuse:=true;
  293. inc(varindex);
  294. if Assigned(last) then
  295. last.nextseq := lc;
  296. if not Assigned(first) then
  297. first := lc;
  298. last := lc;
  299. allocnoreuse := lc.index;
  300. end;
  301. procedure TWasmLocalVars.dealloc(bt: TWasmBasicType; index: integer);
  302. var
  303. lc : TWasmLocal;
  304. begin
  305. lc := locv[bt];
  306. while Assigned(lc) and (lc.index <> index) do
  307. lc := lc.next;
  308. if Assigned(lc) then lc.inuse := false;
  309. end;
  310. procedure TWasmLocalVars.dealloc(index: integer);
  311. var
  312. lc : TWasmLocal;
  313. begin
  314. lc := first;
  315. while Assigned(lc) and (lc.index <> index) do
  316. lc := lc.nextseq;
  317. if Assigned(lc) then lc.inuse := false;
  318. end;
  319. initialization
  320. tgobjclass:=ttgwasm;
  321. end.