symcpu.pas 14 KB

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