Browse Source

[PATCH 103/188] update writing of element

From c5bd478280ac1a57b399a07d2635d1e45ae1a34f Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Tue, 17 Mar 2020 15:24:03 -0400

git-svn-id: branches/wasm@46099 -
nickysn 5 years ago
parent
commit
bd3d37552f
2 changed files with 62 additions and 20 deletions
  1. 59 19
      utils/wasmbin/wasmbinwriter.pas
  2. 3 1
      utils/wasmbin/wasmmodule.pas

+ 59 - 19
utils/wasmbin/wasmbinwriter.pas

@@ -55,6 +55,7 @@ type
     procedure WriteFuncSect;
     procedure WriteFuncSect;
     procedure WriteExportSect;
     procedure WriteExportSect;
     procedure WriteCodeSect;
     procedure WriteCodeSect;
+    procedure WriteElemSect;
 
 
     procedure WriteLinkingSect;
     procedure WriteLinkingSect;
     procedure WriteRelocSect;
     procedure WriteRelocSect;
@@ -173,7 +174,7 @@ procedure TBinWriter.AddReloc(relocType: byte; ofs: int64; index: UInt32);
 var
 var
   i : integer;
   i : integer;
   f : TWasmFunc;
   f : TWasmFunc;
-  so : TSymbolObject;
+  //so : TSymbolObject;
 begin
 begin
   if relocCount=length(reloc) then begin
   if relocCount=length(reloc) then begin
     if relocCount=0 then SetLength(reloc, 16)
     if relocCount=0 then SetLength(reloc, 16)
@@ -259,6 +260,12 @@ begin
     inc(writeSec);
     inc(writeSec);
   end;
   end;
 
 
+  // 09 - element sections
+  if m.ElementCount>0 then begin
+    WriteElemSect;
+    inc(writeSec);
+  end;
+
   // 10 code section
   // 10 code section
   if m.FuncCount>0 then begin
   if m.FuncCount>0 then begin
     WriteCodeSect;
     WriteCodeSect;
@@ -423,6 +430,39 @@ begin
   SectionEnd(sc);
   SectionEnd(sc);
 end;
 end;
 
 
+procedure TBinWriter.WriteElemSect;
+var
+  sc : TSectionRec;
+  el : TWasmElement;
+  i  : Integer;
+  j  : Integer;
+begin
+  SectionBegin(SECT_ELEMENT, sc);
+
+  WriteU32(dst, module.ElementCount);
+  for i:=0 to module.ElementCount-1 do begin
+    el := module.GetElement(i);
+    WriteU32(dst, el.tableIdx);
+
+    dst.WriteByte(INST_i32_const);
+    WriteU32(dst, el.offset);
+    dst.WriteByte(INST_END);
+
+    WriteU32(dst, el.funcCount);
+
+    if writeReloc then begin
+      for j:=0 to el.funcCount-1 do begin
+        AddReloc(R_WASM_FUNCTION_INDEX_LEB, dst.Position - sc.datapos, el.funcs[j]);
+        WriteRelocU32(el.funcs[j]);
+      end;
+    end else
+      for j:=0 to el.funcCount-1 do
+        WriteU32(dst, el.funcs[j]);
+  end;
+
+  SectionEnd(sc);
+end;
+
 procedure TBinWriter.WriteLinkingSect;
 procedure TBinWriter.WriteLinkingSect;
 var
 var
   sc : TSectionRec;
   sc : TSectionRec;
@@ -537,13 +577,21 @@ end;
 
 
 procedure TBinWriter.WriteInstList(list: TWasmInstrList; ofsAddition: LongWord);
 procedure TBinWriter.WriteInstList(list: TWasmInstrList; ofsAddition: LongWord);
 var
 var
-  i  : integer;
-  ci : TWasmInstr;
+  i   : integer;
+  ci  : TWasmInstr;
   idx : integer;
   idx : integer;
+  rt  : Byte;
 begin
 begin
   for i:=0 to list.Count-1 do begin
   for i:=0 to list.Count-1 do begin
     ci :=list[i];
     ci :=list[i];
     dst.WriteByte(ci.code);
     dst.WriteByte(ci.code);
