[PATCH 019/188] update reading code section

From 32a478bb569ff45ba1711801760f0bd4cee67d0b Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <skalogryz.lists@gmail.com>
Date: Thu, 26 Sep 2019 17:10:05 -0400

git-svn-id: branches/wasm@46015 -
This commit is contained in:
nickysn 2020-08-03 12:59:04 +00:00
parent 65f3a99ac7
commit bb7fa2e3e4
3 changed files with 44 additions and 10 deletions

View File

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

View File

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

View File

@ -138,6 +138,22 @@ begin
end; end;
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; function ProcessSections(st, dst: TStream; syms: TStrings): Boolean;
var var
dw : LongWord; dw : LongWord;
@ -162,7 +178,9 @@ begin
ps := st.Position+sc.size; 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); ReadExport(st, x);
RenameExport(x, syms); RenameExport(x, syms);
@ -185,6 +203,7 @@ begin
dst.CopyFrom(st, st.Size-st.Position); dst.CopyFrom(st, st.Size-st.Position);
break; // done break; // done
*)
end; end;
{if sc.id=0 then begin {if sc.id=0 then begin
nm := GetName(st); nm := GetName(st);
@ -222,10 +241,12 @@ begin
syms.LoadFromFile(symfn); syms.LoadFromFile(symfn);
ProcessSections(fs, dst, syms); ProcessSections(fs, dst, syms);
fs.Position:=0; if dst.Size>0 then begin
dst.Position:=0; fs.Position:=0;
fs.CopyFrom(dst, dst.Size); dst.Position:=0;
fs.Size:=dst.Size; fs.CopyFrom(dst, dst.Size);
fs.Size:=dst.Size;
end;
finally finally
dst.Free; dst.Free;