symcpu.pas 13 KB

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