+
+    if ci.hasRelocIdx then begin
+      idx := ci.relocIdx;
+      rt := ci.relocType;
+      AddReloc(rt, dst.Position+ofsAddition, LongWord(idx));
+    end;
+
     case INST_FLAGS[ci.code].Param of
     case INST_FLAGS[ci.code].Param of
       ipi32: begin
       ipi32: begin
         WriteI32Operand(dst, ci.operandText);
         WriteI32Operand(dst, ci.operandText);
@@ -554,24 +602,19 @@ begin
       end;
       end;
 
 
       ipi32OrFunc: begin
       ipi32OrFunc: begin
-        if (ci.operandText<>'') and (ci.operandText[1]='$') then begin
-          idx := FindFunc(module, ci.operandText);
-          AddReloc(INST_RELOC_FLAGS[ci.code].relocType, dst.Position+ofsAddition, idx);
-          //todo: there's no need
-          WriteRelocU32(LongWord(idx));
-        end else
+        if ci.hasRelocIdx then
+          // should have been populated with Normalize
+          WriteRelocU32(LongWord(ci.operandNum))
+        else
           WriteI32Operand(dst, ci.operandText);
           WriteI32Operand(dst, ci.operandText);
       end;
       end;
       //ipf32,     // float point single
       //ipf32,     // float point single
       //ipf64,     // float point double
       //ipf64,     // float point double
 
 
-
       ipLeb:
       ipLeb:
       begin
       begin
-        if INST_RELOC_FLAGS[ci.code].doReloc then begin
-          AddReloc(INST_RELOC_FLAGS[ci.code].relocType, dst.Position+ofsAddition, ci.operandNum);
+        if ci.hasRelocIdx then
           WriteRelocU32(ci.operandNum)
           WriteRelocU32(ci.operandNum)
-        end
         else
         else
           WriteU32(dst, ci.operandNum);
           WriteU32(dst, ci.operandNum);
       end;
       end;
@@ -579,12 +622,9 @@ begin
       ipCallType:
       ipCallType:
       begin
       begin
         if Assigned(ci.insttype) then begin
         if Assigned(ci.insttype) then begin
-          if INST_RELOC_FLAGS[ci.code].doReloc then begin
-            AddReloc(INST_RELOC_FLAGS[ci.code].relocType, dst.Position+ofsAddition, ci.insttype.typeNum);
-            WriteRelocU32(ci.insttype.typeNum);
-          end
-          else
-            WriteU32(dst, ci.insttype.typeNum);
+          if ci.hasRelocIdx
+            then WriteRelocU32(ci.insttype.typeNum)
+            else WriteU32(dst, ci.insttype.typeNum);
         end else
         end else
           WriteU32(dst, LongWord(-1)); // this is an error.
           WriteU32(dst, LongWord(-1)); // this is an error.
 
 

+ 3 - 1
utils/wasmbin/wasmmodule.pas

@@ -208,7 +208,9 @@ function WasmBasTypeToChar(b: byte): Char;
 function WasmFuncTypeDescr(t: TWasmFuncType): string;
 function WasmFuncTypeDescr(t: TWasmFuncType): string;
 
 
 function FindFunc(m: TWasmModule; const funcIdx: string): integer;
 function FindFunc(m: TWasmModule; const funcIdx: string): integer;
+
 // tries to register a function in the module
 // tries to register a function in the module
+// the returned value is the offset of the element within the TABLE.
 function RegisterFuncIdxInElem(m: TWasmModule; const func: Integer): integer;
 function RegisterFuncIdxInElem(m: TWasmModule; const func: Integer): integer;
 function RegisterFuncInElem(m: TWasmModule; const funcId: string): integer;
 function RegisterFuncInElem(m: TWasmModule; const funcId: string): integer;
 
 
@@ -873,7 +875,7 @@ begin
   end;
   end;
   if Result<0 then
   if Result<0 then
     Result := el.AddFunc(func);
     Result := el.AddFunc(func);
-  writeln('result=',result,' for ',func);
+  Result := Result + el.offset;
 end;
 end;
 
 
 function RegisterFuncInElem(m: TWasmModule; const funcId: string): integer;
 function RegisterFuncInElem(m: TWasmModule; const funcId: string): integer;