symcpu.pas 14 KB

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