Browse Source

[PATCH 105/188] making element declaration to use instruction list

From 5054e9537254911ade9b96b8bf2d172c9277dbbf Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Tue, 17 Mar 2020 15:53:22 -0400

git-svn-id: branches/wasm@46101 -
nickysn 5 years ago
parent
commit
0bad423386
2 changed files with 16 additions and 10 deletions
  1. 1 5
      utils/wasmbin/wasmbinwriter.pas
  2. 15 5
      utils/wasmbin/wasmmodule.pas

+ 1 - 5
utils/wasmbin/wasmbinwriter.pas

@@ -443,11 +443,7 @@ begin
   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);
-
+    WriteInstList(el.offset, sc.datapos);
     WriteU32(dst, el.funcCount);
 
     if writeReloc then begin

+ 15 - 5
utils/wasmbin/wasmmodule.pas

@@ -126,11 +126,12 @@ type
 
   TWasmElement = class(TObject)
     tableIdx  : Integer;
-    offset    : Integer;
+    offset    : TWasmInstrList; // offset expression
     funcCount : Integer;
     funcs     : array of TWasmId;
     function AddFunc(idx: integer): integer;
     constructor Create;
+    destructor Destroy; override;
   end;
 
   { TWasmExport }
@@ -277,7 +278,7 @@ end;
 
 { TWasmElement }
 
-function TWasmElement.AddFunc(idx: integer): Integer;
+function TWasmElement.AddFunc(idx: integer): integer;
 begin
   if funcCount = length(funcs) then begin
     if funcCount=0 then SetLength(funcs, 4)
@@ -290,7 +291,14 @@ end;
 
 constructor TWasmElement.Create;
 begin
-  offset:=-1;
+  inherited Create;
+  offset := TWasmInstrList.Create;
+end;
+
+destructor TWasmElement.Destroy;
+begin
+  offset.Free;
+  inherited Destroy;
 end;
 
 { TWasmImport }
@@ -862,10 +870,12 @@ const
   // Finally, when processing table relocations for symbols which
   // have neither an import nor a definition (namely, weakly-undefined
   // function symbols), the value 0 is written out as the value of the relocation.
+  NON_ZEROFFSET_STR = '1';
 begin
   if m.ElementCount=0 then begin
     el := m.AddElement;
-    el.offset := NON_ZEROFFSET;
+    el.offset.AddInstr(INST_i32_const).operandText:=NON_ZEROFFSET_STR;
+    el.offset.AddInstr(INST_END);
   end else
     el := m.GetElement(0);
   Result:=-1;
@@ -875,7 +885,7 @@ begin
   end;
   if Result<0 then
     Result := el.AddFunc(func);
-  Result := Result + el.offset;
+  Result := Result + NON_ZEROFFSET; // todo: THIS IS WRONG
 end;
 
 function RegisterFuncInElem(m: TWasmModule; const funcId: string): integer;