Browse Source

[PATCH 175/188] update normalization of imported units

From b2e45d583af68ff11052b29125b00331ad7a4359 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Wed, 8 Apr 2020 10:32:10 -0400

git-svn-id: branches/wasm@46171 -
nickysn 5 years ago
parent
commit
e13fb1fd1b
1 changed files with 29 additions and 7 deletions
  1. 29 7
      utils/wasmbin/wasmnormalize.pas

+ 29 - 7
utils/wasmbin/wasmnormalize.pas

@@ -165,18 +165,31 @@ begin
     fn.typeNum:=RegisterFuncType(m, fn);
 end;
 
-procedure NormalizeImport(m: TWasmModule; var fnIdx: Integer);
+procedure NormalizeImport(m: TWasmModule; out fnIdx: Integer;
+  out memIdx: Integer; out globIdx: Integer; out tblIdx : Integer);
 var
   i  : integer;
   im : TWasmImport;
 begin
   fnIdx := 0;
+  memIdx := 0;
+  globIdx := 0;
+  tblIdx := 0;
   for i:=0 to m.ImportCount-1 do begin
     im := m.GetImport(i);
     if Assigned(im.fn) then begin
       im.fn.idNum:=fnIdx;
       NormalizeFuncType(m, im.fn.functype);
       inc(fnIdx);
+    end else if Assigned(im.mem) then begin
+      im.mem.id.idNum := memIdx;
+      inc(memIdx);
+    end else if Assigned(im.glob) then begin
+      im.glob.id.idNum := globIdx;
+      inc(globIdx);
+    end else if Assigned(im.table) then begin
+      im.table.id.idNum := tblIdx;
+      inc(tblIdx);
     end;
   end;
 end;
@@ -258,16 +271,20 @@ end;
 // normalizing reference
 procedure Normalize(m: TWasmModule);
 var
-  i     : integer;
-  f     : TWasmFunc;
-  x     : TWasmExport;
-  fnIdx : Integer;
+  i       : integer;
+  f       : TWasmFunc;
+  x       : TWasmExport;
+  fnIdx   : Integer;
+  memIdx  : Integer;
+  globIdx : Integer;
+  tblIdx  : Integer;
+  g       : TWasmGlobal;
 begin
   fnIdx := 0;
   NormalizeGlobals(m);
   NormalizeTable(m);
   NormalizeElems(m);
-  NormalizeImport(m, fnIdx);
+  NormalizeImport(m, fnIdx, memIdx, globIdx, tblIdx);
   NormalizeTableLimit(m);
 
   for i:=0 to m.FuncCount-1 do begin
@@ -280,7 +297,12 @@ begin
   end;
 
   for i:=0 to m.GlobalCount-1 do
-    NormalizeInst(m, nil, m.GetGlobal(i).StartValue);
+  begin
+    g := m.GetGlobal(i);
+    g.id.idNum := globIdx;
+    inc(globIdx);
+    NormalizeInst(m, nil, g.StartValue);
+  end;
 
   // normalizing function body
   for i:=0 to m.FuncCount-1 do begin