symcpu.pas 14 KB

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