tgcpu.pas 11 KB

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