Browse Source

[PATCH 122/188] adding parsing of jump vectors for br_table

From 90a6eded0c3af6a418c7949c3589258f65846931 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Mon, 23 Mar 2020 15:43:23 -0400

git-svn-id: branches/wasm@46118 -
nickysn 5 years ago
parent
commit
7facec1371
2 changed files with 30 additions and 6 deletions
  1. 4 0
      utils/wasmbin/wasmmodule.pas
  2. 26 6
      utils/wasmbin/watparser.pas

+ 4 - 0
utils/wasmbin/wasmmodule.pas

@@ -85,6 +85,10 @@ type
     hasRelocIdx : Boolean;
     relocIdx    : integer;
     relocType   : Byte;
+
+    vecTableCount : Integer;
+    vecTable      : array of TWasmId;
+
     function addInstType: TWasmFuncType;
     constructor Create;
     destructor Destroy; override;

+ 26 - 6
utils/wasmbin/watparser.pas

@@ -144,6 +144,11 @@ begin
   if Result then sc.Next;
 end;
 
+function ParseId(sc: TWatScanner; var id: TWasmId): boolean;
+begin
+  Result := ParseNumOfId(sc, id.idNum, id.id);
+end;
+
 function TokenTypeToValType(t: TWatToken; out tp: byte): Boolean;
 begin
   Result:=true;
@@ -260,7 +265,27 @@ begin
       end;
 
       //ip2Leb,  // memory arguments, ask for offset + align
-      //ipTable,   // a complex structure... used for br_table only
+      ipJumpVec: 
+      begin
+        while (sc.token in [weNumber, weIdent]) do begin
+          if (ci.vecTableCount = length(ci.vecTable)) then begin
+            if (ci.vecTableCount = 0) then SetLength(ci.vecTable, 2)
+            else SetLength(ci.vecTable, ci.vecTableCount * 2);
+          end;
+          ParseId(sc, ci.vecTable[ci.vecTableCount]);
+          inc(ci.vecTableCount);
+        end;
+
+        if (ci.vecTableCount<2) then begin
+          ErrorExpectButFound(sc, 'label');
+          Exit;
+        end;
+
+        dec(ci.vecTableCount);
+        ci.operandIdx := ci.vecTable[ci.vecTableCount].id;
+        ci.operandNum := ci.vecTable[ci.vecTableCount].idNum;
+      end;
+
       ipResType:  // result type used for blocks, such as If, block or loop
       begin
         if sc.token = weIdent then begin
@@ -393,11 +418,6 @@ begin
   sc.Next;
 end;
 
-function ParseId(sc: TWatScanner; var id: TWasmId): boolean;
-begin
-  Result := ParseNumOfId(sc, id.idNum, id.id);
-end;
-
 procedure ParseTable(sc: TWatScanner; dst: TWasmTable);
 begin
   sc.Next;