Browse Source

[PATCH 37/83] update writing out of symbols, using FPC mangled names

From 0158592d68b22162f95cfdf07f0219cce8c262e8 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Mon, 16 Sep 2019 16:00:20 -0400

git-svn-id: branches/wasm@45914 -
nickysn 5 years ago
parent
commit
0275232f16
3 changed files with 77 additions and 12 deletions
  1. 8 5
      compiler/systems/t_wasm.pas
  2. 65 6
      compiler/wasm/agwat.pas
  3. 4 1
      compiler/wasm/hlcgcpu.pas

+ 8 - 5
compiler/systems/t_wasm.pas

@@ -5,8 +5,12 @@ interface
 uses
 uses
   systems,
   systems,
 
 
+  globtype,
+
   export, aasmdata, aasmcpu,
   export, aasmdata, aasmcpu,
 
 
+  symsym, symdef,
+
   link,
   link,
 
 
   i_wasm, tgcpu;
   i_wasm, tgcpu;
@@ -51,25 +55,24 @@ end;
 
 
 procedure texportlibwasm.preparelib(const s: string);
 procedure texportlibwasm.preparelib(const s: string);
 begin
 begin
-  writeln('preparelib: ', s);
   //nothing to happen. wasm files are modules
   //nothing to happen. wasm files are modules
-  //inherited preparelib(s);
 end;
 end;
 
 
 procedure texportlibwasm.exportprocedure(hp: texported_item);
 procedure texportlibwasm.exportprocedure(hp: texported_item);
+var
+  nm : TSymStr;
 begin
 begin
-  current_asmdata.asmlists[al_exports].Concat( tai_impexp.create(hp.name^, hp.sym.RealName, ie_Func));
+  nm := tprocdef(tprocsym(hp.sym).ProcdefList[0]).mangledname;
+  current_asmdata.asmlists[al_exports].Concat(tai_impexp.create(hp.name^, nm, ie_Func));
 end;
 end;
 
 
 procedure texportlibwasm.exportvar(hp: texported_item);
 procedure texportlibwasm.exportvar(hp: texported_item);
 begin
 begin
-  writeln('exportvar: ', PtrUInt(hp));
   //inherited exportvar(hp);
   //inherited exportvar(hp);
 end;
 end;
 
 
 procedure texportlibwasm.generatelib;
 procedure texportlibwasm.generatelib;
 begin
 begin
-  writeln('gen lib');
   //inherited generatelib;
   //inherited generatelib;
 end;
 end;
 
 

+ 65 - 6
compiler/wasm/agwat.pas

@@ -54,6 +54,7 @@ interface
        procedure WriteProcParams(pd: tprocdef);
        procedure WriteProcParams(pd: tprocdef);
        procedure WriteProcResult(pd: tprocdef);
        procedure WriteProcResult(pd: tprocdef);
        procedure WriteSymtableProcdefs(st: TSymtable);
        procedure WriteSymtableProcdefs(st: TSymtable);
+       procedure WriteSymtableVarSyms(st: TSymtable);
        procedure WriteTempAlloc(p:TAsmList);
        procedure WriteTempAlloc(p:TAsmList);
        procedure WriteExports(p: TAsmList);
        procedure WriteExports(p: TAsmList);
      public
      public
@@ -144,7 +145,7 @@ implementation
 
 
      function constsingle(s: single): ansistring;
      function constsingle(s: single): ansistring;
        begin
        begin
-         result:='0fx'+hexstr(longint(t32bitarray(s)),8);
+         result:='0x'+hexstr(longint(t32bitarray(s)),8);
        end;
        end;
 
 
      function constdouble(d: double): ansistring;
      function constdouble(d: double): ansistring;
@@ -205,7 +206,6 @@ implementation
       i   : integer;
       i   : integer;
     begin
     begin
       //writer.AsmWriteLn('instr');
       //writer.AsmWriteLn('instr');
