From 25327812a66b33d18dfc80239a39f2ed7d375a07 Mon Sep 17 00:00:00 2001 From: nickysn Date: Mon, 3 Aug 2020 13:00:06 +0000 Subject: [PATCH] [PATCH 078/188] adding support for the import section normalization From 6d667cdb880b17716dc1ad1b995082dbcbd36ff2 Mon Sep 17 00:00:00 2001 From: Dmitry Boyarintsev Date: Mon, 9 Mar 2020 08:47:34 -0400 git-svn-id: branches/wasm@46074 - --- utils/wasmbin/wasmmodule.pas | 71 ++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/utils/wasmbin/wasmmodule.pas b/utils/wasmbin/wasmmodule.pas index 86a6dd96a7..1fa61a9e6c 100644 --- a/utils/wasmbin/wasmmodule.pas +++ b/utils/wasmbin/wasmmodule.pas @@ -98,6 +98,7 @@ type public LinkInfo : TLinkInfo; id : string; + idInt : Integer; // reference number (after Normalization) instr : TWasmInstrList; functype : TWasmFuncType; @@ -142,6 +143,7 @@ type destructor Destroy; override; function AddImport: TWasmImport; + function GetImport(i: integer): TWasmImport; function ImportCount: Integer; function AddFunc: TWasmFunc; @@ -420,6 +422,14 @@ begin imports.Add(Result); end; +function TWasmModule.GetImport(i: integer): TWasmImport; +begin + if (i>=0) and (i'') and (ci.operandNum<0) then + if (ci.operandIdx<>'') and ( ci.operandNum<0) then ci.operandNum:=FindFunc(m,ci.operandIdx); end; end; @@ -620,24 +640,53 @@ begin l.AddInstr(INST_END); end; + +procedure NormalizeFuncType(m: TWasmModule; fn : TWasmFuncType); +begin + if fn.isNumOrIdx then begin + if fn.typeIdx<>'' then + fn.typeNum:=FindFuncType(m, fn.typeIdx); + end else + fn.typeNum:=RegisterFuncType(m, fn); +end; + +procedure NormalizeImport(m: TWasmModule; var fnIdx: Integer); +var + i : integer; + im : TWasmImport; +begin + fnIdx := 0; + for i:=0 to m.ImportCount-1 do begin + im := m.GetImport(i); + if Assigned(im.fn) then begin + im.fn.idInt:=fnIdx; + NormalizeFuncType(m, im.fn.functype); + inc(fnIdx); + end; + end; +end; + // normalizing reference procedure Normalize(m: TWasmModule); var - i : integer; - f : TWasmFunc; - x : TWasmExport; + i : integer; + f : TWasmFunc; + x : TWasmExport; + fnIdx : Integer; begin + fnIdx := 0; + NormalizeImport(m, fnIdx); + for i:=0 to m.FuncCount-1 do begin f:=m.GetFunc(i); - if f.functype.isNumOrIdx then begin - if f.functype.typeIdx<>'' then - f.functype.typeNum:=FindFuncType(m, f.functype.typeIdx); - end else - f.functype.typeNum:=RegisterFuncType(m, f.functype); + f.idInt := fnIdx; + NormalizeFuncType(m, f.functype); // finding the reference in functions // populating "nums" where string "index" is used NormalizeInst(m, f, f.instr); + + inc(fnIdx); end; // normalizing exports