tgcpu.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. 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 ungettemp(list: TAsmList; const ref : treference); override;
  65. end;
  66. function defToWasmBasic(def: tdef; var wbt: TWasmBasicType): Boolean;
  67. implementation
  68. uses
  69. verbose,
  70. cgbase,
  71. symconst,symtable,symdef,symsym,symcpu,defutil,
  72. aasmbase,aasmcpu,
  73. hlcgobj,hlcgcpu, procinfo;
  74. function defToWasmBasic(def: tdef; var wbt: TWasmBasicType): Boolean;
  75. begin
  76. Result := assigned(def);
  77. if not Result then Exit;
  78. if is_pointer(def) then
  79. wbt := wbt_i32 // wasm32
  80. else if is_ordinal(def) then begin
  81. if is_64bit(def) then wbt := wbt_i64
  82. else wbt := wbt_i32;
  83. end else if is_real(def) then begin
  84. if is_single(def) then wbt := wbt_f32
  85. else wbt := wbt_f64; // real/double/extended
  86. end else
  87. Result := false;
  88. end;
  89. { TWasmLocal }
  90. constructor TWasmLocal.create(atype: TWasmBasicType;
  91. aindex: integer);
  92. begin
  93. typ:=atype;
  94. index:=aindex;
  95. end;
  96. { TWasmLocalVars }
  97. constructor TWasmLocalVars.Create(astartindex: Integer = 0);
  98. begin
  99. inherited Create;
  100. varindex := astartindex;
  101. end;
  102. destructor TWasmLocalVars.Destroy;
  103. var
  104. t : TWasmLocal;
  105. n : TWasmLocal;
  106. begin
  107. t := first;
  108. while Assigned(t) do
  109. begin
  110. n:=t;
  111. t:=t.nextseq;
  112. n.Free;
  113. end;
  114. inherited Destroy;
  115. end;
  116. { ttgwasm }
  117. procedure ttgwasm.updateFirstTemp;
  118. begin
  119. firsttemp := localvars.varindex;
  120. if lasttemp<firsttemp then lasttemp := firsttemp;
  121. end;
  122. constructor ttgwasm.create;
  123. begin
  124. inherited create;
  125. direction := 1; // temp variables are allocated as "locals", and it starts with 0 and goes beyond!
  126. localvars:=TWasmLocalVars.Create;
  127. end;
  128. destructor ttgwasm.destroy;
  129. begin
  130. localvars.Free;
  131. inherited destroy;
  132. end;
  133. procedure ttgwasm.setfirsttemp(l: asizeint);
  134. begin
  135. firsttemp:=l;
  136. lasttemp:=l;
  137. localvars.varindex := l; //?
  138. end;
  139. procedure ttgwasm.getlocal(list: TAsmList; size: asizeint; alignment: shortint; def: tdef; var ref: treference);
  140. var
  141. wbt : TWasmBasicType;
  142. idx : integer;
  143. begin
  144. if defToWasmBasic(def, wbt) then
  145. alloclocalVarToRef(wbt, ref)
  146. else begin
  147. //Internalerror(2019091801); // no support of structural type
  148. inherited;
  149. end;
  150. end;
  151. procedure ttgwasm.gethltemp(list: TAsmList; def: tdef; forcesize: asizeint; temptype: ttemptype; out ref: treference);
  152. var
  153. wbt: TWasmBasicType;
  154. begin
  155. if Assigned(def) and defToWasmBasic(def, wbt) then begin
  156. allocLocalVarToRef(wbt, ref);
  157. end else
  158. inherited;
  159. end;
  160. procedure ttgwasm.gethltempmanaged(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  161. begin
  162. inherited;
  163. end;
  164. procedure ttgwasm.ungettemp(list: TAsmList; const ref: treference);
  165. begin
  166. if ref.base=NR_LOCAL_STACK_POINTER_REG then
  167. localvars.dealloc(ref.offset)
  168. else
  169. inherited;
  170. end;
  171. procedure ttgwasm.allocLocalVarToRef(wbt: TWasmBasicType; out ref: treference);
  172. var
  173. idx : integer;
  174. begin
  175. idx := localvars.alloc(wbt);
  176. localVarToRef(idx, 1, ref);
  177. end;
  178. procedure ttgwasm.localVarToRef(idx: integer; size: integer; out ref: treference);
  179. begin
  180. reference_reset_base(ref,NR_LOCAL_STACK_POINTER_REG,idx,ctempposinvalid,size,[]);
  181. updateFirstTemp;
  182. end;
  183. function TWasmLocalVars.alloc(bt: TWasmBasicType): integer;
  184. var
  185. i : integer;
  186. lc : TWasmLocal;
  187. t : TWasmLocal;
  188. begin
  189. lc := locv[bt];
  190. t := nil;
  191. while Assigned(lc) and (lc.inuse) do begin
  192. t := lc;
  193. lc := lc.next;
  194. end;
  195. if Assigned(lc) then begin
  196. lc.inuse := true;
  197. end else begin
  198. lc := TWasmLocal.Create(bt, varindex);
  199. if Assigned(t)
  200. then t.next := lc
  201. else locv[bt]:=lc;
  202. lc.inuse:=true;
  203. inc(varindex);
  204. if Assigned(last) then last.nextseq := lc;
  205. if not Assigned(first) then first := lc;
  206. last := lc;
  207. end;
  208. alloc := lc.index;
  209. end;
  210. procedure TWasmLocalVars.dealloc(bt: TWasmBasicType; index: integer);
  211. var
  212. lc : TWasmLocal;
  213. begin
  214. lc := locv[bt];
  215. while Assigned(lc) and (lc.index <> index) do
  216. lc := lc.next;
  217. if Assigned(lc) then lc.inuse := false;
  218. end;
  219. procedure TWasmLocalVars.dealloc(index: integer);
  220. var
  221. lc : TWasmLocal;
  222. begin
  223. lc := first;
  224. while Assigned(lc) and (lc.index <> index) do
  225. lc := lc.nextseq;
  226. if Assigned(lc) then lc.inuse := false;
  227. end;
  228. initialization
  229. tgobjclass:=ttgwasm;
  230. end.