-      //writeln('>',taicpu(hp).opcode);
       cpu := taicpu(hp);
       cpu := taicpu(hp);
       writer.AsmWrite(#9);
       writer.AsmWrite(#9);
       writer.AsmWrite(wasm_op2str[cpu.opcode] );
       writer.AsmWrite(wasm_op2str[cpu.opcode] );
@@ -240,12 +240,12 @@ implementation
          (not is_javainterface(pd.struct) or
          (not is_javainterface(pd.struct) or
           (pd.proctypeoption in [potype_unitinit,potype_unitfinalize])) then
           (pd.proctypeoption in [potype_unitinit,potype_unitfinalize])) then
       begin
       begin
-        //writeln('mordoy ne vyshel! ',pd.procsym.RealName );
         exit;
         exit;
       end;
       end;
       writer.AsmWrite('(func ');
       writer.AsmWrite('(func ');
 
 
-      writer.AsmWrite( GetWasmName( pd.procsym.RealName ));
+      writer.AsmWrite( GetWasmName( pd.mangledname ));
+      //procsym.RealName ));
       //writer.AsmWriteln(MethodDefinition(pd));
       //writer.AsmWriteln(MethodDefinition(pd));
       {if jvmtypeneedssignature(pd) then
       {if jvmtypeneedssignature(pd) then
         begin
         begin
@@ -549,8 +549,10 @@ implementation
         writer.AsmWriteLn('(memory 32768) ;; todo: this should be imported or based on the directives ');
         writer.AsmWriteLn('(memory 32768) ;; todo: this should be imported or based on the directives ');
 
 
         { print all global variables }
         { print all global variables }
-        //WriteSymtableVarSyms(current_module.globalsymtable);
-        //WriteSymtableVarSyms(current_module.localsymtable);
+        //current_asmdata.AsmSymbolDict
+        WriteSymtableVarSyms(current_module.globalsymtable);
+        WriteSymtableVarSyms(current_module.localsymtable);
+
         //writer.AsmLn;
         //writer.AsmLn;
         { print all global procedures/functions }
         { print all global procedures/functions }
         WriteSymtableProcdefs(current_module.globalsymtable);
         WriteSymtableProcdefs(current_module.globalsymtable);
@@ -603,6 +605,63 @@ implementation
           end;
           end;
       end;
       end;
 
 
+    procedure TWabtTextAssembler.WriteSymtableVarSyms(st: TSymtable);
+      var
+        i : integer;
+        sym : tsym;
+        sz  : integer;
+      begin
+        if not assigned(st) then
+          exit;
+
+        sz := 0;
+        for i:=0 to st.SymList.Count-1 do
+         begin
+           sym:=tsym(st.SymList[i]);
+           case sym.typ of
+             staticvarsym:
+               begin
+                 //WriteFieldSym(tabstractvarsym(sym));
+                 //if (sym.typ=staticvarsym) and
+                 //   assigned(tstaticvarsym(sym).defaultconstsym) then
+                 //  WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
+                 writer.AsmWrite(#9);
+                 writer.AsmWrite('(global $');
+                 writer.AsmWrite(tcpustaticvarsym(sym).mangledname);
+                 writer.AsmWrite(' i32 (i32.const ');
+                 writer.AsmWrite( tostr(sz));
+                 writer.AsmWrite(')');
+                 writer.AsmWrite(') ');
+                 writer.AsmWriteLn(';; static or field');
+                 inc(sz, tcpustaticvarsym(sym).getsize);
+               end;
+             fieldvarsym:
+               begin
+                 writer.AsmWriteLn(';; field');
+               end;
+             constsym:
+               begin
+                 //if (sym.typ=staticvarsym) and
+                 //   assigned(tstaticvarsym(sym).defaultconstsym) then
+                 //  WriteFieldSym(tabstractvarsym(tstaticvarsym(sym).defaultconstsym));
+                 //{ multiple procedures can have constants with the same name }
+                 //if not assigned(sym.owner.defowner) or
+                 //   (tdef(sym.owner.defowner).typ<>procdef) then
+                 //  WriteConstSym(tconstsym(sym));
+                 writer.AsmWriteLn(';; constant');
+               end;
+             {procsym:
+               begin
+                 for j:=0 to tprocsym(sym).procdeflist.count-1 do
+                   if not(df_generic in tprocdef(tprocsym(sym).procdeflist[j]).defoptions) then
+                     WriteSymtableVarSyms(tprocdef(tprocsym(sym).procdeflist[j]).localst);
+               end;}
+             else
+               ;
+           end;
+         end;
+      end;
+
     procedure TWabtTextAssembler.WriteTempAlloc(p: TAsmList);
     procedure TWabtTextAssembler.WriteTempAlloc(p: TAsmList);
       var
       var
         hp: tai;
         hp: tai;

+ 4 - 1
compiler/wasm/hlcgcpu.pas

@@ -1001,7 +1001,10 @@ implementation
         exit;
         exit;
 
 
       // setting up memory offset
       // setting up memory offset
-      list.Concat(taicpu.op_const(a_i32_const, 0)); //todo: this should not be 0, this should be reference to a global "memory"
+      if assigned(ref.symbol) then
+        list.Concat(taicpu.op_sym(a_get_global, ref.symbol))
+      else
+        list.Concat(taicpu.op_const(a_i32_const, 0)); //todo: this should not be 0, this should be reference to a global "memory"
 
 
         { non-array accesses cannot have an index reg }
         { non-array accesses cannot have an index reg }
         if ref.index<>NR_NO then
         if ref.index<>NR_NO then