Browse Source

+ support the call_indirect instruction and the R_WASM_TYPE_INDEX_LEB
relocation that it requires in the internal wasm object writer

Nikolay Nikolov 3 năm trước cách đây
mục cha
commit
9fa2e2934b
3 tập tin đã thay đổi với 73 bổ sung0 xóa
  1. 1 0
      compiler/ogbase.pas
  2. 37 0
      compiler/ogwasm.pas
  3. 35 0
      compiler/wasm32/aasmcpu.pas

+ 1 - 0
compiler/ogbase.pas

@@ -116,6 +116,7 @@ interface
          RELOC_FUNCTION_INDEX_LEB,
          RELOC_MEMORY_ADDR_LEB,
          RELOC_MEMORY_ADDR_SLEB,
+         RELOC_TYPE_INDEX_LEB,
 {$endif WASM32}
          { Relative relocation }
          RELOC_RELATIVE,

+ 37 - 0
compiler/ogwasm.pas

@@ -57,6 +57,9 @@ interface
       { TWasmObjRelocation }
 
       TWasmObjRelocation = class(TObjRelocation)
+      public
+        TypeIndex: Integer;
+        constructor CreateTypeIndex(ADataOffset:TObjSectionOfs; ATypeIndex: Integer);
       end;
 
       { TWasmObjSymbolExtraData }
@@ -273,6 +276,21 @@ implementation
         WriteUleb5(d,q);
       end;
 
+{****************************************************************************
+                             TWasmObjRelocation
+****************************************************************************}
+
+    constructor TWasmObjRelocation.CreateTypeIndex(ADataOffset: TObjSectionOfs; ATypeIndex: Integer);
+      begin
+        DataOffset:=ADataOffset;
+        Symbol:=nil;
+        OrgSize:=0;
+        Group:=nil;
+        ObjSection:=nil;
+        ftype:=ord(RELOC_TYPE_INDEX_LEB);
+        TypeIndex:=ATypeIndex;
+      end;
+
 {****************************************************************************
                                TWasmObjSymbol
 ****************************************************************************}
@@ -558,6 +576,16 @@ implementation
               Data:=NtoLE(Data);
               writebytes(Data,4);
             end;
+          RELOC_TYPE_INDEX_LEB:
+            begin
+              if len<>5 then
+                internalerror(2021092612);
+              if assigned(p) then
+                internalerror(2021092613);
+              objreloc:=TWasmObjRelocation.CreateTypeIndex(CurrObjSec.Size,Data);
+              CurrObjSec.ObjRelocations.Add(objreloc);
+              WriteUleb5(CurrObjSec,Data);
+            end;
           else
             internalerror(2021092501);
         end;
@@ -903,6 +931,8 @@ implementation
                     end;
                   RELOC_ABSOLUTE:
                     ;
+                  RELOC_TYPE_INDEX_LEB:
+                    ;
                   else
                     internalerror(2021092510);
                 end;
@@ -978,6 +1008,13 @@ implementation
                       else
                         internalerror(2021092609);
                     end;
+                  RELOC_TYPE_INDEX_LEB:
+                    begin
+                      Inc(relcount^);
+                      WriteByte(relout,Ord(R_WASM_TYPE_INDEX_LEB));
+                      WriteUleb(relout,objrel.DataOffset+objsec.FileSectionOfs);
+                      WriteUleb(relout,objrel.TypeIndex);
+                    end;
                   else
                     internalerror(2021092507);
                 end;

+ 35 - 0
compiler/wasm32/aasmcpu.pas

@@ -149,6 +149,9 @@ uses
 
 implementation
 
+uses
+  ogwasm;
+
     { tai_import_name }
 
     constructor tai_import_name.create(const asymname, aimportname: string);
@@ -652,6 +655,22 @@ implementation
                     internalerror(2021092022);
                 end;
             end;
+          a_call_indirect:
+            begin
+              if ops<>1 then
+                internalerror(2021092610);
+              with oper[0]^ do
+                case typ of
+                  top_functype:
+                    begin
+                      TWasmObjData(objdata).AddFuncType(functype);
+                      result:=6+
+                        UlebSize(0);
+                    end;
+                  else
+                    internalerror(2021092611);
+                end;
+            end;
           else
             Writeln('Warning! Not implemented opcode, pass1: ', opcode);
         end;
@@ -1135,6 +1154,22 @@ implementation
                     internalerror(2021092022);
                 end;
             end;
+          a_call_indirect:
+            begin
+              if ops<>1 then
+                internalerror(2021092610);
+              with oper[0]^ do
+                case typ of
+                  top_functype:
+                    begin
+                      WriteByte($11);
+                      objdata.writeReloc(TWasmObjData(objdata).AddFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
+                      WriteUleb(0);
+                    end;
+                  else
+                    internalerror(2021092611);
+                end;
+            end;
           else
             Writeln('Warning! Not implemented opcode, pass2: ', opcode);
         end;