symcpu.pas 13 KB

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