Browse Source

[PATCH 59/83] allowing assembly of units

From c3b4c10638c736a2d337edea867bf389b576cb7c Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Mon, 23 Sep 2019 12:24:10 -0400

git-svn-id: branches/wasm@45936 -
nickysn 5 years ago
parent
commit
b13963f9a7
1 changed files with 42 additions and 12 deletions
  1. 42 12
      compiler/wasm/agwat.pas

+ 42 - 12
compiler/wasm/agwat.pas

@@ -64,6 +64,7 @@ interface
        procedure WriteSymtableVarSyms(st: TSymtable);
        procedure WriteSymtableVarSyms(st: TSymtable);
        procedure WriteTempAlloc(p:TAsmList);
        procedure WriteTempAlloc(p:TAsmList);
        procedure WriteExports(p: TAsmList);
        procedure WriteExports(p: TAsmList);
+       procedure WriteUnitExports(st: TSymtable);
        procedure WriteImports;
        procedure WriteImports;
 
 
        procedure WriteOutPChar(p: pchar; ofs, len: integer);
        procedure WriteOutPChar(p: pchar; ofs, len: integer);
@@ -281,16 +282,12 @@ implementation
           writer.AsmWriteln('"');
           writer.AsmWriteln('"');
         end;}
         end;}
       writer.AsmLn;
       writer.AsmLn;
-      WriteProcParams(tcpuprocdef(pd));
-      WriteProcResult(tcpuprocdef(pd));
+      WriteProcParams(pd);
+      WriteProcResult(pd);
 
 
       if not stub then begin
       if not stub then begin
         WriteTempAlloc(tcpuprocdef(pd).exprasmlist);
         WriteTempAlloc(tcpuprocdef(pd).exprasmlist);
         WriteTree(tcpuprocdef(pd).exprasmlist);
         WriteTree(tcpuprocdef(pd).exprasmlist);
-      end else begin
-        // stub function
-        writer.AsmWrite(#9);
-        writer.AsmWriteLn('unreachable');
       end;
       end;
       writer.AsmWriteln(#9')');
       writer.AsmWriteln(#9')');
       writer.AsmLn;
       writer.AsmLn;
@@ -581,10 +578,6 @@ implementation
       var
       var
         hal : tasmlisttype;
         hal : tasmlisttype;
       begin
       begin
-        if current_module.is_unit then begin
-          writer.AsmWriteLn('(module)');
-          exit;
-        end;
         writer.MarkEmpty;
         writer.MarkEmpty;
         writer.AsmWriteLn('(module ');
         writer.AsmWriteLn('(module ');
         writer.AsmWriteLn('(import "env" "memory" (memory 0)) ;;');
         writer.AsmWriteLn('(import "env" "memory" (memory 0)) ;;');
@@ -604,7 +597,10 @@ implementation
         WriteSymtableProcdefs(current_module.globalsymtable);
         WriteSymtableProcdefs(current_module.globalsymtable);
         WriteSymtableProcdefs(current_module.localsymtable);
         WriteSymtableProcdefs(current_module.localsymtable);
 
 
-        WriteExports(current_asmdata.asmlists[al_exports]);
+        if current_module.islibrary then
+          WriteExports(current_asmdata.asmlists[al_exports])
+        else
+          WriteUnitExports(current_module.globalsymtable);
 
 
         //WriteSymtableStructDefs(current_module.globalsymtable);
         //WriteSymtableStructDefs(current_module.globalsymtable);
         //WriteSymtableStructDefs(current_module.localsymtable);
         //WriteSymtableStructDefs(current_module.localsymtable);
@@ -788,6 +784,40 @@ implementation
       end;
       end;
     end;
     end;
 
 
+    procedure TWabtTextAssembler.WriteUnitExports(st: TSymtable);
+    var
+      i   : longint;
+      def : tdef;
+    begin
+      if not assigned(st) then
+        exit;
+
+      writer.AsmWrite(#9'(table 0 funcref)');
+      writer.AsmWriteLn(#9'(elem 0 (i32.const 0) ');
+      for i:=0 to st.DefList.Count-1 do
+        begin
+          def:=tdef(st.DefList[i]);
+          case def.typ of
+            procdef :
+              begin
+                { methods are also in the static/globalsymtable of the unit
+                  -> make sure they are only written for the objectdefs that
+                  own them }
+                  if
+                   not (po_external in tprocdef(def).procoptions)
+                   then
+                  begin
+                    writer.AsmWrite(#9#9'$');
+                    writer.AsmWriteLn(tprocdef(def).mangledname);
+                  end;
+              end;
+            else
+              ;
+          end;
+        end;
+      writer.AsmWriteLn(#9') ');
+    end;
+
     procedure TWabtTextAssembler.WriteImports;
     procedure TWabtTextAssembler.WriteImports;
       var
       var
         i    : integer;
         i    : integer;
@@ -829,7 +859,7 @@ implementation
               writer.AsmWrite(#9'(import "_fpc_use" "');
               writer.AsmWrite(#9'(import "_fpc_use" "');
               writer.AsmWrite(proc.mangledname);
               writer.AsmWrite(proc.mangledname);
               writer.AsmWrite('" ');
               writer.AsmWrite('" ');
-              WriteProcDef(proc);
+              WriteProcDef(proc, true);
               writer.AsmWrite(')');
               writer.AsmWrite(')');
             end;
             end;
           end;
           end;