Browse Source

[PATCH 51/83] adding support for imported functions

From c027579d00902e2347ee3ad05092145194ccf350 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Fri, 20 Sep 2019 16:48:12 -0400

git-svn-id: branches/wasm@45928 -
nickysn 5 years ago
parent
commit
052a35ae45
4 changed files with 80 additions and 12 deletions
  1. 2 1
      compiler/systems/i_wasm.pas
  2. 28 2
      compiler/systems/t_wasm.pas
  3. 18 9
      compiler/wasm/aasmcpu.pas
  4. 32 0
      compiler/wasm/agwat.pas

+ 2 - 1
compiler/systems/i_wasm.pas

@@ -61,7 +61,8 @@ unit i_wasm;
             objext       : '.wasm';
             resext       : '';
             resobjext    : '.wasm';
-            sharedlibext : '.wasm';
+            sharedlibext : ''; // keep it empty! The sharedlibext drives the export module name
+                               // if this is populated, then the name should be cleared when generating import
             staticlibext : '.wasm';
             staticlibprefix : '';
             sharedlibprefix : '';

+ 28 - 2
compiler/systems/t_wasm.pas

@@ -7,7 +7,8 @@ uses
 
   globtype,
 
-  export, aasmdata, aasmcpu,
+  import, export, aasmdata, aasmcpu,
+  fmodule, ogbase,
 
   symsym, symdef,
 
@@ -26,6 +27,11 @@ type
       procedure generatelib;override;
     end;
 
+  { timportlibwasm }
+  timportlibwasm = class(timportlib)
+      procedure generatelib;override;
+    end;
+
   { tlinkerjvm }
 
   tlinkerwasm=class(texternallinker)
@@ -37,6 +43,26 @@ type
 
 implementation
 
+{ timportlibwasm }
+
+  procedure timportlibwasm.generatelib;
+    var
+      i,j  : longint;
+      SmartFilesCount: Integer;
+      ImportLibrary : TImportLibrary;
+      ImportSymbol  : TImportSymbol;
+    begin
+      for i:=0 to current_module.ImportLibraryList.Count-1 do
+        begin
+          ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
+          for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
+            begin
+              ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
+              current_asmdata.asmlists[al_imports].Concat(tai_impexp.create(ImportLibrary.Name, ImportSymbol.MangledName, ImportSymbol.Name, ie_Func));
+            end;
+        end;
+    end;
+
 { tlinkerwasm }
 
 constructor tlinkerwasm.Create;
@@ -77,7 +103,7 @@ end;
 
 initialization
   RegisterTarget(system_wasm_info);
-
+  RegisterImport(system_wasm_wasm32, timportlibwasm);
   RegisterExport(system_wasm_wasm32, texportlibwasm);
   RegisterLinker(ld_wasm, tlinkerwasm);
 

+ 18 - 9
compiler/wasm/aasmcpu.pas

@@ -89,8 +89,10 @@ uses
       tai_impexp = class(tai)
         extname : ansistring; // external name
         intname : ansistring; // internal name
+        extmodule : ansistring; // external unit name
         symstype: TImpExpType;
-        constructor create(const aextname, aintname: ansistring; asymtype: timpexptype);
+        constructor create(const aextname, aintname: ansistring; asymtype: timpexptype); overload;
+        constructor create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype); overload;
       end;
 
       // local variable declaration
@@ -121,14 +123,21 @@ implementation
 
     { timpexp_ai }
 
-        constructor tai_impexp.create(const aextname, aintname: ansistring;
-      asymtype: timpexptype);
-    begin
-      inherited create;
-      typ := ait_importexport;
-      extname := aextname;
-      intname := aintname;
-    end;
+      constructor tai_impexp.create(const aextname, aintname: ansistring;
+          asymtype: timpexptype);
+        begin
+          create('', aextname, aintname, asymtype);;
+        end;
+
+      constructor tai_impexp.create(const aextmodule, aextname, aintname: ansistring; asymtype: timpexptype);
+        begin
+          inherited create;
+          typ := ait_importexport;
+          extmodule := aextmodule;
+          extname := aextname;
+          intname := aintname;
+          symstype:= asymtype;
+        end;
 
 {*****************************************************************************
                                  taicpu Constructors

+ 32 - 0
compiler/wasm/agwat.pas

@@ -57,6 +57,7 @@ interface
        procedure WriteSymtableVarSyms(st: TSymtable);
        procedure WriteTempAlloc(p:TAsmList);
        procedure WriteExports(p: TAsmList);
+       procedure WriteImports(p: TAsmList);
      public
        constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
        procedure WriteTree(p:TAsmList);override;
@@ -555,6 +556,7 @@ implementation
         writer.MarkEmpty;
         writer.AsmWriteLn('(module ');
         writer.AsmWriteLn('(import "env" "memory" (memory 0)) ;;');
+        WriteImports(current_asmdata.asmlists[al_imports]);
 
         { print all global variables }
         //current_asmdata.AsmSymbolDict
@@ -757,6 +759,36 @@ implementation
       end;
     end;
 
+    procedure TWabtTextAssembler.WriteImports(p: TAsmList);
+    var
+      hp: tai;
+      x: tai_impexp;
+    begin
+      if not Assigned(p) then Exit;
+      hp:=tai(p.First);
+      while Assigned(hp) do begin
+        case hp.typ of
+          ait_importexport:
+          begin
+            x:=tai_impexp(hp);
+            writer.AsmWrite('(import "');
+            writer.AsmWrite(x.extmodule);
+            writer.AsmWrite('" "');
+            writer.AsmWrite(x.extname);
+            writer.AsmWrite('" (');
+            case x.symstype of
+              ie_Func: writer.AsmWrite('func');
+            end;
+            writer.AsmWrite(' ');
+            writer.AsmWrite(GetWasmName(x.intname));
+            writer.AsmWrite('))');
+            writer.AsmLn;
+          end;
+        end;
+        hp := tai_impexp(hp.Next);
+      end;
+    end;
+
 
 { TWatInstrWriter }