symcpu.pas 11 KB

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