symcpu.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. {
  2. Copyright (c) 2014 by Florian Klaempfl
  3. Symbol table overrides for 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 symcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cpubase,
  23. aasmdata,
  24. symtype,
  25. symdef,symsym,
  26. cgutils;
  27. type
  28. { defs }
  29. tcpufiledef = class(tfiledef)
  30. end;
  31. tcpufiledefclass = class of tcpufiledef;
  32. tcpuvariantdef = class(tvariantdef)
  33. end;
  34. tcpuvariantdefclass = class of tcpuvariantdef;
  35. tcpuformaldef = class(tformaldef)
  36. end;
  37. tcpuformaldefclass = class of tcpuformaldef;
  38. tcpuforwarddef = class(tforwarddef)
  39. end;
  40. tcpuforwarddefclass = class of tcpuforwarddef;
  41. tcpuundefineddef = class(tundefineddef)
  42. end;
  43. tcpuundefineddefclass = class of tcpuundefineddef;
  44. tcpuerrordef = class(terrordef)
  45. end;
  46. tcpuerrordefclass = class of tcpuerrordef;
  47. tcpupointerdef = class(tpointerdef)
  48. end;
  49. tcpupointerdefclass = class of tcpupointerdef;
  50. tcpurecorddef = class(trecorddef)
  51. end;
  52. tcpurecorddefclass = class of tcpurecorddef;
  53. tcpuimplementedinterface = class(timplementedinterface)
  54. end;
  55. tcpuimplementedinterfaceclass = class of tcpuimplementedinterface;
  56. tcpuobjectdef = class(tobjectdef)
  57. end;
  58. tcpuobjectdefclass = class of tcpuobjectdef;
  59. tcpuclassrefdef = class(tclassrefdef)
  60. end;
  61. tcpuclassrefdefclass = class of tcpuclassrefdef;
  62. tcpuarraydef = class(tarraydef)
  63. end;
  64. tcpuarraydefclass = class of tcpuarraydef;
  65. tcpuorddef = class(torddef)
  66. end;
  67. tcpuorddefclass = class of tcpuorddef;
  68. tcpufloatdef = class(tfloatdef)
  69. end;
  70. tcpufloatdefclass = class of tcpufloatdef;
  71. { tcpuprocvardef }
  72. tcpuprocvardef = class(tprocvardef)
  73. function create_functype: TWasmFuncType;
  74. function is_pushleftright: boolean; override;
  75. end;
  76. tcpuprocvardefclass = class of tcpuprocvardef;
  77. { tcpuprocdef }
  78. tcpuprocdef = class(tprocdef)
  79. frame_pointer_ref,
  80. base_pointer_ref: treference;
  81. { generated assembler code; used by WebAssembly backend so it can afterwards
  82. easily write out all methods grouped per class }
  83. exprasmlist : TAsmList;
  84. destructor destroy; override;
  85. function create_functype: TWasmFuncType;
  86. function is_pushleftright: boolean; override;
  87. end;
  88. tcpuprocdefclass = class of tcpuprocdef;
  89. tcpustringdef = class(tstringdef)
  90. end;
  91. tcpustringdefclass = class of tcpustringdef;
  92. tcpuenumdef = class(tenumdef)
  93. end;
  94. tcpuenumdefclass = class of tcpuenumdef;
  95. tcpusetdef = class(tsetdef)
  96. end;
  97. tcpusetdefclass = class of tcpusetdef;
  98. { syms }
  99. tcpulabelsym = class(tlabelsym)
  100. end;
  101. tcpulabelsymclass = class of tcpulabelsym;
  102. tcpuunitsym = class(tunitsym)
  103. end;
  104. tcpuunitsymclass = class of tcpuunitsym;
  105. tcpuprogramparasym = class(tprogramparasym)
  106. end;
  107. tcpuprogramparasymclass = class(tprogramparasym);
  108. tcpunamespacesym = class(tnamespacesym)
  109. end;
  110. tcpunamespacesymclass = class of tcpunamespacesym;
  111. { tcpuprocsym }
  112. tcpuprocsym = class(tprocsym)
  113. end;
  114. tcpuprocsymclass = class of tcpuprocsym;
  115. tcputypesym = class(ttypesym)
  116. end;
  117. tcpuypesymclass = class of tcputypesym;
  118. tcpufieldvarsym = class(tfieldvarsym)
  119. end;
  120. tcpufieldvarsymclass = class of tcpufieldvarsym;
  121. tcpulocalvarsym = class(tlocalvarsym)
  122. end;
  123. tcpulocalvarsymclass = class of tcpulocalvarsym;
  124. tcpuparavarsym = class(tparavarsym)
  125. end;
  126. tcpuparavarsymclass = class of tcpuparavarsym;
  127. tcpustaticvarsym = class(tstaticvarsym)
  128. function is_wasm_global: Boolean;
  129. function try_get_wasm_global_vardef_type(out res: TWasmBasicType): Boolean;
  130. function get_wasm_global_vardef_type: TWasmBasicType;
  131. function has_valid_wasm_global_vardef_type: Boolean;
  132. end;
  133. tcpustaticvarsymclass = class of tcpustaticvarsym;
  134. tcpuabsolutevarsym = class(tabsolutevarsym)
  135. end;
  136. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  137. tcpupropertysym = class(tpropertysym)
  138. end;
  139. tcpupropertysymclass = class of tcpupropertysym;
  140. tcpuconstsym = class(tconstsym)
  141. end;
  142. tcpuconstsymclass = class of tcpuconstsym;
  143. tcpuenumsym = class(tenumsym)
  144. end;
  145. tcpuenumsymclass = class of tcpuenumsym;
  146. tcpusyssym = class(tsyssym)
  147. end;
  148. tcpusyssymclass = class of tcpusyssym;
  149. const
  150. pbestrealtype : ^tdef = @s64floattype;
  151. implementation
  152. uses
  153. verbose,cutils,cclasses,globals,cgbase,
  154. symconst,symbase,symtable,symcreat,wasmdef,
  155. defutil,
  156. pdecsub,pparautl,paramgr,
  157. // high-level code generator is needed to get access to type index for ncall
  158. hlcgobj,hlcgcpu,
  159. tgcpu
  160. ;
  161. {****************************************************************************
  162. tcpuproptertysym
  163. ****************************************************************************}
  164. {****************************************************************************
  165. tcpuenumdef
  166. ****************************************************************************}
  167. {****************************************************************************
  168. tcpuprocdef
  169. ****************************************************************************}
  170. function create_functype_common(pd: tabstractprocdef): TWasmFuncType;
  171. var
  172. i: Integer;
  173. prm: tcpuparavarsym;
  174. bt: TWasmBasicType;
  175. begin
  176. pd.init_paraloc_info(callerside);
  177. result:=TWasmFuncType.Create([],[]);
  178. if Assigned(pd.paras) and (pd.paras.Count>0) then
  179. begin
  180. for i:=0 to pd.paras.Count-1 do
  181. begin
  182. prm := tcpuparavarsym(pd.paras[i]);
  183. case prm.paraloc[callerside].Size of
  184. OS_8..OS_32, OS_S8..OS_S32:
  185. result.add_param(wbt_i32);
  186. OS_64, OS_S64:
  187. result.add_param(wbt_i64);
  188. OS_F32:
  189. result.add_param(wbt_f32);
  190. OS_F64:
  191. result.add_param(wbt_f64);
  192. else
  193. begin
  194. {$ifdef EXTDEBUG}
  195. Writeln('Unsupported caller side parameter type: ', prm.paraloc[callerside].Size);
  196. {$endif EXTDEBUG}
  197. // unsupported calleeside parameter type
  198. Internalerror(2019093001);
  199. end;
  200. end;
  201. end;
  202. end;
  203. if Assigned(pd.returndef) and (pd.returndef.size>0) and
  204. not paramanager.ret_in_param(pd.returndef,pd) then
  205. begin
  206. if not defToWasmBasic(pd.returndef,bt) then
  207. bt:=wbt_i32;
  208. result.add_result(bt);
  209. end;
  210. end;
  211. destructor tcpuprocdef.destroy;
  212. begin
  213. exprasmlist.free;
  214. inherited destroy;
  215. end;
  216. function tcpuprocdef.create_functype: TWasmFuncType;
  217. begin
  218. Result:=create_functype_common(self);
  219. end;
  220. function tcpuprocdef.is_pushleftright: boolean;
  221. begin
  222. Result:=true;
  223. end;
  224. {****************************************************************************
  225. tcpuprocvardef
  226. ****************************************************************************}
  227. function tcpuprocvardef.create_functype: TWasmFuncType;
  228. begin
  229. Result:=create_functype_common(Self);
  230. end;
  231. function tcpuprocvardef.is_pushleftright: boolean;
  232. begin
  233. Result:=true;
  234. end;
  235. {****************************************************************************
  236. tcpuprocsym
  237. ****************************************************************************}
  238. {****************************************************************************
  239. tcpustaticvarsym
  240. ****************************************************************************}
  241. function tcpustaticvarsym.is_wasm_global: Boolean;
  242. begin
  243. Result:=UpCase(section)='WEBASSEMBLY.GLOBAL';
  244. end;
  245. function tcpustaticvarsym.try_get_wasm_global_vardef_type(out res: TWasmBasicType): Boolean;
  246. begin
  247. Result:=True;
  248. if is_64bitint(vardef) then
  249. res:=wbt_i64
  250. else if is_pointer(vardef) then
  251. res:=wbt_i32
  252. else if is_32bitint(vardef) then
  253. res:=wbt_i32
  254. else if is_single(vardef) then
  255. res:=wbt_f32
  256. else if is_double(vardef) then
  257. res:=wbt_f64
  258. else
  259. Result:=False;
  260. end;
  261. function tcpustaticvarsym.get_wasm_global_vardef_type: TWasmBasicType;
  262. begin
  263. if not try_get_wasm_global_vardef_type(Result) then
  264. internalerror(2022072501);
  265. end;
  266. function tcpustaticvarsym.has_valid_wasm_global_vardef_type: Boolean;
  267. var
  268. TempWBT: TWasmBasicType;
  269. begin
  270. result:=try_get_wasm_global_vardef_type(TempWBT);
  271. end;
  272. {****************************************************************************
  273. tcpufieldvarsym
  274. ****************************************************************************}
  275. initialization
  276. { used tdef classes }
  277. cfiledef:=tcpufiledef;
  278. cvariantdef:=tcpuvariantdef;
  279. cformaldef:=tcpuformaldef;
  280. cforwarddef:=tcpuforwarddef;
  281. cundefineddef:=tcpuundefineddef;
  282. cerrordef:=tcpuerrordef;
  283. cpointerdef:=tcpupointerdef;
  284. crecorddef:=tcpurecorddef;
  285. cimplementedinterface:=tcpuimplementedinterface;
  286. cobjectdef:=tcpuobjectdef;
  287. cclassrefdef:=tcpuclassrefdef;
  288. carraydef:=tcpuarraydef;
  289. corddef:=tcpuorddef;
  290. cfloatdef:=tcpufloatdef;
  291. cprocvardef:=tcpuprocvardef;
  292. cprocdef:=tcpuprocdef;
  293. cstringdef:=tcpustringdef;
  294. cenumdef:=tcpuenumdef;
  295. csetdef:=tcpusetdef;
  296. { used tsym classes }
  297. clabelsym:=tcpulabelsym;
  298. cunitsym:=tcpuunitsym;
  299. cprogramparasym:=tcpuprogramparasym;
  300. cnamespacesym:=tcpunamespacesym;
  301. cprocsym:=tcpuprocsym;
  302. ctypesym:=tcputypesym;
  303. cfieldvarsym:=tcpufieldvarsym;
  304. clocalvarsym:=tcpulocalvarsym;
  305. cparavarsym:=tcpuparavarsym;
  306. cstaticvarsym:=tcpustaticvarsym;
  307. cabsolutevarsym:=tcpuabsolutevarsym;
  308. cpropertysym:=tcpupropertysym;
  309. cconstsym:=tcpuconstsym;
  310. cenumsym:=tcpuenumsym;
  311. csyssym:=tcpusyssym;
  312. end.