symcpu.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. end;
  129. tcpustaticvarsymclass = class of tcpustaticvarsym;
  130. tcpuabsolutevarsym = class(tabsolutevarsym)
  131. end;
  132. tcpuabsolutevarsymclass = class of tcpuabsolutevarsym;
  133. tcpupropertysym = class(tpropertysym)
  134. end;
  135. tcpupropertysymclass = class of tcpupropertysym;
  136. tcpuconstsym = class(tconstsym)
  137. end;
  138. tcpuconstsymclass = class of tcpuconstsym;
  139. tcpuenumsym = class(tenumsym)
  140. end;
  141. tcpuenumsymclass = class of tcpuenumsym;
  142. tcpusyssym = class(tsyssym)
  143. end;
  144. tcpusyssymclass = class of tcpusyssym;
  145. const
  146. pbestrealtype : ^tdef = @s64floattype;
  147. implementation
  148. uses
  149. verbose,cutils,cclasses,globals,cgbase,
  150. symconst,symbase,symtable,symcreat,wasmdef,
  151. pdecsub,pparautl,paramgr,
  152. // high-level code generator is needed to get access to type index for ncall
  153. hlcgobj,hlcgcpu,
  154. tgcpu
  155. ;
  156. {****************************************************************************
  157. tcpuproptertysym
  158. ****************************************************************************}
  159. {****************************************************************************
  160. tcpuenumdef
  161. ****************************************************************************}
  162. {****************************************************************************
  163. tcpuprocdef
  164. ****************************************************************************}
  165. function create_functype_common(pd: tabstractprocdef): TWasmFuncType;
  166. var
  167. i: Integer;
  168. prm: tcpuparavarsym;
  169. bt: TWasmBasicType;
  170. begin
  171. pd.init_paraloc_info(callerside);
  172. result:=TWasmFuncType.Create([],[]);
  173. if Assigned(pd.paras) and (pd.paras.Count>0) then
  174. begin
  175. for i:=0 to pd.paras.Count-1 do
  176. begin
  177. prm := tcpuparavarsym(pd.paras[i]);
  178. case prm.paraloc[callerside].Size of
  179. OS_8..OS_32, OS_S8..OS_S32:
  180. result.add_param(wbt_i32);
  181. OS_64, OS_S64:
  182. result.add_param(wbt_i64);
  183. OS_F32:
  184. result.add_param(wbt_f32);
  185. OS_F64:
  186. result.add_param(wbt_f64);
  187. else
  188. begin
  189. {$ifdef EXTDEBUG}
  190. Writeln('Unsupported caller side parameter type: ', prm.paraloc[callerside].Size);
  191. {$endif EXTDEBUG}
  192. // unsupported calleeside parameter type
  193. Internalerror(2019093001);
  194. end;
  195. end;
  196. end;
  197. end;
  198. if Assigned(pd.returndef) and (pd.returndef.size>0) and
  199. not paramanager.ret_in_param(pd.returndef,pd) then
  200. begin
  201. if not defToWasmBasic(pd.returndef,bt) then
  202. bt:=wbt_i32;
  203. case bt of
  204. wbt_i64:
  205. result.add_result(wbt_i64);
  206. wbt_f32:
  207. result.add_result(wbt_f32);
  208. wbt_f64:
  209. result.add_result(wbt_f64);
  210. else
  211. result.add_result(wbt_i32);
  212. end;
  213. end;
  214. end;
  215. destructor tcpuprocdef.destroy;
  216. begin
  217. exprasmlist.free;
  218. inherited destroy;
  219. end;
  220. function tcpuprocdef.create_functype: TWasmFuncType;
  221. begin
  222. Result:=create_functype_common(self);
  223. end;
  224. function tcpuprocdef.is_pushleftright: boolean;
  225. begin
  226. Result:=true;
  227. end;
  228. {****************************************************************************
  229. tcpuprocvardef
  230. ****************************************************************************}
  231. function tcpuprocvardef.create_functype: TWasmFuncType;
  232. begin
  233. Result:=create_functype_common(Self);
  234. end;
  235. function tcpuprocvardef.is_pushleftright: boolean;
  236. begin
  237. Result:=true;
  238. end;
  239. {****************************************************************************
  240. tcpuprocsym
  241. ****************************************************************************}
  242. {****************************************************************************
  243. tcpustaticvarsym
  244. ****************************************************************************}
  245. {****************************************************************************
  246. tcpufieldvarsym
  247. ****************************************************************************}
  248. initialization
  249. { used tdef classes }
  250. cfiledef:=tcpufiledef;
  251. cvariantdef:=tcpuvariantdef;
  252. cformaldef:=tcpuformaldef;
  253. cforwarddef:=tcpuforwarddef;
  254. cundefineddef:=tcpuundefineddef;
  255. cerrordef:=tcpuerrordef;
  256. cpointerdef:=tcpupointerdef;
  257. crecorddef:=tcpurecorddef;
  258. cimplementedinterface:=tcpuimplementedinterface;
  259. cobjectdef:=tcpuobjectdef;
  260. cclassrefdef:=tcpuclassrefdef;
  261. carraydef:=tcpuarraydef;
  262. corddef:=tcpuorddef;
  263. cfloatdef:=tcpufloatdef;
  264. cprocvardef:=tcpuprocvardef;
  265. cprocdef:=tcpuprocdef;
  266. cstringdef:=tcpustringdef;
  267. cenumdef:=tcpuenumdef;
  268. csetdef:=tcpusetdef;
  269. { used tsym classes }
  270. clabelsym:=tcpulabelsym;
  271. cunitsym:=tcpuunitsym;
  272. cprogramparasym:=tcpuprogramparasym;
  273. cnamespacesym:=tcpunamespacesym;
  274. cprocsym:=tcpuprocsym;
  275. ctypesym:=tcputypesym;
  276. cfieldvarsym:=tcpufieldvarsym;
  277. clocalvarsym:=tcpulocalvarsym;
  278. cparavarsym:=tcpuparavarsym;
  279. cstaticvarsym:=tcpustaticvarsym;
  280. cabsolutevarsym:=tcpuabsolutevarsym;
  281. cpropertysym:=tcpupropertysym;
  282. cconstsym:=tcpuconstsym;
  283. cenumsym:=tcpuenumsym;
  284. csyssym:=tcpusyssym;
  285. end.