2
0

symcpu.pas 14 KB

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