nwasmutil.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. {
  2. Copyright (c) 2021 by Nikolay Nikolov
  3. WebAssembly version of some node tree helper routines
  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 nwasmutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. ngenutil;
  22. type
  23. { twasmnodeutils }
  24. twasmnodeutils = class(tnodeutils)
  25. public
  26. class procedure InsertObjectInfo; override;
  27. end;
  28. implementation
  29. uses
  30. globtype,
  31. cpubase,
  32. aasmdata,aasmcpu,
  33. hlcgobj,hlcgcpu,
  34. symdef,symtype,symconst,
  35. fmodule;
  36. { twasmnodeutils }
  37. class procedure twasmnodeutils.InsertObjectInfo;
  38. procedure WriteImportDll(list: TAsmList; proc: tprocdef);
  39. begin
  40. thlcgwasm(hlcg).g_procdef(list,proc);
  41. list.Concat(tai_import_module.create(proc.mangledname,proc.import_dll^));
  42. list.Concat(tai_import_name.create(proc.mangledname,proc.import_name^));
  43. end;
  44. var
  45. i : integer;
  46. def : tdef;
  47. proc : tprocdef;
  48. list : TAsmList;
  49. cur_unit: tused_unit;
  50. begin
  51. inherited;
  52. list:=current_asmdata.asmlists[al_start];
  53. for i:=0 to current_module.deflist.Count-1 do
  54. begin
  55. def:=tdef(current_module.deflist[i]);
  56. { since commit 48986 deflist might have NIL entries }
  57. if assigned(def) and (def.typ=procdef) then
  58. begin
  59. proc := tprocdef(def);
  60. if (po_external in proc.procoptions) and (po_has_importdll in proc.procoptions) then
  61. WriteImportDll(list,proc);
  62. end;
  63. end;
  64. cur_unit:=tused_unit(usedunits.First);
  65. while assigned(cur_unit) do
  66. begin
  67. if (cur_unit.u.moduleflags * [mf_init,mf_finalize])<>[] then
  68. begin
  69. if mf_init in cur_unit.u.moduleflags then
  70. list.Concat(tai_functype.create(make_mangledname('INIT$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  71. if mf_finalize in cur_unit.u.moduleflags then
  72. list.Concat(tai_functype.create(make_mangledname('FINALIZE$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  73. end;
  74. for i:=0 to cur_unit.u.deflist.Count-1 do
  75. begin
  76. def:=tdef(cur_unit.u.deflist[i]);
  77. if assigned(def) and (tdef(def).typ = procdef) then
  78. begin
  79. proc := tprocdef(def);
  80. if (po_external in proc.procoptions) and (po_has_importdll in proc.procoptions) then
  81. WriteImportDll(list,proc)
  82. else if (not proc.owner.iscurrentunit or (po_external in proc.procoptions)) and
  83. ((proc.paras.Count=0) or (proc.has_paraloc_info in [callerside,callbothsides])) then
  84. thlcgwasm(hlcg).g_procdef(list,proc);
  85. end;
  86. end;
  87. cur_unit:=tused_unit(cur_unit.Next);
  88. end;
  89. end;
  90. begin
  91. cnodeutils:=twasmnodeutils;
  92. end.