symcpu.pas 13 KB

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