symcpu.pas 12 KB

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