Browse Source

[PATCH 135/188] adding support for module globals

From 019f0a289446f13f4f6575c87af0a8d1c97ed7d5 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Tue, 24 Mar 2020 16:52:07 -0400

git-svn-id: branches/wasm@46131 -
nickysn 5 years ago
parent
commit
9f9ff7a56c
1 changed files with 55 additions and 1 deletions
  1. 55 1
      utils/wasmbin/wasmmodule.pas

+ 55 - 1
utils/wasmbin/wasmmodule.pas

@@ -109,6 +109,18 @@ type
     property Item[i: integer]: TWasmInstr read GetItem; default;
     property Item[i: integer]: TWasmInstr read GetItem; default;
   end;
   end;
 
 
+  { TWasmGlobal }
+
+  TWasmGlobal = class(TObject)
+  public
+    id    : TWasmId;
+    tp    : byte; // byte;
+    value : TWasmInstrList;
+    LinkInfo : TLinkInfo;
+    function StartValue: TWasmInstrList;
+    destructor Destroy; override;
+  end;
+
   { TWasmFunc }
   { TWasmFunc }
 
 
   TWasmFunc = class(TObject)
   TWasmFunc = class(TObject)
@@ -199,6 +211,7 @@ type
 
 
   TWasmModule = class(TObject)
   TWasmModule = class(TObject)
   private
   private
+    globals : TList;
     memes   : TList;
     memes   : TList;
     imports : TList;
     imports : TList;
     types   : TList;
     types   : TList;
@@ -242,6 +255,10 @@ type
     function AddData: TWasmData;
     function AddData: TWasmData;
     function GetData(i: integer): TWasmData;
     function GetData(i: integer): TWasmData;
     function DataCount: Integer;
     function DataCount: Integer;
+
+    function AddGlobal: TWasmGlobal;
+    function GetGlobal(i: integer): TWasmGlobal;
+    function GlobalCount: Integer;
   end;
   end;
 
 
 // making binary friendly. finding proper "nums" for each symbol "index"
 // making binary friendly. finding proper "nums" for each symbol "index"
@@ -326,6 +343,21 @@ begin
   l.Clear;
   l.Clear;
 end;
 end;
 
 
+{ TWasmGlobal }
+
+function TWasmGlobal.StartValue: TWasmInstrList;
+begin
+  if not Assigned(value) then
+    value:=TWasmInstrList.Create;
+  Result:=value;
+end;
+
+destructor TWasmGlobal.Destroy;
+begin
+  value.Free;
+  inherited Destroy;
+end;
+
 { TWasmTable }
 { TWasmTable }
 
 
 function TWasmTable.AddElem: TWasmElement;
 function TWasmTable.AddElem: TWasmElement;
@@ -364,7 +396,7 @@ var
 begin
 begin
   w.id:='';
   w.id:='';
   w.idNum:=idx;
   w.idNum:=idx;
-  AddFuncId(w);
+  Result := AddFuncId(w);
 end;
 end;
 
 
 function TWasmElement.AddFuncId(const idx: TWasmID): integer;
 function TWasmElement.AddFuncId(const idx: TWasmID): integer;
@@ -574,6 +606,7 @@ end;
 constructor TWasmModule.Create;
 constructor TWasmModule.Create;
 begin
 begin
   inherited Create;
   inherited Create;
+  globals := TList.Create;
   types := TList.Create;
   types := TList.Create;
   funcs := TList.Create;
   funcs := TList.Create;
   exp := TList.Create;
   exp := TList.Create;
@@ -602,6 +635,8 @@ begin
   types.Free;
   types.Free;
   ClearList(funcs);
   ClearList(funcs);
   funcs.Free;
   funcs.Free;
+  ClearList(globals);
+  globals.Free;
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 
@@ -757,6 +792,25 @@ begin
   Result:=data.Count;
   Result:=data.Count;
 end;
 end;
 
 
+function TWasmModule.AddGlobal: TWasmGlobal;
+begin
+  Result:=TWasmGlobal.Create;
+  globals.Add(Result);
+end;
+
+function TWasmModule.GetGlobal(i: integer): TWasmGlobal;
+begin
+  if (i>=0) and (i<globals.Count) then
+    Result:=TWasmGlobal(globals[i])
+  else
+    Result:=nil;
+end;
+
+function TWasmModule.GlobalCount: Integer;
+begin
+  Result:=globals.Count;
+end;
+
 { TWasmFunc }
 { TWasmFunc }
 
 
 constructor TWasmFunc.Create;
 constructor TWasmFunc.Create;