nwasmutil.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. procedure InsertUnitInfo(list : TAsmList;cur_unit: tused_unit);
  45. var
  46. i: Integer;
  47. def : tdef;
  48. proc : tprocdef;
  49. begin
  50. if (cur_unit.u.moduleflags * [mf_init,mf_finalize])<>[] then
  51. begin
  52. if mf_init in cur_unit.u.moduleflags then
  53. list.Concat(tai_functype.create(make_mangledname('INIT$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  54. if mf_finalize in cur_unit.u.moduleflags then
  55. list.Concat(tai_functype.create(make_mangledname('FINALIZE$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[])));
  56. end;
  57. for i:=0 to cur_unit.u.deflist.Count-1 do
  58. begin
  59. def:=tdef(cur_unit.u.deflist[i]);
  60. if assigned(def) and (tdef(def).typ = procdef) then
  61. begin
  62. proc := tprocdef(def);
  63. if (po_external in proc.procoptions) and (po_has_importdll in proc.procoptions) then
  64. WriteImportDll(list,proc)
  65. else if not proc.owner.iscurrentunit or (po_external in proc.procoptions) then
  66. thlcgwasm(hlcg).g_procdef(list,proc);
  67. end;
  68. end;
  69. end;
  70. var
  71. i : integer;
  72. def : tdef;
  73. proc : tprocdef;
  74. list : TAsmList;
  75. cur_unit: tused_unit;
  76. begin
  77. inherited;
  78. list:=current_asmdata.asmlists[al_start];
  79. list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32,false));
  80. if ts_wasm_native_exceptions in current_settings.targetswitches then
  81. begin
  82. list.Concat(tai_tagtype.create(FPC_EXCEPTION_TAG_SYM, []));
  83. list.Concat(tai_symbol.Create_Weak(current_asmdata.WeakRefAsmSymbol(FPC_EXCEPTION_TAG_SYM,AT_WASM_EXCEPTION_TAG),0));
  84. end;
  85. for i:=0 to current_module.deflist.Count-1 do
  86. begin
  87. def:=tdef(current_module.deflist[i]);
  88. { since commit 48986 deflist might have NIL entries }
  89. if assigned(def) and (def.typ=procdef) then
  90. begin
  91. proc := tprocdef(def);
  92. if po_external in proc.procoptions then
  93. if po_has_importdll in proc.procoptions then
  94. WriteImportDll(list,proc)
  95. else
  96. thlcgwasm(hlcg).g_procdef(list,proc);
  97. end;
  98. end;
  99. cur_unit:=tused_unit(usedunits.First);
  100. while assigned(cur_unit) do
  101. begin
  102. InsertUnitInfo(list,cur_unit);
  103. cur_unit:=tused_unit(cur_unit.Next);
  104. end;
  105. cur_unit:=tused_unit(current_module.used_units.First);
  106. while assigned(cur_unit) do
  107. begin
  108. InsertUnitInfo(list,cur_unit);
  109. cur_unit:=tused_unit(cur_unit.Next);
  110. end;
  111. end;
  112. begin
  113. cnodeutils:=twasmnodeutils;
  114. end.