tgcpu.pas 10 KB

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