symcpu.pas 13 KB

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