tgcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. index : integer;
  30. typ : TWasmBasicType;
  31. next : TWasmLocal; // next in the same basic type
  32. nextseq : TWasmLocal; // from 0 to max
  33. constructor create(atype: TWasmBasicType; aindex: integer);
  34. end;
  35. { TWasmLocalVars }
  36. TWasmLocalVars = class
  37. private
  38. last: TWasmLocal; // need public?
  39. public
  40. locv: array[TWasmBasicType] of TWasmLocal;
  41. ordered: array of integer;
  42. first: TWasmLocal; // first in sequence
  43. varindex: integer;
  44. constructor Create(astartindex: Integer = 0);
  45. destructor Destroy; override;
  46. function alloc(bt: TWasmBasicType): integer;
  47. procedure dealloc(bt: TWasmBasicType; index: integer);
  48. end;
  49. { ttgwasm }
  50. ttgwasm = class(ttgobj)
  51. //protected
  52. // procedure getimplicitobjtemp(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  53. // function getifspecialtemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference): boolean;
  54. procedure alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference); override;
  55. procedure updateFirstTemp;
  56. public
  57. localvars: TWasmLocalVars;
  58. constructor create; override;
  59. destructor destroy; override;
  60. procedure setfirsttemp(l : asizeint); override;
  61. procedure getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference); override;
  62. procedure gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference); override;
  63. procedure gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference); override;
  64. procedure allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  65. procedure deallocLocalVar(wbt: TWasmBasicType; idx: integer);
  66. procedure LocalVarToRef(idx: integer; size: Integer; out ref: treference);
  67. end;
  68. function defToWasmBasic(def: tdef; var wbt: TWasmBasicType): Boolean;
  69. implementation
  70. uses
  71. verbose,
  72. cgbase,
  73. symconst,symtable,symdef,symsym,symcpu,defutil,
  74. aasmbase,aasmcpu,
  75. hlcgobj,hlcgcpu, procinfo;
  76. function defToWasmBasic(def: tdef; var wbt: TWasmBasicType): Boolean;
  77. begin
  78. Result := assigned(def);
  79. if not Result then Exit;
  80. if is_pointer(def) then
  81. wbt := wbt_i32 // wasm32
  82. else if is_ordinal(def) then begin
  83. if is_64bit(def) then wbt := wbt_i64
  84. else wbt := wbt_i32;
  85. end else if is_real(def) then begin
  86. if is_single(def) then wbt := wbt_f32
  87. else wbt := wbt_f64; // real/double/extended
  88. end else
  89. Result := false;
  90. end;
  91. { TWasmLocal }
  92. constructor TWasmLocal.create(atype: TWasmBasicType;
  93. aindex: integer);
  94. begin
  95. typ:=atype;
  96. index:=aindex;
  97. end;
  98. { TWasmLocalVars }
  99. constructor TWasmLocalVars.Create(astartindex: Integer = 0);
  100. begin
  101. inherited Create;
  102. varindex := astartindex;
  103. end;
  104. destructor TWasmLocalVars.Destroy;
  105. var
  106. t : TWasmLocal;
  107. n : TWasmLocal;
  108. begin
  109. t := first;
  110. while Assigned(t) do
  111. begin
  112. n:=t;
  113. t:=t.nextseq;
  114. n.Free;
  115. end;
  116. inherited Destroy;
  117. end;
  118. { ttgwasm }
  119. //procedure ttgwasm.getimplicitobjtemp(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  120. // var
  121. // sym: tsym;
  122. // pd: tprocdef;
  123. // begin
  124. // gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  125. // list.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(tabstractrecorddef(def).jvm_full_typename(true),AT_METADATA)));
  126. // { the constructor doesn't return anything, so put a duplicate of the
  127. // self pointer on the evaluation stack for use as function result
  128. // after the constructor has run }
  129. // list.concat(taicpu.op_none(a_dup));
  130. // thlcgjvm(hlcg).incstack(list,2);
  131. // { call the constructor }
  132. // sym:=tsym(tabstractrecorddef(def).symtable.find('CREATE'));
  133. // if assigned(sym) and
  134. // (sym.typ=procsym) then
  135. // begin
  136. // pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  137. // if not assigned(pd) then
  138. // internalerror(2011032701);
  139. // end
  140. // else
  141. // internalerror(2011060301);
  142. // hlcg.a_call_name(list,pd,pd.mangledname,[],nil,false);
  143. // thlcgjvm(hlcg).decstack(list,1);
  144. // { store reference to instance }
  145. // thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  146. // end;
  147. //function ttgwasm.getifspecialtemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference): boolean;
  148. // var
  149. // eledef: tdef;
  150. // ndim: longint;
  151. // sym: tsym;
  152. // pd: tprocdef;
  153. // begin
  154. // result:=false;
  155. // case def.typ of
  156. // arraydef:
  157. // begin
  158. // if not is_dynamic_array(def) then
  159. // begin
  160. // { allocate an array of the right size }
  161. // gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  162. // ndim:=0;
  163. // eledef:=def;
  164. // repeat
  165. // if forcesize<>-1 then
  166. // thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,forcesize div tarraydef(eledef).elesize,R_INTREGISTER)
  167. // else
  168. // thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,tarraydef(eledef).elecount,R_INTREGISTER);
  169. // eledef:=tarraydef(eledef).elementdef;
  170. // inc(ndim);
  171. // forcesize:=-1;
  172. // until (eledef.typ<>arraydef) or
  173. // is_dynamic_array(eledef);
  174. // eledef:=tarraydef(def).elementdef;
  175. // thlcgjvm(hlcg).g_newarray(list,def,ndim);
  176. // thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  177. // result:=true;
  178. // end;
  179. // end;
  180. // recorddef:
  181. // begin
  182. // getimplicitobjtemp(list,def,temptype,ref);
  183. // result:=true;
  184. // end;
  185. // setdef:
  186. // begin
  187. // if tsetdef(def).elementdef.typ=enumdef then
  188. // begin
  189. // { load enum class type }
  190. // list.concat(taicpu.op_sym(a_ldc,current_asmdata.RefAsmSymbol(tcpuenumdef(tenumdef(tsetdef(def).elementdef).getbasedef).classdef.jvm_full_typename(true),AT_METADATA)));
  191. // thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  192. // { call tenumset.noneOf() class method }
  193. // sym:=tsym(tobjectdef(java_juenumset).symtable.find('NONEOF'));
  194. // if assigned(sym) and
  195. // (sym.typ=procsym) then
  196. // begin
  197. // if tprocsym(sym).procdeflist.Count<>1 then
  198. // internalerror(2011062801);
  199. // pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  200. // hlcg.a_call_name(list,pd,pd.mangledname,[],nil,false);
  201. // end;
  202. // { static calls method replaces parameter with set instance
  203. // -> no change in stack height }
  204. // end
  205. // else
  206. // begin
  207. // list.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(java_jubitset.jvm_full_typename(true),AT_METADATA)));
  208. // { the constructor doesn't return anything, so put a duplicate of the
  209. // self pointer on the evaluation stack for use as function result
  210. // after the constructor has run }
  211. // list.concat(taicpu.op_none(a_dup));
  212. // thlcgjvm(hlcg).incstack(list,2);
  213. // { call the constructor }
  214. // sym:=tsym(java_jubitset.symtable.find('CREATE'));
  215. // if assigned(sym) and
  216. // (sym.typ=procsym) then
  217. // begin
  218. // pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  219. // if not assigned(pd) then
  220. // internalerror(2011062802);
  221. // end
  222. // else
  223. // internalerror(2011062803);
  224. // hlcg.a_call_name(list,pd,pd.mangledname,[],nil,false);
  225. // { duplicate self pointer is removed }
  226. // thlcgjvm(hlcg).decstack(list,1);
  227. // end;
  228. // { store reference to instance }
  229. // gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  230. // thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  231. // result:=true;
  232. // end;
  233. // procvardef:
  234. // begin
  235. // if not tprocvardef(def).is_addressonly then
  236. // begin
  237. // getimplicitobjtemp(list,tcpuprocvardef(def).classdef,temptype,ref);
  238. // result:=true;
  239. // end;
  240. // end;
  241. // stringdef:
  242. // begin
  243. // if is_shortstring(def) then
  244. // begin
  245. // gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  246. // { add the maxlen parameter (s8inttype because parameters must
  247. // be sign extended) }
  248. // thlcgjvm(hlcg).a_load_const_stack(list,s8inttype,shortint(tstringdef(def).len),R_INTREGISTER);
  249. // { call the constructor }
  250. // sym:=tsym(tobjectdef(java_shortstring).symtable.find('CREATEEMPTY'));
  251. // if assigned(sym) and
  252. // (sym.typ=procsym) then
  253. // begin
  254. // if tprocsym(sym).procdeflist.Count<>1 then
  255. // internalerror(2011052404);
  256. // pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  257. // hlcg.a_call_name(list,pd,pd.mangledname,[],nil,false);
  258. // end;
  259. // { static calls method replaces parameter with string instance
  260. // -> no change in stack height }
  261. // { store reference to instance }
  262. // thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  263. // result:=true;
  264. // end;
  265. // end;
  266. // filedef:
  267. // begin
  268. // case tfiledef(def).filetyp of
  269. // ft_text:
  270. // result:=getifspecialtemp(list,search_system_type('TEXTREC').typedef,forcesize,temptype,ref);
  271. // ft_typed,
  272. // ft_untyped:
  273. // result:=getifspecialtemp(list,search_system_type('FILEREC').typedef,forcesize,temptype,ref);
  274. // end;
  275. // end;
  276. // else
  277. // ;
  278. // end;
  279. // end;
  280. procedure ttgwasm.alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference);
  281. begin
  282. Internalerror(2019091802);
  283. { the WebAssembly only supports 1 slot (= 4 bytes in FPC) and 2 slot (= 8 bytes in
  284. FPC) temps on the stack. double and int64 are 2 slots, the rest is one slot.
  285. There are no problems with reusing the same slot for a value of a different
  286. type. There are no alignment requirements either. }
  287. {if size<4 then
  288. size:=4;
  289. if not(size in [4,8]) then
  290. internalerror(2010121401);
  291. inherited alloctemp(list, size shr 2, 1, temptype, def, false, ref);}
  292. end;
  293. procedure ttgwasm.updateFirstTemp;
  294. begin
  295. firsttemp := localvars.varindex;
  296. if lasttemp<firsttemp then lasttemp := firsttemp;
  297. end;
  298. constructor ttgwasm.create;
  299. begin
  300. inherited create;
  301. direction := 1; // temp variables are allocated as "locals", and it starts with 0 and goes beyond!
  302. localvars:=TWasmLocalVars.Create;
  303. end;
  304. destructor ttgwasm.destroy;
  305. begin
  306. localvars.Free;
  307. inherited destroy;
  308. end;
  309. procedure ttgwasm.setfirsttemp(l: asizeint);
  310. begin
  311. firsttemp:=l;
  312. lasttemp:=l;
  313. localvars.varindex := l; //?
  314. end;
  315. procedure ttgwasm.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference);
  316. var
  317. wbt : TWasmBasicType;
  318. idx : integer;
  319. begin
  320. if defToWasmBasic(def, wbt) then
  321. alloclocalVarToRef(wbt, ref)
  322. else begin
  323. Internalerror(2019091801); // no support of structural type
  324. inherited;
  325. end;
  326. end;
  327. procedure ttgwasm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  328. begin
  329. inherited;
  330. end;
  331. procedure ttgwasm.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  332. begin
  333. inherited;
  334. end;
  335. procedure ttgwasm.allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  336. var
  337. idx : integer;
  338. begin
  339. idx := localvars.alloc(wbt);
  340. localVarToRef(idx, 1, ref);
  341. end;
  342. procedure ttgwasm.deallocLocalVar(wbt: TWasmBasicType; idx: integer);
  343. begin
  344. localvars.dealloc(wbt, idx);
  345. end;
  346. procedure ttgwasm.localVarToRef(idx: integer; size: integer; out ref: treference);
  347. var
  348. t: treftemppos;
  349. begin
  350. t.val:=idx;
  351. reference_reset_base(ref, current_procinfo.framepointer,idx,t,size,[]);
  352. ref.islocal := true;
  353. updateFirstTemp;
  354. end;
  355. function TWasmLocalVars.alloc(bt: TWasmBasicType): integer;
  356. var
  357. i : integer;
  358. lc : TWasmLocal;
  359. t : TWasmLocal;
  360. begin
  361. lc := locv[bt];
  362. t := nil;
  363. while Assigned(lc) and (lc.inuse) do begin
  364. t := lc;
  365. lc := lc.next;
  366. end;
  367. if Assigned(lc) then begin
  368. lc.inuse := true;
  369. end else begin
  370. lc := TWasmLocal.Create(bt, varindex);
  371. if Assigned(t)
  372. then t.next := lc
  373. else locv[bt]:=lc;
  374. lc.inuse:=true;
  375. inc(varindex);
  376. if Assigned(last) then last.nextseq := lc;
  377. if not Assigned(first) then first := lc;
  378. last := lc;
  379. end;
  380. alloc := lc.index;
  381. end;
  382. procedure TWasmLocalVars.dealloc(bt: TWasmBasicType; index: integer);
  383. var
  384. lc : TWasmLocal;
  385. begin
  386. lc := locv[bt];
  387. while Assigned(lc) and (lc.index <> index) do
  388. lc := lc.next;
  389. if Assigned(lc) then lc.inuse := false;
  390. end;
  391. initialization
  392. tgobjclass:=ttgwasm;
  393. end.