tgcpu.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. procedure dealloc(index: integer);
  49. end;
  50. { ttgwasm }
  51. ttgwasm = class(ttgobj)
  52. private
  53. procedure updateFirstTemp;
  54. procedure allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  55. procedure LocalVarToRef(idx: integer; size: Integer; out ref: treference);
  56. protected
  57. procedure alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference); override;
  58. public
  59. localvars: TWasmLocalVars;
  60. constructor create; override;
  61. destructor destroy; override;
  62. procedure setfirsttemp(l : asizeint); override;
  63. procedure getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference); 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. 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.alloctemp(list: TAsmList; size: asizeint; alignment: shortint; temptype: ttemptype; def: tdef; fini: boolean; out ref: treference);
  120. begin
  121. inherited;
  122. //Internalerror(2019091802);
  123. { the WebAssembly only supports 1 slot (= 4 bytes in FPC) and 2 slot (= 8 bytes in
  124. FPC) temps on the stack. double and int64 are 2 slots, the rest is one slot.
  125. There are no problems with reusing the same slot for a value of a different
  126. type. There are no alignment requirements either. }
  127. {if size<4 then
  128. size:=4;
  129. if not(size in [4,8]) then
  130. internalerror(2010121401);
  131. inherited alloctemp(list, size shr 2, 1, temptype, def, false, ref);}
  132. end;
  133. procedure ttgwasm.updateFirstTemp;
  134. begin
  135. firsttemp := localvars.varindex;
  136. if lasttemp<firsttemp then lasttemp := firsttemp;
  137. end;
  138. constructor ttgwasm.create;
  139. begin
  140. inherited create;
  141. direction := 1; // temp variables are allocated as "locals", and it starts with 0 and goes beyond!
  142. localvars:=TWasmLocalVars.Create;
  143. end;
  144. destructor ttgwasm.destroy;
  145. begin
  146. localvars.Free;
  147. inherited destroy;
  148. end;
  149. procedure ttgwasm.setfirsttemp(l: asizeint);
  150. begin
  151. firsttemp:=l;
  152. lasttemp:=l;
  153. localvars.varindex := l; //?
  154. end;
  155. procedure ttgwasm.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference);
  156. var
  157. wbt : TWasmBasicType;
  158. idx : integer;
  159. begin
  160. if defToWasmBasic(def, wbt) then
  161. alloclocalVarToRef(wbt, ref)
  162. else begin
  163. //Internalerror(2019091801); // no support of structural type
  164. inherited;
  165. end;
  166. end;
  167. procedure ttgwasm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  168. var
  169. wbt: TWasmBasicType;
  170. begin
  171. if Assigned(def) and defToWasmBasic(def, wbt) then begin
  172. allocLocalVarToRef(wbt, ref);
  173. end else
  174. inherited;
  175. end;
  176. procedure ttgwasm.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  177. begin
  178. inherited;
  179. end;
  180. procedure ttgwasm.ungettemp(list: TAsmList; const ref: treference);
  181. begin
  182. if ref.base=NR_LOCAL_STACK_POINTER_REG then
  183. localvars.dealloc(ref.offset)
  184. else
  185. inherited;
  186. end;
  187. procedure ttgwasm.allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  188. var
  189. idx : integer;
  190. begin
  191. idx := localvars.alloc(wbt);
  192. localVarToRef(idx, 1, ref);
  193. end;
  194. procedure ttgwasm.localVarToRef(idx: integer; size: integer; out ref: treference);
  195. begin
  196. reference_reset_base(ref,NR_LOCAL_STACK_POINTER_REG,idx,ctempposinvalid,size,[]);
  197. updateFirstTemp;
  198. end;
  199. function TWasmLocalVars.alloc(bt: TWasmBasicType): integer;
  200. var
  201. i : integer;
  202. lc : TWasmLocal;
  203. t : TWasmLocal;
  204. begin
  205. lc := locv[bt];
  206. t := nil;
  207. while Assigned(lc) and (lc.inuse) do begin
  208. t := lc;
  209. lc := lc.next;
  210. end;
  211. if Assigned(lc) then begin
  212. lc.inuse := true;
  213. end else begin
  214. lc := TWasmLocal.Create(bt, varindex);
  215. if Assigned(t)
  216. then t.next := lc
  217. else locv[bt]:=lc;
  218. lc.inuse:=true;
  219. inc(varindex);
  220. if Assigned(last) then last.nextseq := lc;
  221. if not Assigned(first) then first := lc;
  222. last := lc;
  223. end;
  224. alloc := lc.index;
  225. end;
  226. procedure TWasmLocalVars.dealloc(bt: TWasmBasicType; index: integer);
  227. var
  228. lc : TWasmLocal;
  229. begin
  230. lc := locv[bt];
  231. while Assigned(lc) and (lc.index <> index) do
  232. lc := lc.next;
  233. if Assigned(lc) then lc.inuse := false;
  234. end;
  235. procedure TWasmLocalVars.dealloc(index: integer);
  236. var
  237. lc : TWasmLocal;
  238. begin
  239. lc := first;
  240. while Assigned(lc) and (lc.index <> index) do
  241. lc := lc.nextseq;
  242. if Assigned(lc) then lc.inuse := false;
  243. end;
  244. initialization
  245. tgobjclass:=ttgwasm;
  246. end.