nwasmutil.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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,globals,
  31. cpubase,
  32. aasmbase,aasmdata,aasmtai,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. list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32,false));
  54. if ts_wasm_native_exceptions in current_settings.targetswitches then
  55. begin
  56. list.Concat(tai_tagtype.create(FPC_EXCEPTION_TAG_SYM, []));
  57. list.Concat(tai_symbol.Create(current_asmdata.DefineAsmSymbol(FPC_EXCEPTION_TAG_SYM,AB_LOCAL,AT_WASM_EXCEPTION_TAG,nil),0));
  58. end;
  59. for i:=0 to current_module.deflist.Count-1 do
  60. begin
  61. def:=tdef(current_module.deflist[i]);
  62. { since commit 48986 deflist might have NIL entries }
  63. if assigned(def) and (def.typ=procdef) then
  64. begin
  65. proc := tprocdef(def);
  66. if po_external in proc.procoptions then
  67. if po_has_importdll in proc.procoptions then
  68. WriteImportDll(list,proc)
  69. else
  70. thlcgwasm(hlcg).g_procdef(list,proc);
  71. end;
  72. end;
  73. cur_unit:=tused_unit(usedunits.First);
  74. while assigned(cur_unit) do
  75. begin
  76. if (cur_unit.u.moduleflags * [mf_init,mf_finalize])<>[] then
  77. begin
  78. if mf_init in cur_unit.u.moduleflags then
  79. list.Concat(tai_functype.create(make_mangledname('INIT$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  80. if mf_finalize in cur_unit.u.moduleflags then
  81. list.Concat(tai_functype.create(make_mangledname('FINALIZE$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  82. end;
  83. for i:=0 to cur_unit.u.deflist.Count-1 do
  84. begin
  85. def:=tdef(cur_unit.u.deflist[i]);
  86. if assigned(def) and (tdef(def).typ = procdef) then
  87. begin
  88. proc := tprocdef(def);
  89. if (po_external in proc.procoptions) and (po_has_importdll in proc.procoptions) then
  90. WriteImportDll(list,proc)
  91. else if (not proc.owner.iscurrentunit or (po_external in proc.procoptions)) and
  92. ((proc.paras.Count=0) or (proc.has_paraloc_info in [callerside,callbothsides])) then
  93. thlcgwasm(hlcg).g_procdef(list,proc);
  94. end;
  95. end;
  96. cur_unit:=tused_unit(cur_unit.Next);
  97. end;
  98. end;
  99. begin
  100. cnodeutils:=twasmnodeutils;
  101. end.