nwasmutil.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. var
  56. modules: array of tmodule;
  57. function ModuleExists(m: tmodule): Boolean;
  58. var
  59. q: tmodule;
  60. begin
  61. result:=true;
  62. for q in modules do
  63. if q=m then
  64. exit;
  65. result:=false;
  66. end;
  67. procedure AddModule(m: tmodule);
  68. begin
  69. SetLength(modules,Length(modules)+1);
  70. modules[High(modules)]:=m;
  71. end;
  72. procedure WriteImportDll(list: TAsmList; proc: tprocdef);
  73. begin
  74. thlcgwasm(hlcg).g_procdef(list,proc);
  75. list.Concat(tai_import_module.create(proc.mangledname,proc.import_dll^));
  76. list.Concat(tai_import_name.create(proc.mangledname,proc.import_name^));
  77. end;
  78. procedure InsertModuleInfo(list : TAsmList;u: tmodule);
  79. var
  80. i: Integer;
  81. def : tdef;
  82. proc : tprocdef;
  83. cur_unit : tused_unit;
  84. begin
  85. if ModuleExists(u) then
  86. exit;
  87. AddModule(u);
  88. cur_unit:=tused_unit(u.used_units.First);
  89. while assigned(cur_unit) do
  90. begin
  91. InsertModuleInfo(list,cur_unit.u);
  92. cur_unit:=tused_unit(cur_unit.Next);
  93. end;
  94. if ((u.moduleflags * [mf_init,mf_finalize])<>[]) and assigned(u.globalsymtable) then
  95. begin
  96. if mf_init in u.moduleflags then
  97. list.Concat(tai_functype.create(make_mangledname('INIT$',u.globalsymtable,''),TWasmFuncType.Create([],[])));
  98. if mf_finalize in u.moduleflags then
  99. list.Concat(tai_functype.create(make_mangledname('FINALIZE$',u.globalsymtable,''),TWasmFuncType.Create([],[])));
  100. end;
  101. for i:=0 to u.deflist.Count-1 do
  102. begin
  103. def:=tdef(u.deflist[i]);
  104. if assigned(def) and (tdef(def).typ = procdef) then
  105. begin
  106. proc := tprocdef(def);
  107. if (po_external in proc.procoptions) and (po_has_importdll in proc.procoptions) then
  108. WriteImportDll(list,proc)
  109. else if not proc.owner.iscurrentunit or (po_external in proc.procoptions) then
  110. thlcgwasm(hlcg).g_procdef(list,proc);
  111. end;
  112. end;
  113. end;
  114. var
  115. i : integer;
  116. def : tdef;
  117. proc : tprocdef;
  118. list : TAsmList;
  119. cur_unit: tused_unit;
  120. begin
  121. inherited;
  122. FillChar(modules,sizeof(modules),0);
  123. list:=current_asmdata.asmlists[al_start];
  124. list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32,false));
  125. if ts_wasm_threads in current_settings.targetswitches then
  126. begin
  127. list.Concat(tai_globaltype.create(TLS_SIZE_SYM,wbt_i32,true));
  128. list.Concat(tai_globaltype.create(TLS_ALIGN_SYM,wbt_i32,true));
  129. list.Concat(tai_globaltype.create(TLS_BASE_SYM,wbt_i32,false));
  130. end;
  131. if ts_wasm_native_exceptions in current_settings.targetswitches then
  132. begin
  133. list.Concat(tai_tagtype.create(FPC_EXCEPTION_TAG_SYM, []));
  134. list.Concat(tai_symbol.Create_Weak(current_asmdata.WeakRefAsmSymbol(FPC_EXCEPTION_TAG_SYM,AT_WASM_EXCEPTION_TAG),0));
  135. end;
  136. for i:=0 to current_module.deflist.Count-1 do
  137. begin
  138. def:=tdef(current_module.deflist[i]);
  139. { since commit 48986 deflist might have NIL entries }
  140. if assigned(def) and (def.typ=procdef) then
  141. begin
  142. proc := tprocdef(def);
  143. if po_external in proc.procoptions then
  144. if po_has_importdll in proc.procoptions then
  145. WriteImportDll(list,proc)
  146. else
  147. thlcgwasm(hlcg).g_procdef(list,proc);
  148. end;
  149. end;
  150. InsertModuleInfo(list,current_module);
  151. cur_unit:=tused_unit(usedunits.First);
  152. while assigned(cur_unit) do
  153. begin
  154. InsertModuleInfo(list,cur_unit.u);
  155. cur_unit:=tused_unit(cur_unit.Next);
  156. end;
  157. end;
  158. begin
  159. cnodeutils:=twasmnodeutils;
  160. end.