symcpu.pas 12 KB

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