nwasmutil.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. symsym;
  23. type
  24. { twasmnodeutils }
  25. twasmnodeutils = class(tnodeutils)
  26. public
  27. class procedure insertbssdata(sym : tstaticvarsym); override;
  28. class procedure InsertObjectInfo; override;
  29. end;
  30. implementation
  31. uses
  32. globtype,globals,
  33. cpubase,
  34. aasmbase,aasmdata,aasmtai,aasmcpu,
  35. hlcgobj,hlcgcpu,
  36. symdef,symtype,symconst,symcpu,
  37. fmodule;
  38. { twasmnodeutils }
  39. class procedure twasmnodeutils.insertbssdata(sym: tstaticvarsym);
  40. var
  41. symcpu: tcpustaticvarsym;
  42. begin
  43. symcpu:=tcpustaticvarsym(sym);
  44. if symcpu.is_wasm_global then
  45. begin
  46. if sym.globalasmsym then
  47. current_asmdata.asmlists[al_start].Concat(tai_globaltype.create_global(symcpu.mangledname,symcpu.get_wasm_global_vardef_type,false,symcpu.vardef))
  48. else
  49. current_asmdata.asmlists[al_start].Concat(tai_globaltype.create_local(symcpu.mangledname,symcpu.get_wasm_global_vardef_type,false,symcpu.vardef));
  50. end
  51. else
  52. inherited;
  53. end;
  54. class procedure twasmnodeutils.InsertObjectInfo;
  55. procedure WriteImportDll(list: TAsmList; proc: tprocdef);
  56. begin
  57. thlcgwasm(hlcg).g_procdef(list,proc);
  58. list.Concat(tai_import_module.create(proc.mangledname,proc.import_dll^));
  59. list.Concat(tai_import_name.create(proc.mangledname,proc.import_name^));
  60. end;
  61. procedure InsertUnitInfo(list : TAsmList;cur_unit: tused_unit);
  62. var
  63. i: Integer;
  64. def : tdef;
  65. proc : tprocdef;
  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) then
  83. thlcgwasm(hlcg).g_procdef(list,proc);
  84. end;
  85. end;
  86. end;
  87. var
  88. i : integer;
  89. def : tdef;
  90. proc : tprocdef;
  91. list : TAsmList;
  92. cur_unit: tused_unit;
  93. begin
  94. inherited;
  95. list:=current_asmdata.asmlists[al_start];
  96. list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32,false));
  97. if ts_wasm_threads in current_settings.targetswitches then
  98. begin
  99. list.Concat(tai_globaltype.create(TLS_SIZE_SYM,wbt_i32,true));
  100. list.Concat(tai_globaltype.create(TLS_ALIGN_SYM,wbt_i32,true));
  101. list.Concat(tai_globaltype.create(TLS_BASE_SYM,wbt_i32,false));
  102. end;
  103. if ts_wasm_native_exceptions in current_settings.targetswitches then
  104. begin
  105. list.Concat(tai_tagtype.create(FPC_EXCEPTION_TAG_SYM, []));
  106. list.Concat(tai_symbol.Create_Weak(current_asmdata.WeakRefAsmSymbol(FPC_EXCEPTION_TAG_SYM,AT_WASM_EXCEPTION_TAG),0));
  107. end;
  108. for i:=0 to current_module.deflist.Count-1 do
  109. begin
  110. def:=tdef(current_module.deflist[i]);
  111. { since commit 48986 deflist might have NIL entries }
  112. if assigned(def) and (def.typ=procdef) then
  113. begin
  114. proc := tprocdef(def);
  115. if po_external in proc.procoptions then
  116. if po_has_importdll in proc.procoptions then
  117. WriteImportDll(list,proc)
  118. else
  119. thlcgwasm(hlcg).g_procdef(list,proc);
  120. end;
  121. end;
  122. cur_unit:=tused_unit(usedunits.First);
  123. while assigned(cur_unit) do
  124. begin
  125. InsertUnitInfo(list,cur_unit);
  126. cur_unit:=tused_unit(cur_unit.Next);
  127. end;
  128. cur_unit:=tused_unit(current_module.used_units.First);
  129. while assigned(cur_unit) do
  130. begin
  131. InsertUnitInfo(list,cur_unit);
  132. cur_unit:=tused_unit(cur_unit.Next);
  133. end;
  134. end;
  135. begin
  136. cnodeutils:=twasmnodeutils;
  137. end.