Browse Source

[PATCH 160/188] normalizing global variables

From 1fab7f9018a9551ea28489dbcb87bb5b8fd25b2b Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Fri, 27 Mar 2020 09:33:39 -0400

git-svn-id: branches/wasm@46156 -
nickysn 5 years ago
parent
commit
1631be00c8
1 changed files with 22 additions and 0 deletions
  1. 22 0
      utils/wasmbin/wasmnormalize.pas

+ 22 - 0
utils/wasmbin/wasmnormalize.pas

@@ -96,6 +96,19 @@ begin
           end;
           end;
         end;
         end;
 
 
+        INST_global_get, INST_global_set:
+        begin
+          if not Assigned(f) then begin
+            Result:=false;
+            Exit;
+          end;
+
+          if (ci.operandIdx<>'') and (ci.operandNum<0) then begin
+            j:=FindGlobal(m, ci.operandIdx);
+            ci.operandNum:=j;
+          end;
+        end;
+
         INST_call:
         INST_call:
         begin
         begin
           if (ci.operandIdx<>'') and (ci.operandNum<0) then
           if (ci.operandIdx<>'') and (ci.operandNum<0) then
@@ -166,6 +179,14 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure NormalizeGlobals(m: TWasmModule);
+var
+  i : integer;
+begin
+  for i:=0 to m.GlobalCount-1 do
+    m.GetGlobal(i).id.idNum:=i;
+end;
+
 procedure NormalizeTable(m: TWasmModule);
 procedure NormalizeTable(m: TWasmModule);
 var
 var
   i  : integer;
   i  : integer;
@@ -241,6 +262,7 @@ var
   fnIdx : Integer;
   fnIdx : Integer;
 begin
 begin
   fnIdx := 0;
   fnIdx := 0;
+  NormalizeGlobals(m);
   NormalizeTable(m);
   NormalizeTable(m);
   NormalizeElems(m);
   NormalizeElems(m);
   NormalizeImport(m, fnIdx);
   NormalizeImport(m, fnIdx);