Browse Source

[PATCH 136/188] adding parsing global variables

From 3755e71bf33f32deb4b55971149deea6f4adf10f Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Tue, 24 Mar 2020 17:05:24 -0400

git-svn-id: branches/wasm@46132 -
nickysn 5 years ago
parent
commit
b7978044ff
2 changed files with 46 additions and 1 deletions
  1. 2 1
      utils/wasmbin/wasmmodule.pas
  2. 44 0
      utils/wasmbin/watparser.pas

+ 2 - 1
utils/wasmbin/wasmmodule.pas

@@ -116,7 +116,8 @@ type
     id    : TWasmId;
     tp    : byte; // byte;
     value : TWasmInstrList;
-    LinkInfo : TLinkInfo;
+    LinkInfo   : TLinkInfo;
+    ExportInfo : TExportInfo;
     function StartValue: TWasmInstrList;
     destructor Destroy; override;
   end;

+ 44 - 0
utils/wasmbin/watparser.pas

@@ -33,6 +33,8 @@ const
      'elem', 'data', 'offset'
      );
 
+  WasmTypeTokens = [wei32, wei64, wef32, wef64];
+
 //function ConsumeToken(sc: TWatScanner; tk: TWatToken): Boolean;
 function ParseModule(sc: TWatScanner; dst: TWasmModule; var errMsg: string): Boolean; overload;
 function ParseModule(sc: TWatScanner; dst: TWasmModule; out err: TParseResult): Boolean; overload;
@@ -381,6 +383,41 @@ begin
   ConsumeToken(sc, weCloseBrace);
 end;
 
+procedure ParseGlobal(sc: TWatScanner; dst: TWasmGlobal);
+var
+  allowValue: Boolean;
+begin
+  if sc.token = weGlobal then sc.Next;
+
+  allowValue := true;
+  // parsing id
+  if (sc.token in [weIdent, weNumber]) then ParseId(sc, dst.id);
+
+  // import or export
+  if (sc.token=weOpenBrace) then begin
+    sc.Next;
+    if sc.token=weImport then begin
+      // import
+      allowValue := false;
+    end else if sc.token=weExport then begin
+     // export
+    end else
+      ErrorExpectButFound(sc, 'import or export')
+  end;
+
+  if sc.token in WasmTypeTokens then begin
+    TokenTypeToValType(sc.token, dst.tp);
+    sc.Next;
+  end;
+
+  if allowValue and (sc.token = weOpenBrace) then begin
+    sc.Next;
+    ParseInstrList(sc, dst.StartValue);
+    ConsumeToken(sc, weCloseBrace);
+  end;
+  ConsumeToken(sc, weCloseBrace);
+end;
+
 procedure ParseData(sc: TWatScanner; dst: TWasmData);
 var
   l : integer;
@@ -549,6 +586,7 @@ var
   f       : TWasmFunc;
   imp     : TWasmImport;
   m       : TWasmMemory;
+  g       : TWasmGlobal;
 begin
   if not ConsumeOpenToken(sc, weModule) then
     ErrorExpectButFound(sc, 'module');
@@ -596,6 +634,12 @@ begin
           symlist.Clear;
           ParseTypeDef(sc, dst.AddType);
         end;
+        weGlobal: begin
+          g:=dst.AddGlobal;
+          symlist.ToLinkInfo(g.LinkInfo);
+          symlist.Clear;
+          ParseGlobal(sc, g);
+        end;
       else
         ErrorExpectButFound(sc, 'func', TokenStr[sc.token]);
       end;