Browse Source

[PATCH 019/188] update reading code section

From 32a478bb569ff45ba1711801760f0bd4cee67d0b Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Thu, 26 Sep 2019 17:10:05 -0400

git-svn-id: branches/wasm@46015 -
nickysn 5 years ago
parent
commit
bb7fa2e3e4
3 changed files with 44 additions and 10 deletions
  1. 16 3
      utils/wasmbin/wasmbin.pas
  2. 2 2
      utils/wasmbin/wasmld.lpi
  3. 26 5
      utils/wasmbin/wasmld.lpr

+ 16 - 3
utils/wasmbin/wasmbin.pas

@@ -79,8 +79,7 @@ type
 
   TCodeEntry = record
     locals    : array of TCodeLocalEntry;
-    instCount : integer;
-    instr     : array of TCodeInstr;
+    instBuf   : array of byte;
   end;
 
   TCodeSection = record
@@ -123,6 +122,8 @@ procedure ReadCodeEntry(src: TStream; var en: TCodeEntry);
 // reads the code entry into TCodeEntry structure
 procedure ReadCodeSection(src: TStream; var sc: TCodeSection);
 
+function isUnreachable(const cd: TCodeEntry): Boolean;
+
 
 procedure ReadExportEntry(src: TStream; var ex: TExportEntry);
 // reads the export entry
@@ -132,6 +133,9 @@ procedure WriteExport(const ex: TExportSection; dst: TStream);
 function isWasmStream(st: TStream): Boolean;
 function isWasmFile(const fn: string): Boolean;
 
+const
+  INST_TRAP = $00;
+
 implementation
 
 function ValTypeToStr(id: integer): string;
@@ -201,8 +205,10 @@ var
   //pos : int64;
   cnt : Integer;
   i   : integer;
+  eofs : Int64;
 begin
   sz := ReadU(src);
+  eofs := src.Position+sz;
 
   cnt := ReadU(src);
   SetLength(en.locals, cnt);
@@ -210,7 +216,9 @@ begin
     en.locals[i].count := ReadU(src);
     en.locals[i].valtyp := src.ReadByte;
   end;
-
+  SetLength(en.instBuf, eofs-src.Position);
+  if (length(en.instBuf)>0) then
+    src.Read(en.instBuf[0], length(en.instBuf));
 
 end;
 
@@ -225,6 +233,11 @@ begin
     ReadCodeEntry(src, sc.entries[i]);
 end;
 
+function isUnreachable(const cd: TCodeEntry): Boolean;
+begin
+  Result:=(length(cd.instBuf)>0) and (cd.instBuf[0]=INST_TRAP);
+end;
+
 procedure ReadExportEntry(src: TStream; var ex: TExportEntry);
 begin
   ex.name := ReadName(src);

+ 2 - 2
utils/wasmbin/wasmld.lpi

@@ -1,16 +1,16 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <CONFIG>
   <ProjectOptions>
-    <Version Value="12"/>
+    <Version Value="11"/>
     <PathDelim Value="\"/>
     <General>
       <Flags>
         <MainUnitHasCreateFormStatements Value="False"/>
         <MainUnitHasTitleStatement Value="False"/>
         <MainUnitHasScaledStatement Value="False"/>
-        <CompatibilityMode Value="True"/>
       </Flags>
       <SessionStorage Value="InProjectDir"/>
+      <MainUnit Value="0"/>
       <Title Value="wasmld"/>
       <UseAppBundle Value="False"/>
       <ResourceType Value="res"/>

+ 26 - 5
utils/wasmbin/wasmld.lpr

@@ -138,6 +138,22 @@ begin
   end;
 end;
 
+procedure ParseCode(st: TStream);
+var
+  cnt : integer;
+  cs  : TCodeSection;
+  i : integer;
+begin
+  ReadCodeSection(st, cs);
+  writeln('code=',length(cs.entries));
+  for i:=0 to length(cs.entries)-1 do begin
+    writelN('code[', i,'] ', isUnreachable(cs.entries[i]));
+    writeln(' locals= ', length(cs.entries[i].locals));
+    writeln(' code  = ', length(cs.entries[i].instBuf));
+    writeln(' inst  = ', IntToHex(cs.entries[i].instBuf[0],2),' ',IntToHex(cs.entries[i].instBuf[1],2));
+  end;
+end;
+
 function ProcessSections(st, dst: TStream; syms: TStrings): Boolean;
 var
   dw  : LongWord;
@@ -162,7 +178,9 @@ begin
 
     ps := st.Position+sc.size;
 
-    if sc.id = SECT_EXPORT then begin
+    if sc.id=SECT_CODE then begin
+      ParseCode(st);
+    (*end else if sc.id = SECT_EXPORT then begin
       ReadExport(st, x);
       RenameExport(x, syms);
 
@@ -185,6 +203,7 @@ begin
 
       dst.CopyFrom(st, st.Size-st.Position);
       break; // done
+      *)
     end;
     {if sc.id=0 then begin
       nm := GetName(st);
@@ -222,10 +241,12 @@ begin
       syms.LoadFromFile(symfn);
 
     ProcessSections(fs, dst, syms);
-    fs.Position:=0;
-    dst.Position:=0;
-    fs.CopyFrom(dst, dst.Size);
-    fs.Size:=dst.Size;
+    if dst.Size>0 then begin
+      fs.Position:=0;
+      dst.Position:=0;
+      fs.CopyFrom(dst, dst.Size);
+      fs.Size:=dst.Size;
+    end;
 
   finally
     dst.Free;