symcpu.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. implementation
  163. uses
  164. verbose,cutils,cclasses,globals,cgbase,
  165. symconst,symbase,symtable,symcreat,wasmdef,
  166. defutil,
  167. pdecsub,pparautl,paramgr,
  168. // high-level code generator is needed to get access to type index for ncall
  169. hlcgobj,hlcgcpu,
  170. tgcpu
  171. ;
  172. function is_wasm_funcref(p: tdef): boolean;
  173. begin
  174. result:=(p.typ=procvardef) and (po_wasm_funcref in tprocvardef(p).procoptions);
  175. end;
  176. {****************************************************************************
  177. tcpuproptertysym
  178. ****************************************************************************}
  179. {****************************************************************************
  180. tcpuenumdef
  181. ****************************************************************************}
  182. {****************************************************************************
  183. tcpupointerdef
  184. ****************************************************************************}
  185. procedure tcpupointerdef.ppuload_platform(ppufile: tcompilerppufile);
  186. begin
  187. inherited;
  188. is_wasm_externref:=ppufile.getboolean;
  189. end;
  190. procedure tcpupointerdef.ppuwrite_platform(ppufile: tcompilerppufile);
  191. begin
  192. inherited;
  193. ppufile.putboolean(is_wasm_externref);
  194. end;
  195. constructor tcpupointerdef.create_externref(def: tdef);
  196. begin
  197. inherited create(def);
  198. is_wasm_externref:=true;
  199. end;
  200. function tcpupointerdef.getcopy: tstoreddef;
  201. begin
  202. result:=inherited;
  203. tcpupointerdef(result).is_wasm_externref:=is_wasm_externref;
  204. end;
  205. function tcpupointerdef.GetTypeName: string;
  206. begin
  207. result:=inherited;
  208. if is_wasm_externref then
  209. result:=result+';wasmexternref';;
  210. end;
  211. {****************************************************************************
  212. tcpuprocdef
  213. ****************************************************************************}
  214. function create_functype_common(pd: tabstractprocdef): TWasmFuncType;
  215. var
  216. i: Integer;
  217. prm: tcpuparavarsym;
  218. bt: TWasmBasicType;
  219. begin
  220. pd.init_paraloc_info(callerside);
  221. result:=TWasmFuncType.Create([],[]);
  222. if Assigned(pd.paras) and (pd.paras.Count>0) then
  223. begin
  224. for i:=0 to pd.paras.Count-1 do
  225. begin
  226. prm := tcpuparavarsym(pd.paras[i]);
  227. if is_wasm_funcref(prm.vardef) then
  228. result.add_param(wbt_funcref)
  229. else if (prm.vardef.typ=pointerdef) and (tcpupointerdef(prm.vardef).is_wasm_externref) then
  230. result.add_param(wbt_externref)
  231. else case prm.paraloc[callerside].Size of
  232. OS_8..OS_32, OS_S8..OS_S32:
  233. result.add_param(wbt_i32);
  234. OS_64, OS_S64:
  235. result.add_param(wbt_i64);
  236. OS_F32:
  237. result.add_param(wbt_f32);
  238. OS_F64:
  239. result.add_param(wbt_f64);
  240. else
  241. begin
  242. {$ifdef EXTDEBUG}
  243. Writeln('Unsupported caller side parameter type: ', prm.paraloc[callerside].Size);
  244. {$endif EXTDEBUG}
  245. // unsupported calleeside parameter type
  246. Internalerror(2019093001);
  247. end;
  248. end;
  249. end;
  250. end;
  251. if Assigned(pd.returndef) and (pd.returndef.size>0) and
  252. not paramanager.ret_in_param(pd.returndef,pd) then
  253. begin
  254. if not defToWasmBasic(pd.returndef,bt) then
  255. bt:=wbt_i32;
  256. result.add_result(bt);
  257. end;
  258. end;
  259. destructor tcpuprocdef.destroy;
  260. begin
  261. exprasmlist.free;
  262. inherited destroy;
  263. end;
  264. function tcpuprocdef.create_functype: TWasmFuncType;
  265. begin
  266. Result:=create_functype_common(self);
  267. end;
  268. function tcpuprocdef.is_pushleftright: boolean;
  269. begin
  270. Result:=true;
  271. end;
  272. {****************************************************************************
  273. tcpuprocvardef
  274. ****************************************************************************}
  275. function tcpuprocvardef.create_functype: TWasmFuncType;
  276. begin
  277. Result:=create_functype_common(Self);
  278. end;
  279. function tcpuprocvardef.is_pushleftright: boolean;
  280. begin
  281. Result:=true;
  282. end;
  283. {****************************************************************************
  284. tcpuprocsym
  285. ****************************************************************************}
  286. {****************************************************************************
  287. tcpustaticvarsym
  288. ****************************************************************************}
  289. function tcpustaticvarsym.is_wasm_global: Boolean;
  290. begin
  291. Result:=UpCase(section)='WEBASSEMBLY.GLOBAL';
  292. end;
  293. function tcpustaticvarsym.try_get_wasm_global_vardef_type(out res: TWasmBasicType): Boolean;
  294. begin
  295. Result:=True;
  296. if is_64bitint(vardef) then
  297. res:=wbt_i64
  298. else if is_pointer(vardef) then
  299. res:=wbt_i32
  300. else if is_32bitint(vardef) then
  301. res:=wbt_i32
  302. else if is_single(vardef) then
  303. res:=wbt_f32
  304. else if is_double(vardef) then
  305. res:=wbt_f64
  306. else
  307. Result:=False;
  308. end;
  309. function tcpustaticvarsym.get_wasm_global_vardef_type: TWasmBasicType;
  310. begin
  311. if not try_get_wasm_global_vardef_type(Result) then
  312. internalerror(2022072501);
  313. end;
  314. function tcpustaticvarsym.has_valid_wasm_global_vardef_type: Boolean;
  315. var
  316. TempWBT: TWasmBasicType;
  317. begin
  318. result:=try_get_wasm_global_vardef_type(TempWBT);
  319. end;
  320. {****************************************************************************
  321. tcpufieldvarsym
  322. ****************************************************************************}
  323. initialization
  324. { used tdef classes }
  325. cfiledef:=tcpufiledef;
  326. cvariantdef:=tcpuvariantdef;
  327. cformaldef:=tcpuformaldef;
  328. cforwarddef:=tcpuforwarddef;
  329. cundefineddef:=tcpuundefineddef;
  330. cerrordef:=tcpuerrordef;
  331. cpointerdef:=tcpupointerdef;
  332. crecorddef:=tcpurecorddef;
  333. cimplementedinterface:=tcpuimplementedinterface;
  334. cobjectdef:=tcpuobjectdef;
  335. cclassrefdef:=tcpuclassrefdef;
  336. carraydef:=tcpuarraydef;
  337. corddef:=tcpuorddef;
  338. cfloatdef:=tcpufloatdef;
  339. cprocvardef:=tcpuprocvardef;
  340. cprocdef:=tcpuprocdef;
  341. cstringdef:=tcpustringdef;
  342. cenumdef:=tcpuenumdef;
  343. csetdef:=tcpusetdef;
  344. { used tsym classes }
  345. clabelsym:=tcpulabelsym;
  346. cunitsym:=tcpuunitsym;
  347. cprogramparasym:=tcpuprogramparasym;
  348. cnamespacesym:=tcpunamespacesym;
  349. cprocsym:=tcpuprocsym;
  350. ctypesym:=tcputypesym;
  351. cfieldvarsym:=tcpufieldvarsym;
  352. clocalvarsym:=tcpulocalvarsym;
  353. cparavarsym:=tcpuparavarsym;
  354. cstaticvarsym:=tcpustaticvarsym;
  355. cabsolutevarsym:=tcpuabsolutevarsym;
  356. cpropertysym:=tcpupropertysym;
  357. cconstsym:=tcpuconstsym;
  358. cenumsym:=tcpuenumsym;
  359. csyssym:=tcpusyssym;
  360. end.