Browse Source

+ support tai_symbolpair in the wasm internal obj writer, so that the
PASCALMAIN alias of main can be created

Nikolay Nikolov 3 years ago
parent
commit
acdb65c2a3
1 changed files with 32 additions and 5 deletions
  1. 32 5
      compiler/ogwasm.pas

+ 32 - 5
compiler/ogwasm.pas

@@ -46,8 +46,10 @@ interface
         ImportIndex: Integer;
         ImportIndex: Integer;
         FuncIndex: Integer;
         FuncIndex: Integer;
         SymbolIndex: Integer;
         SymbolIndex: Integer;
+        AliasOf: string;
         constructor create(AList:TFPHashObjectList;const AName:string);
         constructor create(AList:TFPHashObjectList;const AName:string);
         function ImportOrFuncIndex: Integer;
         function ImportOrFuncIndex: Integer;
+        function IsAlias: Boolean;
       end;
       end;
 
 
       { TWasmObjRelocation }
       { TWasmObjRelocation }
@@ -98,6 +100,7 @@ interface
         procedure DeclareImportModule(aim: tai_import_module);
         procedure DeclareImportModule(aim: tai_import_module);
         procedure DeclareImportName(ain: tai_import_name);
         procedure DeclareImportName(ain: tai_import_name);
         procedure DeclareLocal(al: tai_local);
         procedure DeclareLocal(al: tai_local);
+        procedure symbolpairdefine(akind: TSymbolPairKind;const asym, avalue: string);override;
       end;
       end;
 
 
       { TWasmObjOutput }
       { TWasmObjOutput }
@@ -278,6 +281,7 @@ implementation
         ImportIndex:=-1;
         ImportIndex:=-1;
         FuncIndex:=-1;
         FuncIndex:=-1;
         SymbolIndex:=-1;
         SymbolIndex:=-1;
+        AliasOf:='';
       end;
       end;
 
 
     function TWasmObjSymbol.ImportOrFuncIndex: Integer;
     function TWasmObjSymbol.ImportOrFuncIndex: Integer;
@@ -290,6 +294,11 @@ implementation
           internalerror(2021092601);
           internalerror(2021092601);
       end;
       end;
 
 
+    function TWasmObjSymbol.IsAlias: Boolean;
+      begin
+        result:=AliasOf<>'';
+      end;
+
 {****************************************************************************
 {****************************************************************************
                               TWasmObjSymbolExtraData
                               TWasmObjSymbolExtraData
 ****************************************************************************}
 ****************************************************************************}
@@ -601,6 +610,16 @@ implementation
         ObjSymExtraData.AddLocal(al.bastyp);
         ObjSymExtraData.AddLocal(al.bastyp);
       end;
       end;
 
 
+    procedure TWasmObjData.symbolpairdefine(akind: TSymbolPairKind; const asym, avalue: string);
+      var
+        valsym: TObjSymbol;
+        aliassym: TWasmObjSymbol;
+      begin
+        valsym:=CreateSymbol(avalue);
+        aliassym:=TWasmObjSymbol(symboldefine(asym,valsym.bind,valsym.typ));
+        aliassym.AliasOf:=valsym.Name;
+      end;
+
 {****************************************************************************
 {****************************************************************************
                                TWasmObjOutput
                                TWasmObjOutput
 ****************************************************************************}
 ****************************************************************************}
@@ -968,7 +987,7 @@ implementation
             objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
             objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
             if IsExternalFunction(objsym) then
             if IsExternalFunction(objsym) then
               Inc(import_functions_count);
               Inc(import_functions_count);
-            if objsym.typ=AT_FUNCTION then
+            if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
               Inc(functions_count);
               Inc(functions_count);
           end;
           end;
 
 
@@ -1068,7 +1087,7 @@ implementation
         for i:=0 to Data.ObjSymbolList.Count-1 do
         for i:=0 to Data.ObjSymbolList.Count-1 do
           begin
           begin
             objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
             objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
-            if objsym.typ=AT_FUNCTION then
+            if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
               begin
               begin
                 objsym.FuncIndex:=NextFunctionIndex;
                 objsym.FuncIndex:=NextFunctionIndex;
                 Inc(NextFunctionIndex);
                 Inc(NextFunctionIndex);
@@ -1092,8 +1111,16 @@ implementation
                 objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
                 objsym.SymbolIndex:=FWasmSymbolTableEntriesCount;
                 Inc(FWasmSymbolTableEntriesCount);
                 Inc(FWasmSymbolTableEntriesCount);
                 WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
                 WriteByte(FWasmSymbolTable,Ord(SYMTAB_FUNCTION));
-                WriteUleb(FWasmSymbolTable,0);
-                WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
+                if objsym.IsAlias then
+                  begin
+                    WriteUleb(FWasmSymbolTable,WASM_SYM_EXPLICIT_NAME or WASM_SYM_NO_STRIP);
+                    WriteUleb(FWasmSymbolTable,TWasmObjSymbol(Data.ObjSymbolList.Find(objsym.AliasOf)).FuncIndex);
+                  end
+                else
+                  begin
+                    WriteUleb(FWasmSymbolTable,0);
+                    WriteUleb(FWasmSymbolTable,objsym.FuncIndex);
+                  end;
                 WriteName(FWasmSymbolTable,objsym.Name);
                 WriteName(FWasmSymbolTable,objsym.Name);
               end
               end
             else if (objsym.typ=AT_DATA) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
             else if (objsym.typ=AT_DATA) or ((objsym.typ=AT_NONE) and (objsym.bind=AB_EXTERNAL)) then
@@ -1125,7 +1152,7 @@ implementation
         for i:=0 to Data.ObjSymbolList.Count-1 do
         for i:=0 to Data.ObjSymbolList.Count-1 do
           begin
           begin
             objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
             objsym:=TWasmObjSymbol(Data.ObjSymbolList[i]);
-            if objsym.typ=AT_FUNCTION then
+            if (objsym.typ=AT_FUNCTION) and not objsym.IsAlias then
               WriteFunctionCode(FWasmSections[wsiCode],objsym);
               WriteFunctionCode(FWasmSections[wsiCode],objsym);
           end;
           end;