nwasmutil.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. cclasses,
  33. globtype,globals,
  34. cpubase,
  35. aasmbase,aasmdata,aasmtai,aasmcpu,
  36. hlcgobj,hlcgcpu,
  37. symdef,symtype,symconst,symcpu,
  38. fmodule;
  39. { twasmnodeutils }
  40. class procedure twasmnodeutils.insertbssdata(sym: tstaticvarsym);
  41. var
  42. symcpu: tcpustaticvarsym;
  43. begin
  44. symcpu:=tcpustaticvarsym(sym);
  45. if symcpu.is_wasm_global then
  46. begin
  47. if sym.globalasmsym then
  48. current_asmdata.asmlists[al_start].Concat(tai_globaltype.create_global(symcpu.mangledname,symcpu.get_wasm_global_vardef_type,false,symcpu.vardef))
  49. else
  50. current_asmdata.asmlists[al_start].Concat(tai_globaltype.create_local(symcpu.mangledname,symcpu.get_wasm_global_vardef_type,false,symcpu.vardef));
  51. end
  52. else
  53. inherited;
  54. end;
  55. class procedure twasmnodeutils.InsertObjectInfo;
  56. procedure resetfunctypechecked;
  57. var
  58. module : tmodule;
  59. begin
  60. module:=tmodule(loaded_units.first);
  61. while assigned(module) do
  62. begin
  63. module.functypechecked:=false;
  64. module:=tmodule(module.next);
  65. end;
  66. end;
  67. procedure WriteImportDll(list: TAsmList; proc: tprocdef);
  68. begin
  69. thlcgwasm(hlcg).g_procdef(list,proc,false);
  70. list.Concat(tai_import_module.create(proc.mangledname,proc.import_dll^));
  71. list.Concat(tai_import_name.create(proc.mangledname,proc.import_name^));
  72. end;
  73. procedure InsertModuleInfo(list : TAsmList;u: tmodule);
  74. var
  75. i: Integer;
  76. def : tdef;
  77. proc : tprocdef;
  78. cur_unit : tused_unit;
  79. begin
  80. if u.functypechecked then
  81. exit;
  82. u.functypechecked:=true;
  83. cur_unit:=tused_unit(u.used_units.First);
  84. while assigned(cur_unit) do
  85. begin
  86. InsertModuleInfo(list,cur_unit.u);
  87. cur_unit:=tused_unit(cur_unit.Next);
  88. end;
  89. if ((u.moduleflags * [mf_init,mf_finalize])<>[]) and assigned(u.globalsymtable) then
  90. begin
  91. if mf_init in u.moduleflags then
  92. list.Concat(tai_functype.create(make_mangledname('INIT$',u.globalsymtable,''),TWasmFuncType.Create([],[]),false));
  93. if mf_finalize in u.moduleflags then
  94. list.Concat(tai_functype.create(make_mangledname('FINALIZE$',u.globalsymtable,''),TWasmFuncType.Create([],[]),false));
  95. end;
  96. for i:=0 to u.deflist.Count-1 do
  97. begin
  98. def:=tdef(u.deflist[i]);
  99. if assigned(def) and (tdef(def).typ = procdef) then
  100. begin
  101. proc := tprocdef(def);
  102. if (po_external in proc.procoptions) and (po_has_importdll in proc.procoptions) then
  103. WriteImportDll(list,proc)
  104. else if not proc.owner.iscurrentunit or (po_external in proc.procoptions) then
  105. thlcgwasm(hlcg).g_procdef(list,proc,false)
  106. else
  107. thlcgwasm(hlcg).g_procdef(list,proc,true);
  108. end;
  109. end;
  110. end;
  111. var
  112. i : integer;
  113. def : tdef;
  114. proc : tprocdef;
  115. list : TAsmList;
  116. cur_unit: tused_unit;
  117. begin
  118. inherited;
  119. resetfunctypechecked;
  120. list:=current_asmdata.asmlists[al_start];
  121. list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32,false));
  122. if ts_wasm_threads in current_settings.targetswitches then
  123. begin
  124. list.Concat(tai_globaltype.create(TLS_SIZE_SYM,wbt_i32,true));
  125. list.Concat(tai_globaltype.create(TLS_ALIGN_SYM,wbt_i32,true));
  126. list.Concat(tai_globaltype.create(TLS_BASE_SYM,wbt_i32,false));
  127. end;
  128. if ts_wasm_native_exceptions in current_settings.targetswitches then
  129. begin
  130. list.Concat(tai_tagtype.create(FPC_EXCEPTION_TAG_SYM, []));
  131. list.Concat(tai_symbol.Create_Weak(current_asmdata.WeakRefAsmSymbol(FPC_EXCEPTION_TAG_SYM,AT_WASM_EXCEPTION_TAG),0));
  132. end;
  133. for i:=0 to current_module.deflist.Count-1 do
  134. begin
  135. def:=tdef(current_module.deflist[i]);
  136. { since commit 48986 deflist might have NIL entries }
  137. if assigned(def) and (def.typ=procdef) then
  138. begin
  139. proc := tprocdef(def);
  140. if po_external in proc.procoptions then
  141. if po_has_importdll in proc.procoptions then
  142. WriteImportDll(list,proc)
  143. else
  144. thlcgwasm(hlcg).g_procdef(list,proc,false);
  145. end;
  146. end;
  147. create_hlcodegen;
  148. InsertModuleInfo(list,current_module);
  149. cur_unit:=tused_unit(usedunits.First);
  150. while assigned(cur_unit) do
  151. begin
  152. InsertModuleInfo(list,cur_unit.u);
  153. cur_unit:=tused_unit(cur_unit.Next);
  154. end;
  155. destroy_hlcodegen;
  156. end;
  157. begin
  158. cnodeutils:=twasmnodeutils;
  159. end.