Browse Source

[PATCH 23/83] exports support in wasm

From 7a838cf84392b1b831ff0fb9f40fd8f811c5543f Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Wed, 11 Sep 2019 15:00:09 -0400

git-svn-id: branches/wasm@45900 -
nickysn 5 years ago
parent
commit
4c87028fa0
2 changed files with 60 additions and 0 deletions
  1. 29 0
      compiler/wasm/aasmcpu.pas
  2. 31 0
      compiler/wasm/agwat.pas

+ 29 - 0
compiler/wasm/aasmcpu.pas

@@ -75,6 +75,24 @@ uses
         { nothing to add }
         { nothing to add }
       end;
       end;
 
 
+      TImpExpType= (
+        ie_Func,   // functions
+        ie_Table,  // tables (arrays of methods)
+        ie_Memory, // memory reference
+        ie_Global  // global variables
+      );
+
+      // the actual use is defined by the assembly section used
+
+      { timpexp_ai }
+
+      tai_impexp = class(tai)
+        extname : ansistring; // external name
+        intname : ansistring; // internal name
+        symstype: TImpExpType;
+        constructor create(const aextname, aintname: ansistring; asymtype: timpexptype);
+      end;
+
     procedure InitAsm;
     procedure InitAsm;
     procedure DoneAsm;
     procedure DoneAsm;
 
 
@@ -83,6 +101,17 @@ uses
 
 
 implementation
 implementation
 
 
+    { timpexp_ai }
+
+        constructor tai_impexp.create(const aextname, aintname: ansistring;
+      asymtype: timpexptype);
+    begin
+      inherited create;
+      typ := ait_importexport;
+      extname := aextname;
+      intname := aintname;
+    end;
+
 {*****************************************************************************
 {*****************************************************************************
                                  taicpu Constructors
                                  taicpu Constructors
 *****************************************************************************}
 *****************************************************************************}

+ 31 - 0
compiler/wasm/agwat.pas

@@ -55,6 +55,7 @@ interface
        procedure WriteProcResult(pd: tprocdef);
        procedure WriteProcResult(pd: tprocdef);
        procedure WriteSymtableProcdefs(st: TSymtable);
        procedure WriteSymtableProcdefs(st: TSymtable);
        procedure WriteTempAlloc(p:TAsmList);
        procedure WriteTempAlloc(p:TAsmList);
+       procedure WriteExports(p: TAsmList);
      public
      public
        constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
        constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
        procedure WriteTree(p:TAsmList);override;
        procedure WriteTree(p:TAsmList);override;
@@ -562,6 +563,8 @@ implementation
         WriteSymtableProcdefs(current_module.globalsymtable);
         WriteSymtableProcdefs(current_module.globalsymtable);
         WriteSymtableProcdefs(current_module.localsymtable);
         WriteSymtableProcdefs(current_module.localsymtable);
 
 
+        WriteExports(current_asmdata.asmlists[al_exports]);
+
         //WriteSymtableStructDefs(current_module.globalsymtable);
         //WriteSymtableStructDefs(current_module.globalsymtable);
         //WriteSymtableStructDefs(current_module.localsymtable);
         //WriteSymtableStructDefs(current_module.localsymtable);
         //writer.decorator.LinePrefix := '';
         //writer.decorator.LinePrefix := '';
@@ -666,6 +669,34 @@ implementation
 
 
       end;
       end;
 
 
+    procedure TWabtTextAssembler.WriteExports(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('(export "');
+            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 }
 { TWatInstrWriter }