symcpu.pas 9.1 KB

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