tgcpu.pas 10 KB

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