Browse Source

[PATCH 043/188] adding export section and instructions list

From 44585af463cda01175b1174b5886b00f74c03c9a Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Wed, 20 Nov 2019 14:13:40 -0500

git-svn-id: branches/wasm@46039 -
nickysn 5 years ago
parent
commit
b726751b3a
1 changed files with 99 additions and 4 deletions
  1. 99 4
      utils/wasmbin/wasmmodule.pas

+ 99 - 4
utils/wasmbin/wasmmodule.pas

@@ -3,7 +3,7 @@ unit wasmmodule;
 interface
 interface
 
 
 uses
 uses
-  Classes, SysUtils, wasmbin;
+  Classes, SysUtils;
 
 
 type
 type
   TWasmParam = class(TObject)
   TWasmParam = class(TObject)
@@ -15,7 +15,7 @@ type
 
 
   TWasmType = class(TObject)
   TWasmType = class(TObject)
   private
   private
-    params : TList;
+    params  : TList;
     results : TList;
     results : TList;
   public
   public
     constructor Create;
     constructor Create;
@@ -29,6 +29,27 @@ type
     function ParamCount: Integer;
     function ParamCount: Integer;
   end;
   end;
 
 
+  TWasmInstr = class(TObject)
+    code        : byte;
+    operandIdx  : string;
+    operandNum  : integer;
+    operandText : string;
+  end;
+
+  { TWasmInstrList }
+
+  TWasmInstrList = class(TObject)
+  private
+    items: TList;
+    function GetItem(i: integer): TWasmInstr;
+  public
+    constructor Create;
+    destructor Destroy; override;
+    function AddInstr(acode: byte = 0): TWasmInstr;
+    function Count: Integer;
+    property Item[i: integer]: TWasmInstr read GetItem; default;
+  end;
+
   { TWasmFunc }
   { TWasmFunc }
 
 
   TWasmFunc = class(TObject)
   TWasmFunc = class(TObject)
@@ -39,6 +60,7 @@ type
     id : string;
     id : string;
     typeIdx : Integer; // if Idx < 0 then type is declared from typeDef
     typeIdx : Integer; // if Idx < 0 then type is declared from typeDef
     typeId  : string;  // if tpyeID='' then type is declared from typeDef
     typeId  : string;  // if tpyeID='' then type is declared from typeDef
+    instr   : TWasmInstrList;
     constructor Create;
     constructor Create;
     destructor Destroy; override;
     destructor Destroy; override;
     function GetInlineType: TWasmType;
     function GetInlineType: TWasmType;
@@ -46,12 +68,22 @@ type
     function LocalsCount: integer;
     function LocalsCount: integer;
   end;
   end;
 
 
+  { TWasmExport }
+
+  TWasmExport = class(TObject)
+    name       : string;
+    exportType : byte;
+    exportNum  : integer;
+    exportIdx  : string;
+  end;
+
   { TWasmModule }
   { TWasmModule }
 
 
   TWasmModule = class(TObject)
   TWasmModule = class(TObject)
   private
   private
-    types      : TList;
-    funcs      : TList;
+    types   : TList;
+    funcs   : TList;
+    exp     : TList;
   public
   public
     constructor Create;
     constructor Create;
     destructor Destroy; override;
     destructor Destroy; override;
@@ -63,6 +95,10 @@ type
     function AddType: TWasmType;
     function AddType: TWasmType;
     function GetTypes(i: integer): TWasmType;
     function GetTypes(i: integer): TWasmType;
     function TypesCount: integer;
     function TypesCount: integer;
+
+    function AddExport: TWasmExport;
+    function GetExport(i: integer): TWasmExport;
+    function ExportCount: integer;
   end;
   end;
 
 
 implementation
 implementation
@@ -76,6 +112,41 @@ begin
   l.Clear;
   l.Clear;
 end;
 end;
 
 
+{ TWasmInstrList }
+
+function TWasmInstrList.GetItem(i: integer): TWasmInstr;
+begin
+  if (i>=0) and (i < items.Count) then
+    Result:=TWasmInstr(items[i])
+  else
+    Result:=nil;
+end;
+
+constructor TWasmInstrList.Create;
+begin
+  inherited Create;
+  items:=TList.Create;
+end;
+
+destructor TWasmInstrList.Destroy;
+begin
+  ClearList(items);
+  items.Free;
+  inherited Destroy;
+end;
+
+function TWasmInstrList.AddInstr(acode: byte = 0): TWasmInstr;
+begin
+  Result:=TWasmInstr.Create;
+  Result.code:=acode;
+  items.Add(Result);
+end;
+
+function TWasmInstrList.Count: Integer;
+begin
+  Result:=items.Count;
+end;
+
 { TWasmType }
 { TWasmType }
 
 
 constructor TWasmType.Create;
 constructor TWasmType.Create;
@@ -147,10 +218,13 @@ begin
   inherited Create;
   inherited Create;
   types := TList.Create;
   types := TList.Create;
   funcs := TList.Create;
   funcs := TList.Create;
+  exp := TList.Create;
 end;
 end;
 
 
 destructor TWasmModule.Destroy;
 destructor TWasmModule.Destroy;
 begin
 begin
+  ClearList(exp);
+  exp.Free;
   ClearList(types);
   ClearList(types);
   types.Free;
   types.Free;
   ClearList(funcs);
   ClearList(funcs);
@@ -196,6 +270,25 @@ begin
   Result:=types.Count;
   Result:=types.Count;
 end;
 end;
 
 
+function TWasmModule.AddExport: TWasmExport;
+begin
+  Result:=TWasmExport.Create;
+  exp.add(Result);
+end;
+
+function TWasmModule.GetExport(i: integer): TWasmExport;
+begin
+  if (i>=0) and (i<exp.Count) then
+    Result:=TWasmExport(exp[i])
+  else
+    Result:=nil;
+end;
+
+function TWasmModule.ExportCount: integer;
+begin
+  Result:=exp.Count;
+end;
+
 { TWasmFunc }
 { TWasmFunc }
 
 
 constructor TWasmFunc.Create;
 constructor TWasmFunc.Create;
@@ -203,6 +296,7 @@ begin
   inherited;
   inherited;
   typeIdx:=-1;
   typeIdx:=-1;
   locals:=TList.Create;
   locals:=TList.Create;
+  instr:=TWasmInstrList.Create;
 end;
 end;
 
 
 destructor TWasmFunc.Destroy;
 destructor TWasmFunc.Destroy;
@@ -210,6 +304,7 @@ begin
   ClearList(locals);
   ClearList(locals);
   locals.Free;
   locals.Free;
   finlineType.Free;
   finlineType.Free;
+  instr.Free;
   inherited Destroy;
   inherited Destroy;
 end;
 end;